Skip to content
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

Pool zstd encoding/decoding #4208

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* [ENHANCEMENT] Support exporting internal Tempo traces via OTLP exporter when `use_otel_tracer` is enabled. Use the OpenTelemetry SDK environment variables to configure the span exporter. [#4028](https://github.com/grafana/tempo/pull/4028) (@andreasgerstmayr)
* [ENHANCEMENT] TraceQL metrics queries: add min_over_time [#3975](https://github.com/grafana/tempo/pull/3975) (@javiermolinar)
* [ENHANCEMENT] Write tenantindex as proto and json with a prefernce for proto [#4072](https://github.com/grafana/tempo/pull/4072) (@zalegrala)
* [ENHANCEMENT] Pool zstd encoding/decoding for tmepodb/backend [#4208](https://github.com/grafana/tempo/pull/4208) (@zalegrala)

# v2.6.1

Expand Down
45 changes: 45 additions & 0 deletions tempodb/backend/compression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package backend

import (
"sync"

"github.com/klauspost/compress/zstd"
)

var _ Codec = (*ZstdCodec)(nil)

type Codec interface {
Encode([]byte, []byte) ([]byte, error)
Decode([]byte) ([]byte, error)
}

type ZstdCodec struct {
encoders sync.Pool // *zstd.Encoder
decoders sync.Pool // *zstd.Decoder
}

func (c *ZstdCodec) Encode(src, dst []byte) ([]byte, error) {
e, _ := c.encoders.Get().(*zstd.Encoder)
if e == nil {
var err error
e, err = zstd.NewWriter(nil, zstd.WithEncoderConcurrency(1))
if err != nil {
return nil, err
}
}
defer c.encoders.Put(e)
return e.EncodeAll(src, dst), nil
}

func (c *ZstdCodec) Decode(buf []byte) ([]byte, error) {
d, _ := c.decoders.Get().(*zstd.Decoder)
if d == nil {
var err error
d, err = zstd.NewReader(nil, zstd.WithDecoderConcurrency(0))
if err != nil {
return nil, err
}
}
defer c.decoders.Put(d)
return d.DecodeAll(buf, nil)
}
33 changes: 7 additions & 26 deletions tempodb/backend/tenantindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (

proto "github.com/gogo/protobuf/proto"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zstd"
)

const (
internalFilename = "index.json"
)

var _ proto.Message = (*TenantIndex)(nil)
var (
_ proto.Message = (*TenantIndex)(nil)
Zstd = &ZstdCodec{}
)

func newTenantIndex(meta []*BlockMeta, compactedMeta []*CompactedBlockMeta) *TenantIndex {
return &TenantIndex{
Expand Down Expand Up @@ -63,38 +65,17 @@ func (b *TenantIndex) unmarshal(buffer []byte) error {
}

func (b *TenantIndex) marshalPb() ([]byte, error) {
buffer := &bytes.Buffer{}

z, err := zstd.NewWriter(buffer)
if err != nil {
return nil, err
}

pbBytes, err := proto.Marshal(b)
if err != nil {
return nil, err
}

if _, err = z.Write(pbBytes); err != nil {
return nil, err
}
if err = z.Flush(); err != nil {
return nil, err
}
if err = z.Close(); err != nil {
return nil, err
}

return buffer.Bytes(), nil
buffer := []byte{}
return Zstd.Encode(pbBytes, buffer)
}

func (b *TenantIndex) unmarshalPb(buffer []byte) error {
decoder, err := zstd.NewReader(nil, zstd.WithDecoderConcurrency(0))
if err != nil {
return fmt.Errorf("error creating zstd decoder: %w", err)
}

bb, err := decoder.DecodeAll(buffer, nil)
bb, err := Zstd.Decode(buffer)
if err != nil {
return fmt.Errorf("error decoding zstd: %w", err)
}
Expand Down
41 changes: 41 additions & 0 deletions tempodb/backend/tenantindex_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,44 @@ func BenchmarkIndexUnmarshal(b *testing.B) {
_ = unIdx.unmarshal(buf)
}
}

func BenchmarkIndexUnmarshalPb(b *testing.B) {
idx := &TenantIndex{
Meta: []*BlockMeta{
NewBlockMeta("test", uuid.New(), "v1", EncGZIP, "adsf"),
NewBlockMeta("test", uuid.New(), "v2", EncNone, "adsf"),
NewBlockMeta("test", uuid.New(), "v3", EncLZ4_4M, "adsf"),
},
CompactedMeta: []*CompactedBlockMeta{
{
BlockMeta: *NewBlockMeta("test", uuid.New(), "v1", EncGZIP, "adsf"),
CompactedTime: time.Now(),
},
{
BlockMeta: *NewBlockMeta("test", uuid.New(), "v1", EncZstd, "adsf"),
CompactedTime: time.Now(),
},
{
BlockMeta: *NewBlockMeta("test", uuid.New(), "v1", EncSnappy, "adsf"),
CompactedTime: time.Now(),
},
},
}

for i := range idx.Meta {
idx.Meta[i].DedicatedColumns = DedicatedColumns{
{Scope: "resource", Name: "namespace", Type: "string"},
{Scope: "span", Name: "http.method", Type: "string"},
{Scope: "span", Name: "namespace", Type: "string"},
}
}

buf, err := idx.marshalPb()
require.NoError(b, err)

unIdx := &TenantIndex{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = unIdx.unmarshalPb(buf)
}
}