26 KiB
Design Document: ha-peer-connection-reuse
Overview
The XCE controller election currently discards outgoing vote connections after the vote exchange, forcing standbys to create new outbound connections to the primary for the persistent control channel. This produces a 10-second heartbeat timeout delay before standbys detect the primary, spurious retry warnings from premature config-fetch attempts, and a complex tier-1/2/3 primary discovery path.
This spec reuses each candidate's outgoing vote endpoint as the persistent peer control channel: the election winner promotes its connections to replication channels; the loser closes its connections and waits passively for the primary's ctrlhello to arrive on its listener.
The entire change is confined to ha.c and cluster.c. iomgr, the capnp wire protocol, and the raft library are not modified.
Goals
- Eliminate the double-connection round-trip after election
- Remove the standby's active primary discovery path (fetch_config_from_primary)
- Arm the standby heartbeat watchdog immediately when CTRL_STANDBY is entered
- Preserve existing HA behaviors: failover, WAL replication, config push, heartbeat detection
Non-Goals
- SSL/TLS certificate management changes
- Vote grant/deny protocol logic changes
- WAL entry format or replication content changes
- Config file format changes
- Cluster membership changes (add/remove peers)
Boundary Commitments
This Spec Owns
- The lifecycle of outgoing vote endpoints from creation through promotion or close
- The
peer_ctrl_msg_handlerstate-aware dispatcher (replacesvote_reply_msg_handler+ explicit reassignment) - Population of
ha->standby_endpointsfrom promoted vote connections on the primary side - Arming the standby heartbeat watchdog from
raft_cb_become_followerinstead of afterha_find_and_connect_to_primary - Setting
ha->primary_ctrl_fdfrom the inbound ctrlhello connection on the standby side - Removing the
fetch_config_from_primarydelay task from the standby path
Out of Boundary
- SSL/TLS negotiation details — handled by existing
client_ssl_ctx/on_vote_connectedpath - iomgr internals — zero changes to
src/lib/netio.corsrc/include/netio.h - Raft library — zero changes to
src/lib/raft/raft.c - Heartbeat watchdog thread implementation — only the arming callsite changes
- The existing
ha_find_and_connect_to_primaryfunction — originally scoped to remain for future use; drifted: it was removed entirely during implementation (exceeds this design's original intent — see this spec's own research.md "Gap Re-Audit 2026-06-18", finding R4). The standby path no longer calls it, and the function no longer exists in the tree.
Allowed Dependencies
ha_state_t,ha_elect_ctx_t,ha_vote_entry_t— extended with new fieldsvote_ep_ctx_t— reused as-is for promoted connectionsendpoint_t,iomgr_monitor,endpoint_set_msg_handler— used unchangedha->standby_endpoints(zarray) — populated from promoted connectionsraft_cb_become_leader/raft_cb_become_follower— extended to populate/close endpointsha_arm_heartbeat_timeout_vtask— called fromraft_cb_become_follower
Revalidation Triggers
- Changing
ha_vote_entry_tstruct layout (tasks depending onvotes[].ep) - Changing
vote_ep_ctx_tfields or lifetime contract - Changing
raft_cb_become_leader/raft_cb_become_followersignatures - Adding new roles to
ctrl_role_t(the dispatcher switch must be updated) - Changing
ha->standby_endpointspopulation semantics
Architecture
Existing Architecture Analysis
The current election flow:
- Candidate creates one
endpoint_tper peer (outgoing vote), registers with iomgr on_vote_connectedsends the vote request and reassignsep->msg_handler = vote_reply_msg_handler- After the election, the outgoing vote endpoints are closed by
ha_election_cleanup - The winning standby calls
ha_find_and_connect_to_primaryto create a new inbound connection to the primary - The heartbeat watchdog is armed after this reconnection succeeds
The promoted-connection flow (this spec):
- Same vote endpoint creation
on_vote_connectedsetsep->msg_handler = peer_ctrl_msg_handler(state-aware, never reassigned)- In
raft_cb_become_leader: each surviving vote endpoint is added toha->standby_endpoints(primary) - In
raft_cb_become_follower: outgoing vote endpoints are closed; standby waits passively; watchdog armed immediately - Primary's ctrlhello arrives on the standby's listener, setting
ha->primary_ctrl_fd
Architecture Pattern & Boundary Map
graph TD
subgraph Election_Phase
A[raft_cb_send_vote_request] --> B[endpoint_connect + iomgr_monitor]
B --> C[on_vote_connected sets peer_ctrl_msg_handler]
C --> D[send RequestVote via xce_request]
end
subgraph Election_Result
E[raft_cb_become_leader] --> F[promote: add ep to standby_endpoints]
G[raft_cb_become_follower] --> H[close: endpoint_fini each vote ep]
G --> I[arm heartbeat watchdog]
end
subgraph Promoted_Connection_PRIMARY
F --> J[peer_ctrl_msg_handler role=PRIMARY]
J --> K[repl_ack_decode_and_dispatch: no-op ack, INFO log]
end
%% Note: CTRL_PRIMARY_COMMITTING was removed (superseded by ha-consistency-invariants /
%% ha-single-leader-role, 2026-06-11); ha_repl_ack_received no longer exists -- promotion
%% is now driven by the heartbeat decision loop + commit engine, not ack-counting on this ep.
subgraph Standby_Passive
H --> L[wait for ctrlhello on listener]
L --> M[handle_ctrlhello sets primary_ctrl_fd]
I --> N[watchdog arm: fires on silence timeout]
end
subgraph Dispatcher
D --> O[peer_ctrl_msg_handler]
O -- role=ELECTING --> P[vote_reply_decode_and_dispatch]
O -- role=PRIMARY --> K
O -- role=STANDBY + PreVote round --> R[pre_vote_reply_decode_and_dispatch]
O -- other role --> Q[drop packet]
end
%% pre_vote_reply_decode_and_dispatch branch added later by ha-prevote (2026-07-18) --
%% shares this dispatcher but was not part of this spec's original scope.
Technology Stack
| Layer | Choice | Role | Notes |
|---|---|---|---|
| HA election | ha.c (C, gnu17) | Endpoint lifecycle, dispatcher | All changes here |
| Cluster runtime | cluster.c (C) | standby_endpoints population, ctrlhello | Minor additions |
| iomgr | netio.c (C) | Unchanged | No modifications |
| Raft library | lib/raft/raft.c | Unchanged | No modifications |
File Structure Plan
Modified Files
src/controller/ha.c— All behavioral changes:peer_ctrl_msg_handler,vote_ep_ctx_tpromoted lifetime,ha_vote_entry_t.eptracking,raft_cb_become_leaderpromotion path,raft_cb_become_followerpassive path + watchdog arm, removefetch_config_from_primarydelay tasksrc/controller/ha.h— Addendpoint_t *eptoha_vote_entry_t; addpeer_ctrl_msg_handlerforward declaration or make it accessible via a new test-seam hook; update comments onha_find_and_connect_to_primarynoting standby post-election path no longer calls itsrc/controller/config_repl.c—handle_ctrlhello: setha->primary_ctrl_fd = ep->fdwhen role == CTRL_STANDBY and ctrlhello arrives for the first time (Note: this design originally citedcluster.c;handle_ctrlhelloactually lives inconfig_repl.c— drift already flagged in this spec's own research.md R4, corrected here)src/controller/testsuite/test_ha_connection_reuse.c— Tests for: dispatcher routes by role, winner promotes endpoints, loser closes endpoints, watchdog armed on CTRL_STANDBY, standby sets primary_ctrl_fd on ctrlhello (Note: implemented under this filename, nottest_ha_election.cas originally planned — that file is a pre-existing, unrelated quorum-election test suite)
System Flows
Election + Promotion (Winner Path)
sequenceDiagram
participant C as Candidate ha.c
participant I as iomgr
participant P as Peer Standby
C->>I: endpoint_connect + iomgr_monitor (per peer)
I->>C: on_vote_connected fires
C->>P: xce_request(OPCode_requestVote)
note over C: ep->msg_handler = peer_ctrl_msg_handler
P->>C: RequestVoteReply
I->>C: peer_ctrl_msg_handler(role=ELECTING)
C->>C: vote_reply_decode_and_dispatch → raft_on_vote_reply
C->>C: raft_cb_become_leader fires
C->>C: votes[i].ep added to ha->standby_endpoints
C->>P: push_config_to_standbys (on promoted ep)
C->>P: ha_send_ctrlhello_to_all_standbys (on promoted ep)
C->>C: repl_add_standby registers promoted ep with WAL replicator (ha-commit-engine addition)
P->>C: OPCode_replCatchUpDone on same ep
I->>C: peer_ctrl_msg_handler(role=PRIMARY)
C->>C: repl_ack_decode_and_dispatch: replCatchUpDone is now a no-op (ack-counting removed)
Note: the original
role=COMMITTINGstep andha_repl_ack_received → ha_promote_to_primaryack-counting tail no longer exist —CTRL_PRIMARY_COMMITTINGandha_repl_ack_receivedwere deleted byha-consistency-invariants/ha-single-leader-role(2026-06-11); promotion is now driven by the heartbeat decision loop + commit engine (ha-commit-engine,ha-repl-hardening), not by countingreplCatchUpDoneacks on the promoted ep. See those specs' design.md.
Election + Passive Wait (Loser Path)
sequenceDiagram
participant C as Loser ha.c
participant I as iomgr
participant W as Winner Primary
C->>I: endpoint_connect + iomgr_monitor (per peer)
I->>C: on_vote_connected fires
C->>W: xce_request(OPCode_requestVote)
W->>C: RequestVoteReply (denied)
I->>C: peer_ctrl_msg_handler(role=ELECTING)
C->>C: raft_cb_become_follower fires
C->>C: endpoint_fini(votes[i].ep) — close all vote endpoints
C->>C: ha_arm_heartbeat_timeout_vtask
note over C: CTRL_STANDBY — passive wait on listener
W->>C: ctrlhello arrives on listener (existing iomgr path)
C->>C: handle_ctrlhello sets ha->primary_ctrl_fd = ep->fd
C->>C: ha_on_heartbeat_received resets watchdog deadline
Requirements Traceability
| Requirement | Summary | Component | Flow |
|---|---|---|---|
| 1.1 | Candidate establishes one outbound TLS connection per peer for vote RPC | raft_cb_send_vote_request (unchanged) |
Election Phase |
| 1.2 | Candidate reuses each surviving outbound vote connection as persistent peer channel | raft_cb_become_leader promotes ep; peer_ctrl_msg_handler dispatches by role |
Promotion |
| 1.3 | Winner promotes vote connections to standby replication channels | raft_cb_become_leader: add votes[i].ep to standby_endpoints |
Winner Path |
| 1.4 | Loser closes all outbound vote connections | raft_cb_become_follower: endpoint_fini(votes[i].ep) |
Loser Path |
| 2.1 | CTRL_STANDBY does not initiate outbound TCP connections | Remove fetch_config_from_primary delay task from raft_cb_become_follower |
Loser Path |
| 2.2 | Standby accepts ctrlhello / config / WAL on inbound connection from winner | Existing vtaskmgr path unchanged; handle_ctrlhello sets primary_ctrl_fd |
Standby Passive |
| 2.3 | No heartbeat from primary within timeout → new election | ha_arm_heartbeat_timeout_vtask called in raft_cb_become_follower |
Loser Path |
| 2.4 | CTRL_STANDBY does not invoke tier-1/2/3 discovery | Remove fetch_config_from_primary call; ha_find_and_connect_to_primary not called |
Loser Path |
| 3.1 | First ctrlhello on promoted connection → designate as primary control channel | handle_ctrlhello sets ha->primary_ctrl_fd = ep->fd when role == CTRL_STANDBY |
Standby Passive |
| 3.2 | Primary control channel RDHUP → detect within one epoll cycle → CTRL_ELECTING | Existing ha_on_primary_ctrl_fd_rdhup unchanged; armed from ctrlhello ep |
Standby Passive |
| 3.3 | Arm heartbeat watchdog with same semantics | ha_arm_heartbeat_timeout_vtask called in raft_cb_become_follower |
Loser Path |
| 4.1 | Loser explicitly closes all outbound vote endpoints on election loss | raft_cb_become_follower: iterate ha->elect_ctx->votes[i].ep, call endpoint_fini |
Loser Path |
| 4.2 | Failed outbound vote connection → count peer as denied, no reconnect | Existing vote_ep_err_handler unchanged |
Error |
| 4.3 | New election → fresh outbound connections | ha_start_election_async creates new endpoints each time (unchanged) |
Election Phase |
| 5.1 | Primary pushes config via promoted connection | push_config_to_standbys uses standby_endpoints (populated from promoted eps) |
Winner Path |
| 5.2 | WAL replication via promoted connections | Existing catch-up path uses standby_endpoints; majority-ack commit tracking now also registers the promoted ep with the replicator via repl_add_standby (Note: added by ha-commit-engine, 2026-06-12 — see that spec's design.md) |
Winner Path |
| 5.3 | Single-node self-elect without peer connections | Existing raft library single-node path unchanged | Election Phase |
| 5.4 | Committing timeout → degraded PRIMARY | committing_timeout_fn no longer exists (Note: deleted along with CTRL_PRIMARY_COMMITTING by ha-consistency-invariants, 2026-06-11; replaced by promotion-fence/own-term-gate machinery — ha_arm_own_term_gate, ha_arm_promotion_fence_timer — see that spec's design.md) |
Winner Path |
| 5.5 | No new ASAN errors | Promoted ep lifetime owned by vote_ep_err_handler / raft_cb_become_follower cleanup |
All |
Components and Interfaces
| Component | File | Intent | Req Coverage |
|---|---|---|---|
peer_ctrl_msg_handler |
ha.c | State-aware dispatcher; replaces vote_reply_msg_handler |
1.2 |
ha_vote_entry_t.ep field |
ha.h | Track per-peer outgoing endpoint for promotion/close | 1.3, 1.4, 4.1 |
raft_cb_become_leader (extended) |
ha.c | Promote vote eps → standby_endpoints | 1.3, 5.1, 5.2 |
raft_cb_become_follower (extended) |
ha.c | Close vote eps, arm watchdog, remove fetch_config call | 1.4, 2.1, 2.3, 2.4, 3.3 |
handle_ctrlhello (extended) |
config_repl.c (design originally cited cluster.c — corrected) | Set primary_ctrl_fd from ctrlhello ep | 3.1, 3.2 |
HA Election Layer
peer_ctrl_msg_handler
| Field | Detail |
|---|---|
| Intent | Single iomgr message handler for outgoing vote endpoints; routes by current role |
| Requirements | 1.2 |
| File | src/controller/ha.c |
Responsibilities & Constraints
- Set once in
on_vote_connected; never reassigned - Reads
ha->roleatomically at dispatch time - Routes
CTRL_ELECTINGto vote reply decode (existingvote_reply_msg_handlerlogic, factored out asvote_reply_decode_and_dispatch) - Routes
CTRL_PRIMARYto repl-ack decode (repl_ack_decode_and_dispatch). (Note:CTRL_PRIMARY_COMMITTINGno longer exists — removed byha-consistency-invariants/ha-single-leader-role, 2026-06-11;repl_ack_decode_and_dispatch'sOPCode_replCatchUpDonebranch is now an INFO-logged no-op andha_repl_ack_receivedwas deleted along with the ack-counting promotion path — see those specs' design.md) - Also routes
CTRL_STANDBYwith an in-progress PreVote round topre_vote_reply_decode_and_dispatch— added later byha-prevote(2026-07-18), not part of this spec's original scope - Any other role: drop packet (free packet, return 0)
- Runs in iomgr thread — must not block, for the default (dispatch-flag-OFF) path;
raft-drive-async(2026-07-18) later added an optional vtask-pool dispatch mode (ha_peerctrl_dispatch_enabled) that can run this body off the iomgr thread — see that spec's design.md
Contracts: State [ x ]
State Management
/* Signature: matches msg_handler_t = int (*)(void*) */
static int peer_ctrl_msg_handler(void *ep_raw);
/* Internal helpers (static, same file) — current signatures (corrected; both
* gained an io_packet_t *pkt parameter, and vote_reply_decode_and_dispatch
* dropped its endpoint_t *ep parameter, as part of the raft-drive-async
* pkt-ownership refactor, 2026-07-18) */
static int vote_reply_decode_and_dispatch(vote_ep_ctx_t *vctx, io_packet_t *pkt);
static int repl_ack_decode_and_dispatch(endpoint_t *ep, vote_ep_ctx_t *vctx, io_packet_t *pkt);
- State model: branches on
atomic_load_explicit(&ha->role, memory_order_acquire) - ELECTING path: existing vote-reply capnp decode →
vote_reply_dispatch→raft_on_vote_reply - PRIMARY path: decode
OPCode_replCatchUpDone— now a no-op INFO log (ack-counting/ha_repl_ack_receivedremoved, see note above); the same handler also decodes heartbeat-replystateReportfor convergence tracking - Other: free packet, no-op
Implementation Notes
vote_reply_decode_and_dispatchis exactly the body of the existingvote_reply_msg_handler(factored out); the XCE_TESTING term-fencing andxce_vote_reply_hookseams are preservedrepl_ack_decode_and_dispatchdecodes an inbound capnp packet;OPCode_replCatchUpDoneis now a no-op (superseded — see note above), and it separately decodes heartbeat-replystateReportmessages for convergence tracking. The original "COMMITTING phase" framing no longer applies since that role was removed.
raft_cb_become_leader (extended)
| Field | Detail |
|---|---|
| Intent | After election win: populate ha->standby_endpoints from promoted vote endpoints |
| Requirements | 1.3, 5.1, 5.2 |
| File | src/controller/ha.c |
Responsibilities & Constraints
- Read
ha->elect_ctxto getha_elect_ctx_t* - Iterate
ctx->votes[i].epforiin[0, ctx->n_peers) - If
votes[i].ep != NULL(connect succeeded): add toha->standby_endpointsunderha->lock - Existing
push_config_to_standbysandha_send_ctrlhello_to_all_standbyscalls are unchanged; they iterate the now-populated array - (Note:
ha-commit-engine, 2026-06-12, added a further registration in this same loop — each promoted ep is also registered with the WAL replicator viarepl_add_standby(&c->persist.repl, ...), which is what majority-ack commit tracking now uses;standby_endpointsremains the config-push/ctrlhello target only. See that spec's design.md.)
Implementation Notes
votes[0]is reserved for self in the raft library; peer endpoints start at index 1. Verify index mapping againstraft_cb_send_vote_requestpeer_idx.- If
ha->elect_ctxis NULL whenraft_cb_become_leaderfires (e.g., single-node), skip promotion loop (no peers to promote). - Risk:
votes[i].epmay be NULL if the peer's connect failed; the NULL check guards this.
raft_cb_become_follower (extended)
| Field | Detail |
|---|---|
| Intent | After election loss: close vote endpoints, arm heartbeat watchdog, remove fetch_config call |
| Requirements | 1.4, 2.1, 2.3, 2.4, 3.3 |
| File | src/controller/ha.c |
Responsibilities & Constraints
- Read
ha->elect_ctx, iteratevotes[i].ep, callendpoint_fini(votes[i].ep)for non-NULL entries, setvotes[i].ep = NULL - Do NOT call
ha_find_and_connect_to_primaryor schedulefetch_config_from_primary - Call
ha_arm_heartbeat_timeout_vtask(ha, c)after role is set to CTRL_STANDBY - Existing
cluster_init_phase2call andha->phase2_doneguard are preserved
Implementation Notes
endpoint_finicloses the fd and releases the endpoint slot; iomgr is notified via epoll RDHUP on the peer side, which is expected- Watchdog arm must happen after
ha->role = CTRL_STANDBYis stored (role gate in watchdog logic) - XCE_TESTING:
ha_arm_heartbeat_timeout_vtaskis a no-op whenc == NULL; the test seam is preserved
Cluster Runtime Layer
handle_ctrlhello (extended)
| Field | Detail |
|---|---|
| Intent | Set ha->primary_ctrl_fd when ctrlhello arrives on standby's inbound connection |
| Requirements | 3.1, 3.2, 3.3 |
| File | src/controller/cluster.c |
Responsibilities & Constraints
- Existing ctrlhello handler already processes OP_hello and triggers WAL catch-up
- Add: if
ha->role == CTRL_STANDBYandha->primary_ctrl_fd == -1, setha->primary_ctrl_fd = ep->fd - Call
ha_on_heartbeat_received(ha)to reset the watchdog deadline (already done by heartbeat path; add to ctrlhello path for first message) - No other changes to the ctrlhello handler
Implementation Notes
- Guard with
primary_ctrl_fd == -1for idempotency; a repeat ctrlhello on the same connection does not need re-registration ha_on_primary_ctrl_fd_rdhupalready handles fd close and triggers re-election; no changes needed there
Data Models
ha_vote_entry_t (modified)
typedef struct {
xce_candidate_t *peer; /* Peer being solicited */
uint64_t my_term; /* Term sent in OP_requestVote */
uint64_t my_tag; /* config_tag sent in request */
uint64_t my_lsn; /* WAL LSN sent in request */
int vote_granted; /* 1 if peer granted vote */
int replied; /* 1 if peer replied */
uint64_t peer_tag; /* peer's config_tag from reply */
uint64_t peer_lsn; /* peer's WAL LSN from reply */
struct endpoint_t *ep; /* NEW: outgoing vote endpoint; NULL if connect failed */
} ha_vote_entry_t;
Invariants:
epis set byraft_cb_send_vote_requestwheniomgr_monitorsucceedsepis NULL ifendpoint_connectoriomgr_monitorfailed for this peer- Winner:
epis transferred tostandby_endpoints; winner does NOT callendpoint_finion promoted eps - Loser:
epis closed viaendpoint_finiinraft_cb_become_follower; set to NULL after close
vote_ep_ctx_t
The promoted flag is NOT added — role is read from ha->role at dispatch time, which is the authoritative state, as this design intended. (Note: despite the original "(unchanged)" heading, the struct is no longer literally unchanged since this spec closed — ha-term-safety (2026-07-17) added my_last_log_term/my_last_log_index fields, and ha-elect-ctx-lifecycle (2026-07-18) changed its lifetime contract: the endpoint's on_fini hook now calls ha_elect_ctx_release to decrement a refcount on the owning ha_elect_ctx_t, rather than the simple election-cleanup-only lifetime this design describes. See those specs' design.md.)
Error Handling
Error Strategy
Treat connection failures and packet decode errors identically to the existing vote path: count the peer as denied (or drop the packet for non-vote phases).
Error Categories
Connect failure (vote_ep_err_handler):
- Existing behavior preserved: a connect-time or post-connect failure counts as a denied vote through the production tally (
vote_reply_dispatch), not the legacyhandle_request_vote_replypath (hardened later byraft-adapter-conformance) - Promoted connection disconnect:
vote_ep_err_handlerfires when the standby drops. ForPRIMARYrole (Note: theCOMMITTINGrole named here was removed — see the peer_ctrl_msg_handler note above), removeepfromha->standby_endpointsunderha->lock; current code also callsrepl_remove_standbyto deregister from the WAL replicator (ha-commit-engineaddition) —ha_repl_ack_receivedno longer exists
Packet decode error in repl_ack_decode_and_dispatch:
- Drop packet, return 0 (iomgr continues monitoring endpoint)
- Log WARN with opcode for debugging
Unexpected role in peer_ctrl_msg_handler:
- Drop packet, return 0 (CTRL_INIT: should not happen normally; CTRL_STANDBY on outgoing ep is now an EXPECTED case when a PreVote round is in progress — routed to
pre_vote_reply_decode_and_dispatchrather than dropped, added byha-prevote, 2026-07-18) - Log WARN
Monitoring
- Add
INFOlog inraft_cb_become_leaderfor each promoted endpoint:"HA: promoted vote ep[%d] → standby channel" - Add
INFOlog inraft_cb_become_followerfor endpoint close:"HA: closing vote ep[%d] (loser path)" - Existing heartbeat watchdog log messages unchanged
Testing Strategy
Unit Tests (test_ha_election.c)
- Dispatcher routes by role: set
ep->msg_handler = peer_ctrl_msg_handler; inject packet with role=ELECTING → verifyxce_vote_reply_hookfires; inject packet with role=COMMITTING → verify repl-ack hook fires; inject with role=STANDBY → verify packet dropped - Winner promotes endpoints: call
raft_cb_become_leaderwithha->elect_ctxpopulated; verifyha->standby_endpoints->count == n_peers_connected - Loser closes endpoints: call
raft_cb_become_follower; verifyvotes[i].ep == NULLfor all i; verifyendpoint_finicalled (ASAN validates no leak) - Watchdog armed on CTRL_STANDBY: mock
ha_arm_heartbeat_timeout_vtaskvia test hook; verify it is called exactly once fromraft_cb_become_follower - fetch_config_from_primary NOT called: verify no
delay_task_mscall forfetch_config_from_primaryinraft_cb_become_follower(use test seam to intercept delay_task_ms) - primary_ctrl_fd set on ctrlhello: call
handle_ctrlhellowith role=CTRL_STANDBY andprimary_ctrl_fd=-1; verifyprimary_ctrl_fd == ep->fd - Promoted ep err_handler removes from standby_endpoints: populate
standby_endpointswith a mock ep; callvote_ep_err_handlerwith role=COMMITTING; verify ep removed from array
Integration Tests (test_ha_bootstrap.c / test_p3_integration.c)
- Two-node election → standby receives ctrlhello without outbound connect: boot two nodes; verify standby never calls
ha_find_and_connect_to_primary; verify ctrlhello arrives withinelection_timeout_ms + 200ms - Promoted connection carries WAL replication: verify
OPCode_replCatchUpDonedecoded on promoted ep incrementsrepl_ack_count - Primary failover → fresh connections on re-election: kill primary; verify standby transitions CTRL_ELECTING and creates fresh endpoints (not reusing closed ones)