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

Fix tailing #11911

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 24 additions & 10 deletions pkg/logql/log/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ type BaseLabelsBuilder struct {
without, noLabels bool
referencedStructuredMetadata bool

mu sync.RWMutex
resultCache map[uint64]LabelsResult
*hasher
}
Expand Down Expand Up @@ -182,25 +183,38 @@ func NewBaseLabelsBuilder() *BaseLabelsBuilder {
return NewBaseLabelsBuilderWithGrouping(nil, NoParserHints(), false, false)
}

func (b *BaseLabelsBuilder) putCache(hash uint64, result LabelsResult) {
b.mu.Lock()
b.resultCache[hash] = result
b.mu.Unlock()
}

func (b *BaseLabelsBuilder) getCache(hash uint64) (LabelsResult, bool) {
b.mu.RLock()
result, ok := b.resultCache[hash]
b.mu.RUnlock()
return result, ok
}

// ForLabels creates a labels builder for a given labels set as base.
// The labels cache is shared across all created LabelsBuilders.
func (b *BaseLabelsBuilder) ForLabels(lbs labels.Labels, hash uint64) *LabelsBuilder {
if labelResult, ok := b.resultCache[hash]; ok {
res := &LabelsBuilder{
if labelResult, ok := b.getCache(hash); ok {
return &LabelsBuilder{
base: lbs,
currentResult: labelResult,
BaseLabelsBuilder: b,
}
return res
}

labelResult := NewLabelsResult(lbs.String(), hash, lbs, labels.EmptyLabels(), labels.EmptyLabels())
b.resultCache[hash] = labelResult
res := &LabelsBuilder{
b.putCache(hash, labelResult)

return &LabelsBuilder{
base: lbs,
currentResult: labelResult,
BaseLabelsBuilder: b,
}
return res
}

// Reset clears all current state for the builder.
Expand Down Expand Up @@ -526,12 +540,12 @@ func (b *LabelsBuilder) LabelsResult() LabelsResult {
parsed := b.labels(ParsedLabel).Copy()
b.buf = flattenLabels(b.buf, stream, structuredMetadata, parsed)
hash := b.hasher.Hash(b.buf)
if cached, ok := b.resultCache[hash]; ok {
if cached, ok := b.getCache(hash); ok {
return cached
}

result := NewLabelsResult(b.buf.String(), hash, stream, structuredMetadata, parsed)
b.resultCache[hash] = result
b.putCache(hash, result)

return result
}
Expand All @@ -557,12 +571,12 @@ func flattenLabels(buf labels.Labels, many ...labels.Labels) labels.Labels {

func (b *BaseLabelsBuilder) toUncategorizedResult(buf labels.Labels) LabelsResult {
hash := b.hasher.Hash(buf)
if cached, ok := b.resultCache[hash]; ok {
if cached, ok := b.getCache(hash); ok {
return cached
}

res := NewLabelsResult(buf.String(), hash, buf.Copy(), nil, nil)
b.resultCache[hash] = res
b.putCache(hash, res)
return res
}

Expand Down
Loading