reckon-db 5.12.1: the leader answers while it activates (#14); a late store joins (#15) #16

Manually merged
terra merged 2 commits from neptunus/14-leader-activation-worker into main 2026-12-02 11:52:04 +00:00
Member

Refs #14, Refs #15

reckon_db 5.12.1: two fixes in the cluster start path, one release (Supervisor ruling: #15 folded into #14). This PR carries the version bump (app.src, README, CHANGELOG), so one land plus a tag ships it.

#14: the leader worker answers is_active/1 while it activates

Activation (save the default subscription, list the subscriptions, start an emitter pool for each, resume the links) ran inside reckon_db_leader's own loop. is_active/1 is a 5 s call to that loop, so on a slow box it timed out for as long as activation ran. The node monitor makes that call every tick, and leadership_transfer_new_leader_serves_existing_subscription waits on it (tag run 6803, job 27753, attempt 1). Hygiea's analysis is in c8259.

The activation now runs in a linked, monitored process of its own, as the DCB re-index already does. Its answer {leadership_activated, Pid, Count} sets active = true only when Pid is the running activation.

r2 (Mercurius r1 FAIL, livelock): an activate asked for while one runs never replaces it. The node monitor ticks every 5 s and asks again while is_active/1 is false, so a replaced slow activation would be killed every tick and never finish. It is kept as ONE rerun (save_defaults wins over subscriptions_only) that starts when the running one answers or crashes.

Races, each with its case in test/unit/reckon_db_leader_tests.erl. Every case holds the activation at a gate in the mocked subscription list and opens it by message, so nothing waits on a sleep:

  • is_active during activation: it answers false in under 1 s while held, and true once done. is_active_answers_while_the_activation_runs. On the 5.12.0 module this case dies on {timeout,{gen_server,call,[…,is_active]}}.
  • Re-transfer or monitor ticks mid-activation: two activates are asked while one is held (one do_activate, then one activate). The running one stays alive, runs to its end (links resumed) and makes the leader active. Then exactly ONE rerun runs, it saves defaults (exists/2 called twice), and no third run starts. activates_asked_while_one_runs_leave_it_running_and_rerun_once. Mutant: the r1 kill-and-replace leader (8ecae78) fails four of this case's asserts.
  • Stale answer: an answer carrying any process other than the running activation sets nothing. an_answer_not_from_the_running_activation_sets_nothing.
  • Worker crash: the leader worker stays up (same pid) and inactive, it is logged as an error, no run starts by itself, and the next activate makes it active. a_crashed_activation_leaves_the_leader_up_and_inactive.
  • Rerun asked before a crash: the rerun starts on its own when the running one crashes, and makes the leader active. a_rerun_asked_before_a_crash_still_runs.

#14 Done-when 3: with a mutant that keeps the gaining leader from starting the subscription's emitter pool (start_emitter(mutant_no_such_store, …)), reckon_db_cluster_join_subscriptions_SUITE:leadership_transfer_new_leader_serves_existing_subscription fails (Failed 1 tests. Passed 0). On the r2 code it passes.

#15: a store started after its node connected joins

A node joined only on a nodeup or an explicit join_cluster/1. A store started after the nodes had connected stayed a cluster of one. The coordinator now starts its own join loop in init/1 (cluster mode only): the existing retry_join, jittered 3-8 s, with the #2 holder guard.

The loop now keeps one pending retry. It uses erlang:start_timer, and the ref is kept in state. A new schedule cancels the old timer, and a fired timer whose ref is not current does nothing. Before, every nodeup or join_cluster/1 answering waiting, no_nodes or coordinator added a chain of its own. With the loop now always running, that would have doubled it on the first nodeup.

Test: test/integration/reckon_db_cluster_join_on_start_SUITE.erl boots A's store, connects B to A, then boots B's store with no join call. Both see one two-member cluster within 60 s. On the 5.12.0 coordinator, after 60 s A's members are [A] and B's are [B].

Trade-off: cluster is the default mode, so a lone cluster-mode node now runs the loop (no_nodes, every 3-8 s, at info level). That is what any nodeup or join_cluster/1 call already started.

Not in this PR

Nothing deactivates the leader worker when the node loses the lead: reckon_db_node_monitor:handle_no_leader/3 only logs. This is unchanged here and outside the ruled scope, so I note it rather than fix it.

Proof

Local gate on acc1aa9 (pinned rebar 3.28.0, OTP 28): xref ok, lint ok, eunit 732/732, ct 302/302 (check_ct.sh; a second run under double load had 1 red in reckon_db_cluster_join_keeps_events_SUITE, reckon-db#17, 3/3 green alone), proper ok, dialyzer ratchet ok. Each new test is red on the 5.12.0 module it guards (logs kept by the builder).

Refs #14, Refs #15 reckon_db **5.12.1**: two fixes in the cluster start path, one release (Supervisor ruling: #15 folded into #14). This PR carries the version bump (app.src, README, CHANGELOG), so one land plus a tag ships it. ## #14: the leader worker answers `is_active/1` while it activates Activation (save the default subscription, list the subscriptions, start an emitter pool for each, resume the links) ran inside `reckon_db_leader`'s own loop. `is_active/1` is a 5 s call to that loop, so on a slow box it timed out for as long as activation ran. The node monitor makes that call every tick, and `leadership_transfer_new_leader_serves_existing_subscription` waits on it (tag run 6803, job 27753, attempt 1). Hygiea's analysis is in c8259. The activation now runs in a linked, monitored process of its own, as the DCB re-index already does. Its answer `{leadership_activated, Pid, Count}` sets `active = true` only when `Pid` is the running activation. **r2 (Mercurius r1 FAIL, livelock):** an activate asked for while one runs never replaces it. The node monitor ticks every 5 s and asks again while `is_active/1` is `false`, so a replaced slow activation would be killed every tick and never finish. It is kept as ONE rerun (`save_defaults` wins over `subscriptions_only`) that starts when the running one answers or crashes. Races, each with its case in `test/unit/reckon_db_leader_tests.erl`. Every case holds the activation at a gate in the mocked subscription list and opens it by message, so nothing waits on a sleep: - **is_active during activation:** it answers `false` in under 1 s while held, and `true` once done. `is_active_answers_while_the_activation_runs`. On the 5.12.0 module this case dies on `{timeout,{gen_server,call,[…,is_active]}}`. - **Re-transfer or monitor ticks mid-activation:** two activates are asked while one is held (one `do_activate`, then one `activate`). The running one stays alive, runs to its end (links resumed) and makes the leader active. Then exactly ONE rerun runs, it saves defaults (`exists/2` called twice), and no third run starts. `activates_asked_while_one_runs_leave_it_running_and_rerun_once`. **Mutant:** the r1 kill-and-replace leader (8ecae78) fails four of this case's asserts. - **Stale answer:** an answer carrying any process other than the running activation sets nothing. `an_answer_not_from_the_running_activation_sets_nothing`. - **Worker crash:** the leader worker stays up (same pid) and inactive, it is logged as an error, no run starts by itself, and the next activate makes it active. `a_crashed_activation_leaves_the_leader_up_and_inactive`. - **Rerun asked before a crash:** the rerun starts on its own when the running one crashes, and makes the leader active. `a_rerun_asked_before_a_crash_still_runs`. **#14 Done-when 3:** with a mutant that keeps the gaining leader from starting the subscription's emitter pool (`start_emitter(mutant_no_such_store, …)`), `reckon_db_cluster_join_subscriptions_SUITE:leadership_transfer_new_leader_serves_existing_subscription` fails (`Failed 1 tests. Passed 0`). On the r2 code it passes. ## #15: a store started after its node connected joins A node joined only on a `nodeup` or an explicit `join_cluster/1`. A store started after the nodes had connected stayed a cluster of one. The coordinator now starts its own join loop in `init/1` (cluster mode only): the existing `retry_join`, jittered 3-8 s, with the #2 holder guard. The loop now keeps **one** pending retry. It uses `erlang:start_timer`, and the ref is kept in state. A new schedule cancels the old timer, and a fired timer whose ref is not current does nothing. Before, every nodeup or `join_cluster/1` answering `waiting`, `no_nodes` or `coordinator` added a chain of its own. With the loop now always running, that would have doubled it on the first nodeup. Test: `test/integration/reckon_db_cluster_join_on_start_SUITE.erl` boots A's store, connects B to A, then boots B's store with no join call. Both see one two-member cluster within 60 s. On the 5.12.0 coordinator, after 60 s A's members are `[A]` and B's are `[B]`. Trade-off: `cluster` is the default mode, so a lone cluster-mode node now runs the loop (`no_nodes`, every 3-8 s, at info level). That is what any nodeup or `join_cluster/1` call already started. ## Not in this PR Nothing deactivates the leader worker when the node loses the lead: `reckon_db_node_monitor:handle_no_leader/3` only logs. This is unchanged here and outside the ruled scope, so I note it rather than fix it. ## Proof Local gate on acc1aa9 (pinned rebar 3.28.0, OTP 28): xref ok, lint ok, eunit 732/732, ct 302/302 (check_ct.sh; a second run under double load had 1 red in reckon_db_cluster_join_keeps_events_SUITE, reckon-db#17, 3/3 green alone), proper ok, dialyzer ratchet ok. Each new test is red on the 5.12.0 module it guards (logs kept by the builder).
reckon-db 5.12.1: the leader answers while it activates (#14); a late store joins (#15)
All checks were successful
lint-and-test / rust (clippy, deny warnings) (pull_request) Successful in 34s
lint-and-test / rust (clippy, deny warnings) (push) Successful in 34s
lint-and-test / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (pull_request) Successful in 8m30s
lint-and-test / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (push) Successful in 9m39s
8ecae78222
#14: leader activation (default subscription, subscription list, emitter
pools, links) runs in a linked, monitored process of its own, as the DCB
re-index does, so is_active/1 (a 5 s call the node monitor makes every
tick) answers at once. Only the current activation's answer sets the
worker active; a newer activation replaces one in flight; a crashed one is
logged and leaves the worker up and inactive for the monitor's re-activate.

#15: a cluster-mode store coordinator starts its join loop when it starts,
so a store started after its node connected to a store-runner joins it with
no join_cluster/1 call. The loop keeps one pending retry (start_timer, ref
in state): a new schedule cancels the old, and a stale timer does nothing.

Tests: test/unit/reckon_db_leader_tests.erl (4 cases, gate in the mocked
subscription list; the first dies on the is_active timeout on 5.12.0) and
test/integration/reckon_db_cluster_join_on_start_SUITE.erl (nodes connect
before the second store starts; on 5.12.0 each stays alone).

Refs #14, Refs #15
reckon-db#14 r2: an activate asked while one runs leaves it running and marks one rerun (Mercurius r1 livelock); tests for it, the crash rerun, and the CHANGELOG
All checks were successful
lint-and-test / rust (clippy, deny warnings) (pull_request) Successful in 34s
lint-and-test / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (pull_request) Successful in 9m0s
lint-and-test / rust (clippy, deny warnings) (push) Successful in 42s
lint-and-test / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (push) Successful in 9m19s
Publish to hex / guard (push) Successful in 4s
Publish to hex / The release guard (push) Successful in 0s
Publish to hex / rust (clippy, deny warnings) (push) Successful in 31s
Publish to hex / Guard and build the package (push) Successful in 36s
Publish to hex / compile + NIFs + xref + lint + eunit + ct + proper + dialyzer (OTP 28.4.3) (push) Successful in 9m28s
Publish to hex / Test the release commit (push) Successful in 0s
Publish to hex / Publish to hex.pm (push) Successful in 30s
acc1aa9418
Refs #14
terra manually merged commit acc1aa9418 into main 2026-12-02 11:52:04 +00:00
Sign in to join this conversation.
No reviewers
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!16
No description provided.