From 454b3a2a618f973600df72b55d80dca99d2f2ab8 Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Fri, 4 Aug 2017 10:36:41 -0500 Subject: [PATCH] Pick up raft library change that fsyncs snapshot files correctly --- .../hashicorp/raft/file_snapshot.go | 44 +++++++++++++++---- vendor/vendor.json | 2 +- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/vendor/github.com/hashicorp/raft/file_snapshot.go b/vendor/github.com/hashicorp/raft/file_snapshot.go index 7ec7b7a759..119bfd308d 100644 --- a/vendor/github.com/hashicorp/raft/file_snapshot.go +++ b/vendor/github.com/hashicorp/raft/file_snapshot.go @@ -37,10 +37,11 @@ type snapMetaSlice []*fileSnapshotMeta // FileSnapshotSink implements SnapshotSink with a file. type FileSnapshotSink struct { - store *FileSnapshotStore - logger *log.Logger - dir string - meta fileSnapshotMeta + store *FileSnapshotStore + logger *log.Logger + dir string + parentDir string + meta fileSnapshotMeta stateFile *os.File stateHash hash.Hash64 @@ -158,9 +159,10 @@ func (f *FileSnapshotStore) Create(version SnapshotVersion, index, term uint64, // Create the sink sink := &FileSnapshotSink{ - store: f, - logger: f.logger, - dir: path, + store: f, + logger: f.logger, + dir: path, + parentDir: f.path, meta: fileSnapshotMeta{ SnapshotMeta: SnapshotMeta{ Version: version, @@ -404,6 +406,19 @@ func (s *FileSnapshotSink) Close() error { return err } + // fsync the parent directory, to sync directory edits to disk + parentFH, err := os.Open(s.parentDir) + defer parentFH.Close() + if err != nil { + s.logger.Printf("[ERR] snapshot: Failed to open snapshot parent directory %v, error: %v", s.parentDir, err) + return err + } + + if err = parentFH.Sync(); err != nil { + s.logger.Printf("[ERR] snapshot: Failed syncing parent directory %v, error: %v", s.parentDir, err) + return err + } + // Reap any old snapshots if err := s.store.ReapSnapshots(); err != nil { return err @@ -437,6 +452,11 @@ func (s *FileSnapshotSink) finalize() error { return err } + // Sync to force fsync to disk + if err := s.stateFile.Sync(); err != nil { + return err + } + // Get the file size stat, statErr := s.stateFile.Stat() @@ -468,13 +488,21 @@ func (s *FileSnapshotSink) writeMeta() error { // Buffer the file IO buffered := bufio.NewWriter(fh) - defer buffered.Flush() // Write out as JSON enc := json.NewEncoder(buffered) if err := enc.Encode(&s.meta); err != nil { return err } + + if err = buffered.Flush(); err != nil { + return err + } + + if err = fh.Sync(); err != nil { + return err + } + return nil } diff --git a/vendor/vendor.json b/vendor/vendor.json index d847b02a23..82b254f0e2 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -57,7 +57,7 @@ {"checksumSHA1":"vt+P9D2yWDO3gdvdgCzwqunlhxU=","path":"github.com/hashicorp/logutils","revision":"0dc08b1671f34c4250ce212759ebd880f743d883","revisionTime":"2015-06-09T07:04:31Z"}, {"checksumSHA1":"zcZtXfxrusJpcaPeGJOBnPG1xjs=","path":"github.com/hashicorp/memberlist","revision":"99594a4f171a77cb7cff01f143ccf608ac577c47","revisionTime":"2017-07-17T19:31:21Z"}, {"checksumSHA1":"qnlqWJYV81ENr61SZk9c65R1mDo=","path":"github.com/hashicorp/net-rpc-msgpackrpc","revision":"a14192a58a694c123d8fe5481d4a4727d6ae82f3","revisionTime":"2015-11-16T02:03:38Z"}, - {"checksumSHA1":"OCPP4JxnuSSmweEL9khCd6OdIts=","path":"github.com/hashicorp/raft","revision":"e45173826775c4b782961c7b5758ba484b91464b","revisionTime":"2017-07-10T17:20:01Z","version":"library-v2-stage-one","versionExact":"library-v2-stage-one"}, + {"checksumSHA1":"5GHIYEtOr1rsHOZUac6RA/82d3I=","path":"github.com/hashicorp/raft","revision":"0a6e1b039ba3d8057e9f16c919d2afb813884f74","revisionTime":"2017-08-04T15:11:58Z","version":"library-v2-stage-one","versionExact":"library-v2-stage-one"}, {"checksumSHA1":"QAxukkv54/iIvLfsUP6IK4R0m/A=","path":"github.com/hashicorp/raft-boltdb","revision":"d1e82c1ec3f15ee991f7cc7ffd5b67ff6f5bbaee","revisionTime":"2015-02-01T20:08:39Z"}, {"checksumSHA1":"/oss17GO4hXGM7QnUdI3VzcAHzA=","comment":"v0.7.0-66-g6c4672d","path":"github.com/hashicorp/serf/coordinate","revision":"c2e4be24cdc9031eb0ad869c5d160775efdf7d7a","revisionTime":"2017-05-25T23:15:04Z"}, {"checksumSHA1":"o1VR3OEjCXQW/vT1wpUBOYfGrPQ=","comment":"v0.7.0-66-g6c4672d","path":"github.com/hashicorp/serf/serf","revision":"6669b5d30985da6dd423ecf65f033cee400203fe","revisionTime":"2017-07-07T06:54:45Z"},