From 543c318ec2054e0be680ad28671b6d30efa314e2 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 9 Mar 2022 22:17:40 +0000 Subject: [PATCH] Update package tsdb for new labels.Labels type Signed-off-by: Bryan Boreham --- tsdb/db.go | 2 +- tsdb/exemplar.go | 5 ++++- tsdb/head_append.go | 8 ++++---- tsdb/head_read.go | 6 +++--- tsdb/ooo_head_read.go | 6 +++--- tsdb/querier.go | 5 +---- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tsdb/db.go b/tsdb/db.go index 7a165e431..616213b03 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -1002,7 +1002,7 @@ func (a dbAppender) GetRef(lset labels.Labels, hash uint64) (storage.SeriesRef, if g, ok := a.Appender.(storage.GetRef); ok { return g.GetRef(lset, hash) } - return 0, nil + return 0, labels.EmptyLabels() } func (a dbAppender) Commit() error { diff --git a/tsdb/exemplar.go b/tsdb/exemplar.go index 3718d9591..5ba3567e4 100644 --- a/tsdb/exemplar.go +++ b/tsdb/exemplar.go @@ -226,13 +226,16 @@ func (ce *CircularExemplarStorage) validateExemplar(key []byte, e exemplar.Exemp // Exemplar label length does not include chars involved in text rendering such as quotes // equals sign, or commas. See definition of const ExemplarMaxLabelLength. labelSetLen := 0 - for _, l := range e.Labels { + if err := e.Labels.Validate(func(l labels.Label) error { labelSetLen += utf8.RuneCountInString(l.Name) labelSetLen += utf8.RuneCountInString(l.Value) if labelSetLen > exemplar.ExemplarMaxLabelSetLength { return storage.ErrExemplarLabelLength } + return nil + }); err != nil { + return err } idx, ok := ce.index[string(key)] diff --git a/tsdb/head_append.go b/tsdb/head_append.go index d5003188d..822863276 100644 --- a/tsdb/head_append.go +++ b/tsdb/head_append.go @@ -102,7 +102,7 @@ func (a *initAppender) GetRef(lset labels.Labels, hash uint64) (storage.SeriesRe if g, ok := a.app.(storage.GetRef); ok { return g.GetRef(lset, hash) } - return 0, nil + return 0, labels.EmptyLabels() } func (a *initAppender) Commit() error { @@ -312,7 +312,7 @@ func (a *headAppender) Append(ref storage.SeriesRef, lset labels.Labels, t int64 if s == nil { // Ensure no empty labels have gotten through. lset = lset.WithoutEmpty() - if len(lset) == 0 { + if lset.IsEmpty() { return 0, errors.Wrap(ErrInvalidSample, "empty labelset") } @@ -494,7 +494,7 @@ func (a *headAppender) AppendHistogram(ref storage.SeriesRef, lset labels.Labels if s == nil { // Ensure no empty labels have gotten through. lset = lset.WithoutEmpty() - if len(lset) == 0 { + if lset.IsEmpty() { return 0, errors.Wrap(ErrInvalidSample, "empty labelset") } @@ -650,7 +650,7 @@ var _ storage.GetRef = &headAppender{} func (a *headAppender) GetRef(lset labels.Labels, hash uint64) (storage.SeriesRef, labels.Labels) { s := a.head.series.getByHash(hash, lset) if s == nil { - return 0, nil + return 0, labels.EmptyLabels() } // returned labels must be suitable to pass to Append() return storage.SeriesRef(s.ref), s.lset diff --git a/tsdb/head_read.go b/tsdb/head_read.go index c6017e7b8..0a6aef2bd 100644 --- a/tsdb/head_read.go +++ b/tsdb/head_read.go @@ -155,7 +155,7 @@ func (h *headIndexReader) Series(ref storage.SeriesRef, builder *labels.ScratchB h.head.metrics.seriesNotFound.Inc() return storage.ErrNotFound } - *lbls = append((*lbls)[:0], s.lset...) + lbls.CopyFrom(s.lset) s.Lock() defer s.Unlock() @@ -222,9 +222,9 @@ func (h *headIndexReader) LabelNamesFor(ids ...storage.SeriesRef) ([]string, err if memSeries == nil { return nil, storage.ErrNotFound } - for _, lbl := range memSeries.lset { + memSeries.lset.Range(func(lbl labels.Label) { namesMap[lbl.Name] = struct{}{} - } + }) } names := make([]string, 0, len(namesMap)) for name := range namesMap { diff --git a/tsdb/ooo_head_read.go b/tsdb/ooo_head_read.go index f63607dc9..d0b09963d 100644 --- a/tsdb/ooo_head_read.go +++ b/tsdb/ooo_head_read.go @@ -47,7 +47,7 @@ func NewOOOHeadIndexReader(head *Head, mint, maxt int64) *OOOHeadIndexReader { return &OOOHeadIndexReader{hr} } -func (oh *OOOHeadIndexReader) Series(ref storage.SeriesRef, lbls *labels.Labels, chks *[]chunks.Meta) error { +func (oh *OOOHeadIndexReader) Series(ref storage.SeriesRef, builder *labels.ScratchBuilder, lbls *labels.Labels, chks *[]chunks.Meta) error { return oh.series(ref, lbls, chks, 0) } @@ -61,7 +61,7 @@ func (oh *OOOHeadIndexReader) series(ref storage.SeriesRef, lbls *labels.Labels, oh.head.metrics.seriesNotFound.Inc() return storage.ErrNotFound } - *lbls = append((*lbls)[:0], s.lset...) + lbls.CopyFrom(s.lset) if chks == nil { return nil @@ -400,7 +400,7 @@ func (ir *OOOCompactionHeadIndexReader) SortedPostings(p index.Postings) index.P return p } -func (ir *OOOCompactionHeadIndexReader) Series(ref storage.SeriesRef, lset *labels.Labels, chks *[]chunks.Meta) error { +func (ir *OOOCompactionHeadIndexReader) Series(ref storage.SeriesRef, builder *labels.ScratchBuilder, lset *labels.Labels, chks *[]chunks.Meta) error { return ir.ch.oooIR.series(ref, lset, chks, ir.ch.lastMmapRef) } diff --git a/tsdb/querier.go b/tsdb/querier.go index 4082279ae..34917dad0 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -529,8 +529,7 @@ func (b *blockBaseSeriesSet) Next() bool { intervals = intervals.Add(tombstones.Interval{Mint: b.maxt + 1, Maxt: math.MaxInt64}) } - b.curr.labels = make(labels.Labels, len(b.bufLbls)) - copy(b.curr.labels, b.bufLbls) + b.curr.labels = b.bufLbls.Copy() b.curr.chks = chks b.curr.intervals = intervals @@ -866,7 +865,6 @@ func newBlockSeriesSet(i IndexReader, c ChunkReader, t tombstones.Reader, p inde mint: mint, maxt: maxt, disableTrimming: disableTrimming, - bufLbls: make(labels.Labels, 0, 10), }, } } @@ -898,7 +896,6 @@ func newBlockChunkSeriesSet(id ulid.ULID, i IndexReader, c ChunkReader, t tombst mint: mint, maxt: maxt, disableTrimming: disableTrimming, - bufLbls: make(labels.Labels, 0, 10), }, } }