diff --git a/core/src/main/java/org/apache/gravitino/listener/FilesetEventDispatcher.java b/core/src/main/java/org/apache/gravitino/listener/FilesetEventDispatcher.java index 7b1d2f070a..79274d57b2 100644 --- a/core/src/main/java/org/apache/gravitino/listener/FilesetEventDispatcher.java +++ b/core/src/main/java/org/apache/gravitino/listener/FilesetEventDispatcher.java @@ -32,16 +32,22 @@ import org.apache.gravitino.file.FilesetChange; import org.apache.gravitino.listener.api.event.AlterFilesetEvent; import org.apache.gravitino.listener.api.event.AlterFilesetFailureEvent; +import org.apache.gravitino.listener.api.event.AlterFilesetPreEvent; import org.apache.gravitino.listener.api.event.CreateFilesetEvent; import org.apache.gravitino.listener.api.event.CreateFilesetFailureEvent; +import org.apache.gravitino.listener.api.event.CreateFilesetPreEvent; import org.apache.gravitino.listener.api.event.DropFilesetEvent; import org.apache.gravitino.listener.api.event.DropFilesetFailureEvent; +import org.apache.gravitino.listener.api.event.DropFilesetPreEvent; import org.apache.gravitino.listener.api.event.GetFileLocationEvent; import org.apache.gravitino.listener.api.event.GetFileLocationFailureEvent; +import org.apache.gravitino.listener.api.event.GetFileLocationPreEvent; import org.apache.gravitino.listener.api.event.ListFilesetEvent; import org.apache.gravitino.listener.api.event.ListFilesetFailureEvent; +import org.apache.gravitino.listener.api.event.ListFilesetPreEvent; import org.apache.gravitino.listener.api.event.LoadFilesetEvent; import org.apache.gravitino.listener.api.event.LoadFilesetFailureEvent; +import org.apache.gravitino.listener.api.event.LoadFilesetPreEvent; import org.apache.gravitino.listener.api.info.FilesetInfo; import org.apache.gravitino.utils.PrincipalUtils; @@ -62,6 +68,7 @@ public FilesetEventDispatcher(EventBus eventBus, FilesetDispatcher dispatcher) { @Override public NameIdentifier[] listFilesets(Namespace namespace) throws NoSuchSchemaException { + eventBus.dispatchEvent(new ListFilesetPreEvent(PrincipalUtils.getCurrentUserName(), namespace)); try { NameIdentifier[] nameIdentifiers = dispatcher.listFilesets(namespace); eventBus.dispatchEvent(new ListFilesetEvent(PrincipalUtils.getCurrentUserName(), namespace)); @@ -75,6 +82,7 @@ public NameIdentifier[] listFilesets(Namespace namespace) throws NoSuchSchemaExc @Override public Fileset loadFileset(NameIdentifier ident) throws NoSuchFilesetException { + eventBus.dispatchEvent(new LoadFilesetPreEvent(PrincipalUtils.getCurrentUserName(), ident)); try { Fileset fileset = dispatcher.loadFileset(ident); eventBus.dispatchEvent( @@ -96,6 +104,10 @@ public Fileset createFileset( String storageLocation, Map properties) throws NoSuchSchemaException, FilesetAlreadyExistsException { + FilesetInfo createFileRequest = + new FilesetInfo(ident.name(), comment, type, storageLocation, properties, null); + eventBus.dispatchEvent( + new CreateFilesetPreEvent(PrincipalUtils.getCurrentUserName(), ident, createFileRequest)); try { Fileset fileset = dispatcher.createFileset(ident, comment, type, storageLocation, properties); eventBus.dispatchEvent( @@ -116,6 +128,8 @@ public Fileset createFileset( @Override public Fileset alterFileset(NameIdentifier ident, FilesetChange... changes) throws NoSuchFilesetException, IllegalArgumentException { + eventBus.dispatchEvent( + new AlterFilesetPreEvent(PrincipalUtils.getCurrentUserName(), ident, changes)); try { Fileset fileset = dispatcher.alterFileset(ident, changes); eventBus.dispatchEvent( @@ -131,6 +145,7 @@ public Fileset alterFileset(NameIdentifier ident, FilesetChange... changes) @Override public boolean dropFileset(NameIdentifier ident) { + eventBus.dispatchEvent(new DropFilesetPreEvent(PrincipalUtils.getCurrentUserName(), ident)); try { boolean isExists = dispatcher.dropFileset(ident); eventBus.dispatchEvent( @@ -146,6 +161,8 @@ public boolean dropFileset(NameIdentifier ident) { @Override public String getFileLocation(NameIdentifier ident, String subPath) throws NoSuchFilesetException { + eventBus.dispatchEvent( + new GetFileLocationPreEvent(PrincipalUtils.getCurrentUserName(), ident, subPath)); try { String actualFileLocation = dispatcher.getFileLocation(ident, subPath); // get the audit info from the thread local context diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/AlterFilesetPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterFilesetPreEvent.java new file mode 100644 index 0000000000..f0ba37a18e --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterFilesetPreEvent.java @@ -0,0 +1,55 @@ +/* + * 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.file.FilesetChange; + +@DeveloperApi +public final class AlterFilesetPreEvent extends FilesetPreEvent { + private final FilesetChange[] filesetChanges; + + public AlterFilesetPreEvent( + String user, NameIdentifier identifier, FilesetChange[] filesetChanges) { + super(user, identifier); + this.filesetChanges = filesetChanges; + } + + /** + * Retrieves the specific changes that were made to the fileset during the alteration process. + * + * @return An array of {@link FilesetChange} objects detailing each modification applied to the + * fileset. + */ + public FilesetChange[] filesetChanges() { + return filesetChanges; + } + + /** + * Returns the type of operation. + * + * @return the operation type. + */ + @Override + public OperationType operationType() { + return OperationType.ALTER_FILESET; + } +} diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/CreateFilesetPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/CreateFilesetPreEvent.java new file mode 100644 index 0000000000..f9d471394f --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/CreateFilesetPreEvent.java @@ -0,0 +1,56 @@ +/* + * 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.FilesetInfo; + +/** Represents an event triggered before creating a fileset. */ +@DeveloperApi +public class CreateFilesetPreEvent extends FilesetPreEvent { + private final FilesetInfo createFilesetRequest; + + public CreateFilesetPreEvent( + String user, NameIdentifier identifier, FilesetInfo createFilesetRequest) { + super(user, identifier); + this.createFilesetRequest = createFilesetRequest; + } + + /** + * Retrieves the create fileset request. + * + * @return A {@link FilesetInfo} instance encapsulating the comprehensive details of create + * fileset request. + */ + public FilesetInfo createFilesetRequest() { + return createFilesetRequest; + } + + /** + * Returns the type of operation. + * + * @return the operation type. + */ + @Override + public OperationType operationType() { + return OperationType.CREATE_FILESET; + } +} diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/DropFilesetPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/DropFilesetPreEvent.java new file mode 100644 index 0000000000..07470ca719 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/DropFilesetPreEvent.java @@ -0,0 +1,41 @@ +/* + * 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, + * 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 triggered before dropping a fileset. */ +@DeveloperApi +public final class DropFilesetPreEvent extends FilesetPreEvent { + public DropFilesetPreEvent(String user, NameIdentifier identifier) { + super(user, identifier); + } + + /** + * Returns the type of operation. + * + * @return the operation type. + */ + @Override + public OperationType operationType() { + return OperationType.DROP_FILESET; + } +} diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/FilesetPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/FilesetPreEvent.java new file mode 100644 index 0000000000..f80cdf0b87 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/FilesetPreEvent.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 fileset operations. */ +@DeveloperApi +public abstract class FilesetPreEvent extends PreEvent { + protected FilesetPreEvent(String user, NameIdentifier identifier) { + super(user, identifier); + } +} diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetFileLocationPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetFileLocationPreEvent.java new file mode 100644 index 0000000000..6250234ba3 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetFileLocationPreEvent.java @@ -0,0 +1,60 @@ +/* + * 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 triggered before attempting to get an actual file location. */ +@DeveloperApi +public final class GetFileLocationPreEvent extends FilesetPreEvent { + private final String subPath; + + /** + * Constructs a new {@code GetFileLocationPreEvent}, recording the intent to get a file location. + * + * @param user The user who initiated the get file location operation. + * @param identifier The identifier of the file location to be accessed. + * @param subPath The accessing sub path of the get file location operation. + */ + public GetFileLocationPreEvent(String user, NameIdentifier identifier, String subPath) { + super(user, identifier); + this.subPath = subPath; + } + + /** + * Get the accessing sub path of the get file location operation. + * + * @return The accessing sub path. + */ + public String subPath() { + return subPath; + } + + /** + * Returns the type of operation. + * + * @return the operation type. + */ + @Override + public OperationType operationType() { + return OperationType.GET_FILESET_LOCATION; + } +} diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/ListFilesetPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/ListFilesetPreEvent.java new file mode 100644 index 0000000000..dc92fd3115 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/ListFilesetPreEvent.java @@ -0,0 +1,54 @@ +/* + * 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 filesets within a namespace. */ +@DeveloperApi +public final class ListFilesetPreEvent extends FilesetPreEvent { + private final Namespace namespace; + + public ListFilesetPreEvent(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 filesets were listed. + */ + public Namespace namespace() { + return namespace; + } + + /** + * Returns the type of operation. + * + * @return the operation type. + */ + @Override + public OperationType operationType() { + return OperationType.LIST_FILESET; + } +} diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/LoadFilesetPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/LoadFilesetPreEvent.java new file mode 100644 index 0000000000..6d731a6be9 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/listener/api/event/LoadFilesetPreEvent.java @@ -0,0 +1,41 @@ +/* + * 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 fileset. */ +@DeveloperApi +public class LoadFilesetPreEvent extends FilesetPreEvent { + public LoadFilesetPreEvent(String user, NameIdentifier identifier) { + super(user, identifier); + } + + /** + * Returns the type of operation. + * + * @return the operation type. + */ + @Override + public OperationType operationType() { + return OperationType.LOAD_FILESET; + } +} diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestFilesetEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestFilesetEvent.java index 5bc4801685..803b38522d 100644 --- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestFilesetEvent.java +++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestFilesetEvent.java @@ -82,6 +82,14 @@ void testCreateFilesetEvent() { checkFilesetInfo(filesetInfo, fileset); Assertions.assertEquals(OperationType.CREATE_FILESET, event.operationType()); Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + + PreEvent preEvent = dummyEventListener.popPreEvent(); + Assertions.assertEquals(identifier, preEvent.identifier()); + Assertions.assertEquals(CreateFilesetPreEvent.class, preEvent.getClass()); + filesetInfo = ((CreateFilesetPreEvent) preEvent).createFilesetRequest(); + checkFilesetInfo(filesetInfo, fileset); + Assertions.assertEquals(OperationType.CREATE_FILESET, preEvent.operationType()); + Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus()); } @Test @@ -95,6 +103,12 @@ void testLoadFilesetEvent() { checkFilesetInfo(filesetInfo, fileset); Assertions.assertEquals(OperationType.LOAD_FILESET, event.operationType()); Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + + PreEvent preEvent = dummyEventListener.popPreEvent(); + Assertions.assertEquals(identifier, preEvent.identifier()); + Assertions.assertEquals(LoadFilesetPreEvent.class, preEvent.getClass()); + Assertions.assertEquals(OperationType.LOAD_FILESET, preEvent.operationType()); + Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus()); } @Test @@ -111,6 +125,14 @@ void testAlterFilesetEvent() { Assertions.assertEquals(change, ((AlterFilesetEvent) event).filesetChanges()[0]); Assertions.assertEquals(OperationType.ALTER_FILESET, event.operationType()); Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + + PreEvent preEvent = dummyEventListener.popPreEvent(); + Assertions.assertEquals(identifier, preEvent.identifier()); + Assertions.assertEquals(AlterFilesetPreEvent.class, preEvent.getClass()); + Assertions.assertEquals(1, ((AlterFilesetPreEvent) preEvent).filesetChanges().length); + Assertions.assertEquals(change, ((AlterFilesetPreEvent) preEvent).filesetChanges()[0]); + Assertions.assertEquals(OperationType.ALTER_FILESET, preEvent.operationType()); + Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus()); } @Test @@ -123,6 +145,12 @@ void testDropFilesetEvent() { Assertions.assertTrue(((DropFilesetEvent) event).isExists()); Assertions.assertEquals(OperationType.DROP_FILESET, event.operationType()); Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + + PreEvent preEvent = dummyEventListener.popPreEvent(); + Assertions.assertEquals(identifier, preEvent.identifier()); + Assertions.assertEquals(DropFilesetPreEvent.class, preEvent.getClass()); + Assertions.assertEquals(OperationType.DROP_FILESET, preEvent.operationType()); + Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus()); } @Test @@ -135,6 +163,13 @@ void testListFilesetEvent() { Assertions.assertEquals(namespace, ((ListFilesetEvent) event).namespace()); Assertions.assertEquals(OperationType.LIST_FILESET, event.operationType()); Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + + PreEvent preEvent = dummyEventListener.popPreEvent(); + Assertions.assertEquals(namespace.toString(), preEvent.identifier().toString()); + Assertions.assertEquals(ListFilesetPreEvent.class, preEvent.getClass()); + Assertions.assertEquals(namespace, ((ListFilesetPreEvent) preEvent).namespace()); + Assertions.assertEquals(OperationType.LIST_FILESET, preEvent.operationType()); + Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus()); } @Test @@ -178,6 +213,12 @@ void testGetFileLocationEvent() { Assertions.assertEquals("test", ((GetFileLocationEvent) event1).subPath()); Assertions.assertEquals(OperationType.GET_FILESET_LOCATION, event1.operationType()); Assertions.assertEquals(OperationStatus.SUCCESS, event1.operationStatus()); + + PreEvent preEvent = dummyEventListener.popPreEvent(); + Assertions.assertEquals(GetFileLocationPreEvent.class, preEvent.getClass()); + Assertions.assertEquals(identifier, preEvent.identifier()); + Assertions.assertEquals(OperationType.GET_FILESET_LOCATION, preEvent.operationType()); + Assertions.assertEquals(OperationStatus.UNPROCESSED, preEvent.operationStatus()); } @Test