feat(ha-config-bootstrap): implement ha_restore_term() for ha-term.bin read path (task 6.2)

This commit is contained in:
Rongsong Shen 2026-05-28 13:21:36 +00:00
parent 722d7824c4
commit 7c27ff1954
3 changed files with 91 additions and 1 deletions

View file

@ -294,6 +294,24 @@ int ha_dispatch_gate(ha_state_t *ha, int opcode);
*/
void ha_persist_term(ha_state_t *ha, const char *workdir);
/*
* ha_restore_term: read {workdir}/ha-term.bin at startup and restore
* ha->term and ha->voted_for_* into ha_state_t.
*
* On missing file (ENOENT): no-op — term stays 0, voted_for_name stays empty.
* Returns 0 (safe first-boot path). Req 9.3.
* On CRC/magic mismatch: log WARN, zero ha->term and ha->voted_for_* for
* safety and return 0. Req 9.2.
* On valid file: atomically restore ha->term; copy voted_for_term
* and voted_for_name. Returns 0. Req 9.2.
* On unexpected I/O error: return -errno.
*
* Must be called after ha_init() and before any election or vote processing.
* If workdir is NULL, the function is a no-op (safe for unit tests).
* Requirements: 9.1, 9.2, 9.3.
*/
int ha_restore_term(ha_state_t *ha, const char *workdir);
/*
* handle_request_vote: process an incoming OP_requestVote RPC.
* Applies term/voted_for/freshness gate; persists state via ha_persist_term().