mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sqs): keep DLQ messages when a move task cannot deliver them (#3470)
* fix(sqs): keep DLQ messages when a move task cannot deliver them StartMessageMoveTask removed a message from the source queue before knowing whether it could be delivered. With no DestinationArn, a message without a recorded original source queue, or whose original source queue was deleted, was dropped or parked in a queue nobody could receive from, and a delivery failure lost the message outright. The move worker now checks that the target queue exists before taking the head message off the source (under one lock hold), restores the message at the head when delivery fails so the source keeps its order, and resolves the target queue by the account in its URL because the worker runs outside any request scope. Without that, only the first message of a queue owned by a non-default account was moved. * fix(sqs): deliver a copy on redrive so a failed move restores the source unchanged * fix(sqs): roll back the in-memory add when a queue persist fails * test(sqs): share the failing-store service helper
This commit is contained in:
1 parent
bea8f85f62
commit
a9feb3ccaf
5 files changed
+291
-27
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -15,11 +16,13 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class GuardedMessageQueueTest {
|
||||
@@ -148,6 +151,28 @@ class GuardedMessageQueueTest {
|
||||
assertEquals(2, result.claimed().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAllRollsBackTheInMemoryAddWhenPersistFails() {
|
||||
AtomicBoolean storeDown = new AtomicBoolean();
|
||||
var store = new InMemoryStorage<String, List<Message>>() {
|
||||
@Override
|
||||
public void put(String key, List<Message> value) {
|
||||
if (storeDown.get()) {
|
||||
throw new IllegalStateException("store unavailable");
|
||||
}
|
||||
super.put(key, value);
|
||||
}
|
||||
};
|
||||
var target = new GuardedMessageQueue(store, "us-east-1::/000000000000/target");
|
||||
target.addMessage(new Message("already-there"));
|
||||
storeDown.set(true);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> target.addAll(List.of(new Message("incoming"))));
|
||||
|
||||
assertEquals(List.of("already-there"), target.peekAll().stream().map(Message::getBody).toList());
|
||||
assertEquals(1, target.messageCounts().visible());
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageCountsReturnsVisibleInFlightAndDelayed() {
|
||||
queue.addMessage(new Message("msg1"));
|
||||
|
||||
@@ -2,26 +2,33 @@ package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Queue;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import jakarta.enterprise.context.ContextNotActiveException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class SqsServiceTest {
|
||||
|
||||
@@ -860,6 +867,175 @@ class SqsServiceTest {
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_withoutDestinationKeepsMessageWithoutOriginalSource() throws Exception {
|
||||
Queue dlq = sqsService.createQueue("orphan-dlq", null, "us-east-1");
|
||||
String dlqArn = queueArn("orphan-dlq");
|
||||
sqsService.createQueue("orphan-source", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
sqsService.sendMessage(dlq.getQueueUrl(), "orphan", 0, null, null, "us-east-1");
|
||||
sqsService.sendMessage(dlq.getQueueUrl(), "next", 0, null, null, "us-east-1");
|
||||
|
||||
String taskHandle = sqsService.startMessageMoveTask(dlqArn, null, 0, "us-east-1");
|
||||
|
||||
awaitMoveTaskStatus(dlqArn, taskHandle, "COMPLETED");
|
||||
assertEquals(List.of("orphan", "next"), bodies(sqsService.peekMessages(dlq.getQueueUrl(), "us-east-1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_withoutDestinationKeepsMessageWhoseOriginalSourceWasDeleted() throws Exception {
|
||||
Queue dlq = sqsService.createQueue("gone-dlq", null, "us-east-1");
|
||||
String dlqArn = queueArn("gone-dlq");
|
||||
Queue source = sqsService.createQueue("gone-source", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
// A sibling keeps the DLQ referenced by a redrive policy after the original source is gone.
|
||||
sqsService.createQueue("gone-sibling", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
sqsService.sendMessage(source.getQueueUrl(), "stranded", 0, null, null, "us-east-1");
|
||||
// Receiving past maxReceiveCount redrives the message into the DLQ with its original source recorded.
|
||||
assertEquals(1, sqsService.receiveMessage(source.getQueueUrl(), 1, 0, 0, "us-east-1").size());
|
||||
assertEquals(0, sqsService.receiveMessage(source.getQueueUrl(), 1, 0, 0, "us-east-1").size());
|
||||
assertEquals(List.of("stranded"), bodies(sqsService.peekMessages(dlq.getQueueUrl(), "us-east-1")));
|
||||
sqsService.deleteQueue(source.getQueueUrl(), "us-east-1");
|
||||
|
||||
String taskHandle = sqsService.startMessageMoveTask(dlqArn, null, 0, "us-east-1");
|
||||
|
||||
awaitMoveTaskStatus(dlqArn, taskHandle, "COMPLETED");
|
||||
assertEquals(List.of("stranded"), bodies(sqsService.peekMessages(dlq.getQueueUrl(), "us-east-1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_movesEveryMessageOfANonDefaultAccountQueue() throws Exception {
|
||||
String account = "111111111111";
|
||||
RequestContext requestContext = new RequestContext();
|
||||
requestContext.setAccountId(account);
|
||||
Thread caller = Thread.currentThread();
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<RequestContext> requestContextInstance = mock(Instance.class);
|
||||
// Only the calling thread has a request scope; the move worker runs outside any request.
|
||||
when(requestContextInstance.get()).thenAnswer(invocation -> {
|
||||
if (Thread.currentThread() != caller) {
|
||||
throw new ContextNotActiveException();
|
||||
}
|
||||
return requestContext;
|
||||
});
|
||||
SqsService service = new SqsService(
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), requestContextInstance, "000000000000"),
|
||||
null, null, 30, 1048576, BASE_URL, new RegionResolver("us-east-1", account), false, null, clock);
|
||||
String dlqArn = "arn:aws:sqs:us-east-1:" + account + ":acct-dlq";
|
||||
Queue dlq = service.createQueue("acct-dlq", null, "us-east-1");
|
||||
service.createQueue("acct-source", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
Queue replay = service.createQueue("acct-replay", null, "us-east-1");
|
||||
for (String body : List.of("one", "two", "three")) {
|
||||
service.sendMessage(dlq.getQueueUrl(), body, 0, null, null, "us-east-1");
|
||||
}
|
||||
|
||||
String taskHandle = service.startMessageMoveTask(
|
||||
dlqArn, "arn:aws:sqs:us-east-1:" + account + ":acct-replay", 0, "us-east-1");
|
||||
|
||||
awaitMoveTaskStatus(service, dlqArn, taskHandle, "COMPLETED");
|
||||
assertEquals(List.of("one", "two", "three"), bodies(service.peekMessages(replay.getQueueUrl(), "us-east-1")));
|
||||
assertEquals(List.of(), bodies(service.peekMessages(dlq.getQueueUrl(), "us-east-1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_failedDeliveryLeavesSourceQueueOrderIntact() throws Exception {
|
||||
AtomicBoolean destinationStoreDown = new AtomicBoolean();
|
||||
SqsService service = serviceWithMessageStoreFailingFor("fail-replay", destinationStoreDown);
|
||||
Queue dlq = service.createQueue("fail-dlq", null, "us-east-1");
|
||||
String dlqArn = queueArn("fail-dlq");
|
||||
service.createQueue("fail-source", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
service.createQueue("fail-replay", null, "us-east-1");
|
||||
for (String body : List.of("first", "second", "third")) {
|
||||
service.sendMessage(dlq.getQueueUrl(), body, 0, null, null, "us-east-1");
|
||||
}
|
||||
destinationStoreDown.set(true);
|
||||
|
||||
String taskHandle = service.startMessageMoveTask(dlqArn, queueArn("fail-replay"), 0, "us-east-1");
|
||||
|
||||
awaitMoveTaskStatus(service, dlqArn, taskHandle, "COMPLETED");
|
||||
assertEquals(List.of("first", "second", "third"), bodies(service.peekMessages(dlq.getQueueUrl(), "us-east-1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_failedDeliveryRestoresTheSourceMessageUnchanged() throws Exception {
|
||||
AtomicBoolean destinationStoreDown = new AtomicBoolean();
|
||||
SqsService service = serviceWithMessageStoreFailingFor("held-replay", destinationStoreDown);
|
||||
Queue dlq = service.createQueue("held-dlq", null, "us-east-1");
|
||||
String dlqArn = queueArn("held-dlq");
|
||||
service.createQueue("held-source", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
service.createQueue("held-replay", null, "us-east-1");
|
||||
service.sendMessage(dlq.getQueueUrl(), "held", 0, null, null, "us-east-1");
|
||||
service.sendMessage(dlq.getQueueUrl(), "next", 0, null, null, "us-east-1");
|
||||
// A consumer holds the head message when the redrive starts.
|
||||
Message held = service.receiveMessage(dlq.getQueueUrl(), 1, 30, 0, "us-east-1").getFirst();
|
||||
String messageId = held.getMessageId();
|
||||
String receiptHandle = held.getReceiptHandle();
|
||||
Instant firstReceive = held.getFirstReceiveTimestamp();
|
||||
Instant visibleAt = held.getVisibleAt();
|
||||
assertEquals(1, held.getReceiveCount());
|
||||
destinationStoreDown.set(true);
|
||||
|
||||
String taskHandle = service.startMessageMoveTask(dlqArn, queueArn("held-replay"), 0, "us-east-1");
|
||||
|
||||
awaitMoveTaskStatus(service, dlqArn, taskHandle, "COMPLETED");
|
||||
List<Message> dlqMessages = service.peekMessages(dlq.getQueueUrl(), "us-east-1");
|
||||
assertEquals(List.of("held", "next"), bodies(dlqMessages));
|
||||
Message restored = dlqMessages.getFirst();
|
||||
assertEquals(messageId, restored.getMessageId());
|
||||
assertEquals(1, restored.getReceiveCount());
|
||||
assertEquals(firstReceive, restored.getFirstReceiveTimestamp());
|
||||
assertEquals(receiptHandle, restored.getReceiptHandle());
|
||||
assertEquals(visibleAt, restored.getVisibleAt());
|
||||
Map<String, String> attributes = service.getQueueAttributes(dlq.getQueueUrl(),
|
||||
List.of("ApproximateNumberOfMessages", "ApproximateNumberOfMessagesNotVisible"), "us-east-1");
|
||||
assertEquals("1", attributes.get("ApproximateNumberOfMessages"));
|
||||
assertEquals("1", attributes.get("ApproximateNumberOfMessagesNotVisible"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_failedDeliveryLeavesNoCopyInTheDestination() throws Exception {
|
||||
AtomicBoolean destinationStoreDown = new AtomicBoolean();
|
||||
SqsService service = serviceWithMessageStoreFailingFor("dup-replay", destinationStoreDown);
|
||||
Queue dlq = service.createQueue("dup-dlq", null, "us-east-1");
|
||||
String dlqArn = queueArn("dup-dlq");
|
||||
service.createQueue("dup-source", Map.of("RedrivePolicy", redrivePolicy(dlqArn)), "us-east-1");
|
||||
Queue replay = service.createQueue("dup-replay", null, "us-east-1");
|
||||
for (String body : List.of("first", "second", "third")) {
|
||||
service.sendMessage(dlq.getQueueUrl(), body, 0, null, null, "us-east-1");
|
||||
}
|
||||
destinationStoreDown.set(true);
|
||||
|
||||
String taskHandle = service.startMessageMoveTask(dlqArn, queueArn("dup-replay"), 0, "us-east-1");
|
||||
|
||||
awaitMoveTaskStatus(service, dlqArn, taskHandle, "COMPLETED");
|
||||
assertEquals(List.of(), bodies(service.peekMessages(replay.getQueueUrl(), "us-east-1")));
|
||||
assertEquals("0", service.getQueueAttributes(replay.getQueueUrl(),
|
||||
List.of("ApproximateNumberOfMessages"), "us-east-1").get("ApproximateNumberOfMessages"));
|
||||
assertTrue(service.receiveMessage(replay.getQueueUrl(), 10, 0, 0, "us-east-1").isEmpty());
|
||||
assertEquals(List.of("first", "second", "third"), bodies(service.peekMessages(dlq.getQueueUrl(), "us-east-1")));
|
||||
}
|
||||
|
||||
/** A service whose message store rejects writes for {@code queueName} while {@code down} is set. */
|
||||
private SqsService serviceWithMessageStoreFailingFor(String queueName, AtomicBoolean down) {
|
||||
InMemoryStorage<String, List<Message>> messageStore = new InMemoryStorage<>() {
|
||||
@Override
|
||||
public void put(String key, List<Message> value) {
|
||||
if (down.get() && key.endsWith("/" + queueName)) {
|
||||
throw new IllegalStateException("destination store unavailable");
|
||||
}
|
||||
super.put(key, value);
|
||||
}
|
||||
};
|
||||
return new SqsService(new InMemoryStorage<>(), messageStore, null, 30, 1048576, BASE_URL,
|
||||
new RegionResolver("us-east-1", "000000000000"), false, null, clock);
|
||||
}
|
||||
|
||||
private static String redrivePolicy(String dlqArn) {
|
||||
return "{\"deadLetterTargetArn\":\"" + dlqArn + "\",\"maxReceiveCount\":\"1\"}";
|
||||
}
|
||||
|
||||
private static List<String> bodies(List<Message> messages) {
|
||||
return messages.stream().map(Message::getBody).toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void startMessageMoveTask_sourceIsNotDeadLetterQueue_throwsInvalidParameterValue() {
|
||||
sqsService.createQueue("just-a-queue", null, "us-east-1");
|
||||
@@ -1058,8 +1234,13 @@ class SqsServiceTest {
|
||||
}
|
||||
|
||||
private void awaitMoveTaskStatus(String sourceArn, String taskHandle, String expectedStatus) throws Exception {
|
||||
awaitMoveTaskStatus(sqsService, sourceArn, taskHandle, expectedStatus);
|
||||
}
|
||||
|
||||
private static void awaitMoveTaskStatus(SqsService service, String sourceArn, String taskHandle,
|
||||
String expectedStatus) throws Exception {
|
||||
for (int attempt = 0; attempt < 100; attempt++) {
|
||||
boolean reachedStatus = sqsService.listMessageMoveTasks(sourceArn, "us-east-1").stream()
|
||||
boolean reachedStatus = service.listMessageMoveTasks(sourceArn, "us-east-1").stream()
|
||||
.anyMatch(task -> task.taskHandle().equals(taskHandle)
|
||||
&& expectedStatus.equals(task.status()));
|
||||
if (reachedStatus) {
|
||||
|
||||
Reference in new issue
Block a user