Skip to content

csnode: the periodic backfill flush runs on its own thread - #148

Merged
EnzinoBB merged 2 commits into
masterfrom
feat/backfill-async-flush
Nov 1, 2026
Merged

EnzinoBB merged 2 commits into
masterfrom
feat/backfill-async-flush

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

Why

On the mainnet test node (full rocksdb from the snapshot, master e422d2f) the periodic backfill flush ran on the processor thread for up to 5.6 s (median ~360 ms, every ~10,000 blocks). #147 defers it while the node is a confidant; this moves it off the processor thread altogether.

Change

  • Settings::asyncFlush (default true): a due periodic flush is handed to a thread of BackwardSync (started at the first one). One at a time: blocks stored meanwhile go with the next flush. The log line ends with on the flush thread; its duration no longer counts as processor-thread time.
  • The forced flush (end of the backfill) waits for the one under way, then flushes the rest on the calling thread.
  • stop() and the destructor stop and join the thread; Node::destroy stops the backfill before its last flushBackfill() and close().
  • Thread safety of what the thread calls (BlockChain::flushBackfill): cs::Lmdb::flush syncs under the map mutex shared with transactions (a map growth waits for it); DatabaseRocksDB::flush is SyncWAL; BerkeleyDB is opened with DB_THREAD; Storage::flush with async writes waits on its own queue lock; csdb error state is thread_local. No dbLock_ (as before).
  • Tests: ThePeriodicFlushRunsOnItsOwnThread (a flush held by the test: storing goes on, no second flush, the final flush waits); the other tests flush synchronously.

Test plan

  • CI build and unit tests (Linux, Windows, macOS)
  • robustness backfill rows (backfill-crash flushed step, power cut)
  • mainnet test node

🤖 Generated with Claude Code

EnzinoBB and others added 2 commits November 1, 2026 09:49
On the mainnet test node the backfill flush (transaction index, block
hashes, storage sync) took up to 5.6 s on the processor thread. It now
runs on a thread of BackwardSync: one at a time (the blocks stored
meanwhile go with the next one), the final flush at the end of the
backfill waits for the one under way and runs on the calling thread, and
stop() / the destructor join the thread; Node::destroy stops the backfill
before it flushes and closes the storage. The engines' flushes are
thread-safe (LMDB syncs under the map lock shared with transactions,
RocksDB SyncWAL, BerkeleyDB with DB_THREAD; error state is per thread).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- the test's gate is shared with the flush thread by value and waits up
  to 5 s for the thread to start (a failed assertion no longer leaves it
  on a dead mutex)
- stopFlusher detaches instead of terminating when called on the flush
  thread itself; a thread that cannot be created falls back to the
  synchronous flush; an exception from the flush is caught and logged
- BlockHashes::failures_ is atomic (the flush thread may report an LMDB
  failure); comment on the flush skipped after stop(); docs: the one
  remaining wait (LMDB map growth during a sync)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EnzinoBB
EnzinoBB merged commit e302bf7 into master Nov 1, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant