feat(raft-library): task 6.1 — raft_state_t field in ha_state_t, raft_init with no-op stubs

This commit is contained in:
Rongsong Shen 2026-05-31 07:20:15 +00:00
parent 6bdf7edacf
commit b77525deb0
3 changed files with 54 additions and 1 deletions

View file

@ -35,6 +35,28 @@
int XCE_ERR_NOT_READY = E_XCE_NOT_READY;
int XCE_ERR_STANDBY = E_XCE_STANDBY;
/* ── No-op raft callback stubs (used by ha_init) ───────────── */
static int stub_send_vote(void *c, int p, uint64_t t) { (void)c;(void)p;(void)t; return 0; }
static void stub_become_leader(void *c, uint64_t t) { (void)c;(void)t; }
static void stub_become_follower(void *c, uint64_t t, int l) { (void)c;(void)t;(void)l; }
static int stub_persist(void *c, uint64_t t, int v) { (void)c;(void)t;(void)v; return 0; }
static void stub_sched_timeout(void *c, uint32_t d) { (void)c;(void)d; }
static void stub_cancel_timeout(void *c) { (void)c; }
static void stub_start_after_delay(void *c, uint32_t d) { (void)c;(void)d; }
static const raft_callbacks_t s_noop_cbs = {
.on_send_vote_request = stub_send_vote,
.on_vote_eligible = NULL,
.on_become_leader = stub_become_leader,
.on_become_follower = stub_become_follower,
.on_persist = stub_persist,
.on_schedule_timeout = stub_sched_timeout,
.on_cancel_timeout = stub_cancel_timeout,
.on_start_election_after_delay = stub_start_after_delay,
.on_trace = NULL,
};
/* ── PRNG seeding ───────────────────────────────────────────── */
void ha_seed_jitter(uint64_t *seed, int my_priority)
@ -105,6 +127,34 @@ int ha_init(ha_state_t *ha, const ha_config_t *cfg, int my_priority)
* ha_wait_for_heartbeat_or_timeout() calls sem_timedwait on this. */
sem_init(&ha->heartbeat_seen, 0, 0);
/* ── Task 6.1: Initialize embedded raft_state_t ──────────────────────────
*
* Wire raft_init with stub (no-op) callbacks — real callbacks are connected
* in task 6.2. initial_term and initial_voted_for start at defaults (0 / -1)
* here; ha_restore_term() updates ha->term from disk immediately after ha_init
* in cluster.c, so the raft state will be reconciled before any election fires.
*
* cfg.ctx = ha so task 6.2 callbacks can reach ha_state_t.
* cfg.n_peers = 1 (single-node default) and cfg.self_idx = 0 are safe placeholders;
* ha_start_election_async already stores the real peer count in ha->n_peers
* before calling any Raft function.
*
* raft_init requires non-NULL required callbacks; task 6.2 replaces these.
* The no-op stubs are defined at file scope above (s_noop_cbs).
*/
raft_config_t raft_cfg;
memset(&raft_cfg, 0, sizeof(raft_cfg));
raft_cfg.n_peers = 1; /* placeholder; real count set by ha_start_election_async */
raft_cfg.self_idx = 0; /* placeholder; real index set in task 6.2 */
raft_cfg.election_timeout_ms = cfg->election_timeout_ms;
raft_cfg.my_priority = my_priority;
raft_cfg.initial_term = 0; /* ha_restore_term() updates ha->term after ha_init */
raft_cfg.initial_voted_for = -1; /* no vote on fresh init */
raft_cfg.cbs = &s_noop_cbs;
raft_cfg.ctx = ha;
raft_init(&ha->raft, &raft_cfg);
return 0;
}