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

lru cache logql.ParseLabels #3092

Merged
merged 5 commits into from
Jan 5, 2021
Merged

Conversation

liguozhong
Copy link
Contributor

lru cache logql.ParseLabels

@CLAassistant
Copy link

CLAassistant commented Dec 18, 2020

CLA assistant check
All committers have signed the CLA.

@codecov-io
Copy link

codecov-io commented Dec 18, 2020

Codecov Report

Merging #3092 (e6b71dd) into master (48b6583) will increase coverage by 0.06%.
The diff coverage is 75.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3092      +/-   ##
==========================================
+ Coverage   62.78%   62.85%   +0.06%     
==========================================
  Files         186      186              
  Lines       15949    15960      +11     
==========================================
+ Hits        10014    10031      +17     
+ Misses       5009     5000       -9     
- Partials      926      929       +3     
Impacted Files Coverage Δ
pkg/distributor/distributor.go 77.02% <75.00%> (+1.11%) ⬆️
pkg/logql/evaluator.go 90.23% <0.00%> (+0.35%) ⬆️
pkg/promtail/targets/file/filetarget.go 64.33% <0.00%> (+0.69%) ⬆️
pkg/canary/comparator/comparator.go 78.18% <0.00%> (+1.81%) ⬆️

Copy link
Contributor

@sandeepsukhani sandeepsukhani left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!
I have left some feedback.

Comment on lines 218 to 234
label, ok := d.labelCache.Get(stream.Labels)
if !ok {
ls, err := logql.ParseLabels(stream.Labels)
if err != nil {
validationErr = httpgrpc.Errorf(http.StatusBadRequest, "error parsing labels: %v", err)
continue
}
// ensure labels are correctly sorted.
// todo(ctovena) we should lru cache this
stream.Labels = ls.String()
if err := d.validator.ValidateLabels(userID, ls, stream); err != nil {
validationErr = err
continue
}
d.labelCache.Add(stream.Labels, stream.Labels)
} else {
stream.Labels = label.(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be good to move this to a separate function and write a benchmark for it.

validationErr = err
continue
}
d.labelCache.Add(stream.Labels, stream.Labels)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look right to me. The key should be raw labels and the value should be the sorted labels, no?

continue
}
// ensure labels are correctly sorted.
// todo(ctovena) we should lru cache this
Copy link
Contributor

Choose a reason for hiding this comment

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

We should remove this todo now that it is being taken care of in this PR.

@liguozhong
Copy link
Contributor Author

liguozhong commented Jan 4, 2021

Thanks for the PR!
I have left some feedback.

thanks , done

goos: darwin
goarch: amd64

cache result
pkg: github.com/grafana/loki/pkg/distributor
Benchmark_SortLabelsOnPush
Benchmark_SortLabelsOnPush-12 619388 1887 ns/op
PASS

no cache result
pkg: github.com/grafana/loki/pkg/distributor
Benchmark_SortLabelsOnPush
Benchmark_SortLabelsOnPush-12 343987 3478 ns/op
PASS

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

A few small things, but this looks great. Really excited to see the lru cache here!

d := prepare(&testing.T{}, limits, nil, func(addr string) (ring_client.PoolClient, error) { return ingester, nil })
defer services.StopAndAwaitTerminated(context.Background(), d) //nolint:errcheck
for n := 0; n < b.N; n++ {
request := makeWriteRequest(10, 10)
Copy link
Member

Choose a reason for hiding this comment

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

nit: these can be created/reused outside the loop to interfere less with benchmarking (initialization doesn't need to be part of the benchmarking).

@@ -51,6 +52,8 @@ var (
Name: "distributor_lines_received_total",
Help: "The total number of lines received per tenant",
}, []string{"tenant"})

maxLabelCacheSize = 10000
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
maxLabelCacheSize = 10000
maxLabelCacheSize = 100000

I think 100k might be a better starting point for this. Unlike ingesters, distributors handle all incoming traffic and are therefore exposed to many more unique label sets.

return "", httpgrpc.Errorf(http.StatusBadRequest, "error parsing labels: %v", err)
}
// ensure labels are correctly sorted.
lsVal := ls.String()
Copy link
Member

Choose a reason for hiding this comment

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

nit: lsVal can be created after the validation occurs as a tiny optimization.

@liguozhong
Copy link
Contributor Author

A few small things, but this looks great. Really excited to see the lru cache here!

thanks , done

@owen-d owen-d merged commit bd321fb into grafana:master Jan 5, 2021
cyriltovena pushed a commit to cyriltovena/loki that referenced this pull request Jun 11, 2021
…locks (grafana#3092)

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants