recovery: ack base batches at durable frontier

This commit is contained in:
pingqiu committed 2026-05-02 10:46:15 -07:00
1 parent 36dd0231cb
commit e356cb3460
2 files changed
+131 -7

No files matched your search

+14 -7
View File
@@ -308,21 +308,28 @@ func (r *Receiver) shouldAck() bool {
// sendAck writes a frameBaseBatchAck with the receiver's current
// AcknowledgedLSN and BaseLBAUpper. Resets cadence counters.
//
// AcknowledgedLSN semantics (POC): uses session.Status().WALApplied,
// the highest WAL LSN this session has applied. For real WAL substrate
// in production, the receiver should call store.Sync() first and use
// the returned syncedLSN to claim true durability — see
// docs/recovery-pin-floor-wire.md §3 ("During base lane: min(walApplied,
// syncedLSN-at-ack-time)"). POC trusts in-memory durability.
// AcknowledgedLSN semantics: receiver ack is a durable claim, not an
// in-memory apply claim. The replica first flushes its local substrate,
// then reports min(session WALApplied, syncedLSN-at-ack-time). This is
// the value the primary uses to advance pin_floor; over-claiming here
// can recycle WAL that the replica has not actually made durable.
func (r *Receiver) sendAck() error {
if r.session == nil {
return newFailure(FailureProtocol, PhaseRecvAckWrite,
errors.New("sendAck before SessionStart"))
}
st := r.session.Status()
syncedLSN, err := r.store.Sync()
if err != nil {
return newFailure(FailureSubstrate, PhaseRecvAckWrite, err)
}
acknowledgedLSN := st.WALApplied
if syncedLSN < acknowledgedLSN {
acknowledgedLSN = syncedLSN
}
payload := encodeBaseBatchAck(baseBatchAckPayload{
SessionID: r.sessionID,
AcknowledgedLSN: st.WALApplied,
AcknowledgedLSN: acknowledgedLSN,
BaseLBAUpper: r.baseInstalledUpper,
})
if err := writeFrame(r.conn, frameBaseBatchAck, payload); err != nil {
@@ -81,3 +81,120 @@ func TestReceiver_SessionStart_UsesFromLSNAsBaseFrontierHint(t *testing.T) {
t.Fatalf("TargetLSN legacy status alias=%d want BaseFrontierHint %d", st.TargetLSN, basePinLSN)
}
}
func TestReceiver_BaseBatchAck_DoesNotExceedDurableSyncFrontier(t *testing.T) {
store := &receiverAckStore{
numBlocks: 4,
blockSize: 4096,
syncFrontier: 7,
}
primaryConn, replicaConn := net.Pipe()
defer primaryConn.Close()
defer replicaConn.Close()
session := NewRebuildSession(store, 0)
if err := session.ApplyWALEntry(WALKindSessionLive, 1, make([]byte, store.blockSize), 10); err != nil {
t.Fatalf("ApplyWALEntry: %v", err)
}
receiver := NewReceiver(store, replicaConn)
receiver.sessionID = 99
receiver.session = session
receiver.baseInstalledUpper = 3
errCh := make(chan error, 1)
go func() {
errCh <- receiver.sendAck()
}()
ft, payload, err := readFrame(primaryConn)
if err != nil {
t.Fatalf("read BaseBatchAck: %v", err)
}
if ft != frameBaseBatchAck {
t.Fatalf("frame type=%v want BaseBatchAck", ft)
}
ack, err := decodeBaseBatchAck(payload)
if err != nil {
t.Fatalf("decode BaseBatchAck: %v", err)
}
if err := <-errCh; err != nil {
t.Fatalf("sendAck: %v", err)
}
if ack.AcknowledgedLSN != store.syncFrontier {
t.Fatalf("AcknowledgedLSN=%d want durable sync frontier %d; WALApplied=10 must not overclaim",
ack.AcknowledgedLSN, store.syncFrontier)
}
if ack.BaseLBAUpper != receiver.baseInstalledUpper {
t.Fatalf("BaseLBAUpper=%d want %d", ack.BaseLBAUpper, receiver.baseInstalledUpper)
}
if store.syncCalls != 1 {
t.Fatalf("Sync calls=%d want 1", store.syncCalls)
}
}
type receiverAckStore struct {
numBlocks uint32
blockSize int
syncFrontier uint64
head uint64
syncCalls int
}
func (s *receiverAckStore) Write(lba uint32, data []byte) (uint64, error) {
s.head++
return s.head, nil
}
func (s *receiverAckStore) Read(lba uint32) ([]byte, error) {
return make([]byte, s.blockSize), nil
}
func (s *receiverAckStore) Sync() (uint64, error) {
s.syncCalls++
return s.syncFrontier, nil
}
func (s *receiverAckStore) Recover() (uint64, error) { return s.syncFrontier, nil }
func (s *receiverAckStore) Boundaries() (uint64, uint64, uint64) {
return s.syncFrontier, 0, s.head
}
func (s *receiverAckStore) NextLSN() uint64 { return s.head + 1 }
func (s *receiverAckStore) NumBlocks() uint32 { return s.numBlocks }
func (s *receiverAckStore) BlockSize() int { return s.blockSize }
func (s *receiverAckStore) AdvanceFrontier(lsn uint64) {
if lsn > s.head {
s.head = lsn
}
}
func (s *receiverAckStore) AdvanceWALTail(newTail uint64) {}
func (s *receiverAckStore) ApplyEntry(lba uint32, data []byte, lsn uint64) error {
if lsn > s.head {
s.head = lsn
}
return nil
}
func (s *receiverAckStore) WriteExtentDirect(lba uint32, data []byte) error { return nil }
func (s *receiverAckStore) AllBlocks() map[uint32][]byte { return nil }
func (s *receiverAckStore) ScanLBAs(fromLSN uint64, fn func(storage.RecoveryEntry) error) error {
return nil
}
func (s *receiverAckStore) RecoveryMode() storage.RecoveryMode {
return storage.RecoveryModeWALReplay
}
func (s *receiverAckStore) AppliedLSNs() (map[uint32]uint64, error) {
return nil, storage.ErrAppliedLSNsNotTracked
}
func (s *receiverAckStore) Close() error { return nil }