Skip to content

Commit

Permalink
Expose store gateway query stats in series response hints (thanos-io#…
Browse files Browse the repository at this point in the history
…6352)

* expose query stats hints

Signed-off-by: Ben Ye <[email protected]>

* update

Signed-off-by: Ben Ye <[email protected]>

* add query stats hints in result

Signed-off-by: Ben Ye <[email protected]>

* update changelog

Signed-off-by: Ben Ye <[email protected]>

* add merge method

Signed-off-by: Ben Ye <[email protected]>

* fix unit test

Signed-off-by: Ben Ye <[email protected]>

modify hints proto

Signed-off-by: Ben Ye <[email protected]>

fix unit test

Signed-off-by: Ben Ye <[email protected]>

update format

Signed-off-by: Ben Ye <[email protected]>

* update comments

Signed-off-by: Ben Ye <[email protected]>

* try again

Signed-off-by: Ben Ye <[email protected]>

---------

Signed-off-by: Ben Ye <[email protected]>
  • Loading branch information
yeya24 authored and HC Zhu committed Jun 27, 2023
1 parent 2163faf commit 77a1535
Show file tree
Hide file tree
Showing 7 changed files with 936 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#6273](https://github.com/thanos-io/thanos/pull/6273) Mixin: Allow specifying an instance name filter in dashboards
- [#6163](https://github.com/thanos-io/thanos/pull/6163) Receiver: Add hidden flag `--receive-forward-max-backoff` to configure the max backoff for forwarding requests.
- [#5777](https://github.com/thanos-io/thanos/pull/5777) Receive: Allow specifying tenant-specific external labels in Router Ingestor.
- [#6352](https://github.com/thanos-io/thanos/pull/6352) Store: Expose store gateway query stats in series response hints.

### Fixed

Expand Down
30 changes: 30 additions & 0 deletions pkg/store/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,13 +1227,16 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
reqBlockMatchers []*labels.Matcher
chunksLimiter = s.chunksLimiterFactory(s.metrics.queriesDropped.WithLabelValues("chunks"))
seriesLimiter = s.seriesLimiterFactory(s.metrics.queriesDropped.WithLabelValues("series"))

queryStatsEnabled = false
)

if req.Hints != nil {
reqHints := &hintspb.SeriesRequestHints{}
if err := types.UnmarshalAny(req.Hints, reqHints); err != nil {
return status.Error(codes.InvalidArgument, errors.Wrap(err, "unmarshal series request hints").Error())
}
queryStatsEnabled = reqHints.EnableQueryStats

reqBlockMatchers, err = storepb.MatchersToPromMatchers(reqHints.BlockMatchers...)
if err != nil {
Expand Down Expand Up @@ -1424,6 +1427,9 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
if s.enableSeriesResponseHints {
var anyHints *types.Any

if queryStatsEnabled {
resHints.QueryStats = stats.toHints()
}
if anyHints, err = types.MarshalAny(resHints); err != nil {
err = status.Error(codes.Unknown, errors.Wrap(err, "marshal series response hints").Error())
return
Expand Down Expand Up @@ -3184,6 +3190,30 @@ func (s queryStats) merge(o *queryStats) *queryStats {
return &s
}

func (s queryStats) toHints() *hintspb.QueryStats {
return &hintspb.QueryStats{
BlocksQueried: int64(s.blocksQueried),
PostingsTouched: int64(s.postingsTouched),
PostingsTouchedSizeSum: int64(s.PostingsTouchedSizeSum),
PostingsToFetch: int64(s.postingsToFetch),
PostingsFetched: int64(s.postingsFetched),
PostingsFetchedSizeSum: int64(s.PostingsFetchedSizeSum),
PostingsFetchCount: int64(s.postingsFetchCount),
SeriesTouched: int64(s.seriesTouched),
SeriesTouchedSizeSum: int64(s.SeriesTouchedSizeSum),
SeriesFetched: int64(s.seriesFetched),
SeriesFetchedSizeSum: int64(s.SeriesFetchedSizeSum),
SeriesFetchCount: int64(s.seriesFetchCount),
ChunksTouched: int64(s.chunksTouched),
ChunksTouchedSizeSum: int64(s.ChunksTouchedSizeSum),
ChunksFetched: int64(s.chunksFetched),
ChunksFetchedSizeSum: int64(s.ChunksFetchedSizeSum),
ChunksFetchCount: int64(s.chunksFetchCount),
MergedSeriesCount: int64(s.mergedSeriesCount),
MergedChunksCount: int64(s.mergedChunksCount),
}
}

// NewDefaultChunkBytesPool returns a chunk bytes pool with default settings.
func NewDefaultChunkBytesPool(maxChunkPoolBytes uint64) (pool.Bytes, error) {
return pool.NewBucketedBytes(chunkBytesPoolMinSize, chunkBytesPoolMaxSize, 2, maxChunkPoolBytes)
Expand Down
53 changes: 51 additions & 2 deletions pkg/store/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,8 @@ func TestSeries_RequestAndResponseHints(t *testing.T) {
},
},
},
}, {
},
{
Name: "querying a range containing multiple blocks should return multiple blocks in the response hints",
Req: &storepb.SeriesRequest{
MinTime: 0,
Expand All @@ -1690,7 +1691,8 @@ func TestSeries_RequestAndResponseHints(t *testing.T) {
},
},
},
}, {
},
{
Name: "querying a range containing multiple blocks but filtering a specific block should query only the requested block",
Req: &storepb.SeriesRequest{
MinTime: 0,
Expand All @@ -1713,6 +1715,53 @@ func TestSeries_RequestAndResponseHints(t *testing.T) {
},
},
},
{
Name: "Query Stats Enabled",
Req: &storepb.SeriesRequest{
MinTime: 0,
MaxTime: 3,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: "foo", Value: "bar"},
},
Hints: mustMarshalAny(&hintspb.SeriesRequestHints{
BlockMatchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_EQ, Name: block.BlockIDLabel, Value: block1.String()},
},
EnableQueryStats: true,
}),
},
ExpectedSeries: seriesSet1,
ExpectedHints: []hintspb.SeriesResponseHints{
{
QueriedBlocks: []hintspb.Block{
{Id: block1.String()},
},
QueryStats: &hintspb.QueryStats{
BlocksQueried: 1,
PostingsTouched: 1,
PostingsFetched: 1,
SeriesTouched: 2,
SeriesFetched: 2,
ChunksTouched: 2,
ChunksFetched: 2,
MergedSeriesCount: 2,
MergedChunksCount: 2,
},
},
},
HintsCompareFunc: func(t testutil.TB, expected, actual hintspb.SeriesResponseHints) {
testutil.Equals(t, expected.QueriedBlocks, actual.QueriedBlocks)
testutil.Equals(t, expected.QueryStats.BlocksQueried, actual.QueryStats.BlocksQueried)
testutil.Equals(t, expected.QueryStats.PostingsTouched, actual.QueryStats.PostingsTouched)
testutil.Equals(t, expected.QueryStats.PostingsFetched, actual.QueryStats.PostingsFetched)
testutil.Equals(t, expected.QueryStats.SeriesTouched, actual.QueryStats.SeriesTouched)
testutil.Equals(t, expected.QueryStats.SeriesFetched, actual.QueryStats.SeriesFetched)
testutil.Equals(t, expected.QueryStats.ChunksTouched, actual.QueryStats.ChunksTouched)
testutil.Equals(t, expected.QueryStats.ChunksFetched, actual.QueryStats.ChunksFetched)
testutil.Equals(t, expected.QueryStats.MergedSeriesCount, actual.QueryStats.MergedSeriesCount)
testutil.Equals(t, expected.QueryStats.MergedChunksCount, actual.QueryStats.MergedChunksCount)
},
},
}

