Skip to content

Commit

Permalink
[apache#5548] feat(core): add fileset pre event to Gravitino event (a…
Browse files Browse the repository at this point in the history
…pache#5652)

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

add fileset pre event to Gravitino event 

### Why are the changes needed?

Fix: apache#5548 

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

no

### How was this patch tested?

added unit test
  • Loading branch information
TungYuChiang authored Nov 26, 2024
1 parent 59d0628 commit 4dd0440
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand All @@ -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(
Expand All @@ -96,6 +104,10 @@ public Fileset createFileset(
String storageLocation,
Map<String, String> 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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.file.FilesetChange;

/** Represents an event triggered before altering a fileset. */
@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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.gravitino.listener.api.event;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.annotation.DeveloperApi;

/** Represents a pre-event for fileset operations. */
@DeveloperApi
public abstract class FilesetPreEvent extends PreEvent {
protected FilesetPreEvent(String user, NameIdentifier identifier) {
super(user, identifier);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 4dd0440

Please sign in to comment.