Skip to content

fix: Commit everything held at a track or identify at immediate persistence - #419

Open
abelonogov-ld wants to merge 329 commits into
mainfrom
andrey/immediate-track-commits-held-events
Open

abelonogov-ld wants to merge 329 commits into
mainfrom
andrey/immediate-track-commits-held-events

Conversation

@abelonogov-ld

Copy link
Copy Markdown
Contributor

Summary

At IMMEDIATE, track and identify wrote only their own event before returning and left the evaluations held before them, and their summaries, to a commit queued on the commit executor. A kill straight after the track (the test app's "Eval+Kill now") could therefore lose the exposure on slower devices, where the kill wins the race against the commit executor.

This goes back to committing everything held, under the commit lock, before the call returns. With oversized summaries no longer split (#408, 577261bb), that costs the caller a normal encode of the held run and the summaries.

Reverts 19afcecb and 12000256 from #408.

Trade-off

A track on the main thread waits for any commit already in progress, including a delivery's commit and close; usually well under a millisecond.

Tests

  • New anImmediateTrackPutsTheEvaluationsBeforeItOnDiskBeforeItReturns: holds the commit executor and expects the feature, summary and custom events on disk when track returns.
  • Full unit suite run 10 times: 1251 tests, 0 failures each run.

Made with Cursor

abelonogov-ld and others added 30 commits September 22, 2026 14:04
…y/event-durability-tier3-persistence

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/OutboundEventBuffer.java
An evaluation of a tracked flag writes a summary counter and a feature
event, and a debug event alongside them when debugging is on. Those
writes took separate locks, so a close() landing between them delivered
the counter and then refused the event, reporting an evaluation that no
event describes.

Widen the lock that guarded the pending events to cover the summary
counters too, so the writes one evaluation makes are one critical
section. drain() splits into takeSummaries() and encode() so the flush
can take the run and the counters together and still encode outside the
lock.

Co-authored-by: Cursor <cursoragent@cursor.com>
…-durability-tier2-bounded-flush

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/DirectEventProcessor.java
…y/event-durability-tier3-persistence

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/DirectEventProcessor.java
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/OutboundEventBuffer.java
createEventAndReset hands back a period's statistics and clears them in
the same call, so whoever builds the event owns it. It was built on the
scheduler thread and posted from the diagnostics thread, which may have
been busy with an earlier post; a transition to offline or background in
that window was noticed after the build and discarded the event. The
period's stream inits, events-in-batch count and dropped count went with
it, and dataSinceDate had already moved, so the next event described a
window beginning after the reset.

Move the build onto the posting thread, behind the suspension check, so
a transition leaves the counters where they are for the next period to
carry. The init event keeps its check, having nothing to consume.

Co-authored-by: Cursor <cursoragent@cursor.com>
…y/event-durability-tier3-persistence

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/DirectEventProcessorTest.java
The normalization said `>= 0`, so a capacity of nought stayed nought. On main
that threw from `new ArrayBlockingQueue<>(0)` at construction; with the pending
list there is nothing to throw, so it became a silent refusal of every event.

Worse than it sounds, because the same number is the buffer's context limit and
only the processor was normalizing it: at nought neither full events nor
summaries survived, and at -5 the processor allowed one event while the buffer
refused to summarize at all. So the fix is one normalization in ComponentsImpl
feeding both, with the processor keeping its own guard for direct construction.

Co-authored-by: Cursor <cursoragent@cursor.com>
…ing the server

describeConfiguration was sending the raw capacity, so a client configured with
nought told the service nought while running at one. It now shares the one
normalization with the buffer and the processor.

The race test was flaking about one run in six. Not the processor: the test
server records a request body with a single unlooped read, so a body past one
socket read's worth comes back truncated, at a point that moves between runs.
Thirty events instead of a hundred and fifty stays clear of it, and the test
still fails on every un-fixed run.

Co-authored-by: Cursor <cursoragent@cursor.com>
…y/event-durability-tier3-persistence

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java
Three components bound themselves by this number and each was normalizing it,
or not. Clamping in the setter makes the field valid by construction, which is
what the sibling setter for the diagnostic interval already does, and leaves
every consumer -- the buffer, the processor, the diagnostic description -- to
just use it.

Co-authored-by: Cursor <cursoragent@cursor.com>
…y/event-durability-tier3-persistence

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java
The two init tests asserted nothing. One asked a foreground processor to go to
the foreground, which setInBackground returns from before reaching any decision;
the other waited for the init event before going offline, so the only thing
stopping a second one was that the first had already gone. Each was held up by
the guard the other claimed to cover, and both passed with every guard deleted.

Coming to the foreground now goes through the background first, and waits for
the diagnostics thread to come free -- while the first init is still posting,
the claim is taken and the transition is turned away before it decides anything.
The offline one builds a processor that never comes online, then brings it
online to show the silence was the offline state and not a dead fixture.

Two new ones. A must-shut-down response has to stop recording and delivery for
good, which nothing exercised. And a flush running against four recorders has to
keep each evaluation in one payload: close() delivers once, so it can catch an
evaluation being stranded but never one being split across two deliveries.
Eight trials, because a single one catches a split lock about two times in three.

Co-authored-by: Cursor <cursoragent@cursor.com>
…-durability-tier2-bounded-flush

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/DirectEventProcessorTest.java
…y/event-durability-tier3-persistence

aFlushNeverSplitsAnEvaluationAcrossTwoPayloads is left behind at tier 2. It
reads each payload as it went out, which says something only where a payload is
the run taken straight from the pending list; here a delivery reads the store,
so a counter and its event can land in one payload or two for reasons that have
nothing to do with the lock. Confirmed by narrowing the lock and watching it
pass five times out of five. A comment in its place says where it went.

Co-authored-by: Cursor <cursoragent@cursor.com>
The MalformedJsonException in aContextAlreadyBeingCountedKeepsCountingOnceTheLimitIsReached
is not a serialization race: recording is sequential on the test thread and the payload is a
deterministic ~28 KB. The test-helpers HTTP server reads request bodies with a single unlooped
read, so larger bodies can be truncated depending on the machine. Fill the summarizer to a
limit of 10 contexts instead of 100 so each flush stays well under that size.

Co-authored-by: Cursor <cursoragent@cursor.com>
Offline means the application told the SDK to stay off the network, and closing does not revoke
that, so the final delivery in close() is skipped and held events are dropped. DefaultEventProcessor
behaved the same way (its FLUSH is a no-op while offline); the comments and the EventProcessor
javadoc promised more than either implementation did. Add a test pinning the behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
…-durability-tier2-bounded-flush

* andrey/event-durability-tier1-buffer:
  Document that close() discards events held while offline
…y/event-durability-tier3-persistence

* andrey/event-durability-tier2-bounded-flush:
  Document that close() discards events held while offline
…n reconnect

The SDK builds the event processor offline and turns it on once initialization is done. Coming online
used to deliver immediately, meant for the end of an outage, but it also fired on that first transition
at startup and sent the initial identify alone before anything else was recorded. DefaultEventProcessor
never did that, and the contract tests expect the first payload to wait for a flush.

The immediate delivery existed only because cancelling the periodic flush for an outage restarted its
interval on every reconnect. The flush task now stays scheduled regardless of the offline state (a run
while offline does nothing), so the interval is never restarted and what an outage buffered goes out at
the first run after it ends. No transition needs special handling.

Co-authored-by: Cursor <cursoragent@cursor.com>
…-durability-tier2-bounded-flush

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/DirectEventProcessorTest.java
…y/event-durability-tier3-persistence

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/DirectEventProcessor.java
#	launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/EventProcessorTestBase.java
…rent constructor

The context limit moved to the fourth parameter; the androidTest copy of the benchmark was never
compiled by the unit-test runs and kept the old order.

Co-authored-by: Cursor <cursoragent@cursor.com>
A commit takes the pending run under recordLock and encodes it outside, so until it is staged the run
is in neither pending nor the store. The capacity check counted only those two, so an event recorded in
that window was accepted past capacity; LDClientEventTest.testEventBufferFillsUp saw two identifies
with a capacity of one once startup stopped delivering the first one alone. EventStore is no longer
final so the test can pause a commit mid-staging.

Co-authored-by: Cursor <cursoragent@cursor.com>
With DEFERRED persistence, flush() and blockingFlush() now write on the
events thread instead of the calling thread. The event store finds its
directory and process name the first time it needs them, on the events
thread, not while LDClient.init builds it. close() writes on the events
thread and only falls back to the caller's thread if that thread doesn't
get to the write within the close budget. commit() returns before taking
the I/O lock when persistence is off. The IMMEDIATE and flush() javadocs
now say which thread does the write.

Co-authored-by: Cursor <cursoragent@cursor.com>
updateScheduledTasks no longer returns early once the processor is
closed. The closed flag now gates only starting work, so cancelling
always happens, and close() cancels its tasks by calling
updateScheduledTasks under stateLock instead of repeating the cancel
calls itself.

Co-authored-by: Cursor <cursoragent@cursor.com>
abelonogov-ld and others added 2 commits November 3, 2026 09:34
…rsistence again"

This reverts commit a65c588, which moves to a
PR of its own.

Co-authored-by: Cursor <cursoragent@cursor.com>
…e again

Writing only the tracked event left the evaluations before it to a commit
queued on the commit executor, so a kill straight after the track, which is
what immediate persistence is for, could take those exposures with it. With the
summary split gone, the commit costs the caller a normal encode of the held run
and the summaries, so the track commits it again before returning.

This reverts 19afcec and 1200025.

Co-authored-by: Cursor <cursoragent@cursor.com>
@abelonogov-ld
abelonogov-ld marked this pull request as ready for review November 3, 2026 17:36
@abelonogov-ld
abelonogov-ld requested a review from a team as a code owner November 3, 2026 17:36
@abelonogov-ld abelonogov-ld changed the title Commit everything held at a track or identify at immediate persistence fix: Commit everything held at a track or identify at immediate persistence Nov 3, 2026
abelonogov-ld and others added 2 commits November 12, 2026 12:24
The constructor queued the recovery of what a previous run left and scheduled
the periodic flush and the diagnostics, so building a processor was enough to
start work on the events thread. The constructor now only sets fields, and
start() does that work. EventProcessorBuilderImpl.build calls it straight
after the constructor, before anything else can use the processor, so the
recovery is still the first task on the events thread and every delivery
queued after it waits for it.

Tests start each processor they build, as the builder does, and a new one
checks that a processor not yet started leaves a previous run's log alone.

Co-authored-by: Cursor <cursoragent@cursor.com>
@tanderson-ld

Copy link
Copy Markdown
Contributor

bugbot review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit d70f24c. Configure here.

abelonogov-ld and others added 4 commits November 20, 2026 11:16
Each environment now has the whole `capacityBytes`, like `capacity`,
rather than an equal share of it. This follows the review on iOS
([ios-client-sdk#538](launchdarkly/ios-client-sdk#538 (comment))),
which makes the same change.

Stacked on #408 (tier 3).

- **An equal share is too small where there are many environments.**
Some customers configure 7, which at the default left each under 3 MiB.
- **Adding an environment no longer shrinks the others.** A share was
fixed when the store was built, so a directory within its old share
could be over its new one, and new events were refused until delivery or
expiry drained it.
- **The disk the SDK may use now grows with the environments**: up to 20
MiB each at the default. Ordinary traffic reaches `capacity` (2000
events) long before that; only unusually large events, or summaries
piling up through a long outage, get near it.
- **`ComponentsImpl.shareOfCapacityBytes` is gone**; the store gets
`capacityBytes` as it is, and already reads zero or less as the default.
The `EventProcessorBuilder.capacityBytes` docs say it is for each
environment.

Test: `theByteCapacityIsSharedEquallyByTheConfiguredEnvironments`
becomes
`eachEnvironmentHasTheWholeByteCapacityHoweverManyAreConfigured`: with
seven environments and a 2,400-byte capacity, an 850-byte event is
delivered, where the old split's 342 bytes would have refused it.

`./gradlew :launchdarkly-android-client-sdk:testDebugUnitTest`: 1257
tests, 0 failures (25 skipped).

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> **Persisted event byte limits are now per environment**, matching
`capacity(int)` and the iOS client change, instead of splitting
`capacityBytes` evenly across all configured mobile keys.
> 
> `ComponentsImpl` drops `shareOfCapacityBytes` and passes the
configured `capacityBytes` straight into `EventStore.create`.
**`EventProcessorBuilder` docs** are updated to state each secondary
environment gets the full limit, so adding environments no longer
shrinks others and apps with many envs (e.g. seven) aren’t stuck with
tiny per-env quotas.
> 
> The unit test is renamed and asserts that with seven environments and
a 2,400-byte limit, an ~850-byte custom event still flushes—behavior
that would fail under the old equal-share rule.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
a809095. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Cursor <cursoragent@cursor.com>
Changes `EventProcessorBuilder.DEFAULT_MAX_AGE_MILLIS` from 7 days to 24
hours. Persisted events that have not been delivered within 24 hours are
discarded and counted as dropped, and so is what the SDK stored for a
mobile key the application stopped using. Applications can still call
`maxAgeMillis(long)` to keep events longer. It has no effect where
`eventPersistence` is `DISABLED`.

Stacked on #408 (tier 3).

- `DEFAULT_MAX_AGE_MILLIS` is now `24L * 60 * 60 * 1000`, with its
Javadoc updated.
- `EventStoreBoundsTest.keepsABatchYoungerThanTheMaximumAge` aged its
batch by 6 days, which is now past the default; it ages it by 12 hours.
The tests that age by 8 days still exceed the maximum age and are
unchanged.

iOS counterpart: launchdarkly/ios-client-sdk#549.

Tested: `:launchdarkly-android-client-sdk:testDebugUnitTest`, 1257
tests, 0 failures.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> **Shortens the default retention window** for persisted analytics
events from **7 days to 24 hours** by updating
`EventProcessorBuilder.DEFAULT_MAX_AGE_MILLIS` and its Javadoc.
> 
> When event persistence is enabled, undelivered batches older than the
default max age are discarded (and counted as dropped); apps can still
override via `maxAgeMillis(long)`. The change has **no effect** when
`eventPersistence` is `DISABLED`.
> 
> `EventStoreBoundsTest.keepsABatchYoungerThanTheMaximumAge` now ages a
batch by **12 hours** instead of 6 days so it still falls under the new
default.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
057a2ef. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: Cursor <cursoragent@cursor.com>
The unknown-flag de-duplication set added in 68d3dbf is backed by a
ConcurrentHashMap, which rejects null, so variation(null, ...) threw a
NullPointerException on the caller's thread. A null key now skips the
de-duplication and returns the default, as it did before.

Co-authored-by: Cursor <cursoragent@cursor.com>
@cursor cursor Bot added the risk:medium label Nov 21, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Risk: medium.

Automated risk review (dry run): Medium

This PR would not be automatically approved. Human approval required.

Why: The diff changes IMMEDIATE track/identify so they commit the whole held run on the caller thread (EventCommitter, EventStore write-alone removal), plus DirectEventProcessor.start() and EventPersistence docs. That is a durability and threading behavior change with bounded blast radius, so it is Medium, not Low.

If you disagree with this review, share this PR in #feedback-agent-approvals or submit a changes-requested review explaining why.

Human review is needed, and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Dry-Run PR Approver

@abelonogov-ld
abelonogov-ld added this pull request to stack #441 November 21, 2026 00:12
@abelonogov-ld
abelonogov-ld removed this pull request from stack #441 November 21, 2026 00:14
abelonogov-ld and others added 5 commits November 20, 2026 16:18
…ty-tier1-buffer

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	launchdarkly-android-client-sdk/build.gradle
#	launchdarkly-android-client-sdk/src/main/java/com/launchdarkly/sdk/android/ComponentsImpl.java
#	shared-test-code/build.gradle
Persisted batches are posted by AnalyticsEventSender, which ignored EventProcessorBuilder.enableCompression, so only diagnostic events were compressed. The batch on disk stays plain JSON; each attempt compresses it, and one that cannot be compressed goes uncompressed.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Risk: medium. SDK rule 2 (approve only PRs opened by an SDK team member) decided this result, and the change is also above the low-risk approval threshold because IMMEDIATE track/identify now commits held events on the caller thread. Human review is needed, and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Risk: medium. SDK rule 2 (approve only PRs opened by an SDK team member) decided this result; Cursor Bugbot passed with no findings, but this change is also above the low-risk approval threshold because IMMEDIATE track/identify now commits held events on the caller thread. Human review is needed, and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

abelonogov-ld and others added 2 commits November 20, 2026 17:19
…o be discarded

sdk-test-harness v2.46.0 added "events/delivery retry/payload is discarded if the
retry also fails". With persistence, a batch whose retry fails is kept and posted
again later (DeliveryRetryState: a failure never stops delivery), so the next post
repeats the payload instead of carrying only the later event. The other delivery
retry tests pass.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Risk: medium. SDK rule 2 (approve only PRs opened by an SDK team member) decided this result; Cursor Bugbot passed with no findings, but this change is also above the low-risk approval threshold because IMMEDIATE track/identify now commits held events on the caller thread. Human review is needed, and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

Base automatically changed from andrey/event-durability-tier3-persistence to main November 21, 2026 01:50

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants