perf(sns): find a topic's subscriptions without copying every subscription key (#7631)

* perf(sns): find a topic's subscriptions without copying every subscription key

* perf(sns): match a topic's subscription keys without copying each one
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-05 15:42:48 -04:00
1 parent d34c8443cd
commit 0c33083803
1 file changed
+16 -21
@@ -609,16 +609,9 @@ public class SnsService implements Resettable, ResourceProvider {
if (topicStore.get(key).isEmpty()) {
throw new AwsException("NotFound", "Topic does not exist.", 404);
}
List<String> toDelete = new ArrayList<>();
String subPrefix = "sub::" + region + "::";
for (String subKey : subscriptionStore.keys()) {
if (subKey.startsWith(subPrefix)) {
subscriptionStore.get(subKey).ifPresent(sub -> {
if (topicArn.equals(sub.getTopicArn())) toDelete.add(subKey);
});
}
for (Subscription sub : subscriptionsByTopic(topicArn, region)) {
subscriptionStore.delete(subKey(region, sub.getSubscriptionArn()));
}
toDelete.forEach(subscriptionStore::delete);
topicStore.delete(key);
LOG.infov("Deleted SNS topic: {0}", topicArn);
}
@@ -1015,15 +1008,11 @@ public class SnsService implements Resettable, ResourceProvider {
if (topicStore.get(topicKey(region, topicArn)).isEmpty()) {
throw new AwsException("NotFound", "Topic does not exist.", 404);
}
String subPrefix = "sub::" + region + "::";
for (String subKey : subscriptionStore.keys()) {
if (!subKey.startsWith(subPrefix)) continue;
Subscription sub = subscriptionStore.get(subKey).orElse(null);
if (sub == null || !topicArn.equals(sub.getTopicArn())) continue;
for (Subscription sub : subscriptionsByTopic(topicArn, region)) {
if (token.equals(sub.getAttributes().get("ConfirmationToken"))) {
sub.getAttributes().put("PendingConfirmation", "false");
sub.getAttributes().remove("ConfirmationToken");
subscriptionStore.put(subKey, sub);
subscriptionStore.put(subKey(region, sub.getSubscriptionArn()), sub);
LOG.infov("Confirmed subscription {0} for topic {1}", sub.getSubscriptionArn(), topicArn);
return sub.getSubscriptionArn();
}
@@ -1569,6 +1558,7 @@ public class SnsService implements Resettable, ResourceProvider {
List<String[]> failed = new ArrayList<>();
String timestamp = MESSAGE_TIMESTAMP.format(Instant.now());
String version = signatureVersion(topic);
List<Subscription> subscriptions = subscriptionsByTopic(topicArn, region);
for (Map<String, Object> entry : entries) {
String id = (String) entry.get("Id");
String message = (String) entry.get("Message");
@@ -1610,7 +1600,7 @@ public class SnsService implements Resettable, ResourceProvider {
Map<String, MessageAttributeValue> attrs = (Map<String, MessageAttributeValue>) entry.get("MessageAttributes");
JsonNode parsedBody = null;
boolean bodyParseAttempted = false;
for (Subscription sub : subscriptionsByTopic(topicArn, region)) {
for (Subscription sub : subscriptions) {
if ("true".equals(sub.getAttributes().get("PendingConfirmation"))) continue;
if (!bodyParseAttempted && isMessageBodyScope(sub)) {
parsedBody = tryParseBody(message);
@@ -2488,11 +2478,16 @@ public class SnsService implements Resettable, ResourceProvider {
private List<Subscription> subscriptionsByTopic(String topicArn, String region) {
List<Subscription> result = new ArrayList<>();
String prefix = "sub::" + region + "::";
for (String k : subscriptionStore.keys()) {
if (k.startsWith(prefix)) {
subscriptionStore.get(k).ifPresent(sub -> {
if (topicArn.equals(sub.getTopicArn())) result.add(sub);
});
List<Subscription> regional;
if (subscriptionStore instanceof AccountAwareStorageBackend<Subscription> aware) {
String rawPrefix = AccountAwareStorageBackend.rawKey(aware.accountId(), prefix);
regional = aware.scanRaw(raw -> raw.startsWith(rawPrefix));
} else {
regional = subscriptionStore.scan(k -> k.startsWith(prefix));
}
for (Subscription sub : regional) {
if (topicArn.equals(sub.getTopicArn())) {
result.add(sub);
}
}
return result;