This is an automated email from the ASF dual-hosted git repository.
mchades pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 413bee7ebd [#8752] feat(gvfs-java): set operation context in gvfs hook
(#8867)
413bee7ebd is described below
commit 413bee7ebdbeb42c2895b13849dab793b0c11535
Author: Junda Yang <[email protected]>
AuthorDate: Wed Oct 22 01:47:03 2025 -0700
[#8752] feat(gvfs-java): set operation context in gvfs hook (#8867)
### What changes were proposed in this pull request?
add setOperationsContext in hook interface with default implementation.
### Why are the changes needed?
The hook's fallback logic needs to use functions like getFileSystem,
which currently only exist in the operation object. To solve this, we'll
pass the operation context to the hook. This approach allows the hook to
reuse the getFileSystem logic and its cache, simplifying the fallback
implementation.
Fix: #8752
### Does this PR introduce _any_ user-facing change?
No. The default behavior remains unchanged.
### How was this patch tested?
unit tested
---
.../hadoop/GravitinoVirtualFileSystem.java | 2 +
.../hadoop/GravitinoVirtualFileSystemHook.java | 10 +++++
.../gravitino/filesystem/hadoop/MockGVFSHook.java | 8 ++++
.../gravitino/filesystem/hadoop/TestGvfsBase.java | 45 ++++++++++++++++++++++
4 files changed, 65 insertions(+)
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 =