A3 package 4: each mesh fact goes out once per cluster (mcl_om_singleton) #5

Closed
opened 2026-12-02 09:14:17 +00:00 by venus · 2 comments
Member

Purpose (feature): in a clustered mcl-martha, each whiteboard mesh fact goes out once per cluster, not once per node. Read models stay per node, and the standalone release behaves exactly as it does today.

Raf, 2026-12-02: "Run the whole path (Recommended)"
Raf, 2026-12-02: "Three for failover (Recommended)"

Refs martha-ai/mcl-martha#1, macula-services/mcl-whiteboard#3, macula-services/mcl-om#8
Consolidation analysis §A1 (side effects run once per node), A3 package 4.

The side effect

GuideBoardLifecycle.Supervisor starts three evoq event handlers that republish domain events as mesh facts through :mcl_om_pubsub.publish/3:

  • BoardLifecycleV1ToMesh (board initiated/hosted/archived/unarchived/renamed),
  • ShapeLifecycle.ShapeLifecycleV1ToMesh (shape initiated/amended/removed),
  • LeaveBoard.PeerDepartedV1ToMesh (peer departed). The analysis names only the first, but the other two have the same shape.

evoq delivers events only to handlers on the local node (evoq_event_router.erl:181). On a cluster whose store replicates, every node receives every event, so every node would publish each fact once.

What changes

  • Each of the three handlers is started through mcl_om_singleton:child_spec/2 (hex mcl_om 1.9.0, pinned ~> 1.9), named after its module. Exactly one node of a connected cluster runs it. When that node goes away, a standby on another node starts it.
  • Why the handler and not the publisher. The publish is a plain function call inside the handler callback, and whiteboard has no publisher process of its own. A singleton publisher would still be called once per node by every node's handler. So the handler process is the thing to wrap: on a standby node no handler is registered for these event types, and nothing is published.
  • The read-model projections (ProjectWhiteboardBoards.*) and every mesh subscriber are unchanged. They stay per node.
  • On a node without distribution (the standalone release), the guard takes the name and runs the handler, as before.

A double run, and a gap

  • During a split. global sees only connected nodes, so each connected partition runs its own copy, and each publishes the facts of events that its node receives. When the partitions meet again, mcl_om_singleton:resolve/3 keeps one copy. No harm: every receiver is idempotent. Board facts are upserts keyed by board_id, where nil means no update. shape_initiated_v1 is deduplicated by Store.new_shape?/1. shape_amended_v1 sets the same points again. shape_removed_v1 and peer_departed_v1 remove something that is already gone. On a cluster-mode store, the minority side of a split cannot append (Ra quorum), so in practice it has nothing new to publish.
  • During a handover. Events that arrive between the holder's loss and the standby's start are not republished (replay_policy: :skip, and a fresh handler gets only new events). This is the same loss as a dark mesh today. Peers catch up through the board list and snapshot queries on their next page load. No guard is added.
  • Before A3 package 2 (store mode from config). The store is still mode: :single, so each clustered node holds its own events and the one holder publishes only its own node's events. A board on a standby node is not announced live: peers see it only on their next query. This holds only for a cluster that A3 package 2 has not yet made replicated, and no such release ships. Package 2 closes it.

Done when

  1. The three *ToMesh handlers start through mcl_om_singleton:child_spec/2, and the mcl_om pin in the umbrella apps is ~> 1.9 (hex 1.9.0, not a git ref).
  2. A peer-node test: on two connected nodes that both run GuideBoardLifecycle.Supervisor, exactly one node has a handler registered for each published event type. After that node stops, the other node has it.
  3. A mutant that starts the handlers plainly again (no singleton) fails that test.
  4. Standalone: on a node without distribution, the handlers run and an event is published (existing tests stay green).
  5. mix format --check-formatted, mix compile --warnings-as-errors, mix credo, mix test and mix dialyzer are green on OTP 28 / Elixir 1.18.4, in the forge gates job.

Not in scope: store mode from config (A3 package 2, which waits for the reckon-db minor), and anything else on whiteboard (paused except for path work).

