Skip to content

Commit

Permalink
tests: disable fsync in file snapshot ops
Browse files Browse the repository at this point in the history
Raft unit tests don't cover crash recovery, and fsync operations are slow
and may trigger election/rpc timeouts unexpectedly.
  • Loading branch information
Mahmood Ali committed Mar 3, 2020
1 parent b47bafa commit a6bc96a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
24 changes: 18 additions & 6 deletions file_snapshot.go
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/go-hclog"
"hash"
"hash/crc64"
"io"
Expand All @@ -16,6 +15,8 @@ import (
"sort"
"strings"
"time"

hclog "github.com/hashicorp/go-hclog"
)

const (
Expand All @@ -32,6 +33,10 @@ type FileSnapshotStore struct {
path string
retain int
logger hclog.Logger

// noSync, if true, skips crash-safe file fsync api calls.
// It's a private field, only used in testing
noSync bool
}

type snapMetaSlice []*fileSnapshotMeta
Expand All @@ -44,6 +49,8 @@ type FileSnapshotSink struct {
parentDir string
meta fileSnapshotMeta

noSync bool

stateFile *os.File
stateHash hash.Hash64
buffered *bufio.Writer
Expand Down Expand Up @@ -172,6 +179,7 @@ func (f *FileSnapshotStore) Create(version SnapshotVersion, index, term uint64,
logger: f.logger,
dir: path,
parentDir: f.path,
noSync: f.noSync,
meta: fileSnapshotMeta{
SnapshotMeta: SnapshotMeta{
Version: version,
Expand Down Expand Up @@ -414,7 +422,7 @@ func (s *FileSnapshotSink) Close() error {
return err
}

if runtime.GOOS != "windows" { // skipping fsync for directory entry edits on Windows, only needed for *nix style file systems
if !s.noSync && runtime.GOOS != "windows" { // skipping fsync for directory entry edits on Windows, only needed for *nix style file systems
parentFH, err := os.Open(s.parentDir)
defer parentFH.Close()
if err != nil {
Expand Down Expand Up @@ -462,8 +470,10 @@ func (s *FileSnapshotSink) finalize() error {
}

// Sync to force fsync to disk
if err := s.stateFile.Sync(); err != nil {
return err
if !s.noSync {
if err := s.stateFile.Sync(); err != nil {
return err
}
}

// Get the file size
Expand Down Expand Up @@ -510,8 +520,10 @@ func (s *FileSnapshotSink) writeMeta() error {
return err
}

if err = fh.Sync(); err != nil {
return err
if !s.noSync {
if err = fh.Sync(); err != nil {
return err
}
}

return nil
Expand Down
1 change: 1 addition & 0 deletions testing.go
Expand Up @@ -808,5 +808,6 @@ func FileSnapTest(t *testing.T) (string, *FileSnapshotStore) {
if err != nil {
t.Fatalf("err: %v", err)
}
snap.noSync = true
return dir, snap
}

0 comments on commit a6bc96a

Please sign in to comment.