From 0f766776ba05067d9e83fba35dabeb6771f0436e Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Sun, 31 May 2026 11:04:35 +0000 Subject: [PATCH] fix(ha-term): move ha-term.bin to {workdir}/{name}/data/ alongside WAL/snapshot --- src/controller/cluster.c | 12 +++++++----- src/controller/ha.c | 7 ++++++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/controller/cluster.c b/src/controller/cluster.c index 70afde2..c736126 100644 --- a/src/controller/cluster.c +++ b/src/controller/cluster.c @@ -791,13 +791,15 @@ int cluster_init_phase1(cluster_t* c) } /* 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 - * startup so the restarted node remembers its last term. */ - if (c->myconf->workdir) { - rc = ha_restore_term(&c->ha, c->myconf->workdir); + * File lives under {workdir}/{myname}/data/ alongside WAL and snapshot. */ + if (c->myconf->workdir && c->myconf->name) { + char term_dir[1024]; + 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) { 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) { WARN("HA: ha_restore_term failed: %d", rc); } diff --git a/src/controller/ha.c b/src/controller/ha.c index a908a3a..326de6d 100644 --- a/src/controller/ha.c +++ b/src/controller/ha.c @@ -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; 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 */ atomic_store_explicit(&ha->term, term, memory_order_release);