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 16, 2024
1 parent 13c92f9 commit a806479
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 1 deletion.
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,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;
}
}
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,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;
}
}
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 before a catalog is 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,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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ void testCreateCatalogEvent() {
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
Expand All @@ -82,6 +88,10 @@ void testLoadCatalogEvent() {
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
Expand All @@ -97,6 +107,13 @@ void testAlterCatalogEvent() {
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
Expand All @@ -107,6 +124,10 @@ void testDropCatalogEvent() {
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
Expand All @@ -117,6 +138,11 @@ void testListCatalogEvent() {
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
Expand All @@ -127,6 +153,11 @@ void testListCatalogInfoEvent() {
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
Expand Down
3 changes: 2 additions & 1 deletion docs/gravitino-server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ Gravitino triggers a pre-event before the operation, a post-event after the comp
|-------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------|
| 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 |
| Gravitino server schema operation | `CreateSchemaPreEvent`, `AlterSchemaPreEvent`, `DropSchemaPreEvent`, `LoadSchemaPreEvent`, `ListSchemaPreEvent` | 0.8.0-incubating |
| Gravitino server catalog operation | `CreateCatalogPreEvent`, `AlterCatalogPreEvent`, `DropCatalogPreEvent`, `LoadCatalogPreEvent`, `ListCatalogPreEvent` | |

#### Event listener plugin

Expand Down

0 comments on commit a806479

Please sign in to comment.