Skip to content

Commit

Permalink
Support catalog pre event for Gravitino server apache#5549
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxiaojian committed Nov 15, 2024
1 parent fe041a9 commit c664e87
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@
import org.apache.gravitino.exceptions.NonEmptyEntityException;
import org.apache.gravitino.listener.api.event.AlterCatalogEvent;
import org.apache.gravitino.listener.api.event.AlterCatalogFailureEvent;
import org.apache.gravitino.listener.api.event.AlterCatalogPreEvent;
import org.apache.gravitino.listener.api.event.CreateCatalogEvent;
import org.apache.gravitino.listener.api.event.CreateCatalogFailureEvent;
import org.apache.gravitino.listener.api.event.CreateCatalogPreEvent;
import org.apache.gravitino.listener.api.event.DropCatalogEvent;
import org.apache.gravitino.listener.api.event.DropCatalogFailureEvent;
import org.apache.gravitino.listener.api.event.DropCatalogPreEvent;
import org.apache.gravitino.listener.api.event.ListCatalogEvent;
import org.apache.gravitino.listener.api.event.ListCatalogFailureEvent;
import org.apache.gravitino.listener.api.event.ListCatalogPreEvent;
import org.apache.gravitino.listener.api.event.LoadCatalogEvent;
import org.apache.gravitino.listener.api.event.LoadCatalogFailureEvent;
import org.apache.gravitino.listener.api.event.LoadCatalogPreEvent;
import org.apache.gravitino.listener.api.info.CatalogInfo;
import org.apache.gravitino.utils.PrincipalUtils;

