mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(sns): honor FifoThroughputScope=MessageGroup on FIFO topics (#3181)
* fix(sns): honor FifoThroughputScope=MessageGroup on FIFO topics A FIFO topic accepted FifoThroughputScope and echoed it back from GetTopicAttributes, but deduplication stayed topic-wide, so the same MessageDeduplicationId published under two different MessageGroupIds delivered only the first message. A fan-out that reuses one dedup id per group silently lost every message after the first. Scope the deduplication cache key to the message group when the topic sets FifoThroughputScope=MessageGroup, on both Publish and PublishBatch, mirroring how SQS handles DeduplicationScope=messageGroup. Topic remains the default and keeps the existing topic-wide behavior. * fix(cloudformation): forward SNS FifoThroughputScope from AWS::SNS::Topic SnsCfnProvisioner only forwarded ContentBasedDeduplication, so a topic provisioned from a template kept the default topic-wide deduplication even with FifoThroughputScope set, and a FIFO topic left unnamed got a generated name without the .fifo suffix SnsService recognizes. Forward FifoThroughputScope, suffix a generated FIFO topic name with .fifo as CloudFormation does, treat a prior name whose suffix no longer matches the createOnly FifoTopic flag as a replacing update, and rewrite the mutable attributes on UpdateStack, which createTopic leaves untouched when the topic already exists. * fix(cloudformation): reset dropped SNS topic attributes on update An UpdateStack that dropped FifoThroughputScope or ContentBasedDeduplication kept the old value while reporting success. Write the create-time default instead, for FIFO topics only: real SNS rejects both on a standard topic. A FifoTopic flip now fails rather than replacing the topic under a new generated name, which would orphan it and its subscriptions. An explicitly named .fifo topic counts as FIFO without the flag. * fix(sns): make the group-scoped FIFO dedup key unambiguous Nothing validates the characters in either id, so the NUL delimiter could arrive inside one and make two different group and dedup pairs share a cache key. Length-prefix the group instead. * fix(sns): keep FIFO dedup scopes in separate cache namespaces A topic-scoped dedup id shaped like the group-scoped key, "1#ab" for group "a" with id "b", hit the same cache entry once the scope changed inside the deduplication window. Mark each key with its scope so the two formats cannot collide.
This commit is contained in:
1 parent
0cff3156ec
commit
a1d0597ffe
6 files changed
+570
-9
No files matched your search
+38
-4
@@ -20,6 +20,7 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
|
||||
private static final String SUBSCRIPTION = "AWS::SNS::Subscription";
|
||||
private static final String TOPIC_POLICY = "AWS::SNS::TopicPolicy";
|
||||
private static final int TOPIC_NAME_MAX_LENGTH = 256;
|
||||
private static final String FIFO_SUFFIX = ".fifo";
|
||||
|
||||
private final SnsService snsService;
|
||||
|
||||
@@ -46,22 +47,55 @@ public class SnsCfnProvisioner implements CfnResourceProvisioner {
|
||||
private void provisionTopic(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String topicName = ctx.resolveOptional(props, "TopicName");
|
||||
String contentBasedDedupFlag = ctx.resolveOptional(props, "ContentBasedDeduplication");
|
||||
String throughputScope = ctx.resolveOptional(props, "FifoThroughputScope");
|
||||
// The .fifo suffix is what makes SnsService treat a topic as FIFO, so an explicitly named
|
||||
// one is FIFO with or without the flag, which only has to steer a generated name.
|
||||
boolean fifo = "true".equalsIgnoreCase(ctx.resolveOptional(props, "FifoTopic"))
|
||||
|| (topicName != null && topicName.endsWith(FIFO_SUFFIX));
|
||||
String priorName = r.getAttributes().get("TopicName");
|
||||
// FifoTopic is createOnly and carried by the name, so a flip needs a replacement. Nothing
|
||||
// cleans up the displaced topic or its subscriptions, so refuse it, as SqsCfnProvisioner
|
||||
// does for FifoQueue.
|
||||
if (ctx.isUpdate() && priorName != null && !priorName.isBlank()
|
||||
&& priorName.endsWith(FIFO_SUFFIX) != fifo) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Updating FifoTopic requires resource replacement, which is not supported.", 400);
|
||||
}
|
||||
if (topicName == null || topicName.isBlank()) {
|
||||
// ctx.stablePhysicalName does not fit here: the physical id is the topic ARN, so the
|
||||
// prior name comes from the attribute recorded at create time. Reusing it keeps an
|
||||
// unnamed topic (and its subscriptions) across updates instead of orphaning it.
|
||||
String priorName = r.getAttributes().get("TopicName");
|
||||
topicName = priorName != null && !priorName.isBlank()
|
||||
? priorName
|
||||
: ctx.generatePhysicalName(r.getLogicalId(), TOPIC_NAME_MAX_LENGTH, false);
|
||||
if (priorName != null && !priorName.isBlank()) {
|
||||
topicName = priorName;
|
||||
} else if (fifo) {
|
||||
// Like real CloudFormation, a generated FIFO name ends in .fifo.
|
||||
topicName = ctx.generatePhysicalName(r.getLogicalId(),
|
||||
TOPIC_NAME_MAX_LENGTH - FIFO_SUFFIX.length(), false) + FIFO_SUFFIX;
|
||||
} else {
|
||||
topicName = ctx.generatePhysicalName(r.getLogicalId(), TOPIC_NAME_MAX_LENGTH, false);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, String> attributes = new HashMap<>();
|
||||
if (contentBasedDedupFlag != null && !contentBasedDedupFlag.isBlank()) {
|
||||
attributes.put("ContentBasedDeduplication", contentBasedDedupFlag);
|
||||
}
|
||||
if (throughputScope != null && !throughputScope.isBlank()) {
|
||||
attributes.put("FifoThroughputScope", throughputScope);
|
||||
}
|
||||
|
||||
var topic = snsService.createTopic(topicName, attributes, Map.of(), ctx.region());
|
||||
// createTopic leaves an existing topic untouched, so an update writes the mutable
|
||||
// attributes itself. A property the template dropped is reset, not left standing.
|
||||
if (ctx.isUpdate()) {
|
||||
Map<String, String> desired = new HashMap<>(attributes);
|
||||
if (fifo) {
|
||||
desired.putIfAbsent("ContentBasedDeduplication", "false");
|
||||
desired.putIfAbsent("FifoThroughputScope", "Topic");
|
||||
}
|
||||
desired.forEach((name, value) ->
|
||||
snsService.setTopicAttributes(topic.getTopicArn(), name, value, ctx.region()));
|
||||
}
|
||||
// Ref returns the topic ARN, which is why the physical id is the ARN and not the name.
|
||||
r.setPhysicalId(topic.getTopicArn());
|
||||
// TopicArn is the attribute aws-sns-topic.json declares read-only. Arn is kept alongside it
|
||||
|
||||
@@ -435,7 +435,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
if (dedupId == null && "true".equals(topic.getAttributes().get("ContentBasedDeduplication"))) {
|
||||
dedupId = sha256(message);
|
||||
}
|
||||
if (dedupId != null && isDuplicate(effectiveArn, dedupId)) {
|
||||
if (dedupId != null && isDuplicate(effectiveArn, messageGroupId, dedupId,
|
||||
isGroupScopedDeduplication(topic))) {
|
||||
LOG.debugv("FIFO dedup: skipping duplicate for topic {0}, dedupId {1}", effectiveArn, dedupId);
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
@@ -841,6 +842,7 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
boolean isFifo = "true".equals(topic.getAttributes().get("FifoTopic"));
|
||||
boolean groupScopedDedup = isGroupScopedDeduplication(topic);
|
||||
List<String[]> successful = new ArrayList<>();
|
||||
List<String[]> failed = new ArrayList<>();
|
||||
for (Map<String, Object> entry : entries) {
|
||||
@@ -871,7 +873,8 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
if (isFifo && messageDeduplicationId == null && "true".equals(topic.getAttributes().get("ContentBasedDeduplication"))) {
|
||||
messageDeduplicationId = sha256(message);
|
||||
}
|
||||
if (isFifo && messageDeduplicationId != null && isDuplicate(topicArn, messageDeduplicationId)) {
|
||||
if (isFifo && messageDeduplicationId != null && isDuplicate(topicArn, messageGroupId,
|
||||
messageDeduplicationId, groupScopedDedup)) {
|
||||
successful.add(new String[]{id, UUID.randomUUID().toString()});
|
||||
continue;
|
||||
}
|
||||
@@ -1252,8 +1255,23 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isDuplicate(String topicArn, String deduplicationId) {
|
||||
String cacheKey = topicArn + ":" + deduplicationId;
|
||||
/**
|
||||
* {@code MessageGroup} narrows deduplication to a single message group. {@code Topic}, the AWS
|
||||
* default, keeps it topic-wide.
|
||||
*/
|
||||
private static boolean isGroupScopedDeduplication(Topic topic) {
|
||||
return "MessageGroup".equalsIgnoreCase(topic.getAttributes().get("FifoThroughputScope"));
|
||||
}
|
||||
|
||||
private boolean isDuplicate(String topicArn, String messageGroupId, String deduplicationId,
|
||||
boolean groupScoped) {
|
||||
// The scope is part of the key: the attribute can change inside the deduplication window,
|
||||
// and a topic-scoped id must never land on a group-scoped entry. The group is
|
||||
// length-prefixed because nothing validates the characters in either id.
|
||||
String scopedId = groupScoped
|
||||
? "group:" + messageGroupId.length() + ":" + messageGroupId + deduplicationId
|
||||
: "topic:" + deduplicationId;
|
||||
String cacheKey = topicArn + ":" + scopedId;
|
||||
Instant now = Instant.now();
|
||||
Instant existing = fifoDeduplicationCache.get(cacheKey);
|
||||
if (existing != null && existing.plus(FIFO_DEDUP_WINDOW).isAfter(now)) {
|
||||
|
||||
Reference in new issue
Block a user