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

Experiment with string interning on BlockMeta #3931

Closed
wants to merge 8 commits into from
Closed
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
41 changes: 41 additions & 0 deletions pkg/intern/intern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package intern

import "sync"

// A Value pointer is the handle to an underlying comparable value.
type Value struct {
cmpVal string
}

// Get returns the comparable value passed to the Get func that returned v.
func (v *Value) Get() string { return v.cmpVal }

// Our pool of interned values and a lock to serialize access.
var (
mu sync.Mutex
val = map[string]*Value{}
)

// Get returns a pointer representing the comparable value cmpVal.
//
// The returned pointer will be the same for Get(v) and Get(v2)
// if and only if v == v2, and can be used as a map key.
//
// Note that Get returns a *Value so we only return one word of data
// to the caller, despite potentially storing a large amount of data
// within the Value itself.
func Get(cmpVal string) *Value {
mu.Lock()
defer mu.Unlock()

v := val[cmpVal]
if v != nil {
// Value is already interned in the pool.
return v
}

// Value must be created now and then interned.
v = &Value{cmpVal: cmpVal}
val[cmpVal] = v
return v
}
35 changes: 35 additions & 0 deletions pkg/intern/intern_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package intern

import (
"fmt"
"testing"
"unsafe"

"github.com/stretchr/testify/require"
)

func TestBasics(t *testing.T) {
x := Get("abc123")
y := Get("abc123")

if x.Get() != y.Get() {
t.Error("abc123 values differ")
}
if x.Get() != "abc123" {
t.Error("x.Get not abc123")
}
if x != y {
t.Error("abc123 pointers differ")
}

a1 := x.Get()
a2 := y.Get()

p1 := fmt.Sprintf("%08x\n", stringAddr(a1))
p2 := fmt.Sprintf("%08x\n", stringAddr(a2))
require.Equal(t, p1, p2)
}

func stringAddr(s string) uintptr {
return uintptr(unsafe.Pointer(unsafe.StringData(s)))
}
41 changes: 41 additions & 0 deletions tempodb/backend/block_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cespare/xxhash/v2"
"github.com/google/uuid"

"github.com/grafana/tempo/pkg/intern"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/grafana/tempo/pkg/traceql"
)
Expand Down Expand Up @@ -89,6 +90,28 @@ type CompactedBlockMeta struct {
CompactedTime time.Time `json:"compactedTime"`
}

func (b *CompactedBlockMeta) UnmarshalJSON(data []byte) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented with the alias here as well, but the embedding throws it off. I think we keep the custom change without the alias here unless I missed something obvious.

if err := json.Unmarshal(data, &b.BlockMeta); err != nil {
return fmt.Errorf("failed at unmarshal for dedicated columns: %w", err)
}

var msg interface{}
err := json.Unmarshal(data, &msg)
if err != nil {
return err
}
msgMap := msg.(map[string]interface{})

if v, ok := msgMap["compactedTime"]; ok {
b.CompactedTime, err = time.Parse(time.RFC3339, v.(string))
if err != nil {
return fmt.Errorf("failed to parse time at compactedTime: %w", err)
}
}

return nil
}

const (
DefaultReplicationFactor = 0 // Replication factor for blocks from the ingester. This is the default value to indicate RF3.
MetricsGeneratorReplicationFactor = 1
Expand Down Expand Up @@ -136,6 +159,19 @@ type BlockMeta struct {
ReplicationFactor uint32 `json:"replicationFactor,omitempty"`
}

func (b *BlockMeta) UnmarshalJSON(data []byte) error {
type metaAlias BlockMeta

err := json.Unmarshal(data, (*metaAlias)(b))
if err != nil {
return err
}
b.Version = intern.Get(b.Version).Get()
b.TenantID = intern.Get(b.TenantID).Get()

return nil
}

// DedicatedColumn contains the configuration for a single attribute with the given name that should
// be stored in a dedicated column instead of the generic attribute column.
type DedicatedColumn struct {
Expand Down Expand Up @@ -173,6 +209,11 @@ func (dc *DedicatedColumn) UnmarshalJSON(b []byte) error {
if dc.Type == "" {
dc.Type = DefaultDedicatedColumnType
}

dc.Scope = DedicatedColumnScope(intern.Get(string(dc.Scope)).Get())
dc.Type = DedicatedColumnType(intern.Get(string(dc.Type)).Get())
dc.Name = intern.Get(dc.Name).Get()

return nil
}

Expand Down
12 changes: 10 additions & 2 deletions tempodb/backend/block_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestBlockMetaParsing(t *testing.T) {

meta := BlockMeta{
Version: "vParquet3",
BlockID: uuid.MustParse("00000000-0000-0000-0000-000000000000"),
BlockID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
MinID: minID,
MaxID: maxID,
TenantID: "single-tenant",
Expand All @@ -135,7 +135,7 @@ func TestBlockMetaParsing(t *testing.T) {

expectedJSON := `{
"format": "vParquet3",
"blockID": "00000000-0000-0000-0000-000000000000",
"blockID": "00000000-0000-0000-0000-000000000001",
zalegrala marked this conversation as resolved.
Show resolved Hide resolved
"minID": "ACA/8tpRKjtPqxHXJDrBzA==",
"maxID": "8St6GtMRX/IHc0+rDQqyNQ==",
"tenantID": "single-tenant",
Expand Down Expand Up @@ -165,6 +165,14 @@ func TestBlockMetaParsing(t *testing.T) {
err = json.Unmarshal(metaJSON, &metaRoundtrip)
require.NoError(t, err)
assert.Equal(t, meta, metaRoundtrip)

var metaRoundtrip2 BlockMeta
err = json.Unmarshal(metaJSON, &metaRoundtrip2)
require.NoError(t, err)
assert.Equal(t, meta, metaRoundtrip2)

assert.Exactly(t, metaRoundtrip.Version, metaRoundtrip2.Version)
assert.Exactly(t, metaRoundtrip.TenantID, metaRoundtrip2.TenantID)
}

func TestDedicatedColumnsFromTempopb(t *testing.T) {
Expand Down
78 changes: 78 additions & 0 deletions tempodb/backend/tenantindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,81 @@ func TestIndexUnmarshalErrors(t *testing.T) {
err := test.unmarshal([]byte("bad data"))
assert.Error(t, err)
}

func BenchmarkIndexMarshal(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"},
}
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = idx.marshal()
}
}

func BenchmarkIndexUnmarshal(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.marshal()
require.NoError(b, err)

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