diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 06c871810e..e6cd4662d9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,3 +28,27 @@ jobs: - name: Run Tests run: GO=/usr/local/go/bin/go make common-test + + test-stringlabels: + runs-on: ubuntu-20.04 + steps: + - name: Upgrade golang + run: | + cd /tmp + wget https://dl.google.com/go/go1.20.3.linux-amd64.tar.gz + tar -zxvf go1.20.3.linux-amd64.tar.gz + sudo rm -fr /usr/local/go + sudo mv /tmp/go /usr/local/go + cd - + ls -l /usr/bin/go + + - name: Checkout Repo + uses: actions/checkout@v2 + + # This file would normally be created by `make assets`, here we just + # mock it because the file is required for the tests to pass. + - name: Mock building of necessary react file + run: mkdir web/ui/static/react && touch web/ui/static/react/index.html + + - name: Run Tests -tags=stringlabels + run: GO=/usr/local/go/bin/go GOOPTS=-tags=stringlabels make common-test \ No newline at end of file diff --git a/model/labels/sharding.go b/model/labels/sharding.go index 9b51c3ab88..0e3c4b49b2 100644 --- a/model/labels/sharding.go +++ b/model/labels/sharding.go @@ -1,3 +1,5 @@ +//go:build !stringlabels + package labels import ( diff --git a/model/labels/sharding_stringlabels.go b/model/labels/sharding_stringlabels.go new file mode 100644 index 0000000000..e9e4728346 --- /dev/null +++ b/model/labels/sharding_stringlabels.go @@ -0,0 +1,41 @@ +//go:build stringlabels + +package labels + +import ( + "github.com/cespare/xxhash/v2" +) + +// StableHash is a labels hashing implementation which is guaranteed to not change over time. +// This function should be used whenever labels hashing backward compatibility must be guaranteed. +func StableHash(ls Labels) uint64 { + // Use xxhash.Sum64(b) for fast path as it's faster. + b := make([]byte, 0, 1024) + var h *xxhash.Digest + for i := 0; i < len(ls.data); { + var v Label + v.Name, i = decodeString(ls.data, i) + v.Value, i = decodeString(ls.data, i) + if h == nil && len(b)+len(v.Name)+len(v.Value)+2 >= cap(b) { + // If labels entry is 1KB+, switch to Write API. Copy in the values up to this point. + h = xxhash.New() + _, _ = h.Write(b) + } + if h != nil { + _, _ = h.WriteString(v.Name) + _, _ = h.Write(seps) + _, _ = h.WriteString(v.Value) + _, _ = h.Write(seps) + continue + } + + b = append(b, v.Name...) + b = append(b, seps[0]) + b = append(b, v.Value...) + b = append(b, seps[0]) + } + if h != nil { + return h.Sum64() + } + return xxhash.Sum64(b) +} diff --git a/model/labels/sharding_test.go b/model/labels/sharding_test.go new file mode 100644 index 0000000000..dc83da5488 --- /dev/null +++ b/model/labels/sharding_test.go @@ -0,0 +1,19 @@ +package labels + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// TestStableHash tests that StableHash is stable. +// The hashes this test asserts should not be changed. +func TestStableHash(t *testing.T) { + for expectedHash, lbls := range map[uint64]Labels{ + 0xef46db3751d8e999: EmptyLabels(), + 0x347c8ee7a9e29708: FromStrings("hello", "world"), + 0xcbab40540f26097d: FromStrings(MetricName, "metric", "label", "value"), + } { + require.Equal(t, expectedHash, StableHash(lbls)) + } +}