From 5dedeff9e4fe54d85c12af76ae2e0d1ef08a885f Mon Sep 17 00:00:00 2001 From: Stuart Lang Date: Tue, 19 May 2026 05:01:30 +0100 Subject: [PATCH] feat(sqs): implement StartMessageMoveTask / List / Cancel with async drain + rate limiting (#910) * feat(sqs): implement MessageMoveTask tracking, cancel, and validation StartMessageMoveTask was a synchronous drain that returned a throwaway task handle. ListMessageMoveTasks always returned [] and CancelMessageMoveTask was not implemented at all. This change keeps the synchronous-move semantics but introduces a per-account in-memory task store and proper validation around the API. Changes ------- - Add SqsService.MoveTask record (handle, source, destination, max rate, status, counters, timestamps, failure reason). - ConcurrentHashMap retained for the lifetime of the process so List/Cancel can find tasks that have already completed. - StartMessageMoveTask: * Accepts MaxNumberOfMessagesPerSecond and stores it on the task. * Validates source ARN parses and queue exists (NonExistentQueue). * Validates source is referenced as a DLQ by at least one other queue's RedrivePolicy; otherwise throws InvalidParameterValue. * Validates destination queue exists when provided; otherwise throws ResourceNotFoundException (was a generic AmazonSQSException). * Rejects a second concurrent task on the same source with UnsupportedOperation. - ListMessageMoveTasks returns tasks for the given source, newest first, capped by MaxResults. - CancelMessageMoveTask marks the task CANCELLED and returns ApproximateNumberOfMessagesMoved; ResourceNotFoundException for unknown handles. - Both JSON 1.0 and Query handlers wired through; CancelMessageMoveTask dispatch added to both. Known follow-ups ---------------- - StartMessageMoveTask with no DestinationArn currently drops drained messages; AWS semantics is "return each message to its original source queue", which requires tracking per-message origin on the DLQ side. Left for a future change. * feat(sqs): async + rate-limited StartMessageMoveTask, reset receive state, NoDestinationArn returns messages to origin The synchronous move-task implementation had several gaps that surfaced under realistic client behavior: * MaxNumberOfMessagesPerSecond was accepted but ignored -- the move completed instantly, so a low rate with a follow-up cancel could never actually catch the worker mid-flight. * Cancel was effectively a no-op for the same reason. * Two-task-for-same-source rejection couldn't fire because the first task was already done. * NoDestinationArn dropped the messages instead of returning them to the queue they were DLQ'd from. * Messages moved from a DLQ back to a queue with maxReceiveCount=1 retained their old receiveCount, so the very first ReceiveMessage re-triggered the DLQ rule and silently round-tripped them back. This change reworks the move task to run on a background worker that: * Drains the source one message at a time (so the source queue stays observably populated while the move is in flight at low rates). * Throttles to the requested MaxNumberOfMessagesPerSecond (sleep 1000/rate ms between deliveries; 0 = no throttle). * Resets receiveCount / firstReceiveTimestamp / receiptHandle / visibleAt on each moved message so it starts a fresh life on the destination queue. * Honours a cancellation flag that CancelMessageMoveTask flips; the worker observes it between iterations and transitions the task to CANCELLED in its finally block. * Routes per-message origin when no DestinationArn is supplied. The receive path now records the source queue URL on each message it routes to a DLQ via the redrive policy. * Moves the very first message synchronously inside the request scope so callers that poll the destination immediately after the call returns see motion without racing the executor's thread start. The two-task concurrency check trips on a recent task (started within the last second) as well as a still-RUNNING one, which catches the back-to-back-call pattern AWS rejects without spuriously blocking legitimate re-runs minutes later. * fix(sqs): MessageMoveTask review fixes - exact ARN match + AWS-aligned error Review feedback on #910: - Concurrent-task rejection used "AWS.SimpleQueueService.UnsupportedOperation". Real AWS uses InvalidParameterValue with the message "There is already a task running. Only one active task is allowed for a source queue arn at a given time." Align the error code and message with the observed AWS response so SDK exception typing works correctly. - isDeadLetterQueue and listDeadLetterSourceQueues substring-matched the raw RedrivePolicy JSON, so a queue ARN that happened to be a prefix of another queue's ARN was falsely identified as a DLQ. Parse the RedrivePolicy and compare deadLetterTargetArn exactly. Add a regression test covering both the StartMessageMoveTask and ListDeadLetterSourceQueues paths. - Update the stale comment describing the concurrent-task race window so it reflects the background-worker design (the move is no longer synchronous). --- .../services/sqs/GuardedMessageQueue.java | 14 + .../floci/services/sqs/SqsJsonHandler.java | 35 ++- .../floci/services/sqs/SqsQueryHandler.java | 35 ++- .../floci/services/sqs/SqsService.java | 277 ++++++++++++++++-- .../floci/services/sqs/model/Message.java | 9 + .../floci/services/sqs/SqsServiceTest.java | 133 +++++++++ 6 files changed, 474 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/GuardedMessageQueue.java b/src/main/java/io/github/hectorvent/floci/services/sqs/GuardedMessageQueue.java index 91d18638d4..b61ca67e7f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/GuardedMessageQueue.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/GuardedMessageQueue.java @@ -189,6 +189,20 @@ class GuardedMessageQueue { } } + /** Remove and return the first message in insertion order, or null if empty. + * Used by the message-move-task worker so the source queue stays observably + * populated for the duration of a rate-limited move. */ + Message drainOne() { + try (var _ = hold()) { + if (messages.isEmpty()) { + return null; + } + Message head = messages.remove(0); + persist(); + return head; + } + } + record MessageCounts(long visible, long inFlight) { } diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java index 4c9c2469b5..e0c65ad9be 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java @@ -57,6 +57,7 @@ public class SqsJsonHandler { case "ListDeadLetterSourceQueues" -> handleListDeadLetterSourceQueues(request, region); case "StartMessageMoveTask" -> handleStartMessageMoveTask(request, region); case "ListMessageMoveTasks" -> handleListMessageMoveTasks(request, region); + case "CancelMessageMoveTask" -> handleCancelMessageMoveTask(request, region); case "AddPermission" -> handleAddPermission(request, region); case "RemovePermission" -> handleRemovePermission(request, region); default -> Response.status(400) @@ -412,7 +413,8 @@ public class SqsJsonHandler { private Response handleStartMessageMoveTask(JsonNode request, String region) { String sourceArn = request.path("SourceArn").asText(null); String destinationArn = request.path("DestinationArn").asText(null); - String taskHandle = sqsService.startMessageMoveTask(sourceArn, destinationArn, region); + int maxRate = request.path("MaxNumberOfMessagesPerSecond").asInt(0); + String taskHandle = sqsService.startMessageMoveTask(sourceArn, destinationArn, maxRate, region); ObjectNode response = objectMapper.createObjectNode(); response.put("TaskHandle", taskHandle); return Response.ok(response).build(); @@ -420,9 +422,36 @@ public class SqsJsonHandler { private Response handleListMessageMoveTasks(JsonNode request, String region) { String sourceArn = request.path("SourceArn").asText(null); - sqsService.listMessageMoveTasks(sourceArn, region); + int maxResults = request.path("MaxResults").asInt(10); + List tasks = sqsService.listMessageMoveTasks(sourceArn, region); ObjectNode response = objectMapper.createObjectNode(); - response.putArray("Results"); + ArrayNode results = response.putArray("Results"); + int count = 0; + for (SqsService.MoveTask t : tasks) { + if (count++ >= maxResults) break; + ObjectNode node = results.addObject(); + node.put("TaskHandle", t.taskHandle()); + node.put("SourceArn", t.sourceArn()); + if (t.destinationArn() != null) { + node.put("DestinationArn", t.destinationArn()); + } + node.put("MaxNumberOfMessagesPerSecond", t.maxNumberOfMessagesPerSecond()); + node.put("Status", t.status()); + node.put("ApproximateNumberOfMessagesMoved", t.approximateNumberOfMessagesMoved()); + node.put("ApproximateNumberOfMessagesToMove", t.approximateNumberOfMessagesToMove()); + node.put("StartedTimestamp", t.startedTimestampMillis()); + if (t.failureReason() != null) { + node.put("FailureReason", t.failureReason()); + } + } + return Response.ok(response).build(); + } + + private Response handleCancelMessageMoveTask(JsonNode request, String region) { + String taskHandle = request.path("TaskHandle").asText(null); + long moved = sqsService.cancelMessageMoveTask(taskHandle, region); + ObjectNode response = objectMapper.createObjectNode(); + response.put("ApproximateNumberOfMessagesMoved", moved); return Response.ok(response).build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java index 782925f9ae..26e2708119 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsQueryHandler.java @@ -56,6 +56,7 @@ public class SqsQueryHandler { case "ListDeadLetterSourceQueues" -> handleListDeadLetterSourceQueues(params, region); case "StartMessageMoveTask" -> handleStartMessageMoveTask(params, region); case "ListMessageMoveTasks" -> handleListMessageMoveTasks(params, region); + case "CancelMessageMoveTask" -> handleCancelMessageMoveTask(params, region); case "AddPermission" -> handleAddPermission(params, region); case "RemovePermission" -> handleRemovePermission(params, region); default -> AwsQueryResponse.error("UnsupportedOperation", @@ -377,18 +378,46 @@ public class SqsQueryHandler { private Response handleStartMessageMoveTask(MultivaluedMap params, String region) { String sourceArn = getParam(params, "SourceArn"); String destinationArn = getParam(params, "DestinationArn"); - String taskHandle = sqsService.startMessageMoveTask(sourceArn, destinationArn, region); + int maxRate = getIntParam(params, "MaxNumberOfMessagesPerSecond", 0); + String taskHandle = sqsService.startMessageMoveTask(sourceArn, destinationArn, maxRate, region); var xml = new XmlBuilder().elem("TaskHandle", taskHandle); return Response.ok(AwsQueryResponse.envelope("StartMessageMoveTask", null, xml.build())).build(); } private Response handleListMessageMoveTasks(MultivaluedMap params, String region) { String sourceArn = getParam(params, "SourceArn"); - sqsService.listMessageMoveTasks(sourceArn, region); - var xml = new XmlBuilder(); // Empty list for mock + int maxResults = getIntParam(params, "MaxResults", 10); + List tasks = sqsService.listMessageMoveTasks(sourceArn, region); + var xml = new XmlBuilder(); + int count = 0; + for (SqsService.MoveTask t : tasks) { + if (count++ >= maxResults) break; + xml.start("member") + .elem("TaskHandle", t.taskHandle()) + .elem("SourceArn", t.sourceArn()); + if (t.destinationArn() != null) { + xml.elem("DestinationArn", t.destinationArn()); + } + xml.elem("MaxNumberOfMessagesPerSecond", t.maxNumberOfMessagesPerSecond()) + .elem("Status", t.status()) + .elem("ApproximateNumberOfMessagesMoved", t.approximateNumberOfMessagesMoved()) + .elem("ApproximateNumberOfMessagesToMove", t.approximateNumberOfMessagesToMove()) + .elem("StartedTimestamp", t.startedTimestampMillis()); + if (t.failureReason() != null) { + xml.elem("FailureReason", t.failureReason()); + } + xml.end("member"); + } return Response.ok(AwsQueryResponse.envelope("ListMessageMoveTasks", null, xml.build())).build(); } + private Response handleCancelMessageMoveTask(MultivaluedMap params, String region) { + String taskHandle = getParam(params, "TaskHandle"); + long moved = sqsService.cancelMessageMoveTask(taskHandle, region); + var xml = new XmlBuilder().elem("ApproximateNumberOfMessagesMoved", moved); + return Response.ok(AwsQueryResponse.envelope("CancelMessageMoveTask", null, xml.build())).build(); + } + private Response handlePurgeQueue(MultivaluedMap params, String region) { String queueUrl = getParam(params, "QueueUrl"); sqsService.purgeQueue(queueUrl, region); diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java index ce615126cf..45caef03ba 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java @@ -38,12 +38,32 @@ public class SqsService { private final ConcurrentHashMap queueLocks = new ConcurrentHashMap<>(); private final ConcurrentHashMap redrivePolicyCache = new ConcurrentHashMap<>(); private final ConcurrentHashMap> deduplicationCache = new ConcurrentHashMap<>(); + /** Move tasks keyed by opaque task handle. */ + private final ConcurrentHashMap moveTasksByHandle = new ConcurrentHashMap<>(); + /** Per-task cancellation flag the move worker polls between iterations. */ + private final ConcurrentHashMap moveTaskCancellation = + new ConcurrentHashMap<>(); + /** Move tasks execute on a background thread so MaxNumberOfMessagesPerSecond can throttle + * and CancelMessageMoveTask has something to interrupt. One thread per task is sufficient. */ + private final java.util.concurrent.ExecutorService moveTaskExecutor = + java.util.concurrent.Executors.newCachedThreadPool(r -> { + Thread t = new Thread(r, "sqs-move-task"); + t.setDaemon(true); + return t; + }); private final AtomicLong sequenceCounter = new AtomicLong(0); private static final HexFormat HEX = HexFormat.of(); private record RedrivePolicy(int maxReceiveCount, String deadLetterTargetArn) { } + public record MoveTask(String taskHandle, String sourceArn, String destinationArn, + int maxNumberOfMessagesPerSecond, String status, + long approximateNumberOfMessagesMoved, + long approximateNumberOfMessagesToMove, + long startedTimestampMillis, String failureReason) { + } + private final int defaultVisibilityTimeout; private final int maxMessageSize; private final String baseUrl; @@ -651,6 +671,9 @@ public class SqsService { for (Message msg : dlqCandidates) { msg.setVisibleAt(null); msg.setReceiptHandle(null); + // Remember where the message came from so StartMessageMoveTask + // with no DestinationArn can put it back. + msg.setOriginalSourceQueueUrl(queue.getQueueUrl()); } String dlqStorageKey = regionKey(region, dlqUrl); getOrCreateQueue(dlqStorageKey).addAll(dlqCandidates); @@ -769,8 +792,7 @@ public class SqsService { List sourceQueues = new ArrayList<>(); String prefix = region + "::"; for (Queue q : queueStore.scan(k -> k.startsWith(prefix))) { - String redrive = q.getAttributes().get("RedrivePolicy"); - if (redrive != null && redrive.contains(targetArn)) { + if (targetArn.equals(redriveDlqArn(q))) { sourceQueues.add(q.getQueueUrl()); } } @@ -778,29 +800,238 @@ public class SqsService { } public String startMessageMoveTask(String sourceArn, String destinationArn, String region) { - String sourceUrl = queueUrlFromArn(sourceArn, region); - String destUrl = destinationArn != null ? queueUrlFromArn(destinationArn, region) : null; - if (sourceUrl == null) { - throw new AwsException("InvalidParameterValue", "Invalid source ARN", 400); - } - - String srcKey = regionKey(region, sourceUrl); - ensureQueueExists(srcKey); - - var srcQueue = getOrCreateQueue(srcKey); - List drained = srcQueue.drainAll(); - if (destUrl != null) { - String destKey = regionKey(region, destUrl); - ensureQueueExists(destKey); - getOrCreateQueue(destKey).addAll(drained); - } - - LOG.infov("Moved messages from {0} to {1}", sourceArn, destinationArn != null ? destinationArn : "original source"); - return "task-" + UUID.randomUUID().toString(); + return startMessageMoveTask(sourceArn, destinationArn, 0, region); } - public List> listMessageMoveTasks(String sourceArn, String region) { - return Collections.emptyList(); + public String startMessageMoveTask(String sourceArn, String destinationArn, + int maxNumberOfMessagesPerSecond, String region) { + if (sourceArn == null) { + throw new AwsException("InvalidParameterValue", "SourceArn is required.", 400); + } + String sourceUrl = queueUrlFromArn(sourceArn, region); + if (sourceUrl == null) { + throw new AwsException("InvalidParameterValue", + "Invalid source ARN: " + sourceArn, 400); + } + String srcKey = regionKey(region, sourceUrl); + if (queueStore.get(srcKey).isEmpty()) { + throw new AwsException("ResourceNotFoundException", + "The resource that you specified for the SourceArn parameter does not exist.", 404); + } + // Source must be referenced as a DLQ by some other queue's RedrivePolicy. + if (!isDeadLetterQueue(sourceArn, region)) { + throw new AwsException("InvalidParameterValue", + "Source queue is not configured as a dead-letter queue.", 400); + } + // Reject a second task that races against a still-active one for the same source. + // The move runs on a background worker, so RUNNING is the canonical "active" check; + // the additional 1-second window covers the small gap between StartMessageMoveTask + // returning and the worker flipping the task to RUNNING (so back-to-back start + // calls that AWS would reject can't slip through). + long now = System.currentTimeMillis(); + for (MoveTask existing : moveTasksByHandle.values()) { + if (!sourceArn.equals(existing.sourceArn())) { + continue; + } + if ("RUNNING".equals(existing.status()) + || (now - existing.startedTimestampMillis()) < 1_000) { + throw new AwsException("InvalidParameterValue", + "There is already a task running. Only one active task is allowed for a source queue arn at a given time.", + 400); + } + } + + String destUrl = null; + if (destinationArn != null) { + destUrl = queueUrlFromArn(destinationArn, region); + if (destUrl == null) { + throw new AwsException("ResourceNotFoundException", + "The resource that you specified for the DestinationArn parameter does not exist.", 404); + } + String destKey = regionKey(region, destUrl); + if (queueStore.get(destKey).isEmpty()) { + throw new AwsException("ResourceNotFoundException", + "The resource that you specified for the DestinationArn parameter does not exist.", 404); + } + } + + var srcQueueInitial = getOrCreateQueue(srcKey); + long toMove = srcQueueInitial.messageCounts().visible() + + srcQueueInitial.messageCounts().inFlight(); + + String taskHandle = "task-" + UUID.randomUUID(); + moveTasksByHandle.put(taskHandle, new MoveTask( + taskHandle, sourceArn, destinationArn, + maxNumberOfMessagesPerSecond, "RUNNING", + 0L, toMove, System.currentTimeMillis(), null)); + var cancelled = new java.util.concurrent.atomic.AtomicBoolean(false); + moveTaskCancellation.put(taskHandle, cancelled); + + // Move the first message synchronously inside the request scope so callers + // that poll the destination immediately after StartMessageMoveTask returns + // see motion without racing against the executor's thread startup. The + // remainder runs on the background worker at the requested rate, draining + // one message at a time so the source queue stays observably populated. + long initialMoved = 0; + Message firstMsg = srcQueueInitial.drainOne(); + if (firstMsg != null && deliverMovedMessage(firstMsg, destUrl, region)) { + initialMoved = 1; + updateMoveTaskCounter(taskHandle, initialMoved); + } + + final String destUrlFinal = destUrl; + final long initialMovedFinal = initialMoved; + moveTaskExecutor.submit(() -> runMoveTask(taskHandle, srcKey, + destUrlFinal, maxNumberOfMessagesPerSecond, region, sourceArn, + destinationArn, cancelled, initialMovedFinal)); + + return taskHandle; + } + + /** Place a single drained message in its destination according to the rules of + * StartMessageMoveTask (explicit destination, or the per-message origin + * recorded when it was DLQ'd). Resets per-receive state so the message + * starts a fresh life. Returns true when the message was placed. */ + private boolean deliverMovedMessage(Message msg, String destUrl, String region) { + msg.setReceiveCount(0); + msg.setFirstReceiveTimestamp(null); + msg.setReceiptHandle(null); + msg.setVisibleAt(null); + if (destUrl != null) { + String destKey = regionKey(region, destUrl); + getOrCreateQueue(destKey).addAll(List.of(msg)); + return true; + } + if (msg.getOriginalSourceQueueUrl() != null) { + // No request scope on the background worker, so queueStore.get() resolves + // to the default account prefix and can't verify the destination. The + // in-memory messagesByQueue map is keyed by the full URL (which carries + // the account), so addAll on the right storage key lands the message in + // the queue any future receive will see. + String originKey = regionKey(region, msg.getOriginalSourceQueueUrl()); + getOrCreateQueue(originKey).addAll(List.of(msg)); + return true; + } + return false; + } + + /** Background body of a move task; drains one message at a time so a + * positive MaxNumberOfMessagesPerSecond can throttle, CancelMessageMoveTask + * has a window to stop the work before it finishes, and the source queue + * stays observably populated for the duration of the move (rather than + * being drained into worker memory all at once). */ + private void runMoveTask(String taskHandle, String srcKey, String destUrl, + int maxRate, String region, String sourceArn, + String destinationArn, + java.util.concurrent.atomic.AtomicBoolean cancelled, + long initialMoved) { + long intervalMillis = maxRate > 0 ? Math.max(1L, 1000L / maxRate) : 0L; + long moved = initialMoved; + try { + var srcQueue = getOrCreateQueue(srcKey); + while (!cancelled.get()) { + if (intervalMillis > 0) { + try { + Thread.sleep(intervalMillis); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + return; + } + if (cancelled.get()) { + break; + } + } + Message msg = srcQueue.drainOne(); + if (msg == null) { + break; + } + if (deliverMovedMessage(msg, destUrl, region)) { + moved++; + updateMoveTaskCounter(taskHandle, moved); + } + } + } finally { + MoveTask cur = moveTasksByHandle.get(taskHandle); + if (cur != null) { + String status = cancelled.get() ? "CANCELLED" : "COMPLETED"; + moveTasksByHandle.put(taskHandle, new MoveTask( + cur.taskHandle(), cur.sourceArn(), cur.destinationArn(), + cur.maxNumberOfMessagesPerSecond(), status, + moved, cur.approximateNumberOfMessagesToMove(), + cur.startedTimestampMillis(), cur.failureReason())); + } + LOG.infov("Move task {0} {1}: moved {2} messages from {3} to {4}", taskHandle, + cancelled.get() ? "cancelled" : "completed", moved, sourceArn, + destinationArn != null ? destinationArn : "original source"); + } + } + + private void updateMoveTaskCounter(String taskHandle, long moved) { + MoveTask cur = moveTasksByHandle.get(taskHandle); + if (cur == null) { + return; + } + moveTasksByHandle.put(taskHandle, new MoveTask( + cur.taskHandle(), cur.sourceArn(), cur.destinationArn(), + cur.maxNumberOfMessagesPerSecond(), cur.status(), + moved, cur.approximateNumberOfMessagesToMove(), + cur.startedTimestampMillis(), cur.failureReason())); + } + + public List listMessageMoveTasks(String sourceArn, String region) { + if (sourceArn == null) { + return List.of(); + } + List matching = new ArrayList<>(); + for (MoveTask t : moveTasksByHandle.values()) { + if (sourceArn.equals(t.sourceArn())) { + matching.add(t); + } + } + matching.sort(Comparator.comparingLong(MoveTask::startedTimestampMillis).reversed()); + return matching; + } + + public long cancelMessageMoveTask(String taskHandle, String region) { + if (taskHandle == null) { + throw new AwsException("InvalidParameterValue", "TaskHandle is required.", 400); + } + MoveTask task = moveTasksByHandle.get(taskHandle); + if (task == null) { + throw new AwsException("ResourceNotFoundException", + "The task you specified does not exist.", 404); + } + // Signal the background worker to stop. The worker flips status to CANCELLED in + // its finally block and updates the moved counter; read both back here. + var flag = moveTaskCancellation.get(taskHandle); + if (flag != null) { + flag.set(true); + } + return moveTasksByHandle.getOrDefault(taskHandle, task).approximateNumberOfMessagesMoved(); + } + + private boolean isDeadLetterQueue(String queueArn, String region) { + String prefix = region + "::"; + for (Queue q : queueStore.scan(k -> k.startsWith(prefix))) { + if (queueArn.equals(redriveDlqArn(q))) { + return true; + } + } + return false; + } + + private static String redriveDlqArn(Queue queue) { + String raw = queue.getAttributes().get("RedrivePolicy"); + if (raw == null) { + return null; + } + try { + JsonNode policy = new com.fasterxml.jackson.databind.ObjectMapper().readTree(raw); + JsonNode dlqArn = policy.get("deadLetterTargetArn"); + return dlqArn != null && !dlqArn.isNull() ? dlqArn.asText() : null; + } catch (Exception e) { + return null; + } } public record ChangeVisibilityBatchEntry(String id, String receiptHandle, int visibilityTimeout) { diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java b/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java index 0dab645b4e..f1e2dde91f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/model/Message.java @@ -27,6 +27,12 @@ public class Message { private String messageDeduplicationId; private long sequenceNumber; + // When this message was relocated to a DLQ via the redrive policy, the URL of the + // queue it came from. Consumed by StartMessageMoveTask with no DestinationArn so + // we can return each message to where it originated. Not part of any AWS-visible + // surface. + private String originalSourceQueueUrl; + // Transient fields for visibility timeout tracking @JsonIgnore private String receiptHandle; @@ -85,6 +91,9 @@ public class Message { public long getSequenceNumber() { return sequenceNumber; } public void setSequenceNumber(long sequenceNumber) { this.sequenceNumber = sequenceNumber; } + public String getOriginalSourceQueueUrl() { return originalSourceQueueUrl; } + public void setOriginalSourceQueueUrl(String originalSourceQueueUrl) { this.originalSourceQueueUrl = originalSourceQueueUrl; } + @JsonIgnore public boolean isVisible() { return visibleAt == null || !Instant.now().isBefore(visibleAt); diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java index a8b72b3300..3058e1533e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsServiceTest.java @@ -596,6 +596,139 @@ class SqsServiceTest { assertEquals("msg3", third.get(0).getBody()); } + private static String queueArn(String name) { + return "arn:aws:sqs:us-east-1:000000000000:" + name; + } + + @Test + void startMessageMoveTask_storesTaskRetrievableByListMessageMoveTasks() { + sqsService.createQueue("orders-dlq", null); + String dlqArn = queueArn("orders-dlq"); + sqsService.createQueue("orders", + Map.of("RedrivePolicy", + "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}")); + sqsService.createQueue("orders-replay", null); + String destArn = queueArn("orders-replay"); + + String taskHandle = sqsService.startMessageMoveTask(dlqArn, destArn, 25, "us-east-1"); + + assertNotNull(taskHandle); + List tasks = sqsService.listMessageMoveTasks(dlqArn, "us-east-1"); + SqsService.MoveTask task = tasks.get(0); + assertEquals(taskHandle, task.taskHandle()); + assertEquals(dlqArn, task.sourceArn()); + assertEquals(destArn, task.destinationArn()); + assertEquals(25, task.maxNumberOfMessagesPerSecond()); + } + + @Test + void startMessageMoveTask_destinationDoesNotExist_throwsResourceNotFound() { + sqsService.createQueue("a-dlq", null); + String dlqArn = queueArn("a-dlq"); + sqsService.createQueue("a", + Map.of("RedrivePolicy", + "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}")); + + AwsException ex = assertThrows(AwsException.class, () -> + sqsService.startMessageMoveTask(dlqArn, queueArn("nope"), 0, "us-east-1")); + assertEquals("ResourceNotFoundException", ex.getErrorCode()); + } + + @Test + void startMessageMoveTask_sourceIsNotDeadLetterQueue_throwsInvalidParameterValue() { + sqsService.createQueue("just-a-queue", null); + sqsService.createQueue("dest", null); + + AwsException ex = assertThrows(AwsException.class, () -> + sqsService.startMessageMoveTask(queueArn("just-a-queue"), + queueArn("dest"), 0, "us-east-1")); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + } + + @Test + void startMessageMoveTask_doesNotMatchSubstringOfAnotherQueueArn() { + // Regression: isDeadLetterQueue / listDeadLetterSourceQueues used to + // substring-match the raw RedrivePolicy JSON, so a queue named "foo" + // would be falsely treated as a DLQ when another queue's redrive policy + // referenced ":foo-bar". Now both look at the parsed deadLetterTargetArn. + sqsService.createQueue("dlq-real", null); + String realDlqArn = queueArn("dlq-real"); + sqsService.createQueue("main-real", + Map.of("RedrivePolicy", + "{\"deadLetterTargetArn\":\"" + realDlqArn + "\",\"maxReceiveCount\":\"1\"}")); + + // Create a queue whose ARN is a strict prefix of realDlqArn. + sqsService.createQueue("dlq", null); + String prefixArn = queueArn("dlq"); + sqsService.createQueue("dest", null); + + AwsException ex = assertThrows(AwsException.class, () -> + sqsService.startMessageMoveTask(prefixArn, queueArn("dest"), 0, "us-east-1")); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + + // listDeadLetterSourceQueues should also only return source queues whose + // redrive policy references the exact ARN, not a substring match. + assertEquals(0, sqsService.listDeadLetterSourceQueues( + sqsService.getQueueUrl("dlq", "us-east-1"), "us-east-1").size()); + assertEquals(1, sqsService.listDeadLetterSourceQueues( + sqsService.getQueueUrl("dlq-real", "us-east-1"), "us-east-1").size()); + } + + @Test + void startMessageMoveTask_concurrentSecondTaskOnSameSource_throwsInvalidParameterValue() { + sqsService.createQueue("d-concurrent", null); + String dlqArn = queueArn("d-concurrent"); + sqsService.createQueue("p-concurrent", + Map.of("RedrivePolicy", + "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}")); + sqsService.createQueue("dest-concurrent", null); + String destArn = queueArn("dest-concurrent"); + + sqsService.startMessageMoveTask(dlqArn, destArn, 0, "us-east-1"); + + AwsException ex = assertThrows(AwsException.class, () -> + sqsService.startMessageMoveTask(dlqArn, destArn, 0, "us-east-1")); + assertEquals("InvalidParameterValue", ex.getErrorCode()); + assertTrue(ex.getMessage().toLowerCase().contains("already a task running")); + } + + @Test + void cancelMessageMoveTask_unknownHandle_throwsResourceNotFound() { + AwsException ex = assertThrows(AwsException.class, () -> + sqsService.cancelMessageMoveTask("task-does-not-exist", "us-east-1")); + assertEquals("ResourceNotFoundException", ex.getErrorCode()); + } + + @Test + void cancelMessageMoveTask_knownHandle_stopsBackgroundWorker() throws Exception { + Queue dlq = sqsService.createQueue("d", null); + String dlqArn = queueArn("d"); + sqsService.createQueue("p", + Map.of("RedrivePolicy", + "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}")); + sqsService.createQueue("dest", null); + String destArn = queueArn("dest"); + + // Load enough messages that, at the throttled rate, the worker can't possibly + // drain the queue before cancel is observed. + for (int i = 0; i < 50; i++) { + sqsService.sendMessage(dlq.getQueueUrl(), "msg-" + i, 0, null, null); + } + + String taskHandle = sqsService.startMessageMoveTask(dlqArn, destArn, 1, "us-east-1"); + sqsService.cancelMessageMoveTask(taskHandle, "us-east-1"); + + // Give the worker a moment to observe the cancel and write the terminal status. + for (int i = 0; i < 50; i++) { + var status = sqsService.listMessageMoveTasks(dlqArn, "us-east-1").get(0).status(); + if ("CANCELLED".equals(status)) { + return; + } + Thread.sleep(50); + } + fail("Move task did not transition to CANCELLED within timeout"); + } + @Test void fifoDedup_scopedToMessageGroup_acceptsSameDedupIdAcrossGroups() { Queue fifo = sqsService.createQueue("fair.fifo",