Skip to content

fix(raft): each member indexes the branches it owns, leadership changes answer a typed 503, and orphaned commit files are released - #2492

Open
bplatz wants to merge 5 commits into
mainfrom
fix/raft-owner-indexing
Open

bplatz wants to merge 5 commits into
mainfrom
fix/raft-owner-indexing

Conversation

@bplatz

@bplatz bplatz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Raft fixes found while testing three-node clusters, one commit each.

Each member indexes the branches it owns

A cluster indexed in one place, the leader's indexer, which subscribed to every commit event. That subscription has no byte gate, so:

  • the leader built after every commit, and a raised --reindex-min-bytes changed nothing on a cluster;
  • --indexing-enabled=false did not stop it.

The other members ran no indexer at all: a Fluree built with a supplied nameservice got IndexingMode::Disabled. And RaftNameService::publish_index on a member that did not lead dropped the head and reported success, which #1768 describes.

Indexing now follows branch ownership, as staging does:

  • Owners build. A member built with the replicated nameservice runs its background indexer when indexing is configured, with the same providers (fulltext, attachments, warm cache) as a standalone server. The commit worker triggers it once a commit brings the branch's novelty to --reindex-min-bytes, or after a fast-forward merge.
  • The trigger waits for the local apply. The indexer reads the head from the replicated state, which on a follower trails the leader's answer, so the trigger waits (up to 5 s) until this member has applied a commit at the staged t. It is spawned before the publish: applying the commit can empty the branch's queue, and the supervisor then stops the worker wherever it is.
  • Followers publish through the leader. A member that does not lead sends the index head to the leader's new POST /raft/apply_index_head, which proposes AdvanceIndexHead, or RewriteIndexHead for an admin rebuild. No outcome reads as success unless the head was proposed: a lost answer, a step-down or no known leader is ProposeUnresolved, and the indexer retries.
  • The leader sweeps. The leader-scope worker no longer subscribes to commits. It keeps its catch-up sweeps (at leadership and every --indexer-catchup-interval-secs) for ledgers left behind, and now takes the operator's GC retention, so its builds collect as the owners' do.
  • --indexing-enabled=false is refused on a cluster member, through ServerConfig::validate and through FlureeServerBuilder::with_raft: no process outside the cluster can publish its indexes. fluree-server used to validate only while building the server, after it had bootstrapped Raft; it now validates first, so a refused configuration never opens the log.

Leadership-change 503s are typed

A transaction that reached a node which had stopped leading, or whose wait a new leader's snapshot cut off, answered 503 typed err:system/InternalError. Both now answer 503 err:db/LeaderChanged with Retry-After, through SubmissionError::LeaderChanged, which stays unsettled so a keyed retry is not recorded as a failure. A Raft fatal during enqueue keeps its 503: that node's Raft has stopped.

Commit files from unanswered publishes are released

While the leader is unreachable, the owning worker stages its entry again on every retry (about every 250 ms). Each attempt writes a new commit file, and one whose publish outcome is unknown is kept in case it became the head, so an outage left one orphan per retry. The worker now remembers them for the entry. Only one commit can take a queue entry, so once a later attempt lands, the rest can never become the head and are released. Pushes and fast-forwards name commits the worker did not write and are not tracked. A worker stopped mid-outage still leaves its files.

An unsettled error releases its idempotency claim

CachingCommitter does not record an error that may not be the submission's outcome (is_settled() == false), and its comment says the claim is then released. Every caller marked the claim done regardless, so it stayed InFlight for the cache TTL (an hour) and every keyed retry on that node answered AlreadyInFlight. In the worst case, a request reusing a key with another body reached a node whose cache did not hold the key (after a restart). The replicated map refused it as a collision, but its claim stayed under the colliding body, so the original request's retries answered KeyCollision for the hour. This also happens on v4.2.9. A LeaderChanged answer, which tells the client to retry with the same key, pinned the key the same way.

record_outcome now takes the claim and releases it before returning when the error is unsettled. A keyed retry runs again: under Raft the replicated map answers it with the queued entry or its receipt, and the unsettled errors a local execution can return all come before its commit.

Rolling upgrade

/raft/apply_index_head is a new route. Until the leader runs this release, it builds after every commit as before, and answers an upgraded follower's index publish with 404; the follower logs Failed to publish index, will retry and finds the leader's index on its next attempt. Upgrading the leader last, as documented, avoids the reverse window, in which a branch owned by an older follower would be indexed only by the leader's catch-up sweep. No replicated command changed.

A reindex is forwarded to the leader and holds that member's indexer. An owner on another member is not held, so its incremental build can still publish after the reindex, as the leader's unheld indexer could before.

