World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#5547] feat(core): support schema pre event for Gravitino server (#5559)
JUN3cd372f10a70
diff --git a/core/src/main/java/org/apache/gravitino/listener/SchemaEventDispatcher.java b/core/src/main/java/org/apache/gravitino/listener/SchemaEventDispatcher.javaindex 69e0e49b64..ffbdf28c1f 100644--- a/core/src/main/java/org/apache/gravitino/listener/SchemaEventDispatcher.java+++ b/core/src/main/java/org/apache/gravitino/listener/SchemaEventDispatcher.java@@ -32,14 +32,19 @@ import org.apache.gravitino.exceptions.NonEmptySchemaException;import org.apache.gravitino.exceptions.SchemaAlreadyExistsException;import org.apache.gravitino.listener.api.event.AlterSchemaEvent;import org.apache.gravitino.listener.api.event.AlterSchemaFailureEvent;+import org.apache.gravitino.listener.api.event.AlterSchemaPreEvent;import org.apache.gravitino.listener.api.event.CreateSchemaEvent;import org.apache.gravitino.listener.api.event.CreateSchemaFailureEvent;+import org.apache.gravitino.listener.api.event.CreateSchemaPreEvent;import org.apache.gravitino.listener.api.event.DropSchemaEvent;import org.apache.gravitino.listener.api.event.DropSchemaFailureEvent;+import org.apache.gravitino.listener.api.event.DropSchemaPreEvent;import org.apache.gravitino.listener.api.event.ListSchemaEvent;import org.apache.gravitino.listener.api.event.ListSchemaFailureEvent;+import org.apache.gravitino.listener.api.event.ListSchemaPreEvent;import org.apache.gravitino.listener.api.event.LoadSchemaEvent;import org.apache.gravitino.listener.api.event.LoadSchemaFailureEvent;+import org.apache.gravitino.listener.api.event.LoadSchemaPreEvent;import org.apache.gravitino.listener.api.info.SchemaInfo;import org.apache.gravitino.utils.PrincipalUtils;@@ -67,6 +72,7 @@ public class SchemaEventDispatcher implements SchemaDispatcher {@Overridepublic NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchCatalogException {+ eventBus.dispatchEvent(new ListSchemaPreEvent(PrincipalUtils.getCurrentUserName(), namespace));try {NameIdentifier[] nameIdentifiers = dispatcher.listSchemas(namespace);eventBus.dispatchEvent(new ListSchemaEvent(PrincipalUtils.getCurrentUserName(), namespace));@@ -86,6 +92,9 @@ public class SchemaEventDispatcher implements SchemaDispatcher {@Overridepublic Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties)throws NoSuchCatalogException, SchemaAlreadyExistsException {+ SchemaInfo createSchemaRequest = new SchemaInfo(ident.name(), comment, properties, null);+ eventBus.dispatchEvent(+ new CreateSchemaPreEvent(PrincipalUtils.getCurrentUserName(), ident, createSchemaRequest));try {Schema schema = dispatcher.createSchema(ident, comment, properties);eventBus.dispatchEvent(@@ -93,7 +102,6 @@ public class SchemaEventDispatcher implements SchemaDispatcher {PrincipalUtils.getCurrentUserName(), ident, new SchemaInfo(schema)));return schema;} catch (Exception e) {- SchemaInfo createSchemaRequest = new SchemaInfo(ident.name(), comment, properties, null);eventBus.dispatchEvent(new CreateSchemaFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e, createSchemaRequest));@@ -103,6 +111,7 @@ public class SchemaEventDispatcher implements SchemaDispatcher {@Overridepublic Schema loadSchema(NameIdentifier ident) throws NoSuchSchemaException {+ eventBus.dispatchEvent(new LoadSchemaPreEvent(PrincipalUtils.getCurrentUserName(), ident));try {Schema schema = dispatcher.loadSchema(ident);eventBus.dispatchEvent(@@ -118,6 +127,8 @@ public class SchemaEventDispatcher implements SchemaDispatcher {@Overridepublic Schema alterSchema(NameIdentifier ident, SchemaChange... changes)throws NoSuchSchemaException {+ eventBus.dispatchEvent(+ new AlterSchemaPreEvent(PrincipalUtils.getCurrentUserName(), ident, changes));try {Schema schema = dispatcher.alterSchema(ident, changes);eventBus.dispatchEvent(@@ -133,6 +144,7 @@ public class SchemaEventDispatcher implements SchemaDispatcher {@Overridepublic boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmptySchemaException {+ eventBus.dispatchEvent(new DropSchemaPreEvent(PrincipalUtils.getCurrentUserName(), ident));try {boolean isExists = dispatcher.dropSchema(ident, cascade);eventBus.dispatchEvent(diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/AlterSchemaPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterSchemaPreEvent.javanew file mode 100644index 0000000000..ac2a03f000--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterSchemaPreEvent.java@@ -0,0 +1,45 @@+/*+ * 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.SchemaChange;+import org.apache.gravitino.annotation.DeveloperApi;++/** Represents an event triggered before altering a schema. */+@DeveloperApi+public final class AlterSchemaPreEvent extends SchemaPreEvent {+ private final SchemaChange[] schemaChanges;++ public AlterSchemaPreEvent(String user, NameIdentifier identifier, SchemaChange[] schemaChanges) {+ super(user, identifier);+ this.schemaChanges = schemaChanges;+ }++ /**+ * Retrieves the specific changes that were made to the schema during the alteration process.+ *+ * @return An array of {@link SchemaChange} objects detailing each modification applied to the+ * schema.+ */+ public SchemaChange[] schemaChanges() {+ return schemaChanges;+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/CreateSchemaPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/CreateSchemaPreEvent.javanew file mode 100644index 0000000000..1bf69154a5--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/CreateSchemaPreEvent.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.SchemaInfo;++/** Represents an event triggered before creating a schema. */+@DeveloperApi+public final class CreateSchemaPreEvent extends SchemaPreEvent {+ private final SchemaInfo createSchemaRequest;++ public CreateSchemaPreEvent(+ String user, NameIdentifier identifier, SchemaInfo createSchemaRequest) {+ super(user, identifier);+ this.createSchemaRequest = createSchemaRequest;+ }++ /**+ * Retrieves the create schema request.+ *+ * @return A {@link SchemaInfo} instance encapsulating the comprehensive details of create schema+ * request.+ */+ public SchemaInfo createSchemaRequest() {+ return createSchemaRequest;+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/DropSchemaPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/DropSchemaPreEvent.javanew file mode 100644index 0000000000..88e74a0260--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/DropSchemaPreEvent.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 dropping a schema. */+@DeveloperApi+public final class DropSchemaPreEvent extends SchemaPreEvent {+ public DropSchemaPreEvent(String user, NameIdentifier identifier) {+ super(user, identifier);+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/ListSchemaPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/ListSchemaPreEvent.javanew file mode 100644index 0000000000..ff88482148--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/ListSchemaPreEvent.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 schemas within a namespace. */+@DeveloperApi+public final class ListSchemaPreEvent extends SchemaPreEvent {+ private final Namespace namespace;++ public ListSchemaPreEvent(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 schema were listed.+ */+ public Namespace namespace() {+ return namespace;+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/LoadSchemaPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/LoadSchemaPreEvent.javanew file mode 100644index 0000000000..d9348a4511--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/LoadSchemaPreEvent.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 triggered before loading a schema. */+@DeveloperApi+public final class LoadSchemaPreEvent extends SchemaPreEvent {+ public LoadSchemaPreEvent(String user, NameIdentifier identifier) {+ super(user, identifier);+ }+}diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/SchemaPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/SchemaPreEvent.javanew file mode 100644index 0000000000..dbbc35d49d--- /dev/null+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/SchemaPreEvent.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 schema operations. */+@DeveloperApi+public abstract class SchemaPreEvent extends PreEvent {+ protected SchemaPreEvent(String user, NameIdentifier identifier) {+ super(user, identifier);+ }+}diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestSchemaEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestSchemaEvent.javaindex c2c0d7e446..5b1b39d966 100644--- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestSchemaEvent.java+++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestSchemaEvent.java@@ -64,65 +64,94 @@ public class TestSchemaEvent {@Testvoid testCreateSchemaEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");- dispatcher.createSchema(identifier, "", ImmutableMap.of());+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());+ dispatcher.createSchema(identifier, schema.comment(), schema.properties());+Event event = dummyEventListener.popPostEvent();Assertions.assertEquals(identifier, event.identifier());Assertions.assertEquals(CreateSchemaEvent.class, event.getClass());SchemaInfo schemaInfo = ((CreateSchemaEvent) event).createdSchemaInfo();checkSchemaInfo(schemaInfo, schema);++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(CreateSchemaPreEvent.class, preEvent.getClass());+ SchemaInfo preSchemaInfo = ((CreateSchemaPreEvent) preEvent).createSchemaRequest();+ checkSchemaInfo(preSchemaInfo, schema);}@Testvoid testLoadSchemaEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());dispatcher.loadSchema(identifier);+Event event = dummyEventListener.popPostEvent();Assertions.assertEquals(identifier, event.identifier());Assertions.assertEquals(LoadSchemaEvent.class, event.getClass());SchemaInfo schemaInfo = ((LoadSchemaEvent) event).loadedSchemaInfo();checkSchemaInfo(schemaInfo, schema);++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(LoadSchemaPreEvent.class, preEvent.getClass());}@Testvoid testListSchemaEvent() {Namespace namespace = Namespace.of("metalake", "catalog");dispatcher.listSchemas(namespace);+Event event = dummyEventListener.popPostEvent();+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());Assertions.assertEquals(ListSchemaEvent.class, event.getClass());Assertions.assertEquals(namespace, ((ListSchemaEvent) event).namespace());++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(namespace.toString(), preEvent.identifier().toString());+ Assertions.assertEquals(ListSchemaPreEvent.class, preEvent.getClass());+ Assertions.assertEquals(namespace, ((ListSchemaPreEvent) preEvent).namespace());}@Testvoid testAlterSchemaEvent() {SchemaChange schemaChange = SchemaChange.setProperty("a", "b");- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());dispatcher.alterSchema(identifier, schemaChange);- Event event = dummyEventListener.popPostEvent();+ Event event = dummyEventListener.popPostEvent();Assertions.assertEquals(identifier, event.identifier());Assertions.assertEquals(AlterSchemaEvent.class, event.getClass());SchemaInfo schemaInfo = ((AlterSchemaEvent) event).updatedSchemaInfo();checkSchemaInfo(schemaInfo, schema);-Assertions.assertEquals(1, ((AlterSchemaEvent) event).schemaChanges().length);Assertions.assertEquals(schemaChange, ((AlterSchemaEvent) event).schemaChanges()[0]);++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(AlterSchemaPreEvent.class, preEvent.getClass());+ Assertions.assertEquals(1, ((AlterSchemaPreEvent) preEvent).schemaChanges().length);+ Assertions.assertEquals(schemaChange, ((AlterSchemaPreEvent) preEvent).schemaChanges()[0]);}@Testvoid testDropSchemaEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());dispatcher.dropSchema(identifier, true);+Event event = dummyEventListener.popPostEvent();Assertions.assertEquals(identifier, event.identifier());Assertions.assertEquals(DropSchemaEvent.class, event.getClass());Assertions.assertEquals(true, ((DropSchemaEvent) event).cascade());Assertions.assertEquals(false, ((DropSchemaEvent) event).isExists());++ PreEvent preEvent = dummyEventListener.popPreEvent();+ Assertions.assertEquals(identifier, preEvent.identifier());+ Assertions.assertEquals(DropSchemaPreEvent.class, preEvent.getClass());}@Testvoid testCreateSchemaFailureEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());Assertions.assertThrowsExactly(GravitinoRuntimeException.class,() -> failureDispatcher.createSchema(identifier, schema.comment(), schema.properties()));@@ -136,7 +165,7 @@ public class TestSchemaEvent {@Testvoid testLoadSchemaFailureEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());Assertions.assertThrowsExactly(GravitinoRuntimeException.class, () -> failureDispatcher.loadSchema(identifier));Event event = dummyEventListener.popPostEvent();@@ -150,7 +179,7 @@ public class TestSchemaEvent {void testAlterSchemaFailureEvent() {// alter schemaSchemaChange schemaChange = SchemaChange.setProperty("a", "b");- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());Assertions.assertThrowsExactly(GravitinoRuntimeException.class,() -> failureDispatcher.alterSchema(identifier, schemaChange));@@ -165,7 +194,7 @@ public class TestSchemaEvent {@Testvoid testDropSchemaFailureEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", schema.name());Assertions.assertThrowsExactly(GravitinoRuntimeException.class, () -> failureDispatcher.dropSchema(identifier, true));Event event = dummyEventListener.popPostEvent();diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestTableEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestTableEvent.javaindex 2ea2d3a1e3..48b2557c76 100644--- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestTableEvent.java+++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestTableEvent.java@@ -183,7 +183,7 @@ public class TestTableEvent {@Testvoid testCreateTableFailureEvent() {- NameIdentifier identifier = NameIdentifier.of("metalake", "table", table.name());+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", table.name());Assertions.assertThrowsExactly(GravitinoRuntimeException.class,() ->diff --git a/docs/gravitino-server-config.md b/docs/gravitino-server-config.mdindex b3b555885b..55bb49bf31 100644--- a/docs/gravitino-server-config.md+++ b/docs/gravitino-server-config.md@@ -132,7 +132,8 @@ Gravitino triggers a pre-event before the operation, a post-event after the comp| 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`, `UpdateTablePreEvent`, `DropTablePreEvent`, `PurgeTablePreEvent`, `LoadTablePreEvent`, `ListTablePreEvent` | 0.8.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 |#### Event listener plugin