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 93cb319 commit e55687c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/store/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,9 @@ type bucketChunkReader struct {
mtx sync.Mutex
stats *queryStats
chunkBytes []*[]byte // Byte slice to return to the chunk pool on close.

loadingChunks bool
finishLoadingChks chan struct{}
}

func newBucketChunkReader(block *bucketBlock) *bucketChunkReader {
Expand All @@ -3174,9 +3177,18 @@ func (r *bucketChunkReader) reset() {
for i := range r.toLoad {
r.toLoad[i] = r.toLoad[i][:0]
}
if r.finishLoadingChks != nil {
<-r.finishLoadingChks
}
r.finishLoadingChks = make(chan struct{})
}

func (r *bucketChunkReader) Close() error {
// NOTE(GiedriusS): we need to wait until loading chunks because loading
// chunks modifies r.block.chunkPool.
if r.loadingChunks {
<-r.finishLoadingChks
}
r.block.pendingReaders.Done()

for _, b := range r.chunkBytes {
Expand All @@ -3201,6 +3213,12 @@ func (r *bucketChunkReader) addLoad(id chunks.ChunkRef, seriesEntry, chunk int)

// load loads all added chunks and saves resulting aggrs to refs.
func (r *bucketChunkReader) load(ctx context.Context, res []seriesEntry, aggrs []storepb.Aggr, calculateChunkChecksum bool, bytesLimiter BytesLimiter) error {
r.loadingChunks = true
defer func() {
r.loadingChunks = false
close(r.finishLoadingChks)
}()

g, ctx := errgroup.WithContext(ctx)

for seq, pIdxs := range r.toLoad {
Expand Down

0 comments on commit e55687c

Please sign in to comment.