mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -53,10 +54,18 @@ class GuardedMessageQueue {
|
||||
}
|
||||
}
|
||||
|
||||
/** If persisting fails the in-memory add is rolled back and the exception propagates, so a
|
||||
* caller that compensates on the source side does not leave a duplicate here. */
|
||||
void addAll(List<Message> toAdd) {
|
||||
try (var _ = hold()) {
|
||||
int mark = messages.size();
|
||||
messages.addAll(toAdd);
|
||||
persist();
|
||||
try {
|
||||
persist();
|
||||
} catch (RuntimeException e) {
|
||||
messages.subList(mark, messages.size()).clear();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,20 +203,29 @@ 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() {
|
||||
/** Remove and return the head message if {@code eligible} accepts it; otherwise leave the
|
||||
* queue untouched and return {@code null}. The check and the removal happen under one lock
|
||||
* hold so nothing can slip in between. Used by the message-move-task worker so the source
|
||||
* queue stays observably populated for the duration of a rate-limited move. */
|
||||
Message drainFirstIf(Predicate<Message> eligible) {
|
||||
try (var _ = hold()) {
|
||||
if (messages.isEmpty()) {
|
||||
if (messages.isEmpty() || !eligible.test(messages.getFirst())) {
|
||||
return null;
|
||||
}
|
||||
Message head = messages.remove(0);
|
||||
Message head = messages.removeFirst();
|
||||
persist();
|
||||
return head;
|
||||
}
|
||||
}
|
||||
|
||||
/** Put a message that could not be delivered back at the head, preserving queue order. */
|
||||
void restoreFirst(Message message) {
|
||||
try (var _ = hold()) {
|
||||
messages.addFirst(message);
|
||||
persist();
|
||||
}
|
||||
}
|
||||
|
||||
record MessageCounts(long visible, long inFlight, long delayed) {
|
||||
}
|
||||
|
||||
|
||||
@@ -1042,8 +1042,7 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
// 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)) {
|
||||
if (moveOneMessage(srcQueueInitial, destUrl, region)) {
|
||||
initialMoved = 1;
|
||||
updateMoveTaskCounter(taskHandle, initialMoved);
|
||||
}
|
||||
@@ -1059,31 +1058,55 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
|
||||
/** 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. */
|
||||
* recorded when it was DLQ'd). Delivers a copy whose per-receive state starts
|
||||
* a fresh life; {@code msg} itself is left untouched so that, if delivery
|
||||
* fails, the caller can restore it to the source exactly as it was.
|
||||
* 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);
|
||||
Message moved = msg.copyForRedrive();
|
||||
if (destUrl != null) {
|
||||
String destKey = regionKey(region, destUrl);
|
||||
getOrCreateQueue(destKey).addAll(List.of(msg));
|
||||
getOrCreateQueue(destKey).addAll(List.of(moved));
|
||||
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
|
||||
// canDeliverMovedMessage() already confirmed the original source queue exists.
|
||||
// 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));
|
||||
getOrCreateQueue(originKey).addAll(List.of(moved));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The move worker runs outside any request scope, so the queue lookup must take the
|
||||
* account from the target URL instead of the (absent) request context. */
|
||||
private boolean canDeliverMovedMessage(Message msg, String destUrl, String region) {
|
||||
String targetUrl = destUrl != null ? destUrl : msg.getOriginalSourceQueueUrl();
|
||||
return targetUrl != null && getQueueByUrl(regionKey(region, targetUrl), targetUrl).isPresent();
|
||||
}
|
||||
|
||||
/** Move the head message of {@code sourceQueue}. Returns {@code false} when there is
|
||||
* nothing to move, the target queue is missing, or delivery failed; in the latter case
|
||||
* the message is put back at the head so the source keeps its order. */
|
||||
private boolean moveOneMessage(GuardedMessageQueue sourceQueue, String destUrl, String region) {
|
||||
Message message = sourceQueue.drainFirstIf(msg -> canDeliverMovedMessage(msg, destUrl, region));
|
||||
if (message == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if (deliverMovedMessage(message, destUrl, region)) {
|
||||
return true;
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Failed to move a message to {0}", destUrl);
|
||||
}
|
||||
sourceQueue.restoreFirst(message);
|
||||
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
|
||||
@@ -1110,14 +1133,11 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Message msg = srcQueue.drainOne();
|
||||
if (msg == null) {
|
||||
if (!moveOneMessage(srcQueue, destUrl, region)) {
|
||||
break;
|
||||
}
|
||||
if (deliverMovedMessage(msg, destUrl, region)) {
|
||||
moved++;
|
||||
updateMoveTaskCounter(taskHandle, moved);
|
||||
}
|
||||
moved++;
|
||||
updateMoveTaskCounter(taskHandle, moved);
|
||||
}
|
||||
} finally {
|
||||
MoveTask cur = moveTasksByHandle.get(taskHandle);
|
||||
|
||||
@@ -57,6 +57,26 @@ public class Message {
|
||||
this.md5OfBody = computeMd5(body);
|
||||
}
|
||||
|
||||
/** A copy for delivery to another queue by StartMessageMoveTask: identity and content carry
|
||||
* over; per-receive state (receive count, first-receive timestamp, receipt handle, visibility)
|
||||
* starts over. The source instance is untouched so a failed delivery can put it back as it was. */
|
||||
public Message copyForRedrive() {
|
||||
Message copy = new Message();
|
||||
copy.messageId = messageId;
|
||||
copy.body = body;
|
||||
copy.messageAttributes = messageAttributes == null ? new HashMap<>() : new HashMap<>(messageAttributes);
|
||||
copy.sentTimestamp = sentTimestamp;
|
||||
copy.md5OfBody = md5OfBody;
|
||||
copy.md5OfMessageAttributes = md5OfMessageAttributes;
|
||||
copy.messageGroupId = messageGroupId;
|
||||
copy.messageDeduplicationId = messageDeduplicationId;
|
||||
copy.sequenceNumber = sequenceNumber;
|
||||
copy.originalSourceQueueUrl = originalSourceQueueUrl;
|
||||
copy.awsTraceHeader = awsTraceHeader;
|
||||
// receiveCount 0, firstReceiveTimestamp / receiptHandle / visibleAt null: a fresh life.
|
||||
return copy;
|
||||
}
|
||||
|
||||
public String getMessageId() { return messageId; }
|
||||
public void setMessageId(String messageId) { this.messageId = messageId; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user