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

smengcl 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 5f7111a27ea HDDS-17028. Filter CommonPrefixes by marker in ListObjects 
and ListObjectVersions (#11587)
5f7111a27ea is described below

commit 5f7111a27ea07ce6b7bf5284a4213ebacb65f2ae
Author: KUAN-HAO HUANG <[email protected]>
AuthorDate: Fri Oct 23 23:12:36 2026 +0100

    HDDS-17028. Filter CommonPrefixes by marker in ListObjects and 
ListObjectVersions (#11587)
    
    Co-authored-by: Codex (GPT-6) <[email protected]>
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java   | 16 +++++
 .../hadoop/ozone/s3/endpoint/BucketEndpoint.java   | 19 ++---
 .../hadoop/ozone/s3/endpoint/TestBucketList.java   | 82 ++++++++++++++++++++++
 3 files changed, 108 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 8c031808cbd..fa3d015adc2 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
@@ -134,6 +134,7 @@
 import software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest;
 import software.amazon.awssdk.services.s3.model.Bucket;
 import software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration;
+import software.amazon.awssdk.services.s3.model.CommonPrefix;
 import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest;
 import 
software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
 import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload;
@@ -1103,6 +1104,21 @@ public void 
testDeleteObjectsListedByListObjectVersions() {
     assertThat(remaining.isTruncated()).isFalse();
   }
 
+  @Test
+  public void testListObjectVersionsFiltersCommonPrefixesByKeyMarker() {
+    final String bucketName = getBucketName();
+    s3Client.createBucket(b -> b.bucket(bucketName));
+    Arrays.asList("dir/a", "dir/b", "dira/c", "file")
+        .forEach(key -> s3Client.putObject(b -> b.bucket(bucketName).key(key), 
RequestBody.fromString("bar")));
+
+    final ListObjectVersionsResponse result = s3Client.listObjectVersions(
+        b -> b.bucket(bucketName).delimiter("/").keyMarker("dir/a"));
+
+    
assertThat(result.commonPrefixes()).extracting(CommonPrefix::prefix).containsExactly("dira/");
+    
assertThat(result.versions()).extracting(ObjectVersion::key).containsExactly("file");
+    assertThat(result.isTruncated()).isFalse();
+  }
+
   @Test
   public void testPutObjectLegalHoldNotImplemented() {
     final String bucketName = getBucketName();
diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java
index c7cdfb3a981..d310ea7f1a6 100644
--- 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java
@@ -120,9 +120,8 @@ Response handleGetRequest(S3RequestContext context, String 
bucketName) throws IO
     Iterator<? extends OzoneKey> ozoneKeyIterator = null;
     // AWS S3 treats an empty continuation-token as no token: list from the
     // start and echo the empty token back (see setContinueToken below).
-    ContinueToken decodedToken =
-        (continueToken == null || continueToken.isEmpty())
-            ? null : ContinueToken.decodeFromString(continueToken);
+    ContinueToken decodedToken = (continueToken == null || 
continueToken.isEmpty())
+        ? null : ContinueToken.decodeFromString(continueToken);
     OzoneBucket bucket = null;
 
     try {
@@ -135,13 +134,11 @@ Response handleGetRequest(S3RequestContext context, 
String bucketName) throws IO
 
       // If continuation token and start after both are provided, then we
       // ignore start After
-      String prevKey = decodedToken != null ? decodedToken.getLastKey()
-          : startAfter;
+      String prevKey = decodedToken != null ? decodedToken.getLastKey() : 
startAfter;
 
       // If shallow is true, only list immediate children
       // delimited by OZONE_URI_DELIMITER
-      boolean shallow = listKeysShallowEnabled
-          && OZONE_URI_DELIMITER.equals(delimiter);
+      boolean shallow = listKeysShallowEnabled && 
OZONE_URI_DELIMITER.equals(delimiter);
 
       bucket = context.getVolume().getBucket(bucketName);
       S3Owner.verifyBucketOwnerCondition(getHeaders(), bucketName, 
bucket.getOwner());
@@ -202,9 +199,13 @@ Response handleGetRequest(S3RequestContext context, String 
bucketName) throws IO
               // delimiter /) also hit this branch and become a common prefix.
               String dirName = relativeKeyName.substring(0, delimiterIndex);
               if (!dirName.equals(prevDir)) {
-                response.addPrefix(EncodingTypeObject.createNullable(
-                    prefix + dirName + delimiter, encodingType));
                 prevDir = dirName;
+                String commonPrefix = prefix + dirName + delimiter;
+                // AWS omits common prefixes at or before start-after; 
startAfter also holds marker and key-marker.
+                if (StringUtils.startsWith(startAfter, commonPrefix)) {
+                  continue;
+                }
+                
response.addPrefix(EncodingTypeObject.createNullable(commonPrefix, 
encodingType));
                 count++;
               }
             } else {
diff --git 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketList.java
 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketList.java
index dd872757500..4ce44da75be 100644
--- 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketList.java
+++ 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestBucketList.java
@@ -32,6 +32,7 @@
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.util.List;
 import java.util.stream.IntStream;
 import javax.xml.bind.JAXB;
 import org.apache.hadoop.hdds.conf.OzoneConfiguration;
@@ -45,6 +46,8 @@
 import org.apache.hadoop.ozone.s3.util.S3Consts.QueryParams;
 import org.apache.hadoop.security.UserGroupInformation;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 
 /**
  * Testing basic object list browsing.
@@ -786,6 +789,85 @@ public void listObjectVersionsPaginatesWithKeyMarker() 
throws OS3Exception, IOEx
     assertThat(second.getVersions()).extracting(v -> 
v.getKey().getName()).containsExactly("file2", "file3");
   }
 
+  @Test
+  public void listObjectVersionsPaginatesCommonPrefixes() throws Exception {
+    setup("dir:a", "dir:b", "file");
+
+    bucketEndpoint.queryParamsForTest().set(QueryParams.VERSIONS, "");
+    bucketEndpoint.queryParamsForTest().set(QueryParams.DELIMITER, ":");
+    bucketEndpoint.queryParamsForTest().setInt(QueryParams.MAX_KEYS, 1);
+    ListVersionsResult first = (ListVersionsResult) 
bucketEndpoint.get(BUCKET_NAME).getEntity();
+
+    assertThat(first.getCommonPrefixes()).extracting(p -> 
p.getPrefix().getName()).containsExactly("dir:");
+    assertThat(first.getVersions()).isEmpty();
+    assertThat(first.isTruncated()).isTrue();
+    assertThat(first.getNextKeyMarker().getName()).isEqualTo("dir:a");
+    assertThat(first.getNextVersionIdMarker()).isEqualTo(NULL_VERSION_ID);
+
+    bucketEndpoint.queryParamsForTest().set(QueryParams.KEY_MARKER, 
first.getNextKeyMarker().getName());
+    bucketEndpoint.queryParamsForTest().set(QueryParams.VERSION_ID_MARKER, 
first.getNextVersionIdMarker());
+    ListVersionsResult second = (ListVersionsResult) 
bucketEndpoint.get(BUCKET_NAME).getEntity();
+
+    assertThat(second.getCommonPrefixes()).isEmpty();
+    assertThat(second.getVersions()).extracting(v -> 
v.getKey().getName()).containsExactly("file");
+    assertThat(second.isTruncated()).isFalse();
+    assertThat(second.getNextKeyMarker()).isNull();
+    assertThat(second.getNextVersionIdMarker()).isNull();
+  }
+
+  @Test
+  public void listObjectsV1PaginatesCommonPrefixes() throws Exception {
+    setup("dir:a", "dir:b", "file");
+
+    bucketEndpoint.queryParamsForTest().set(QueryParams.DELIMITER, ":");
+    bucketEndpoint.queryParamsForTest().setInt(QueryParams.MAX_KEYS, 1);
+    ListObjectResponse first = (ListObjectResponse) 
bucketEndpoint.get(BUCKET_NAME).getEntity();
+
+    assertThat(first.getCommonPrefixes()).extracting(p -> 
p.getPrefix().getName()).containsExactly("dir:");
+    assertThat(first.getContents()).isEmpty();
+    assertThat(first.isTruncated()).isTrue();
+    assertThat(first.getNextMarker()).isEqualTo("dir:a");
+
+    bucketEndpoint.queryParamsForTest().set(QueryParams.MARKER, 
first.getNextMarker());
+    ListObjectResponse second = (ListObjectResponse) 
bucketEndpoint.get(BUCKET_NAME).getEntity();
+
+    assertThat(second.getCommonPrefixes()).isEmpty();
+    assertThat(second.getContents()).extracting(k -> 
k.getKey().getName()).containsExactly("file");
+    assertThat(second.isTruncated()).isFalse();
+  }
+
+  @Test
+  public void listObjectsV2FiltersCommonPrefixesByStartAfter() throws 
Exception {
+    setup("dir/a", "dir/b", "file");
+
+    bucketEndpoint.queryParamsForTest().set(QueryParams.DELIMITER, "/");
+    bucketEndpoint.queryParamsForTest().set(QueryParams.START_AFTER, "dir/a");
+    ListObjectResponse result = (ListObjectResponse) 
bucketEndpoint.get(BUCKET_NAME).getEntity();
+
+    assertThat(result.getCommonPrefixes()).isEmpty();
+    assertThat(result.getContents()).extracting(k -> 
k.getKey().getName()).containsExactly("file");
+    assertThat(result.isTruncated()).isFalse();
+  }
+
+  @ParameterizedTest
+  @CsvSource({"p:di, true", "p:dir:, false", "p:dir:a, false"})
+  public void listObjectVersionsFiltersCommonPrefixesByKeyMarker(String 
keyMarker, boolean includeMarkerPrefix)
+      throws Exception {
+    setup("p:dir:a", "p:dir:b", "p:dira:c", "p:file");
+
+    bucketEndpoint.queryParamsForTest().set(QueryParams.VERSIONS, "");
+    bucketEndpoint.queryParamsForTest().set(QueryParams.PREFIX, "p:");
+    bucketEndpoint.queryParamsForTest().set(QueryParams.DELIMITER, ":");
+    bucketEndpoint.queryParamsForTest().set(QueryParams.KEY_MARKER, keyMarker);
+    ListVersionsResult result = (ListVersionsResult) 
bucketEndpoint.get(BUCKET_NAME).getEntity();
+
+    List<String> expectedPrefixes = includeMarkerPrefix ? List.of("p:dir:", 
"p:dira:") : List.of("p:dira:");
+    assertThat(result.getCommonPrefixes()).extracting(p -> 
p.getPrefix().getName())
+        .containsExactlyElementsOf(expectedPrefixes);
+    assertThat(result.getVersions()).extracting(v -> 
v.getKey().getName()).containsExactly("p:file");
+    assertThat(result.isTruncated()).isFalse();
+  }
+
   @Test
   public void listObjectVersionsRejectsVersionIdMarkerWithoutKeyMarker() 
throws IOException {
     setup("file1");


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

Reply via email to