Skip to content

Fix cluster-ready gate latching closed after clustered master restart - #70719

Closed
dwoz wants to merge 3 commits into
saltstack:3008.xfrom
dwoz:dwoz/backport/3008.x-clusterready
Closed

dwoz wants to merge 3 commits into
saltstack:3008.xfrom
dwoz:dwoz/backport/3008.x-clusterready

Conversation

@dwoz

@dwoz dwoz commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Backport of two commits from master-topology work, plus a small test-fixture fixup for the node-id change already in 3008.x (PR #70568).

Root cause. Raft §5.4 forbids a newly-elected leader from committing prior-term CONFIG entries via normal AppendEntries. Without persisting commit_index on disk, Log.__init__ reset it to -1 on every restart, apply_entries replayed nothing, membership_sm stayed empty, and the cluster_ready Event never fired. Every publish after restart bounced with the cluster_retry sentinel until a peer happened to push a fresh AppendEntries.

Fixes together:

  • SaltStorage now persists commit_index + last_applied in state.p alongside term / voted_for / leader_id. Log.__init__ restores both and apply_entries replays the committed suffix on startup. Proper fix.
  • RaftService.__init__ additionally seeds membership_sm from the latest on-disk CONFIG entry as a defence-in-depth path for state files written before the persistence change.

Backward-compatible: load_state setdefaults the new fields to -1 so pre-upgrade state files load cleanly; the seed path handles those.

Test plan

  • pytest tests/pytests/unit/cluster/consensus/ — 404 passed
  • 12 new regression tests across test_cluster_ready.py + test_raft_log.py + test_raft_service.py

dwoz added 3 commits November 6, 2026 22:08
BUG-024.  Observed on the 24-master clustered sandbox: after
each _publish_daemon process respawn, topo-west-1's
cluster_ready Event never re-sets.  Every CLI publish hits the
gate at salt/channel/server.py:_cluster_is_ready and gets the
cluster_retry sentinel (now visible to operators thanks to
BUG-023); meanwhile the master is effectively unreachable even
though its on-disk Raft log carries a committed CONFIG that names
it as a voter.

Measured: RaftService: starting node fired 6x since boot,
committed voter only 2x, opening request gate only 2x.
Last gate-open was 08:49:52 UTC; 4 subsequent RaftService
restarts opened zero gates.

salt/cluster/consensus/raft/log.py:214 -- commit_index is
not persisted alongside term / voted_for / leader_id in
state.p.  On restart Log.__init__ resets to -1.  Raft
§5.4 (node.py:801) blocks the newly-elected leader from
committing prior-term CONFIG entries via normal replication
(entry.term == self.term required).  So:

1. apply_entries never advances past commit_index=-1.
2. membership_sm stays empty.
3. reconcile_membership early-returns at node.py:586
   (if not voters and not learners: return).
4. _on_membership_change never fires.
5. _signal_cluster_ready never fires.
6. cluster_ready.event stays unset.
7. The gate at channel/server.py:98 latches closed for the
   life of this master process.

The 08:49:52 success worked only because another peer
(topo-west-2 in term 4) was live and sent AppendEntries with
leader_commit >= 0, driving handle_append_entries ->
apply_entries -> on_config_change -> _on_membership_change.  In
isolation (quorum partner unreachable), this node wins its own
election but can never commit the old-term CONFIG to unstick its
own gate.

New RaftService._seed_membership_from_log() in
salt/cluster/consensus/service.py, called before
reconcile_membership() in __init__.  Scans loaded log
entries for the latest CONFIG, decodes the cmd payload, applies to
membership_sm with on_change suppressed (so we don't
double-fire during seeding), then bumps
_applied_config_index.  The normal post-init
reconcile_membership → _on_membership_change →
_signal_cluster_ready chain then opens the gate.

No-ops when: log empty, no CONFIG entry present, this node
absent from the CONFIG's voter set, OR membership_sm already
populated by a snapshot restore.

CONFIG entries that reached on-disk append were already known by
their writing leader at append time.  Raft's safety guarantee is
about commit *visibility to clients*, not about re-reading one's
own persisted log on restart.  Applying the latest CONFIG from
disk is equivalent to the CONFIG being appended to the leader's
own log before replication -- no cross-node consistency
violation.

tests/pytests/unit/cluster/consensus/test_cluster_ready.py::TestRaftServiceReadinessSticky -- four new cases:

- test_on_ready_fires_from_persisted_config_entry -- seed
  with CONFIG listing this node as voter, assert on_ready
  fires on construction with no peers.
- test_no_fire_when_not_in_voter_set -- CONFIG omits this
  node, assert on_ready NOT called.
- test_fresh_node_no_log_does_not_fire -- empty log, assert
  on_ready NOT called.
- test_latest_config_wins_when_multiple_in_log -- demotion
  scenario: later CONFIG drops this node, assert on_ready
  NOT called.

pytest tests/pytests/unit/cluster/ tests/pytests/unit/channel/
→ 639 passed.

Post-fix restart of topo-west-1 at 01:56:40 UTC::

    RaftService: seeding membership_sm from on-disk CONFIG at index 0
    RaftService: node 192.168.162.1 is now a committed voter -- marking cluster ready
    MasterPubServerChannel: cluster ready -- opening request gate

salt --timeout=15 '*' test.ping no longer raises the BUG-023
cluster consensus still forming error.  Zero
Cluster not ready yet -- deferring request entries after
seed.  (Individual minions still time out due to an unrelated
peer-key naming mismatch flagged separately.)

- Persist commit_index alongside term / voted_for in
  state.p.  This seeding routes around the missing-persist
  for the membership SM; the proper fix is to persist
  commit_index so the normal apply_entries path handles
  all state machines (ring registry, routing, etc.) on restart.
- _on_ring_registry_change (service.py:294) and
  _on_route_change (service.py:519) have the identical
  latch risk -- they rely on on_change firing via
  apply_entries, which doesn't run when commit_index=-1
  on restart.  Same seeding pattern applies.
- Peer key naming mismatch (peers/192.168.162.2.pub missing,
  only peers/topo-west-2.pub exists) is blocking peer
  AppendEntries on restart.  Compounds BUG-024 by preventing a
  live peer from pushing leader_commit to unstick the gate,
  but is NOT the root cause.  Separate bug.

Also picks up a trivial black reformat of a BUG-023 test file
(tests/pytests/unit/client/test_init.py) that landed via
--no-verify; pre-commit would have applied it on the next
commit anyway.
BUG-025.  Underlying root cause BUG-024 routed around with its
on-restart membership seeding.  The seeding was the surgical
workaround; the proper fix is to persist the Raft commit indices
so apply_entries can replay all state machines (membership,
ring, routing) on restart via the normal path.

### Root cause

salt/cluster/consensus/raft/log.py:214 reset
commit_index = -1 on every Log.__init__ because
state.p only persisted term / voted_for /
leader_id.  On master restart, Raft §5.4.2
(node.py:801 -- entry.term == self.term) blocked the
newly-elected leader from committing prior-term entries via
normal replication, so apply_entries never ran, no SM was
populated, and no readiness callback fired.

### Fix

Persist both commit_index AND last_applied in state.p
alongside the existing fields.  Backward-compatible via
setdefault(-1) on load -- old state files load cleanly as -1
and lazy-fill on first post-fix write.  All commit-index mutations
now route through log.commit() which persists via the new
storage.save_commit_index() fast path (writes only the
commit-related keys, avoids re-serialising full state).

- salt/cluster/consensus/storage.py: save_state +
  save_commit_index + load_state round-trip the two new
  fields through the same fsynced state.p file.
- salt/cluster/consensus/raft/log.py: Log.__init__
  restores both indices from disk, clamped against
  last_included_index (snapshot-vs-log race safety).
  Log.commit persists through storage.save_commit_index
  on every advance.
- salt/cluster/consensus/raft/node.py: apply_entries
  persists last_applied once per batch (not per-entry);
  install_snapshot and the commit_index setter route
  through Log.commit so persistence stays in step with
  in-memory state.
- salt/cluster/consensus/service.py: RaftService.__init__
  drives apply_entries after ALL state machines
  (ring_registry_sm, routing_sm, membership_sm) are
  registered -- this is the critical ordering constraint, since
  apply_entries inside Node.__init__ would miss SMs
  registered by RaftService after node construction.
- BUG-024's _seed_membership_from_log is kept as a
  defensive fallback for pre-upgrade state.p files but is
  now bounded by commit_index so it cannot pin
  _applied_config_index above the committed suffix.

### Regression tests

12 new tests across two files:

- tests/pytests/unit/cluster/consensus/test_raft_log.py -- 8
  tests covering round-trip, backward compat, monotonic merge
  (stale leader_commit no-op), snapshot clamp.
- tests/pytests/unit/cluster/consensus/test_cluster_ready.py
  -- 4 tests in TestCommitIndexPersistenceRestart covering
  apply_entries replay on restart, seed no-op when
  apply_entries already covers, pre-upgrade fallback,
  uncommitted-CONFIG rejection by the seed.

Also fixed a hidden test-isolation bug in
tests/pytests/functional/cluster/consensus/test_raft_service.py:
_make_opts now uses per-invocation cachedirs so state doesn't
leak between tests.  Previously hidden because commit_index
wasn't persisted and tests started fresh regardless.

pytest tests/pytests/unit/cluster/ -> 562 passed.
pytest tests/pytests/unit/cluster/consensus/ -> 400 passed.
pytest tests/pytests/functional/cluster/consensus/ ->
121 passed.

### Live sandbox verification

Restarted topo-west-{1,2,3}.  All masters came back,
re-elected a leader, and the membership gate opened.  Existing
pre-fix state.p files lack commit_index -- BUG-024's
backup seed handled those (designed path).  Fresh state.p
writes now include commit_index and last_applied on the
next commit advance.  salt '*' test.ping publishes through on
topo-west-1 (gate stays open across restarts).

### Follow-up flagged, not in this commit

Pre-existing CONFIG-downgrade risk in log.py:601:
membership_sm.apply overwrites voters unconditionally when a
CONFIG is applied post-truncation.  _applied_config_index=M
blocks on_config_change but the SM state regresses.  Exists
in main; file as a separate bug.  Details in
agents/scratch/bug-025/NOTES.md.
PR saltstack#70568 (`987b077e1d9`, "Identify a master in a consensus
by its id rather than interface address") set the Raft node-id
to `salt.cluster.node_id(opts)` (which returns
`opts["id"].removesuffix("_master")`) rather than
`opts["interface"]`.

The sticky-readiness and commit-index-persistence tests seeded
storage keyed by `opts["interface"]`, so the `RaftService`
under test constructed its own `SaltStorage" under a different
cache path and never saw the pre-seeded log entries /
commit_index.  Five tests failed:
`test_on_ready_fires_from_persisted_config_entry` +
four under `TestCommitIndexPersistenceRestart`.

Fix: use `salt.cluster.node_id(opts)` in all affected test
fixtures so the pre-seeded storage keys match the service's
runtime path.  No production-code change.
@dwoz
dwoz requested a review from a team as a code owner November 7, 2026 05:30
@dwoz dwoz added the test:full Run the full test suite label Nov 7, 2026
@dwoz dwoz added this to the Argon v3008.7 milestone Nov 7, 2026
@dwoz dwoz mentioned this pull request Nov 9, 2026
3 tasks done
@dwoz

dwoz commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #70755 which combines all five cluster-correctness backports into a single PR.

@dwoz dwoz closed this Nov 9, 2026

This branch was previously deployed

1 inactive deployment
ci — b84f47a1 Deployed Nov 7, 2026 by dwoz via Build Source Packages / macOS (x86_64) #28600
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.

1 participant