diff --git a/go.mod b/go.mod index 52dcb7d22c1..150593d16ce 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/benbjohnson/clock v0.0.0-20161215174838-7dc76406b6d3 github.com/benbjohnson/tmpl v1.0.0 github.com/buger/jsonparser v1.1.1 - github.com/cespare/xxhash v1.1.0 + github.com/cespare/xxhash/v2 v2.3.0 github.com/davecgh/go-spew v1.1.1 github.com/dgryski/go-bitstream v0.0.0-20180413035011-3522498ce2c8 github.com/dustin/go-humanize v1.0.1 @@ -130,7 +130,6 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/bonitoo-io/go-sql-bigquery v0.3.4-1.4.0 // indirect github.com/cenkalti/backoff/v4 v4.2.0 // indirect - github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/containerd/containerd v1.6.18 // indirect github.com/danieljoos/wincred v1.2.1 // indirect github.com/deepmap/oapi-codegen v1.6.0 // indirect diff --git a/go.sum b/go.sum index ecbaf28a135..dc28239903c 100644 --- a/go.sum +++ b/go.sum @@ -124,7 +124,6 @@ github.com/NYTimes/gziphandler v1.0.1 h1:iLrQrdwjDd52kHDA5op2UBJFjmOb9g+7scBan4R github.com/NYTimes/gziphandler v1.0.1/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= -github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/RoaringBitmap/roaring v0.4.16 h1:NholfewybRLOwACgfqfzn/N5xa6keKNs4fP00t0cwLo= github.com/RoaringBitmap/roaring v0.4.16/go.mod h1:8khRDP4HmeXns4xIj9oGrKSz7XTQiJx2zgh7AcNke4w= @@ -225,7 +224,6 @@ github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QH github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4= github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= @@ -895,7 +893,6 @@ github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIK github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= -github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= diff --git a/pkg/bloom/bloom.go b/pkg/bloom/bloom.go index b77ca71b946..0d81c3b7f14 100644 --- a/pkg/bloom/bloom.go +++ b/pkg/bloom/bloom.go @@ -12,7 +12,7 @@ import ( "fmt" "math" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" ) // Filter represents a bloom filter. diff --git a/pkg/estimator/hll/hll.go b/pkg/estimator/hll/hll.go index df886dcb4b4..72e2632b658 100644 --- a/pkg/estimator/hll/hll.go +++ b/pkg/estimator/hll/hll.go @@ -22,7 +22,7 @@ import ( "sort" "unsafe" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" "github.com/influxdata/influxdb/v2/pkg/estimator" ) diff --git a/pkg/rhh/rhh.go b/pkg/rhh/rhh.go index 2b50414c317..24781de3bc5 100644 --- a/pkg/rhh/rhh.go +++ b/pkg/rhh/rhh.go @@ -7,7 +7,7 @@ import ( "sort" "time" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" "github.com/prometheus/client_golang/prometheus" ) diff --git a/task/backend/scheduler/treescheduler.go b/task/backend/scheduler/treescheduler.go index 69e98551f93..fcadeb07ef3 100644 --- a/task/backend/scheduler/treescheduler.go +++ b/task/backend/scheduler/treescheduler.go @@ -8,7 +8,7 @@ import ( "time" "github.com/benbjohnson/clock" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" "github.com/google/btree" ) diff --git a/tsdb/engine/tsm1/cache.go b/tsdb/engine/tsm1/cache.go index d6bb2b9d567..73771bb7fc1 100644 --- a/tsdb/engine/tsm1/cache.go +++ b/tsdb/engine/tsm1/cache.go @@ -147,7 +147,7 @@ func (e *entry) InfluxQLType() (influxql.DataType, error) { // storer is the interface that descibes a cache's store. type storer interface { entry(key []byte) *entry // Get an entry by its key. - write(key []byte, values Values) (bool, error) // Write an entry to the store. + write(key string, values Values) (bool, error) // Write an entry to the store. remove(key []byte) // Remove an entry from the store. keys(sorted bool) [][]byte // Return an optionally sorted slice of entry keys. apply(f func([]byte, *entry) error) error // Apply f to all entries in the store in parallel. @@ -340,7 +340,7 @@ func (c *Cache) WriteMulti(values map[string][]Value) error { // We'll optimistically set size here, and then decrement it for write errors. c.increaseSize(addedSize) for k, v := range values { - newKey, err := store.write([]byte(k), v) + newKey, err := store.write(k, v) if err != nil { // The write failed, hold onto the error and adjust the size delta. werr = err @@ -797,7 +797,7 @@ func valueType(v Value) byte { type emptyStore struct{} func (e emptyStore) entry(key []byte) *entry { return nil } -func (e emptyStore) write(key []byte, values Values) (bool, error) { return false, nil } +func (e emptyStore) write(key string, values Values) (bool, error) { return false, nil } func (e emptyStore) remove(key []byte) {} func (e emptyStore) keys(sorted bool) [][]byte { return nil } func (e emptyStore) apply(f func([]byte, *entry) error) error { return nil } diff --git a/tsdb/engine/tsm1/cache_test.go b/tsdb/engine/tsm1/cache_test.go index f5f0c6305c6..dc177059633 100644 --- a/tsdb/engine/tsm1/cache_test.go +++ b/tsdb/engine/tsm1/cache_test.go @@ -1,7 +1,6 @@ package tsm1 import ( - "bytes" "errors" "fmt" "math" @@ -80,8 +79,8 @@ func TestCache_WriteMulti_Stats(t *testing.T) { c.init() c.store = ms - ms.writef = func(key []byte, v Values) (bool, error) { - if bytes.Equal(key, []byte("foo")) { + ms.writef = func(key string, v Values) (bool, error) { + if key == "foo" { return false, errors.New("write failed") } return true, nil @@ -811,7 +810,7 @@ func mustMarshalEntry(entry WALEntry) (WalEntryType, []byte) { // Cache's storer implementation. type TestStore struct { entryf func(key []byte) *entry - writef func(key []byte, values Values) (bool, error) + writef func(key string, values Values) (bool, error) removef func(key []byte) keysf func(sorted bool) [][]byte applyf func(f func([]byte, *entry) error) error @@ -823,7 +822,7 @@ type TestStore struct { func NewTestStore() *TestStore { return &TestStore{} } func (s *TestStore) entry(key []byte) *entry { return s.entryf(key) } -func (s *TestStore) write(key []byte, values Values) (bool, error) { return s.writef(key, values) } +func (s *TestStore) write(key string, values Values) (bool, error) { return s.writef(key, values) } func (s *TestStore) remove(key []byte) { s.removef(key) } func (s *TestStore) keys(sorted bool) [][]byte { return s.keysf(sorted) } func (s *TestStore) apply(f func([]byte, *entry) error) error { return s.applyf(f) } diff --git a/tsdb/engine/tsm1/ring.go b/tsdb/engine/tsm1/ring.go index 615eb48f11e..f8b066b6593 100644 --- a/tsdb/engine/tsm1/ring.go +++ b/tsdb/engine/tsm1/ring.go @@ -4,7 +4,7 @@ import ( "fmt" "sync" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" "github.com/influxdata/influxdb/v2/pkg/bytesutil" ) @@ -80,6 +80,12 @@ func (r *ring) getPartition(key []byte) *partition { return r.partitions[int(xxhash.Sum64(key)%uint64(len(r.partitions)))] } +// getPartition retrieves the hash ring partition associated with the provided +// key, as a string, which can be faster if you already have a string as this is read only +func (r *ring) getPartitionStringKey(key string) *partition { + return r.partitions[int(xxhash.Sum64String(key)%uint64(len(r.partitions)))] +} + // entry returns the entry for the given key. // entry is safe for use by multiple goroutines. func (r *ring) entry(key []byte) *entry { @@ -89,8 +95,8 @@ func (r *ring) entry(key []byte) *entry { // write writes values to the entry in the ring's partition associated with key. // If no entry exists for the key then one will be created. // write is safe for use by multiple goroutines. -func (r *ring) write(key []byte, values Values) (bool, error) { - return r.getPartition(key).write(key, values) +func (r *ring) write(key string, values Values) (bool, error) { + return r.getPartitionStringKey(key).write(key, values) } // remove deletes the entry for the given key. @@ -218,9 +224,9 @@ func (p *partition) entry(key []byte) *entry { // write writes the values to the entry in the partition, creating the entry // if it does not exist. // write is safe for use by multiple goroutines. -func (p *partition) write(key []byte, values Values) (bool, error) { +func (p *partition) write(key string, values Values) (bool, error) { p.mu.RLock() - e := p.store[string(key)] + e := p.store[key] p.mu.RUnlock() if e != nil { // Hot path. @@ -231,7 +237,7 @@ func (p *partition) write(key []byte, values Values) (bool, error) { defer p.mu.Unlock() // Check again. - if e = p.store[string(key)]; e != nil { + if e = p.store[key]; e != nil { return false, e.add(values) } @@ -241,7 +247,7 @@ func (p *partition) write(key []byte, values Values) (bool, error) { return false, err } - p.store[string(key)] = e + p.store[key] = e return true, nil } diff --git a/tsdb/engine/tsm1/ring_test.go b/tsdb/engine/tsm1/ring_test.go index 172dccb5db8..c9449cddcf2 100644 --- a/tsdb/engine/tsm1/ring_test.go +++ b/tsdb/engine/tsm1/ring_test.go @@ -51,7 +51,7 @@ var strSliceRes [][]byte func benchmarkRingkeys(b *testing.B, r *ring, keys int) { // Add some keys for i := 0; i < keys; i++ { - r.write([]byte(fmt.Sprintf("cpu,host=server-%d value=1", i)), Values([]Value{ + r.write(fmt.Sprintf("cpu,host=server-%d value=1", i), Values([]Value{ IntegerValue{ unixnano: 1, value: int64(i), @@ -77,7 +77,7 @@ func benchmarkRingGetPartition(b *testing.B, r *ring, keys int) { // Add some keys for i := 0; i < keys; i++ { vals[i] = []byte(fmt.Sprintf("cpu,host=server-%d field1=value1,field2=value2,field4=value4,field5=value5,field6=value6,field7=value7,field8=value1,field9=value2,field10=value4,field11=value5,field12=value6,field13=value7", i)) - r.write([]byte(fmt.Sprintf("cpu,host=server-%d value=1", i)), Values([]Value{ + r.write(fmt.Sprintf("cpu,host=server-%d value=1", i), Values([]Value{ IntegerValue{ unixnano: 1, value: int64(i), @@ -109,7 +109,7 @@ func benchmarkRingWrite(b *testing.B, r *ring, n int) { go func() { defer wg.Done() for j := 0; j < n; j++ { - if _, err := r.write([]byte(fmt.Sprintf("cpu,host=server-%d value=1", j)), Values{}); err != nil { + if _, err := r.write(fmt.Sprintf("cpu,host=server-%d value=1", j), Values{}); err != nil { errC <- err } } diff --git a/tsdb/index/tsi1/index.go b/tsdb/index/tsi1/index.go index 97f9f5fe1df..3bdf20d18c5 100644 --- a/tsdb/index/tsi1/index.go +++ b/tsdb/index/tsi1/index.go @@ -13,7 +13,7 @@ import ( "time" "unsafe" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" "github.com/influxdata/influxdb/v2/models" "github.com/influxdata/influxdb/v2/pkg/estimator" "github.com/influxdata/influxdb/v2/pkg/estimator/hll" diff --git a/tsdb/series_file.go b/tsdb/series_file.go index 5e74ef9fe3b..883bd52c549 100644 --- a/tsdb/series_file.go +++ b/tsdb/series_file.go @@ -11,7 +11,7 @@ import ( "sort" "sync" - "github.com/cespare/xxhash" + "github.com/cespare/xxhash/v2" "github.com/influxdata/influxdb/v2/models" "github.com/influxdata/influxdb/v2/pkg/binaryutil" "github.com/influxdata/influxdb/v2/pkg/limiter"