Skip to content

Commit

Permalink
[#2920]feat(core): supports more table event (#2895)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
supports `AlterTableEvent` `PurgeTableEvent` `LoadTableEvent`
`ListTableEvent`

### Why are the changes needed?
Fix: #2920 

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

### How was this patch tested?
existing tests
  • Loading branch information
FANNG1 authored Apr 15, 2024
1 parent 68b4587 commit 0ff45cd
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
import com.datastrato.gravitino.exceptions.NoSuchTableException;
import com.datastrato.gravitino.exceptions.TableAlreadyExistsException;
import com.datastrato.gravitino.listener.EventBus;
import com.datastrato.gravitino.listener.api.event.AlterTableEvent;
import com.datastrato.gravitino.listener.api.event.AlterTableFailureEvent;
import com.datastrato.gravitino.listener.api.event.CreateTableEvent;
import com.datastrato.gravitino.listener.api.event.CreateTableFailureEvent;
import com.datastrato.gravitino.listener.api.event.DropTableEvent;
import com.datastrato.gravitino.listener.api.event.DropTableFailureEvent;
import com.datastrato.gravitino.listener.api.event.ListTableEvent;
import com.datastrato.gravitino.listener.api.event.ListTableFailureEvent;
import com.datastrato.gravitino.listener.api.event.LoadTableEvent;
import com.datastrato.gravitino.listener.api.event.LoadTableFailureEvent;
import com.datastrato.gravitino.listener.api.event.PurgeTableEvent;
import com.datastrato.gravitino.listener.api.event.PurgeTableFailureEvent;
import com.datastrato.gravitino.listener.api.info.TableInfo;
import com.datastrato.gravitino.rel.Column;
import com.datastrato.gravitino.rel.Table;
Expand Down Expand Up @@ -50,12 +58,29 @@ public TableEventDispatcher(EventBus eventBus, TableDispatcher dispatcher) {

@Override
public NameIdentifier[] listTables(Namespace namespace) throws NoSuchSchemaException {
return dispatcher.listTables(namespace);
try {
NameIdentifier[] nameIdentifiers = dispatcher.listTables(namespace);
eventBus.dispatchEvent(new ListTableEvent(PrincipalUtils.getCurrentUserName(), namespace));
return nameIdentifiers;
} catch (Exception e) {
eventBus.dispatchEvent(
new ListTableFailureEvent(PrincipalUtils.getCurrentUserName(), namespace, e));
throw e;
}
}

@Override
public Table loadTable(NameIdentifier ident) throws NoSuchTableException {
return dispatcher.loadTable(ident);
try {
Table table = dispatcher.loadTable(ident);
eventBus.dispatchEvent(
new LoadTableEvent(PrincipalUtils.getCurrentUserName(), ident, new TableInfo(table)));
return table;
} catch (Exception e) {
eventBus.dispatchEvent(
new LoadTableFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e));
throw e;
}
}

@Override
Expand Down Expand Up @@ -98,7 +123,17 @@ public Table createTable(
@Override
public Table alterTable(NameIdentifier ident, TableChange... changes)
throws NoSuchTableException, IllegalArgumentException {
return dispatcher.alterTable(ident, changes);
try {
Table table = dispatcher.alterTable(ident, changes);
eventBus.dispatchEvent(
new AlterTableEvent(
PrincipalUtils.getCurrentUserName(), ident, changes, new TableInfo(table)));
return table;
} catch (Exception e) {
eventBus.dispatchEvent(
new AlterTableFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e, changes));
throw e;
}
}

@Override
Expand All @@ -117,7 +152,16 @@ public boolean dropTable(NameIdentifier ident) {

@Override
public boolean purgeTable(NameIdentifier ident) {
return dispatcher.purgeTable(ident);
try {
boolean isExists = dispatcher.purgeTable(ident);
eventBus.dispatchEvent(
new PurgeTableEvent(PrincipalUtils.getCurrentUserName(), ident, isExists));
return isExists;
} catch (Exception e) {
eventBus.dispatchEvent(
new PurgeTableFailureEvent(PrincipalUtils.getCurrentUserName(), ident, e));
throw e;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.NameIdentifier;
import com.datastrato.gravitino.annotation.DeveloperApi;
import com.datastrato.gravitino.listener.api.info.TableInfo;
import com.datastrato.gravitino.rel.TableChange;

/** Represents an event fired when a table is successfully altered. */
@DeveloperApi
public final class AlterTableEvent extends TableEvent {
private final TableInfo updatedTableInfo;
private final TableChange[] tableChanges;

/**
* Constructs an instance of {@code AlterTableEvent}, encapsulating the key details about the
* successful alteration of a table.
*
* @param user The username of the individual responsible for initiating the table alteration.
* @param identifier The unique identifier of the altered table, serving as a clear reference
* point for the table in question.
* @param tableChanges An array of {@link TableChange} objects representing the specific changes
* applied to the table during the alteration process.
* @param updatedTableInfo The post-alteration state of the table.
*/
public AlterTableEvent(
String user,
NameIdentifier identifier,
TableChange[] tableChanges,
TableInfo updatedTableInfo) {
super(user, identifier);
this.tableChanges = tableChanges.clone();
this.updatedTableInfo = updatedTableInfo;
}

/**
* Retrieves the updated state of the table after the successful alteration.
*
* @return A {@link TableInfo} instance encapsulating the details of the altered table.
*/
public TableInfo updatedTableInfo() {
return updatedTableInfo;
}

/**
* Retrieves the specific changes that were made to the table during the alteration process.
*
* @return An array of {@link TableChange} objects detailing each modification applied to the
* table.
*/
public TableChange[] tableChanges() {
return tableChanges;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.rel.TableChange;

/**
* Represents an event that is triggered when an attempt to alter a table fails due to an exception.
*/
@DeveloperApi
public final class AlterTableFailureEvent extends TableFailureEvent {
private final TableChange[] tableChanges;

/**
* Constructs an {@code AlterTableFailureEvent} instance, capturing detailed information about the
* failed table alteration attempt.
*
* @param user The user who initiated the table alteration operation.
* @param identifier The identifier of the table that was attempted to be altered.
* @param exception The exception that was thrown during the table alteration operation.
* @param tableChanges The changes that were attempted on the table.
*/
public AlterTableFailureEvent(
String user, NameIdentifier identifier, Exception exception, TableChange[] tableChanges) {
super(user, identifier, exception);
this.tableChanges = tableChanges.clone();
}

/**
* Retrieves the changes that were attempted on the table.
*
* @return An array of {@link TableChange} objects representing the attempted modifications to the
* table.
*/
public TableChange[] tableChanges() {
return tableChanges;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
import com.datastrato.gravitino.annotation.DeveloperApi;
import com.datastrato.gravitino.listener.api.info.TableInfo;

/**
* Represents an event triggered upon the successful creation of a table. This class extends {@link
* TableEvent} to provide detailed information specifically related to the table's creation. This
* includes the final table information as it is returned to the user once the creation process has
* successfully completed.
*
* <p>Such an event is instrumental for a variety of purposes including, but not limited to,
* auditing activities associated with table creation, monitoring the creation of tables within a
* system, and acquiring insights into the final configuration and state of a newly created table.
*/
/** Represents an event triggered upon the successful creation of a table. */
@DeveloperApi
public final class CreateTableEvent extends TableEvent {
private final TableInfo createdTableInfo;
Expand All @@ -27,29 +18,21 @@ public final class CreateTableEvent extends TableEvent {
* Constructs an instance of {@code CreateTableEvent}, capturing essential details about the
* successful creation of a table.
*
* <p>This constructor documents the successful culmination of the table creation process by
* encapsulating the final state of the table. This includes any adjustments or resolutions made
* to the table's properties or configuration during the creation process.
*
* @param user The username of the individual who initiated the table creation. This information
* is vital for tracking the source of changes and for audit trails.
* @param identifier The unique identifier of the table that was created, providing a precise
* reference to the affected table.
* @param createdTableInfo The final state of the table post-creation. This information is
* reflective of the table's configuration, including any default settings or properties
* applied during the creation process.
* @param user The username of the individual who initiated the table creation.
* @param identifier The unique identifier of the table that was created.
* @param createdTableInfo The final state of the table post-creation.
*/
public CreateTableEvent(String user, NameIdentifier identifier, TableInfo createdTableInfo) {
super(user, identifier);
this.createdTableInfo = createdTableInfo;
}

/**
* Retrieves the final state and configuration information of the table as it was returned to the
* user after successful creation.
* Retrieves the final state of the table as it was returned to the user after successful
* creation.
*
* @return A {@link TableInfo} instance encapsulating the comprehensive details of the newly
* created table, highlighting its configuration and any default settings applied.
* created table.
*/
public TableInfo createdTableInfo() {
return createdTableInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.annotation.DeveloperApi;
import com.datastrato.gravitino.listener.api.info.TableInfo;
import com.datastrato.gravitino.rel.Table;

/**
* Represents an event that is generated when an attempt to create a table fails due to an
* exception. This class extends {@link TableFailureEvent} to specifically address failure scenarios
* encountered during the table creation process. It encapsulates both the exception that caused the
* failure and the original request details for the table creation, providing comprehensive context
* for the failure.
* exception.
*/
@DeveloperApi
public final class CreateTableFailureEvent extends TableFailureEvent {
Expand All @@ -25,10 +21,8 @@ public final class CreateTableFailureEvent extends TableFailureEvent {
* Constructs a {@code CreateTableFailureEvent} instance, capturing detailed information about the
* failed table creation attempt.
*
* @param user The user who initiated the table creation operation. This information is essential
* for auditing and diagnosing the cause of the failure.
* @param identifier The identifier of the table that was attempted to be created. This helps in
* pinpointing the specific table related to the failure.
* @param user The user who initiated the table creation operation.
* @param identifier The identifier of the table that was attempted to be created.
* @param exception The exception that was thrown during the table creation operation, providing
* insight into what went wrong.
* @param createTableRequest The original request information used to attempt to create the table.
Expand All @@ -42,12 +36,10 @@ public CreateTableFailureEvent(
}

/**
* Retrieves the original request information for the attempted table creation. This information
* can be valuable for understanding the configuration and expectations that led to the failure,
* facilitating analysis and potential corrective actions.
* Retrieves the original request information for the attempted table creation.
*
* @return The {@link Table} instance representing the request information for the failed table
* creation attempt.
* @return The {@link TableInfo} instance representing the request information for the failed
* table creation attempt.
*/
public TableInfo createTableRequest() {
return createTableRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

/**
* Represents an event that is generated after a table is successfully dropped from the database.
* This class extends {@link TableEvent} to capture specific details related to the dropping of a
* table, including the status of the table's existence at the time of the operation and identifying
* information about the table and the user who initiated the drop operation.
*/
@DeveloperApi
public final class DropTableEvent extends TableEvent {
Expand All @@ -22,12 +19,9 @@ public final class DropTableEvent extends TableEvent {
* Constructs a new {@code DropTableEvent} instance, encapsulating information about the outcome
* of a table drop operation.
*
* @param user The user who initiated the drop table operation. This information is important for
* auditing purposes and understanding who is responsible for the change.
* @param identifier The identifier of the table that was attempted to be dropped. This provides a
* clear reference to the specific table affected by the operation.
* @param user The user who initiated the drop table operation.
* @param identifier The identifier of the table that was attempted to be dropped.
* @param isExists A boolean flag indicating whether the table existed at the time of the drop
* operation. This can be useful to understand the state of the database prior to the
* operation.
*/
public DropTableEvent(String user, NameIdentifier identifier, boolean isExists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,16 @@

/**
* Represents an event that is generated when an attempt to drop a table from the database fails due
* to an exception. This class extends {@link TableFailureEvent} to provide specific context related
* to table drop failures, encapsulating details about the user who initiated the operation, the
* identifier of the table that was attempted to be dropped, and the exception that led to the
* failure. This event can be used for auditing purposes and to facilitate error handling and
* diagnostic processes.
* to an exception.
*/
@DeveloperApi
public final class DropTableFailureEvent extends TableFailureEvent {
/**
* Constructs a new {@code DropTableFailureEvent} instance, capturing detailed information about
* the failed attempt to drop a table.
*
* @param user The user who initiated the drop table operation. This information is crucial for
* understanding the context of the operation and for auditing who is responsible for the
* attempted change.
* @param identifier The identifier of the table that the operation attempted to drop. This
* provides a clear reference to the specific table involved in the failure.
* @param user The user who initiated the drop table operation.
* @param identifier The identifier of the table that the operation attempted to drop.
* @param exception The exception that was thrown during the drop table operation, offering
* insights into what went wrong and why the operation failed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.annotation.DeveloperApi;
import javax.annotation.Nullable;

/**
* The abstract base class for all events. It encapsulates common information such as the user who
Expand All @@ -16,7 +17,7 @@
@DeveloperApi
public abstract class Event {
private final String user;
private final NameIdentifier identifier;
@Nullable private final NameIdentifier identifier;

/**
* Constructs an Event instance with the specified user and resource identifier details.
Expand All @@ -43,9 +44,13 @@ public String user() {
/**
* Retrieves the resource identifier associated with this event.
*
* <p>For list operations within a namespace, the identifier is the identifier corresponds to that
* namespace. For metalake list operation, identifier is null.
*
* @return A NameIdentifier object that represents the resource, like a metalake, catalog, schema,
* table, etc., associated with the event.
*/
@Nullable
public NameIdentifier identifier() {
return identifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public abstract class FailureEvent extends Event {
* Constructs a new {@code FailureEvent} instance with the specified user, resource identifier,
* and the exception that was thrown.
*
* @param user The user associated with the operation that resulted in a failure. This information
* is important for auditing and understanding the context of the failure.
* @param user The user associated with the operation that resulted in a failure.
* @param identifier The identifier of the resource involved in the operation that failed. This
* provides a clear reference to what was being acted upon when the exception occurred.
* @param exception The exception that was thrown during the operation. This is the primary piece
Expand Down
Loading

0 comments on commit 0ff45cd

Please sign in to comment.