Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(sqs): scope FIFO dedup per MessageGroupId when DeduplicationScope=messageGroup (#908)

Stuart Lang337b35c93869
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 9f316744a7..91d18638d4 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
@@ -217,8 +217,15 @@ class GuardedMessageQueue {
}
Message findByDeduplicationId(String dedupId) {
+ return findByDeduplicationId(dedupId, null);
+ }
+
+ Message findByDeduplicationId(String dedupId, String messageGroupId) {
try (var _ = hold()) {
- return messages.stream().filter(msg -> dedupId.equals(msg.getMessageDeduplicationId()))
+ return messages.stream()
+ .filter(msg -> dedupId.equals(msg.getMessageDeduplicationId()))
+ .filter(msg -> messageGroupId == null
+ || messageGroupId.equals(msg.getMessageGroupId()))
.findFirst().orElse(null);
}
}
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 1e79cfad4a..ce615126cf 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
@@ -400,18 +400,27 @@ public class SqsService {
}
}
- // Check deduplication window — atomic putIfAbsent to avoid race condition
+ // Check deduplication window — atomic putIfAbsent to avoid race condition.
+ // When DeduplicationScope=messageGroup, dedup is scoped per MessageGroupId,
+ // so two messages with the same MessageDeduplicationId in different groups
+ // are not duplicates and must both be accepted.
+ boolean groupScoped = "messageGroup".equalsIgnoreCase(
+ queue.getAttributes().get("DeduplicationScope"));
+ // Use NUL as the delimiter — it is outside the SQS-allowed character set for
+ // MessageGroupId/MessageDeduplicationId, so the composite key is unambiguous.
+ String dedupCacheKey = groupScoped ? messageGroupId + "\0" + dedupId : dedupId;
cleanupDeduplicationCache(storageKey);
var dedupMap = deduplicationCache.computeIfAbsent(storageKey, k -> new ConcurrentHashMap<>());
Instant expiry = Instant.now().plusSeconds(DEDUP_WINDOW_SECONDS);
- Instant previous = dedupMap.putIfAbsent(dedupId, expiry);
+ Instant previous = dedupMap.putIfAbsent(dedupCacheKey, expiry);
persistDedup(storageKey);
if (previous != null && Instant.now().isBefore(previous)) {
// Duplicate within window — keep the original messageId and
// sequenceNumber but compute response MD5s from this request's
// body and attributes, otherwise SDK clients (which validate
// MD5 against what they sent) reject the response.
- Message existing = getOrCreateQueue(storageKey).findByDeduplicationId(dedupId);
+ Message existing = getOrCreateQueue(storageKey).findByDeduplicationId(
+ dedupId, groupScoped ? messageGroupId : null);
if (existing != null) {
Message response = new Message(body);
response.setMessageId(existing.getMessageId());
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 684370463e..a8b72b3300 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,44 @@ class SqsServiceTest {
assertEquals("msg3", third.get(0).getBody());
}
+ @Test
+ void fifoDedup_scopedToMessageGroup_acceptsSameDedupIdAcrossGroups() {
+ Queue fifo = sqsService.createQueue("fair.fifo",
+ Map.of("FifoQueue", "true",
+ "DeduplicationScope", "messageGroup",
+ "FifoThroughputLimit", "perMessageGroupId"));
+ sqsService.sendMessage(fifo.getQueueUrl(), "A", 0, "groupA", "sameDedup");
+ sqsService.sendMessage(fifo.getQueueUrl(), "B", 0, "groupB", "sameDedup");
+ List<Message> received = sqsService.receiveMessage(fifo.getQueueUrl(), 10, 30, 0);
+ assertEquals(2, received.size());
+ }
+
+ @Test
+ void fifoDedup_scopedToMessageGroup_dedupKeyHasNoCollisionFromDelimiterInIds() {
+ // AWS allows '|' (and other punctuation) in both MessageGroupId and
+ // MessageDeduplicationId, so a naive "group|dedup" cache key could collide:
+ // (group="a", dedup="b|c") and (group="a|b", dedup="c") both yield "a|b|c".
+ // Both pairs must be treated as distinct messages.
+ Queue fifo = sqsService.createQueue("collision.fifo",
+ Map.of("FifoQueue", "true",
+ "DeduplicationScope", "messageGroup",
+ "FifoThroughputLimit", "perMessageGroupId"));
+ sqsService.sendMessage(fifo.getQueueUrl(), "A", 0, "a", "b|c");
+ sqsService.sendMessage(fifo.getQueueUrl(), "B", 0, "a|b", "c");
+ List<Message> received = sqsService.receiveMessage(fifo.getQueueUrl(), 10, 30, 0);
+ assertEquals(2, received.size());
+ }
+
+ @Test
+ void fifoDedup_queueScope_rejectsSameDedupIdAcrossGroups() {
+ Queue fifo = sqsService.createQueue("queue-scoped.fifo",
+ Map.of("FifoQueue", "true"));
+ sqsService.sendMessage(fifo.getQueueUrl(), "A", 0, "groupA", "sameDedup");
+ sqsService.sendMessage(fifo.getQueueUrl(), "B", 0, "groupB", "sameDedup");
+ List<Message> received = sqsService.receiveMessage(fifo.getQueueUrl(), 10, 30, 0);
+ assertEquals(1, received.size());
+ }
+
@Test
void purgeQueueWithClearFifoDelegatesToSnsForFifoDedupOnSubscribedTopics() {
final var sns = mock(SnsService.class);