fix: avoid copying label values from tsdb unless required (#17077)

pull/17085/head
Sandeep Sukhani 1 month ago committed by GitHub
parent 5625464332
commit 8e4b10408d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      pkg/storage/stores/shipper/indexshipper/tsdb/head_read.go
  2. 12
      pkg/storage/stores/shipper/indexshipper/tsdb/index/index.go
  3. 3
      pkg/storage/stores/shipper/indexshipper/tsdb/index/index_test.go
  4. 3
      pkg/storage/stores/shipper/indexshipper/tsdb/querier.go
  5. 22
      pkg/storage/stores/shipper/indexshipper/tsdb/single_file_index.go

@ -55,18 +55,6 @@ func (h *headIndexReader) Symbols() index.StringIter {
return h.head.postings.Symbols()
}
// SortedLabelValues returns label values present in the head for the
// specific label name that are within the time range mint to maxt.
// If matchers are specified the returned result set is reduced
// to label values of metrics matching the matchers.
func (h *headIndexReader) SortedLabelValues(name string, matchers ...*labels.Matcher) ([]string, error) {
values, err := h.LabelValues(name, matchers...)
if err == nil {
sort.Strings(values)
}
return values, err
}
// LabelValues returns label values present in the head for the
// specific label name that are within the time range mint to maxt.
// If matchers are specified the returned result set is reduced

@ -1617,16 +1617,8 @@ func (r *Reader) SymbolTableSize() uint64 {
return uint64(r.symbols.Size())
}
// SortedLabelValues returns value tuples that exist for the given label name.
func (r *Reader) SortedLabelValues(name string, matchers ...*labels.Matcher) ([]string, error) {
values, err := r.LabelValues(name, matchers...)
if err == nil && r.version == FormatV1 {
sort.Strings(values)
}
return values, err
}
// LabelValues returns value tuples that exist for the given label name.
// The returned values should be copied if they need to be used beyond the current tsdb read operation, including sending back as response.
// TODO(replay): Support filtering by matchers
func (r *Reader) LabelValues(name string, matchers ...*labels.Matcher) ([]string, error) {
if len(matchers) > 0 {
@ -1670,7 +1662,7 @@ func (r *Reader) LabelValues(name string, matchers ...*labels.Matcher) ([]string
} else {
d.Skip(skip)
}
s := string(d.UvarintBytes()) // Label value.
s := yoloString(d.UvarintBytes()) // Label value.
values = append(values, s)
if s == lastVal {
break

@ -449,9 +449,10 @@ func TestPersistence_index_e2e(t *testing.T) {
for k, v := range labelPairs {
sort.Strings(v)
res, err := ir.SortedLabelValues(k)
res, err := ir.LabelValues(k)
require.NoError(t, err)
sort.Strings(res)
require.Equal(t, len(v), len(res))
for i := 0; i < len(v); i++ {
require.Equal(t, v[i], res[i])

@ -51,9 +51,6 @@ type IndexReader interface {
// beyond the lifetime of the index reader.
Symbols() index.StringIter
// SortedLabelValues returns sorted possible label values.
SortedLabelValues(name string, matchers ...*labels.Matcher) ([]string, error)
// LabelValues returns possible label values which may not be sorted.
LabelValues(name string, matchers ...*labels.Matcher) ([]string, error)

@ -7,6 +7,7 @@ import (
"io"
"math"
"path/filepath"
"strings"
"time"
"github.com/opentracing/opentracing-go"
@ -268,12 +269,19 @@ func (i *TSDBIndex) LabelNames(_ context.Context, _ string, _, _ model.Time, mat
}
func (i *TSDBIndex) LabelValues(_ context.Context, _ string, _, _ model.Time, name string, matchers ...*labels.Matcher) ([]string, error) {
if len(matchers) == 0 {
return i.reader.LabelValues(name)
}
if len(matchers) != 0 {
return labelValuesWithMatchers(i.reader, name, matchers...)
}
labelValues, err := i.reader.LabelValues(name)
if err != nil {
return nil, err
}
// cloning the string
return cloneStringList(labelValues), nil
}
func (i *TSDBIndex) Checksum() uint32 {
return i.reader.Checksum()
}
@ -454,3 +462,11 @@ func (i *TSDBIndex) Volume(
return p.Err()
})
}
func cloneStringList(strs []string) []string {
res := make([]string, 0, len(strs))
for _, str := range strs {
res = append(res, strings.Clone(str))
}
return res
}

Loading…
Cancel
Save