Skip to content

refactor(reference-index): simplify reader read gate (drop generation seqlock, demote phase to metrics) #145

Closed

Description

@panos-xyz

Background

crates/reference-index/src/reader.rs::query_at currently gates reads through a 7-variant ReferenceIndexPhase state machine (Opening/PreJade/Deferred/Backfill/Live/Repairing/Unavailable) plus a generation seqlock (set_phase bumps generation odd→even; query_at spins for an even value and re-checks it at the end).

Reviewing the read path, most of this machinery is not load-bearing for correctness. Read correctness already comes from three independent layers:

  1. MDBX MVCC read-transaction snapshot (db.tx()): indexed_to, indexed_hash, and the iterated rows all come from one consistent snapshot; later commits cannot perturb an in-flight read.
  2. (indexed_to, indexed_hash) == tip comparison (reader.rs:176): decides whether that snapshot is exactly the canonical tip.
  3. RPC-layer before/after chain_info() bracketing (crates/rpc/src/morph/handler.rs): decides whether the tip moved during the read.

Problems

  • generation seqlock is redundant. A seqlock protects multi-field snapshots; here it guards a single AtomicU8 (phase), whose Acquire load is already consistent. The end-of-read generation re-check (reader.rs:208-212) is also over-conservative: a fully-correct result (single snapshot, == tip) is discarded as IndexUnavailable merely because the runtime flipped phase mid-read. It adds a spin point and can cause spurious failures without adding any guarantee the tx snapshot + tip comparison don't already provide.
  • phase over-couples the runtime state machine into the read gate. From the reader's "can I serve?" perspective, Deferred/Backfill are identical (IndexBehind) and Repairing/Unavailable are identical (IndexUnavailable). Worse, in Deferred/Backfill/Repairing the cursor already satisfies indexed_to != tip, so the line-176 comparison would return IndexBehind on its own — the phase gate there is only a micro-optimization (skip opening a tx) plus an error-code distinction, not a correctness requirement.

Non-goal / keep as-is

PreJade semantics must stay: pre-Jade must return Ok([]) (a complete, terminal empty answer — do NOT retry), which is the opposite instruction to IndexBehind (may have data — retry). Folding pre-Jade into IndexBehind would make every query retry forever until Jade activates. The Jade-boundary race handling (tip.timestamp < jade_timestamp fallback) must be preserved too.

Proposed simplification

  1. Delete the generation seqlock (the spin-for-even loop and the end-of-read re-check). Read phase (if still needed) with a single AtomicU8 load.
  2. Take phase out of the read path. Keep it as a pure metrics/log field (runtime keeps setting it; the gauge keeps exporting Deferred/Backfill/Repairing for observability). If the "Repairing/Unavailable → tell client not to retry" hint is worth keeping, use a single unavailable: AtomicBool instead of a 7-way match.
  3. Move the pre-Jade check up to the RPC layer. handler.rs already has header.timestamp(); do if tip.timestamp < jade_timestamp { return Ok(vec![]) } there so the query never enters the reader, and remove the PreJade phase.

Resulting query_at: open tx → read indexed_to/indexed_hash → compare (number, hash) == tip → iterate → return. Correctness carried by the MDBX snapshot + tip comparison + RPC bracketing, equivalent to (arguably better than) today, with much less complexity.

Scope

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions