Parallelize message persisters.

This commit is contained in:
Jon Chambers 2021-01-12 11:46:56 -05:00 committed by Jon Chambers
parent ff0bdcd0c2
commit ad30786f4a
1 changed files with 27 additions and 20 deletions

View File

@ -34,7 +34,7 @@ public class MessagePersister implements Managed {
private final Duration persistDelay;
private final Thread workerThread;
private final Thread[] workerThreads = new Thread[WORKER_THREAD_COUNT];
private volatile boolean running;
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
@ -48,6 +48,8 @@ public class MessagePersister implements Managed {
static final int QUEUE_BATCH_LIMIT = 100;
static final int MESSAGE_BATCH_LIMIT = 100;
private static final int WORKER_THREAD_COUNT = 4;
private static final Logger logger = LoggerFactory.getLogger(MessagePersister.class);
public MessagePersister(final MessagesCache messagesCache, final MessagesManager messagesManager, final AccountsManager accountsManager, final Duration persistDelay) {
@ -56,7 +58,8 @@ public class MessagePersister implements Managed {
this.accountsManager = accountsManager;
this.persistDelay = persistDelay;
this.workerThread = new Thread(() -> {
for (int i = 0; i < workerThreads.length; i++) {
workerThreads[i] = new Thread(() -> {
while (running) {
try {
final int queuesPersisted = persistNextQueues(Instant.now());
@ -69,9 +72,8 @@ public class MessagePersister implements Managed {
logger.warn("Failed to persist queues", t);
}
}
}, "MessagePersisterWorker");
metricRegistry.gauge(name(getClass(), "workerThreadRunning"), () -> () -> workerThread.isAlive() ? 1 : 0);
}, "MessagePersisterWorker-" + i);
}
}
@VisibleForTesting
@ -82,19 +84,24 @@ public class MessagePersister implements Managed {
@Override
public void start() {
running = true;
for (final Thread workerThread : workerThreads) {
workerThread.start();
}
}
@Override
public void stop() {
running = false;
for (final Thread workerThread : workerThreads) {
try {
workerThread.join();
} catch (final InterruptedException e) {
logger.warn("Interrupted while waiting for worker thread to complete current operation");
}
}
}
@VisibleForTesting
int persistNextQueues(final Instant currentTime) {