Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store-gateways: extract read locking during Series #3546

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions pkg/storegateway/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,34 +873,27 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie

gspan, gctx := tracing.StartSpan(gctx, "bucket_store_preload_all")

s.mtx.RLock()

// Find all blocks owned by this store-gateway instance and matching the request.
blocks := s.blockSet.getFor(req.MinTime, req.MaxTime, req.MaxResolutionWindow, reqBlockMatchers)
if s.debugLogging {
debugFoundBlockSetOverview(s.logger, req.MinTime, req.MaxTime, req.MaxResolutionWindow, blocks)
}

chunkBytes := &pool.BatchBytes{Delegate: s.chunkPool}
defer chunkBytes.Release()

blocks, indexReaders, chunkReaders := s.openBlocksForReading(ctx, req.SkipChunks, req.MinTime, req.MaxTime, req.MaxResolutionWindow, reqBlockMatchers, chunkBytes)

for _, b := range blocks {
b := b

// Keep track of queried blocks.
resHints.AddQueriedBlock(b.meta.ULID)

var chunkr *bucketChunkReader
// We must keep the readers open until all their data has been sent.
indexr := b.indexReader()
if !req.SkipChunks {
chunkr = b.chunkReader(gctx, chunkBytes)
indexr := indexReaders[b.meta.ULID]
defer runutil.CloseWithLogOnErr(s.logger, indexr, "series block")

chunkr := chunkReaders[b.meta.ULID]
if chunkr != nil { // chunkr is nil when skipChunks == true
// Defer all closes to the end of Series method.
defer runutil.CloseWithLogOnErr(s.logger, chunkr, "series block")
}

// Defer all closes to the end of Series method.
defer runutil.CloseWithLogOnErr(s.logger, indexr, "series block")

// If query sharding is enabled we have to get the block-specific series hash cache
// which is used by blockSeries().
var blockSeriesHashCache *hashcache.BlockSeriesHashCache
Expand Down Expand Up @@ -936,8 +929,6 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
})
}

s.mtx.RUnlock()

defer func() {
s.metrics.seriesDataTouched.WithLabelValues("postings").Observe(float64(stats.postingsTouched))
s.metrics.seriesDataFetched.WithLabelValues("postings").Observe(float64(stats.postingsFetched))
Expand Down Expand Up @@ -1051,6 +1042,32 @@ func chunksSize(chks []storepb.AggrChunk) (size int) {
return size
}

func (s *BucketStore) openBlocksForReading(ctx context.Context, skipChunks bool, minT, maxT, maxResolutionMillis int64, blockMatchers []*labels.Matcher, chunkPool *pool.BatchBytes) ([]*bucketBlock, map[ulid.ULID]*bucketIndexReader, map[ulid.ULID]*bucketChunkReader) {
s.mtx.RLock()
defer s.mtx.RUnlock()

// Find all blocks owned by this store-gateway instance and matching the request.
blocks := s.blockSet.getFor(minT, maxT, maxResolutionMillis, blockMatchers)
if s.debugLogging {
debugFoundBlockSetOverview(s.logger, minT, maxT, maxResolutionMillis, blocks)
}

indexReaders := make(map[ulid.ULID]*bucketIndexReader, len(blocks))
for _, b := range blocks {
indexReaders[b.meta.ULID] = b.indexReader()
}
if skipChunks {
return blocks, indexReaders, nil
}

chunkReaders := make(map[ulid.ULID]*bucketChunkReader, len(blocks))
for _, b := range blocks {
chunkReaders[b.meta.ULID] = b.chunkReader(ctx, chunkPool)
}

return blocks, indexReaders, chunkReaders
}

// LabelNames implements the storepb.StoreServer interface.
func (s *BucketStore) LabelNames(ctx context.Context, req *storepb.LabelNamesRequest) (*storepb.LabelNamesResponse, error) {
reqSeriesMatchers, err := storepb.MatchersToPromMatchers(req.Matchers...)
Expand Down