Skip to content

Commit

Permalink
NRG: Always write term/vote and peer state synchronously
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
neilalexander committed Aug 29, 2024
1 parent 5b9de02 commit 77b954f
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 @@ -9746,14 +9746,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 @@ -1055,11 +1055,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 @@ -3755,12 +3751,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 @@ -3784,12 +3775,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 77b954f

Please sign in to comment.