fix(sqs): reflect CancelMessageMoveTask at once instead of after the rate interval (#3757)

* fix(sqs): reflect CancelMessageMoveTask at once instead of after the rate interval

A throttled message move task slept 1000 / MaxNumberOfMessagesPerSecond ms
between moves and checked its cancel flag only afterwards, and
CancelMessageMoveTask set the flag without waking the worker. At rate 1 a
cancel therefore took up to a second to be reflected and the task kept
reporting RUNNING meanwhile. AWS reports CANCELLING as soon as the cancel is
accepted and CANCELLED once the in-flight message is done. The latency is
what made cancelledMessageMoveTasks_areBoundedAndAllowTheNextTask flaky:
eleven rate-1 cancels each waiting about a second on a loaded shard.

Replace the flag with a one-shot latch the worker waits on as its throttle,
so a cancel wakes it and its finally block writes CANCELLED at once. Mark
the task CANCELLING when the cancel is accepted, with a compare-and-set so a
task that already completed is left alone, and classify CANCELLING as
active so it neither starts the terminal TTL nor counts against the
terminal history cap, and still blocks a second task on the same source.

Closes #3756

* fix(sqs): record move task progress under the store lock

cancelMessageMoveTask now writes a task's status from a second thread, and
updateMoveTaskCounter's get-then-put outside the store's lock was only safe
while the worker was the sole writer: a cancel landing between the get and
the put could be stomped back to RUNNING until the loop resolved to
CANCELLED. Move the counter update into a synchronized store method that
reads the current status and writes back under the same lock transition
uses. Import CountDownLatch and TimeUnit and make the two touched
declarations explicit.

Raised in review on #3757.

* fix(sqs): resolve a move task's terminal status under the store lock

The worker's finally block read the task, decided COMPLETED or CANCELLED
from its own cancellation signal, and only then wrote, all outside the
store's lock. A cancel accepted between the decision and the write, which
had already moved the task to CANCELLING, was overwritten with COMPLETED.
Resolve the outcome in a synchronized store method that reads the status
at that moment: a task already CANCELLING ends CANCELLED whatever the
worker concluded, otherwise the worker's view stands. A package-private
finishMoveTask lets the regression replay the worker's terminal write with
a stale decision after a cancel has been accepted.

Raised in review on #3757.
This commit is contained in:
David authored and GitHub committed 2026-09-16 19:00:07 -04:00
1 parent 98c47c4347
commit 8d755c20d6
2 files changed
+204 -36

No files matched your search

@@ -1309,6 +1309,102 @@ class SqsServiceTest {
awaitMoveTaskStatus(dlqArn, taskHandle, "CANCELLED");
}
@Test
void cancelMessageMoveTask_takesEffectWithoutWaitingOutTheRateInterval() throws Exception {
Queue dlq = sqsService.createQueue("cancel-latency-dlq", null, "us-east-1");
String dlqArn = queueArn("cancel-latency-dlq");
sqsService.createQueue("cancel-latency-source",
Map.of("RedrivePolicy",
"{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}"),
"us-east-1");
sqsService.createQueue("cancel-latency-destination", null, "us-east-1");
String destinationArn = queueArn("cancel-latency-destination");
for (int i = 0; i < 50; i++) {
sqsService.sendMessage(dlq.getQueueUrl(), "msg-" + i, 0, null, null, "us-east-1");
}
// At one message per second the worker sleeps a full second between moves. AWS
// reflects a cancel immediately (CANCELLING, then CANCELLED once nothing is in
// flight); the worker must be woken, not left to sleep out its interval.
String taskHandle = sqsService.startMessageMoveTask(dlqArn, destinationArn, 1, "us-east-1");
sqsService.cancelMessageMoveTask(taskHandle, "us-east-1");
String statusRightAfterCancel = moveTaskStatus(dlqArn, taskHandle);
assertTrue("CANCELLING".equals(statusRightAfterCancel) || "CANCELLED".equals(statusRightAfterCancel),
"expected CANCELLING or CANCELLED right after CancelMessageMoveTask returned, was " + statusRightAfterCancel);
long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(300);
while (!"CANCELLED".equals(moveTaskStatus(dlqArn, taskHandle))) {
assertTrue(System.nanoTime() < deadline,
"move task still " + moveTaskStatus(dlqArn, taskHandle) + " 300 ms after the cancel");
Thread.sleep(5);
}
assertEquals(1, sqsService.listMessageMoveTasks(dlqArn, "us-east-1").size());
}
@Test
void cancelMessageMoveTask_statusNeverRegressesToRunningWhileTheWorkerRecordsProgress() throws Exception {
Queue dlq = sqsService.createQueue("cancel-stomp-dlq", null, "us-east-1");
String dlqArn = queueArn("cancel-stomp-dlq");
sqsService.createQueue("cancel-stomp-source",
Map.of("RedrivePolicy",
"{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}"),
"us-east-1");
sqsService.createQueue("cancel-stomp-destination", null, "us-east-1");
String destinationArn = queueArn("cancel-stomp-destination");
for (int i = 0; i < 3000; i++) {
sqsService.sendMessage(dlq.getQueueUrl(), "msg-" + i, 0, null, null, "us-east-1");
}
// Unthrottled, so the worker records its count after every message while the cancel
// lands: the two writers share the store's lock, and once the cancel has returned the
// task must only ever read CANCELLING or CANCELLED.
String taskHandle = sqsService.startMessageMoveTask(dlqArn, destinationArn, "us-east-1");
sqsService.cancelMessageMoveTask(taskHandle, "us-east-1");
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
String status;
while (!"CANCELLED".equals(status = moveTaskStatus(dlqArn, taskHandle))) {
assertNotEquals("RUNNING", status, "a cancelled task reported RUNNING again");
assertTrue(System.nanoTime() < deadline, "move task never reached CANCELLED, last seen " + status);
}
}
@Test
void cancelMessageMoveTask_acceptedCancelIsNotOverwrittenByTheWorkersTerminalWrite() throws Exception {
Queue dlq = sqsService.createQueue("cancel-terminal-dlq", null, "us-east-1");
String dlqArn = queueArn("cancel-terminal-dlq");
sqsService.createQueue("cancel-terminal-source",
Map.of("RedrivePolicy",
"{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}"),
"us-east-1");
sqsService.createQueue("cancel-terminal-destination", null, "us-east-1");
String destinationArn = queueArn("cancel-terminal-destination");
for (int i = 0; i < 50; i++) {
sqsService.sendMessage(dlq.getQueueUrl(), "msg-" + i, 0, null, null, "us-east-1");
}
// The interleaving: the worker reads its own signal and concludes COMPLETED, the cancel is
// accepted (the task reads CANCELLING), and only then does the worker's terminal write
// land. Replaying that write with the stale decision must not turn CANCELLING back into
// COMPLETED.
String taskHandle = sqsService.startMessageMoveTask(dlqArn, destinationArn, 1, "us-east-1");
sqsService.cancelMessageMoveTask(taskHandle, "us-east-1");
sqsService.finishMoveTask(taskHandle, 1, false);
assertEquals("CANCELLED", moveTaskStatus(dlqArn, taskHandle));
awaitMoveTaskStatus(dlqArn, taskHandle, "CANCELLED");
assertEquals("CANCELLED", moveTaskStatus(dlqArn, taskHandle));
}
private String moveTaskStatus(String sourceArn, String taskHandle) {
return sqsService.listMessageMoveTasks(sourceArn, "us-east-1").stream()
.filter(task -> task.taskHandle().equals(taskHandle))
.map(SqsService.MoveTask::status)
.findFirst()
.orElse("<absent>");
}
@Test
void completedMessageMoveTasks_areBoundedToRecentSummaries() throws Exception {
sqsService.createQueue("terminal-cap-dlq", null, "us-east-1");