Skip to content

Commit

Permalink
store/bucket: wait until chunk loading ends in Close()
Browse files Browse the repository at this point in the history
Chunk reader needs to wait until the chunk loading ends in Close()
because otherwise there will be a race between appending to r.chunkBytes
and reading from it.

Signed-off-by: Giedrius Statkevičius <[email protected]>
  • Loading branch information
GiedriusS committed Aug 9, 2023
1 parent eb80318 commit 08da27e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/store/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,8 @@ type bucketChunkReader struct {
mtx sync.Mutex
stats *queryStats
chunkBytes []*[]byte // Byte slice to return to the chunk pool on close.

chunkLoading sync.WaitGroup
}

func newBucketChunkReader(block *bucketBlock) *bucketChunkReader {
Expand All @@ -3170,6 +3172,9 @@ func (r *bucketChunkReader) reset() {
}

func (r *bucketChunkReader) Close() error {
// Wait until any chunk loading ends.
r.chunkLoading.Wait()

r.block.pendingReaders.Done()

for _, b := range r.chunkBytes {
Expand Down Expand Up @@ -3211,6 +3216,7 @@ func (r *bucketChunkReader) load(ctx context.Context, res []seriesEntry, aggrs [
r.stats.DataDownloadedSizeSum += units.Base2Bytes(p.End - p.Start)
}

r.chunkLoading.Add(len(parts))
for _, p := range parts {
seq := seq
p := p
Expand All @@ -3234,6 +3240,7 @@ func (r *bucketChunkReader) loadChunks(ctx context.Context, res []seriesEntry, a
}
r.stats.ChunksFetchDurationSum += time.Since(fetchBegin)
r.mtx.Unlock()
r.chunkLoading.Done()
}()

// Get a reader for the required range.
Expand Down

0 comments on commit 08da27e

Please sign in to comment.