From 88088d9da56a10c6751953f8a65f9cb34599ce05 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Thu, 12 Nov 2026 05:24:41 -0600 Subject: [PATCH 1/2] fix(journal): keep the replay head at the end of the data area When the last replayed record ended exactly at the end of the data area, replay recorded its progress as head 0 with the tail still at the end, which reads as a full ring. If recovery then stopped before resetting the journal, after losing its lease or in a crash, the next recovery scanned the whole ring from 0 and replayed records again. The head now stays at the end, so head == tail and the journal reads as empty. Co-Authored-By: Claude Opus 5.5 --- internal/journal/checkpoint_test.go | 43 +++++++++++++++++++++++++++++ internal/journal/recovery.go | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/journal/checkpoint_test.go b/internal/journal/checkpoint_test.go index 2588877c..2d113cec 100644 --- a/internal/journal/checkpoint_test.go +++ b/internal/journal/checkpoint_test.go @@ -193,3 +193,46 @@ func TestScanStopsAtUnreachableTail(t *testing.T) { t.Fatal("scan still running after 10s with an unreachable tail") } } + +// Replay that stops after the last record, which ends at the end of the data area, leaves head == tail, which reads as +// empty: a head of 0 would read as a full ring and replay the previous lap's records again. +func TestStoppedReplayAtEndLeavesJournalEmpty(t *testing.T) { + dev, cleanup := createTestDevice(t, 1024*1024) + t.Cleanup(cleanup) + j, err := Init(dev, 0, uint64(JournalDataOffset)+3*4096, [16]byte{1}, 1, DefaultConfig()) + if err != nil { + t.Fatal(err) + } + commitIntents(t, j, true) + if _, err := j.Checkpoint(); err != nil { + t.Fatal(err) + } + pending := commitIntents(t, j, false) + if tail := j.region.TailOffset(); tail != j.region.DataAreaSize() { + t.Fatalf("tail %d, want the end of the %d-byte data area", tail, j.region.DataAreaSize()) + } + + region, err := OpenRegionForRecovery(dev, 0, j.region.regionSize) + if err != nil { + t.Fatal(err) + } + var replayed []uint64 + checks := 0 + lostLease := errors.New("lost the recovery lease") + _, err = RecoverChecked(region, func(txn *RecoveredTxn) error { + replayed = append(replayed, txn.Header.Seq) + return nil + }, func() error { + if checks++; checks == 3 { + return lostLease + } + return nil + }) + if !errors.Is(err, lostLease) || !slices.Equal(replayed, pending) { + t.Fatalf("first replay = %v, %v; want %v, then the lost lease", replayed, err, pending) + } + + if again := replayedAfterCrash(t, j); len(again) > 0 { + t.Errorf("replay after the stopped one replayed %v again", again) + } +} diff --git a/internal/journal/recovery.go b/internal/journal/recovery.go index 53da5b43..91b1d56e 100644 --- a/internal/journal/recovery.go +++ b/internal/journal/recovery.go @@ -312,7 +312,7 @@ func scanJournal(region *Region, handler RecoveryHandler, stats *RecoveryStats, } if advance != nil { next := pos + ((txnHeader.TotalLength+storage.SectorSize-1)/storage.SectorSize)*storage.SectorSize - if next >= dataAreaSize { + if next > dataAreaSize { next -= dataAreaSize } if err := advance(txnHeader.Seq, next); err != nil { From e90710651cd64169547d91b0299c5dfda18ddcd0 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Thu, 12 Nov 2026 05:34:56 -0600 Subject: [PATCH 2/2] test: track the stopped-replay test in the manifest Co-Authored-By: Claude Opus 5.5 --- tests/test-manifest.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index 5d200868..762265bf 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -735,6 +735,13 @@ scenarios: environments: go: internal/journal/checkpoint_test.go::TestScanStopsAtUnreachableTail + - id: stopped-replay-at-end-reads-empty + name: A replay stopped after the last record leaves an empty journal + description: Replay stops before its reset after replaying a record that ends at the end of the data area; the head stays at the end, so the next recovery replays nothing again + priority: go_only + environments: + go: internal/journal/checkpoint_test.go::TestStoppedReplayAtEndLeavesJournalEmpty + - id: create-replays-after-checkpoint-crash name: A create cut off after a checkpoint is replayed description: A create stops between its journal commit and its changes, a checkpoint runs, and the process dies; replay at the next mount creates the file and fsck is clean