Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 127 additions & 9 deletions src/indexer/discovery_backfill.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ pub const Outcome = struct {
groups: usize = 0,
calls: usize = 0,
events: usize = 0,
/// Targets left unswept because the cycle's time budget ran out. They keep
/// `completed = 0` in the ledger, so the next cycle picks them up — the
/// ledger IS the resume state, no extra bookkeeping needed.
deferred: usize = 0,
};

const Interval = struct {
Expand Down Expand Up @@ -322,17 +326,53 @@ const RpcProber = struct {
const Target = struct {
addr: Address,
anchor: u64,
/// Highest block this target's sweep may touch. The init gap is exactly the
/// events a contract emitted BEFORE the indexer knew it existed, so
/// everything from its first-seen block onward was already indexed by the
/// ordinary path — re-sweeping it costs provider scan time and re-dispatches
/// events that are already stored.
ceil: u64,
};

/// The sweep window for one target: `null` means nothing is owed.
///
/// Two rules, and the asymmetry between them is deliberate:
/// - an UNKNOWN ceiling (0) falls back to `to_block`, i.e. the pre-existing
/// full-history sweep. Reading 0 as a literal ceiling would sweep nothing
/// and leave the init gap forever — a silent permanent hole, which is far
/// worse than the redundant scan it replaces.
/// - a known ceiling is clamped to `to_block`, because no sweep may run past
/// the block the indexer has actually reached.
/// A ceiling below the anchor means the contract was already known at its own
/// deployment block, so no unseen events exist.
fn sweepWindow(anchor: u64, raw_ceil: u64, to_block: u64) ?u64 {
const ceil = if (raw_ceil == 0) to_block else @min(raw_ceil, to_block);
if (ceil < anchor) return null;
return ceil;
}

/// Sort by ceiling first so a group is ceiling-homogeneous: the group sweeps to
/// its highest member's ceiling, and clustering equal ceilings keeps the
/// over-sweep above a member's own ceiling near zero. Anchor breaks ties, which
/// preserves the anchor clustering that keeps a group's block span tight.
fn targetLessThan(_: void, a: Target, b: Target) bool {
if (a.ceil != b.ceil) return a.ceil < b.ceil;
return a.anchor < b.anchor;
}

/// Sweep every contract in `addrs` from its deployment block to `to_block`
/// and record each one's outcome in the init-gap heal ledger (v2 only, the
/// ledger's authoritative protocol). Never fails: a failed group is logged
/// and its targets stay owed in the ledger.
pub fn backfillDiscovered(self: *Indexer, protocol: models.Protocol, client: *rpc.Client, kind: Kind, addrs: []const Address, to_block: u64) Outcome {
/// `ceils`, when given, is aligned with `addrs` and caps each target's sweep
/// individually; 0 means "unknown, use `to_block`". Pass null to sweep every
/// address to `to_block` (the discovery-time path already knows its own ceiling
/// and passes `from_block - 1` as `to_block`).
/// `deadline_ms`, when given, stops the pass between groups once the wall clock
/// passes it. The first group always runs, so a budget smaller than one group's
/// cost degrades to slow progress rather than to none at all.
pub fn backfillDiscovered(self: *Indexer, protocol: models.Protocol, client: *rpc.Client, kind: Kind, addrs: []const Address, to_block: u64, ceils: ?[]const u64, deadline_ms: ?i64) Outcome {
std.debug.assert(ceils == null or ceils.?.len == addrs.len);
var outcome: Outcome = .{ .total = addrs.len };
if (addrs.len == 0) return outcome;
const started = zsys.milliTimestamp();
Expand Down Expand Up @@ -403,36 +443,71 @@ pub fn backfillDiscovered(self: *Indexer, protocol: models.Protocol, client: *rp

var targets: std.ArrayList(Target) = .empty;
defer targets.deinit(self.allocator);
for (addrs, anchors) |addr, anchor| {
for (addrs, anchors, 0..) |addr, anchor, i| {
if (anchor) |a| {
targets.append(self.allocator, .{ .addr = addr, .anchor = a }) catch {
recordOutcome(self, kind, protocol, addr, error.OutOfMemory);
outcome.failed += 1;
};
if (sweepWindow(a, if (ceils) |cs| cs[i] else 0, to_block)) |ceil| {
targets.append(self.allocator, .{ .addr = addr, .anchor = a, .ceil = ceil }) catch {
recordOutcome(self, kind, protocol, addr, error.OutOfMemory);
outcome.failed += 1;
};
} else {
// Nothing owed — same outcome as "no history".
recordOutcome(self, kind, protocol, addr, null);
outcome.no_history += 1;
}
} else {
recordOutcome(self, kind, protocol, addr, null);
outcome.no_history += 1;
}
}
std.mem.sort(Target, targets.items, {}, targetLessThan);
if (targets.items.len > 0) {
std.log.info(" Discovery anchors: floor {d}, ceil {d}, {d} target(s) spanning {d}..{d}", .{ floor, to_block, targets.items.len, targets.items[0].anchor, targets.items[targets.items.len - 1].anchor });
// Report the ceiling range the sweeps will actually use, not the head:
// with per-target ceilings these differ by ~100k blocks on a real heal,
// and the old line made a capped sweep look like a full-history one.
std.log.info(" Discovery anchors: floor {d}, ceil {d}..{d} (head {d}), {d} target(s) spanning {d}..{d}", .{
floor,
targets.items[0].ceil,
targets.items[targets.items.len - 1].ceil,
to_block,
targets.items.len,
targets.items[0].anchor,
targets.items[targets.items.len - 1].anchor,
});
}

var start: usize = 0;
var swept_groups: usize = 0;
while (start < targets.items.len) {
// Between groups only, and never before the first one. Stopping mid-group
// would be the dangerous version: sweepTargets records each member's
// outcome, so a half-swept group could mark members complete for a range
// it never read. Guaranteeing one group per cycle rules out the livelock
// where a budget below one group's cost makes no progress forever.
if (swept_groups > 0 and deadline_ms != null and zsys.milliTimestamp() >= deadline_ms.?) {
outcome.deferred = targets.items.len - start;
std.log.info(" Discovery backfill: time budget reached after {d} group(s); {d} target(s) stay owed for the next cycle", .{ swept_groups, outcome.deferred });
break;
}
const end = @min(start + GROUP_MAX_ADDRESSES, targets.items.len);
sweepTargets(self, protocol, client, kind, targets.items[start..end], to_block, &outcome);
const group = targets.items[start..end];
// Sorted by ceiling, so the last member's is the group's maximum. Using
// the max over-sweeps the lower-ceiling members (harmless: those blocks
// are already indexed and the inserts conflict-ignore) whereas using the
// min would under-sweep the higher ones, which is a permanent gap.
sweepTargets(self, protocol, client, kind, group, group[group.len - 1].ceil, &outcome);
swept_groups += 1;
start = end;
}

std.log.info("Discovery backfill: {d} {s}(s) — {d} without history, {d} swept in {d} group(s), {d} failed; {d} anchors cached, {d} events, {d} RPC calls, {d}ms", .{
std.log.info("Discovery backfill: {d} {s}(s) — {d} without history, {d} swept in {d} group(s), {d} failed, {d} deferred; {d} anchors cached, {d} events, {d} RPC calls, {d}ms", .{
outcome.total,
kind.label(),
outcome.no_history,
outcome.swept,
outcome.groups,
outcome.failed,
outcome.deferred,
outcome.anchors_cached,
outcome.events,
outcome.calls,
Expand Down Expand Up @@ -918,3 +993,46 @@ test "searchAnchors resolves exact anchors despite a rate-limited probe round" {
// The retry answered, so nothing fell back to the floor.
try testing.expect(anchors[0].? > 11_708_980);
}

test "sweepWindow: an unknown ceiling falls back to the head, never to nothing" {
// 0 is "the join found no known_* row". It MUST widen to the head, because
// treating it as a literal ceiling would skip the sweep and leave the init
// gap permanently unhealed.
try std.testing.expectEqual(@as(?u64, 11_813_278), sweepWindow(11_700_000, 0, 11_813_278));
try std.testing.expectEqual(@as(?u64, 500), sweepWindow(500, 0, 500));
}

test "sweepWindow: a known ceiling caps the sweep and is clamped to the head" {
// The ordinary case: sweep only the pre-discovery window.
try std.testing.expectEqual(@as(?u64, 11_750_000), sweepWindow(11_708_980, 11_750_000, 11_813_278));
// Never past the block the indexer has reached, even if first_seen is ahead
// (possible right after a rollback moved the head backwards).
try std.testing.expectEqual(@as(?u64, 11_813_278), sweepWindow(11_708_980, 11_900_000, 11_813_278));
}

test "sweepWindow: a ceiling below the anchor owes nothing" {
// Known at (or before) its own deployment block: no window of unseen events.
try std.testing.expectEqual(@as(?u64, null), sweepWindow(11_800_000, 11_700_000, 11_813_278));
// Boundary: equal is a one-block window and IS owed.
try std.testing.expectEqual(@as(?u64, 11_800_000), sweepWindow(11_800_000, 11_800_000, 11_813_278));
// A head behind the anchor also owes nothing.
try std.testing.expectEqual(@as(?u64, null), sweepWindow(11_800_000, 0, 11_700_000));
}

test "targetLessThan groups by ceiling first so a group sweeps to its own max" {
var ts = [_]Target{
.{ .addr = testAddress(1), .anchor = 100, .ceil = 900 },
.{ .addr = testAddress(2), .anchor = 500, .ceil = 200 },
.{ .addr = testAddress(3), .anchor = 300, .ceil = 200 },
};
std.mem.sort(Target, &ts, {}, targetLessThan);
// Ceiling ascending, anchor breaking ties.
try std.testing.expectEqual(@as(u64, 200), ts[0].ceil);
try std.testing.expectEqual(@as(u64, 300), ts[0].anchor);
try std.testing.expectEqual(@as(u64, 200), ts[1].ceil);
try std.testing.expectEqual(@as(u64, 500), ts[1].anchor);
try std.testing.expectEqual(@as(u64, 900), ts[2].ceil);
// The last member's ceiling is the group maximum, which is what the group
// loop sweeps to — using the minimum would under-sweep the others.
try std.testing.expectEqual(@as(u64, 900), ts[ts.len - 1].ceil);
}
49 changes: 42 additions & 7 deletions src/indexer/event_fetchers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ pub fn indexResolverEvents(self: *Indexer, protocol: models.Protocol, client: *r
}
if (owed.items.len > 0) {
std.log.info("Backfilling {d} resolver(s) first sighted this batch (up to block {d})", .{ owed.items.len, from_block - 1 });
_ = discovery_backfill.backfillDiscovered(self, protocol, client, .resolver, owed.items, from_block - 1);
_ = discovery_backfill.backfillDiscovered(self, protocol, client, .resolver, owed.items, from_block - 1, null, null);
}

if (discovery.all.len == 0) {
Expand Down Expand Up @@ -650,6 +650,15 @@ const HEAL_MAX_ATTEMPTS = discovery_backfill.HEAL_MAX_ATTEMPTS;
/// Minimum seconds between attempts on one target, so a multi-hour RPC outage
/// cannot burn the whole attempt budget in a few poll cycles.
const HEAL_RETRY_SPACING_SECS: i64 = 600;
/// Per-poll-cycle heal budget. The drain runs inside the sync loop, so whatever
/// it spends is time the indexer is NOT following the chain head: an unbounded
/// pass once blocked head-following for 54 minutes after a single 1-block reorg
/// re-armed the whole ledger. Two bounds, because the pass has two cost drivers:
/// anchor bisection scales with the number of targets, sweeping with blocks and
/// events. Neither bound risks correctness — an unswept target simply stays owed
/// in the ledger, which is already how a failed sweep is resumed.
const HEAL_TARGETS_PER_CYCLE: usize = 200;
const HEAL_BUDGET_MS: i64 = 20_000;

/// Seed the ledger with every already-known resolver and subregistry. Runs at
/// initial-sync completion; enqueue is INSERT OR IGNORE, so completed targets
Expand Down Expand Up @@ -682,25 +691,51 @@ pub fn hasDueInitGapHeals(self: *Indexer, protocol: models.Protocol) bool {
pub fn drainInitGapHeals(self: *Indexer, protocol: models.Protocol, client: *rpc.Client) void {
if (protocol != .v2) return;
const retry_before = zsys.timestamp() - HEAL_RETRY_SPACING_SECS;
drainHealTask(self, .resolver, protocol, client, retry_before);
drainHealTask(self, .subregistry, protocol, client, retry_before);
// One deadline shared by both tasks, so a cycle's total heal cost is bounded
// rather than doubled. Whatever the resolver pass leaves is what the
// subregistry pass gets; on the next cycle the order is the same but the
// completed targets are gone, so both drain over successive cycles.
const deadline = zsys.milliTimestamp() + HEAL_BUDGET_MS;
drainHealTask(self, .resolver, protocol, client, retry_before, deadline);
drainHealTask(self, .subregistry, protocol, client, retry_before, deadline);
}

fn drainHealTask(self: *Indexer, kind: discovery_backfill.Kind, protocol: models.Protocol, client: *rpc.Client, retry_before: i64) void {
fn drainHealTask(self: *Indexer, kind: discovery_backfill.Kind, protocol: models.Protocol, client: *rpc.Client, retry_before: i64, deadline_ms: i64) void {
const task = switch (kind) {
.resolver => RESOLVER_HEAL_TASK,
.subregistry => SUBREGISTRY_HEAL_TASK,
};
var targets = self.database.pendingHealTargets(self.allocator, task, protocol, HEAL_MAX_ATTEMPTS, retry_before) catch |err| {
var targets = self.database.pendingHealTargets(self.allocator, task, protocol, HEAL_MAX_ATTEMPTS, retry_before, HEAL_TARGETS_PER_CYCLE) catch |err| {
std.log.warn("init-gap heal: failed to list {s} targets: {}", .{ task, err });
return;
};
defer targets.deinit(self.allocator);
if (targets.items.len == 0) return;

// Split the rows into the two parallel slices backfillDiscovered wants. The
// ceiling is each contract's first-seen block: the sweep owes only the
// events emitted before the indexer knew the contract existed. Sweeping to
// the head instead re-scanned ~9x more blocks and re-dispatched tens of
// thousands of already-stored events on every pass.
var addrs: std.ArrayList(Address) = .empty;
defer addrs.deinit(self.allocator);
var ceils: std.ArrayList(u64) = .empty;
defer ceils.deinit(self.allocator);
for (targets.items) |t| {
addrs.append(self.allocator, t.addr) catch break;
// first_seen_block is the first block the ordinary path covered, so the
// gap ends one block earlier. 0 means unknown and stays 0, which
// backfillDiscovered reads as "fall back to the head".
ceils.append(self.allocator, if (t.first_seen_block > 0) t.first_seen_block - 1 else 0) catch break;
}
if (addrs.items.len != targets.items.len or ceils.items.len != targets.items.len) {
std.log.warn("init-gap heal: out of memory listing {s} targets; leaving all {d} owed", .{ task, targets.items.len });
return;
}

const to_block = (self.database.getSyncState(protocol) catch null) orelse 0;
std.log.info("init-gap heal: {d} pending {s} target(s), sweeping up to block {d}", .{ targets.items.len, task, to_block });
const outcome = discovery_backfill.backfillDiscovered(self, protocol, client, kind, targets.items, to_block);
const outcome = discovery_backfill.backfillDiscovered(self, protocol, client, kind, addrs.items, to_block, ceils.items, deadline_ms);
std.log.info("init-gap heal: {s} pass complete, {d}/{d} swept", .{ task, outcome.swept + outcome.no_history, targets.items.len });
}

Expand All @@ -711,7 +746,7 @@ fn backfillAddressStrings(self: *Indexer, protocol: models.Protocol, client: *rp
const addr = Address.fromHex(hex) catch continue;
addrs.append(self.allocator, addr) catch continue;
}
_ = discovery_backfill.backfillDiscovered(self, protocol, client, kind, addrs.items, to_block);
_ = discovery_backfill.backfillDiscovered(self, protocol, client, kind, addrs.items, to_block, null, null);
}

/// Combined Phase 1 fetch: EthRegistry + ETHRegistrar + Datastore events in a single batch RPC call.
Expand Down
Loading
Loading