storetestutil.TestServerSeries(tb, store, testCases...)
Expand Down
25 changes: 25 additions & 0 deletions pkg/store/hintspb/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ func (m *LabelValuesResponseHints) AddQueriedBlock(id ulid.ULID) {
Id: id.String(),
})
}

func (m *QueryStats) Merge(other *QueryStats) {
m.BlocksQueried += other.BlocksQueried
m.MergedSeriesCount += other.MergedSeriesCount
m.MergedChunksCount += other.MergedChunksCount

m.PostingsFetched += other.PostingsFetched
m.PostingsToFetch += other.PostingsToFetch
m.PostingsFetchCount += other.PostingsFetchCount
m.PostingsFetchedSizeSum += other.PostingsFetchedSizeSum
m.PostingsTouched += other.PostingsTouched
m.PostingsTouchedSizeSum += other.PostingsTouchedSizeSum

m.SeriesFetched += other.SeriesFetched
m.SeriesFetchCount += other.SeriesFetchCount
m.SeriesFetchedSizeSum += m.SeriesFetchedSizeSum
m.SeriesTouched += other.SeriesTouched
m.SeriesTouchedSizeSum += other.SeriesTouchedSizeSum

m.ChunksFetched += other.ChunksFetched
m.ChunksFetchCount += other.ChunksFetchCount
m.ChunksFetchedSizeSum += other.ChunksFetchedSizeSum
m.ChunksTouched += other.ChunksTouched
m.ChunksTouchedSizeSum += other.ChunksTouchedSizeSum
}
Loading

0 comments on commit 77a1535

Please sign in to comment.