Expand Down Expand Up @@ -68,6 +73,7 @@ public CatalogEventDispatcher(EventBus eventBus, CatalogDispatcher dispatcher) {

@Override
public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {
eventBus.dispatchEvent(new ListCatalogPreEvent(PrincipalUtils.getCurrentUserName(), namespace));
try {
NameIdentifier[] nameIdentifiers = dispatcher.listCatalogs(namespace);
eventBus.dispatchEvent(new ListCatalogEvent(PrincipalUtils.getCurrentUserName(), namespace));
Expand All @@ -81,6 +87,7 @@ public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeE

@Override
public Catalog[] listCatalogsInfo(Namespace namespace) throws NoSuchMetalakeException {
eventBus.dispatchEvent(new ListCatalogPreEvent(PrincipalUtils.getCurrentUserName(), namespace));
try {
Catalog[] catalogs = dispatcher.listCatalogsInfo(namespace);
eventBus.dispatchEvent(new ListCatalogEvent(PrincipalUtils.getCurrentUserName(), namespace));
Expand All @@ -94,6 +101,7 @@ public Catalog[] listCatalogsInfo(Namespace namespace) throws NoSuchMetalakeExce

@Override
public Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {
eventBus.dispatchEvent(new LoadCatalogPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
Catalog catalog = dispatcher.loadCatalog(ident);
eventBus.dispatchEvent(
Expand All @@ -115,6 +123,10 @@ public Catalog createCatalog(
String comment,
Map<String, String> properties)
throws NoSuchMetalakeException, CatalogAlreadyExistsException {
CatalogInfo catalogInfo =
new CatalogInfo(ident.name(), type, provider, comment, properties, null);
eventBus.dispatchEvent(
new CreateCatalogPreEvent(PrincipalUtils.getCurrentUserName(), ident, catalogInfo));
try {
Catalog catalog = dispatcher.createCatalog(ident, type, provider, comment, properties);
eventBus.dispatchEvent(
Expand All @@ -134,6 +146,8 @@ public Catalog createCatalog(
@Override
public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)
throws NoSuchCatalogException, IllegalArgumentException {
eventBus.dispatchEvent(
new AlterCatalogPreEvent(PrincipalUtils.getCurrentUserName(), ident, changes));
try {
Catalog catalog = dispatcher.alterCatalog(ident, changes);
eventBus.dispatchEvent(
Expand All @@ -150,6 +164,7 @@ public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)
@Override
public boolean dropCatalog(NameIdentifier ident, boolean force)
throws NonEmptyEntityException, CatalogInUseException {
eventBus.dispatchEvent(new DropCatalogPreEvent(PrincipalUtils.getCurrentUserName(), ident));
try {
boolean isExists = dispatcher.dropCatalog(ident, force);
eventBus.dispatchEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.CatalogChange;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event triggered upon the successful creation of a catalog. */
@DeveloperApi
public final class AlterCatalogPreEvent extends CatalogPreEvent {
private final CatalogChange[] catalogChanges;

/**
* Constructs an instance of {@code AlterCatalogEvent}, encapsulating the key details about the
* successful alteration of a catalog.
*
* @param user The username of the individual responsible for initiating the catalog alteration.
* @param identifier The unique identifier of the altered catalog, serving as a clear reference
* point for the catalog in question.
* @param catalogChanges An array of {@link CatalogChange} objects representing the specific
* changes applied to the catalog during the alteration process.
* @param updatedCatalogInfo The post-alteration state of the catalog.
*/
public AlterCatalogPreEvent(
String user, NameIdentifier identifier, CatalogChange[] catalogChanges) {
super(user, identifier);
this.catalogChanges = catalogChanges.clone();
}

/**
* Retrieves the specific changes that were made to the catalog during the alteration process.
*
* @return An array of {@link CatalogChange} objects detailing each modification applied to the
* catalog.
*/
public CatalogChange[] catalogChanges() {
return catalogChanges;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents a pre-event for catalog operations. */
@DeveloperApi
public abstract class CatalogPreEvent extends PreEvent {
protected CatalogPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;
import org.apache.gravitino.listener.api.info.CatalogInfo;

/** Represents an event that is activated upon the successful creation of a catalog. */
@DeveloperApi
public class CreateCatalogPreEvent extends CatalogPreEvent {
private final CatalogInfo createdCatalogInfo;

/**
* Constructs an instance of {@code CreateCatalogEvent}, capturing essential details about the
* successful creation of a catalog.
*
* @param user The username of the individual who initiated the catalog creation.
* @param identifier The unique identifier of the catalog that was created.
* @param createdCatalogInfo The final state of the catalog post-creation.
*/
public CreateCatalogPreEvent(
String user, NameIdentifier identifier, CatalogInfo createdCatalogInfo) {
super(user, identifier);
this.createdCatalogInfo = createdCatalogInfo;
}

/**
* Provides the final state of the catalog as it is presented to the user following the successful
* creation.
*
* @return A {@link CatalogInfo} object that encapsulates the detailed characteristics of the
* newly created catalog.
*/
public CatalogInfo createdCatalogInfo() {
return createdCatalogInfo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event that is generated after a catalog is successfully dropped. */
@DeveloperApi
public final class DropCatalogPreEvent extends CatalogPreEvent {
public DropCatalogPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event that is triggered upon the successful list of catalogs. */
@DeveloperApi
public final class ListCatalogPreEvent extends CatalogPreEvent {
private final Namespace namespace;

/**
* Constructs an instance of {@code ListCatalogEvent}.
*
* @param user The username of the individual who initiated the catalog listing.
* @param namespace The namespace from which catalogs were listed.
*/
public ListCatalogPreEvent(String user, Namespace namespace) {
super(user, NameIdentifier.of(namespace.levels()));
this.namespace = namespace;
}

/**
* Provides the namespace associated with this event.
*
* @return A {@link Namespace} instance from which catalogs were listed.
*/
public Namespace namespace() {
return namespace;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents an event triggered upon the successful loading of a catalog. */
@DeveloperApi
public final class LoadCatalogPreEvent extends CatalogPreEvent {

/**
* Constructs an instance of {@code LoadCatalogEvent}.
*
* @param user The username of the individual who initiated the catalog loading.
* @param identifier The unique identifier of the catalog that was loaded.
* @param loadedCatalogInfo The state of the catalog post-loading.
*/
public LoadCatalogPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}
}
Loading

0 comments on commit c664e87

Please sign in to comment.