|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.sqs.internal.batchmanager; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 27 | +import software.amazon.awssdk.services.sqs.SqsAsyncClient; |
| 28 | +import software.amazon.awssdk.services.sqs.model.GetQueueAttributesRequest; |
| 29 | +import software.amazon.awssdk.services.sqs.model.QueueAttributeName; |
| 30 | +import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; |
| 31 | +import software.amazon.awssdk.utils.CompletableFutureUtils; |
| 32 | +import software.amazon.awssdk.utils.Validate; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * The {@code QueueAttributesManager} class is responsible for managing and retrieving specific attributes |
| 37 | + * of an AWS SQS queue, such as message wait time and visibility timeout. It efficiently caches these attributes |
| 38 | + * to minimize redundant API calls to SQS, ensuring that the attributes are fetched only once and reused in subsequent requests. |
| 39 | + * |
| 40 | + * <p>This class uses an {@link AtomicReference} to maintain the state of the attribute map, allowing concurrent access |
| 41 | + * and handling cases where the fetching of attributes may fail. If an error occurs during the retrieval of attributes, |
| 42 | + * the state is reset to allow for a fresh attempt in subsequent calls.</p> |
| 43 | + * |
| 44 | + * <p>The class provides methods to get the visibility timeout and calculate the message receive timeout, which |
| 45 | + * are asynchronously retrieved and processed using {@link CompletableFuture}. These methods handle cancellation |
| 46 | + * scenarios by cancelling the SQS request if the calling future is cancelled.</p> |
| 47 | + * |
| 48 | + * <p>This class is intended for internal use and is marked with the {@link SdkInternalApi} annotation.</p> |
| 49 | + */ |
| 50 | +@SdkInternalApi |
| 51 | +public final class QueueAttributesManager { |
| 52 | + |
| 53 | + private static final List<QueueAttributeName> QUEUE_ATTRIBUTE_NAMES = |
| 54 | + Arrays.asList(QueueAttributeName.RECEIVE_MESSAGE_WAIT_TIME_SECONDS, |
| 55 | + QueueAttributeName.VISIBILITY_TIMEOUT); |
| 56 | + private final SqsAsyncClient sqsClient; |
| 57 | + private final String queueUrl; |
| 58 | + private final AtomicReference<CompletableFuture<Map<QueueAttributeName, String>>> queueAttributeMap = new AtomicReference<>(); |
| 59 | + |
| 60 | + public QueueAttributesManager(SqsAsyncClient sqsClient, String queueUrl) { |
| 61 | + this.sqsClient = sqsClient; |
| 62 | + this.queueUrl = queueUrl; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieves the received message timeout based on the provided request and queue attributes. |
| 67 | + * |
| 68 | + * @param rq The receive message request |
| 69 | + * @param configuredWaitTime The configured minimum wait time |
| 70 | + * @return CompletableFuture with the calculated receive message timeout in milliseconds |
| 71 | + */ |
| 72 | + public CompletableFuture<Duration> getReceiveMessageTimeout(ReceiveMessageRequest rq, Duration configuredWaitTime) { |
| 73 | + Integer waitTimeSeconds = rq.waitTimeSeconds(); |
| 74 | + if (waitTimeSeconds != null) { |
| 75 | + long waitTimeMillis = TimeUnit.SECONDS.toMillis(waitTimeSeconds); |
| 76 | + return CompletableFuture.completedFuture(Duration.ofMillis(Math.max(configuredWaitTime.toMillis(), waitTimeMillis))); |
| 77 | + } |
| 78 | + |
| 79 | + CompletableFuture<Map<QueueAttributeName, String>> attributeFuture = getAttributeMap(); |
| 80 | + CompletableFuture<Duration> resultFuture = attributeFuture.thenApply(attributes -> { |
| 81 | + String waitTimeSecondsStr = attributes.get(QueueAttributeName.RECEIVE_MESSAGE_WAIT_TIME_SECONDS); |
| 82 | + long waitTimeFromSqsMillis = TimeUnit.SECONDS.toMillis(Long.parseLong(waitTimeSecondsStr)); |
| 83 | + return Duration.ofMillis(Math.max(configuredWaitTime.toMillis(), waitTimeFromSqsMillis)); |
| 84 | + }); |
| 85 | + |
| 86 | + return CompletableFutureUtils.forwardExceptionTo(resultFuture, attributeFuture); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Retrieves the visibility timeout for the queue. |
| 91 | + * |
| 92 | + * @return CompletableFuture with the visibility timeout in nanoseconds |
| 93 | + */ |
| 94 | + public CompletableFuture<Duration> getVisibilityTimeout() { |
| 95 | + CompletableFuture<Map<QueueAttributeName, String>> attributeFuture = getAttributeMap(); |
| 96 | + CompletableFuture<Duration> resultFuture = attributeFuture.thenApply(attributes -> { |
| 97 | + String visibilityTimeoutStr = attributes.get(QueueAttributeName.VISIBILITY_TIMEOUT); |
| 98 | + return Duration.ofSeconds(Integer.parseInt(visibilityTimeoutStr)); |
| 99 | + }); |
| 100 | + |
| 101 | + return CompletableFutureUtils.forwardExceptionTo(resultFuture, attributeFuture); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Retrieves the queue attributes based on the predefined attribute names. |
| 106 | + * |
| 107 | + * @return CompletableFuture with the map of attribute names and their values. |
| 108 | + */ |
| 109 | + private CompletableFuture<Map<QueueAttributeName, String>> getAttributeMap() { |
| 110 | + CompletableFuture<Map<QueueAttributeName, String>> future = queueAttributeMap.get(); |
| 111 | + |
| 112 | + if (future == null || future.isCompletedExceptionally()) { |
| 113 | + CompletableFuture<Map<QueueAttributeName, String>> newFuture = new CompletableFuture<>(); |
| 114 | + |
| 115 | + if (queueAttributeMap.compareAndSet(future, newFuture)) { |
| 116 | + fetchQueueAttributes().whenComplete((r, t) -> { |
| 117 | + if (t != null) { |
| 118 | + newFuture.completeExceptionally(t); |
| 119 | + } else { |
| 120 | + newFuture.complete(r); |
| 121 | + } |
| 122 | + }); |
| 123 | + return newFuture; |
| 124 | + } else { |
| 125 | + newFuture.cancel(true); |
| 126 | + return queueAttributeMap.get(); |
| 127 | + } |
| 128 | + } |
| 129 | + return future; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Fetches the queue attributes from SQS and completes the provided future with the result. |
| 134 | + * |
| 135 | + * @return CompletableFuture with the map of attribute names and values. |
| 136 | + */ |
| 137 | + private CompletableFuture<Map<QueueAttributeName, String>> fetchQueueAttributes() { |
| 138 | + GetQueueAttributesRequest request = GetQueueAttributesRequest.builder() |
| 139 | + .queueUrl(queueUrl) |
| 140 | + .attributeNames(QUEUE_ATTRIBUTE_NAMES) |
| 141 | + .build(); |
| 142 | + |
| 143 | + CompletableFuture<Map<QueueAttributeName, String>> future = |
| 144 | + sqsClient.getQueueAttributes(request) |
| 145 | + .thenApply(response -> { |
| 146 | + Map<QueueAttributeName, String> attributes = response.attributes(); |
| 147 | + Validate.notNull(attributes.get(QueueAttributeName.RECEIVE_MESSAGE_WAIT_TIME_SECONDS), |
| 148 | + QueueAttributeName.RECEIVE_MESSAGE_WAIT_TIME_SECONDS |
| 149 | + + " attribute is null in SQS."); |
| 150 | + Validate.notNull(attributes.get(QueueAttributeName.VISIBILITY_TIMEOUT), |
| 151 | + QueueAttributeName.VISIBILITY_TIMEOUT + " attribute is null in SQS."); |
| 152 | + return attributes.entrySet().stream() |
| 153 | + .filter(entry -> QUEUE_ATTRIBUTE_NAMES.contains(entry.getKey())) |
| 154 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 155 | + }); |
| 156 | + |
| 157 | + return future; |
| 158 | + } |
| 159 | +} |
0 commit comments