fix(events): bound diagnostic retirement during shutdown
This commit is contained in:
1 parent
7353436a77
commit
5e5e21374f
6 files changed
+373
-24
No files matched your search
@@ -57,7 +57,7 @@ final class FeatureEventOutbox implements AutoCloseable {
|
||||
synchronized (MAINTENANCE_LIFETIME) {
|
||||
OWNERS.remove(owner);
|
||||
if (OWNERS.isEmpty()) {
|
||||
REPLAYS.shutdown(); DIAGNOSTICS.shutdown();
|
||||
REPLAYS.shutdown(); DIAGNOSTICS.shutdownNow();
|
||||
REPLAYS = null; DIAGNOSTICS = null;
|
||||
}
|
||||
}
|
||||
@@ -84,10 +84,14 @@ final class FeatureEventOutbox implements AutoCloseable {
|
||||
private final Path directory;
|
||||
private final Function<EventInput, CompletionStage<LocalEventLog.Delivery>> sink;
|
||||
private final Consumer<Throwable> failures;
|
||||
private final java.util.concurrent.atomic.AtomicBoolean workerFinished = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
private final ExecutorService worker = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS,
|
||||
new ArrayBlockingQueue<>(MAX_HANDOFFS), task -> new Thread(task, "fantasy-feature-event-intents"),
|
||||
new ThreadPoolExecutor.AbortPolicy()) {
|
||||
@Override protected void terminated() { closeReplayCursor(); }
|
||||
@Override protected void terminated() {
|
||||
try { closeReplayCursor(); }
|
||||
finally { workerFinished.set(true); retire(); }
|
||||
}
|
||||
};
|
||||
private final CompletableFuture<Void> drained = new CompletableFuture<>();
|
||||
private final Set<UUID> inFlight = java.util.concurrent.ConcurrentHashMap.newKeySet();
|
||||
@@ -100,6 +104,10 @@ final class FeatureEventOutbox implements AutoCloseable {
|
||||
private final java.util.concurrent.atomic.AtomicLong rejected = new java.util.concurrent.atomic.AtomicLong();
|
||||
private final java.util.concurrent.atomic.AtomicLong deferred = new java.util.concurrent.atomic.AtomicLong();
|
||||
private final Object diagnosticFence = new Object();
|
||||
// Terminal after the bounded closing attempt; late rejections cannot resurrect this owner.
|
||||
private volatile boolean diagnosticsStopped;
|
||||
// Guarded by diagnosticFence: interrupt only this owner's active aggregate callback.
|
||||
private Thread diagnosticThread;
|
||||
private volatile long lastDiagnostic;
|
||||
private volatile boolean replayRequested;
|
||||
// The directory cursor is confined to the intent worker; it retains no materialized backlog.
|
||||
@@ -118,39 +126,66 @@ final class FeatureEventOutbox implements AutoCloseable {
|
||||
}
|
||||
|
||||
private boolean register() {
|
||||
if (diagnosticsStopped) return false;
|
||||
if (!registration.compareAndSet(0, 3)) return true;
|
||||
if (!OWNER_SLOTS.tryAcquire()) { registration.set(0); return false; }
|
||||
attach(this); registration.set(1); return true;
|
||||
attach(this); registration.set(1);
|
||||
// Closing may finish while a producer is attaching; retire that raced registration too.
|
||||
if (diagnosticsStopped) retire();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void retire() {
|
||||
if (!worker.isTerminated() || rejected.get() != 0 || deferred.get() != 0 || diagnosing.get()) return;
|
||||
if (!workerFinished.get() || (!diagnosticsStopped
|
||||
&& (rejected.get() != 0 || deferred.get() != 0 || diagnosing.get()))) return;
|
||||
if (!registration.compareAndSet(1, 2)) return;
|
||||
detach(this); OWNER_SLOTS.release(); registration.set(0);
|
||||
// A producer seeing state 2 cannot reattach until removal finishes; its counts remain owned here.
|
||||
if (rejected.get() != 0 || deferred.get() != 0) register();
|
||||
if (!diagnosticsStopped && (rejected.get() != 0 || deferred.get() != 0)) register();
|
||||
}
|
||||
|
||||
private void rejectObservation() { rejected.incrementAndGet(); register(); }
|
||||
|
||||
private void diagnose() {
|
||||
if (!diagnosing.compareAndSet(false, true)) return;
|
||||
synchronized (diagnosticFence) {
|
||||
if (diagnosticsStopped || !diagnosing.compareAndSet(false, true)) return;
|
||||
diagnosticThread = Thread.currentThread();
|
||||
}
|
||||
try {
|
||||
long now = System.nanoTime();
|
||||
if (!closingDiagnosis.getAndSet(false) && lastDiagnostic != 0
|
||||
&& now - lastDiagnostic < TimeUnit.SECONDS.toNanos(1)) return;
|
||||
long lost = rejected.getAndSet(0), delayed = deferred.getAndSet(0);
|
||||
if (lost != 0 || delayed != 0) {
|
||||
lastDiagnostic = now;
|
||||
report(new HandoffRejection(lost, delayed));
|
||||
long lost, delayed;
|
||||
synchronized (diagnosticFence) {
|
||||
// Admission/dequeue shares the stop fence: a late count cannot be withdrawn
|
||||
// by a diagnostic pass selected just before terminal shutdown.
|
||||
if (diagnosticsStopped) return;
|
||||
long now = System.nanoTime();
|
||||
if (!closingDiagnosis.getAndSet(false) && lastDiagnostic != 0
|
||||
&& now - lastDiagnostic < TimeUnit.SECONDS.toNanos(1)) return;
|
||||
lost = rejected.getAndSet(0); delayed = deferred.getAndSet(0);
|
||||
if (lost != 0 || delayed != 0) lastDiagnostic = now;
|
||||
}
|
||||
if (lost != 0 || delayed != 0) report(new HandoffRejection(lost, delayed));
|
||||
} finally {
|
||||
diagnosing.set(false);
|
||||
synchronized (diagnosticFence) { diagnosticFence.notifyAll(); }
|
||||
synchronized (diagnosticFence) {
|
||||
diagnosticThread = null;
|
||||
diagnosing.set(false);
|
||||
// Do not leak our cooperative cancellation into another owner's callback.
|
||||
if (diagnosticsStopped) Thread.interrupted();
|
||||
diagnosticFence.notifyAll();
|
||||
}
|
||||
retire();
|
||||
}
|
||||
}
|
||||
|
||||
private void stopDiagnostics() {
|
||||
synchronized (diagnosticFence) {
|
||||
diagnosticsStopped = true;
|
||||
if (diagnosticThread != null) diagnosticThread.interrupt();
|
||||
}
|
||||
// A non-cooperative handler can still retain its own executing stack/classloader.
|
||||
// Detachment is bookkeeping retirement, never proof that diagnostics were delivered.
|
||||
retire();
|
||||
}
|
||||
|
||||
void accepted(EventInput event) {
|
||||
if (!admit()) return;
|
||||
try { worker.execute(() -> {
|
||||
@@ -429,15 +464,20 @@ final class FeatureEventOutbox implements AutoCloseable {
|
||||
synchronized (diagnosticFence) {
|
||||
while (rejected.get() != 0 || deferred.get() != 0 || diagnosing.get()) {
|
||||
long remaining = deadline - System.nanoTime();
|
||||
if (remaining <= 0) break; // Bounded shutdown attempt; maintenance retains the aggregate diagnosis.
|
||||
if (remaining <= 0) break; // Best-effort diagnostics stop at this lifetime's budget.
|
||||
TimeUnit.NANOSECONDS.timedWait(diagnosticFence, remaining);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt(); report(interrupted);
|
||||
} finally { retire(); drained.complete(null); }
|
||||
} finally { stopDiagnostics(); drained.complete(null); }
|
||||
});
|
||||
}
|
||||
|
||||
/** Observations not submitted to an aggregate callback, including late calls after diagnostic retirement.
|
||||
* This is neither a durable-event count nor acknowledgment of earlier callback delivery. */
|
||||
long pendingRejectedObservations() { return rejected.get(); }
|
||||
|
||||
/** Bounded closing attempt, not an acknowledgment of diagnostic delivery or handler termination. */
|
||||
CompletionStage<Void> drained() { return drained; }
|
||||
}
|
||||
Reference in new issue
Block a user