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