fix(ha-term): move ha-term.bin to {workdir}/{name}/data/ alongside WAL/snapshot

This commit is contained in:
Rongsong Shen 2026-05-31 11:04:35 +00:00
parent 04b31afb06
commit 0f766776ba
2 changed files with 13 additions and 6 deletions

View file

@ -791,13 +791,15 @@ int cluster_init_phase1(cluster_t* c)
} }
/* Restore persisted election term + voted_for from ha-term.bin (Req 9.2). /* Restore persisted election term + voted_for from ha-term.bin (Req 9.2).
* ha_persist_term() already writes on vote grant; this reads it back at * File lives under {workdir}/{myname}/data/ alongside WAL and snapshot. */
* startup so the restarted node remembers its last term. */ if (c->myconf->workdir && c->myconf->name) {
if (c->myconf->workdir) { char term_dir[1024];
rc = ha_restore_term(&c->ha, c->myconf->workdir); snprintf(term_dir, sizeof(term_dir), "%s/%s/data",
c->myconf->workdir, c->myconf->name);
rc = ha_restore_term(&c->ha, term_dir);
if (rc == 0) { if (rc == 0) {
INFO("HA: restored term=%lu from %s/ha-term.bin", INFO("HA: restored term=%lu from %s/ha-term.bin",
(unsigned long)atomic_load(&c->ha.term), c->myconf->workdir); (unsigned long)atomic_load(&c->ha.term), term_dir);
} else if (rc != -ENOENT) { } else if (rc != -ENOENT) {
WARN("HA: ha_restore_term failed: %d", rc); WARN("HA: ha_restore_term failed: %d", rc);
} }

View file

@ -476,7 +476,12 @@ static int raft_cb_persist(void *ctx, uint64_t term, int voted_for)
{ {
ha_state_t *ha = (ha_state_t *)ctx; ha_state_t *ha = (ha_state_t *)ctx;
cluster_t *c = ha->raft_c; cluster_t *c = ha->raft_c;
const char *workdir = (c && c->myconf) ? c->myconf->workdir : NULL; /* Store ha-term.bin under {workdir}/{myname}/data/ alongside WAL/snapshot. */
char data_dir[1024] = {0};
if (c && c->myconf && c->myconf->workdir && c->myconf->name)
snprintf(data_dir, sizeof(data_dir), "%s/%s/data",
c->myconf->workdir, c->myconf->name);
const char *workdir = data_dir[0] ? data_dir : NULL;
/* Update ha->term */ /* Update ha->term */
atomic_store_explicit(&ha->term, term, memory_order_release); atomic_store_explicit(&ha->term, term, memory_order_release);