Tests

  • Three-node server cluster: with --reindex-min-bytes at 512 KiB and catch-up re-sweeps off, small commits to a branch a follower owns stay unindexed, and the commit that crosses the threshold is indexed at its own t through the leader. Restoring the leader's commit subscription fails it at the small commits; dropping the follower's publish, disabling member indexing, or triggering before the local apply fails it at the large one.
  • Worker: a publisher that leaves two attempts unanswered and accepts the third; the first two commit files are gone and the third is kept. With the release removed, it fails.
  • Nameservice: a publish with no leader is unresolved for both publishers (replacing the test that pinned the swallow); wire round trip and error mapping of the new route.
  • Server: a Raft member without indexing is refused by config validation and by the builder; LeaderChanged is classified 503 err:db/LeaderChanged with Retry-After; SnapshotInstalled maps to it.
  • Idempotency cache: a key collision leaves the key to the original request, and a keyed retry after LeaderChanged runs again. A recorded-outcome test now expects an unsettled error to release the claim. With the old behaviour restored, all three fail.
  • liveness_monitor_demotes_killed_follower checked that no voter was demoted right after bootstrap, against a 500 ms threshold. With members now indexing, the binary's other clusters starve a voter long enough to be demoted and promoted again, so the precondition waits for the full voter set.

Docs

  • raft-clusters.md: an Indexing section, the CAS table, the apply_index_head route, the rolling-upgrade window, LeaderChanged and the commit-file cleanup in the submission flow.
  • configuration.md: --indexing-enabled is refused on a cluster member.
  • api/errors.md: err:db/LeaderChanged.
  • design/raft-command-queue.md: indexing follows ownership.

Merging into next

next has the same leader-only indexer, plus maintenance holds this PR does not have on main: a reindex or a purge's erasure holds this instance's indexer and the leader's (hold_indexers, set_leader_indexer). With owners building, an owner on a third member is held by neither, so the port must hold the owning member's builds too, or fence the publish against the hold, before it can land there. next's commit worker also has #2488's waits and index_after belongs before its publish in stage_and_advance.

Fixes #1768

…erChanged

A transaction that reached a node which had stopped leading, or whose
wait a new leader's snapshot cut off, answered 503 typed
err:system/InternalError. Both now answer 503 with err:db/LeaderChanged
and Retry-After, through a typed SubmissionError::LeaderChanged that stays
unsettled, so a keyed retry is not recorded as a failure.
…released once one lands

While the leader is unreachable, the owning worker stages its queue entry
again on every retry. Each attempt writes a new commit file, and an
attempt whose publish outcome is unknown keeps its file in case it became
the head, so a long outage left one orphan per retry.

The worker now remembers those files for the entry. Only one commit can
take a queue entry, so once a later attempt of the same worker lands,
the others can never become the head and are released. Pushes and
fast-forwards name commits the worker did not write, so they are not
tracked.
…ough the leader

A cluster's leader built an index after every commit: its indexer
subscribed to every commit event, which no threshold gates, so a raised
--reindex-min-bytes did nothing on a cluster, and it ran even with
--indexing-enabled=false. The other members ran no indexer, and an index
publish from a member that did not lead was dropped as a success.

- A member built with the replicated nameservice now runs its background
  indexer, like a standalone server. The commit worker triggers it once a
  commit brings the branch's novelty to --reindex-min-bytes, or after a
  fast-forward. The trigger is armed before the publish and waits for this
  member to apply the commit, since applying it can stop the worker and
  the indexer reads the head from the replicated state.
- A member that does not lead publishes an index head over
  POST /raft/apply_index_head, which the leader proposes. A leader of an
  earlier release answers 404, and the build is retried.
- The leader's indexer no longer subscribes to commits. It keeps its
  catch-up sweeps for ledgers left behind, and the operator's GC
  retention, so a sweep's build collects as the owners' do.
- A cluster member with --indexing-enabled=false refuses to start: no
  process outside the cluster can publish its indexes.

A test this change loads harder waited for no voter to be demoted right
after bootstrap. Its 500 ms threshold demoted one starved of CPU by the
binary's other clusters, so it now waits for the full voter set.
@bplatz bplatz added bug Something isn't working as expected P1 Next up: wrong results, live user pain, unblocks area:server HTTP surface, routes, error mapping, swagger, timeouts/admission, config graph labels Nov 23, 2026
…ns its log

fluree-server validated its configuration only while building the
server, after it had bootstrapped Raft: a cluster member started with
--indexing-enabled=false opened its log and started as a follower before
it refused. main now validates first.
…y claim

An error that may not be the submission's outcome is not recorded in the
idempotency cache, and the claim was meant to be released. Every caller
then marked the claim done anyway, so it stayed InFlight for the cache
TTL (an hour) and every keyed retry on that node answered AlreadyInFlight.

The worst case was a key collision. A request that reused a key with
another body, on a node whose cache did not hold the key (after a
restart), was refused by the replicated map. Its claim stayed under the
colliding body, and the original request's retries answered
KeyCollision for the hour. A LeaderChanged answer, which tells the client
to retry with the same key, pinned the key the same way.

record_outcome now takes the claim and releases it before returning when
the error is unsettled. A keyed retry runs again: under Raft the
replicated map answers it with the queued entry or its receipt, and a
local execution has nothing left running once it returns.
@bplatz
bplatz requested a review from a team November 23, 2026 18:19

This branch has not been deployed

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

Labels

area:server HTTP surface, routes, error mapping, swagger, timeouts/admission, config graph bug Something isn't working as expected P1 Next up: wrong results, live user pain, unblocks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raft: a non-leader's index publish is swallowed as success, so follower builds are discarded and waiters get a false Completed

1 participant