Skip to content

Fix cluster_ready never fires after restart from persisted Raft state - #70754

Merged
dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/cluster-ready-on-restart
Nov 10, 2026
Merged

dwoz merged 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/cluster-ready-on-restart

Conversation

@dwoz

@dwoz dwoz commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Three cluster masters that bootstrap cleanly on a fresh cachedir leave cluster_ready unset indefinitely after a clean stop + restart with the same cache/consensus / cache/raft state on disk. Minion requests then receive cluster_retry forever.

Root cause is Raft §5.4.2 (the commit restriction for prior-term entries):

  1. On restart, Log.__init__ loads the persisted log entries from storage (including the founding CONFIG at index 0, in some prior term N).
  2. commit_index is not persisted, so it starts back at -1; the MembershipStateMachine is therefore empty.
  3. RaftService.__init__ wires _on_ready and calls reconcile_membership(), but the SM has nothing to reconcile — nothing has been applied.
  4. A new leader is elected in term N+1. Heartbeats bring match_index up to the top of the log, but advance_commit_index refuses to commit index 0 because entry.term (= N) != self.term (= N+1): Raft forbids a leader from committing a prior-term entry by replication count alone.
  5. Nothing new is ever proposed in steady state, so commit_index stays at -1, the membership SM never applies the restored founding CONFIG, _on_ready never fires, and cluster_retry persists.

Fix: Node.become_leader now appends a LogEntryType.NOOP barrier in the new leader's term when the log carries uncommitted prior-term entries. Once the heartbeat path replicates the NOOP to quorum, advance_commit_index can commit it (same-term entry), which by Raft's commit rules transitively commits every prior entry — including the restored founding CONFIG. apply_entries then applies the CONFIG to the MembershipStateMachine, which fires on_change / _on_ready on every voter.

The NOOP has no payload and no state-machine effect; apply_entries skips it. The append is gated on log.index > commit_index AND any prior-term entry so a fresh cluster (empty log) still runs the normal founding-CONFIG path in RaftService._maybe_commit_founding_config (which keys on log.index < 0).

Diff scope:

  • salt/cluster/consensus/raft/log.py: new LogEntryType.NOOP = 6.
  • salt/cluster/consensus/raft/node.py: become_leader appends NOOP barrier; apply_entries handles the NOOP type as a no-op.
  • tests/pytests/functional/cluster/consensus/test_cluster_ready_scenarios.py: new TestClusterReadyOnRestart class.
  • changelog/70754.fixed.md.

Test plan

  • pytest tests/pytests/unit/cluster/ tests/pytests/functional/cluster/ — 724 passed.
  • pytest -k "ready or restart or reconcile or membership" — 136 passed.
  • New TestClusterReadyOnRestart regression:
    • test_cluster_ready_fires_on_restart_from_persisted_log — bootstraps 3 nodes, drives the founding CONFIG commit, rebuilds the ServiceCluster against the same cachedir (simulating a process restart against persisted storage), re-elects, asserts every node fires on_ready, commit_index advances past the founding CONFIG, and the membership SM reflects the restored voter set. Fails without the fix.
    • test_signal_cluster_ready_idempotent — _signal_cluster_ready is safe to call multiple times.
    • test_noop_barrier_appended_on_become_leader_with_prior_term_log — asserts the NOOP is appended when prior-term entries exist and not appended on an empty log (which would permanently suppress the founding-CONFIG path).
  • pre-commit run --files ... clean.

Diagnosis
---------
Three cluster masters that bootstrap cleanly on a fresh cachedir leave
``cluster_ready`` unset indefinitely after a clean stop and restart with
the same ``cache/consensus`` / ``cache/raft`` state on disk.  Minion
requests then receive ``cluster_retry`` forever.

Root cause is Raft §5.4.2 (commit restriction for prior terms):

1. ``Log.__init__`` loads the persisted log entries from storage —
   including the founding CONFIG at index 0 in some prior term N.
2. ``commit_index`` is NOT persisted, so it comes back as -1; the
   ``MembershipStateMachine`` therefore starts empty on restart.
3. ``RaftService.__init__`` wires ``_on_ready`` and calls
   ``reconcile_membership()``, but the SM has nothing to reconcile
   because no entries have been applied yet.
4. A new leader is elected in term N+1.  Heartbeats propagate the
   persisted log entries' indices via ``match_index``, but
   ``advance_commit_index`` refuses to commit index 0 because
   ``entry.term (= N) != self.term (= N+1)``: Raft forbids a leader
   from committing a prior-term entry by replication count alone.
5. In steady state there's nothing new to propose, so no current-term
   entry ever reaches the log, ``commit_index`` stays at -1, the
   membership SM never applies the founding CONFIG, and
   ``_on_ready`` / ``_signal_cluster_ready`` never fires.

Fix
---
``Node.become_leader`` now appends a ``LogEntryType.NOOP`` barrier in
the new leader's term when the log carries uncommitted entries from a
prior term.  Once the heartbeat path replicates the NOOP to quorum,
``advance_commit_index`` can commit it (same-term entry), which by
Raft's commit rules transitively commits every prior entry — including
the restored founding CONFIG.  ``apply_entries`` then applies the
CONFIG to the ``MembershipStateMachine``, which fires ``on_change`` /
``_on_ready`` on every voter.

The NOOP has no payload and no state-machine effect; ``apply_entries``
skips it.  We gate the append on
``log.index > commit_index AND any prior-term entry`` so a fresh
cluster (empty log) still runs the normal founding CONFIG path in
``RaftService._maybe_commit_founding_config`` (which keys on
``log.index < 0``).

Regression coverage in
``tests/pytests/functional/cluster/consensus/test_cluster_ready_scenarios.py::TestClusterReadyOnRestart``:
* End-to-end restart: bootstrap → stop → rebuild services against the
  same cachedir → assert every founding node fires ``on_ready`` again,
  ``commit_index`` advances past the founding CONFIG, and the SM
  reflects the restored voter set.
* ``_signal_cluster_ready`` is idempotent (safe to re-fire).
* ``become_leader`` appends the NOOP barrier on a Node whose log
  already has prior-term entries, and does NOT append one on a Node
  with an empty log.
@dwoz
dwoz requested a review from a team as a code owner November 8, 2026 00:51
@dwoz dwoz added the test:full Run the full test suite label Nov 8, 2026
@dwoz dwoz added this to the Argon v3008.7 milestone Nov 8, 2026
@dwoz
dwoz merged commit 820730a into saltstack:3008.x Nov 10, 2026
490 of 495 checks passed
@dwoz dwoz mentioned this pull request Nov 12, 2026
4 of 6 tasks
@dwoz
dwoz deleted the dwoz/fix/cluster-ready-on-restart branch November 13, 2026 08:30

This branch was previously deployed

1 inactive deployment
ci — a94530ee Deployed Nov 8, 2026 by dwoz via Build Onedir Packages / RPM (x86_64) #28666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants