-
Notifications
You must be signed in to change notification settings - Fork 914
Fix issue where the Scheduled Timeout was incorrectly completing the futures with empty messages #5571
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
joviegas
merged 2 commits into
feature/master/sqs-batch-manager
from
joviegas/timeout_on_recv_wait
Sep 6, 2024
Merged
Fix issue where the Scheduled Timeout was incorrectly completing the futures with empty messages #5571
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
238 changes: 238 additions & 0 deletions
238
...rc/test/java/software/amazon/awssdk/services/sqs/batchmanager/ReceiveBatchesMockTest.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,238 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.services.sqs.batchmanager; | ||
|
||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.github.tomakehurst.wiremock.junit5.WireMockExtension; | ||
import java.net.URI; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient; | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClientBuilder; | ||
import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest; | ||
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; | ||
import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ReceiveBatchesMockTest { | ||
|
||
private static final int OFFSET_DELAY = 100; | ||
// Default queue attribute response with placeholders for parameters | ||
private static final String QUEUE_ATTRIBUTE_RESPONSE = "{\n" + | ||
" \"Attributes\": {\n" + | ||
" \"ReceiveMessageWaitTimeSeconds\": \"%s\",\n" + | ||
" \"VisibilityTimeout\": \"%s\"\n" + | ||
" }\n" + | ||
"}"; | ||
@RegisterExtension | ||
static WireMockExtension wireMock = WireMockExtension.newInstance() | ||
.options(wireMockConfig().dynamicPort().dynamicHttpsPort()) | ||
.configureStaticDsl(true) | ||
.build(); | ||
private SqsAsyncBatchManager receiveMessageBatchManager; | ||
|
||
@Test | ||
void testTimeoutOccursBeforeSqsResponds() throws Exception { | ||
setupBatchManager(); | ||
|
||
// Delays for testing | ||
int queueAttributesApiDelay = 51; | ||
int receiveMessagesDelay = 150; | ||
|
||
// Stub the WireMock server to simulate delayed responses | ||
mockQueueAttributes(queueAttributesApiDelay); | ||
mockReceiveMessages(receiveMessagesDelay, 2); | ||
|
||
CompletableFuture<ReceiveMessageResponse> future = batchManagerReceiveMessage(); | ||
assertThat(future.get(1000, TimeUnit.MILLISECONDS).messages()).isEmpty(); | ||
|
||
Thread.sleep(queueAttributesApiDelay + receiveMessagesDelay + OFFSET_DELAY); | ||
|
||
CompletableFuture<ReceiveMessageResponse> secondCall = batchManagerReceiveMessage(); | ||
assertThat(secondCall.get(1000, TimeUnit.MILLISECONDS).messages()).hasSize(2); | ||
} | ||
|
||
@Test | ||
void testResponseReceivedBeforeTimeout() throws Exception { | ||
setupBatchManager(); | ||
|
||
// Delays for testing | ||
int queueAttributesApiDelay = 5; | ||
int receiveMessagesDelay = 5; | ||
|
||
// Set short delays to ensure response before timeout | ||
mockQueueAttributes(queueAttributesApiDelay); | ||
mockReceiveMessages(receiveMessagesDelay, 2); | ||
|
||
CompletableFuture<ReceiveMessageResponse> future = batchManagerReceiveMessage(); | ||
assertThat(future.get(1000, TimeUnit.MILLISECONDS).messages()).hasSize(2); | ||
} | ||
|
||
@Test | ||
void testTimeoutOccursBasedOnUserSetWaitTime() throws Exception { | ||
setupBatchManager(); | ||
|
||
// Delays for testing | ||
int queueAttributesApiDelay = 100; | ||
int receiveMessagesDelay = 100; | ||
|
||
// Configure response delays | ||
mockQueueAttributes(queueAttributesApiDelay); | ||
mockReceiveMessages(receiveMessagesDelay, 2); | ||
|
||
CompletableFuture<ReceiveMessageResponse> future = receiveMessageWithWaitTime(1); | ||
assertThat(future.get(1000, TimeUnit.MILLISECONDS).messages()).hasSize(2); | ||
} | ||
|
||
@Test | ||
void testMessagesAreFetchedFromBufferWhenAvailable() throws Exception { | ||
ApiCaptureInterceptor interceptor = new ApiCaptureInterceptor(); | ||
SqsAsyncClient sqsAsyncClient = getAsyncClientBuilder() | ||
.overrideConfiguration(o -> o.addExecutionInterceptor(interceptor)) | ||
.build(); | ||
|
||
SqsAsyncBatchManager batchManager = sqsAsyncClient.batchManager(); | ||
|
||
// Delays for testing | ||
int queueAttributesApiDelay = 100; | ||
int receiveMessagesDelay = 1000; | ||
|
||
// Setup delayed responses | ||
mockQueueAttributes(queueAttributesApiDelay); | ||
mockReceiveMessages(receiveMessagesDelay, 10); | ||
|
||
// First message should be empty due to delay | ||
CompletableFuture<ReceiveMessageResponse> firstMessage = | ||
batchManager.receiveMessage(r -> r.queueUrl("test").maxNumberOfMessages(1)); | ||
assertThat(firstMessage.get(1000, TimeUnit.MILLISECONDS).messages()).isEmpty(); | ||
|
||
// Wait for SQS message to be processed | ||
Thread.sleep(queueAttributesApiDelay + receiveMessagesDelay + OFFSET_DELAY); | ||
assertThat(interceptor.receiveApiCalls.get()).isEqualTo(1); | ||
assertThat(interceptor.getQueueAttributesApiCalls.get()).isEqualTo(1); | ||
interceptor.reset(); | ||
|
||
// Fetch 10 messages from the buffer | ||
for (int i = 0; i < 10; i++) { | ||
CompletableFuture<ReceiveMessageResponse> future = | ||
batchManager.receiveMessage(r -> r.queueUrl("test").maxNumberOfMessages(1)); | ||
ReceiveMessageResponse response = future.get(500, TimeUnit.MILLISECONDS); | ||
assertThat(response.messages()).hasSize(1); | ||
} | ||
assertThat(interceptor.receiveApiCalls.get()).isEqualTo(0); | ||
assertThat(interceptor.getQueueAttributesApiCalls.get()).isEqualTo(0); | ||
} | ||
|
||
// Utility methods for reuse across tests | ||
|
||
private void setupBatchManager() { | ||
SqsAsyncClient sqsAsyncClient = getAsyncClientBuilder().build(); | ||
receiveMessageBatchManager = sqsAsyncClient.batchManager(); | ||
} | ||
|
||
private void mockQueueAttributes(int delay) { | ||
stubFor(post(urlEqualTo("/")) | ||
.withHeader("x-amz-target", equalTo("AmazonSQS.GetQueueAttributes")) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(String.format(QUEUE_ATTRIBUTE_RESPONSE, "0", "30")) | ||
.withFixedDelay(delay))); | ||
} | ||
|
||
private void mockReceiveMessages(int delay, int numMessages) { | ||
stubFor(post(urlEqualTo("/")) | ||
.withHeader("x-amz-target", equalTo("AmazonSQS.ReceiveMessage")) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(generateMessagesJson(numMessages)) | ||
.withFixedDelay(delay))); | ||
} | ||
|
||
private CompletableFuture<ReceiveMessageResponse> batchManagerReceiveMessage() { | ||
return receiveMessageBatchManager.receiveMessage(r -> r.queueUrl("test")); | ||
} | ||
|
||
private CompletableFuture<ReceiveMessageResponse> receiveMessageWithWaitTime(int waitTimeSeconds) { | ||
return receiveMessageBatchManager.receiveMessage(r -> r.queueUrl("test").waitTimeSeconds(waitTimeSeconds)); | ||
} | ||
|
||
// Helper method for building the async client | ||
private SqsAsyncClientBuilder getAsyncClientBuilder() { | ||
return SqsAsyncClient.builder() | ||
.endpointOverride(URI.create(String.format("http://localhost:%s/", wireMock.getPort()))) | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("key", "secret"))); | ||
} | ||
|
||
// Utility to generate the response for multiple messages in JSON format | ||
private String generateMessagesJson(int numMessages) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("{\n \"Messages\": [\n"); | ||
for (int i = 0; i < numMessages; i++) { | ||
sb.append(" {\n"); | ||
sb.append(" \"Body\": \"Message 6\",\n"); | ||
sb.append(" \"MD5OfBody\": \"05d2a129ebdb00cfa6e92aaf9f090547\",\n"); | ||
sb.append(" \"MessageId\": \"57d2\",\n"); | ||
sb.append(" \"ReceiptHandle\": \"AQEB\"\n"); | ||
sb.append(" }"); | ||
if (i < numMessages - 1) { | ||
sb.append(","); | ||
} | ||
sb.append("\n"); | ||
} | ||
sb.append(" ]\n}"); | ||
return sb.toString(); | ||
} | ||
|
||
// Interceptor to capture the API call counts | ||
static class ApiCaptureInterceptor implements ExecutionInterceptor { | ||
|
||
AtomicInteger receiveApiCalls = new AtomicInteger(); | ||
AtomicInteger getQueueAttributesApiCalls = new AtomicInteger(); | ||
|
||
void reset() { | ||
receiveApiCalls.set(0); | ||
getQueueAttributesApiCalls.set(0); | ||
} | ||
|
||
@Override | ||
public void afterExecution(Context.AfterExecution context, ExecutionAttributes executionAttributes) { | ||
if (context.request() instanceof ReceiveMessageRequest) { | ||
receiveApiCalls.incrementAndGet(); | ||
} | ||
if (context.request() instanceof GetQueueAttributesRequest) { | ||
getQueueAttributesApiCalls.incrementAndGet(); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we removing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the user passes this value then user can control the Polling time out , this is behaviour same as https://github.com/aws/aws-sdk-java/blob/61d73631fac8535ad70666bbce9e70a1d2cea2ca/aws-java-sdk-sqs/src/main/java/com/amazonaws/services/sqs/buffered/ReceiveQueueBuffer.java#L192-L197
I removed this in earlier PR but when I was doing V1 comparison I realized that V1 uses this value to determine the Client side Wait time for that particular request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already supported in V2
aws-sdk-java-v2/services/sqs/src/main/java/software/amazon/awssdk/services/sqs/internal/batchmanager/QueueAttributesManager.java
Lines 76 to 82 in dd9379d
But after handling the Surface API review comments where I added a check to bypass, I went ahead and added check to this since it was an additional Attribute related to long polling but when used with Automatic Batch manager we use it client side polling time so thus we need this to go through the batch manager