feat(election-iomgr-refactor): config/WAL replication push to standbys in COMMITTING path — task 4.2

This commit is contained in:
Rongsong Shen 2026-05-30 11:56:31 +00:00
parent f4a6726f1b
commit 757558bb33
6 changed files with 433 additions and 0 deletions

View file

@ -1229,6 +1229,60 @@ int cluster_run(cluster_t* c)
return 0;
}
/* xce_request: defined in request.c, not yet in any public header */
extern int xce_request(endpoint_t *ep, xce_request_t *req);
/* ── Task 4.2: ha_send_ctrlhello_to_all_standbys ───────────────────────────
*
* Send OP_hello CtrlHelloRequest to every connected standby endpoint.
* This triggers handle_ctrlhello on the receiving standby, which runs the
* existing catch-up replication sequence (WAL streaming, config push).
*
* Must be called from the COMMITTING path in ha_elect_evaluate_and_signal
* after ha->role is set to CTRL_PRIMARY_COMMITTING (Req 7.2).
*
* Thread safety: acquires ha->lock around standby_endpoints iteration.
* Best-effort: failures per-endpoint are logged but do not abort the loop.
*/
void ha_send_ctrlhello_to_all_standbys(cluster_t *c)
{
if (!c) return;
ha_state_t *ha = &c->ha;
if (!ha->standby_endpoints) return;
uint64_t my_lsn = atomic_load_explicit(&c->persist.committed_lsn,
memory_order_acquire);
uint64_t my_tag = c->myconf ? c->myconf->config_version_tag : 0;
char *my_name = c->myconf ? c->myconf->name : "";
pthread_mutex_lock(&ha->lock);
int n = ha->standby_endpoints->count;
for (int i = 0; i < n; i++) {
endpoint_t *ep = (endpoint_t *)zarray_get(ha->standby_endpoints, i);
if (!ep) continue;
ctrl_hello_req_t hello = {
.name = my_name,
.key = NULL,
.applied_lsn = my_lsn,
.config_tag = my_tag,
};
xce_request_t req;
memset(&req, 0, sizeof(req));
req.opcode = OPCode_hello;
req.body_which = Request_body_ctrlhellomsg;
req.body.ctrlhellomsg = &hello;
int rc = xce_request(ep, &req);
if (rc < 0) {
WARN("ha_send_ctrlhello_to_all_standbys: xce_request to standby[%d] "
"failed: %d", i, rc);
}
}
pthread_mutex_unlock(&ha->lock);
}
void cluster_fini(cluster_t* c)
{
/* Free heap-allocated standby_endpoints array (created in phase1) */