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: add initial symbol tables support #5906

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
66 changes: 66 additions & 0 deletions pkg/query/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ package query

import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"

"github.com/go-kit/log"
"github.com/gogo/protobuf/types"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -24,6 +26,7 @@ import (
"github.com/thanos-io/thanos/pkg/extprom"
"github.com/thanos-io/thanos/pkg/gate"
"github.com/thanos-io/thanos/pkg/store"
"github.com/thanos-io/thanos/pkg/store/hintspb"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
"github.com/thanos-io/thanos/pkg/tracing"
Expand Down Expand Up @@ -208,6 +211,47 @@ type seriesServer struct {
seriesSet []storepb.Series
seriesSetStats storepb.SeriesStatsCounter
warnings []string
symbolTables []map[uint64]string

compressedSeriesSet []storepb.CompressedSeries
}

func (s *seriesServer) DecompressSeries() error {
for _, cs := range s.compressedSeriesSet {
newSeries := &storepb.Series{
Chunks: cs.Chunks,
}

lbls := labels.Labels{}

for _, cLabel := range cs.Labels {
var name, val string
for _, symTable := range s.symbolTables {
if foundName, ok := symTable[uint64(cLabel.NameRef)]; ok {
name = foundName
}

if foundValue, ok := symTable[uint64(cLabel.ValueRef)]; ok {
val = foundValue
}
GiedriusS marked this conversation as resolved.
Show resolved Hide resolved
}
if name == "" {
return fmt.Errorf("found no reference for name ref %d", cLabel.NameRef)
}
if val == "" {
return fmt.Errorf("found no reference for value ref %d", cLabel.ValueRef)
}

lbls = append(lbls, labels.Label{
Name: name,
Value: val,
})
}

newSeries.Labels = labelpb.ZLabelsFromPromLabels(lbls)
s.seriesSet = append(s.seriesSet, *newSeries)
}
return nil
}

func (s *seriesServer) Send(r *storepb.SeriesResponse) error {
Expand All @@ -222,6 +266,24 @@ func (s *seriesServer) Send(r *storepb.SeriesResponse) error {
return nil
}

if r.GetCompressedSeries() != nil {
s.compressedSeriesSet = append(s.compressedSeriesSet, *r.GetCompressedSeries())
s.seriesSetStats.Count(r.GetCompressedSeries())
return nil
}

if r.GetHints() != nil {
var seriesResponseHints hintspb.SeriesResponseHints

// Some other, unknown type. Skip it.
if err := types.UnmarshalAny(r.GetHints(), &seriesResponseHints); err != nil {
return nil
}

s.symbolTables = append(s.symbolTables, seriesResponseHints.StringSymbolTable)
return nil
}

// Unsupported field, skip.
return nil
}
Expand Down Expand Up @@ -369,6 +431,10 @@ func (q *querier) selectFn(ctx context.Context, hints *storage.SelectHints, ms .
warns = append(warns, errors.New(w))
}

if err := resp.DecompressSeries(); err != nil {
return nil, storepb.SeriesStatsCounter{}, errors.Wrap(err, "decompressing series")
}

// Delete the metric's name from the result because that's what the
// PromQL does either way and we want our iterator to work with data
// that was either pushed down or not.
Expand Down
43 changes: 39 additions & 4 deletions pkg/store/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ type BucketStore struct {
postingOffsetsInMemSampling int

// Enables hints in the Series() response.
enableSeriesResponseHints bool
enableQueriedBlocksHints bool

enableChunkHashCalculation bool
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func NewBucketStore(
partitioner: partitioner,
enableCompatibilityLabel: enableCompatibilityLabel,
postingOffsetsInMemSampling: postingOffsetsInMemSampling,
enableSeriesResponseHints: enableSeriesResponseHints,
enableQueriedBlocksHints: enableSeriesResponseHints,
enableChunkHashCalculation: enableChunkHashCalculation,
seriesBatchSize: SeriesBatchSize,
}
Expand Down Expand Up @@ -1186,6 +1186,7 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
}

s.mtx.RLock()

for _, bs := range s.blockSets {
blockMatchers, ok := bs.labelMatchers(matchers...)
if !ok {
Expand All @@ -1202,7 +1203,7 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
blk := b
gctx := gctx

if s.enableSeriesResponseHints {
if s.enableQueriedBlocksHints {
// Keep track of queried blocks.
resHints.AddQueriedBlock(blk.meta.ULID)
}
Expand Down Expand Up @@ -1250,6 +1251,8 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
shardMatcher,
false,
s.metrics.emptyPostingCount,
req.MaximumStringSlots,
nil,
)

mtx.Lock()
Expand Down Expand Up @@ -1311,6 +1314,8 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
s.metrics.seriesBlocksQueried.Observe(float64(stats.blocksQueried))
}

symbolTableBuilder := newSymbolTableBuilder(req.MaximumStringSlots)

// Merge the sub-results from each selected block.
tracing.DoInSpan(ctx, "bucket_store_merge_all", func(ctx context.Context) {
defer func() {
Expand Down Expand Up @@ -1338,7 +1343,35 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
stats.mergedChunksCount += len(series.Chunks)
s.metrics.chunkSizeBytes.Observe(float64(chunksSize(series.Chunks)))
}

lset := series.Labels

var compressedResponse bool = true
compressedLabels := make([]labelpb.CompressedLabel, 0, len(lset))

for _, lbl := range lset {
nameRef, nok := symbolTableBuilder.getOrStoreString(lbl.Name)
valueRef, vok := symbolTableBuilder.getOrStoreString(lbl.Value)

if !nok || !vok {
compressedResponse = false
break
} else if compressedResponse && nok && vok {
compressedLabels = append(compressedLabels, labelpb.CompressedLabel{
NameRef: nameRef,
ValueRef: valueRef,
})
}
}

if compressedResponse {
at = storepb.NewCompressedSeriesResponse(&storepb.CompressedSeries{
Labels: compressedLabels,
Chunks: series.Chunks,
})
}
}

if err = srv.Send(at); err != nil {
err = status.Error(codes.Unknown, errors.Wrap(err, "send series response").Error())
return
Expand All @@ -1353,7 +1386,9 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
return err
}

if s.enableSeriesResponseHints {
resHints.StringSymbolTable = symbolTableBuilder.getTable()

{
var anyHints *types.Any

if anyHints, err = types.MarshalAny(resHints); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/store/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,9 @@ func benchBucketSeries(t testutil.TB, skipChunk bool, samplesPerSeries, totalSer
},
// This does not cut chunks properly, but those are assured against for non benchmarks only, where we use 100% case only.
ExpectedSeries: series[:seriesCut],
ExpectedHints: []hintspb.SeriesResponseHints{
{},
},
})
}
storetestutil.TestServerSeries(t, st, bCases...)
Expand Down
Loading