Skip to content

ISSUE-30: Delete SQS messages when S3 payload not found #78

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.util.VersionInfo;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import software.amazon.awssdk.services.sqs.SqsClient;

import software.amazon.awssdk.services.sqs.model.BatchEntryIdsNotDistinctException;
Expand Down Expand Up @@ -339,7 +340,20 @@ public ReceiveMessageResponse receiveMessage(ReceiveMessageRequest receiveMessag
String largeMessagePointer = message.body();
largeMessagePointer = largeMessagePointer.replace("com.amazon.sqs.javamessaging.MessageS3Pointer", "software.amazon.payloadoffloading.PayloadS3Pointer");

messageBuilder.body(payloadStore.getOriginalPayload(largeMessagePointer));
try {
messageBuilder.body(payloadStore.getOriginalPayload(largeMessagePointer));
} catch (SdkException e) {
if (e.getCause() instanceof NoSuchKeyException && clientConfiguration.ignoresPayloadNotFound()) {
DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest
.builder()
.queueUrl(receiveMessageRequest.queueUrl())
.receiptHandle(message.receiptHandle())
.build();
deleteMessage(deleteMessageRequest);
LOG.warn("Message deleted from SQS since payload with pointer could not be found in S3.");
continue;
} else throw e;
}

// remove the additional attribute before returning the message
// to user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
import software.amazon.awssdk.core.ApiName;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.sync.RequestBody;
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.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.sqs.SqsClient;
Expand All @@ -60,6 +62,7 @@
import static com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.USER_AGENT_VERSION;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -641,6 +644,60 @@ private void testReceiveMessage_when_MessageIsLarge(String reservedAttributeName
verify(mockS3, times(1)).getObject(isA(GetObjectRequest.class));
}

@Test
public void testReceiveMessage_when_ignorePayloadNotFound_then_messageWithPayloadNotFoundIsDeletedFromSQS() {
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withPayloadSupportEnabled(mockS3, S3_BUCKET_NAME)
.withIgnorePayloadNotFound(true);
SqsClient sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));

String receiptHandle = "receipt-handle";
Message message = Message.builder()
.messageAttributes(ImmutableMap.of(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME, MessageAttributeValue.builder().build()))
.body(new PayloadS3Pointer(S3_BUCKET_NAME, "S3Key").toJson())
.receiptHandle(receiptHandle)
.build();

when(mockSqsBackend.receiveMessage(isA(ReceiveMessageRequest.class))).thenReturn(ReceiveMessageResponse.builder().messages(message).build());
doThrow(NoSuchKeyException.class).when(mockS3).getObject(any(GetObjectRequest.class));

ReceiveMessageRequest messageRequest = ReceiveMessageRequest.builder().queueUrl(SQS_QUEUE_URL).build();
ReceiveMessageResponse receiveMessageResponse = sqsExtended.receiveMessage(messageRequest);

Assert.assertTrue(receiveMessageResponse.messages().isEmpty());

ArgumentCaptor<DeleteMessageRequest> deleteMessageRequestArgumentCaptor = ArgumentCaptor.forClass(DeleteMessageRequest.class);
verify(mockSqsBackend).deleteMessage(deleteMessageRequestArgumentCaptor.capture());
Assert.assertEquals(SQS_QUEUE_URL, deleteMessageRequestArgumentCaptor.getValue().queueUrl());
Assert.assertEquals(receiptHandle, deleteMessageRequestArgumentCaptor.getValue().receiptHandle());
}

@Test
public void testReceiveMessage_when_ignorePayloadNotFoundIsFalse_then_messageWithPayloadNotFoundThrowsException() {
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withPayloadSupportEnabled(mockS3, S3_BUCKET_NAME)
.withIgnorePayloadNotFound(false);
SqsClient sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));

Message message = Message.builder()
.messageAttributes(ImmutableMap.of(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME, MessageAttributeValue.builder().build()))
.body(new PayloadS3Pointer(S3_BUCKET_NAME, "S3Key").toJson())
.receiptHandle("receipt-handle")
.build();

when(mockSqsBackend.receiveMessage(isA(ReceiveMessageRequest.class))).thenReturn(ReceiveMessageResponse.builder().messages(message).build());
doThrow(NoSuchKeyException.class).when(mockS3).getObject(any(GetObjectRequest.class));

ReceiveMessageRequest messageRequest = ReceiveMessageRequest.builder().build();
try {
sqsExtended.receiveMessage(messageRequest);
fail("Expected exception after receiving NoSuchKeyException from S3 was not thrown.");
} catch (SdkException e) {
assertEquals(NoSuchKeyException.class.getName(), e.getCause().getClass().getName());
verify(mockSqsBackend, never()).deleteMessage(any(DeleteMessageRequest.class));
}
}

private DeleteMessageBatchRequest generateLargeDeleteBatchRequest(List<String> originalReceiptHandles) {
List<DeleteMessageBatchRequestEntry> deleteEntries = IntStream.range(0, originalReceiptHandles.size())
.mapToObj(i -> DeleteMessageBatchRequestEntry.builder()
Expand Down