Skip to content

Epoch GC is permanently disabled on any node restarted before its first rotation #292

Open

Description

@SDL-A1

Migrated from SergioDemianLerner/rustock#292 (issue by @SDL-A1, 2026-10-12).
Original: SergioDemianLerner/rustock#292

Seen live on the testnet sync at /mnt/import/tn-snapshot/node, which is
emitting this once a minute and has collected nothing:

WARN  Seeded epoch has no snapshot metadata, so its highest block is unknown;
      it can never be swept until that is recorded
...
INFO  Collecting against #280160 (0xeecdb5e3...)
ERROR Collection failed: refusing to sweep: the oldest epoch has no record of
      the highest block it holds, so it cannot be shown safe to delete

What happens

EpochTrieStore::open (crates/storage/src/epoch_store.rs:456) treats "exactly
one epoch, and it is non-empty" as "seeded from a snapshot", and rotates so
the seed stays read-only:

if epochs.len() == 1 && !Self::epoch_is_empty(&epochs[0]) {
    ...
    epochs.push(Epoch { seq, db: open_epoch(&d, &cache)?, dir: d, last_block: None });
}

That condition is also true for a perfectly ordinary case: a node restarted
after it has written to its first epoch but before that epoch ever rotated.

This testnet store was never seeded from anything — it synced from genesis,
reached #6,601, and was restarted.

Unlike rotate_only and the sweep path, this branch pushes the new epoch
without calling close_newest, so epochs[0].last_block stays None. Every
other path records it:

fn close_newest(&self, epochs: &mut [Epoch], current_block: u64) {
    if let Some(e) = epochs.last_mut() {
        if e.last_block.is_none() {
            write_last_block(&e.dir, current_block);
            e.last_block = Some(current_block);
        }
    }
}

last_block: None on the oldest epoch is permanent — nothing ever revisits
it — and the oldest epoch is the only sweep candidate. So collection is
disabled for the life of that store. The guard is doing its job; it is being
handed a store it can never clear.

Impact

Correctness is unaffected — nothing is deleted, so no state goes missing, and
the sync is running clean. What is lost is the entire point of the epoch
backend: the trie grows without bound. This store is at 2.8 GB by #281,000
with 8.17M blocks of testnet to go.

A node that rotated at least once before its first restart is fine, which is
why mainnet has not shown this: it rotates every 1024 MB and restarts are
rare relative to that.

Fix

The branch needs to record a highest block for the epoch it is closing. Any
value at or above what the epoch actually holds is safe — it only delays
sweeping — and the executed head at open time qualifies, since nothing in the
epoch can be above it.

open does not currently receive the head. Two ways:

  1. Pass the executed-head height into open and call close_newest with it
    in this branch. Also distinguishes a real snapshot seed (no head yet) from
    a restart.
  2. Record last_block for the newest epoch on clean shutdown, in flush.
    Cheaper, but leaves an unclean shutdown in the same hole.

(1) is the one that actually closes it. Needs a test that opens a store,
writes without rotating, reopens, and asserts the oldest epoch can be swept —
the case no current test covers, since the existing ones either rotate first
or seed from a snapshot.

Activity

  1. SDL-A1 commented on Nov 24, 2026

    @SDL-A1
    CollaboratorAuthor

    Could not reproduce on the mainnet store — and a trap to avoid when checking

    Investigated on the mainnet node (/var/lib/rustock/trie-epochs-flat) on 2026-11-24 while chasing an unrelated segbuild warning. The symptom is not present there, and the checking itself went wrong in a way worth recording.

    The metadata file is epoch-last-block, not last-block

    write_last_block writes dir.join(EPOCH_META), and EPOCH_META is epoch-last-block. A check like

    for d in epoch-*; do cat "$d/last-block"; done     # WRONG -- no such file, ever

    prints nothing for every epoch and looks exactly like the bug: "no epoch has recorded its highest block, so collection is permanently disabled". It is not evidence of anything. I drew that conclusion and was wrong.

    The correct check:

    for d in epoch-*; do printf '%s %s\n' "$d" "$(cat "$d/epoch-last-block" 2>/dev/null || echo none)"; done

    On the mainnet store that gives:

    epoch epoch-last-block
    1 9342320
    2 9367401
    3 9417643
    4 (none)

    Epoch 4 is the newest and still open, so none there is correct and expected — only the oldest epoch missing it is the fault this issue describes.

    The journal is not a reliable record of whether sweeps happened

    journalctl -u rustock | grep 'Collecting against' returned zero matches, which also looks like confirmation. It is not: the journal only reached back to 2026-11-15, and the sweep predates that.

    The durable record is sweep-log in the store root, one line of seq last_block swept_at reclaimed_bytes:

    0 9342320 9417643 828347755
    

    One sweep, against the root at #9,417,643, reclaiming 828 MB. So collection has run on this store and the guard is not stuck.

    Why no second sweep, which also looks like the bug

    --gc-epochs 4 with exactly four epochs present means there is nothing to sweep: the oldest becomes a candidate only once a fifth rotates in, and epoch 4 was at 769 MB of its 1024 MB rotation threshold. A store sitting at its epoch limit with no recent sweep is the expected steady state, not a symptom.

    Suggested before anyone takes this

    Confirm it on the store it was filed against (/mnt/import/tn-snapshot/node) using the epoch-last-block check above, and specifically that the oldest epoch lacks it. The reported log line — Seeded epoch has no snapshot metadata, so its highest block is unknown — is the real signal, and it is emitted once a minute, so a live or recent journal from that node settles it immediately.

    Also worth noting for whoever fixes it: a backfill already exists at crates/storage/src/epoch_store.rs:459, which closes any non-newest epoch missing its record at the resume_at height, with the safety argument in the comment (too high only delays collection; too low would delete state a later block needs). The node passes resume_at from its executed head. So the remaining question may be narrower than the issue title suggests: not that nothing records the bound, but which stores reach open without a usable resume point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions