Skip to content

perf(blooms): Replace JSON lib for encoding/decoding metas #14767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/storage/stores/shipper/bloomshipper/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bloomshipper

import (
"context"
"encoding/json"
"os"
"path/filepath"
"sync"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/dolthub/swiss"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/utils/keymutex"
Expand Down Expand Up @@ -189,6 +189,8 @@ func (f *Fetcher) processMetasCacheResponse(_ context.Context, refs []MetaRef, k

var lastErr error
var size int64

json := jsoniter.ConfigFastest
for i, ref := range refs {
if raw, ok := found[f.client.Meta(ref).Addr()]; ok {
meta := Meta{
Expand All @@ -209,6 +211,8 @@ func (f *Fetcher) writeBackMetas(ctx context.Context, metas []Meta) error {
var err error
keys := make([]string, len(metas))
data := make([][]byte, len(metas))

json := jsoniter.ConfigFastest
for i := range metas {
keys[i] = f.client.Meta(metas[i].MetaRef).Addr()
data[i], err = json.Marshal(metas[i])
Expand Down
53 changes: 46 additions & 7 deletions pkg/storage/stores/shipper/bloomshipper/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -436,16 +437,54 @@ func TestFetcher_IsBlockDir(t *testing.T) {
})
}

func metasFromCache(data map[string][]byte) []Meta {
metas := make([]Meta, 0, len(data))
for _, v := range data {
func Benchmark_Fetcher_processMetasCacheResponse(b *testing.B) {
b.Log(b.N)

cfg := bloomStoreConfig{
numWorkers: 1,
workingDirs: []string{b.TempDir()},
}

c, err := NewBloomClient(cfg, nil, log.NewNopLogger())
require.NoError(b, err)
f, err := NewFetcher(cfg, c, nil, nil, nil, log.NewNopLogger(), v1.NewMetrics(nil))
require.NoError(b, err)

step := math.MaxUint64 / uint64(b.N)

refs := make([]MetaRef, 0, b.N)
keys := make([]string, 0, b.N)
bufs := make([][]byte, 0, b.N)

for i := 0; i < b.N; i++ {
minFp := model.Fingerprint(uint64(i) * step)
maxFp := model.Fingerprint(uint64(i)*step + step)

ref := Ref{
TenantID: "fake",
TableName: "tsdb_index_20000",
Bounds: v1.NewBounds(minFp, maxFp),
StartTimestamp: model.Earliest,
EndTimestamp: model.Latest,
}
metaRef := MetaRef{Ref: ref}
refs = append(refs, metaRef)

keys = append(keys, f.client.Meta(metaRef).Addr())

meta := Meta{
MetaRef: MetaRef{},
MetaRef: metaRef,
Blocks: []BlockRef{{Ref: ref}},
}
_ = json.Unmarshal(v, &meta)
metas = append(metas, meta)

buf, _ := json.Marshal(meta)
bufs = append(bufs, buf)
}
return metas

b.ReportAllocs()
b.ResetTimer()

_, _, _ = f.processMetasCacheResponse(context.TODO(), refs, keys, bufs)
}

func metaRefs(metas []Meta) []MetaRef {
Expand Down
Loading