From 015df288d0496ec4f911477949f1154665199bcd Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Tue, 1 Aug 2023 11:19:57 -0700 Subject: [PATCH] fix data race of chunk reader stats Signed-off-by: Ben Ye --- pkg/store/bucket.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index c93f00ac2a..6bcbf48893 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -3214,9 +3214,14 @@ func (r *bucketChunkReader) load(ctx context.Context, res []seriesEntry, aggrs [ // loadChunks will read range [start, end] from the segment file with sequence number seq. // This data range covers chunks starting at supplied offsets. func (r *bucketChunkReader) loadChunks(ctx context.Context, res []seriesEntry, aggrs []storepb.Aggr, seq int, part Part, pIdxs []loadIdx, calculateChunkChecksum bool, bytesLimiter BytesLimiter) error { + var locked bool fetchBegin := time.Now() defer func() { + if !locked { + r.mtx.Lock() + } r.stats.ChunksFetchDurationSum += time.Since(fetchBegin) + r.mtx.Unlock() }() // Get a reader for the required range. @@ -3227,15 +3232,9 @@ func (r *bucketChunkReader) loadChunks(ctx context.Context, res []seriesEntry, a defer runutil.CloseWithLogOnErr(r.block.logger, reader, "readChunkRange close range reader") bufReader := bufio.NewReaderSize(reader, r.block.estimatedMaxChunkSize) - locked := true + locked = true r.mtx.Lock() - defer func() { - if locked { - r.mtx.Unlock() - } - }() - r.stats.chunksFetchCount++ r.stats.chunksFetched += len(pIdxs) r.stats.ChunksFetchedSizeSum += units.Base2Bytes(int(part.End - part.Start))