fix(sns): pass messageDeduplicationId from FIFO topics to SQS FIFO queues (#171)

* fix(sns): pass messageDeduplicationId from FIFO topics to SQS FIFO queues

   SnsService.deliverMessage was passing null as the messageDeduplicationId
   to sqsService.sendMessage when delivering to FIFO SQS subscriptions.
   This caused the SQS service to throw an InvalidParameterValue exception
   for queues without ContentBasedDeduplication enabled. The exception was
   silently swallowed by the catch block, resulting in silent message loss.

   Thread the resolved deduplication ID through deliverMessage for both
   publish() and publishBatch() call sites.

* fix(sns): resolve ContentBasedDeduplication in publishBatch and add FIFO dedup tests
This commit is contained in:
Benedikt Schneppe authored and GitHub committed 2026-04-02 18:14:32 -05:00
1 parent fb42087631
commit 4529823557
3 files changed
+196 -11

No files matched your search

@@ -274,12 +274,12 @@ public class SnsService {
}
boolean isFifo = "true".equals(topic.getAttributes().get("FifoTopic"));
String dedupId = messageDeduplicationId;
if (isFifo) {
if (messageGroupId == null || messageGroupId.isBlank()) {
throw new AwsException("InvalidParameter",
"MessageGroupId is required for FIFO topics.", 400);
}
String dedupId = messageDeduplicationId;
if (dedupId == null && "true".equals(topic.getAttributes().get("ContentBasedDeduplication"))) {
dedupId = sha256(message);
}
@@ -298,7 +298,7 @@ public class SnsService {
if (!matchesFilterPolicy(sub, messageAttributes)) {
continue;
}
deliverMessage(sub, message, subject, messageAttributes, messageId, effectiveArn, messageGroupId);
deliverMessage(sub, message, subject, messageAttributes, messageId, effectiveArn, messageGroupId, dedupId);
}
LOG.infov("Published message {0} to topic {1}", messageId, effectiveArn);
return messageId;
@@ -331,7 +331,8 @@ public class SnsService {
subscriptionStore.put(key, sub);
}
public record BatchPublishResult(List<String[]> successful, List<String[]> failed) {}
public record BatchPublishResult(List<String[]> successful, List<String[]> failed) {
}
public BatchPublishResult publishBatch(String topicArn, List<Map<String, Object>> entries, String region) {
String topicStoreKey = topicKey(region, topicArn);
@@ -356,6 +357,10 @@ public class SnsService {
"MessageGroupId is required for FIFO topics.", "true"});
continue;
}
// Derive deduplication ID if ContentBasedDeduplication is enabled and not provided
if (isFifo && messageDeduplicationId == null && "true".equals(topic.getAttributes().get("ContentBasedDeduplication"))) {
messageDeduplicationId = sha256(message);
}
if (isFifo && messageDeduplicationId != null && isDuplicate(topicArn, messageDeduplicationId)) {
successful.add(new String[]{id, UUID.randomUUID().toString()});
continue;
@@ -367,7 +372,7 @@ public class SnsService {
for (Subscription sub : subscriptionsByTopic(topicArn, region)) {
if ("true".equals(sub.getAttributes().get("PendingConfirmation"))) continue;
if (!matchesFilterPolicy(sub, attrs)) continue;
deliverMessage(sub, message, subject, attrs, messageId, topicArn, messageGroupId);
deliverMessage(sub, message, subject, attrs, messageId, topicArn, messageGroupId, messageDeduplicationId);
}
LOG.debugv("Batch published message {0} (id={1}) to {2}", messageId, id, topicArn);
successful.add(new String[]{id, messageId});
@@ -563,7 +568,7 @@ public class SnsService {
private void deliverMessage(Subscription sub, String message, String subject,
Map<String, MessageAttributeValue> messageAttributes, String messageId,
String topicArn, String messageGroupId) {
String topicArn, String messageGroupId, String messageDeduplicationId) {
try {
switch (sub.getProtocol()) {
case "sqs" -> {
@@ -579,7 +584,7 @@ public class SnsService {
Map<String, MessageAttributeValue> sqsAttributes = rawDelivery
? toSqsMessageAttributes(messageAttributes)
: Collections.emptyMap();
sqsService.sendMessage(queueUrl, body, 0, messageGroupId, null, sqsAttributes, region);
sqsService.sendMessage(queueUrl, body, 0, messageGroupId, messageDeduplicationId, sqsAttributes, region);
LOG.debugv("Delivered SNS message to SQS: {0} ({1}) raw={2}", sub.getEndpoint(), queueUrl, rawDelivery);
}
case "lambda" -> {
@@ -590,11 +595,9 @@ public class SnsService {
lambdaService.invoke(region, fnName, eventJson.getBytes(), InvocationType.Event);
LOG.debugv("Delivered SNS message to Lambda: {0}", sub.getEndpoint());
}
case "email", "email-json" ->
LOG.infov("SNS email delivery (stub): to={0}, subject={1}, message={2}",
sub.getEndpoint(), subject, message);
case "sms" ->
LOG.infov("SNS SMS delivery (stub): to={0}, message={1}", sub.getEndpoint(), message);
case "email", "email-json" -> LOG.infov("SNS email delivery (stub): to={0}, subject={1}, message={2}",
sub.getEndpoint(), subject, message);
case "sms" -> LOG.infov("SNS SMS delivery (stub): to={0}, message={1}", sub.getEndpoint(), message);
default -> LOG.debugv("Protocol {0} delivery not implemented, skipping: {1}",
sub.getProtocol(), sub.getEndpoint());
}