Skip to content

perf(storage): parallelize sharded history last-shard reads - #7404

Closed
decofe wants to merge 5 commits into
cyclops-mirror/reth-26766-basefrom
centaur/mirror-sharded-history-pr-1788362015
Closed

decofe wants to merge 5 commits into
cyclops-mirror/reth-26766-basefrom
centaur/mirror-sharded-history-pr-1788362015

Conversation

@decofe

@decofe decofe commented Sep 2, 2026

Copy link
Copy Markdown
Member

Mirrors paradigmxyz/reth#26766 for Cyclops review.

Supersedes #26765, which only parallelized incremental IndexStorageHistory. This covers both history tables, in both the pipeline and live persist.

The expensive bit is the committed get of each key's last shard (highest_block = u64::MAX). On unique-slot (and unique-account) workloads, serial gets cannot keep up with 2s blocks. #25282 already parallelized it in write_storage_history. write_account_history and the incremental index stages were still serial, and the stage path did the gets inside with_rocksdb_batch_auto_commit.

Last-shard gets read committed state only (they do not see pending batch puts). They must finish and join before the write batch is created so a key is never read after it has been put, and so parallel RocksDB gets do not overlap auto-commit write_opt. MDBX DbTx is not safe for concurrent gets. So: group new indices, finish all last-shard reads (Rayon on RocksDB, serial on MDBX) and join, then open the batch and put. Puts stay serial on purpose. first_sync append-only is unchanged and never touches last shards.

--full snapshot restore does not ship RocksDB indexes. The stage now computes rebuild_from_empty from the durable checkpoint before prune jumps the in-memory checkpoint to tip - 10064, so that first execute clears the empty table and append-only indexes the prune window ((tip - 10063)..=tip, 10064 blocks). Parallel last-shard reads are for live persist and incremental stage restarts (checkpoint != 0), not that first --full execute.

Cherry-picked onto v2.5.0 (nothing else from main) and A/B'd a Base Sepolia --full restore on an i7i.16xlarge (snapshot block 45,664,656, then ~35–39k blocks of catchup):

  • Stock v2.5.0: IndexStorageHistory stuck at Loading indices for 30+ minutes. Checkpoint frozen, ~1 core, page-cache reads, almost no disk. Never reached Writing indices. That node took the incremental last-shard path because prune bumped the checkpoint before first_sync was computed.
  • This patch before the empty-rebuild split: grouped in ~23s, last-shard gets ~8 min at 60–73 cores, short serial put, then caught tip. That A/B measured the incremental last-shard path.
  • HEAD: --full first execute is clear + append-only over the prune window. Catchup and live persist still use the two-phase last-shard plan.

Test plan

  • history_shards unit tests (rechunk, serial==parallel, overlapping gets on a dedicated pool)
  • Incremental IndexAccountHistory / IndexStorageHistory with many distinct keys (MDBX)
  • Existing first_sync / unwind / prune stage tests
  • Grouping keeps a coalesced logical key in one prepare batch when ETL rows exceed the byte cap
  • Base Sepolia --full snapshot restore + catchup vs unpatched v2.5.0

Prompted by: @joshieDo

niran added 5 commits August 19, 2026 13:30
Serial committed gets of the u64::MAX last shard stall unique-slot (and
unique-account) persist and incremental index stages. Share one two-phase
plan so RocksDB last-shard reads overlap, then serially put into a batch
opened only after those gets join.
The two-phase loader still groups ETL rows under a distinct message, then
emits the original Writing indices progress while serially putting shards.
Chunk history collection and preparation so large rebuilds remain restartable without exhausting memory.
Last-shard gets read committed state and must join before the write
batch; an open batch does not deadlock Gets. A logical key whose ETL
rows exceed the prepare byte cap still occupies one batch.
@decofe

decofe commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

cyclops audit fast

@tempoxyz-bot tempoxyz-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👁️ Cyclops Review

This change's read-before-write design is sound, but three actionable issues remain in chunked restart healing, zero-target pruning, and the public batch helper API.

Reviewer Callouts
  • ⚡ MDBX initial-sync performance: After the first chunk, each subsequent chunk performs serial committed last-shard reads for every touched key. Benchmark a from-genesis MDBX sync, not only --full restore.
  • ⚡ History preparation memory bound: history_group_bytes and the projected-size calculation use Vec::capacity() differently, making the 256 MiB limit soft.
  • ⚡ Prune clear branch: target_reached() && prune_target_applied is currently unreachable because history retains 10,064 blocks, but it clears the entire table if that invariant changes. Document or assert the dependency.

if initial_sync && input.target() == 0 {
BlockRangeOutput { block_range: 0..=0, is_final_range: true }
} else {
input.next_block_range_with_threshold(self.commit_threshold.max(1))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ [ISSUE] Chunked history checkpoints trigger full RocksDB heal scans after mid-sync restarts

Persisting each bounded chunk creates durable states where 0 < checkpoint < static_file_tip. On restart, the unchanged RocksDB healer bypasses its checkpoint == 0 fast path and scans every remaining account changeset with per-key unwind work; the storage stage has the same issue at index_storage_history.rs:133. Large initial syncs can therefore incur hours of no-op startup healing after every interruption.

Recommended Fix:
Persist a RocksDB history-index watermark with each flush and early-return when it matches the MDBX checkpoint, or make the RocksDB write and stage checkpoint one recovery unit so healing is bounded to one chunk.

.flatten() &&
target_prunable_block > input.checkpoint().block_number
(target_prunable_block > input.checkpoint().block_number ||
target_prunable_block == 0 && input.checkpoint().block_number == 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ [ISSUE] Before(0) is treated as pruning genesis history

PruneMode::Before(0) semantically prunes nothing, but its saturated target is 0, so this equality marks pruning as applied. The subsequent rebuild clears the table and skips block 0; the storage stage has the same issue at index_storage_history.rs:97. Standard provider fallback may mask the omission, but the persisted history index no longer honors the no-op mode.

Recommended Fix:
Return None from prune_target_block_with_min for Before(0), with account and storage tests showing Before(0) retains genesis while Before(1) excludes it.

)?;
let prepared = prepare_history_shard_writes_serial::<tables::AccountsHistory, _>(
BTreeMap::from([(address, indices)]),
|key| self.provider.get::<tables::AccountsHistory>(key),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛡️ [DEFENSE-IN-DEPTH] Public batch append helpers can overwrite pending history updates

This committed-state read occurs inside an already-open batch. A second append for the same logical key misses the first pending put and overwrites it; auto-commit can make visibility threshold-dependent. The storage helper has the same contract violation. Callers are currently test-only, but both methods are public and conflict with the read-before-batch protocol introduced by this PR.

Recommended Fix:
Restrict both helpers to tests or crate scope, or accept all grouped keys at once so one update per logical key is structurally enforced.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been marked stale due to 7 days of inactivity.

@github-actions github-actions Bot added the stale label Sep 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Closed due to inactivity. Reopen if still needed.

@github-actions github-actions Bot closed this Sep 14, 2026
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.

4 participants