-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
cortex_util "github.com/cortexproject/cortex/pkg/util" | ||
"github.com/cortexproject/cortex/pkg/util/limiter" | ||
"github.com/cortexproject/cortex/pkg/util/services" | ||
lru "github.com/hashicorp/golang-lru" | ||
"github.com/pkg/errors" | ||
"go.uber.org/atomic" | ||
|
||
|
@@ -51,6 +52,8 @@ var ( | |
Name: "distributor_lines_received_total", | ||
Help: "The total number of lines received per tenant", | ||
}, []string{"tenant"}) | ||
|
||
maxLabelCacheSize = 10000 | ||
) | ||
|
||
// Config for a Distributor. | ||
|
@@ -86,6 +89,7 @@ type Distributor struct { | |
|
||
// Per-user rate limiter. | ||
ingestionRateLimiter *limiter.RateLimiter | ||
labelCache *lru.Cache | ||
} | ||
|
||
// New a distributor creates. | ||
|
@@ -121,6 +125,10 @@ func New(cfg Config, clientCfg client.Config, ingestersRing ring.ReadRing, overr | |
ingestionRateStrategy = newLocalIngestionRateStrategy(overrides) | ||
} | ||
|
||
labelCache, err := lru.New(maxLabelCacheSize) | ||
if err != nil { | ||
return nil, err | ||
} | ||
d := Distributor{ | ||
cfg: cfg, | ||
clientCfg: clientCfg, | ||
|
@@ -129,6 +137,7 @@ func New(cfg Config, clientCfg client.Config, ingestersRing ring.ReadRing, overr | |
validator: validator, | ||
pool: cortex_distributor.NewPool(clientCfg.PoolConfig, ingestersRing, factory, cortex_util.Logger), | ||
ingestionRateLimiter: limiter.NewRateLimiter(ingestionRateStrategy, 10*time.Second), | ||
labelCache: labelCache, | ||
} | ||
|
||
servs = append(servs, d.pool) | ||
|
@@ -206,17 +215,23 @@ func (d *Distributor) Push(ctx context.Context, req *logproto.PushRequest) (*log | |
validatedSamplesCount := 0 | ||
|
||
for _, stream := range req.Streams { | ||
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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
stream.Labels = ls.String() | ||
if err := d.validator.ValidateLabels(userID, ls, stream); err != nil { | ||
validationErr = err | ||
continue | ||
} | ||
d.labelCache.Add(stream.Labels, stream.Labels) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} else { | ||
stream.Labels = label.(string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
entries := make([]logproto.Entry, 0, len(stream.Entries)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.