A cluster join never wipes a store that holds events #2

Closed
opened 2026-12-02 05:32:35 +00:00 by neptunus · 2 comments
Member

Purpose (fix): in cluster mode, a cluster join never wipes a store that holds events. This includes two standalone boxes meeting on a LAN, whatever their node names are.

Refs martha-ai/mcl-martha#1 (step 1b of A3 in the LAN addendum). No service switches to mode = cluster before this lands.

The owner's words

  • DECIDED 2026-12-02 "trust on sight. mcl-martha services should auto-cluster over LAN, no mcl-station required. Distributed Erlang offers us all we need." (Raf)
  • DECIDED 2026-12-02 "Three for failover (Recommended)" (Raf, mcl-martha#1 LAN addendum menu)
  • DECIDED 2026-12-02 "Yes, start 1 and 1b (Recommended)" (Raf, same menu)

The harm

In cluster mode, a nodeup runs reckon_db_store_coordinator's cold-start election.

  • The store-runner with the lowest node name becomes coordinator (src/reckon_db_store_coordinator.erl:303-338).
  • Every other node calls khepri_cluster:join, which resets the joiner's local tree (:354-384).
  • The persisted-membership guard (:238-263) protects only a node whose configured member set is already larger than one.

So a standalone node that holds events wipes its own store as soon as it meets a node whose name sorts lower. Which node loses its data is decided by the order of the names, not by which node holds the data.

The same reset happens on the other two join paths:

  • a standalone holder that finds an existing multi-member cluster (handle_cluster_nodes/3);
  • the explicit join_cluster/2.

Threat model

  • Asset: the events in a store, the only copy on a standalone box.
  • Who causes the harm: no attacker is needed. Two boxes of our own meet (Raf's LAN direction: auto-cluster on sight), or a box with a lower name is added. An outside attacker on the LAN would need the shared cookie and gossip secret (DECIDED "Shared cookie + secret"). Once they have those, they own distributed Erlang anyway; this fix does not try to defend against that.
  • What the fix guarantees: before any khepri_cluster:join, the joining node checks its own store and refuses to join while it holds at least one event. The check runs on the node that would lose the data, so a wrong or unreachable answer from the peer can never cause a wipe; the worst it can cause is that no cluster forms.
  • Fail closed: if the local store cannot be read (no answer, an error), the join does not happen and the next retry tick asks again.
  • Liveness, not safety: in the election, nodes that hold events rank first, so when one node holds data and the other is empty, the holder coordinates and the empty node joins it. When both hold events, the joining node refuses and logs one error that names both nodes; it does not retry that join on its own. Merging two event histories is out of scope.
  • Out of scope: the healer's reset_and_rejoin (reckon_db_store_healer.erl). It acts only on a replica that is already in a multi-member configuration and has been orphaned; it deliberately clears that replica first, and the majority still holds the data. After its reset the store is empty, so the new guard lets its rejoin through unchanged.

Done when

  1. A node whose store holds at least one event never calls khepri_cluster:join. This holds on all three paths: the cold-start election, a found multi-member cluster, and join_cluster/2. The node logs the refusal, naming the store and the target node.
  2. If the local store's event check cannot answer, no join happens (the attempt counts as failed and is retried).
  3. In the cold-start election, a node that holds events ranks above every empty node, and node names only break ties. The unit tests cover holder-high, holder-low, both holding and none holding.
  4. A two-node common_test case: the higher-named node holds events and the lower-named one is empty. Both boot in cluster mode and meet. They end up as one two-member cluster, and every event the higher node held can be read on both nodes.
  5. A two-node common_test case: both nodes hold events and meet. Neither store loses an event, and the refusal is logged.
  6. Mutants on the guard (drop the check, invert it, treat an unreadable store as empty, drop the holder ranking) each turn a test red.
  7. The existing suites pass on OTP 28: rebar3 eunit and scripts/check_ct.sh. No test reaches an outside host.
Purpose (fix): in cluster mode, a cluster join never wipes a store that holds events. This includes two standalone boxes meeting on a LAN, whatever their node names are. Refs martha-ai/mcl-martha#1 (step 1b of A3 in the LAN addendum). No service switches to `mode = cluster` before this lands. ## The owner's words - DECIDED 2026-12-02 "trust on sight. mcl-martha services should auto-cluster over LAN, no mcl-station required. Distributed Erlang offers us all we need." (Raf) - DECIDED 2026-12-02 "Three for failover (Recommended)" (Raf, mcl-martha#1 LAN addendum menu) - DECIDED 2026-12-02 "Yes, start 1 and 1b (Recommended)" (Raf, same menu) ## The harm In cluster mode, a nodeup runs `reckon_db_store_coordinator`'s cold-start election. - The store-runner with the **lowest node name** becomes coordinator (`src/reckon_db_store_coordinator.erl:303-338`). - Every other node calls `khepri_cluster:join`, which **resets the joiner's local tree** (`:354-384`). - The persisted-membership guard (`:238-263`) protects only a node whose configured member set is already larger than one. So a standalone node that holds events wipes its own store as soon as it meets a node whose name sorts lower. Which node loses its data is decided by the order of the names, not by which node holds the data. The same reset happens on the other two join paths: - a standalone holder that finds an existing multi-member cluster (`handle_cluster_nodes/3`); - the explicit `join_cluster/2`. ## Threat model - **Asset:** the events in a store, the only copy on a standalone box. - **Who causes the harm:** no attacker is needed. Two boxes of our own meet (Raf's LAN direction: auto-cluster on sight), or a box with a lower name is added. An outside attacker on the LAN would need the shared cookie and gossip secret (DECIDED "Shared cookie + secret"). Once they have those, they own distributed Erlang anyway; this fix does not try to defend against that. - **What the fix guarantees:** before any `khepri_cluster:join`, the joining node checks its own store and refuses to join while it holds at least one event. The check runs on the node that would lose the data, so a wrong or unreachable answer from the peer can never cause a wipe; the worst it can cause is that no cluster forms. - **Fail closed:** if the local store cannot be read (no answer, an error), the join does not happen and the next retry tick asks again. - **Liveness, not safety:** in the election, nodes that hold events rank first, so when one node holds data and the other is empty, the holder coordinates and the empty node joins it. When both hold events, the joining node refuses and logs one error that names both nodes; it does not retry that join on its own. Merging two event histories is out of scope. - **Out of scope:** the healer's `reset_and_rejoin` (`reckon_db_store_healer.erl`). It acts only on a replica that is already in a multi-member configuration and has been orphaned; it deliberately clears that replica first, and the majority still holds the data. After its reset the store is empty, so the new guard lets its rejoin through unchanged. ## Done when 1. A node whose store holds at least one event never calls `khepri_cluster:join`. This holds on all three paths: the cold-start election, a found multi-member cluster, and `join_cluster/2`. The node logs the refusal, naming the store and the target node. 2. If the local store's event check cannot answer, no join happens (the attempt counts as failed and is retried). 3. In the cold-start election, a node that holds events ranks above every empty node, and node names only break ties. The unit tests cover holder-high, holder-low, both holding and none holding. 4. A two-node common_test case: the **higher**-named node holds events and the lower-named one is empty. Both boot in cluster mode and meet. They end up as one two-member cluster, and every event the higher node held can be read on both nodes. 5. A two-node common_test case: both nodes hold events and meet. Neither store loses an event, and the refusal is logged. 6. Mutants on the guard (drop the check, invert it, treat an unreadable store as empty, drop the holder ranking) each turn a test red. 7. The existing suites pass on OTP 28: `rebar3 eunit` and `scripts/check_ct.sh`. No test reaches an outside host.
Author
Member

Threat model, appended while building (the body above is unchanged):

  • The guard defines "holds events" as any node under the streams root (khepri:count(Store, [streams, *])). Every appended event lives under one, and a fresh store has none, since init_store_paths creates only the root. So Done-when 1 holds: a store with at least one event is always counted as holding data.
  • The guard also counts a stream whose events were all deleted. That errs on the side that keeps data: such a store refuses to join and stays standalone.
  • Why not read one event (reckon_db_streams:has_events/1): that goes through read_all_global's index, which is a full scan-and-sort whenever its 30 s cache is cold. The election reruns every 3-8 s on a coordinator that is still standalone. In the "both hold events" case that is forever, so the cost would never stop. Counting the root's children costs about as much as the number of stream types.
  • The peer check uses the same count over rpc (khepri:count), so it needs nothing new on the peer's side.
Threat model, appended while building (the body above is unchanged): - The guard defines "holds events" as **any node under the streams root** (`khepri:count(Store, [streams, *])`). Every appended event lives under one, and a fresh store has none, since `init_store_paths` creates only the root. So Done-when 1 holds: a store with at least one event is always counted as holding data. - The guard also counts a stream whose events were all deleted. That errs on the side that keeps data: such a store refuses to join and stays standalone. - Why not read one event (`reckon_db_streams:has_events/1`): that goes through `read_all_global`'s index, which is a full scan-and-sort whenever its 30 s cache is cold. The election reruns every 3-8 s on a coordinator that is still standalone. In the "both hold events" case that is forever, so the cost would never stop. Counting the root's children costs about as much as the number of stream types. - The peer check uses the same count over rpc (`khepri:count`), so it needs nothing new on the peer's side.
Author
Member

Landed: main = 9d8ce2a (fast-forward from af75fd49, PR 3, by Terra). Done-when, against what landed:

  1. A node whose store holds events never calls khepri_cluster:join on any of the three paths (cold-start election, found multi-member cluster, join_cluster/2). The refusal is logged with the store and the target. Guard join_unless_holding in reckon_db_store_coordinator. Jupiter PASS r=1.
  2. An event check that cannot answer means no join: the attempt counts as failed and is retried (unit tests on join_action).
  3. The election ranks holders above empty nodes, and names only break ties. reckon_db_store_coordinator_election_tests covers holder-high, holder-low, both holding and none holding.
  4. CT reckon_db_cluster_join_keeps_events_SUITE: the higher-named holder plus an empty lower node form one two-member cluster, and every event can be read on both nodes.
  5. Same suite: when both stores hold events, neither loses one, and the refusal is logged.
  6. Mutants on the guard: 10/10 killed (drop check, invert, unreadable-as-empty, drop holder ranking, plus the strengthened unknown_joins/refused_retries).
  7. Local gate on 4bd17ca, OTP 28: xref, lint, eunit 712, ct 300, proper, dialyzer, all green. On the forge (9d8ce2a), push run 21 and PR run 22 passed everything except one case, reckon_db_concurrent_append_SUITE twenty_writers_on_one_stream_keep_every_event. That case also fails on main without #2, and Mars ruled those two jobs accepted reds under #7. Both runs passed the new join suites. No test reaches an outside host.
Landed: main = 9d8ce2a (fast-forward from af75fd49, PR 3, by Terra). Done-when, against what landed: 1. A node whose store holds events never calls `khepri_cluster:join` on any of the three paths (cold-start election, found multi-member cluster, `join_cluster/2`). The refusal is logged with the store and the target. Guard `join_unless_holding` in reckon_db_store_coordinator. Jupiter PASS r=1. 2. An event check that cannot answer means no join: the attempt counts as failed and is retried (unit tests on `join_action`). 3. The election ranks holders above empty nodes, and names only break ties. reckon_db_store_coordinator_election_tests covers holder-high, holder-low, both holding and none holding. 4. CT reckon_db_cluster_join_keeps_events_SUITE: the higher-named holder plus an empty lower node form one two-member cluster, and every event can be read on both nodes. 5. Same suite: when both stores hold events, neither loses one, and the refusal is logged. 6. Mutants on the guard: 10/10 killed (drop check, invert, unreadable-as-empty, drop holder ranking, plus the strengthened unknown_joins/refused_retries). 7. Local gate on 4bd17ca, OTP 28: xref, lint, eunit 712, ct 300, proper, dialyzer, all green. On the forge (9d8ce2a), push run 21 and PR run 22 passed everything except one case, `reckon_db_concurrent_append_SUITE` `twenty_writers_on_one_stream_keep_every_event`. That case also fails on main without #2, and Mars ruled those two jobs accepted reds under #7. Both runs passed the new join suites. No test reaches an outside host.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
reckon-db-org/reckon-db#2
No description provided.