From e90e75300a54204f603fa403fdfd83caf413f607 Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Tue, 20 Apr 2021 16:34:50 +0200 Subject: [PATCH] Fixes. Signed-off-by: Bartlomiej Plotka --- pkg/receive/handler_test.go | 2 +- pkg/receive/hashring.go | 5 +++++ pkg/store/labelpb/label.go | 5 +++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/receive/handler_test.go b/pkg/receive/handler_test.go index 98d54409da..f7dcc7cae2 100644 --- a/pkg/receive/handler_test.go +++ b/pkg/receive/handler_test.go @@ -1293,7 +1293,7 @@ func Heap(dir string) (err error) { return err } - f, err := os.Create(filepath.Join(dir, "impr4-go1.16.3.pprof")) + f, err := os.Create(filepath.Join(dir, "impr5-go1.16.3.pprof")) if err != nil { return err } diff --git a/pkg/receive/hashring.go b/pkg/receive/hashring.go index 0de4dbefe1..b368bf30e7 100644 --- a/pkg/receive/hashring.go +++ b/pkg/receive/hashring.go @@ -6,6 +6,7 @@ package receive import ( "context" "fmt" + "sort" "sync" "github.com/pkg/errors" @@ -65,6 +66,10 @@ func (s simpleHashring) GetN(tenant string, ts *prompb.TimeSeries, n uint64) (st if n >= uint64(len(s)) { return "", &insufficientNodesError{have: uint64(len(s)), want: n + 1} } + + // TODO(bwplotka): This might be not needed, double check. + sort.Slice(ts.Labels, func(i, j int) bool { return ts.Labels[i].Name < ts.Labels[j].Name }) + return s[(labelpb.HashWithPrefix(tenant, ts.Labels)+n)%uint64(len(s))], nil } diff --git a/pkg/store/labelpb/label.go b/pkg/store/labelpb/label.go index 2aa778d71c..5d6e7378cb 100644 --- a/pkg/store/labelpb/label.go +++ b/pkg/store/labelpb/label.go @@ -19,6 +19,8 @@ import ( "github.com/prometheus/prometheus/pkg/labels" ) +var sep = []byte{'\xff'} + func noAllocString(buf []byte) string { return *(*string)(unsafe.Pointer(&buf)) } @@ -316,13 +318,12 @@ func DeepCopy(lbls []ZLabel) []ZLabel { return ret } -var sep = []byte{'\xff'} - // HashWithPrefix returns a hash for the given prefix and labels. func HashWithPrefix(prefix string, lbls []ZLabel) uint64 { // Use xxhash.Sum64(b) for fast path as it's faster. b := make([]byte, 0, 1024) b = append(b, prefix...) + b = append(b, sep[0]) for i, v := range lbls { if len(b)+len(v.Name)+len(v.Value)+2 >= cap(b) {