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 89b8b82df0 [#7371] fix(fileset): fix get file location with multiple 
locations (#7372)
89b8b82df0 is described below

commit 89b8b82df02fbe29a10c59f77031d6be7720cd18
Author: XiaoZ <[email protected]>
AuthorDate: Wed Jun 11 23:00:46 2025 +0800

    [#7371] fix(fileset): fix get file location with multiple locations (#7372)
    
    ### What changes were proposed in this pull request?
    
    Fix get file location with multiple locations.
    
    ### Why are the changes needed?
    
    Fix: #7371
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    UT.
    
    Co-authored-by: zhanghan <[email protected]>
---
 .../hadoop/SecureHadoopCatalogOperations.java      |  5 +-
 .../hadoop/TestHadoopCatalogOperations.java        | 56 ++++++++++++++++++++++
 2 files changed, 59 insertions(+), 2 deletions(-)

diff --git 
a/catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/SecureHadoopCatalogOperations.java
 
b/catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/SecureHadoopCatalogOperations.java
index 1fa3ef60d7..4235c14036 100644
--- 
a/catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/SecureHadoopCatalogOperations.java
+++ 
b/catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/SecureHadoopCatalogOperations.java
@@ -295,7 +295,8 @@ public class SecureHadoopCatalogOperations
         .collect(Collectors.toList());
   }
 
-  private String getTargetLocation(Fileset fileset) {
+  @VisibleForTesting
+  protected String getTargetLocation(Fileset fileset) {
     CallerContext callerContext = CallerContext.CallerContextHolder.get();
     String targetLocationName;
     String targetLocation;
@@ -330,7 +331,7 @@ public class SecureHadoopCatalogOperations
           StringUtils.isNotBlank(targetLocationName),
           "The default location name of the fileset %s should not be empty.",
           fileset.name());
-      targetLocation = 
fileset.properties().get(PROPERTY_DEFAULT_LOCATION_NAME);
+      targetLocation = fileset.storageLocations().get(targetLocationName);
     }
 
     Preconditions.checkArgument(
diff --git 
a/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
 
b/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
index ff4924381e..3b91a008b1 100644
--- 
a/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
+++ 
b/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
@@ -41,6 +41,7 @@ import static 
org.apache.gravitino.file.Fileset.LOCATION_NAME_UNKNOWN;
 import static org.apache.gravitino.file.Fileset.PROPERTY_DEFAULT_LOCATION_NAME;
 import static 
org.apache.gravitino.file.Fileset.PROPERTY_MULTIPLE_LOCATIONS_PREFIX;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mockStatic;
 import static org.mockito.Mockito.when;
 
 import com.google.common.collect.ImmutableMap;
@@ -50,6 +51,7 @@ import java.io.IOException;
 import java.net.ConnectException;
 import java.nio.file.Paths;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -75,6 +77,7 @@ import org.apache.gravitino.connector.CatalogInfo;
 import org.apache.gravitino.connector.HasPropertyMetadata;
 import org.apache.gravitino.connector.PropertiesMetadata;
 import org.apache.gravitino.connector.PropertyEntry;
+import org.apache.gravitino.credential.CredentialConstants;
 import org.apache.gravitino.exceptions.GravitinoRuntimeException;
 import org.apache.gravitino.exceptions.NoSuchFilesetException;
 import org.apache.gravitino.exceptions.NoSuchSchemaException;
@@ -1673,6 +1676,59 @@ public class TestHadoopCatalogOperations {
     }
   }
 
+  @Test
+  public void testGetTargetLocation() throws IOException {
+    try (SecureHadoopCatalogOperations ops = new 
SecureHadoopCatalogOperations(store)) {
+      ops.initialize(
+          Collections.emptyMap(), randomCatalogInfo("m1", "c1"), 
HADOOP_PROPERTIES_METADATA);
+
+      Fileset fileset = Mockito.mock(Fileset.class);
+      when(fileset.name()).thenReturn("fileset_single_location");
+      when(fileset.storageLocations())
+          .thenReturn(ImmutableMap.of(LOCATION_NAME_UNKNOWN, "file://a/b/c"));
+      when(fileset.properties())
+          .thenReturn(ImmutableMap.of(PROPERTY_DEFAULT_LOCATION_NAME, 
LOCATION_NAME_UNKNOWN));
+      Assertions.assertEquals("file://a/b/c", ops.getTargetLocation(fileset));
+
+      try (MockedStatic<CallerContext.CallerContextHolder> callerContextHolder 
=
+          mockStatic(CallerContext.CallerContextHolder.class)) {
+        CallerContext callerContext = Mockito.mock(CallerContext.class);
+        when(callerContext.context())
+            .thenReturn(
+                ImmutableMap.of(
+                    CredentialConstants.HTTP_HEADER_CURRENT_LOCATION_NAME, 
LOCATION_NAME_UNKNOWN));
+        
callerContextHolder.when(CallerContext.CallerContextHolder::get).thenReturn(callerContext);
+        Assertions.assertEquals("file://a/b/c", 
ops.getTargetLocation(fileset));
+      }
+
+      Fileset filesetWithMultipleLocation = Mockito.mock(Fileset.class);
+      
when(filesetWithMultipleLocation.name()).thenReturn("fileset_multiple_location");
+      when(filesetWithMultipleLocation.storageLocations())
+          .thenReturn(
+              ImmutableMap.of(
+                  LOCATION_NAME_UNKNOWN,
+                  "file://a/b/c",
+                  "location_1",
+                  "file://a/b/d",
+                  "location_2",
+                  "file://a/b/e"));
+      when(filesetWithMultipleLocation.properties())
+          .thenReturn(ImmutableMap.of(PROPERTY_DEFAULT_LOCATION_NAME, 
"location_1"));
+      Assertions.assertEquals("file://a/b/d", 
ops.getTargetLocation(filesetWithMultipleLocation));
+
+      try (MockedStatic<CallerContext.CallerContextHolder> callerContextHolder 
=
+          mockStatic(CallerContext.CallerContextHolder.class)) {
+        CallerContext callerContext = Mockito.mock(CallerContext.class);
+        when(callerContext.context())
+            .thenReturn(
+                ImmutableMap.of(
+                    CredentialConstants.HTTP_HEADER_CURRENT_LOCATION_NAME, 
"location_2"));
+        
callerContextHolder.when(CallerContext.CallerContextHolder::get).thenReturn(callerContext);
+        Assertions.assertEquals("file://a/b/e", 
ops.getTargetLocation(filesetWithMultipleLocation));
+      }
+    }
+  }
+
   private static Stream<Arguments> multipleLocationsArguments() {
     return Stream.of(
         // Honor the catalog location

Reply via email to