Skip to content

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
Merged
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 @@ -20,7 +20,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.services.sqs.SqsAsyncClient;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
Expand Down Expand Up @@ -61,10 +60,10 @@ public CompletableFuture<ReceiveMessageResponse> processRequest(ReceiveMessageRe
return queueAttributesManager.getReceiveMessageTimeout(rq, config.messageMinWaitDuration()).thenCompose(waitTimeMs -> {
CompletableFuture<ReceiveMessageResponse> receiveMessageFuture = new CompletableFuture<>();
receiveQueueBuffer.receiveMessage(receiveMessageFuture, numMessages);
CompletableFuture<ReceiveMessageResponse> timeoutFuture = new CompletableFuture<>();
executor.schedule(() -> timeoutFuture.complete(ReceiveMessageResponse.builder().build()), waitTimeMs.toMillis(),
executor.schedule(() -> receiveMessageFuture.complete(ReceiveMessageResponse.builder().build()),
waitTimeMs.toMillis(),
TimeUnit.MILLISECONDS);
return receiveMessageFuture.applyToEither(timeoutFuture, Function.identity());
return receiveMessageFuture;

});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ private String checkBatchingEligibility(ReceiveMessageRequest rq) {
if (rq.overrideConfiguration().isPresent()) {
return "Request has override configurations.";
}
if (rq.waitTimeSeconds() != null && rq.waitTimeSeconds() != 0) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Integer waitTimeSeconds = rq.waitTimeSeconds();
if (waitTimeSeconds != null) {
long waitTimeMillis = TimeUnit.SECONDS.toMillis(waitTimeSeconds);
return CompletableFuture.completedFuture(Duration.ofMillis(Math.max(configuredWaitTime.toMillis(), waitTimeMillis)));
}
CompletableFuture<Map<QueueAttributeName, String>> attributeFuture = getAttributeMap();

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

return "Request has long polling enabled.";
}
return null;
}

Expand Down
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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static Stream<Arguments> provideBatchOverrideConfigurations() {
"Request has override configurations."
),
Arguments.of(
"Buffering disabled, with waitTimeSeconds in ReceiveMessageRequest",
"Buffering enabled, with waitTimeSeconds in ReceiveMessageRequest",
ResponseBatchConfiguration.builder()
.messageSystemAttributeNames(Collections.singletonList(MessageSystemAttributeName.SENDER_ID))
.build(),
Expand All @@ -245,8 +245,8 @@ private static Stream<Arguments> provideBatchOverrideConfigurations() {
.maxNumberOfMessages(3)
.waitTimeSeconds(3)
.build(),
false,
"Request has long polling enabled."
true,
""
)
);
}
Expand Down
Loading