Skip to content

Storage sweep: planning re-reads the same branch manifests once per index root #1637

Closed

Description

@zonotope

plan_sweep builds its live set by walking every branch's index chain and expanding each root's reachable CAS set. Both halves of that re-read data they already have:

  • collect_root_cas_ids_expanded runs once per root and issues a store.get per named-graph branch manifest and per annotation branch. Consecutive roots in an incremental chain share nearly all of those manifests, so the same blobs are fetched and decoded once per root rather than once per distinct manifest.
  • live_addresses calls the uncached walk_prev_index_chain_cs (fluree-db-indexer/src/gc/sweep.rs:195), even though walk_prev_index_chain_cs_cached exists and clean_garbage already feeds it config.artifact_cache_dir (collector.rs:245). Every root is re-read from remote storage with a warm artifact cache sitting unused.

Both costs land on the operation's critical path, and the sweep holds every branch of the ledger out of indexing for its whole duration.

Why it matters here specifically

The sweep exists for ledgers whose index chains grew unbounded — that is the population it targets, and it is the worst case for this shape. On a reported deployment the chain is ~3,000 roots. Multiplied by a handful of graph orders, expansion alone is tens of thousands of round trips against remote storage, all while indexing is excluded.

Reported as part of the #1614 review: "the difference between a sweep that takes a minute and one that takes an hour on the ledger this PR is aimed at."

Correctness is not affected. live_addresses already deduplicates at the ContentId level before deriving addresses, so the extra reads change only how long planning takes.

What to change

  1. Thread a HashSet<ContentId> of already-expanded branch manifests through the chain loop, so expansion approaches O(distinct manifests) rather than O(roots × branches). This is the same observation the existing CID-dedup comment at sweep.rs:191-193 makes about derived addresses, applied to reads instead. It likely wants a variant of collect_root_cas_ids_expanded that accepts a seen-set, since the current signature has no way to carry one across calls — note expanded_cas.rs:33-42 keeps a "use sites (must all stay in sync)" list that a new variant would need to join.

  2. Use the cached walk. plan_sweep has no artifact_cache_dir in its signature today, so this means plumbing one through from the caller, mirroring CleanGarbageConfig.

Also in scope while the function is open

These came out of the same review pass and sit in the same loop, so they are cheaper to do together than separately:

  • Branch chains are walked strictly sequentially. Each chain is inherently serial — it is a linked list — but branches are independent. Bounded concurrency across branches turns an N-branch ledger's planning time into roughly one branch's. Bound it rather than using unbounded join_all, because each concurrent walk holds a full Vec<IndexChainEntry> of decoded roots (see below).
  • live_addresses and swept_addresses are awaited in sequence (sweep.rs:94-95) although they are independent. try_join! overlaps the prefix listings with the chain walk. Small — the listings are the cheap side — but free.
  • The whole decoded chain is materialised before anything is consumed. walk_prev_index_chain_cs returns Vec<IndexChainEntry>, each holding a fully decoded IndexRoot whose default_graph_orders inline a LeafEntry per leaf. On a long chain over a wide index that is substantial resident memory for data the sweep reads exactly once, in order. A streaming walk that yields per root would let each be expanded, recorded, and dropped. This shape predates the sweep — the collector has it too — but the sweep is the caller most likely to meet a pathological chain.

Acceptance criteria

  • Planning a chain of N roots that share their branch manifests issues on the order of the distinct-manifest count, not N × manifests. A counting store double in the existing gc/sweep.rs tests can assert this directly.
  • The sweep uses the artifact cache when one is configured.
  • No change to which addresses are classified as orphans. The existing sweep tests — particularly a_dict_only_a_sibling_branch_references_survives and sweeping_a_real_ledger_reclaims_strays_and_leaves_queries_intact — must stay green, since this issue is purely about how planning gets its data.

Related

Activity

  1. added
    P2
    area:storage
    on Aug 25, 2026
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

    P2area:storage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions