Skip to content
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

Use batch payload delete, and properly chain deletes for async #148

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.amazonaws</groupId>
<artifactId>amazon-sqs-java-extended-client-lib</artifactId>
<version>2.1.0</version>
<version>2.1.1</version>
<packaging>jar</packaging>
<name>Amazon SQS Extended Client Library for Java</name>
<description>An extension to the Amazon SQS client that enables sending and receiving messages up to 2GB via Amazon S3.
Expand Down Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>software.amazon.payloadoffloading</groupId>
<artifactId>payloadoffloading-common</artifactId>
<version>2.2.0</version>
<version>2.2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ public CompletableFuture<DeleteMessageResponse> deleteMessage(DeleteMessageReque

// Delete from SQS first, then S3.
final String messageToDeletePointer = messagePointer;
return super.deleteMessage(deleteMessageRequestBuilder.build())
.thenCompose(deleteMessageResponse ->
payloadStore.deleteOriginalPayload(messageToDeletePointer)
.thenApply(v -> deleteMessageResponse));
return payloadStore.deleteOriginalPayload(messageToDeletePointer)
.thenCompose(ignore -> super.deleteMessage(deleteMessageRequestBuilder.build()));
}

/**
Expand Down Expand Up @@ -395,6 +393,7 @@ public CompletableFuture<DeleteMessageBatchResponse> deleteMessageBatch(
}

List<DeleteMessageBatchRequestEntry> entries = new ArrayList<>(deleteMessageBatchRequest.entries().size());
List<String> s3ToCleanup = new ArrayList<>(deleteMessageBatchRequest.entries().size());
for (DeleteMessageBatchRequestEntry entry : deleteMessageBatchRequest.entries()) {
DeleteMessageBatchRequestEntry.Builder entryBuilder = entry.toBuilder();
String receiptHandle = entry.receiptHandle();
Expand All @@ -406,7 +405,7 @@ public CompletableFuture<DeleteMessageBatchResponse> deleteMessageBatch(
// Delete s3 payload if needed
if (clientConfiguration.doesCleanupS3Payload()) {
String messagePointer = getMessagePointerFromModifiedReceiptHandle(receiptHandle);
payloadStore.deleteOriginalPayload(messagePointer);
s3ToCleanup.add(messagePointer);
}
}

Expand All @@ -415,7 +414,16 @@ public CompletableFuture<DeleteMessageBatchResponse> deleteMessageBatch(
}

deleteMessageBatchRequestBuilder.entries(entries);
return super.deleteMessageBatch(deleteMessageBatchRequestBuilder.build());

// Check if message is in S3 or only in SQS.
if (s3ToCleanup.isEmpty()) {
// Delete only from SQS
return super.deleteMessageBatch(deleteMessageBatchRequestBuilder.build());
}

// Delete from S3 first, then SQS.
return payloadStore.deleteOriginalPayloads(s3ToCleanup)
.thenCompose(ignore -> super.deleteMessageBatch(deleteMessageBatchRequestBuilder.build()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ public DeleteMessageBatchResponse deleteMessageBatch(DeleteMessageBatchRequest d
}

List<DeleteMessageBatchRequestEntry> entries = new ArrayList<>(deleteMessageBatchRequest.entries().size());
List<String> s3ToCleanup = new ArrayList<>(deleteMessageBatchRequest.entries().size());
for (DeleteMessageBatchRequestEntry entry : deleteMessageBatchRequest.entries()) {
DeleteMessageBatchRequestEntry.Builder entryBuilder = entry.toBuilder();
String receiptHandle = entry.receiptHandle();
Expand All @@ -709,7 +710,7 @@ public DeleteMessageBatchResponse deleteMessageBatch(DeleteMessageBatchRequest d
// Delete s3 payload if needed
if (clientConfiguration.doesCleanupS3Payload()) {
String messagePointer = getMessagePointerFromModifiedReceiptHandle(receiptHandle);
payloadStore.deleteOriginalPayload(messagePointer);
s3ToCleanup.add(messagePointer);
}
}

Expand All @@ -718,6 +719,11 @@ public DeleteMessageBatchResponse deleteMessageBatch(DeleteMessageBatchRequest d
}

deleteMessageBatchRequestBuilder.entries(entries);

if (!s3ToCleanup.isEmpty()) {
payloadStore.deleteOriginalPayloads(s3ToCleanup);
}

return super.deleteMessageBatch(deleteMessageBatchRequestBuilder.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectResponse;
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectsResponse;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
Expand Down Expand Up @@ -91,6 +93,8 @@ public void setupClients() {
CompletableFuture.completedFuture(null));
when(mockS3.deleteObject(isA(DeleteObjectRequest.class))).thenReturn(
CompletableFuture.completedFuture(DeleteObjectResponse.builder().build()));
when(mockS3.deleteObjects(isA(DeleteObjectsRequest.class))).thenReturn(
CompletableFuture.completedFuture(DeleteObjectsResponse.builder().build()));
when(mockSqsBackend.sendMessage(isA(SendMessageRequest.class))).thenReturn(
CompletableFuture.completedFuture(SendMessageResponse.builder().build()));
when(mockSqsBackend.sendMessageBatch(isA(SendMessageBatchRequest.class))).thenReturn(
Expand Down Expand Up @@ -590,7 +594,7 @@ public void testDefaultExtendedClientDeletesObjectsFromS3UponDeleteBatch() {
IntStream.range(0, originalReceiptHandles.size()).forEach(i -> assertEquals(
originalReceiptHandles.get(i),
request.entries().get(i).receiptHandle()));
verify(mockS3, times(batchSize)).deleteObject(any(DeleteObjectRequest.class));
verify(mockS3, times(1)).deleteObjects(any(DeleteObjectsRequest.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import software.amazon.awssdk.http.AbortableInputStream;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
Expand Down Expand Up @@ -621,7 +622,7 @@ public void testDefaultExtendedClientDeletesObjectsFromS3UponDeleteBatch() {
IntStream.range(0, originalReceiptHandles.size()).forEach(i -> assertEquals(
originalReceiptHandles.get(i),
request.entries().get(i).receiptHandle()));
verify(mockS3, times(batchSize)).deleteObject(any(DeleteObjectRequest.class));
verify(mockS3, times(1)).deleteObjects(any(DeleteObjectsRequest.class));
}

@Test
Expand Down