Purpose (feature): in a clustered mcl-martha, each whiteboard mesh fact goes out once per cluster, not once per node. Read models stay per node, and the standalone release behaves exactly as it does today. Raf, 2026-12-02: "Run the whole path (Recommended)" Raf, 2026-12-02: "Three for failover (Recommended)" Refs martha-ai/mcl-martha#1, macula-services/mcl-whiteboard#3, macula-services/mcl-om#8 Consolidation analysis §A1 (side effects run once per node), A3 package 4. ## The side effect `GuideBoardLifecycle.Supervisor` starts three evoq event handlers that republish domain events as mesh facts through `:mcl_om_pubsub.publish/3`: - `BoardLifecycleV1ToMesh` (board initiated/hosted/archived/unarchived/renamed), - `ShapeLifecycle.ShapeLifecycleV1ToMesh` (shape initiated/amended/removed), - `LeaveBoard.PeerDepartedV1ToMesh` (peer departed). The analysis names only the first, but the other two have the same shape. evoq delivers events only to handlers on the local node (`evoq_event_router.erl:181`). On a cluster whose store replicates, every node receives every event, so every node would publish each fact once. ## What changes - Each of the three handlers is started through `mcl_om_singleton:child_spec/2` (hex mcl_om 1.9.0, pinned `~> 1.9`), named after its module. Exactly one node of a connected cluster runs it. When that node goes away, a standby on another node starts it. - **Why the handler and not the publisher.** The publish is a plain function call inside the handler callback, and whiteboard has no publisher process of its own. A singleton publisher would still be called once per node by every node's handler. So the handler process is the thing to wrap: on a standby node no handler is registered for these event types, and nothing is published. - The read-model projections (`ProjectWhiteboardBoards.*`) and every mesh subscriber are unchanged. They stay per node. - On a node without distribution (the standalone release), the guard takes the name and runs the handler, as before. ## A double run, and a gap - **During a split.** `global` sees only connected nodes, so each connected partition runs its own copy, and each publishes the facts of events that its node receives. When the partitions meet again, `mcl_om_singleton:resolve/3` keeps one copy. **No harm:** every receiver is idempotent. Board facts are upserts keyed by `board_id`, where `nil` means no update. `shape_initiated_v1` is deduplicated by `Store.new_shape?/1`. `shape_amended_v1` sets the same points again. `shape_removed_v1` and `peer_departed_v1` remove something that is already gone. On a cluster-mode store, the minority side of a split cannot append (Ra quorum), so in practice it has nothing new to publish. - **During a handover.** Events that arrive between the holder's loss and the standby's start are not republished (`replay_policy: :skip`, and a fresh handler gets only new events). This is the same loss as a dark mesh today. Peers catch up through the board list and snapshot queries on their next page load. No guard is added. - **Before A3 package 2 (store mode from config).** The store is still `mode: :single`, so each clustered node holds its own events and the one holder publishes only its own node's events. A board on a standby node is not announced live: peers see it only on their next query. This holds only for a cluster that A3 package 2 has not yet made replicated, and no such release ships. Package 2 closes it. ## Done when 1. The three `*ToMesh` handlers start through `mcl_om_singleton:child_spec/2`, and the mcl_om pin in the umbrella apps is `~> 1.9` (hex 1.9.0, not a git ref). 2. A peer-node test: on two connected nodes that both run `GuideBoardLifecycle.Supervisor`, exactly one node has a handler registered for each published event type. After that node stops, the other node has it. 3. A mutant that starts the handlers plainly again (no singleton) fails that test. 4. Standalone: on a node without distribution, the handlers run and an event is published (existing tests stay green). 5. `mix format --check-formatted`, `mix compile --warnings-as-errors`, `mix credo`, `mix test` and `mix dialyzer` are green on OTP 28 / Elixir 1.18.4, in the forge `gates` job. Not in scope: store mode from config (A3 package 2, which waits for the reckon-db minor), and anything else on whiteboard (paused except for path work).
Author
Member

Held until A3 package 2. Not pushed.

Mars (2026-12-02), after Jupiter's finding on scribe: the wrap is right only once whiteboard's store replicates. The code confirms this: MclWhiteboard.Service.event_store/0 still opens mode: :single (apps/mcl_whiteboard/lib/mcl_whiteboard/service.ex:124), so each node has its own store. The *ToMesh handlers project only their own node's store. Today each node publishing its own events is correct and is not a double. Under a singleton held on node A, events written on node B would never be published. The third bullet of "A double run, and a gap" above names this. It is a reason to hold the wrap, not an interim cost. This wrap lands with whiteboard's A3 package 2 (store mode from config), in the same range.

The built work is kept on the local branch venus/a3-mesh-singleton at 71c944f (worktree mcl-whiteboard-a3-mesh-singleton), on base 509c6c5. It contains:

  • the three emitters under mcl_om_singleton, with the pin at ~> 1.9;
  • test/one_node_publishes_test.exs: a standalone case, and two peer nodes with exactly one handler per event type plus the takeover;
  • a mutant (plain handlers) that fails both cases.

Local results on OTP 28.4.3 / Elixir 1.18.4: format, compile --warnings-as-errors, credo, the whole mix test (126 tests, 0 failures) and dialyzer are all green.

Correction to "During a handover" above: a takeover makes a burst, not a gap. I read this in evoq 1.32 evoq_store_subscription.erl (module doc "Registrants", and pending/3 + backfill/5). A handler that registers after the store subscription has started is backfilled from from (0) up to the subscription's current position, and only then becomes active. Below max(SeenUpTo, Reach[identity]), events are marked replay. SeenUpTo is that node's checkpoint at boot. Reach is 0 on a node that has never run this handler. So when the standby takes over:

  • events appended since the standby's node booted are delivered as new and republished. A node with a long uptime can republish a large burst;
  • events from before its boot are replay, and replay_policy: :skip drops them;
  • nothing is missed between the holder's loss and the standby's start, because those events sit in the standby's store and fall inside the backfill.

