Skip to content

Commit

Permalink
NRG (2.11): Always write term/vote and peer state synchronously (#5848)
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison authored Oct 29, 2024
2 parents 4428dd9 + 53c85c2 commit 86944bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
16 changes: 12 additions & 4 deletions server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9834,14 +9834,22 @@ func (alg StoreCompression) Decompress(buf []byte) ([]byte, error) {
// sets O_SYNC on the open file if SyncAlways is set. The dios semaphore is
// handled automatically by this function, so don't wrap calls to it in dios.
func (fs *fileStore) writeFileWithOptionalSync(name string, data []byte, perm fs.FileMode) error {
if fs.fcfg.SyncAlways {
return writeFileWithSync(name, data, perm)
}
<-dios
defer func() {
dios <- struct{}{}
}()
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
if fs.fcfg.SyncAlways {
flags |= os.O_SYNC
}
return os.WriteFile(name, data, perm)
}

func writeFileWithSync(name string, data []byte, perm fs.FileMode) error {
<-dios
defer func() {
dios <- struct{}{}
}()
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_SYNC
f, err := os.OpenFile(name, flags, perm)
if err != nil {
return err
Expand Down
20 changes: 3 additions & 17 deletions server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,11 +1165,7 @@ func (n *raft) installSnapshot(snap *snapshot) error {
sn := fmt.Sprintf(snapFileT, snap.lastTerm, snap.lastIndex)
sfile := filepath.Join(snapDir, sn)

<-dios
err := os.WriteFile(sfile, n.encodeSnapshot(snap), defaultFilePerms)
dios <- struct{}{}

if err != nil {
if err := writeFileWithSync(sfile, n.encodeSnapshot(snap), defaultFilePerms); err != nil {
// We could set write err here, but if this is a temporary situation, too many open files etc.
// we want to retry and snapshots are not fatal.
return err
Expand Down Expand Up @@ -3874,12 +3870,7 @@ func writePeerState(sd string, ps *peerState) error {
if _, err := os.Stat(psf); err != nil && !os.IsNotExist(err) {
return err
}

<-dios
err := os.WriteFile(psf, encodePeerState(ps), defaultFilePerms)
dios <- struct{}{}

return err
return writeFileWithSync(psf, encodePeerState(ps), defaultFilePerms)
}

func readPeerState(sd string) (ps *peerState, err error) {
Expand All @@ -3903,12 +3894,7 @@ func writeTermVote(sd string, wtv []byte) error {
if _, err := os.Stat(psf); err != nil && !os.IsNotExist(err) {
return err
}

<-dios
err := os.WriteFile(psf, wtv, defaultFilePerms)
dios <- struct{}{}

return err
return writeFileWithSync(psf, wtv, defaultFilePerms)
}

// readTermVote will read the largest term and who we voted from to stable storage.
Expand Down

0 comments on commit 86944bc

Please sign in to comment.