Skip to content

fix(indexer): stop a reorg re-arming the whole init-gap heal ledger - #137

Merged
mdtanrikulu merged 1 commit into
stagingfrom
fix/heal-stall-per-reorg
Sep 30, 2026
Merged

mdtanrikulu merged 1 commit into
stagingfrom
fix/heal-stall-per-reorg

Conversation

@mdtanrikulu

Copy link
Copy Markdown
Contributor

What happened

On staging, 2026-09-30 07:41–08:41, the V2 indexer followed the chain head for
9.4% of the hour. A single 1-block Sepolia reorg cost ~54 minutes of blackout:

07:47:36.949 REORG DETECTED at block 11813277 → rolled back to 11813276
07:47:42.211 init-gap heal: 1726 pending resolver_init_gaps target(s) ← 5.3s later
08:24:47.516 Discovery backfill: 1724 swept in 18 group(s), 0 anchors cached,
29852 events, 725 RPC calls, 2225305ms
08:24:47.608 init-gap heal: 680 pending subregistry_init_gaps target(s)
...still running 16 minutes later at the end of the log

No Indexing v2 from block line appears for the whole window, and the reorg
cursor stays disarmed throughout — so a reorg arriving during it would write
uncaptured.

Root cause

Three compounding defects, all pre-existing (they arrived with the heal ledger,
not with any recent merge):

  1. rollbackToBlock re-armed every target unconditionally. One blanket
    UPDATE completed = 0 re-opened all 2,406 rows. Yet the 1,724 resolver
    anchors span 11708980–11812555 — every one predates the rollback point by
    ≥722 blocks and was provably untouched by the created-after deletes.
  2. The drain is unbounded and runs inside the sync loop. No budget, no
    deadline, no resume; it drains both tasks to completion before the loop
    continues.
  3. Sweeps ran to the chain head, not to the contract's first-seen block. The
    init gap is only the events emitted before the indexer knew a contract
    existed; everything after was already indexed. So ~25,000 of the 29,852
    dispatched events were never owed, and 958,140 block-ranges were scanned for
    104,298 distinct blocks — 9.2× redundant.

Where the 2,225,305 ms went

Phase Time Content
Anchor bisection 128,585 ms (5.8%) ~707 eth_getCode requests at ~160 ms
Sweep 2,096,720 ms (94.2%) only ~18 getLogs; the rest is Postgres

The sweep is 29,852 events × 70.2 ms of per-event unbatched autocommit. The
27.3 ms/statement figure is derived independently, from a different part of the
same log (a 65,710 ms boot gap ÷ 2,411 statements), so the accounting isn't
circular.

Worth flagging for anyone reading the raw log: the headline "725 calls in
2,225,305 ms = 3.07 s per call" is an artifact. 708 of those calls are the
anchor bisection; per-call latency was never the problem.

What this changes

Sweeps stop at the contract's first-seen block. Not a new convention — the
discovery-time path already capped itself this way; the heal path now matches its
sibling. Two safety properties, both tested:

  • An unknown first-seen block widens to the head (the old behaviour).
    Reading it as a literal ceiling would sweep nothing and leave a permanent gap.
  • The join is a LEFT JOIN scoped by protocol. An inner join silently drops
    an unregistered target out of the sweep; an address-only join lets a v1
    sighting cap a v2 sweep (known_* is keyed (protocol, address)).

The rollback re-arm is scoped to targets whose gap window reaches above the
rollback point.
A group sweeps to its highest member's ceiling, so a
lower-ceiling member does see events above its own — those are safe
specifically because they sit at or above that contract's first-seen block, so
the ordinary path indexed them too, and the sync-state rewind immediately after
this call re-creates them. An unknown window re-arms; attempts still resets on
every row.

For the incident's exact shape, this is 0 re-arms instead of 2,406.

The drain is bounded by both of its cost drivers. A target cap for the anchor
bisection (which runs over every target before the first group sweeps, so a
time budget alone cannot bound a pass) and a wall-clock deadline checked
between groups for the sweeping. The first group always runs, so a budget below
one group's cost degrades to slow progress rather than livelock. Resume needs no
new state: an unswept target simply stays owed, which is already how a failed
sweep resumes.

Verification

Lane Result
test-all 89/89 steps, 1513/1513
test-pg 6/6 steps, 20/20
test-storage-pg 28/28 steps, 288/289 (1 skipped, baseline parity)
test-integration-pg 4/4 steps, 41/41
zig fmt --check src build.zig clean
schema/postgres in sync yes

12 new tests. Each was sabotage-checked — the fix reverted, the suite
confirmed to fail — including the +1 boundary, the unknown-row fail-safe, the
LEFT→INNER join, the protocol scoping, and the non-binding cap.

Measured on a tree also carrying an unrelated v2-attribution change set (disjoint
files); that set accounts for ~30 of the test count.

Not included, deliberately

  • Rate-limiter rework. acquire() guards only ~2 of ~12 outbound requests
    per cycle — every call site is an inter-chunk spacer whose guard is false on a
    single-chunk pass — and it meters HTTP batches, not sub-calls: the 725 counted
    calls carried ~68,000 JSON-RPC sub-calls, so the probe phase ran ~530
    eth_getCode/s (~10,000 CU/s) while the boot line said "25 req/s". That's the
    direct cause of the 15 provider throttles in the log. Deciding what to meter
    and whether to add adaptive backpressure is a redesign, not a fix.
  • Three block_timestamp hints on the registration path (~30k RPC calls on
    the next resync) — they touch a file with unrelated pending work.
  • POLL_INTERVAL and the indexer's RATE_LIMIT_PER_SECOND are compose literals,
    so an operator can't tune either without editing the file.

Cap heal sweeps at the contract's first-seen block, scope the rollback re-arm to targets whose gap window reaches above the rollback
  point, and bound the drain per poll cycle.
@mdtanrikulu
mdtanrikulu added this pull request to stack #139 September 30, 2026 12:36
@mdtanrikulu
mdtanrikulu merged commit 68f11ca into staging Sep 30, 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