You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 41d7c51
Browse filesBrowse the repository at this point in the historyBrowse files
perf(core): Batch and coalesce scope-persistence disk writes (#5791)
* perf(core): Batch and coalesce scope-persistence disk writes (JAVA-628)
Scope persistence wrote to disk on every scope mutation, which dominated
SDK cost during startup: each breadcrumb triggered a synchronous fsync'd
QueueFile append, and every other scope field (contexts, trace, user,
tags, ...) rewrote its whole file on each change even though only the
latest value matters.
Coalesce mutations instead of writing eagerly. Each field keeps only its
latest pending value and is flushed once per debounce window; breadcrumbs
are buffered and appended together behind a single fsync (QueueFile gains
an opt-in buffered-write mode plus sync()). This trades a small data-loss
window (~100ms before the process dies) for far fewer writes and fsyncs.
Persistence exists to enrich crash/ANR events on the next launch, and was
already asynchronous, so the widened loss window is acceptable.
* Add changelog entry for #5791
* perf(core): Coalesce scope writes on submit instead of a timer (JAVA-628)
Scope-persistence flushes were debounced 100ms behind a scheduled task.
The debounce was unnecessary: the Sentry executor is single-threaded, so a
submitted flush task already sits in the queue long enough for mutations
arriving behind it to be folded into the same write. That is exactly the
window that matters, since the queue is deepest during startup.
Submit the flush instead of scheduling it. Coalescing now tracks executor
load rather than a fixed delay, which closes the data-loss window the
debounce introduced. Guard against the executor rejecting the task without
throwing once its queue is full, which would otherwise leave the pending
flag set and stop scope persistence for the rest of the process.
Also record why resetCache() deliberately leaves pending mutations alone:
they only ever hold values from the current process, so dropping them
would lose scope state set during init rather than clearing the previous
run's data.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(core): Keep breadcrumbs added during a scope flush (JAVA-628)
Batching split one ordered stream of breadcrumb operations into two
independent pieces of state: a queue of pending adds and a separate
clear flag. Independent state cannot preserve an ordering that the old
FIFO executor queue gave for free.
flushPending consumed the clear flag with a CAS at the top, then spent
the rest of the method serializing and writing each breadcrumb. A clear
plus a subsequent add landing in that window had its clear applied after
the new breadcrumb was already on disk, so the follow-up flush wiped a
breadcrumb that was added after the clear. Breadcrumbs are added from
arbitrary threads while the flush runs on the executor thread, so this
needs no unusual timing.
Enqueue the clear into the breadcrumb queue as a sentinel instead,
mirroring the DELETE_MARKER pattern already used for pendingWrites.
Ordering becomes intrinsic to the queue rather than something the flush
has to reconstruct. This also stops setBreadcrumbs(emptyList()) from
calling clear() on the shared queue, which could discard a breadcrumb
offered concurrently by another thread even with no flush in flight.
* docs(core): Clarify scope-persistence comments (JAVA-628)
"Durable" overstated what the buffered-write mode guarantees. sync()
issues an fsync, but durability also depends on the storage stack, so
describe what the code does — write to disk — rather than promise an
outcome it cannot ensure on its own.
Also name the pendingBreadcrumbs queue for what it holds: requests to
add and clear, not breadcrumbs already buffered.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* refactor(core): Fold the scope flush into one method (JAVA-628)
flushPending existed only so the @testonly flush() could do the disk
write without the latch bookkeeping that belongs to the queued task.
Nothing in production called flush(), and its only test asserted the
behaviour of flush() itself, so the second entry point was buying two
near-identical methods and a way to corrupt hasPendingFlush.
Drop flush() and merge the pair: flush() is now the queued task and
writePending() the write it performs. Clear hasPendingFlush in a finally
so an unexpected throw cannot leave the flag set, which would stop scope
persistence for the rest of the process.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* test(core): Drop redundant buffered-write QueueFile test (JAVA-628)
bufferedWritesSurviveReopenAfterSync was testAddOneElement with a second
element and a sync() call: same write, close, reopen, read-it-back shape,
and testAddAndRemoveElements already covers round-tripping far more
thoroughly.
The sync() it appeared to exercise was not actually load-bearing. close()
flushes through to the OS whichever mode the file was opened in, so the
assertions would hold even if sync() did nothing — the name promised
durability semantics the test could not verify in-process. Verifying that
would take a killed process, not a reopen.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* docs(core): Record that the vendored Tape fork has diverged (JAVA-628)
Square archived Tape on 2024-10-25, so the upstream link in each file
header points at a snapshot that will never receive fixes. Our copy has
also drifted from it: corruption recovery, a bounded ring size, and
optional buffered writes.
Say both things where a reader will look — the file headers, the class
javadoc, and THIRD_PARTY_NOTICES.md — so nobody diffs against upstream
expecting it to explain our behaviour, or files a bug there.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* refactor(core): Iterate pendingWrites directly when flushing (JAVA-628)
writePending() copied the key set into an ArrayList before draining it,
which allocates on every flush for no benefit: ConcurrentHashMap's
iterator is weakly consistent, so walking it while removing entries is
already safe.
Removal still goes through the map, not the iterator, because that is an
atomic get-and-remove — we never drop a value stored by a mutation racing
with the loop. Keys added after iteration starts can be missed, which is
fine since flush() re-checks and queues another write.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,8 @@
11
11
12
12
- Remove an unused lock from `SentryPerformanceProvider`, which was allocated on every cold start in `ContentProvider.onCreate` without ever being acquired ([#5871](https://github.com/getsentry/sentry-java/pull/5871))
13
13
- Parse the app start profiling config with only the deserializer it needs instead of building a full `JsonSerializer` and `SentryOptions`, cutting 188 of 221 allocations on the main thread before `Application.onCreate` ([#5867](https://github.com/getsentry/sentry-java/pull/5867))
14
+
- Batch and coalesce scope-persistence disk writes to reduce startup cost ([#5791](https://github.com/getsentry/sentry-java/pull/5791))
15
+
- Scope mutations are now coalesced (latest value per field) and breadcrumbs are appended in batches behind a single fsync, instead of one synchronous disk write per mutation.
The Sentry Java SDK includes an adapted version of Square's Tape library, a file-based FIFO queue implementation used for reliable event storage. The code resides in the `io.sentry.cache.tape` package and includes `QueueFile`, `FileObjectQueue`, and `ObjectQueue`.
146
146
147
+
Upstream was archived on 2024-10-25 and is no longer maintained. This copy is maintained in-tree and has diverged from the linked commit: it recovers from file corruption by recreating the file, bounds the queue to a maximum number of elements, and supports optional buffered writes flushed by an explicit `sync()`.
0 commit comments