From 1078081aec0a1b68beaf22ad483347d732bb0b48 Mon Sep 17 00:00:00 2001 From: Xiaochao Dong Date: Thu, 4 Aug 2022 16:09:14 +0800 Subject: [PATCH] Fix race condition when updating lastSeriesID during loading chunk snapshot (#11099) Signed-off-by: Xiaochao Dong (@damnever) --- tsdb/head_wal.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tsdb/head_wal.go b/tsdb/head_wal.go index f6990d8bf..277410ad1 100644 --- a/tsdb/head_wal.go +++ b/tsdb/head_wal.go @@ -921,8 +921,12 @@ func (h *Head) loadChunkSnapshot() (int, int, map[chunks.HeadSeriesRef]*memSerie return } localRefSeries[csr.ref] = series - if chunks.HeadSeriesRef(h.lastSeriesID.Load()) < series.ref { - h.lastSeriesID.Store(uint64(series.ref)) + for { + seriesID := uint64(series.ref) + lastSeriesID := h.lastSeriesID.Load() + if lastSeriesID >= seriesID || h.lastSeriesID.CAS(lastSeriesID, seriesID) { + break + } } series.chunkRange = csr.chunkRange