Skip to content

Commit 47fec12

Browse files
committed
fix: decouple diagnostics from cooldown; mirror go-sdk-events review fixes (SDK-3375)
Diagnostics are now fully out of scope for the event-delivery cooldown, matching the Go implementation. A diagnostic delivery no longer mints a window token or feeds the tracker; it only calibrates the clock from the Date header as before. This removes a blackout mode a reviewer surfaced: a proxy that accepts /bulk but rejects /diagnostic would otherwise open a new window on every periodic diagnostic and discard analytics the service would accept. Backpressure on the diagnostic endpoint itself belongs to the DIAG spec (SDK-2777), not here. Mirrors the fixes made on the Go PR's human review (launchdarkly/go-sdk-events#68): - When a window opens, forget every context key so post-window payloads re-introduce contexts whose index/identify events were lost with the refused batch. The window opens on a flush worker thread while the deduplicator is only safe to touch from the dispatcher thread, so the clear is routed through the inbox as a FLUSH_USERS message. - A delivery failure now breaks the consecutive-success streak even when its token is stale, so success/failure/success no longer resets the ladder across a failure (RETRY 1.8.3). Stale outcomes still cannot open, step, or end a window; unstamped (null) tokens remain fully inert. - Comment trims, and the EventSender.failure() javadoc no longer claims it does not affect the delivery schedule. Tests updated for the window-open wipe: the three diagnostic-coupling tests become decoupling tests (the straddle case is gone with the token), and the dedup tests assert via index-event presence rather than exact flush counts.
1 parent 8358410 commit 47fec12

5 files changed

Lines changed: 123 additions & 141 deletions

File tree

‎lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/DefaultEventProcessor.java‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -700,23 +700,22 @@ private void handleResponse(EventSender.Result result, DeliveryCooldownTracker.W
700700
}
701701
}
702702

