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 { 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