-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add support for async deletion in S3BlobContainer #15621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ashking94
merged 10 commits into
opensearch-project:main
from
ashking94:async-deletion-s3-repository
Sep 20, 2024
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7822739
Add support for async deletion in S3BlobContainer
ashking94 639257a
Merge remote-tracking branch 'upstream/main' into async-deletion-s3-r…
ashking94 1f5a9cb
Move helper methods to helper class
ashking94 5e74661
Merge remote-tracking branch 'upstream/main' into async-deletion-s3-r…
ashking94 ffc81c0
Minor refactor
ashking94 09b7c68
Add UTs
ashking94 eee4a69
Add more tests
ashking94 932a03e
Merge remote-tracking branch 'upstream/main' into async-deletion-s3-r…
ashking94 96cd6f7
Integrate async deletion in the snapshot interactions
ashking94 5209835
Merge remote-tracking branch 'upstream/main' into async-deletion-s3-r…
ashking94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
plugins/repository-s3/src/main/java/org/opensearch/repositories/s3/S3AsyncDeleteHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.repositories.s3; | ||
|
|
||
| import software.amazon.awssdk.services.s3.S3AsyncClient; | ||
| import software.amazon.awssdk.services.s3.model.Delete; | ||
| import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; | ||
| import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse; | ||
| import software.amazon.awssdk.services.s3.model.ObjectIdentifier; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.message.ParameterizedMessage; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class S3AsyncDeleteHelper { | ||
| private static final Logger logger = LogManager.getLogger(S3AsyncDeleteHelper.class); | ||
|
|
||
| static CompletableFuture<Void> executeDeleteChain( | ||
| S3AsyncClient s3AsyncClient, | ||
| S3BlobStore blobStore, | ||
| List<String> objectsToDelete, | ||
| CompletableFuture<Void> currentChain, | ||
| boolean ignoreIfNotExists, | ||
| Runnable afterDeleteAction | ||
| ) { | ||
| List<List<String>> batches = createDeleteBatches(objectsToDelete, blobStore.getBulkDeletesSize()); | ||
| CompletableFuture<Void> newChain = currentChain.thenCompose( | ||
| v -> executeDeleteBatches(s3AsyncClient, blobStore, batches, ignoreIfNotExists) | ||
| ); | ||
| if (afterDeleteAction != null) { | ||
| newChain = newChain.thenRun(afterDeleteAction); | ||
| } | ||
| return newChain; | ||
| } | ||
|
|
||
| static List<List<String>> createDeleteBatches(List<String> keys, int bulkDeleteSize) { | ||
| List<List<String>> batches = new ArrayList<>(); | ||
| for (int i = 0; i < keys.size(); i += bulkDeleteSize) { | ||
| batches.add(keys.subList(i, Math.min(keys.size(), i + bulkDeleteSize))); | ||
| } | ||
| return batches; | ||
| } | ||
|
|
||
| private static CompletableFuture<Void> executeDeleteBatches( | ||
| S3AsyncClient s3AsyncClient, | ||
| S3BlobStore blobStore, | ||
| List<List<String>> batches, | ||
| boolean ignoreIfNotExists | ||
| ) { | ||
| CompletableFuture<Void> allDeletesFuture = CompletableFuture.completedFuture(null); | ||
|
|
||
| for (List<String> batch : batches) { | ||
| allDeletesFuture = allDeletesFuture.thenCompose( | ||
| v -> executeSingleDeleteBatch(s3AsyncClient, blobStore, batch, ignoreIfNotExists) | ||
| ); | ||
| } | ||
|
|
||
| return allDeletesFuture; | ||
| } | ||
|
|
||
| private static CompletableFuture<Void> executeSingleDeleteBatch( | ||
| S3AsyncClient s3AsyncClient, | ||
| S3BlobStore blobStore, | ||
| List<String> batch, | ||
| boolean ignoreIfNotExists | ||
| ) { | ||
| DeleteObjectsRequest deleteRequest = bulkDelete(blobStore.bucket(), batch, blobStore); | ||
| return s3AsyncClient.deleteObjects(deleteRequest) | ||
| .thenApply(response -> processDeleteResponse(response, ignoreIfNotExists)) | ||
| .exceptionally(e -> { | ||
| if (!ignoreIfNotExists) { | ||
| throw new CompletionException(e); | ||
| } | ||
| logger.warn("Error during batch deletion", e); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| private static Void processDeleteResponse(DeleteObjectsResponse deleteObjectsResponse, boolean ignoreIfNotExists) { | ||
| if (!deleteObjectsResponse.errors().isEmpty()) { | ||
| if (ignoreIfNotExists) { | ||
| logger.warn( | ||
| () -> new ParameterizedMessage( | ||
| "Failed to delete some blobs {}", | ||
| deleteObjectsResponse.errors() | ||
| .stream() | ||
| .map(s3Error -> "[" + s3Error.key() + "][" + s3Error.code() + "][" + s3Error.message() + "]") | ||
| .collect(Collectors.toList()) | ||
| ) | ||
| ); | ||
ashking94 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| throw new CompletionException(new IOException("Failed to delete some blobs: " + deleteObjectsResponse.errors())); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static DeleteObjectsRequest bulkDelete(String bucket, List<String> blobs, S3BlobStore blobStore) { | ||
| return DeleteObjectsRequest.builder() | ||
| .bucket(bucket) | ||
| .delete( | ||
| Delete.builder() | ||
| .objects(blobs.stream().map(blob -> ObjectIdentifier.builder().key(blob).build()).collect(Collectors.toList())) | ||
| .quiet(true) | ||
| .build() | ||
| ) | ||
| .overrideConfiguration(o -> o.addMetricPublisher(blobStore.getStatsMetricPublisher().deleteObjectsMetricPublisher)) | ||
| .build(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.