mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(sqs): clear SNS FIFO dedup cache on PurgeQueue (#594)
Clear SNS FIFO topic deduplication for topics with SQS subscriptions targeting a purged queue when `clearFifoDeduplicationCacheOnPurge` is enabled.
This commit is contained in:
1 parent
763ea379d7
commit
5ad4c06d74
7 files changed
+120
-11
No files matched your search
@@ -567,6 +567,54 @@ public class SnsService {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all FIFO deduplication cache entries for SNS topics that have an SQS subscription
|
||||
* whose endpoint resolves to the same queue path as {@code queueUrl} (used when purging SQS
|
||||
* with {@code clearFifoDeduplicationCacheOnPurge}).
|
||||
*/
|
||||
public void clearFifoDeduplicationCacheForSqsQueueSubscriptions(String queueUrl, String region) {
|
||||
String queuePath = extractQueuePathFromUrl(queueUrl);
|
||||
if (queuePath.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String subPrefix = "sub::" + region + "::";
|
||||
subscriptionStore.keys().stream()
|
||||
.filter(key -> key.startsWith(subPrefix))
|
||||
.map(key -> subscriptionStore.get(key).orElse(null))
|
||||
.filter(Objects::nonNull)
|
||||
.filter(sub -> "sqs".equals(sub.getProtocol()))
|
||||
.filter(sub -> sqsSubscriptionEndpointMatchesQueuePath(sub.getEndpoint(), queuePath))
|
||||
.map(Subscription::getTopicArn)
|
||||
.forEach(topicArn ->
|
||||
fifoDeduplicationCache.keySet().removeIf(cacheKey -> cacheKey.startsWith(topicArn + ":")));
|
||||
}
|
||||
|
||||
private boolean sqsSubscriptionEndpointMatchesQueuePath(String endpoint, String queuePath) {
|
||||
if (endpoint == null) {
|
||||
return false;
|
||||
}
|
||||
String asUrl = sqsArnToUrl(endpoint);
|
||||
return extractQueuePathFromUrl(asUrl).equals(queuePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same path extraction as {@code SqsService} queue URL normalization ({@code /accountId/queueName}).
|
||||
*/
|
||||
private static String extractQueuePathFromUrl(String url) {
|
||||
if (url == null) {
|
||||
return "";
|
||||
}
|
||||
int schemeEnd = url.indexOf("://");
|
||||
if (schemeEnd < 0) {
|
||||
return url;
|
||||
}
|
||||
int pathStart = url.indexOf('/', schemeEnd + 3);
|
||||
if (pathStart < 0) {
|
||||
return url;
|
||||
}
|
||||
return url.substring(pathStart);
|
||||
}
|
||||
|
||||
private List<Subscription> subscriptionsByTopic(String topicArn, String region) {
|
||||
List<Subscription> result = new ArrayList<>();
|
||||
String prefix = "sub::" + region + "::";
|
||||
@@ -739,4 +787,4 @@ public class SnsService {
|
||||
private static String subKey(String region, String subscriptionArn) {
|
||||
return "sub::" + region + "::" + subscriptionArn;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import io.github.hectorvent.floci.services.sqs.model.Queue;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -41,9 +42,11 @@ public class SqsService {
|
||||
private final String baseUrl;
|
||||
private final RegionResolver regionResolver;
|
||||
private final boolean clearFifoDeduplicationCacheOnPurge;
|
||||
private final SnsService snsService;
|
||||
|
||||
@Inject
|
||||
public SqsService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver) {
|
||||
public SqsService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver,
|
||||
SnsService snsService) {
|
||||
this(
|
||||
storageFactory.create("sqs", "sqs-queues.json",
|
||||
new TypeReference<Map<String, Queue>>() {
|
||||
@@ -58,7 +61,8 @@ public class SqsService {
|
||||
config.services().sqs().maxMessageSize(),
|
||||
config.effectiveBaseUrl(),
|
||||
regionResolver,
|
||||
config.services().sqs().clearFifoDeduplicationCacheOnPurge()
|
||||
config.services().sqs().clearFifoDeduplicationCacheOnPurge(),
|
||||
snsService
|
||||
);
|
||||
}
|
||||
|
||||
@@ -68,7 +72,7 @@ public class SqsService {
|
||||
SqsService(StorageBackend<String, Queue> queueStore,
|
||||
int defaultVisibilityTimeout, int maxMessageSize, String baseUrl) {
|
||||
this(queueStore, null, null, defaultVisibilityTimeout, maxMessageSize, baseUrl,
|
||||
new RegionResolver("us-east-1", "000000000000"), false);
|
||||
new RegionResolver("us-east-1", "000000000000"), false, null);
|
||||
}
|
||||
|
||||
SqsService(StorageBackend<String, Queue> queueStore, StorageBackend<String, List<Message>> messageStore,
|
||||
@@ -76,13 +80,14 @@ public class SqsService {
|
||||
int defaultVisibilityTimeout, int maxMessageSize, String baseUrl,
|
||||
RegionResolver regionResolver) {
|
||||
this(queueStore, messageStore, dedupStore, defaultVisibilityTimeout, maxMessageSize, baseUrl,
|
||||
regionResolver, false);
|
||||
regionResolver, false, null);
|
||||
}
|
||||
|
||||
SqsService(StorageBackend<String, Queue> queueStore, StorageBackend<String, List<Message>> messageStore,
|
||||
StorageBackend<String, Map<String, Long>> dedupStore,
|
||||
int defaultVisibilityTimeout, int maxMessageSize, String baseUrl,
|
||||
RegionResolver regionResolver, boolean clearFifoDeduplicationCacheOnPurge) {
|
||||
RegionResolver regionResolver, boolean clearFifoDeduplicationCacheOnPurge,
|
||||
SnsService snsService) {
|
||||
this.queueStore = queueStore;
|
||||
this.messageStore = messageStore;
|
||||
this.dedupStore = dedupStore;
|
||||
@@ -91,6 +96,7 @@ public class SqsService {
|
||||
this.baseUrl = baseUrl;
|
||||
this.regionResolver = regionResolver;
|
||||
this.clearFifoDeduplicationCacheOnPurge = clearFifoDeduplicationCacheOnPurge;
|
||||
this.snsService = snsService;
|
||||
loadPersistedMessages();
|
||||
loadPersistedDedup();
|
||||
}
|
||||
@@ -618,6 +624,9 @@ public class SqsService {
|
||||
if (dedupStore != null) {
|
||||
dedupStore.delete(storageKey);
|
||||
}
|
||||
if (snsService != null) {
|
||||
snsService.clearFifoDeduplicationCacheForSqsQueueSubscriptions(queueUrl, region);
|
||||
}
|
||||
}
|
||||
LOG.infov("Purged queue{0}: {1}",
|
||||
clearFifoDeduplicationCacheOnPurge ? " (dedup cache cleared)" : "", queueUrl);
|
||||
|
||||
Reference in new issue
Block a user