Add scan COUNT arguments and rate limits for queue scans

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-03-13 18:20:51 -04:00
1 parent b55afdd704
commit b4c594c657
5 files changed
+22 -6

No files matched your search

@@ -53,6 +53,7 @@ public class MessagePersister implements Managed {
private final Duration persistDelay;
private final int maxConcurrency;
private final int scanCount;
private final RetryBackoffSpec retryBackoffSpec;
@@ -98,7 +99,8 @@ public class MessagePersister implements Managed {
final Scheduler persistQueueScheduler,
final Clock clock,
final Duration persistDelay,
final int maxConcurrency) {
final int maxConcurrency,
final int scanCount) {
this(messagesCache,
messagesManager,
@@ -108,6 +110,7 @@ public class MessagePersister implements Managed {
clock,
persistDelay,
maxConcurrency,
scanCount,
Retry.backoff(3, Duration.ofSeconds(1)));
}
@@ -120,6 +123,7 @@ public class MessagePersister implements Managed {
final Clock clock,
final Duration persistDelay,
final int maxConcurrency,
final int scanCount,
final RetryBackoffSpec retryBackoffSpec) {
this.messagesCache = messagesCache;
@@ -129,6 +133,7 @@ public class MessagePersister implements Managed {
this.clock = clock;
this.persistDelay = persistDelay;
this.maxConcurrency = maxConcurrency;
this.scanCount = scanCount;
this.retryBackoffSpec = retryBackoffSpec;
this.persistQueueScheduler = persistQueueScheduler;
}
@@ -204,7 +209,7 @@ public class MessagePersister implements Managed {
try {
final Tags tags = Tags.of("node", node.getUri().getHost());
return messagesCache.getQueues(node)
return messagesCache.getQueues(node, scanCount)
.filter(_ -> dynamicConfigurationManager.getConfiguration().getMessagePersisterConfiguration()
.isPersistenceEnabled())
.flatMap(queueKey -> Mono.defer(() -> shouldPersistQueue(queueKey))
@@ -712,10 +712,10 @@ public class MessagesCache {
return "persister_node_claim::" + node.getNodeId();
}
Flux<String> getQueues(final RedisClusterNode node) {
Flux<String> getQueues(final RedisClusterNode node, final int scanCount) {
return redisCluster.withCluster(connection ->
ScanStream.scan(connection.getConnection(node.getNodeId()).reactive(),
ScanArgs.Builder.matches("user_queue::*")));
ScanArgs.Builder.matches("user_queue::*").limit(scanCount)));
}
void lockQueueForPersistence(final UUID accountUuid, final byte deviceId) {
@@ -36,6 +36,7 @@ public class MessagePersisterServiceCommand extends ServerCommand<WhisperServerC
private Scheduler persistQueueScheduler;
private static final String MAX_CONCURRENCY = "maxConcurrency";
private static final String SCAN_COUNT = "scanCount";
private static final Logger logger = LoggerFactory.getLogger(MessagePersisterServiceCommand.class);
@@ -59,6 +60,13 @@ public class MessagePersisterServiceCommand extends ServerCommand<WhisperServerC
.dest(MAX_CONCURRENCY)
.required(true)
.help("The maximum number of concurrent Redis/DynamoDB operations");
subparser.addArgument("--scan-count")
.type(Integer.class)
.dest(SCAN_COUNT)
.required(false)
.setDefault(1024)
.help("The COUNT argument for the Redis SCAN operation that finds message queues");
}
@Override
@@ -90,7 +98,8 @@ public class MessagePersisterServiceCommand extends ServerCommand<WhisperServerC
persistQueueScheduler,
Clock.systemUTC(),
Duration.ofMinutes(configuration.getMessageCacheConfiguration().getPersistDelayMinutes()),
namespace.getInt(MAX_CONCURRENCY));
namespace.getInt(MAX_CONCURRENCY),
namespace.getInt(SCAN_COUNT));
environment.lifecycle().manage(messagePersister);
@@ -107,7 +107,8 @@ class MessagePersisterIntegrationTest {
persistQueueScheduler,
Clock.systemUTC(),
PERSIST_DELAY,
1);
1,
1024);
account = mock(Account.class);
@@ -140,6 +140,7 @@ class MessagePersisterTest {
CLOCK,
PERSIST_DELAY,
1,
1024,
Retry.backoff(1, Duration.ZERO));
when(messagesManager.clear(any(UUID.class), anyByte())).thenReturn(CompletableFuture.completedFuture(null));