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

@ -140,7 +140,7 @@
- [ ] 6. (P) Controller adapter: wire raft.h into ha.c
_Depends: 3_
- [ ] 6.1 Add raft_state_t to ha_state_t and initialize at startup
- [x] 6.1 Add raft_state_t to ha_state_t and initialize at startup
- Add `#include "raft/raft.h"` to `ha.h`; add `raft_state_t raft` field to `ha_state_t`
- In the ha initialization path, call `raft_init(&ha->raft, &cfg)` with term/voted_for loaded from `ha_persist_load()`, all ha-implemented callbacks, and `ha_state_t*` as context
- `/xce-build controller` compiles without errors after the change

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;
}

View file

@ -28,6 +28,7 @@
#include "config.h" /* XCE_MAXNAME_LEN, MAX_STANDBYS, etc. */
#include "xce/cluster.h" /* xce_candidate_t */
#include "zarray.h"
#include "raft/raft.h" /* raft_state_t, raft_init */
/* Forward declaration — avoids circular include with cluster_def.h */
typedef struct cluster_s cluster_t;
@ -173,6 +174,8 @@ typedef struct {
int phase2_done; /* 1 after cluster_init_phase2 runs */
_Atomic int repl_ack_count; /* count of standbys that ACKed in COMMITTING state */
int n_peers; /* total candidate count (excluding self) */
/* ── New fields added by raft-library spec (Task 6.1) ──────── */
raft_state_t raft; /* Raft consensus state (wired in task 6.2) */
} ha_state_t;
/* ── Public API ────────────────────────────────────────────── */