test(storage): stale pages are never completed; positions ahead are refused

Three reds on the production store: a header written over pages that miss
an old writer's acknowledged write after a half fence; an unfinished record
whose pages are ahead of the recovered store; a complete record whose header
position is ahead of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-30 03:42:37 -08:00
1 parent 0a2d86dc44
commit 0e1b46eed4
1 file changed
+61
@@ -210,3 +210,64 @@ func TestDamagedPage(t *testing.T) {
t.Fatalf("complete record with a damaged page: %+v %v", got, err)
}
}
// Stale pages cannot be completed: the fence keeps the old version out from
// then on, but the pages already miss its write; the header is refused.
func TestStalePagesAreNeverCompleted(t *testing.T) {
s, path := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 1, 2)
dir := chunkRecordDir(path)
chunks := recordPagesAt(t, s, dir, s.NextLSN()-1)
halfFence(t, s, 7)
_ = s.Close()
old := reopenRecovered(t, path)
writeSynced(t, old, 3, 3)
crashStore(t, old)
next := reopenRecovered(t, path)
if err := completeChunkRecord(next, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks, G: next.NextLSN() - 1}); err == nil {
t.Fatal("a header was written over pages that miss an acknowledged write")
}
if _, err := os.Stat(filepath.Join(dir, chunkRecordHeaderName)); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("header exists over stale pages: %v", err)
}
}
// A position ahead of the recovered store is not durable evidence: an
// unfinished record with a page ahead is not used.
func TestUnfinishedPagesAheadOfTheStoreAreNotTrusted(t *testing.T) {
s, path := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 1, 2)
dir := chunkRecordDir(path)
pos := s.NextLSN() - 1
chunks := recordPagesAt(t, s, dir, pos+5)
got, err := loadChunkRecord(dir, chunks, true, 7, pos)
if err != nil {
t.Fatal(err)
}
if got.Pages != nil || got.Discarded == "" {
t.Fatalf("pages ahead of the store were trusted: %+v", got)
}
}
// A complete record whose header position is ahead of the store is not used.
func TestCompleteRecordAheadOfTheStoreIsNotTrusted(t *testing.T) {
s, path := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 1, 2)
dir := chunkRecordDir(path)
pos := s.NextLSN() - 1
chunks := recordPagesAt(t, s, dir, pos)
if err := completeChunkRecord(s, dir, chunkRecordHeader{Epoch: 7, Chunks: chunks, G: pos}); err != nil {
t.Fatal(err)
}
if err := writeChunkRecordFile(dir, chunkRecordHeaderName, encodeChunkRecordHeader(chunkRecordHeader{Epoch: 7, Chunks: chunks, G: pos + 5})); err != nil {
t.Fatal(err)
}
got, err := loadChunkRecord(dir, chunks, true, 7, pos)
if err != nil {
t.Fatal(err)
}
if got.Complete || got.Pages != nil {
t.Fatalf("a header ahead of the store was trusted: %+v", got)
}
}