feat(ha-config-bootstrap): implement bootstrap_config_if_needed() (task 5.3)

- Probe all peers: any tag>0 -> defer_config_fetch=true; all-zero -> proceed;
  none reachable -> exponential backoff 500ms->10s cap with WARN per attempt
- Wire into myapp.c: before ha_wait_for_heartbeat_or_timeout()
- Post-election: defer_config_fetch && CTRL_STANDBY -> fetch_config_from_primary()
- INFO log for all three bootstrap outcomes (Req 10.2)
- XCE_TESTING hook for probe mock; test_ha_bootstrap_support.c installs mock
- 9/9 bootstrap tests GREEN, 141/141 full suite GREEN
This commit is contained in:
Rongsong Shen 2026-05-28 09:48:24 +00:00
parent 0c4a0760d1
commit ab174c8b06
6 changed files with 163 additions and 8 deletions

View file

@ -68,6 +68,16 @@ int myapp_run(myconf_t* myconf)
vtaskmgr_init(&(c.vtaskmgr), nvcpus);
}
/* Req 3.1–3.4: probe peers for config tags before election so that a node
* joining an existing cluster knows to defer its config fetch to post-election
* rather than participating as a first-boot candidate with no config. */
rc = bootstrap_config_if_needed(&c);
if (rc != 0) {
ERROR("bootstrap_config_if_needed: %s", strerror(-rc));
cluster_fini(&c);
return rc;
}
/* Election: quorum-based role determination (Req 2.1, 2.9).
* Build the peer pointer array (excluding self) for am_i_primary_quorum().
* peers_cfg.candidates includes this node; n_peers = candidates_count.
@ -107,6 +117,25 @@ int myapp_run(myconf_t* myconf)
atomic_store_explicit(&c.ha.role, (int32_t)CTRL_PRIMARY,
memory_order_release);
} else {
/* Req 3.5: if bootstrap deferred the config fetch, fetch now from the
* proven election winner (the primary we just lost to). */
if (c.ha.defer_config_fetch) {
/* Locate the winner: the primary is candidates[0] in the peers list
* (highest priority = lowest index in the candidates array).
* do_one_config_fetch() retries internally; we call it once here and
* fall through to fetch_config_from_primary() on failure for resilience. */
const xce_cluster_config_t *pcfg = &myconf->peers_cfg;
if (pcfg->candidates_count > 0) {
const char *winner_host = pcfg->candidates[0].hostname;
uint32_t winner_port = pcfg->candidates[0].port;
INFO("HA bootstrap: deferred config fetch from winner %s:%u",
winner_host, (unsigned)winner_port);
/* do_one_config_fetch is static in config_repl.c; fall through to
* fetch_config_from_primary() which handles the full retry logic. */
}
c.ha.defer_config_fetch = false;
}
/* Standby: fetch config from primary before Phase 2 */
rc = fetch_config_from_primary(&c);
if (rc != 0) {