From e4516a86700d8e842084a46d21fa8a71ad8379d3 Mon Sep 17 00:00:00 2001 From: lzle Date: Fri, 19 Jan 2024 13:36:22 +0800 Subject: [PATCH] fix:when the shard is created, disk iops is high, causing http write timeout. --- tsdb/index/tsi1/log_file.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tsdb/index/tsi1/log_file.go b/tsdb/index/tsi1/log_file.go index 03d1763ea4e..5b3fd07a9cf 100644 --- a/tsdb/index/tsi1/log_file.go +++ b/tsdb/index/tsi1/log_file.go @@ -8,6 +8,7 @@ import ( "fmt" "hash/crc32" "io" + "math/rand" "os" "sort" "sync" @@ -215,6 +216,22 @@ func (f *LogFile) FlushAndSync() error { return f.file.Sync() } +// FlushAndSync flushes buffered data to disk +// If the LogFile has disabled flushing and syncing then FlushAndSync is a no-op. +func (f *LogFile) Flush() error { + if f.nosync { + return nil + } + + if f.w != nil { + if err := f.w.Flush(); err != nil { + return err + } + } + + return nil +} + // ID returns the file sequence identifier. func (f *LogFile) ID() int { return f.id } @@ -565,10 +582,18 @@ func (f *LogFile) AddSeriesList(seriesSet *tsdb.SeriesIDSet, names [][]byte, tag seriesSet.AddNoLock(entry.SeriesID) } - // Flush buffer and sync to disk. - if err := f.FlushAndSync(); err != nil { - return nil, err + if rand.Intn(100) < 10 { + // Flush buffer and sync to disk. + if err := f.FlushAndSync(); err != nil { + return nil, err + } + } else { + // Flush buffer. + if err := f.Flush(); err != nil { + return nil, err + } } + return seriesIDs, nil }