Merge pull request #135 from alin-amana/fix_eval_blocking_on_fsync

Don't do blocking disk I/O under mutex blocking scrape and eval
This commit is contained in:
Fabian Reinartz 2017-09-01 11:49:25 +02:00 committed by GitHub
commit 0fe67df9f2

29
wal.go
View file

@ -298,22 +298,41 @@ func (w *SegmentWAL) tail() *os.File {
// Sync flushes the changes to disk. // Sync flushes the changes to disk.
func (w *SegmentWAL) Sync() error { func (w *SegmentWAL) Sync() error {
var tail *os.File
var err error
// Flush the writer and retrieve the reference to the tail segment under mutex lock
func() {
w.mtx.Lock() w.mtx.Lock()
defer w.mtx.Unlock() defer w.mtx.Unlock()
if err = w.flush(); err != nil {
return
}
tail = w.tail()
} ()
return w.sync() if err != nil {
return err
}
// But only fsync the tail segment after releasing the mutex as it will block on disk I/O
return fileutil.Fdatasync(tail)
} }
func (w *SegmentWAL) sync() error { func (w *SegmentWAL) sync() error {
if w.cur == nil { if err := w.flush(); err != nil {
return nil
}
if err := w.cur.Flush(); err != nil {
return err return err
} }
return fileutil.Fdatasync(w.tail()) return fileutil.Fdatasync(w.tail())
} }
func (w *SegmentWAL) flush() error {
if w.cur == nil {
return nil
}
return w.cur.Flush()
}
func (w *SegmentWAL) run(interval time.Duration) { func (w *SegmentWAL) run(interval time.Duration) {
var tick <-chan time.Time var tick <-chan time.Time