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
21 changes: 9 additions & 12 deletions internal/journal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,21 @@ func (j *Journal) Stop() error {
close(j.checkpointStop)
j.checkpointWg.Wait()

// Mark journal clean on orderly shutdown
j.mu.Lock()
defer j.mu.Unlock()

// Leave the journal dirty so the next mount replays what may not have synced.
if flushErr != nil {
stopErr = fmt.Errorf("failed to flush batched commits: %w", flushErr)
return
}

if len(j.activeTxns) == 0 && len(j.pendingIntents) == 0 {
var cleanErr error
switch {
case flushErr != nil:
// Leave the journal dirty so the next mount replays what may not have synced.
cleanErr = fmt.Errorf("failed to flush batched commits: %w", flushErr)
case len(j.activeTxns) == 0 && len(j.pendingIntents) == 0:
// Mark journal clean on orderly shutdown
if err := j.region.MarkClean(); err != nil {
stopErr = fmt.Errorf("failed to mark journal clean: %w", err)
return
cleanErr = fmt.Errorf("failed to mark journal clean: %w", err)
}
}

stopErr = j.region.Close()
stopErr = errors.Join(cleanErr, j.region.Close())
})
return stopErr
}
Expand Down
69 changes: 56 additions & 13 deletions internal/journal/journal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package journal

import (
"errors"
"fmt"
"os"
"sync"
Expand Down Expand Up @@ -679,19 +680,7 @@ func TestBatchCommitReturnsSyncError(t *testing.T) {
result <- err
}()

deadline := time.Now().Add(5 * time.Second)
for {
j.batchMu.Lock()
pending := j.batchPendingCount
j.batchMu.Unlock()
if pending == 1 {
break
}
if time.Now().After(deadline) {
t.Fatal("commit never joined a batch")
}
time.Sleep(time.Millisecond)
}
waitForPendingBatch(t, j)
if err := dev.Close(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -734,3 +723,57 @@ func TestCheckpointAdvancesHeadNow(t *testing.T) {
t.Fatalf("head %d != tail %d after Checkpoint", head, tail)
}
}

func waitForPendingBatch(t *testing.T, j *Journal) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for {
j.batchMu.Lock()
pending := j.batchPendingCount
j.batchMu.Unlock()
if pending > 0 {
return
}
if time.Now().After(deadline) {
t.Fatal("commit never joined a batch")
}
time.Sleep(time.Millisecond)
}
}

// Stop reports a final batch flush that failed, and a second Stop does nothing.
func TestStopReportsFailedBatchFlush(t *testing.T) {
dev, cleanup := createTestDevice(t, 10*1024*1024)
defer cleanup()
config := DefaultConfig()
config.CommitMode = CommitBatch
config.BatchConfig = BatchConfig{MaxBatchTransactions: 100, MaxBatchAge: time.Minute}
j, err := Init(dev, 0, DefaultJournalSlotSize, [16]byte{1}, 1, config)
if err != nil {
t.Fatalf("Init failed: %v", err)
}

txn := j.Begin()
if err := txn.LogInodeCreate(testInodePayload(100)); err != nil {
t.Fatal(err)
}
result := make(chan error, 1)
go func() {
_, err := j.Commit(txn)
result <- err
}()
waitForPendingBatch(t, j)
if err := dev.Close(); err != nil {
t.Fatal(err)
}

if err := j.Stop(); !errors.Is(err, os.ErrInvalid) {
t.Fatalf("Stop = %v, want the failed sync's error", err)
}
if err := <-result; err == nil {
t.Fatal("Commit returned nil after its batch sync failed")
}
if err := j.Stop(); err != nil {
t.Fatalf("second Stop = %v, want nil", err)
}
}
7 changes: 7 additions & 0 deletions tests/test-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,13 @@ scenarios:
environments:
go: internal/journal/journal_test.go::TestBatchCommitReturnsSyncError

- id: journal-stop-failed-flush
name: Stop reports a failed final flush
description: Journal Stop returns the error of a final batch flush that failed, the waiting commit gets it too, and a second Stop does nothing
priority: go_only
environments:
go: internal/journal/journal_test.go::TestStopReportsFailedBatchFlush

- id: checkpoint-keeps-incomplete-intents
name: Checkpoints never pass an intent that has not completed
description: A transaction committed but not yet applied stays in the journal through a checkpoint and an unmount, and the next mount replays it; completed intents are checkpointed away
Expand Down
Loading