transport: keep rebuild base writes out of WAL frontier

This commit is contained in:
pingqiu committed 2026-05-02 09:52:43 -07:00
1 parent d8e3c2e67b
commit 2b09fc6f4f
4 files changed
+44 -12

No files matched your search

+32
View File
@@ -74,6 +74,12 @@ type WALStore struct {
// only by the flusher.
checkpointLSN uint64
// pendingDirectFrontierLSN is set by AdvanceFrontier after direct
// extent writes (rebuild BASE lane). It is persisted as checkpoint
// metadata only after a successful Sync, so an un-synced BASE install
// cannot be mistaken for durable recovery progress after a crash.
pendingDirectFrontierLSN uint64
// recoveryRetentionLSNs is the operator-tunable retention window
// past checkpointLSN: the recovery scan accepts fromLSN as long as
// fromLSN > checkpointLSN - recoveryRetentionLSNs (G6 §1.A α).
@@ -493,7 +499,30 @@ func (s *WALStore) Sync() (uint64, error) {
s.syncedLSN = s.walHead
}
frontier := s.syncedLSN
var sbBytes []byte
if s.pendingDirectFrontierLSN > s.checkpointLSN && s.pendingDirectFrontierLSN <= s.syncedLSN {
s.checkpointLSN = s.pendingDirectFrontierLSN
s.sb.WALCheckpointLSN = s.pendingDirectFrontierLSN
if s.walHead > s.sb.WALHead {
s.sb.WALHead = s.walHead
}
buf := newSimpleByteBuf()
if _, err := s.sb.writeTo(buf); err != nil {
s.mu.Unlock()
return 0, fmt.Errorf("storage: encode superblock: %w", err)
}
sbBytes = buf.bytes()
s.pendingDirectFrontierLSN = 0
}
s.mu.Unlock()
if len(sbBytes) > 0 {
if _, err := s.fd.WriteAt(sbBytes, 0); err != nil {
return 0, fmt.Errorf("storage: pwrite direct frontier superblock: %w", err)
}
if err := s.fd.Sync(); err != nil {
return 0, fmt.Errorf("storage: fsync direct frontier superblock: %w", err)
}
}
return frontier, nil
}
@@ -571,6 +600,9 @@ func (s *WALStore) AdvanceFrontier(lsn uint64) {
if lsn > s.walHead {
s.walHead = lsn
}
if lsn > s.pendingDirectFrontierLSN {
s.pendingDirectFrontierLSN = lsn
}
}
// AdvanceWALTail moves the retained-window tail forward. After this,
@@ -83,10 +83,10 @@ func TestFailClosed_OverlappingSessions_OldCannotPolluteNew(t *testing.T) {
// successfully completes, the achieved frontier reflects the NEW
// session's target, and replica data matches the new primary.
//
// Note: post-M2 each rebuild block applies at lineage.TargetLSN, so
// walHead can advance from a partial rebuild even before RebuildDone
// runs. The convergence guarantee is at the data + achieved-frontier
// level, not at "walHead must stay at zero".
// Rebuild blocks are BASE-lane bytes: a partial rebuild may write
// extent data, but it must not advance the replica frontier until
// RebuildDone. The convergence guarantee is at the next complete
// session's data + achieved-frontier level.
func TestFailClosed_PrimaryHalfCrash_NextSessionConverges(t *testing.T) {
_, replica, listener := setupPrimaryReplica(t)
+5 -5
View File
@@ -305,11 +305,11 @@ func (r *ReplicaListener) handleConn(conn net.Conn) {
lineage.SessionID, lineage.Epoch, lineage.EndpointVersion)
return
}
// Rebuild blocks carry the engine's frozen base frontier hint in
// their lineage. Apply that real LSN immediately so any future
// LSN-aware ApplyEntry guard still treats rebuild data as current.
if err := r.store.ApplyEntry(lba, data, lineage.EffectiveFrontierHint()); err != nil {
log.Printf("replica: apply rebuild block: %v", err)
// Rebuild blocks are BASE-lane bytes. They must not enter the
// WAL apply path and must not advance R/S/H per block; the
// frontier is reconciled once, at MsgRebuildDone.
if err := r.store.WriteExtentDirect(lba, data); err != nil {
log.Printf("replica: apply rebuild base block: %v", err)
}
case MsgRebuildDone:
@@ -321,7 +321,7 @@ func TestTransport_ReplicaRejectsStaleMutationLineage(t *testing.T) {
}
}
func TestTransport_RebuildBlock_UsesTargetLSNBeforeDone(t *testing.T) {
func TestTransport_RebuildBlock_DoesNotAdvanceFrontierBeforeDone(t *testing.T) {
_, replica, listener := setupPrimaryReplica(t)
conn, err := net.Dial("tcp", listener.Addr())
@@ -353,8 +353,8 @@ func TestTransport_RebuildBlock_UsesTargetLSNBeforeDone(t *testing.T) {
}
_, _, h := replica.Boundaries()
if h != 77 {
t.Fatalf("rebuild block should advance walHead to targetLSN before done, got H=%d want 77", h)
if h != 0 {
t.Fatalf("rebuild base block must not advance walHead before done, got H=%d want 0", h)
}
}