Skip to content

Commit

Permalink
[apache#5437] feat(core): add schema event for Iceberg REST server (a…
Browse files Browse the repository at this point in the history
…pache#5651)

### What changes were proposed in this pull request?

add schema event for Iceberg REST server

### Why are the changes needed?

Fix: apache#5437

### Does this PR introduce _any_ user-facing change?

no

### How was this patch tested?

unit test
  • Loading branch information
orenccl authored Nov 25, 2024
1 parent c1b9885 commit dbeb017
Show file tree
Hide file tree
Showing 41 changed files with 1,922 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class CompatibilityUtils {
.put(OperationType.DROP_SCHEMA, Operation.DROP_SCHEMA)
.put(OperationType.LOAD_SCHEMA, Operation.LOAD_SCHEMA)
.put(OperationType.LIST_SCHEMA, Operation.LIST_SCHEMA)
.put(OperationType.SCHEMA_EXISTS, Operation.UNKNOWN_OPERATION)

// Table operation
.put(OperationType.CREATE_TABLE, Operation.CREATE_TABLE)
Expand All @@ -58,6 +59,8 @@ public class CompatibilityUtils {
.put(OperationType.LOAD_TABLE, Operation.LOAD_TABLE)
.put(OperationType.TABLE_EXISTS, Operation.UNKNOWN_OPERATION)
.put(OperationType.LIST_TABLE, Operation.LIST_TABLE)
.put(OperationType.RENAME_TABLE, Operation.UNKNOWN_OPERATION)
.put(OperationType.REGISTER_TABLE, Operation.UNKNOWN_OPERATION)

// Partition operation
.put(OperationType.ADD_PARTITION, Operation.UNKNOWN_OPERATION)
Expand All @@ -82,6 +85,15 @@ public class CompatibilityUtils {
.put(OperationType.DROP_TOPIC, Operation.DROP_TOPIC)
.put(OperationType.LOAD_TOPIC, Operation.LOAD_TOPIC)
.put(OperationType.LIST_TOPIC, Operation.LIST_TOPIC)

// View operation
.put(OperationType.CREATE_VIEW, Operation.UNKNOWN_OPERATION)
.put(OperationType.ALTER_VIEW, Operation.UNKNOWN_OPERATION)
.put(OperationType.DROP_VIEW, Operation.UNKNOWN_OPERATION)
.put(OperationType.LOAD_VIEW, Operation.UNKNOWN_OPERATION)
.put(OperationType.VIEW_EXISTS, Operation.UNKNOWN_OPERATION)
.put(OperationType.RENAME_VIEW, Operation.UNKNOWN_OPERATION)
.put(OperationType.LIST_VIEW, Operation.UNKNOWN_OPERATION)
.build();

static Operation toAuditLogOperation(OperationType operationType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum OperationType {
LIST_TABLE,
ALTER_TABLE,
RENAME_TABLE,
REGISTER_TABLE,
TABLE_EXISTS,

// Schema operations
Expand All @@ -36,6 +37,7 @@ public enum OperationType {
ALTER_SCHEMA,
LOAD_SCHEMA,
LIST_SCHEMA,
SCHEMA_EXISTS,

// Fileset operations
DROP_FILESET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ public void testToAuditLogOperation() {
{OperationType.DROP_SCHEMA, Operation.DROP_SCHEMA},
{OperationType.LOAD_SCHEMA, Operation.LOAD_SCHEMA},
{OperationType.LIST_SCHEMA, Operation.LIST_SCHEMA},
{OperationType.SCHEMA_EXISTS, Operation.UNKNOWN_OPERATION},
{OperationType.CREATE_TABLE, Operation.CREATE_TABLE},
{OperationType.ALTER_TABLE, Operation.ALTER_TABLE},
{OperationType.DROP_TABLE, Operation.DROP_TABLE},
{OperationType.PURGE_TABLE, Operation.PURGE_TABLE},
{OperationType.LOAD_TABLE, Operation.LOAD_TABLE},
{OperationType.TABLE_EXISTS, Operation.UNKNOWN_OPERATION},
{OperationType.LIST_TABLE, Operation.LIST_TABLE},
{OperationType.RENAME_TABLE, Operation.UNKNOWN_OPERATION},
{OperationType.REGISTER_TABLE, Operation.UNKNOWN_OPERATION},
{OperationType.ADD_PARTITION, Operation.UNKNOWN_OPERATION},
{OperationType.DROP_PARTITION, Operation.UNKNOWN_OPERATION},
{OperationType.PURGE_PARTITION, Operation.PURGE_PARTITION},
Expand All @@ -73,6 +76,13 @@ public void testToAuditLogOperation() {
{OperationType.DROP_TOPIC, Operation.DROP_TOPIC},
{OperationType.LOAD_TOPIC, Operation.LOAD_TOPIC},
{OperationType.LIST_TOPIC, Operation.LIST_TOPIC},
{OperationType.CREATE_VIEW, Operation.UNKNOWN_OPERATION},
{OperationType.ALTER_VIEW, Operation.UNKNOWN_OPERATION},
{OperationType.DROP_VIEW, Operation.UNKNOWN_OPERATION},
{OperationType.LOAD_VIEW, Operation.UNKNOWN_OPERATION},
{OperationType.VIEW_EXISTS, Operation.UNKNOWN_OPERATION},
{OperationType.RENAME_VIEW, Operation.UNKNOWN_OPERATION},
{OperationType.LIST_VIEW, Operation.UNKNOWN_OPERATION},
{null, Operation.UNKNOWN_OPERATION}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public GetNamespaceResponse loadNamespace(Namespace namespace) {
return CatalogHandlers.loadNamespace(asNamespaceCatalog, namespace);
}

public boolean existNamespace(Namespace namespace) {
public boolean namespaceExists(Namespace namespace) {
validateNamespace(Optional.of(namespace));
return asNamespaceCatalog.namespaceExists(namespace);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ public void renameView(RenameTableRequest request) {
CatalogHandlers.renameView(getViewCatalog(), request);
}

public boolean existView(TableIdentifier viewIdentifier) {
public boolean viewExists(TableIdentifier viewIdentifier) {
return getViewCatalog().viewExists(viewIdentifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.apache.gravitino.iceberg.service.IcebergCatalogWrapperManager;
import org.apache.gravitino.iceberg.service.IcebergExceptionMapper;
import org.apache.gravitino.iceberg.service.IcebergObjectMapperProvider;
import org.apache.gravitino.iceberg.service.dispatcher.IcebergNamespaceEventDispatcher;
import org.apache.gravitino.iceberg.service.dispatcher.IcebergNamespaceOperationDispatcher;
import org.apache.gravitino.iceberg.service.dispatcher.IcebergNamespaceOperationExecutor;
import org.apache.gravitino.iceberg.service.dispatcher.IcebergTableEventDispatcher;
import org.apache.gravitino.iceberg.service.dispatcher.IcebergTableOperationDispatcher;
import org.apache.gravitino.iceberg.service.dispatcher.IcebergTableOperationExecutor;
Expand Down Expand Up @@ -97,6 +100,11 @@ private void initServer(IcebergConfig icebergConfig) {
new IcebergViewOperationExecutor(icebergCatalogWrapperManager);
IcebergViewEventDispatcher icebergViewEventDispatcher =
new IcebergViewEventDispatcher(icebergViewOperationExecutor, eventBus, metalakeName);
IcebergNamespaceOperationExecutor icebergNamespaceOperationExecutor =
new IcebergNamespaceOperationExecutor(icebergCatalogWrapperManager);
IcebergNamespaceEventDispatcher icebergNamespaceEventDispatcher =
new IcebergNamespaceEventDispatcher(
icebergNamespaceOperationExecutor, eventBus, metalakeName);

config.register(
new AbstractBinder() {
Expand All @@ -106,6 +114,9 @@ protected void configure() {
bind(icebergMetricsManager).to(IcebergMetricsManager.class).ranked(1);
bind(icebergTableEventDispatcher).to(IcebergTableOperationDispatcher.class).ranked(1);
bind(icebergViewEventDispatcher).to(IcebergViewOperationDispatcher.class).ranked(1);
bind(icebergNamespaceEventDispatcher)
.to(IcebergNamespaceOperationDispatcher.class)
.ranked(1);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* 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.iceberg.service.dispatcher;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.iceberg.service.IcebergRestUtils;
import org.apache.gravitino.listener.EventBus;
import org.apache.gravitino.listener.api.event.IcebergCreateNamespaceEvent;
import org.apache.gravitino.listener.api.event.IcebergCreateNamespaceFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergCreateNamespacePreEvent;
import org.apache.gravitino.listener.api.event.IcebergDropNamespaceEvent;
import org.apache.gravitino.listener.api.event.IcebergDropNamespaceFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergDropNamespacePreEvent;
import org.apache.gravitino.listener.api.event.IcebergListNamespacesEvent;
import org.apache.gravitino.listener.api.event.IcebergListNamespacesFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergListNamespacesPreEvent;
import org.apache.gravitino.listener.api.event.IcebergLoadNamespaceEvent;
import org.apache.gravitino.listener.api.event.IcebergLoadNamespaceFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergLoadNamespacePreEvent;
import org.apache.gravitino.listener.api.event.IcebergNamespaceExistsEvent;
import org.apache.gravitino.listener.api.event.IcebergNamespaceExistsFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergNamespaceExistsPreEvent;
import org.apache.gravitino.listener.api.event.IcebergRegisterTableEvent;
import org.apache.gravitino.listener.api.event.IcebergRegisterTableFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergRegisterTablePreEvent;
import org.apache.gravitino.listener.api.event.IcebergRequestContext;
import org.apache.gravitino.listener.api.event.IcebergUpdateNamespaceEvent;
import org.apache.gravitino.listener.api.event.IcebergUpdateNamespaceFailureEvent;
import org.apache.gravitino.listener.api.event.IcebergUpdateNamespacePreEvent;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.rest.requests.CreateNamespaceRequest;
import org.apache.iceberg.rest.requests.RegisterTableRequest;
import org.apache.iceberg.rest.requests.UpdateNamespacePropertiesRequest;
import org.apache.iceberg.rest.responses.CreateNamespaceResponse;
import org.apache.iceberg.rest.responses.GetNamespaceResponse;
import org.apache.iceberg.rest.responses.ListNamespacesResponse;
import org.apache.iceberg.rest.responses.LoadTableResponse;
import org.apache.iceberg.rest.responses.UpdateNamespacePropertiesResponse;

/**
* {@code IcebergNamespaceEventDispatcher} is a decorator for {@link
* IcebergNamespaceOperationExecutor} that not only delegates namespace operations to the underlying
* dispatcher but also dispatches corresponding events to an {@link EventBus}.
*/
public class IcebergNamespaceEventDispatcher implements IcebergNamespaceOperationDispatcher {

private IcebergNamespaceOperationDispatcher operationDispatcher;
private EventBus eventBus;
private String metalakeName;

public IcebergNamespaceEventDispatcher(
IcebergNamespaceOperationDispatcher operationDispatcher,
EventBus eventBus,
String metalakeName) {
this.operationDispatcher = operationDispatcher;
this.eventBus = eventBus;
this.metalakeName = metalakeName;
}

@Override
public CreateNamespaceResponse createNamespace(
IcebergRequestContext context, CreateNamespaceRequest createRequest) {
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(
metalakeName, context.catalogName(), createRequest.namespace());

eventBus.dispatchEvent(
new IcebergCreateNamespacePreEvent(context, nameIdentifier, createRequest));

CreateNamespaceResponse createResponse;
try {
createResponse = operationDispatcher.createNamespace(context, createRequest);
} catch (Exception e) {
eventBus.dispatchEvent(
new IcebergCreateNamespaceFailureEvent(context, nameIdentifier, createRequest, e));
throw e;
}

eventBus.dispatchEvent(
new IcebergCreateNamespaceEvent(context, nameIdentifier, createRequest, createResponse));
return createResponse;
}

@Override
public UpdateNamespacePropertiesResponse updateNamespace(
IcebergRequestContext context,
Namespace namespace,
UpdateNamespacePropertiesRequest updateRequest) {
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(metalakeName, context.catalogName(), namespace);

eventBus.dispatchEvent(
new IcebergUpdateNamespacePreEvent(context, nameIdentifier, updateRequest));

UpdateNamespacePropertiesResponse updateResponse;
try {
updateResponse = operationDispatcher.updateNamespace(context, namespace, updateRequest);
} catch (Exception e) {
eventBus.dispatchEvent(
new IcebergUpdateNamespaceFailureEvent(context, nameIdentifier, updateRequest, e));
throw e;
}

eventBus.dispatchEvent(
new IcebergUpdateNamespaceEvent(context, nameIdentifier, updateRequest, updateResponse));
return updateResponse;
}

@Override
public void dropNamespace(IcebergRequestContext context, Namespace namespace) {
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(metalakeName, context.catalogName(), namespace);
eventBus.dispatchEvent(new IcebergDropNamespacePreEvent(context, nameIdentifier));

try {
operationDispatcher.dropNamespace(context, namespace);
} catch (Exception e) {
eventBus.dispatchEvent(new IcebergDropNamespaceFailureEvent(context, nameIdentifier, e));
throw e;
}

eventBus.dispatchEvent(new IcebergDropNamespaceEvent(context, nameIdentifier));
}

@Override
public GetNamespaceResponse loadNamespace(IcebergRequestContext context, Namespace namespace) {
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(metalakeName, context.catalogName(), namespace);
eventBus.dispatchEvent(new IcebergLoadNamespacePreEvent(context, nameIdentifier));

GetNamespaceResponse getResponse;
try {
getResponse = operationDispatcher.loadNamespace(context, namespace);
} catch (Exception e) {
eventBus.dispatchEvent(new IcebergLoadNamespaceFailureEvent(context, nameIdentifier, e));
throw e;
}

eventBus.dispatchEvent(new IcebergLoadNamespaceEvent(context, nameIdentifier, getResponse));
return getResponse;
}

@Override
public ListNamespacesResponse listNamespaces(
IcebergRequestContext context, Namespace parentNamespace) {
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(
metalakeName, context.catalogName(), parentNamespace);
eventBus.dispatchEvent(new IcebergListNamespacesPreEvent(context, nameIdentifier));

ListNamespacesResponse listResponse;
try {
listResponse = operationDispatcher.listNamespaces(context, parentNamespace);
} catch (Exception e) {
eventBus.dispatchEvent(new IcebergListNamespacesFailureEvent(context, nameIdentifier, e));
throw e;
}

eventBus.dispatchEvent(new IcebergListNamespacesEvent(context, nameIdentifier));
return listResponse;
}

@Override
public boolean namespaceExists(IcebergRequestContext context, Namespace namespace) {
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(metalakeName, context.catalogName(), namespace);
eventBus.dispatchEvent(new IcebergNamespaceExistsPreEvent(context, nameIdentifier));

boolean isExists;
try {
isExists = operationDispatcher.namespaceExists(context, namespace);
} catch (Exception e) {
eventBus.dispatchEvent(new IcebergNamespaceExistsFailureEvent(context, nameIdentifier, e));
throw e;
}

eventBus.dispatchEvent(new IcebergNamespaceExistsEvent(context, nameIdentifier, isExists));
return isExists;
}

@Override
public LoadTableResponse registerTable(
IcebergRequestContext context,
Namespace namespace,
RegisterTableRequest registerTableRequest) {
TableIdentifier tableIdentifier = TableIdentifier.of(namespace, registerTableRequest.name());
NameIdentifier nameIdentifier =
IcebergRestUtils.getGravitinoNameIdentifier(
metalakeName, context.catalogName(), tableIdentifier);

eventBus.dispatchEvent(
new IcebergRegisterTablePreEvent(context, nameIdentifier, registerTableRequest));

LoadTableResponse loadTableResponse;
try {
loadTableResponse =
operationDispatcher.registerTable(context, namespace, registerTableRequest);
} catch (Exception e) {
eventBus.dispatchEvent(
new IcebergRegisterTableFailureEvent(context, nameIdentifier, registerTableRequest, e));
throw e;
}

eventBus.dispatchEvent(
new IcebergRegisterTableEvent(
context, nameIdentifier, registerTableRequest, loadTableResponse));
return loadTableResponse;
}
}
Loading

0 comments on commit dbeb017

Please sign in to comment.