Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions internal/journal/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion internal/journal/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions tests/test-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading