World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#5549]feat(core):support catalog pre event for Gravitino server (#5591)
Xiaojian Sun977758d87174
diff --git a/core/src/main/java/org/apache/gravitino/listener/CatalogEventDispatcher.java b/core/src/main/java/org/apache/gravitino/listener/CatalogEventDispatcher.javaindex ff70d4cffd..782ef7df4e 100644--- a/core/src/main/java/org/apache/gravitino/listener/CatalogEventDispatcher.java+++ b/core/src/main/java/org/apache/gravitino/listener/CatalogEventDispatcher.java@@ -33,14 +33,19 @@ import org.apache.gravitino.exceptions.NoSuchMetalakeException;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;@@ -68,6 +73,7 @@ public class CatalogEventDispatcher implements CatalogDispatcher {@Overridepublic 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));@@ -81,6 +87,7 @@ public class CatalogEventDispatcher implements CatalogDispatcher {@Overridepublic 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));@@ -94,6 +101,7 @@ public class CatalogEventDispatcher implements CatalogDispatcher {@Overridepublic Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {+ eventBus.dispatchEvent(new LoadCatalogPreEvent(PrincipalUtils.getCurrentUserName(), ident));try {Catalog catalog = dispatcher.loadCatalog(ident);eventBus.dispatchEvent(@@ -115,6 +123,10 @@ public class CatalogEventDispatcher implements CatalogDispatcher {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(@@ -134,6 +146,8 @@ public class CatalogEventDispatcher implements CatalogDispatcher {@Overridepublic 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(@@ -150,6 +164,7 @@ public class CatalogEventDispatcher implements CatalogDispatcher {@Overridepublic 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(diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/AlterCatalogPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterCatalogPreEvent.javanew file mode 100644index 0000000000..a8786adf48--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterCatalogPreEvent.java@@ -0,0 +1,46 @@+/*+ * 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 before altering a catalog. */+@DeveloperApi+public final class AlterCatalogPreEvent extends CatalogPreEvent {+ private final CatalogChange[] catalogChanges;++ public AlterCatalogPreEvent(+ String user, NameIdentifier identifier, CatalogChange[] catalogChanges) {+ super(user, identifier);+ this.catalogChanges = catalogChanges;+ }++ /**+ * 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;+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/CatalogPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/CatalogPreEvent.javanew file mode 100644index 0000000000..e1fcd51cb1--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/CatalogPreEvent.java@@ -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);+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/CreateCatalogPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/CreateCatalogPreEvent.javanew file mode 100644index 0000000000..9abc1cbbfe--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/CreateCatalogPreEvent.java@@ -0,0 +1,46 @@+/*+ * 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 triggered before creating a catalog. */+@DeveloperApi+public class CreateCatalogPreEvent extends CatalogPreEvent {+ private final CatalogInfo createCatalogRequest;++ public CreateCatalogPreEvent(+ String user, NameIdentifier identifier, CatalogInfo createCatalogRequest) {+ super(user, identifier);+ this.createCatalogRequest = createCatalogRequest;+ }++ /**+ * Retrieves the create catalog request.+ *+ * @return A {@link CatalogInfo} instance encapsulating the comprehensive details of create+ * catalog request.+ */+ public CatalogInfo createCatalogRequest() {+ return createCatalogRequest;+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/DropCatalogPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/DropCatalogPreEvent.javanew file mode 100644index 0000000000..f359c99e71--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/DropCatalogPreEvent.java@@ -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 before a catalog is dropped. */+@DeveloperApi+public final class DropCatalogPreEvent extends CatalogPreEvent {+ public DropCatalogPreEvent(String user, NameIdentifier identifier) {+ super(user, identifier);+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/ListCatalogPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/ListCatalogPreEvent.javanew file mode 100644index 0000000000..d430b80ac0--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/ListCatalogPreEvent.java@@ -0,0 +1,44 @@+/*+ * 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 before listing of catalogs within a namespace. */+@DeveloperApi+public final class ListCatalogPreEvent extends CatalogPreEvent {+ private final Namespace namespace;++ 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;+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/LoadCatalogPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/LoadCatalogPreEvent.javanew file mode 100644index 0000000000..b73552be93--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/LoadCatalogPreEvent.java@@ -0,0 +1,32 @@+/*+ * 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 before loading a catalog. */+@DeveloperApi+public final class LoadCatalogPreEvent extends CatalogPreEvent {++ public LoadCatalogPreEvent(String user, NameIdentifier identifier) {+ super(user, identifier);+ }+}diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestCatalogEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestCatalogEvent.javaindex 7b7cb9e3c4..b1ac4eba65 100644--- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestCatalogEvent.java+++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestCatalogEvent.java@@ -71,6 +71,12 @@ public class TestCatalogEvent {Assertions.assertEquals(CreateCatalogEvent.class, event.getClass());CatalogInfo catalogInfo = ((CreateCatalogEvent) event).createdCatalogInfo();checkCatalogInfo(catalogInfo, catalog);++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(CreateCatalogPreEvent.class, preEvent.getClass());+ CatalogInfo preCatalogInfo = ((CreateCatalogPreEvent) preEvent).createCatalogRequest();+ checkCatalogInfo(preCatalogInfo, catalog);}@Test@@ -82,6 +88,10 @@ public class TestCatalogEvent {Assertions.assertEquals(LoadCatalogEvent.class, event.getClass());CatalogInfo catalogInfo = ((LoadCatalogEvent) event).loadedCatalogInfo();checkCatalogInfo(catalogInfo, catalog);++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(LoadCatalogPreEvent.class, preEvent.getClass());}@Test@@ -97,6 +107,13 @@ public class TestCatalogEvent {CatalogChange[] catalogChanges = ((AlterCatalogEvent) event).catalogChanges();Assertions.assertEquals(1, catalogChanges.length);Assertions.assertEquals(catalogChange, catalogChanges[0]);++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(AlterCatalogPreEvent.class, preEvent.getClass());+ CatalogChange[] preCatalogChanges = ((AlterCatalogPreEvent) preEvent).catalogChanges();+ Assertions.assertEquals(1, preCatalogChanges.length);+ Assertions.assertEquals(catalogChange, preCatalogChanges[0]);}@Test@@ -107,6 +124,10 @@ public class TestCatalogEvent {Assertions.assertEquals(identifier, event.identifier());Assertions.assertEquals(DropCatalogEvent.class, event.getClass());Assertions.assertEquals(true, ((DropCatalogEvent) event).isExists());++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(DropCatalogPreEvent.class, preEvent.getClass());}@Test@@ -117,6 +138,11 @@ public class TestCatalogEvent {Assertions.assertEquals(namespace.toString(), event.identifier().toString());Assertions.assertEquals(ListCatalogEvent.class, event.getClass());Assertions.assertEquals(namespace, ((ListCatalogEvent) event).namespace());++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(namespace.toString(), preEvent.identifier().toString());+ Assertions.assertEquals(ListCatalogPreEvent.class, preEvent.getClass());+ Assertions.assertEquals(namespace, ((ListCatalogPreEvent) preEvent).namespace());}@Test@@ -127,6 +153,11 @@ public class TestCatalogEvent {Assertions.assertEquals(namespace.toString(), event.identifier().toString());Assertions.assertEquals(ListCatalogEvent.class, event.getClass());Assertions.assertEquals(namespace, ((ListCatalogEvent) event).namespace());++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(namespace.toString(), preEvent.identifier().toString());+ Assertions.assertEquals(ListCatalogPreEvent.class, preEvent.getClass());+ Assertions.assertEquals(namespace, ((ListCatalogPreEvent) preEvent).namespace());}@Testdiff --git a/docs/gravitino-server-config.md b/docs/gravitino-server-config.mdindex f4f274dc75..b367c0ef25 100644--- a/docs/gravitino-server-config.md+++ b/docs/gravitino-server-config.md@@ -129,11 +129,12 @@ Gravitino triggers a pre-event before the operation, a post-event after the comp##### Pre-event-| Operation type | Pre-event | Since Version |-|-------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|-| Iceberg REST server table operation | `IcebergCreateTablePreEvent`, `IcebergUpdateTablePreEvent`, `IcebergDropTablePreEvent`, `IcebergLoadTablePreEvent`, `IcebergListTablePreEvent`, `IcebergTableExistsPreEvent`, `IcebergRenameTablePreEvent` | 0.7.0-incubating |-| Gravitino server table operation | `CreateTablePreEvent`, `AlterTablePreEvent`, `DropTablePreEvent`, `PurgeTablePreEvent`, `LoadTablePreEvent`, `ListTablePreEvent` | 0.8.0-incubating |-| Gravitino server schema operation | `CreareSchemaPreEvent`, `AlterSchemaPreEvent`, `DropSchemaPreEvent`, `LoadSchemaPreEvent`, `ListSchemaPreEvent` | 0.8.0-incubating |+| Operation type | Pre-event | Since Version |+|-------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|+| Iceberg REST server table operation | `IcebergCreateTablePreEvent`, `IcebergUpdateTablePreEvent`, `IcebergDropTablePreEvent`, `IcebergLoadTablePreEvent`, `IcebergListTablePreEvent`, `IcebergTableExistsPreEvent`, `IcebergRenameTablePreEvent` | 0.7.0-incubating |+| Gravitino server table operation | `CreateTablePreEvent`, `AlterTablePreEvent`, `DropTablePreEvent`, `PurgeTablePreEvent`, `LoadTablePreEvent`, `ListTablePreEvent` | 0.8.0-incubating |+| Gravitino server schema operation | `CreateSchemaPreEvent`, `AlterSchemaPreEvent`, `DropSchemaPreEvent`, `LoadSchemaPreEvent`, `ListSchemaPreEvent` | 0.8.0-incubating |+| Gravitino server catalog operation | `CreateCatalogPreEvent`, `AlterCatalogPreEvent`, `DropCatalogPreEvent`, `LoadCatalogPreEvent`, `ListCatalogPreEvent` | 0.8.0-incubating |#### Event listener plugin