Skip to content

Commit

Permalink
Fix limit check for listing S3 objects
Browse files Browse the repository at this point in the history
Signed-off-by: Bhumika Saini <[email protected]>
  • Loading branch information
Bhumika Saini committed Jan 25, 2024
1 parent b4306b6 commit a752ab2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ private static List<ListObjectsV2Response> executeListing(
for (ListObjectsV2Response listObjectsV2Response : listObjectsIterable) {
results.add(listObjectsV2Response);
totalObjects += listObjectsV2Response.contents().size();
if (limit != -1 && totalObjects > limit) {
if (limit != -1 && totalObjects >= limit) {
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@

package org.opensearch.repositories.s3;

import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.opensearch.action.LatchedActionListener;
import org.opensearch.common.blobstore.BlobContainer;
import org.opensearch.common.blobstore.BlobMetadata;
import org.opensearch.common.blobstore.BlobPath;
import org.opensearch.common.blobstore.BlobStoreException;
import org.opensearch.common.blobstore.DeleteResult;
import org.opensearch.common.blobstore.stream.read.ReadContext;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.io.InputStreamContainer;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.test.OpenSearchTestCase;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.exception.SdkException;
Expand Down Expand Up @@ -70,19 +84,6 @@
import software.amazon.awssdk.services.s3.model.UploadPartResponse;
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;

import org.opensearch.action.LatchedActionListener;
import org.opensearch.common.blobstore.BlobContainer;
import org.opensearch.common.blobstore.BlobMetadata;
import org.opensearch.common.blobstore.BlobPath;
import org.opensearch.common.blobstore.BlobStoreException;
import org.opensearch.common.blobstore.DeleteResult;
import org.opensearch.common.blobstore.stream.read.ReadContext;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.io.InputStreamContainer;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.test.OpenSearchTestCase;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -103,9 +104,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -916,6 +914,15 @@ public void testListBlobsByPrefixInLexicographicOrderWithLimitLessThanPageSize()
testListBlobsByPrefixInLexicographicOrder(2, 1, BlobContainer.BlobNameSortOrder.LEXICOGRAPHIC);
}

/**
* Test the boundary value at page size to ensure
* unnecessary calls are not made to S3 by fetching the next page.
* @throws IOException
*/
public void testListBlobsByPrefixInLexicographicOrderWithLimitEqualToPageSize() throws IOException {
testListBlobsByPrefixInLexicographicOrder(5, 1, BlobContainer.BlobNameSortOrder.LEXICOGRAPHIC);
}

public void testListBlobsByPrefixInLexicographicOrderWithLimitGreaterThanPageSize() throws IOException {
testListBlobsByPrefixInLexicographicOrder(8, 2, BlobContainer.BlobNameSortOrder.LEXICOGRAPHIC);
}
Expand Down

0 comments on commit a752ab2

Please sign in to comment.