Skip to content

Build lease in the nameservice, so the storage sweep is safe across processes #1635

Open

Description

@zonotope

The storage sweep added for #1548 excludes concurrent index builds with MaintenanceGuard (fluree-db-indexer/src/orchestrator.rs), an in-process hold backed by a HashSet<String> shared between the handle and the background worker. That is sufficient today, because a single server process owns all index writes to its storage. It stops being sufficient the moment a second writer exists — an external indexer, a second server instance, or a peer configured to index the same storage.

This issue tracks replacing that in-process hold with a durable lease in the nameservice, so exclusion holds across processes. It is a prerequisite for supporting external indexers, not a fix for a current defect.

Why the in-process hold is not enough

An index build writes its artifacts — leaves, branch manifests, dictionary blobs — before publishing the root that references them. Between those two moments the artifacts are unreachable from any index chain, which is exactly the sweep's definition of an orphan. A sweep running concurrently with a build owned by another process would classify that build's in-flight output as garbage and delete it; the build would then publish a root pointing at deleted blobs. That is silent corruption of a live ledger, strictly worse than the unbounded growth #1548 was about.

MaintenanceGuard prevents this only for builds the in-process worker would start. Nothing stops a second process, so the sweep is currently documented as single-process-only in docs/api/endpoints.md, docs/cli/sweep.md, docs/indexing-and-search/background-indexing.md, and on the guard type itself.

Why a time-based grace period does not work

The obvious alternative — sweep only artifacts older than some window — cannot be made safe here. A reindex has no upper bound on duration: it replays the entire commit chain, so it grows with the ledger. Any fixed grace period is wrong for a large enough ledger, and the failure is silent deletion rather than a timeout. There is also no timestamp available to compare against: RemoteObject (fluree-db-core/src/storage.rs) carries only address and size_bytes, and list_prefix_with_metadata is implemented for MemoryStorage and S3 but not FileStorage.

A lease with a heartbeat avoids both problems. The expiry is tied to the heartbeat interval, not the operation's duration, so a two-second incremental build and a six-hour reindex behave identically — the long one simply heartbeats more times. The sweep waits on "is a lease live", never on "has enough time passed", and crash detection stays bounded by the expiry regardless of how long a healthy build runs.

The primitives already exist

No new storage machinery is needed. The nameservice has two CAS-capable surfaces, and one of them is explicitly documented as the home for locks.

RefPublisher::compare_and_set_ref (fluree-db-nameservice/src/lib.rs:970) is a strict compare-and-set: "Updates the ref only if the current identity matches expected. Pass expected = None for initial creation (ref must not exist)." That is acquire-if-free. But RefKind (:894) has only CommitHead and IndexHead, and RefValue { id: Option<ContentId>, t: i64 } (:909) is shaped for content identities and watermarks — a lease holder and expiry would have to be encoded into fields that mean something else.

StatusPublisher::push_status (:1241) fits properly. StatusValue { v: i64, payload: StatusPayload } (:1119) carries a monotonically increasing version that push CAS-checks, and StatusPayload.extra (:1039) is a flattened HashMap<String, serde_json::Value> whose doc comment already names the intended contents: "Extensible metadata (queue_depth, locks, progress, error messages, etc.)". The module doc at :1222 repeats it. A lease record with holder identity and expiry drops in without a schema change.

What needs to change

  1. Define the lease record — holder id, expiry, and acquire/renew/release operations over push_status, with CAS retry on contention.
  2. Acquire it in every path that writes index artifacts. Today that is the background worker's admission point (orchestrator.rs, where maintenance_held is checked) and Fluree::reindex (fluree-db-api/src/admin.rs). maybe_refresh_after_commit is a third build path but is behind the unused embedded-orchestrator feature and has no in-tree callers outside tests — decide whether it is supported before wiring it.
  3. Heartbeat while the operation runs, on an interval well under the expiry, so a crashed holder's lease expires without a healthy long build ever losing its hold.
  4. Have the sweep take the same lease rather than MaintenanceGuard, and refuse rather than block when it is held — a sweep queued behind a multi-hour reindex is indistinguishable from a hang.
  5. Retire MaintenanceGuard or reduce it to a local fast path in front of the lease.
  6. Remove the single-process caveat from the four places it is currently documented.

Cost to weigh

Every index build would acquire, heartbeat, and release a nameservice record. With DEFAULT_REINDEX_MIN_BYTES at 100 bytes (fluree-db-api/src/server_defaults.rs:31), background builds run at roughly one per commit, so this is not incidental traffic on a write-heavy ledger — it is a nameservice round trip per build plus a heartbeat task per build. Worth measuring against the default-evaluation work, since the two interact: a larger reindex_min_bytes makes lease overhead negligible, and a lease makes the current default more expensive than it already is.

Acceptance criteria

  • Two processes attempting an index build on the same ledger: exactly one proceeds, the other defers or refuses.
  • A sweep in one process excludes a build in another, and vice versa.
  • A holder killed mid-build releases within the expiry window without operator action.
  • A six-hour reindex holds its lease for the whole run without renewal gaps, exercised with a compressed expiry rather than a real six hours.
  • Single-process behavior is unchanged, and the existing sweep and maintenance tests still pass.

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