mchades commented on code in PR #14975:
URL: https://github.com/apache/gravitino/pull/14975#discussion_r4584740244


##########
clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/BaseGVFSOperations.java:
##########
@@ -697,9 +771,7 @@ protected FileSystem getActualFileSystem(Path filesetPath, 
String locationName)
    * @return the fileset catalog.
    */
   protected FilesetCatalog getFilesetCatalog(NameIdentifier catalogIdent) {
-    return getFilesetMetadataCache()
-        .map(cache -> cache.getFilesetCatalog(catalogIdent))
-        .orElseGet(() -> 
getGravitinoClient().loadCatalog(catalogIdent.name()).asFilesetCatalog());
+    return loadCatalog(catalogIdent).asFilesetCatalog();

Review Comment:
   This changes which file `open` reads when a custom operations class supplies 
a `FilesetMetadataCache` that overrides `getFilesetCatalog`. I tested with two 
local files. The base commit read the path returned by the cache override. This 
commit skips the override, calls `cache.getCatalog(id).asFilesetCatalog()`, and 
reads the other file. Can we keep calling `cache.getFilesetCatalog(id)` when 
the metadata cache is enabled and apply the new deduplication to the uncached 
path?



##########
clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/BaseGVFSOperations.java:
##########
@@ -599,6 +603,76 @@ protected Path getActualFilePath(
     return new Path(fileLocation);
   }
 
+  /**
+   * Shares the catalogs and filesets loaded from the server among the calls 
made until the scope is
+   * closed. Only the outermost scope on a thread takes effect, and only for 
the instance that
+   * opened it.
+   */
+  final class OperationScope implements AutoCloseable {
+    private final boolean outermost = CURRENT_SCOPE.get() == null;

Review Comment:
   In `testNestedOperationsInstancesDoNotShareScope`, I counted two 
`loadCatalog` calls and two `loadFileset` calls on `inner` during one 
resolution. The outer instance already occupies the thread-local, so the inner 
scope is never installed and `currentScope()` returns null for it. Making the 
thread-local an instance field reduced both inner counts to one in the same 
test. Can we keep a scope per instance and assert the inner counts too?



##########
clients/filesystem-hadoop3/src/test/java/org/apache/gravitino/filesystem/hadoop/TestGvfsBase.java:
##########
@@ -1713,6 +1720,250 @@ public void testFailureHandlingMethodParameters()
     }
   }
 
+  @ParameterizedTest
+  @CsvSource({
+    "open, , false",
+    "create, , false",
+    "append, , false",
+    "delete, , false",
+    "mkdirs, , false",
+    "getFileStatus, , false",
+    "listStatus, , false",
+    "getDefaultBlockSize, , false",
+    "getDefaultReplication, , false",
+    "setWorkingDirectory, , false",
+    "rename, , false",
+    "getFileStatus, location2, false",
+    "rename, location2, false",
+    "open, , true",
+    "rename, , true"
+  })
+  public void testMetadataRestCallsPerOperation(
+      String operation, String locationName, boolean metadataCache) throws 
IOException {
+    Assumptions.assumeTrue(getClass() == TestGvfsBase.class);
+    String filesetName =
+        "testMetadataRestCalls_"
+            + operation
+            + (locationName == null ? "" : "_" + locationName)
+            + (metadataCache ? "_cached" : "");
+    Path managedFilesetPath =
+        FileSystemTestUtils.createFilesetPath(catalogName, schemaName, 
filesetName, true);
+    Path localPath = FileSystemTestUtils.createLocalDirPrefix(catalogName, 
schemaName, filesetName);
+    String catalogPath = String.format("/api/metalakes/%s/catalogs/%s", 
metalakeName, catalogName);
+    String filesetPath =
+        String.format(
+            "%s/schemas/%s/filesets/%s",
+            catalogPath, schemaName, RESTUtils.encodeString(filesetName));
+    String locationPath = filesetPath + "/location";
+
+    // Pin the metadata cache setting so the expected counts do not depend on 
a default defined
+    // elsewhere.
+    Configuration opsConf = new Configuration(conf);
+    opsConf.setBoolean(
+        
GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_FILESET_METADATA_CACHE_ENABLE,
+        metadataCache);
+    Map<String, String> storageLocations;
+    if (locationName == null) {
+      storageLocations = ImmutableMap.of("location1", localPath.toString());
+    } else {
+      // The default location uses a scheme no provider serves, so resolving 
the file system
+      // against the default location instead of the current one fails the 
operation.
+      opsConf.set(
+          
GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_CURRENT_LOCATION_NAME, 
locationName);
+      storageLocations =
+          ImmutableMap.of(
+              "location1",
+              "unknown-scheme://bucket/" + filesetName,
+              locationName,
+              localPath.toString());
+    }
+
+    try (FileSystem gravitinoFileSystem = 
managedFilesetPath.getFileSystem(opsConf);
+        FileSystem localFileSystem = localPath.getFileSystem(conf)) {
+      FileSystemTestUtils.mkdirs(new Path(localPath + "/dir"), 
localFileSystem);
+      localFileSystem.create(new Path(localPath + "/test.txt"), true).close();
+      localFileSystem.delete(new Path(localPath + "/renamed.txt"), false);
+      mockFilesetDTO(
+          metalakeName,
+          catalogName,
+          schemaName,
+          filesetName,
+          Fileset.Type.MANAGED,
+          storageLocations,
+          ImmutableMap.of(PROPERTY_DEFAULT_LOCATION_NAME, "location1"));
+      for (String subPath : new String[] {"/test.txt", "/renamed.txt", 
"/dir"}) {
+        // With a current location name, the location lookup only matches when 
the client sends it.
+        Map<String, String> queryParams =
+            locationName == null
+                ? ImmutableMap.of("sub_path", subPath)
+                : ImmutableMap.of("sub_path", subPath, "location_name", 
locationName);
+        buildMockResource(
+            Method.GET,
+            locationPath,
+            queryParams,
+            null,
+            new FileLocationResponse(localPath + subPath),
+            SC_OK);
+      }
+      buildMockResourceForCredential(filesetName, localPath.toString());
+
+      Path filePath = new Path(managedFilesetPath + "/test.txt");
+      Path renamedPath = new Path(managedFilesetPath + "/renamed.txt");
+      Path dirPath = new Path(managedFilesetPath + "/dir");
+      if (metadataCache) {
+        // Warm the metadata cache with another operation on the same fileset.
+        assertTrue(gravitinoFileSystem.getFileStatus(filePath).isFile());
+      }
+      int catalogLoads = recordedGetRequests(catalogPath);
+      int filesetLoads = recordedGetRequests(filesetPath);
+      int locationLookups = recordedGetRequests(locationPath);
+      int srcLocationLookups = recordedGetRequests(locationPath, "/test.txt");
+      int dstLocationLookups = recordedGetRequests(locationPath, 
"/renamed.txt");
+
+      switch (operation) {
+        case "open":
+          gravitinoFileSystem.open(filePath, 4096).close();
+          break;
+        case "create":
+          gravitinoFileSystem
+              .create(filePath, FsPermission.getFileDefault(), true, 4096, 
(short) 1, 1024L, null)
+              .close();
+          break;
+        case "append":
+          // The local file system does not support append, but the metadata 
is resolved first.
+          assertThrows(
+              UnsupportedOperationException.class,
+              () -> gravitinoFileSystem.append(filePath, 4096, null));
+          break;
+        case "delete":
+          assertTrue(gravitinoFileSystem.delete(filePath, false));
+          break;
+        case "mkdirs":
+          assertTrue(gravitinoFileSystem.mkdirs(dirPath, 
FsPermission.getDirDefault()));
+          break;
+        case "getFileStatus":
+          assertTrue(gravitinoFileSystem.getFileStatus(filePath).isFile());
+          break;
+        case "listStatus":
+          assertEquals(0, gravitinoFileSystem.listStatus(dirPath).length);
+          break;
+        case "getDefaultBlockSize":
+          gravitinoFileSystem.getDefaultBlockSize(filePath);
+          break;
+        case "getDefaultReplication":
+          gravitinoFileSystem.getDefaultReplication(filePath);
+          break;
+        case "setWorkingDirectory":
+          gravitinoFileSystem.setWorkingDirectory(dirPath);
+          break;
+        case "rename":
+          assertTrue(gravitinoFileSystem.rename(filePath, renamedPath));
+          break;
+        default:
+          throw new IllegalArgumentException("Unknown operation: " + 
operation);
+      }
+
+      catalogLoads = recordedGetRequests(catalogPath) - catalogLoads;
+      filesetLoads = recordedGetRequests(filesetPath) - filesetLoads;
+      locationLookups = recordedGetRequests(locationPath) - locationLookups;
+      // Expected values come from the call tree in issue #12856: one 
operation needs the catalog,
+      // the fileset and the file location once each, and a rename looks up 
the location of both
+      // its source and its destination. With the metadata cache enabled, the 
warm-up operation has
+      // already cached the catalog and the fileset, while file locations are 
never cached.
+      int expectedLoads = metadataCache ? 0 : 1;
+      assertEquals(expectedLoads, catalogLoads, operation + " catalog loads");
+      assertEquals(expectedLoads, filesetLoads, operation + " fileset loads");
+      assertEquals(
+          "rename".equals(operation) ? 2 : 1,
+          locationLookups,
+          operation + " must look up each file location exactly once");
+      if ("rename".equals(operation)) {
+        // The local file system also renames a path to itself, so only the 
per-path counts catch a
+        // destination resolved from the source path.
+        assertEquals(
+            1,
+            recordedGetRequests(locationPath, "/test.txt") - 
srcLocationLookups,
+            "rename source location lookups");
+        assertEquals(
+            1,
+            recordedGetRequests(locationPath, "/renamed.txt") - 
dstLocationLookups,
+            "rename destination location lookups");
+      }
+      localFileSystem.delete(localPath, true);
+    }
+  }
+
+  @ParameterizedTest
+  @CsvSource({
+    "LocationNameHookOperations, fsByName fsByName",
+    "PathHookOperations, fs fs",
+    "FilePathHookOperations, path:test.txt path:test.txt path:renamed.txt",
+    "AllHooksOperations, fs fsByName fileset catalog catalog path:test.txt 
catalog"
+        + " path:test.txt catalog path:renamed.txt catalog fs fsByName fileset 
catalog catalog"
+  })
+  public void testCustomOperationsResolutionHooks(String operationsClass, 
String expectedHookCalls)
+      throws IOException {
+    Assumptions.assumeTrue(getClass() == TestGvfsBase.class);
+    String filesetName = "testCustomOperationsResolutionHooks_" + 
operationsClass;
+    Path managedFilesetPath =
+        FileSystemTestUtils.createFilesetPath(catalogName, schemaName, 
filesetName, true);
+    Path localPath = FileSystemTestUtils.createLocalDirPrefix(catalogName, 
schemaName, filesetName);
+    String catalogPath = String.format("/api/metalakes/%s/catalogs/%s", 
metalakeName, catalogName);
+    String locationPath =
+        String.format(
+            "%s/schemas/%s/filesets/%s/location",
+            catalogPath, schemaName, RESTUtils.encodeString(filesetName));
+    Configuration opsConf = new Configuration(conf);

Review Comment:
   Please pin the metadata cache to disabled and automatic location creation to 
enabled in this test. With the metadata cache enabled in the inherited Hadoop 
configuration, `FilePathHookOperations` loads the catalog once across open and 
rename, so the assertion expecting two fails. With `autoCreateLocation=false`, 
`AllHooksOperations` makes two fewer catalog hook calls and the trace assertion 
fails. Both settings affect the expectations here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to