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

Reuse keyvalues #1165

Merged
merged 5 commits into from
Dec 10, 2021
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 @@ -19,6 +19,7 @@
* [ENHANCEMENT] Switch open-telemetry/opentelemetry-collector to grafana/opentelemetry-collectorl fork, update it to 0.40.0 and add missing dependencies due to the change [#1142](https://github.com/grafana/tempo/pull/1142) (@tete17)
* [ENHANCEMENT] Allow environment variables for Azure storage credentials [#1147](https://github.com/grafana/tempo/pull/1147) (@zalegrala)
* [ENHANCEMENT] jsonnet: set rollingUpdate.maxSurge to 3 for distributor, frontend and queriers [#1164](https://github.com/grafana/tempo/pull/1164) (@kvrhdn)
* [ENHANCEMENT] Reduce search data file sizes by optimizing contents [#1165](https://github.com/grafana/tempo/pull/1165) (@mdisibio)
* [BUGFIX] Add process name to vulture traces to work around display issues [#1127](https://github.com/grafana/tempo/pull/1127) (@mdisibio)
* [BUGFIX] Fixed issue where compaction sometimes dropped spans. [#1130](https://github.com/grafana/tempo/pull/1130) (@joe-elliott)
* [BUGFIX] Ensure that the admin client jsonnet has correct S3 bucket property. (@hedss)
Expand Down
2 changes: 1 addition & 1 deletion pkg/tempofb/SearchBlockHeader_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *SearchBlockHeaderMutable) Contains(k []byte, v []byte, _ *KeyValues) bo
func (s *SearchBlockHeaderMutable) ToBytes() []byte {
b := flatbuffers.NewBuilder(1024)

tags := s.Tags.WriteToBuilder(b)
tags := WriteSearchDataMap(b, s.Tags, nil)

SearchBlockHeaderStart(b)
SearchBlockHeaderAddMinDurationNanos(b, s.MinDur)
Expand Down
60 changes: 60 additions & 0 deletions pkg/tempofb/search_entry_mutable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tempofb

import (
flatbuffers "github.com/google/flatbuffers/go"
"github.com/grafana/tempo/tempodb/encoding/common"
)

// SearchEntryMutable is a mutable form of the flatbuffer-compiled SearchEntry struct to make building and transporting easier.
type SearchEntryMutable struct {
TraceID common.ID
Tags SearchDataMap
StartTimeUnixNano uint64
EndTimeUnixNano uint64
}

// AddTag adds the unique tag name and value to the search data. No effect if the pair is already present.
func (s *SearchEntryMutable) AddTag(k string, v string) {
if s.Tags == nil {
s.Tags = NewSearchDataMap()
}
s.Tags.Add(k, v)
}

// SetStartTimeUnixNano records the earliest of all timestamps passed to this function.
func (s *SearchEntryMutable) SetStartTimeUnixNano(t uint64) {
if t > 0 && (s.StartTimeUnixNano == 0 || s.StartTimeUnixNano > t) {
s.StartTimeUnixNano = t
}
}

// SetEndTimeUnixNano records the latest of all timestamps passed to this function.
func (s *SearchEntryMutable) SetEndTimeUnixNano(t uint64) {
if t > 0 && t > s.EndTimeUnixNano {
s.EndTimeUnixNano = t
}
}

func (s *SearchEntryMutable) ToBytes() []byte {
b := flatbuffers.NewBuilder(2048)
offset := s.WriteToBuilder(b, nil)
b.Finish(offset)
return b.FinishedBytes()
}

func (s *SearchEntryMutable) WriteToBuilder(b *flatbuffers.Builder, kvCache map[uint64]flatbuffers.UOffsetT) flatbuffers.UOffsetT {
if s.Tags == nil {
s.Tags = NewSearchDataMap()
}

idOffset := b.CreateByteString(s.TraceID)

tagOffset := WriteSearchDataMap(b, s.Tags, kvCache)

SearchEntryStart(b)
SearchEntryAddId(b, idOffset)
SearchEntryAddStartTimeUnixNano(b, s.StartTimeUnixNano)
SearchEntryAddEndTimeUnixNano(b, s.EndTimeUnixNano)
SearchEntryAddTags(b, tagOffset)
return SearchEntryEnd(b)
}
66 changes: 66 additions & 0 deletions pkg/tempofb/search_page_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tempofb

import flatbuffers "github.com/google/flatbuffers/go"

type SearchPageBuilder struct {
builder *flatbuffers.Builder
allTags SearchDataMap
pageEntries []flatbuffers.UOffsetT
kvcache map[uint64]flatbuffers.UOffsetT
}

func NewSearchPageBuilder() *SearchPageBuilder {
return &SearchPageBuilder{
builder: flatbuffers.NewBuilder(1024),
allTags: NewSearchDataMap(),
kvcache: map[uint64]flatbuffers.UOffsetT{},
}
}

func (b *SearchPageBuilder) AddData(data *SearchEntryMutable) int {
if data.Tags != nil {
data.Tags.Range(func(k, v string) {
b.allTags.Add(k, v)
})
}

oldOffset := b.builder.Offset()
offset := data.WriteToBuilder(b.builder, b.kvcache)
b.pageEntries = append(b.pageEntries, offset)

// bytes written
return int(offset - oldOffset)
}

func (b *SearchPageBuilder) Finish() []byte {
// At this point all individual entries have been written
// to the fb builder. Now we need to wrap them up in the final
// batch object.

// Create vector
SearchPageStartEntriesVector(b.builder, len(b.pageEntries))
for _, entry := range b.pageEntries {
b.builder.PrependUOffsetT(entry)
}
entryVector := b.builder.EndVector(len(b.pageEntries))

// Create batch-level tags
tagOffset := WriteSearchDataMap(b.builder, b.allTags, b.kvcache)

// Write final batch object
SearchPageStart(b.builder)
SearchPageAddEntries(b.builder, entryVector)
SearchPageAddTags(b.builder, tagOffset)
batch := SearchPageEnd(b.builder)
b.builder.Finish(batch)
buf := b.builder.FinishedBytes()

return buf
}

func (b *SearchPageBuilder) Reset() {
b.builder.Reset()
b.pageEntries = b.pageEntries[:0]
b.allTags = NewSearchDataMap()
b.kvcache = map[uint64]flatbuffers.UOffsetT{}
}
115 changes: 0 additions & 115 deletions pkg/tempofb/searchdata_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,123 +4,8 @@ import (
"bytes"

flatbuffers "github.com/google/flatbuffers/go"
"github.com/grafana/tempo/tempodb/encoding/common"
)

// SearchEntryMutable is a mutable form of the flatbuffer-compiled SearchEntry struct to make building and transporting easier.
type SearchEntryMutable struct {
TraceID common.ID
Tags SearchDataMap
StartTimeUnixNano uint64
EndTimeUnixNano uint64
}

// AddTag adds the unique tag name and value to the search data. No effect if the pair is already present.
func (s *SearchEntryMutable) AddTag(k string, v string) {
if s.Tags == nil {
s.Tags = NewSearchDataMap()
}
s.Tags.Add(k, v)
}

// SetStartTimeUnixNano records the earliest of all timestamps passed to this function.
func (s *SearchEntryMutable) SetStartTimeUnixNano(t uint64) {
if t > 0 && (s.StartTimeUnixNano == 0 || s.StartTimeUnixNano > t) {
s.StartTimeUnixNano = t
}
}

// SetEndTimeUnixNano records the latest of all timestamps passed to this function.
func (s *SearchEntryMutable) SetEndTimeUnixNano(t uint64) {
if t > 0 && t > s.EndTimeUnixNano {
s.EndTimeUnixNano = t
}
}

func (s *SearchEntryMutable) ToBytes() []byte {
b := flatbuffers.NewBuilder(2048)
offset := s.WriteToBuilder(b)
b.Finish(offset)
return b.FinishedBytes()
}

func (s *SearchEntryMutable) WriteToBuilder(b *flatbuffers.Builder) flatbuffers.UOffsetT {
if s.Tags == nil {
s.Tags = NewSearchDataMap()
}

idOffset := b.CreateByteString(s.TraceID)

tagOffset := s.Tags.WriteToBuilder(b)

SearchEntryStart(b)
SearchEntryAddId(b, idOffset)
SearchEntryAddStartTimeUnixNano(b, s.StartTimeUnixNano)
SearchEntryAddEndTimeUnixNano(b, s.EndTimeUnixNano)
SearchEntryAddTags(b, tagOffset)
return SearchEntryEnd(b)
}

type SearchPageBuilder struct {
builder *flatbuffers.Builder
allTags SearchDataMap
pageEntries []flatbuffers.UOffsetT
}

func NewSearchPageBuilder() *SearchPageBuilder {
return &SearchPageBuilder{
builder: flatbuffers.NewBuilder(1024),
allTags: NewSearchDataMap(),
}
}

func (b *SearchPageBuilder) AddData(data *SearchEntryMutable) int {
if data.Tags != nil {
data.Tags.Range(func(k, v string) {
b.allTags.Add(k, v)
})
}

oldOffset := b.builder.Offset()
offset := data.WriteToBuilder(b.builder)
b.pageEntries = append(b.pageEntries, offset)

// bytes written
return int(offset - oldOffset)
}

func (b *SearchPageBuilder) Finish() []byte {
// At this point all individual entries have been written
// to the fb builder. Now we need to wrap them up in the final
// batch object.

// Create vector
SearchPageStartEntriesVector(b.builder, len(b.pageEntries))
for _, entry := range b.pageEntries {
b.builder.PrependUOffsetT(entry)
}
entryVector := b.builder.EndVector(len(b.pageEntries))

// Create batch-level tags
tagOffset := b.allTags.WriteToBuilder(b.builder)

// Write final batch object
SearchPageStart(b.builder)
SearchPageAddEntries(b.builder, entryVector)
SearchPageAddTags(b.builder, tagOffset)
batch := SearchPageEnd(b.builder)
b.builder.Finish(batch)
buf := b.builder.FinishedBytes()

return buf
}

func (b *SearchPageBuilder) Reset() {
b.builder.Reset()
b.pageEntries = b.pageEntries[:0]
b.allTags = NewSearchDataMap()
}

// Get searches the entry and returns the first value found for the given key.
func (s *SearchEntry) Get(k string) string {
kv := &KeyValues{}
Expand Down
Loading