diff --git a/lib/filestream/filestream.go b/lib/filestream/filestream.go index f4c0d790f..9fbe35872 100644 --- a/lib/filestream/filestream.go +++ b/lib/filestream/filestream.go @@ -254,11 +254,17 @@ func (w *Writer) Write(p []byte) (int, error) { } // MustFlush flushes all the buffered data to file. -func (w *Writer) MustFlush() { +// +// if isSync is true, then the flushed data is fsynced to the underlying storage. +func (w *Writer) MustFlush(isSync bool) { if err := w.bw.Flush(); err != nil { logger.Panicf("FATAL: cannot flush buffered data to file %q: %s", w.f.Name(), err) } - // Do not call w.f.Sync() for performance reasons. + if isSync { + if err := w.f.Sync(); err != nil { + logger.Panicf("FATAL: cannot fsync data to the underlying storage for file %q: %s", w.f.Name(), err) + } + } } type statWriter struct { diff --git a/lib/persistentqueue/persistentqueue.go b/lib/persistentqueue/persistentqueue.go index 72b63ab40..c003a3507 100644 --- a/lib/persistentqueue/persistentqueue.go +++ b/lib/persistentqueue/persistentqueue.go @@ -546,7 +546,7 @@ func (q *Queue) write(buf []byte) error { func (q *Queue) readFull(buf []byte) error { bufLen := uint64(len(buf)) if q.readerOffset+bufLen > q.writerFlushedOffset { - q.writer.MustFlush() + q.writer.MustFlush(false) q.writerFlushedOffset = q.writerOffset } n, err := io.ReadFull(q.reader, buf) @@ -567,8 +567,7 @@ func (q *Queue) flushMetainfoIfNeededLocked(flushData bool) error { return nil } if flushData { - q.writer.MustFlush() - fs.MustSyncPath(q.writerPath) + q.writer.MustFlush(true) } if err := q.flushMetainfoLocked(); err != nil { return fmt.Errorf("cannot flush metainfo: %w", err)