Skip to content

persistit: settle MVCC state at the end of Persistit.initialize()#266

Merged
vharseko merged 1 commit into
OpenIdentityPlatform:masterfrom
vharseko:fix/persistit-initialize-settle-mvcc
Jul 11, 2026
Merged

persistit: settle MVCC state at the end of Persistit.initialize()#266
vharseko merged 1 commit into
OpenIdentityPlatform:masterfrom
vharseko:fix/persistit-initialize-settle-mvcc

Conversation

@vharseko

Copy link
Copy Markdown
Member

Problem

Fixes #264.

After a crash/restart, Volume.getTree(name, false) (and step-based MVCC visibility checks generally) can return a stale answer until background maintenance happens to run. Recovery itself completes synchronously inside Persistit.initialize(), but two maintenance tasks are asynchronous after it:

  • the TransactionIndex active-transaction-cache updater thread;
  • CleanupManager's pruneTimelyResources(), which fires no earlier than 1 second after startup (MINIMUM_MAINTENANCE_INTERVAL_NS guard: start() sets _lastMaintenance = now, and poll() skips maintenance until the interval elapses).

Until those run, residual tree-version state from before the crash can leak into step-based visibility — e.g. a tree created at transaction step 1 is reported visible at step 0. A fresh ActiveTransactionCache has _ceiling = 0, so hasConcurrentTransaction() returns true for everything until the first recompute(), keeping residual versions alive; and recovery's DefaultRollbackListener.removeTree is a TODO no-op while endTransaction marks the aborted status with setMvvCount(0), so tree-lifecycle residue of uncommitted transactions survives recovery and waits for timer-driven pruning. An application calling Volume.getTree(name, false) shortly after initialize() has no way to know it must wait for internal maintenance timers.

This surfaced as the intermittent CI failure of TreeTransactionalLifetimeTest.createRemoveByStep (see #260 for the test-side settle workaround and #257 for the review discussion that raised the core-level question).

Fix

At the end of initialize(), after pruneObsoleteTransactions() and before the background managers start, settle the recovered MVCC state synchronously:

_transactionIndex.updateActiveTransactionCache();
pruneTimelyResources();

The order matters: pruning consults the active-transaction cache. Both calls are idempotent (the CleanupManager performs the same work on its timer), and the cost is bounded — one ATC recompute (no application transactions exist yet at this point) plus one pass over the timely resources touched during recovery. copyBackPages() already uses the same synchronous idiom before checkpointing.

A deeper follow-up candidate noted in #264: implementing the DefaultRollbackListener.removeTree TODO so rollback replay prunes tree-lifecycle residue itself, instead of relying on later background pruning.

Verification

All green locally (macOS, JDK 21):

  • TreeTransactionalLifetimeTest — 5/5, including createRemoveByStep
  • RecoveryTest — 11 run, 1 skipped (skipped before the change as well)
  • AccumulatorRecoveryTest — 4/4
  • WarmupTest — 2/2

Recovery completes synchronously inside initialize(), but two maintenance
tasks were left to background timers: the TransactionIndex active-transaction
cache update and CleanupManager's pruneTimelyResources() (first pass no
earlier than 1s after startup). Until they ran, residual tree-version state
from before a crash could leak into step-based MVCC visibility — e.g.
Volume.getTree(name, false) reporting a tree created at step 1 as visible
at step 0.

Update the active-transaction cache and prune timely resources synchronously
at the end of initialize(), before the background managers start, so
applications get correct step visibility immediately after initialize()
returns. Both calls are idempotent and their cost is bounded (one ATC
recompute plus one pass over recovered timely resources).

Fixes OpenIdentityPlatform#264.
vharseko added a commit to vharseko/commons that referenced this pull request Jul 11, 2026
The manager's performed/error counters are cumulative for the Persistit
instance, and startup maintenance may already have performed cleanup
actions before the test enqueues its own - in particular the
pruneTimelyResources() call added to Persistit.initialize() by OpenIdentityPlatform#266.
Waiting for and asserting absolute values could then exit early or fail;
take a baseline before enqueueing and compare deltas instead.
vharseko added a commit that referenced this pull request Jul 11, 2026
* Stabilize flaky CleanupManagerTest.testCleanupHappens

The test offered 500 cleanup actions and waited only while
getEnqueuedCount() > 0. That counter drops to 0 as soon as the background
CLEANUP_MANAGER thread dequeues the batch -- before performAction runs -- so on
a slow runner the wait loop exited with no action yet performed and _counter
still 0 (expected:<500> but was:<0>, seen on ubuntu-latest JDK 17).

Wait on the manager's own performed/error counters (which advance only after
each action completes) until all 500 are processed, with a 30s cap.

* Compare cleanup counters against a baseline in testCleanupHappens

The manager's performed/error counters are cumulative for the Persistit
instance, and startup maintenance may already have performed cleanup
actions before the test enqueues its own - in particular the
pruneTimelyResources() call added to Persistit.initialize() by #266.
Waiting for and asserting absolute values could then exit early or fail;
take a baseline before enqueueing and compare deltas instead.
@vharseko vharseko merged commit 6f0a2a8 into OpenIdentityPlatform:master Jul 11, 2026
14 checks passed
@vharseko vharseko deleted the fix/persistit-initialize-settle-mvcc branch July 11, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

persistit: stale step-visibility from Volume.getTree() after restart until CleanupManager prunes

2 participants