Skip to content

Commit

Permalink
Hold the lock for a msg block while calling sync.
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison authored and neilalexander committed Apr 11, 2024
1 parent 63d2d71 commit c0068cc
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5124,7 +5124,7 @@ func (fs *fileStore) syncBlocks() {
}

// Check if we need to sync. We will not hold lock during actual sync.
needSync, fn := mb.needSync, mb.mfn
needSync := mb.needSync
if needSync {
// Flush anything that may be pending.
mb.flushPendingMsgsLocked()
Expand All @@ -5141,23 +5141,32 @@ func (fs *fileStore) syncBlocks() {
fs.mu.RUnlock()
}

// Check if we need to sync.
// This is done not holding any locks.
// Check if we need to sync this block.
if needSync {
<-dios
fd, _ := os.OpenFile(fn, os.O_RDWR, defaultFilePerms)
dios <- struct{}{}
mb.mu.Lock()
var fd *os.File
var didOpen bool
if mb.mfd != nil {
fd = mb.mfd
} else {
<-dios
fd, _ = os.OpenFile(mb.mfn, os.O_RDWR, defaultFilePerms)
dios <- struct{}{}
didOpen = true
}
// If we have an fd.
if fd != nil {
canClear := fd.Sync() == nil
fd.Close()
// If we opened the file close the fd.
if didOpen {
fd.Close()
}
// Only clear sync flag on success.
if canClear {
mb.mu.Lock()
mb.needSync = false
mb.mu.Unlock()
}
}
mb.mu.Unlock()
}
}

Expand Down

0 comments on commit c0068cc

Please sign in to comment.