mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(sns): retry failed sqs subscription deliveries (#4468)
* fix(sns): retry failed sqs subscription deliveries * fix(sns): support fifo subscription dead letter queues * fix(sns): distinguish shared fifo dlq deliveries --------- Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com>
This commit is contained in:
1 parent
5af1d4048e
commit
f3dde75c0b
6 files changed
+285
-3
No files matched your search
@@ -68,6 +68,9 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SnsService.class);
|
||||
private static final Duration FIFO_DEDUP_WINDOW = Duration.ofMinutes(5);
|
||||
/** Includes the original send and two immediate retries for local SQS fan-out. */
|
||||
private static final int SQS_SUBSCRIPTION_DELIVERY_ATTEMPTS = 3;
|
||||
private static final String SUBSCRIPTION_REDRIVE_POLICY = "RedrivePolicy";
|
||||
/** Default value of the {@code MaximumMessageSize} topic attribute, and the ceiling below
|
||||
* which a topic carries no subscription restrictions. AWS raised the maximum to 1 MiB in
|
||||
* September 2026 but left the default at 256 KiB, so an unconfigured topic is unchanged. */
|
||||
@@ -1730,8 +1733,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
Map<String, MessageAttributeValue> sqsAttributes = rawDelivery
|
||||
? toSqsMessageAttributes(messageAttributes)
|
||||
: Collections.emptyMap();
|
||||
sqsService.sendMessage(queueUrl, body, null, messageGroupId, messageDeduplicationId, sqsAttributes, region);
|
||||
LOG.debugv("Delivered SNS message to SQS: {0} ({1}) raw={2}", sub.getEndpoint(), queueUrl, rawDelivery);
|
||||
deliverToSqsSubscription(sub, queueUrl, body, messageId, messageGroupId,
|
||||
messageDeduplicationId, sqsAttributes, region, rawDelivery);
|
||||
}
|
||||
case "lambda" -> {
|
||||
String region = extractRegionFromArn(sub.getEndpoint());
|
||||
@@ -1846,6 +1849,73 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private void deliverToSqsSubscription(Subscription sub, String queueUrl, String body, String messageId,
|
||||
String messageGroupId, String messageDeduplicationId,
|
||||
Map<String, MessageAttributeValue> sqsAttributes,
|
||||
String region, boolean rawDelivery) {
|
||||
RuntimeException deliveryFailure = null;
|
||||
for (int attempt = 1; attempt <= SQS_SUBSCRIPTION_DELIVERY_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
sqsService.sendMessage(queueUrl, body, null, messageGroupId,
|
||||
messageDeduplicationId, sqsAttributes, region);
|
||||
LOG.debugv("Delivered SNS message to SQS: {0} ({1}) raw={2}",
|
||||
sub.getEndpoint(), queueUrl, rawDelivery);
|
||||
return;
|
||||
} catch (RuntimeException e) {
|
||||
deliveryFailure = e;
|
||||
}
|
||||
}
|
||||
|
||||
String deadLetterTargetArn = subscriptionDeadLetterTargetArn(sub);
|
||||
if (deadLetterTargetArn != null) {
|
||||
try {
|
||||
String deadLetterRegion = extractRegionFromArn(deadLetterTargetArn);
|
||||
if (deadLetterRegion == null) {
|
||||
deadLetterRegion = region;
|
||||
}
|
||||
boolean fifoDeadLetterQueue = AwsArnUtils.parse(deadLetterTargetArn)
|
||||
.resource().endsWith(".fifo");
|
||||
String deadLetterGroupId = messageGroupId;
|
||||
String deadLetterDeduplicationId = messageDeduplicationId;
|
||||
if (fifoDeadLetterQueue) {
|
||||
if (deadLetterGroupId == null || deadLetterGroupId.isBlank()) {
|
||||
deadLetterGroupId = messageId;
|
||||
}
|
||||
// Each failed subscription is a distinct DLQ delivery, even when
|
||||
// several subscriptions share this queue for the same publish.
|
||||
deadLetterDeduplicationId = sha256(messageId + "\0" + sub.getSubscriptionArn());
|
||||
}
|
||||
sqsService.sendMessage(sqsArnToUrl(deadLetterTargetArn), body, null,
|
||||
deadLetterGroupId, deadLetterDeduplicationId, sqsAttributes, deadLetterRegion);
|
||||
LOG.warnv("SNS delivery to {0} failed after {1} attempts; sent notification to subscription DLQ {2}",
|
||||
sub.getEndpoint(), SQS_SUBSCRIPTION_DELIVERY_ATTEMPTS, deadLetterTargetArn);
|
||||
return;
|
||||
} catch (RuntimeException deadLetterFailure) {
|
||||
LOG.warnv("SNS delivery to {0} and subscription DLQ {1} failed: {2}",
|
||||
sub.getEndpoint(), deadLetterTargetArn, deadLetterFailure.getMessage());
|
||||
}
|
||||
}
|
||||
LOG.warnv("SNS delivery to {0} failed after {1} attempts: {2}",
|
||||
sub.getEndpoint(), SQS_SUBSCRIPTION_DELIVERY_ATTEMPTS,
|
||||
deliveryFailure == null ? "unknown failure" : deliveryFailure.getMessage());
|
||||
}
|
||||
|
||||
private String subscriptionDeadLetterTargetArn(Subscription sub) {
|
||||
String redrivePolicy = sub.getAttributes().get(SUBSCRIPTION_REDRIVE_POLICY);
|
||||
if (redrivePolicy == null || redrivePolicy.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JsonNode policy = objectMapper.readTree(redrivePolicy);
|
||||
String targetArn = policy.path("deadLetterTargetArn").asText(null);
|
||||
return AwsArnUtils.isArnFor(targetArn, "sqs") ? targetArn : null;
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Ignoring invalid SNS subscription RedrivePolicy for {0}: {1}",
|
||||
sub.getSubscriptionArn(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String buildSnsLambdaEvent(String topicArn, String messageId, String message,
|
||||
String subject, Map<String, MessageAttributeValue> messageAttributes,
|
||||
String subscriptionArn) {
|
||||
|
||||
@@ -54,8 +54,14 @@ class GuardedMessageQueue {
|
||||
|
||||
void addMessage(Message message) {
|
||||
try (Guard _ = hold()) {
|
||||
int mark = messages.size();
|
||||
messages.add(message);
|
||||
persist();
|
||||
try {
|
||||
persist();
|
||||
} catch (RuntimeException e) {
|
||||
messages.remove(mark);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user