This is an automated email from the ASF dual-hosted git repository.

Gargi-jais11 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d4a3932a6d HDDS-17308. ListObjectsV2 Incorrectly Filters 
CommonPrefixes When StartAfter Is Before the Matching Prefixes (#11715).
9d4a3932a6d is described below

commit 9d4a3932a6dd9eaab195112c41be602f58fb2d01
Author: Gargi Jaiswal <[email protected]>
AuthorDate: Mon Oct 26 11:31:17 2026 +0530

    HDDS-17308. ListObjectsV2 Incorrectly Filters CommonPrefixes When 
StartAfter Is Before the Matching Prefixes (#11715).
---
 .../apache/hadoop/ozone/client/OzoneBucket.java    | 13 +++---
 .../ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java   | 46 ++++++++++++++++++++++
 .../org/apache/hadoop/ozone/om/TestListKeys.java   | 28 ++++++++++---
 .../hadoop/ozone/om/TestListKeysWithFSO.java       | 44 ++++++++++++++++-----
 .../hadoop/ozone/om/OmMetadataManagerImpl.java     |  5 ++-
 .../hadoop/ozone/om/TestOmMetadataManager.java     | 38 ++++++++++++++++++
 6 files changed, 151 insertions(+), 23 deletions(-)

diff --git 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java
 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java
index b1674b740b4..0898a2fc763 100644
--- 
a/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java
+++ 
b/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneBucket.java
@@ -2061,16 +2061,15 @@ private boolean prepareStack(String prevKey) throws 
IOException {
             stack.push(seekDirPath);
           }
         } else if (StringUtils.isNotBlank(getKeyPrefix())) {
-          if (!OzoneFSUtils.isAncestorPath(getKeyPrefix(), prevKey)) {
-            // Case-1 - sibling: keyPrefix="a1/b2", startKey="a0/b123Invalid"
-            // Skip traversing, if the startKey is not a sibling.
-            // "a1/b", "a1/b1/e/"
-            return false;
-          } else if (StringUtils.compare(prevKey, getKeyPrefix()) < 0) {
-            // Case-2 - compare: keyPrefix="a1/b2", startKey="a1/b123Invalid"
+          if (StringUtils.compare(prevKey, getKeyPrefix()) < 0) {
+            // Case-1 - compare: keyPrefix="a1/b2", startKey="a1/b123Invalid" 
or "a0/b123Invalid"
             // Since startKey is lexographically behind keyPrefix,
             // the seek precedence goes to keyPrefix.
             stack.push(new ImmutablePair<>(getKeyPrefix(), ""));
+          } else if (!OzoneFSUtils.isAncestorPath(getKeyPrefix(), prevKey)) {
+            // Case-2 - sibling: keyPrefix="a1/b2", startKey="a2/b1"
+            // Skip traversing, if the startKey is not a sibling.
+            return false;
           }
         }
       }
diff --git 
a/hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java
 
b/hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java
index fa3d015adc2..9b3f724210d 100644
--- 
a/hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java
+++ 
b/hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java
@@ -116,6 +116,7 @@
 import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.EnumSource;
 import org.junit.jupiter.params.provider.MethodSource;
 import org.junit.jupiter.params.provider.ValueSource;
 import software.amazon.awssdk.core.ResponseBytes;
@@ -1743,6 +1744,51 @@ public void testListObjectsV2FetchOwner() {
     assertEquals(S3Owner.DEFAULT_S3OWNER_ID, 
trueResponse.contents().get(0).owner().id());
   }
 
+  /**
+   * StartAfter (and the V1 Marker) sorts before Prefix on a different branch, 
and the bucket has keys
+   * between them that do not match Prefix. AWS returns every key and common 
prefix that matches Prefix
+   * and sorts after StartAfter.
+   */
+  @ParameterizedTest
+  @EnumSource(value = BucketLayout.class, names = {"OBJECT_STORE", 
"FILE_SYSTEM_OPTIMIZED"})
+  public void testListObjectsStartAfterBeforePrefix(BucketLayout layout) 
throws Exception {
+    final String bucketName = getBucketName("start-after");
+    try (OzoneClient ozoneClient = cluster.newClient()) {
+      ozoneClient.getObjectStore().getS3Volume().createBucket(bucketName,
+          BucketArgs.newBuilder().setBucketLayout(layout).build());
+    }
+    for (String key : Arrays.asList("dir1/dir2/file1", "dir1/file2", 
"dir1bh/file1", "dir1bha/file1", "dir2/file1")) {
+      s3Client.putObject(b -> b.bucket(bucketName).key(key), 
RequestBody.fromString("x"));
+    }
+    final String prefix = "dir1bh";
+    final String startAfter = "dir1/dir2/file1";
+
+    ListObjectsV2Response v2WithDelimiter = s3Client.listObjectsV2(b -> 
b.bucket(bucketName)
+        .prefix(prefix).delimiter("/").startAfter(startAfter));
+    
assertThat(v2WithDelimiter.commonPrefixes()).extracting(CommonPrefix::prefix)
+        .containsExactly("dir1bh/", "dir1bha/");
+    assertThat(v2WithDelimiter.contents()).isEmpty();
+
+    ListObjectsResponse v1WithDelimiter = s3Client.listObjects(b -> 
b.bucket(bucketName)
+        .prefix(prefix).delimiter("/").marker(startAfter));
+    
assertThat(v1WithDelimiter.commonPrefixes()).extracting(CommonPrefix::prefix)
+        .containsExactly("dir1bh/", "dir1bha/");
+
+    // A non-delimited FSO listing of a Prefix that is also a directory lists 
only that directory,
+    // so the result is compared with the listing without StartAfter.
+    ListObjectsV2Response v2WithoutStartAfter = s3Client.listObjectsV2(b -> 
b.bucket(bucketName).prefix(prefix));
+    assertThat(v2WithoutStartAfter.contents()).isNotEmpty();
+    ListObjectsV2Response v2WithoutDelimiter = s3Client.listObjectsV2(b -> 
b.bucket(bucketName)
+        .prefix(prefix).startAfter(startAfter));
+    assertEquals(v2WithoutStartAfter.contents(), 
v2WithoutDelimiter.contents());
+
+    // StartAfter sorts after every key matching Prefix.
+    ListObjectsV2Response v2AfterPrefix = s3Client.listObjectsV2(b -> 
b.bucket(bucketName)
+        .prefix(prefix).delimiter("/").startAfter("dir1bi"));
+    assertThat(v2AfterPrefix.commonPrefixes()).isEmpty();
+    assertThat(v2AfterPrefix.contents()).isEmpty();
+  }
+
   private void testListObjectsMany(boolean isListV2) throws Exception {
     final String bucketName = getBucketName();
     s3Client.createBucket(b -> b.bucket(bucketName));
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeys.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeys.java
index e983c931c48..b489c33de9c 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeys.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeys.java
@@ -152,8 +152,13 @@ private static void buildNameSpaceTree(OzoneBucket 
ozoneBucket)
   private static Stream<Arguments> shallowListDataWithTrailingSlash() {
     return Stream.of(
 
-        // Case-1: StartKey is less than prefixKey, return emptyList.
-        of("a1/b2/", "a1", newLinkedList(Collections.emptyList())),
+        // Case-1: StartKey is less than prefixKey, return all immediate node.
+        of("a1/b2/", "a1", newLinkedList(Arrays.asList(
+            "a1/b2/",
+            "a1/b2/d1/",
+            "a1/b2/d2/",
+            "a1/b2/d3/"
+        ))),
 
         // Case-2: StartKey is empty, return all immediate node.
         of("a1/b2/", "", newLinkedList(Arrays.asList(
@@ -201,8 +206,12 @@ private static Stream<Arguments> 
shallowListDataWithTrailingSlash() {
   private static Stream<Arguments> shallowListObsDataWithTrailingSlash() {
     return Stream.of(
 
-        // Case-1: StartKey is less than prefixKey, return emptyList.
-        of("a1/b2/", "a1", newLinkedList(Collections.emptyList())),
+        // Case-1: StartKey is less than prefixKey, return all immediate node.
+        of("a1/b2/", "a1", newLinkedList(Arrays.asList(
+            "a1/b2/d1/",
+            "a1/b2/d2/",
+            "a1/b2/d3/"
+        ))),
 
         // Case-2: StartKey is empty, return all immediate node.
         of("a1/b2/", "", newLinkedList(Arrays.asList(
@@ -248,8 +257,15 @@ private static Stream<Arguments> 
shallowListObsDataWithTrailingSlash() {
   private static Stream<Arguments> shallowListDataWithoutTrailingSlash() {
     return Stream.of(
 
-        // Case-1: StartKey is less than prefixKey, return emptyList.
-        of("a1/b2", "a1", newLinkedList(Collections.emptyList())),
+        // Case-1: StartKey is less than prefixKey, return all immediate node.
+        of("a1/b2", "a1", newLinkedList(Arrays.asList(
+            "a1/b2/"
+        ))),
+
+        // Case-1a: StartKey is an existing key on a branch before prefixKey.
+        of("a1/b2", "a1/b1/c1/c1.tx", newLinkedList(Arrays.asList(
+            "a1/b2/"
+        ))),
 
         // Case-2: StartKey is empty, return all immediate node.
         of("a1/b2", "", newLinkedList(Arrays.asList(
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeysWithFSO.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeysWithFSO.java
index 6929dac516a..7e98f5945f8 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeysWithFSO.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestListKeysWithFSO.java
@@ -27,6 +27,7 @@
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -156,11 +157,12 @@ private void initFSNameSpace() throws Exception {
 
   @Test
   public void testListKeysWithValidStartKey() throws Exception {
-    // case-1: StartKey LeafNode is lexographically behind than prefixKey.
-    // So, will return EmptyList
-    // a1/b2 < a1/b2Invalid
-    List<String> expectedKeys =
-        getExpectedKeyList("a1/b2", "a1", legacyOzoneBucket);
+    // case-1: StartKey is lexographically behind prefixKey.
+    // So will return all the sub-paths of prefixKey
+    // a1 < a1/b2
+    List<String> expectedKeys = Arrays.asList("a1/b2/", "a1/b2/d1/", 
"a1/b2/d1/d11.tx", "a1/b2/d2/",
+        "a1/b2/d2/d21.tx", "a1/b2/d2/d22.tx", "a1/b2/d3/", "a1/b2/d3/d31.tx");
+    checkKeyList("a1/b2", "a1", expectedKeys, legacyOzoneBucket);
     checkKeyList("a1/b2", "a1", expectedKeys, fsoOzoneBucket);
 
     // case-2: Same prefixKey and startKey, but with an ending slash
@@ -297,10 +299,12 @@ public void testListKeysWithNonExistentStartKey() throws 
Exception {
     checkKeyList("a1/b1", "a1/b2/d0", expectedKeys, fsoOzoneBucket);
 
     // case-3:
-    // StartKey LeafNode's parent is not matching with than prefixKey's parent.
-    // So, will return EmptyList
-    expectedKeys =
-        getExpectedKeyList("a1/b2", "a0/b123Invalid", legacyOzoneBucket);
+    // StartKey LeafNode's parent is not matching with prefixKey's parent,
+    // but startKey is lexographically behind prefixKey.
+    // So will return all the sub-paths of prefixKey
+    expectedKeys = Arrays.asList("a1/b2/", "a1/b2/d1/", "a1/b2/d1/d11.tx", 
"a1/b2/d2/",
+        "a1/b2/d2/d21.tx", "a1/b2/d2/d22.tx", "a1/b2/d3/", "a1/b2/d3/d31.tx");
+    checkKeyList("a1/b2", "a0/b123Invalid", expectedKeys, legacyOzoneBucket);
     checkKeyList("a1/b2", "a0/b123Invalid", expectedKeys, fsoOzoneBucket);
 
     // case-4: StartKey LeafNode is lexographically behind prefixKey.
@@ -423,6 +427,26 @@ public void testListKeysWithMixOfDirsAndFiles() throws 
Exception {
     checkKeyList("x/y/z/z1.tx", "", expectedKeys, fsoOzoneBucket2);
   }
 
+  /**
+   * StartKey sorts before keyPrefix and lies on a different branch, with keys 
between them
+   * that do not match keyPrefix. Every key matching keyPrefix sorts after 
startKey.
+   */
+  @Test
+  public void testListKeysWithStartKeyBeforePrefix() throws Exception {
+    String keyPrefix = "dir1bh";
+    List<String> expectedKeys = Arrays.asList("dir1bh/", "dir1bh/file1", 
"dir1bha/", "dir1bha/file1");
+    List<String> expectedShallowKeys = Arrays.asList("dir1bh/", "dir1bha/");
+    // A non-shallow FSO listing of a keyPrefix that is also a directory lists 
only that directory,
+    // so the FSO result is compared with the listing without startKey.
+    List<String> expectedFsoKeys = getExpectedKeyList(keyPrefix, "", 
fsoOzoneBucket2);
+    for (String startKey : Arrays.asList("dir1/dir2/dir3/d11.tx", 
"dir1/dir2/file1", "a0")) {
+      checkKeyList(keyPrefix, startKey, expectedKeys, legacyOzoneBucket2);
+      checkKeyList(keyPrefix, startKey, expectedFsoKeys, fsoOzoneBucket2);
+      checkKeyShallowList(keyPrefix, startKey, expectedShallowKeys, 
legacyOzoneBucket2);
+      checkKeyShallowList(keyPrefix, startKey, expectedShallowKeys, 
fsoOzoneBucket2);
+    }
+  }
+
   @Test
   public void testShallowListKeys() throws Exception {
     List<String> expectedKeys;
@@ -593,6 +617,8 @@ private static void buildNameSpaceTree2(OzoneBucket 
ozoneBucket)
     keys.add("/x/y/z/z1.txdir/z2.tx");
 
     keys.add("/dir1/dir2/dir3/d11.tx");
+    keys.add("/dir1bh/file1");
+    keys.add("/dir1bha/file1");
 
     createAndAssertKeys(ozoneBucket, keys);
   }
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
index e59945f249b..7a653f78aa1 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmMetadataManagerImpl.java
@@ -1111,7 +1111,10 @@ public ListKeysResult listKeys(String volumeName, String 
bucketName,
 
     String seekKey;
     boolean skipStartKey = false;
-    if (StringUtils.isNotBlank(startKey)) {
+    // Every key matching keyPrefix sorts after a startKey that sorts before 
keyPrefix,
+    // so seek to keyPrefix; seeking to startKey would stop at the first 
non-matching key.
+    if (StringUtils.isNotBlank(startKey)
+        && (StringUtils.isEmpty(keyPrefix) || startKey.compareTo(keyPrefix) >= 
0)) {
       // Seek to the specified key.
       seekKey = getOzoneKey(volumeName, bucketName, startKey);
       skipStartKey = true;
diff --git 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmMetadataManager.java
 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmMetadataManager.java
index 322948ffbcd..5f569448658 100644
--- 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmMetadataManager.java
+++ 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmMetadataManager.java
@@ -535,6 +535,44 @@ public void testListKeys() throws Exception {
     }
   }
 
+  @Test
+  public void testListKeysWithStartKeyBeforePrefix() throws Exception {
+    String volumeName = "volumeA";
+    String bucketName = "startKeyBeforePrefix";
+    OMRequestTestUtils.addVolumeToDB(volumeName, omMetadataManager);
+    addBucketsToCache(volumeName, bucketName);
+
+    // Even index keys go to the DB and odd index keys to the cache.
+    addKeysToOM(volumeName, bucketName, "dir1/dir2/file1", 0);
+    addKeysToOM(volumeName, bucketName, "dir1/file2", 2);
+    addKeysToOM(volumeName, bucketName, "dir1bh/file1", 4);
+    addKeysToOM(volumeName, bucketName, "dir1bha/file1", 6);
+    addKeysToOM(volumeName, bucketName, "dir1bhb/file1", 1);
+    addKeysToOM(volumeName, bucketName, "dir2/file1", 8);
+
+    List<String> expected = Arrays.asList("dir1bh/file1", "dir1bha/file1", 
"dir1bhb/file1");
+
+    // startKey exists and sorts before keyPrefix, with non-matching keys in 
between.
+    assertEquals(expected, listKeyNames(volumeName, bucketName, 
"dir1/dir2/file1", "dir1bh", 100));
+    // startKey does not exist and sorts before keyPrefix.
+    assertEquals(expected, listKeyNames(volumeName, bucketName, "a", "dir1bh", 
100));
+    assertEquals(expected, listKeyNames(volumeName, bucketName, null, 
"dir1bh", 100));
+
+    // Pagination continues from the last returned key.
+    assertEquals(expected.subList(0, 2), listKeyNames(volumeName, bucketName, 
"dir1/dir2/file1", "dir1bh", 2));
+    assertEquals(expected.subList(2, 3), listKeyNames(volumeName, bucketName, 
"dir1bha/file1", "dir1bh", 2));
+
+    // startKey sorts after every key matching keyPrefix.
+    assertEquals(Collections.emptyList(), listKeyNames(volumeName, bucketName, 
"dir1bi", "dir1bh", 100));
+  }
+
+  private List<String> listKeyNames(String volumeName, String bucketName, 
String startKey, String keyPrefix,
+      int maxKeys) throws IOException {
+    return omMetadataManager.listKeys(volumeName, bucketName, startKey, 
keyPrefix, maxKeys).getKeys().stream()
+        .map(OmKeyInfo::getKeyName)
+        .collect(Collectors.toList());
+  }
+
   @Test
   public void testListKeysWithFewDeleteEntriesInCache() throws Exception {
     String volumeNameA = "volumeA";


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to