World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#2821] feat(core): supports metalake event for event listener (#2897)
FANNG84f4b0139a2b
diff --git a/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java b/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.javaindex 1286b8c9c9..02498d2d07 100644--- a/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java+++ b/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java@@ -22,6 +22,8 @@ import com.datastrato.gravitino.catalog.TopicOperationDispatcher;import com.datastrato.gravitino.listener.EventBus;import com.datastrato.gravitino.listener.EventListenerManager;import com.datastrato.gravitino.lock.LockManager;+import com.datastrato.gravitino.metalake.MetalakeDispatcher;+import com.datastrato.gravitino.metalake.MetalakeEventDispatcher;import com.datastrato.gravitino.metalake.MetalakeManager;import com.datastrato.gravitino.metrics.MetricsSystem;import com.datastrato.gravitino.metrics.source.JVMMetricsSource;@@ -55,7 +57,7 @@ public class GravitinoEnv {private TopicOperationDispatcher topicOperationDispatcher;- private MetalakeManager metalakeManager;+ private MetalakeDispatcher metalakeDispatcher;private AccessControlManager accessControlManager;@@ -131,7 +133,8 @@ public class GravitinoEnv {EventBus eventBus = eventListenerManager.createEventBus();// Create and initialize metalake related modules- this.metalakeManager = new MetalakeManager(entityStore, idGenerator);+ MetalakeManager metalakeManager = new MetalakeManager(entityStore, idGenerator);+ this.metalakeDispatcher = new MetalakeEventDispatcher(eventBus, metalakeManager);// Create and initialize Catalog related modulesthis.catalogManager = new CatalogManager(config, entityStore, idGenerator);@@ -231,12 +234,12 @@ public class GravitinoEnv {}/**- * Get the MetalakeManager associated with the Gravitino environment.+ * Get the MetalakeDispatcher associated with the Gravitino environment.*- * @return The MetalakeManager instance.+ * @return The MetalakeDispatcher instance.*/- public MetalakeManager metalakesManager() {- return metalakeManager;+ public MetalakeDispatcher metalakeDispatcher() {+ return metalakeDispatcher;}/**diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/AlterMetalakeEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/AlterMetalakeEvent.javanew file mode 100644index 0000000000..ba1baaafe8--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/AlterMetalakeEvent.java@@ -0,0 +1,58 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.MetalakeChange;+import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;+import com.datastrato.gravitino.listener.api.info.MetalakeInfo;++/** Represents an event fired when a metalake is successfully altered. */+@DeveloperApi+public final class AlterMetalakeEvent extends MetalakeEvent {+ private final MetalakeInfo updatedMetalakeInfo;+ private final MetalakeChange[] metalakeChanges;++ /**+ * Constructs an instance of {@code AlterMetalakeEvent}, encapsulating the key details about the+ * successful alteration of a metalake.+ *+ * @param user The username of the individual responsible for initiating the metalake alteration.+ * @param identifier The unique identifier of the altered metalake, serving as a clear reference+ * point for the metalake in question.+ * @param metalakeChanges An array of {@link MetalakeChange} objects representing the specific+ * changes applied to the metalake during the alteration process.+ * @param updatedMetalakeInfo The post-alteration state of the metalake.+ */+ public AlterMetalakeEvent(+ String user,+ NameIdentifier identifier,+ MetalakeChange[] metalakeChanges,+ MetalakeInfo updatedMetalakeInfo) {+ super(user, identifier);+ this.metalakeChanges = metalakeChanges.clone();+ this.updatedMetalakeInfo = updatedMetalakeInfo;+ }++ /**+ * Retrieves the updated state of the metalake after the successful alteration.+ *+ * @return A {@link MetalakeInfo} instance encapsulating the details of the altered metalake.+ */+ public MetalakeInfo updatedMetalakeInfo() {+ return updatedMetalakeInfo;+ }++ /**+ * Retrieves the specific changes that were made to the metalake during the alteration process.+ *+ * @return An array of {@link MetalakeChange} objects detailing each modification applied to the+ * metalake.+ */+ public MetalakeChange[] metalakeChanges() {+ return metalakeChanges;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/AlterMetalakeFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/AlterMetalakeFailureEvent.javanew file mode 100644index 0000000000..dd58aad26a--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/AlterMetalakeFailureEvent.java@@ -0,0 +1,47 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.MetalakeChange;+import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * Represents an event that is triggered when an attempt to alter a metalake fails due to an+ * exception.+ */+@DeveloperApi+public final class AlterMetalakeFailureEvent extends MetalakeFailureEvent {+ private final MetalakeChange[] metalakeChanges;++ /**+ * Constructs an {@code AlterMetalakeFailureEvent} instance, capturing detailed information about+ * the failed metalake alteration attempt.+ *+ * @param user The user who initiated the metalake alteration operation.+ * @param identifier The identifier of the metalake that was attempted to be altered.+ * @param exception The exception that was thrown during the metalake alteration operation.+ * @param metalakeChanges The changes that were attempted on the metalake.+ */+ public AlterMetalakeFailureEvent(+ String user,+ NameIdentifier identifier,+ Exception exception,+ MetalakeChange[] metalakeChanges) {+ super(user, identifier, exception);+ this.metalakeChanges = metalakeChanges.clone();+ }++ /**+ * Retrieves the changes that were attempted on the metalake.+ *+ * @return An array of {@link MetalakeChange} objects representing the attempted modifications to+ * the metalake.+ */+ public MetalakeChange[] metalakeChanges() {+ return metalakeChanges;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/CreateMetalakeEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/CreateMetalakeEvent.javanew file mode 100644index 0000000000..b3c9c4a1f5--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/CreateMetalakeEvent.java@@ -0,0 +1,40 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;+import com.datastrato.gravitino.listener.api.info.MetalakeInfo;++/** Represents an event triggered upon the successful creation of a Metalake. */+@DeveloperApi+public final class CreateMetalakeEvent extends MetalakeEvent {+ private final MetalakeInfo createdMetalakeInfo;+ /**+ * Constructs an instance of {@code CreateMetalakeEvent}, capturing essential details about the+ * successful creation of a metalake.+ *+ * @param user The username of the individual who initiated the metalake creation.+ * @param identifier The unique identifier of the metalake that was created.+ * @param createdMetalakeInfo The final state of the metalake post-creation.+ */+ public CreateMetalakeEvent(+ String user, NameIdentifier identifier, MetalakeInfo createdMetalakeInfo) {+ super(user, identifier);+ this.createdMetalakeInfo = createdMetalakeInfo;+ }++ /**+ * Retrieves the final state of the Metalake as it was returned to the user after successful+ * creation.+ *+ * @return A {@link MetalakeInfo} instance encapsulating the comprehensive details of the newly+ * created Metalake.+ */+ public MetalakeInfo createdMetalakeInfo() {+ return createdMetalakeInfo;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/CreateMetalakeFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/CreateMetalakeFailureEvent.javanew file mode 100644index 0000000000..64fa352cd1--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/CreateMetalakeFailureEvent.java@@ -0,0 +1,38 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;+import com.datastrato.gravitino.listener.api.info.MetalakeInfo;++/**+ * Represents an event that is generated when an attempt to create a Metalake fails due to an+ * exception.+ */+@DeveloperApi+public final class CreateMetalakeFailureEvent extends MetalakeFailureEvent {+ private final MetalakeInfo createMetalakeRequest;++ public CreateMetalakeFailureEvent(+ String user,+ NameIdentifier identifier,+ Exception exception,+ MetalakeInfo createMetalakeRequest) {+ super(user, identifier, exception);+ this.createMetalakeRequest = createMetalakeRequest;+ }++ /**+ * Retrieves the original request information for the attempted Metalake creation.+ *+ * @return The {@link MetalakeInfo} instance representing the request information for the failed+ * Metalake creation attempt.+ */+ public MetalakeInfo createMetalakeRequest() {+ return createMetalakeRequest;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/DropMetalakeEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/DropMetalakeEvent.javanew file mode 100644index 0000000000..944bed7a9d--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/DropMetalakeEvent.java@@ -0,0 +1,41 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * Represents an event that is generated after a Metalake is successfully removed from the system.+ */+@DeveloperApi+public final class DropMetalakeEvent extends MetalakeEvent {+ private final boolean isExists;++ /**+ * Constructs a new {@code DropMetalakeEvent} instance, encapsulating information about the+ * outcome of a metalake drop operation.+ *+ * @param user The user who initiated the drop metalake operation.+ * @param identifier The identifier of the metalake that was attempted to be dropped.+ * @param isExists A boolean flag indicating whether the metalake existed at the time of the drop+ * operation.+ */+ public DropMetalakeEvent(String user, NameIdentifier identifier, boolean isExists) {+ super(user, identifier);+ this.isExists = isExists;+ }++ /**+ * Retrieves the existence status of the Metalake at the time of the removal operation.+ *+ * @return A boolean value indicating whether the Metalake existed. {@code true} if the Metalake+ * existed, otherwise {@code false}.+ */+ public boolean isExists() {+ return isExists;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/DropMetalakeFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/DropMetalakeFailureEvent.javanew file mode 100644index 0000000000..4a70b33a14--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/DropMetalakeFailureEvent.java@@ -0,0 +1,29 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * Represents an event that is generated when an attempt to remove a Metalake from the system fails+ * due to an exception.+ */+@DeveloperApi+public final class DropMetalakeFailureEvent extends MetalakeFailureEvent {+ /**+ * Constructs a new {@code DropMetalakeFailureEvent} instance, capturing detailed information+ * about the failed attempt to drop a metalake.+ *+ * @param user The user who initiated the drop metalake operation.+ * @param identifier The identifier of the metalake that the operation attempted to drop.+ * @param exception The exception that was thrown during the drop metalake operation, offering+ * insights into what went wrong and why the operation failed.+ */+ public DropMetalakeFailureEvent(String user, NameIdentifier identifier, Exception exception) {+ super(user, identifier, exception);+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListMetalakeEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListMetalakeEvent.javanew file mode 100644index 0000000000..1d72b6477f--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListMetalakeEvent.java@@ -0,0 +1,21 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.annotation.DeveloperApi;++/** Represents an event that is triggered upon the successful list of metalakes. */+@DeveloperApi+public final class ListMetalakeEvent extends MetalakeEvent {+ /**+ * Constructs an instance of {@code ListMetalakeEvent}.+ *+ * @param user The username of the individual who initiated the metalake listing.+ */+ public ListMetalakeEvent(String user) {+ super(user, null);+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListMetalakeFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListMetalakeFailureEvent.javanew file mode 100644index 0000000000..bd065e909c--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListMetalakeFailureEvent.java@@ -0,0 +1,26 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * Represents an event that is triggered when an attempt to list metalakes fails due to an+ * exception.+ */+@DeveloperApi+public final class ListMetalakeFailureEvent extends MetalakeFailureEvent {++ /**+ * Constructs a {@code ListMetalakeFailureEvent} instance.+ *+ * @param user The username of the individual who initiated the operation to list metalakes.+ * @param exception The exception encountered during the attempt to list metalakes.+ */+ public ListMetalakeFailureEvent(String user, Exception exception) {+ super(user, null, exception);+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/LoadMetalakeEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/LoadMetalakeEvent.javanew file mode 100644index 0000000000..10827eb081--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/LoadMetalakeEvent.java@@ -0,0 +1,38 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;+import com.datastrato.gravitino.listener.api.info.MetalakeInfo;++/** Represents an event that is generated when a Metalake is successfully loaded. */+@DeveloperApi+public final class LoadMetalakeEvent extends MetalakeEvent {+ private final MetalakeInfo loadedMetalakeInfo;++ /**+ * Constructs an instance of {@code LoadMetalakeEvent}.+ *+ * @param user The username of the individual who initiated the metalake loading.+ * @param identifier The unique identifier of the metalake that was loaded.+ * @param metalakeInfo The state of the metalake post-loading.+ */+ public LoadMetalakeEvent(String user, NameIdentifier identifier, MetalakeInfo metalakeInfo) {+ super(user, identifier);+ this.loadedMetalakeInfo = metalakeInfo;+ }++ /**+ * Retrieves detailed information about the Metalake that was successfully loaded.+ *+ * @return A {@link MetalakeInfo} instance containing comprehensive details of the Metalake,+ * including its configuration, properties, and state at the time of loading.+ */+ public MetalakeInfo loadedMetalakeInfo() {+ return loadedMetalakeInfo;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/LoadMetalakeFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/LoadMetalakeFailureEvent.javanew file mode 100644index 0000000000..51cda87dd0--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/LoadMetalakeFailureEvent.java@@ -0,0 +1,28 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * Represents an event that is generated when an attempt to load a Metalake into the system fails+ * due to an exception.+ */+@DeveloperApi+public final class LoadMetalakeFailureEvent extends MetalakeFailureEvent {+ /**+ * Constructs a {@code LoadMetalakeFailureEvent} instance.+ *+ * @param user The user who initiated the metalake loading operation.+ * @param identifier The identifier of the metalake that the loading attempt was made for.+ * @param exception The exception that was thrown during the metalake loading operation, offering+ * insight into the issues encountered.+ */+ public LoadMetalakeFailureEvent(String user, NameIdentifier identifier, Exception exception) {+ super(user, identifier, exception);+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/MetalakeEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/MetalakeEvent.javanew file mode 100644index 0000000000..7820003c02--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/MetalakeEvent.java@@ -0,0 +1,28 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * Represents an abstract base class for events related to Metalake operations. This class extends+ * {@link Event} to provide a more specific context involving operations on Metalakes, such as+ * creation, deletion, or modification. It captures essential information including the user+ * performing the operation and the identifier of the Metalake being operated on.+ */+@DeveloperApi+public abstract class MetalakeEvent extends Event {+ /**+ * Constructs a new {@code MetalakeEvent} with the specified user and Metalake identifier.+ *+ * @param user The user responsible for triggering the Metalake operation.+ * @param identifier The identifier of the Metalake involved in the operation.+ */+ protected MetalakeEvent(String user, NameIdentifier identifier) {+ super(user, identifier);+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/MetalakeFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/MetalakeFailureEvent.javanew file mode 100644index 0000000000..01637c6aee--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/MetalakeFailureEvent.java@@ -0,0 +1,31 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.event;++import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.annotation.DeveloperApi;++/**+ * An abstract class representing events that are triggered when a Metalake operation fails due to+ * an exception. This class extends {@link FailureEvent} to provide a more specific context related+ * to Metalake operations, encapsulating details about the user who initiated the operation, the+ * identifier of the Metalake involved, and the exception that led to the failure.+ */+@DeveloperApi+public abstract class MetalakeFailureEvent extends FailureEvent {+ /**+ * Constructs a new {@code MetalakeFailureEvent} instance, capturing information about the failed+ * Metalake operation.+ *+ * @param user The user associated with the failed Metalake operation.+ * @param identifier The identifier of the Metalake that was involved in the failed operation.+ * @param exception The exception that was thrown during the Metalake operation, indicating the+ * cause of the failure.+ */+ protected MetalakeFailureEvent(String user, NameIdentifier identifier, Exception exception) {+ super(user, identifier, exception);+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/info/MetalakeInfo.java b/core/src/main/java/com/datastrato/gravitino/listener/api/info/MetalakeInfo.javanew file mode 100644index 0000000000..d17566b192--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/info/MetalakeInfo.java@@ -0,0 +1,88 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.listener.api.info;++import com.datastrato.gravitino.Audit;+import com.datastrato.gravitino.Metalake;+import com.datastrato.gravitino.annotation.DeveloperApi;+import com.google.common.collect.ImmutableMap;+import java.util.Map;+import javax.annotation.Nullable;++/**+ * Provides access to metadata about a Metalake instance, designed for use by event listeners. This+ * class encapsulates the essential attributes of a Metalake, including its name, optional+ * description, properties, and audit information.+ */+@DeveloperApi+public final class MetalakeInfo {+ private final String name;+ @Nullable private final String comment;+ private final Map<String, String> properties;+ @Nullable private final Audit audit;++ /**+ * Constructs MetalakeInfo from an existing Metalake object.+ *+ * @param metalake The Metalake instance to extract information from.+ */+ public MetalakeInfo(Metalake metalake) {+ this(metalake.name(), metalake.comment(), metalake.properties(), metalake.auditInfo());+ }++ /**+ * Directly constructs MetalakeInfo with specified details.+ *+ * @param name The name of the Metalake.+ * @param comment An optional description for the Metalake.+ * @param properties A map of properties associated with the Metalake.+ * @param audit Optional audit details for the Metalake.+ */+ public MetalakeInfo(String name, String comment, Map<String, String> properties, Audit audit) {+ this.name = name;+ this.comment = comment;+ this.properties = properties == null ? ImmutableMap.of() : ImmutableMap.copyOf(properties);+ this.audit = audit;+ }++ /**+ * Returns the audit information of the Metalake.+ *+ * @return Audit details, or null if not available.+ */+ @Nullable+ public Audit auditInfo() {+ return audit;+ }++ /**+ * Returns the name of the Metalake.+ *+ * @return The Metalake's name.+ */+ public String name() {+ return name;+ }++ /**+ * Returns the optional comment describing the Metalake.+ *+ * @return The comment, or null if not provided.+ */+ @Nullable+ public String comment() {+ return comment;+ }++ /**+ * Returns the properties of the Metalake.+ *+ * @return A map of Metalake properties.+ */+ public Map<String, String> properties() {+ return properties;+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeDispatcher.java b/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeDispatcher.javanew file mode 100644index 0000000000..c7848326a1--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeDispatcher.java@@ -0,0 +1,16 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.metalake;++import com.datastrato.gravitino.SupportsMetalakes;++/**+ * {@code MetalakeDispatcher} interface acts as a specialization of the {@link SupportsMetalakes}+ * interface. This interface is designed to potentially add custom behaviors or operations related+ * to dispatching or handling metalake-related events or actions that are not covered by the+ * standard {@code SupportsMetalakes} operations.+ */+public interface MetalakeDispatcher extends SupportsMetalakes {}diff --git a/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeEventDispatcher.java b/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeEventDispatcher.javanew file mode 100644index 0000000000..16744247ea--- /dev/null+++ b/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeEventDispatcher.java@@ -0,0 +1,130 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.metalake;++import com.datastrato.gravitino.Metalake;+import com.datastrato.gravitino.MetalakeChange;+import com.datastrato.gravitino.NameIdentifier;+import com.datastrato.gravitino.exceptions.MetalakeAlreadyExistsException;+import com.datastrato.gravitino.exceptions.NoSuchMetalakeException;+import com.datastrato.gravitino.listener.EventBus;+import com.datastrato.gravitino.listener.api.event.AlterMetalakeEvent;+import com.datastrato.gravitino.listener.api.event.AlterMetalakeFailureEvent;+import com.datastrato.gravitino.listener.api.event.CreateMetalakeEvent;+import com.datastrato.gravitino.listener.api.event.CreateMetalakeFailureEvent;+import com.datastrato.gravitino.listener.api.event.DropMetalakeEvent;+import com.datastrato.gravitino.listener.api.event.DropMetalakeFailureEvent;+import com.datastrato.gravitino.listener.api.event.ListMetalakeEvent;+import com.datastrato.gravitino.listener.api.event.ListMetalakeFailureEvent;+import com.datastrato.gravitino.listener.api.event.LoadMetalakeEvent;+import com.datastrato.gravitino.listener.api.event.LoadMetalakeFailureEvent;+import com.datastrato.gravitino.listener.api.info.MetalakeInfo;+import com.datastrato.gravitino.utils.PrincipalUtils;+import java.util.Map;++/**+ * {@code MetalakeEventDispatcher} is a decorator for {@link MetalakeDispatcher} that not only+ * delegates metalake operations to the underlying metalake dispatcher but also dispatches+ * corresponding events to an {@link EventBus} after each operation is completed. This allows for+ * event-driven workflows or monitoring of metalake operations.+ */+public class MetalakeEventDispatcher implements MetalakeDispatcher {+ private final EventBus eventBus;+ private final MetalakeDispatcher dispatcher;++ /**+ * Constructs a MetalakeEventDispatcher with a specified EventBus and MetalakeDispatcher.+ *+ * @param eventBus The EventBus to which events will be dispatched.+ * @param dispatcher The underlying {@link MetalakeDispatcher} that will perform the actual+ * metalake operations.+ */+ public MetalakeEventDispatcher(EventBus eventBus, MetalakeDispatcher dispatcher) {+ this.eventBus = eventBus;+ this.dispatcher = dispatcher;+ }++ @Override+ public Metalake[] listMetalakes() {+ try {+ Metalake[] metalakes = dispatcher.listMetalakes();+ eventBus.dispatchEvent(new ListMetalakeEvent(PrincipalUtils.getCurrentUserName()));+ return metalakes;+ } catch (Exception e) {+ eventBus.dispatchEvent(new ListMetalakeFailureEvent(PrincipalUtils.getCurrentUserName(), e));+ throw e;+ }+ }++ @Override+ public Metalake loadMetalake(NameIdentifier ident) throws NoSuchMetalakeException {+ try {+ Metalake metalake = dispatcher.loadMetalake(ident);+ eventBus.dispatchEvent(+ new LoadMetalakeEvent(+ PrincipalUtils.getCurrentUserName(), ident, new MetalakeInfo(metalake)));+ return metalake;+ } catch (Exception e) {+ eventBus.dispatchEvent(+ new LoadMetalakeFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e));+ throw e;+ }+ }++ @Override+ public boolean metalakeExists(NameIdentifier ident) {+ return dispatcher.metalakeExists(ident);+ }++ @Override+ public Metalake createMetalake(+ NameIdentifier ident, String comment, Map<String, String> properties)+ throws MetalakeAlreadyExistsException {+ try {+ Metalake metalake = dispatcher.createMetalake(ident, comment, properties);+ eventBus.dispatchEvent(+ new CreateMetalakeEvent(+ PrincipalUtils.getCurrentUserName(), ident, new MetalakeInfo(metalake)));+ return metalake;+ } catch (Exception e) {+ MetalakeInfo metalakeInfo = new MetalakeInfo(ident.name(), comment, properties, null);+ eventBus.dispatchEvent(+ new CreateMetalakeFailureEvent(+ PrincipalUtils.getCurrentUserName(), ident, e, metalakeInfo));+ throw e;+ }+ }++ @Override+ public Metalake alterMetalake(NameIdentifier ident, MetalakeChange... changes)+ throws NoSuchMetalakeException, IllegalArgumentException {+ try {+ Metalake metalake = dispatcher.alterMetalake(ident, changes);+ eventBus.dispatchEvent(+ new AlterMetalakeEvent(+ PrincipalUtils.getCurrentUserName(), ident, changes, new MetalakeInfo(metalake)));+ return metalake;+ } catch (Exception e) {+ eventBus.dispatchEvent(+ new AlterMetalakeFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e, changes));+ throw e;+ }+ }++ @Override+ public boolean dropMetalake(NameIdentifier ident) {+ try {+ boolean isExists = dispatcher.dropMetalake(ident);+ eventBus.dispatchEvent(+ new DropMetalakeEvent(PrincipalUtils.getCurrentUserName(), ident, isExists));+ return isExists;+ } catch (Exception e) {+ eventBus.dispatchEvent(+ new DropMetalakeFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e));+ throw e;+ }+ }+}diff --git a/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeManager.java b/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeManager.javaindex d67bc8aa27..3f445cbbf2 100644--- a/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeManager.java+++ b/core/src/main/java/com/datastrato/gravitino/metalake/MetalakeManager.java@@ -12,7 +12,6 @@ import com.datastrato.gravitino.MetalakeChange;import com.datastrato.gravitino.NameIdentifier;import com.datastrato.gravitino.Namespace;import com.datastrato.gravitino.StringIdentifier;-import com.datastrato.gravitino.SupportsMetalakes;import com.datastrato.gravitino.exceptions.AlreadyExistsException;import com.datastrato.gravitino.exceptions.MetalakeAlreadyExistsException;import com.datastrato.gravitino.exceptions.NoSuchEntityException;@@ -30,7 +29,7 @@ import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** Manages Metalakes within the Gravitino system. */-public class MetalakeManager implements SupportsMetalakes {+public class MetalakeManager implements MetalakeDispatcher {private static final String METALAKE_DOES_NOT_EXIST_MSG = "Metalake %s does not exist";diff --git a/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java b/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.javaindex bc1182865b..88ca4a815a 100644--- a/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java+++ b/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java@@ -10,7 +10,7 @@ import com.datastrato.gravitino.catalog.CatalogDispatcher;import com.datastrato.gravitino.catalog.FilesetDispatcher;import com.datastrato.gravitino.catalog.SchemaDispatcher;import com.datastrato.gravitino.catalog.TableDispatcher;-import com.datastrato.gravitino.metalake.MetalakeManager;+import com.datastrato.gravitino.metalake.MetalakeDispatcher;import com.datastrato.gravitino.metrics.MetricsSystem;import com.datastrato.gravitino.metrics.source.MetricsSource;import com.datastrato.gravitino.server.authentication.ServerAuthenticator;@@ -76,7 +76,7 @@ public class GravitinoServer extends ResourceConfig {new AbstractBinder() {@Overrideprotected void configure() {- bind(gravitinoEnv.metalakesManager()).to(MetalakeManager.class).ranked(1);+ bind(gravitinoEnv.metalakeDispatcher()).to(MetalakeDispatcher.class).ranked(1);bind(gravitinoEnv.catalogDispatcher()).to(CatalogDispatcher.class).ranked(1);bind(gravitinoEnv.schemaDispatcher()).to(SchemaDispatcher.class).ranked(1);diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.javaindex c0a91613fa..b2037ee2b7 100644--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java@@ -6,6 +6,7 @@ package com.datastrato.gravitino.server.web.rest;import com.codahale.metrics.annotation.ResponseMetered;import com.codahale.metrics.annotation.Timed;+import com.datastrato.gravitino.Metalake;import com.datastrato.gravitino.MetalakeChange;import com.datastrato.gravitino.NameIdentifier;import com.datastrato.gravitino.Namespace;@@ -19,8 +20,7 @@ import com.datastrato.gravitino.dto.responses.MetalakeResponse;import com.datastrato.gravitino.dto.util.DTOConverters;import com.datastrato.gravitino.lock.LockType;import com.datastrato.gravitino.lock.TreeLockUtils;-import com.datastrato.gravitino.meta.BaseMetalake;-import com.datastrato.gravitino.metalake.MetalakeManager;+import com.datastrato.gravitino.metalake.MetalakeDispatcher;import com.datastrato.gravitino.metrics.MetricNames;import com.datastrato.gravitino.server.web.Utils;import java.util.Arrays;@@ -47,13 +47,13 @@ public class MetalakeOperations {private static final Logger LOG = LoggerFactory.getLogger(MetalakeOperations.class);- private final MetalakeManager manager;+ private final MetalakeDispatcher metalakeDispatcher;@Context private HttpServletRequest httpRequest;@Inject- public MetalakeOperations(MetalakeManager manager) {- this.manager = manager;+ public MetalakeOperations(MetalakeDispatcher dispatcher) {+ this.metalakeDispatcher = dispatcher;}@GET@@ -65,8 +65,8 @@ public class MetalakeOperations {return Utils.doAs(httpRequest,() -> {- BaseMetalake[] metalakes =- TreeLockUtils.doWithRootTreeLock(LockType.READ, manager::listMetalakes);+ Metalake[] metalakes =+ TreeLockUtils.doWithRootTreeLock(LockType.READ, metalakeDispatcher::listMetalakes);MetalakeDTO[] metalakeDTOS =Arrays.stream(metalakes).map(DTOConverters::toDTO).toArray(MetalakeDTO[]::new);return Utils.ok(new MetalakeListResponse(metalakeDTOS));@@ -89,11 +89,11 @@ public class MetalakeOperations {() -> {request.validate();NameIdentifier ident = NameIdentifier.ofMetalake(request.getName());- BaseMetalake metalake =+ Metalake metalake =TreeLockUtils.doWithRootTreeLock(LockType.WRITE,() ->- manager.createMetalake(+ metalakeDispatcher.createMetalake(ident, request.getComment(), request.getProperties()));return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));});@@ -114,9 +114,9 @@ public class MetalakeOperations {httpRequest,() -> {NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);- BaseMetalake metalake =+ Metalake metalake =TreeLockUtils.doWithTreeLock(- identifier, LockType.READ, () -> manager.loadMetalake(identifier));+ identifier, LockType.READ, () -> metalakeDispatcher.loadMetalake(identifier));return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));});@@ -142,9 +142,9 @@ public class MetalakeOperations {updatesRequest.getUpdates().stream().map(MetalakeUpdateRequest::metalakeChange).toArray(MetalakeChange[]::new);- BaseMetalake updatedMetalake =+ Metalake updatedMetalake =TreeLockUtils.doWithRootTreeLock(- LockType.WRITE, () -> manager.alterMetalake(identifier, changes));+ LockType.WRITE, () -> metalakeDispatcher.alterMetalake(identifier, changes));return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));});@@ -166,7 +166,7 @@ public class MetalakeOperations {NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);boolean dropped =TreeLockUtils.doWithRootTreeLock(- LockType.WRITE, () -> manager.dropMetalake(identifier));+ LockType.WRITE, () -> metalakeDispatcher.dropMetalake(identifier));if (!dropped) {LOG.warn("Failed to drop metalake by name {}", metalakeName);}diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.javaindex ca0a2e1e95..fd2e9a6baf 100644--- a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java+++ b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java@@ -29,6 +29,7 @@ import com.datastrato.gravitino.lock.LockManager;import com.datastrato.gravitino.meta.AuditInfo;import com.datastrato.gravitino.meta.BaseMetalake;import com.datastrato.gravitino.meta.SchemaVersion;+import com.datastrato.gravitino.metalake.MetalakeDispatcher;import com.datastrato.gravitino.metalake.MetalakeManager;import com.datastrato.gravitino.rest.RESTUtils;import com.google.common.collect.ImmutableMap;@@ -87,7 +88,7 @@ public class TestMetalakeOperations extends JerseyTest {new AbstractBinder() {@Overrideprotected void configure() {- bind(metalakeManager).to(MetalakeManager.class).ranked(2);+ bind(metalakeManager).to(MetalakeDispatcher.class).ranked(2);bindFactory(MockServletRequestFactory.class).to(HttpServletRequest.class);}});