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

created lru cache in querysharding pkg #5749

Merged
merged 15 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions pkg/queryfrontend/shard_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// PromQLShardingMiddleware creates a new Middleware that shards PromQL aggregations using grouping labels.
func PromQLShardingMiddleware(queryAnalyzer *querysharding.QueryAnalyzer, numShards int, limits queryrange.Limits, merger queryrange.Merger, registerer prometheus.Registerer) queryrange.Middleware {
func PromQLShardingMiddleware(queryAnalyzer querysharding.Analyze, numShards int, limits queryrange.Limits, merger queryrange.Merger, registerer prometheus.Registerer) queryrange.Middleware {
return queryrange.MiddlewareFunc(func(next queryrange.Handler) queryrange.Handler {
queriesTotal := promauto.With(registerer).NewCounterVec(prometheus.CounterOpts{
Namespace: "thanos",
Expand All @@ -45,7 +45,7 @@ type querySharder struct {
next queryrange.Handler
limits queryrange.Limits

queryAnalyzer *querysharding.QueryAnalyzer
queryAnalyzer querysharding.Analyze
numShards int
merger queryrange.Merger

Expand Down
49 changes: 44 additions & 5 deletions pkg/querysharding/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,63 @@ package querysharding
import (
"fmt"

lru "github.com/hashicorp/golang-lru"
"github.com/prometheus/prometheus/promql/parser"
)

// QueryAnalyzer is an analyzer which determines
// whether a PromQL Query is shardable and using which labels.
type QueryAnalyzer struct{}

type Analyze interface {
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
Analyze(string) (QueryAnalysis, error)
}

type QueryAnalyzer struct {
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
}

type CachedQueryAnalyzer struct {
analyzer *QueryAnalyzer
cache *lru.Cache
}

var nonShardableFuncs = []string{
"label_join",
"label_replace",
}

// NewQueryAnalyzer creates a new QueryAnalyzer.
func NewQueryAnalyzer() *QueryAnalyzer {
return &QueryAnalyzer{}
func NewQueryAnalyzer() *CachedQueryAnalyzer {
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
cache, _ := lru.New(256)
yeya24 marked this conversation as resolved.
Show resolved Hide resolved

return &CachedQueryAnalyzer{
analyzer: &QueryAnalyzer{},
cache: cache,
}
}

type cachedValue struct {
QueryAnalysis QueryAnalysis
err error
}

// Analyze analyzes a query and returns a QueryAnalysis.
func (a *CachedQueryAnalyzer) Analyze(query string) (QueryAnalysis, error) {
if a.cache.Contains(query) {
value, _ := a.cache.Get(query)
return value.(cachedValue).QueryAnalysis, value.(cachedValue).err
}

// Analyze if needed
analysis, err := a.analyzer.Analyze(query)

// Analyze uses the following algorithm:
// Adding to cache
yeya24 marked this conversation as resolved.
Show resolved Hide resolved
_ = a.cache.Add(query, cachedValue{QueryAnalysis: analysis, err: err})

return analysis, err
}

// queryAnalyzer analyzes a query and returns a QueryAnalysis.

// queryAnalyzer uses the following algorithm:
// * if a query has subqueries, such as label_join or label_replace,
// or has functions which cannot be sharded, then treat the query as non shardable.
// * if the query's root expression has grouping labels,
Expand All @@ -36,7 +73,9 @@ func NewQueryAnalyzer() *QueryAnalyzer {
// as shardable by those labels.
// * otherwise, treat the query as non-shardable.
// The le label is excluded from sharding.

func (a *QueryAnalyzer) Analyze(query string) (QueryAnalysis, error) {

yeya24 marked this conversation as resolved.
Show resolved Hide resolved
expr, err := parser.ParseExpr(query)
if err != nil {
return nonShardableQuery(), err
Expand Down
1 change: 1 addition & 0 deletions pkg/querysharding/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestAnalyzeQuery(t *testing.T) {

type testCase struct {
name string
expression string
Expand Down