undo executorservice close

use-21-features
Mike Barry 2023-10-27 08:01:07 -04:00
rodzic dccbf78879
commit 7487205d69
2 zmienionych plików z 26 dodań i 26 usunięć

Wyświetl plik

@ -213,7 +213,7 @@ class PrometheusStats implements Stats {
@Override @Override
public void close() { public void close() {
executor.close(); executor.shutdown();
push(); push();
} }

Wyświetl plik

@ -54,33 +54,33 @@ public class Worker {
public Worker(String prefix, Stats stats, int threads, IntConsumerThatThrows task) { public Worker(String prefix, Stats stats, int threads, IntConsumerThatThrows task) {
this.prefix = prefix; this.prefix = prefix;
stats.gauge(prefix + "_threads", threads); stats.gauge(prefix + "_threads", threads);
try (var es = Executors.newFixedThreadPool(threads, new NamedThreadFactory(prefix))) { var es = Executors.newFixedThreadPool(threads, new NamedThreadFactory(prefix));
String parentStage = LogUtil.getStage(); String parentStage = LogUtil.getStage();
List<CompletableFuture<?>> results = new ArrayList<>(); List<CompletableFuture<?>> results = new ArrayList<>();
for (int i = 0; i < threads; i++) { for (int i = 0; i < threads; i++) {
final int threadId = i; final int threadId = i;
results.add(CompletableFuture.runAsync(() -> { results.add(CompletableFuture.runAsync(() -> {
LogUtil.setStage(parentStage, prefix); LogUtil.setStage(parentStage, prefix);
String id = Thread.currentThread().getName(); String id = Thread.currentThread().getName();
LOGGER.trace("Starting worker"); LOGGER.trace("Starting worker");
try { try {
long start = System.nanoTime(); long start = System.nanoTime();
task.accept(threadId); task.accept(threadId);
stats.timers().finishedWorker(prefix, Duration.ofNanos(System.nanoTime() - start)); stats.timers().finishedWorker(prefix, Duration.ofNanos(System.nanoTime() - start));
} catch (Throwable e) { } catch (Throwable e) {
System.err.println("Worker " + id + " died"); System.err.println("Worker " + id + " died");
// when one worker dies it may close resources causing others to die as well, so only log the first // when one worker dies it may close resources causing others to die as well, so only log the first
if (firstWorkerDied.compareAndSet(false, true)) { if (firstWorkerDied.compareAndSet(false, true)) {
e.printStackTrace(); // NOSONAR e.printStackTrace(); // NOSONAR
}
throwFatalException(e);
} finally {
LOGGER.trace("Finished worker");
} }
}, es)); throwFatalException(e);
} } finally {
done = joinFutures(results); LOGGER.trace("Finished worker");
}
}, es));
} }
es.shutdown();
done = joinFutures(results);
} }
/** /**