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 9598a11b124 HDDS-17564. ListObjects with Prefix=/ never returns when a 
page ends at a key that sorts before / (#12105).
9598a11b124 is described below

commit 9598a11b12479808a672d1a7fe7e0a2b5c86a228
Author: Chung En Lee <[email protected]>
AuthorDate: Wed Nov 25 15:58:37 2026 +0800

    HDDS-17564. ListObjects with Prefix=/ never returns when a page ends at a 
key that sorts before / (#12105).
---
 .../ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java   | 38 ++++++++++++++++++
 .../hadoop/ozone/om/OmMetadataManagerImpl.java     | 18 ++++-----
 .../hadoop/ozone/om/TestOmMetadataManager.java     | 45 ++++++++++++++++++++++
 3 files changed, 92 insertions(+), 9 deletions(-)

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 bcab8ea86bd..9432801e08f 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.Test;
 import org.junit.jupiter.api.TestInstance;
 import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.Timeout;
 import org.junit.jupiter.api.function.Executable;
 import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -2221,6 +2222,43 @@ public void 
testListObjectsStartAfterBeforePrefix(BucketLayout layout) throws Ex
     assertThat(v2AfterPrefix.contents()).isEmpty();
   }
 
+  /**
+   * Prefix "/" matches no key here. On an OBS bucket, S3 Gateway still pages 
through the whole bucket and filters
+   * the keys, so pagination must end when a page ends at a key that sorts 
before "/".
+   */
+  @ParameterizedTest
+  @EnumSource(value = BucketLayout.class, names = {"OBJECT_STORE", 
"FILE_SYSTEM_OPTIMIZED"})
+  @Timeout(120)
+  public void testListObjectsSlashPrefix(BucketLayout layout) throws Exception 
{
+    final String bucketName = getBucketName("slash-prefix");
+    try (OzoneClient ozoneClient = cluster.newClient()) {
+      ozoneClient.getObjectStore().getS3Volume().createBucket(bucketName,
+          BucketArgs.newBuilder().setBucketLayout(layout).build());
+    }
+    s3Client.putObject(b -> b.bucket(bucketName).key(".cache/0"), 
RequestBody.fromString("x"));
+
+    ListObjectsV2Response v2 = s3Client.listObjectsV2(b -> 
b.bucket(bucketName).prefix("/"));
+    assertThat(v2.contents()).isEmpty();
+  }
+
+  /**
+   * FSO removes the leading "/" from key names, so only an OBS bucket can 
have a key matching Prefix "/".
+   */
+  @Test
+  @Timeout(120)
+  public void testListObjectsSlashPrefixMatchesKey() throws Exception {
+    final String bucketName = getBucketName("slash-prefix-key");
+    try (OzoneClient ozoneClient = cluster.newClient()) {
+      ozoneClient.getObjectStore().getS3Volume().createBucket(bucketName,
+          
BucketArgs.newBuilder().setBucketLayout(BucketLayout.OBJECT_STORE).build());
+    }
+    s3Client.putObject(b -> b.bucket(bucketName).key(".cache/0"), 
RequestBody.fromString("x"));
+    s3Client.putObject(b -> b.bucket(bucketName).key("/keep"), 
RequestBody.fromString("x"));
+
+    ListObjectsV2Response v2 = s3Client.listObjectsV2(b -> 
b.bucket(bucketName).prefix("/"));
+    
assertThat(v2.contents()).extracting(S3Object::key).containsExactly("/keep");
+  }
+
   private void testListObjectsMany(boolean isListV2) throws Exception {
     final String bucketName = getBucketName();
     s3Client.createBucket(b -> b.bucket(bucketName));
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 96bc333149a..2da390da6ec 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
@@ -1107,12 +1107,19 @@ public ListKeysResult listKeys(String volumeName, 
String bucketName,
           ResultCodes.BUCKET_NOT_FOUND);
     }
 
+    String seekPrefix;
+    if (StringUtils.isNotEmpty(keyPrefix)) {
+      seekPrefix = getOzoneKey(volumeName, bucketName, keyPrefix);
+    } else {
+      seekPrefix = getBucketKey(volumeName, bucketName) + OM_KEY_PREFIX;
+    }
+
     String seekKey;
     boolean skipStartKey = false;
     // 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)) {
+    // Compare DB keys: keyPrefix "/" maps to the DB prefix of the bucket, 
which also matches keys sorting before "/".
+    if (StringUtils.isNotEmpty(startKey) && getOzoneKey(volumeName, 
bucketName, startKey).compareTo(seekPrefix) >= 0) {
       // Seek to the specified key.
       seekKey = getOzoneKey(volumeName, bucketName, startKey);
       skipStartKey = true;
@@ -1122,13 +1129,6 @@ public ListKeysResult listKeys(String volumeName, String 
bucketName,
           StringUtils.isNotEmpty(keyPrefix) ? keyPrefix : OM_KEY_PREFIX);
     }
 
-    String seekPrefix;
-    if (StringUtils.isNotEmpty(keyPrefix)) {
-      seekPrefix = getOzoneKey(volumeName, bucketName, keyPrefix);
-    } else {
-      seekPrefix = getBucketKey(volumeName, bucketName) + OM_KEY_PREFIX;
-    }
-
     TreeMap<String, OmKeyInfo> cacheKeyMap = new TreeMap<>();
     Iterator<Map.Entry<CacheKey<String>, CacheValue<OmKeyInfo>>> iterator =
         keyTable.cacheIterator();
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 8d1ff6e07c6..c407230b780 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
@@ -570,6 +570,51 @@ public void testListKeysWithStartKeyBeforePrefix() throws 
Exception {
     assertEquals(Collections.emptyList(), listKeyNames(volumeName, bucketName, 
"dir1bi", "dir1bh", 100));
   }
 
+  /**
+   * keyPrefix "/" maps to the DB prefix of the bucket, so it also matches 
keys that sort before "/",
+   * and pagination must not restart from the first key.
+   */
+  @Test
+  public void testListKeysWithSlashPrefixAndStartKeyBeforeSlash() throws 
Exception {
+    String volumeName = "volumeA";
+    String bucketName = "slashPrefix";
+    OMRequestTestUtils.addVolumeToDB(volumeName, omMetadataManager);
+    addBucketsToCache(volumeName, bucketName);
+
+    // Even index keys go to the DB and odd index keys to the cache.
+    addKeysToOM(volumeName, bucketName, ".cache/0", 0);
+    addKeysToOM(volumeName, bucketName, ".cache/1", 1);
+    addKeysToOM(volumeName, bucketName, ".cache/2", 2);
+    addKeysToOM(volumeName, bucketName, "/keep", 3);
+
+    List<String> expected = Arrays.asList(".cache/0", ".cache/1", ".cache/2", 
"/keep");
+
+    assertEquals(expected.subList(0, 2), listKeyNames(volumeName, bucketName, 
null, "/", 2));
+    assertEquals(expected.subList(2, 4), listKeyNames(volumeName, bucketName, 
".cache/1", "/", 2));
+    assertEquals(Collections.emptyList(), listKeyNames(volumeName, bucketName, 
"/keep", "/", 2));
+  }
+
+  /**
+   * A whitespace-only key is a valid startKey, so pagination must not restart 
from the first key.
+   */
+  @Test
+  public void testListKeysWithWhitespaceStartKey() throws Exception {
+    String volumeName = "volumeA";
+    String bucketName = "whitespaceStartKey";
+    OMRequestTestUtils.addVolumeToDB(volumeName, omMetadataManager);
+    addBucketsToCache(volumeName, bucketName);
+
+    // Even index keys go to the DB and odd index keys to the cache.
+    addKeysToOM(volumeName, bucketName, " ", 0);
+    addKeysToOM(volumeName, bucketName, "  x", 1);
+    addKeysToOM(volumeName, bucketName, "k", 2);
+
+    List<String> expected = Arrays.asList(" ", "  x", "k");
+
+    assertEquals(expected.subList(0, 1), listKeyNames(volumeName, bucketName, 
null, null, 1));
+    assertEquals(expected.subList(1, 3), listKeyNames(volumeName, bucketName, 
" ", null, 2));
+  }
+
   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()


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

Reply via email to