703-
// Diagnostic deliveries are not gated by the cooldown, and their outcomes drive it only the
704-
// way they drove the old permanent disable: an unexpected failure opens a window, while a
705-
// diagnostic success or ordinary failure has no effect on delivery state. In particular, a
706-
// diagnostic success is not the probe and cannot count toward the reset threshold; only
707-
// analytics deliveries prove the service is accepting analytics payloads again.
708-
private void handleDiagnosticResponse(EventSender.Result result, DeliveryCooldownTracker.WindowToken token) {
703+
// The cooldown governs analytics delivery only (EVTPROC section 1.8 scopes it to flush
704+
// payloads). A diagnostic delivery never touches cooldown state.
705+
private void handleDiagnosticResponse(EventSender.Result result) {
709706
if (result.getTimeFromServer() != null) {
710707
lastKnownPastTime.set(result.getTimeFromServer().getTime());
711708
}
712-
if (result.isUnexpectedFailure()) {
713-
noteUnexpectedFailure(token);
714-
}
715709
}
716710

717711
private void noteUnexpectedFailure(DeliveryCooldownTracker.WindowToken token) {
718712
long windowMillis = cooldown.recordUnexpectedFailure(token);
719713
if (windowMillis > 0) {
714+
// The refused batch's index and identify events were lost with it, so forget every
715+
// context key
716+
if (contextDeduplicator != null) {
717+
inbox.offer(new EventProcessorMessage(MessageType.FLUSH_USERS, null, false));
718+
}
720719
logger.error("Event delivery failed with an unexpected error;"
721720
+ " event delivery will not be attempted for the next {} seconds", windowMillis / 1000);
722721
}
@@ -731,13 +730,9 @@ public void run() {
731730
Writer writer = new BufferedWriter(new OutputStreamWriter(buffer, Charset.forName("UTF-8")), INITIAL_OUTPUT_BUFFER_SIZE);
732731
gson.toJson(diagnosticEvent.value, writer);
733732
writer.flush();
734-
// The send itself is not gated by the cooldown, but a diagnostic dispatched inside a
735-
// window gets a null token, so its outcome cannot move delivery state even if it
736-
// lands after the window has elapsed.
737-
DeliveryCooldownTracker.WindowToken token = cooldown.beginDeliveryUnlessInCooldown();
738733
EventSender.Result result = eventsConfig.eventSender.sendDiagnosticEvent(
739734
buffer.toByteArray(), eventsConfig.eventsUri);
740-
handleDiagnosticResponse(result, token);
735+
handleDiagnosticResponse(result);
741736
if (diagnosticEvent.initEvent) {
742737
didSendInitEvent.set(true);
743738
}

‎lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/DeliveryCooldownTracker.java‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,13 @@ synchronized boolean recordSuccess(WindowToken token) {
147147
return false;
148148
}
149149
if (inCooldown()) {
150-
// Defensive: tokens are issued atomically with the cooldown check (a diagnostic dispatched
151-
// in-window gets no token at all), and a pre-window token goes stale when the window opens,
152-
// so no current caller gets here. The invariant stands on its own: an outcome arriving
153-
// while a window is open must never move delivery state.
154-
return false;
150+
return false; // defensive: a current token cannot originate inside a window
155151
}
156152
boolean resumed = false;
157153
if (windowEndNanos != null) {
158154
windowEndNanos = null;
159155
resumed = true;
160-
// The probe ends the window-boundary round: the tokens of its concurrently dispatched
161-
// siblings go stale, so the boundary round cannot combine parallel deliveries into a
162-
// reset. (A sibling unexpected failure goes stale with them; the next failed delivery of
163-
// the new round opens the window instead, at most a cadence later.) Later rounds leave the
164-
// epoch unchanged, so parallel deliveries of one such round each count toward the streak:
165-
// the reset threshold counts successful deliveries, not rounds.
156+
// The probe ends the window: the tokens previously given out go stale.
166157
epoch++;
167158
}
168159
consecutiveSuccesses++;
@@ -173,53 +164,56 @@ synchronized boolean recordSuccess(WindowToken token) {
173164
}
174165

175166
/**
176-
* Records a delivery failure that was not unexpected. It breaks the consecutive-success streak
177-
* but never opens or lengthens a window. Stale outcomes, and outcomes arriving while a window
178-
* is open, update nothing.
167+
* Records a delivery failure that was not unexpected. It breaks the consecutive-success streak,
168+
* even on a stale token, but never opens or lengthens a window. Unstamped outcomes update
169+
* nothing.
179170
*
180171
* @param token the token issued when this delivery was dispatched
181172
*/
182173
synchronized void recordFailure(WindowToken token) {
174+
if (token == null) {
175+
return; // unstamped: not the outcome of a real delivery
176+
}
177+
// A real failure breaks the streak regardless of its era: the reset threshold counts
178+
// consecutive successful deliveries, and a failure between two successes makes them
179+
// non-consecutive no matter which round dispatched it.
180+
consecutiveSuccesses = 0;
183181
if (!isCurrent(token)) {
184182
return;
185183
}
186184
if (inCooldown()) {
187-
// Defensive; see recordSuccess.
188-
return;
185+
return; // defensive: a current token cannot originate inside a window
189186
}
190187
if (windowEndNanos != null) {
191-
// A probe that fails recoverably still ends the window: clear it so a much later success is
192-
// not misreported as the resumption, and close the boundary round (see recordSuccess) so the
193-
// probe's concurrently dispatched siblings cannot combine into a reset. The accepted cost is
194-
// that a sibling unexpected failure in the same round goes stale with them, deferring the
195-
// next window by at most one cadence: the next round's failure opens it, with the ladder
196-
// position preserved.
188+
// A probe that fails recoverably still ends the window: clear it so a later success is not
189+
// misreported as the resumption.
197190
windowEndNanos = null;
198191
epoch++;
199192
}
200-
consecutiveSuccesses = 0;
201193
}
202194

203195
/**
204-
* Records a delivery failure classified as unexpected, opening a cooldown window. Stale
205-
* outcomes update nothing: in particular, once one failure of a round has opened a window, its
206-
* concurrently dispatched siblings cannot lengthen it or step the ladder again for the same
207-
* incident. The window doubles from {@link #COOLDOWN_INITIAL_WINDOW_MILLIS} up to
208-
* {@link #COOLDOWN_MAX_WINDOW_MILLIS} per failed probe.
196+
* Records a delivery failure classified as unexpected. It opens a window that doubles from
197+
* {@link #COOLDOWN_INITIAL_WINDOW_MILLIS} up to {@link #COOLDOWN_MAX_WINDOW_MILLIS} per failed
198+
* probe. A stale token still breaks the consecutive-success streak but never opens or steps a
199+
* window, so one incident's concurrent failures step the ladder only once. Unstamped outcomes
200+
* update nothing.
209201
*
210202
* @param token the token issued when this delivery was dispatched
211-
* @return the length in milliseconds of the newly opened window, or zero if the outcome was
212-
* stale and no window was opened
203+
* @return the length in milliseconds of the newly opened window, or zero if no window was opened
213204
*/
214205
synchronized long recordUnexpectedFailure(WindowToken token) {
206+
if (token == null) {
207+
return 0; // unstamped: not the outcome of a real delivery
208+
}
209+
// See recordFailure: a real failure breaks the streak regardless of its era.
210+
consecutiveSuccesses = 0;
215211
if (!isCurrent(token)) {
216212
return 0;
217213
}
218214
if (inCooldown()) {
219-
// Defensive; see recordSuccess.
220-
return 0;
215+
return 0; // defensive: a current token cannot originate inside a window
221216
}
222-
consecutiveSuccesses = 0;
223217
attempts++;
224218
epoch++;
225219
warnedThisWindow = false;

‎lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/events/EventSender.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public static Result success(Date timeFromServer) {
8383
}
8484

8585
/**
86-
* Returns a result representing an ordinary delivery failure, such as a service outage, that
87-
* does not affect the delivery schedule.
86+
* Returns a result representing an ordinary delivery failure, such as a service outage or
87+
* network error. Unlike {@link #unexpectedFailure()}, it does not cause event delivery to
88+
* enter a cooldown window.
8889
*
8990
* @return the result
9091
*/

0 commit comments

Comments
 (0)