Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8752] feat(gvfs-java): set operation context in gvfs hook (#8867)

Junda Yang413bee7ebdbe
diff --git a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystem.java b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystem.java
index 346c4724b1..8b93d89d10 100644
--- a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystem.java
+++ b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystem.java
@@ -99,6 +99,8 @@ public class GravitinoVirtualFileSystem extends FileSystem {
e, "Cannot create operations instance: %s", operationsClassName);
}
+ hook.setOperationsContext(operations);
+
this.workingDirectory = new Path(name);
this.uri = URI.create(name.getScheme() + "://" + name.getAuthority());
diff --git a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemHook.java b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemHook.java
index 27e8f7684e..a0978035ef 100644
--- a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemHook.java
+++ b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemHook.java
@@ -37,6 +37,16 @@ import org.apache.hadoop.fs.permission.FsPermission;
*/
public interface GravitinoVirtualFileSystemHook extends Closeable {
+ /**
+ * Set the operations context for this hook. This method will be called during GVFS initialization
+ * to provide the hook with access to the BaseGVFSOperations instance.
+ *
+ * @param operations the BaseGVFSOperations instance
+ */
+ default void setOperationsContext(BaseGVFSOperations operations) {
+ // Default implementation does nothing - hooks can override if they need operations access
+ }
+
/**
* Initialize the hook with the configuration. This method will be called in the GVFS initialize
* method, and the configuration will be passed from the GVFS configuration. The implementor can
diff --git a/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/MockGVFSHook.java b/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/MockGVFSHook.java
index 640af6325d..cf0413df4c 100644
--- a/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/MockGVFSHook.java
+++ b/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/MockGVFSHook.java
@@ -29,6 +29,8 @@ import org.apache.hadoop.fs.permission.FsPermission;
public class MockGVFSHook implements GravitinoVirtualFileSystemHook {
+ boolean setOperationsContextCalled = false;
+ BaseGVFSOperations operations = null;
boolean preSetWorkingDirectoryCalled = false;
boolean preOpenCalled = false;
boolean preCreateCalled = false;
@@ -52,6 +54,12 @@ public class MockGVFSHook implements GravitinoVirtualFileSystemHook {
boolean postGetDefaultReplicationCalled = false;
boolean postGetDefaultBlockSizeCalled = false;
+ @Override
+ public void setOperationsContext(BaseGVFSOperations operations) {
+ this.setOperationsContextCalled = true;
+ this.operations = operations;
+ }
+
@Override
public void initialize(Map<String, String> config) {}
diff --git a/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/TestGvfsBase.java b/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/TestGvfsBase.java
index a0c29014fe..ef80150308 100644
--- a/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/TestGvfsBase.java
+++ b/clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/TestGvfsBase.java
@@ -32,6 +32,7 @@ import static org.apache.hc.core5.http.HttpStatus.SC_OK;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -1038,6 +1039,50 @@ public class TestGvfsBase extends GravitinoMockServerBase {
Assertions.assertEquals("Read timed out", throwable.getCause().getMessage());
}
+ @Test
+ public void testHookSetOperationsContext() throws IOException {
+ String filesetName = "testHookSetOperationsContext";
+ Path managedFilesetPath =
+ FileSystemTestUtils.createFilesetPath(catalogName, schemaName, filesetName, true);
+ Path localPath = FileSystemTestUtils.createLocalDirPrefix(catalogName, schemaName, filesetName);
+ String locationPath =
+ String.format(
+ "/api/metalakes/%s/catalogs/%s/schemas/%s/filesets/%s/location",
+ metalakeName, catalogName, schemaName, filesetName);
+
+ try (GravitinoVirtualFileSystem fs =
+ (GravitinoVirtualFileSystem) managedFilesetPath.getFileSystem(conf)) {
+
+ // Verify that setOperationsContext was called during GVFS initialization
+ MockGVFSHook hook = getHook(fs);
+ assertTrue(
+ hook.setOperationsContextCalled,
+ "setOperationsContext should be called during initialization");
+ assertNotNull(hook.operations, "Operations context should not be null");
+ assertEquals(
+ fs.getOperations(),
+ hook.operations,
+ "Hook should have reference to the same operations instance");
+
+ // Verify the hook can access operations methods
+ FileLocationResponse fileLocationResponse = new FileLocationResponse(localPath.toString());
+ Map<String, String> queryParams = new HashMap<>();
+ queryParams.put("sub_path", "");
+ buildMockResource(Method.GET, locationPath, queryParams, null, fileLocationResponse, SC_OK);
+ buildMockResourceForCredential(filesetName, localPath.toString());
+
+ try (FileSystem localFileSystem = localPath.getFileSystem(conf)) {
+ FileSystemTestUtils.mkdirs(localPath, localFileSystem);
+ FileSystemTestUtils.mkdirs(managedFilesetPath, fs);
+
+ // Verify hook's operations context is still valid after operations are performed
+ assertNotNull(hook.operations, "Operations context should remain available");
+ assertTrue(hook.preMkdirsCalled, "Hook should be invoked for operations");
+ assertTrue(hook.postMkdirsCalled, "Hook should be invoked for operations");
+ }
+ }
+ }
+
private void buildMockResourceForCredential(String filesetName, String filesetLocation)
throws JsonProcessingException {
String filesetPath =