fix(ha-commit-engine,persist): reserve WAL lsn 0 — first entry starts at lsn 1

Root cause (found via live 3-node E2E + a verification test): lsn was 0-based
(hdr.lsn = current_lsn++ from current_lsn=0), and 0 was overloaded as a
sentinel, causing three cold-start defects on a freshly-promoted leader:
  - the first WAL entry (own-term promotion NOOP at lsn 0) was structurally
    uncommittable: commit_advance_locked only stores when cand>cur and
    committed_lsn starts at 0, so lsn 0 can never advance it;
  - commit_set_writable_lsn(noop_lsn=0) set writable_lsn=0, which wait_done
    reads as 'gate off' — the own-term safety gate was a no-op on the first leader;
  - bug-543 (WAL hole): ha_stream_wal_to_standby filters hdr.lsn>standby_lsn;
    a fresh empty standby reports standby_lsn=0 (ambiguous 'empty' vs 'has lsn 0'),
    so lsn 0 failed 0>0 and was dropped -> non-contiguous standby WAL.

Fix: a fresh persist store initializes current_lsn=1, reserving lsn 0 as the
none/empty sentinel. The first real entry is lsn 1 -> committable (0->1),
writable_lsn=1 -> gate effective, and standby_lsn=0 unambiguously means 'empty'
so catch-up streams from the first entry with no hole. persist_recover is
data-driven (current_lsn=max(hdr.lsn+1)), so existing on-disk WALs and non-fresh
recovery are unaffected; the change only sets the base for brand-new stores.
No wire/capnp change.

Verification: test_coldstart_lsn0 drives the real commit engine + real streamer
and proves H1 (first entry commits 0->1), H3 (gate effective + blocks when
committed<writable), H4 (empty-standby catch-up is contiguous [1,2], control
standby_lsn=1 gets only [2]). Full controller suite green under ASAN. Tests that
relied on the lsn-0 base were shifted (several strengthened); commit.c /
config_repl.c / ha.c / wire format untouched.

Owning specs: ha-commit-engine / ha-wal-replication / persist. Follow-up:
sweep stale lsn-0 comments (repl.c:429-436, persist.c:1055-1075/1240-1243/1296).
This commit is contained in:
Rongsong Shen 2026-06-14 10:46:16 +00:00
parent c97c7632e9
commit 7519fa96dd
10 changed files with 480 additions and 31 deletions

View file

@ -1296,8 +1296,10 @@ static void test_sync_append_wired_commits_on_per_entry_majority(void)
pthread_t th;
int rc, sync_rc;
/* Arrange: real persist, 2 standbys, engine n_peers=3 wired; one
* priming async append so the sync entry lands at LSN 1 */
/* Arrange: real persist, 2 standbys, engine n_peers=3 wired. Under the
* reserved-0 contract a fresh store starts current_lsn=1, so the sync entry
* lands at LSN 1 with no priming append needed (LSN 0 is the reserved
* sentinel — never a real entry). */
g_sp = sync_persist_setup("maj", 2000);
TEST_ASSERT_TRUE(repl_add_standby(&g_sp->repl, &g_ep_sa, "sb_a", 7100) >= 0);
TEST_ASSERT_TRUE(repl_add_standby(&g_sp->repl, &g_ep_sb, "sb_b", 7101) >= 0);
@ -1305,7 +1307,6 @@ static void test_sync_append_wired_commits_on_per_entry_majority(void)
repl_set_commit_engine(&g_sp->repl, &g_ce);
repl_broadcast_send_hook = ce_drop_send_hook;
TEST_ASSERT_EQUAL_INT(0, persist_wal_async(g_sp, WAL_JOB_UPDATE, "p", 1));
/* Act: standby A's ACK covering the sync entry (LSN 1) arrives mid-wait */
g_rack = (repl_ack_arg_t){ &g_sp->repl, &g_ep_sa, 1, 30 };
@ -1382,12 +1383,12 @@ static void test_sync_append_wired_single_member_commits_locally(void)
{
int rc, sync_rc;
/* Arrange: real persist, NO standbys, engine n_peers=1 wired; one
* priming async append so the sync entry lands at LSN 1 */
/* Arrange: real persist, NO standbys, engine n_peers=1 wired. Under the
* reserved-0 contract the sync entry lands at LSN 1 with no priming append
* (fresh store starts current_lsn=1; LSN 0 is the reserved sentinel). */
g_sp = sync_persist_setup("solo", 2000);
TEST_ASSERT_EQUAL_INT(0, commit_engine_init(&g_ce, g_sp, 1, 8));
repl_set_commit_engine(&g_sp->repl, &g_ce);
TEST_ASSERT_EQUAL_INT(0, persist_wal_async(g_sp, WAL_JOB_UPDATE, "p", 1));
/* Act */
sync_rc = -1;