Skip to content

Commit

Permalink
Merge branch 'upstream/main' into dimitar/update-upstream-2023-09-22
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitarvdimitrov committed Sep 22, 2023
2 parents 320f0c9 + 86729d4 commit 18cb757
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 71 deletions.
2 changes: 1 addition & 1 deletion cmd/promtool/tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func analyzeBlock(ctx context.Context, path, blockID string, limit int, runExten
postingInfos := []postingInfo{}

printInfo := func(postingInfos []postingInfo) {
slices.SortFunc(postingInfos, func(a, b postingInfo) bool { return a.metric > b.metric })
slices.SortFunc(postingInfos, func(a, b postingInfo) int { return int(b.metric) - int(a.metric) })

for i, pc := range postingInfos {
if i >= limit {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ require (
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
golang.org/x/mod v0.12.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
9 changes: 5 additions & 4 deletions model/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"encoding/json"
"strconv"
"strings"

"github.com/cespare/xxhash/v2"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -362,7 +363,7 @@ func EmptyLabels() Labels {
func New(ls ...Label) Labels {
set := make(Labels, 0, len(ls))
set = append(set, ls...)
slices.SortFunc(set, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(set, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })

return set
}
Expand All @@ -386,7 +387,7 @@ func FromStrings(ss ...string) Labels {
res = append(res, Label{Name: ss[i], Value: ss[i+1]})
}

slices.SortFunc(res, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(res, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
return res
}

Expand Down Expand Up @@ -591,7 +592,7 @@ func (b *Builder) Labels() Labels {
}
if len(b.add) > 0 { // Base is already in order, so we only need to sort if we add to it.
res = append(res, b.add...)
slices.SortFunc(res, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(res, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
}
return res
}
Expand All @@ -618,7 +619,7 @@ func (b *ScratchBuilder) Add(name, value string) {

// Sort the labels added so far by name.
func (b *ScratchBuilder) Sort() {
slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(b.add, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
}

// Assign is for when you already have a Labels which you want this ScratchBuilder to return.
Expand Down
7 changes: 4 additions & 3 deletions model/labels/labels_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"reflect"
"strconv"
"strings"
"unsafe"

"github.com/cespare/xxhash/v2"
Expand Down Expand Up @@ -412,7 +413,7 @@ func yoloBytes(s string) (b []byte) {
// New returns a sorted Labels from the given labels.
// The caller has to guarantee that all label names are unique.
func New(ls ...Label) Labels {
slices.SortFunc(ls, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(ls, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
size := labelsSize(ls)
buf := make([]byte, size)
marshalLabelsToSizedBuffer(ls, buf)
Expand Down Expand Up @@ -671,7 +672,7 @@ func (b *Builder) Labels() Labels {
return b.base
}

slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(b.add, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
slices.Sort(b.del)
a, d := 0, 0

Expand Down Expand Up @@ -830,7 +831,7 @@ func (b *ScratchBuilder) Add(name, value string) {

// Sort the labels added so far by name.
func (b *ScratchBuilder) Sort() {
slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
slices.SortFunc(b.add, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
}

// Assign is for when you already have a Labels which you want this ScratchBuilder to return.
Expand Down
11 changes: 9 additions & 2 deletions promql/quantile.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@ func bucketQuantile(q float64, buckets buckets) float64 {
if q > 1 {
return math.Inf(+1)
}
slices.SortFunc(buckets, func(a, b bucket) bool {
return a.upperBound < b.upperBound
slices.SortFunc(buckets, func(a, b bucket) int {
// We don't expect the bucket boundary to be a NaN.
if a.upperBound < b.upperBound {
return -1
}
if a.upperBound > b.upperBound {
return +1
}
return 0
})
if !math.IsInf(buckets[len(buckets)-1].upperBound, +1) {
return math.NaN()
Expand Down
21 changes: 14 additions & 7 deletions rules/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math"
"net/url"
"sort"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -506,9 +507,11 @@ func (g *Group) AlertingRules() []*AlertingRule {
alerts = append(alerts, alertingRule)
}
}
slices.SortFunc(alerts, func(a, b *AlertingRule) bool {
return a.State() > b.State() ||
(a.State() == b.State() && a.Name() < b.Name())
slices.SortFunc(alerts, func(a, b *AlertingRule) int {
if a.State() == b.State() {
return strings.Compare(a.Name(), b.Name())
}
return int(b.State() - a.State())
})
return alerts
}
Expand Down Expand Up @@ -1277,11 +1280,15 @@ func (m *Manager) RuleGroups() []*Group {
rgs = append(rgs, g)
}

slices.SortFunc(rgs, func(a, b *Group) bool {
if a.file != b.file {
return a.file < b.file
slices.SortFunc(rgs, func(a, b *Group) int {
fileCompare := strings.Compare(a.file, b.file)

// If its 0, then the file names are the same.
// Lets look at the group names in that case.
if fileCompare != 0 {
return fileCompare
}
return a.name < b.name
return strings.Compare(a.name, b.name)
})

return rgs
Expand Down
4 changes: 2 additions & 2 deletions scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*re
}

func resolveConflictingExposedLabels(lb *labels.Builder, conflictingExposedLabels []labels.Label) {
slices.SortStableFunc(conflictingExposedLabels, func(a, b labels.Label) bool {
return len(a.Name) < len(b.Name)
slices.SortStableFunc(conflictingExposedLabels, func(a, b labels.Label) int {
return len(a.Name) - len(b.Name)
})

for _, l := range conflictingExposedLabels {
Expand Down
8 changes: 2 additions & 6 deletions storage/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,8 @@ func (b *BufferedSeriesIterator) Seek(t int64) chunkenc.ValueType {
switch b.valueType {
case chunkenc.ValNone:
return chunkenc.ValNone
case chunkenc.ValFloat:
b.lastTime, _ = b.At()
case chunkenc.ValHistogram:
b.lastTime, _ = b.AtHistogram()
case chunkenc.ValFloatHistogram:
b.lastTime, _ = b.AtFloatHistogram()
case chunkenc.ValFloat, chunkenc.ValHistogram, chunkenc.ValFloatHistogram:
b.lastTime = b.AtT()
default:
panic(fmt.Errorf("BufferedSeriesIterator: unknown value type %v", b.valueType))
}
Expand Down
4 changes: 2 additions & 2 deletions storage/remote/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func FromQueryResult(sortSeries bool, res *prompb.QueryResult) storage.SeriesSet
}

if sortSeries {
slices.SortFunc(series, func(a, b storage.Series) bool {
return labels.Compare(a.Labels(), b.Labels()) < 0
slices.SortFunc(series, func(a, b storage.Series) int {
return labels.Compare(a.Labels(), b.Labels())
})
}
return &concreteSeriesSet{
Expand Down
5 changes: 3 additions & 2 deletions storage/remote/read_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package remote
import (
"context"
"net/http"
"strings"
"sync"

"github.com/go-kit/log"
Expand Down Expand Up @@ -93,8 +94,8 @@ func (h *readHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Value: value,
})
}
slices.SortFunc(sortedExternalLabels, func(a, b prompb.Label) bool {
return a.Name < b.Name
slices.SortFunc(sortedExternalLabels, func(a, b prompb.Label) int {
return strings.Compare(a.Name, b.Name)
})

responseType, err := NegotiateResponseType(req.AcceptedResponseTypes)
Expand Down
8 changes: 4 additions & 4 deletions tsdb/compact.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func (c *LeveledCompactor) Plan(dir string) ([]string, error) {
}

func (c *LeveledCompactor) plan(dms []dirMeta) ([]string, error) {
slices.SortFunc(dms, func(a, b dirMeta) bool {
return a.meta.MinTime < b.meta.MinTime
slices.SortFunc(dms, func(a, b dirMeta) int {
return int(a.meta.MinTime - b.meta.MinTime)
})

res := c.selectOverlappingDirs(dms)
Expand Down Expand Up @@ -415,8 +415,8 @@ func CompactBlockMetas(uid ulid.ULID, blocks ...*BlockMeta) *BlockMeta {
for s := range sources {
res.Compaction.Sources = append(res.Compaction.Sources, s)
}
slices.SortFunc(res.Compaction.Sources, func(a, b ulid.ULID) bool {
return a.Compare(b) < 0
slices.SortFunc(res.Compaction.Sources, func(a, b ulid.ULID) int {
return a.Compare(b)
})

res.MinTime = mint
Expand Down
12 changes: 6 additions & 6 deletions tsdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ func (db *DBReadOnly) Blocks() ([]BlockReader, error) {
return nil, nil
}

slices.SortFunc(loadable, func(a, b *Block) bool {
return a.Meta().MinTime < b.Meta().MinTime
slices.SortFunc(loadable, func(a, b *Block) int {
return int(a.Meta().MinTime - b.Meta().MinTime)
})

blockMetas := make([]BlockMeta, 0, len(loadable))
Expand Down Expand Up @@ -1495,8 +1495,8 @@ func (db *DB) reloadBlocks() (err error) {
}
db.metrics.blocksBytes.Set(float64(blocksSize))

slices.SortFunc(toLoad, func(a, b *Block) bool {
return a.Meta().MinTime < b.Meta().MinTime
slices.SortFunc(toLoad, func(a, b *Block) int {
return int(a.Meta().MinTime - b.Meta().MinTime)
})

// Swap new blocks first for subsequently created readers to be seen.
Expand Down Expand Up @@ -1570,8 +1570,8 @@ func deletableBlocks(db *DB, blocks []*Block) map[ulid.ULID]struct{} {

// Sort the blocks by time - newest to oldest (largest to smallest timestamp).
// This ensures that the retentions will remove the oldest blocks.
slices.SortFunc(blocks, func(a, b *Block) bool {
return a.Meta().MaxTime > b.Meta().MaxTime
slices.SortFunc(blocks, func(a, b *Block) int {
return int(b.Meta().MaxTime - a.Meta().MaxTime)
})

for _, block := range blocks {
Expand Down
4 changes: 2 additions & 2 deletions tsdb/exemplar.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ func (ce *CircularExemplarStorage) Select(start, end int64, matchers ...[]*label
}
}

slices.SortFunc(ret, func(a, b exemplar.QueryResult) bool {
return labels.Compare(a.SeriesLabels, b.SeriesLabels) < 0
slices.SortFunc(ret, func(a, b exemplar.QueryResult) int {
return labels.Compare(a.SeriesLabels, b.SeriesLabels)
})

return ret, nil
Expand Down
4 changes: 2 additions & 2 deletions tsdb/head_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (h *headIndexReader) SortedPostings(p index.Postings) index.Postings {
return index.ErrPostings(errors.Wrap(err, "expand postings"))
}

slices.SortFunc(series, func(a, b *memSeries) bool {
return labels.Compare(a.lset, b.lset) < 0
slices.SortFunc(series, func(a, b *memSeries) int {
return labels.Compare(a.lset, b.lset)
})

// Convert back to list.
Expand Down
12 changes: 8 additions & 4 deletions tsdb/index/postings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/binary"
"runtime"
"sort"
"strings"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -108,11 +109,14 @@ func (p *MemPostings) SortedKeys() []labels.Label {
}
p.mtx.RUnlock()

slices.SortFunc(keys, func(a, b labels.Label) bool {
if a.Name != b.Name {
return a.Name < b.Name
slices.SortFunc(keys, func(a, b labels.Label) int {
nameCompare := strings.Compare(a.Name, b.Name)
// If names are the same, compare values.
if nameCompare != 0 {
return nameCompare
}
return a.Value < b.Value

return strings.Compare(a.Value, b.Value)
})
return keys
}
Expand Down
4 changes: 1 addition & 3 deletions tsdb/index/postingsstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func (m *maxHeap) push(item Stat) {
}

func (m *maxHeap) get() []Stat {
slices.SortFunc(m.Items, func(a, b Stat) bool {
return a.Count > b.Count
})
slices.SortFunc(m.Items, func(a, b Stat) int { return int(b.Count - a.Count) })
return m.Items
}
12 changes: 6 additions & 6 deletions tsdb/ooo_head_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,18 @@ type chunkMetaAndChunkDiskMapperRef struct {
origMaxT int64
}

func refLessByMinTimeAndMinRef(a, b chunkMetaAndChunkDiskMapperRef) bool {
func refLessByMinTimeAndMinRef(a, b chunkMetaAndChunkDiskMapperRef) int {
if a.meta.MinTime == b.meta.MinTime {
return a.meta.Ref < b.meta.Ref
return int(a.meta.Ref - b.meta.Ref)
}
return a.meta.MinTime < b.meta.MinTime
return int(a.meta.MinTime - b.meta.MinTime)
}

func lessByMinTimeAndMinRef(a, b chunks.Meta) bool {
func lessByMinTimeAndMinRef(a, b chunks.Meta) int {
if a.MinTime == b.MinTime {
return a.Ref < b.Ref
return int(a.Ref - b.Ref)
}
return a.MinTime < b.MinTime
return int(a.MinTime - b.MinTime)
}

func (oh *OOOHeadIndexReader) Postings(ctx context.Context, name string, values ...string) (index.Postings, error) {
Expand Down
10 changes: 7 additions & 3 deletions tsdb/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,12 @@ func PostingsForMatchers(ctx context.Context, ix IndexPostingsReader, ms ...*lab
// there is no chance that the set we subtract from
// contains postings of series that didn't exist when
// we constructed the set we subtract by.
slices.SortStableFunc(ms, func(i, j *labels.Matcher) bool {
return !isSubtractingMatcher(i) && isSubtractingMatcher(j)
slices.SortStableFunc(ms, func(i, j *labels.Matcher) int {
if !isSubtractingMatcher(i) && isSubtractingMatcher(j) {
return -1
}

return +1
})

for _, m := range ms {
Expand Down Expand Up @@ -706,7 +710,7 @@ func (p *populateWithDelGenericSeriesIterator) reset(blockID ulid.ULID, cr Chunk
p.chks = chks
p.i = -1
p.err = nil
p.bufIter.Iter = nil
// Note we don't touch p.bufIter.Iter; it is holding on to an iterator we might reuse in next().
p.bufIter.Intervals = p.bufIter.Intervals[:0]
p.intervals = intervals
p.currDelIter = nil
Expand Down
4 changes: 2 additions & 2 deletions tsdb/wlog/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ func listCheckpoints(dir string) (refs []checkpointRef, err error) {
refs = append(refs, checkpointRef{name: fi.Name(), index: idx})
}

slices.SortFunc(refs, func(a, b checkpointRef) bool {
return a.index < b.index
slices.SortFunc(refs, func(a, b checkpointRef) int {
return a.index - b.index
})

return refs, nil
Expand Down
Loading

0 comments on commit 18cb757

Please sign in to comment.