The burst is harmless: every receiver is idempotent (see above). A gap remains only in one narrow case: the holder dies while the standby's own node is itself down. Events the standby consumed before its own last restart that the holder had not yet published are then replay and are skipped. A node that took over before and later takes over again has its Reach set, so it republishes only what has arrived since it last held the handler.

**Held until A3 package 2. Not pushed.** Mars (2026-12-02), after Jupiter's finding on scribe: the wrap is right only once whiteboard's store replicates. The code confirms this: `MclWhiteboard.Service.event_store/0` still opens `mode: :single` (`apps/mcl_whiteboard/lib/mcl_whiteboard/service.ex:124`), so each node has its own store. The `*ToMesh` handlers project only their own node's store. Today each node publishing its own events is correct and is not a double. Under a singleton held on node A, events written on node B would never be published. The third bullet of "A double run, and a gap" above names this. It is a reason to hold the wrap, not an interim cost. **This wrap lands with whiteboard's A3 package 2 (store mode from config), in the same range.** The built work is kept on the local branch `venus/a3-mesh-singleton` at `71c944f` (worktree `mcl-whiteboard-a3-mesh-singleton`), on base `509c6c5`. It contains: - the three emitters under `mcl_om_singleton`, with the pin at `~> 1.9`; - `test/one_node_publishes_test.exs`: a standalone case, and two peer nodes with exactly one handler per event type plus the takeover; - a mutant (plain handlers) that fails both cases. Local results on OTP 28.4.3 / Elixir 1.18.4: format, compile `--warnings-as-errors`, credo, the whole `mix test` (126 tests, 0 failures) and dialyzer are all green. **Correction to "During a handover" above: a takeover makes a burst, not a gap.** I read this in evoq 1.32 `evoq_store_subscription.erl` (module doc "Registrants", and `pending/3` + `backfill/5`). A handler that registers after the store subscription has started is backfilled from `from` (0) up to the subscription's current position, and only then becomes active. Below `max(SeenUpTo, Reach[identity])`, events are marked replay. `SeenUpTo` is that node's checkpoint at boot. `Reach` is 0 on a node that has never run this handler. So when the standby takes over: - events appended since the standby's node booted are delivered as new and republished. A node with a long uptime can republish a large burst; - events from before its boot are replay, and `replay_policy: :skip` drops them; - nothing is missed between the holder's loss and the standby's start, because those events sit in the standby's store and fall inside the backfill. The burst is harmless: every receiver is idempotent (see above). A gap remains only in one narrow case: the holder dies while the standby's own node is itself down. Events the standby consumed before its own last restart that the holder had not yet published are then replay and are skipped. A node that took over before and later takes over again has its `Reach` set, so it republishes only what has arrived since it last held the handler.
Author
Member

Done on main 9c6fe7c (PR #7, landed together with #6). Main runs 6914 (c4) and 6915 (gates) are green. Each Done-when item:

  1. The three *ToMesh handlers start through mcl_om_singleton:child_spec/2 in cluster mode (71c944f). #6 then made that depend on the store mode: in single mode each node's store holds only its own events, so each node runs plain handlers. mcl_om pin ~> 1.9 (hex 1.9.0) in the umbrella apps.
  2. one_node_publishes_test.exs, cluster mode: one handler per type across two connected peers, and it moves when its node stops.
  3. Mutant: mode fixed to single (plain handlers) fails that test.
  4. Standalone/single mode: the handlers run as plain children, and the existing tests stay green.
  5. Local gates green, and the forge gates job is green: PR runs 6900/6901 on 9c6fe7c, main 6915.
Done on main 9c6fe7c (PR #7, landed together with #6). Main runs 6914 (c4) and 6915 (gates) are green. Each Done-when item: 1. The three `*ToMesh` handlers start through `mcl_om_singleton:child_spec/2` **in cluster mode** (71c944f). #6 then made that depend on the store mode: in single mode each node's store holds only its own events, so each node runs plain handlers. mcl_om pin `~> 1.9` (hex 1.9.0) in the umbrella apps. 2. `one_node_publishes_test.exs`, cluster mode: one handler per type across two connected peers, and it moves when its node stops. 3. Mutant: mode fixed to single (plain handlers) fails that test. 4. Standalone/single mode: the handlers run as plain children, and the existing tests stay green. 5. Local gates green, and the forge gates job is green: PR runs 6900/6901 on 9c6fe7c, main 6915.
venus closed this issue 2026-12-02 13:17:30 +00:00
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
macula-services/mcl-whiteboard#5
No description provided.