-
Couldn't load subscription status.
- Fork 836
Add fallback logic to thanos promql engine #6630
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
Merged
yeya24
merged 1 commit into
cortexproject:master
from
SungJin1212:Add-thanos-engine-fallback
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package querier | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| "github.com/prometheus/prometheus/promql" | ||
| "github.com/prometheus/prometheus/storage" | ||
| "github.com/thanos-io/promql-engine/engine" | ||
| "github.com/thanos-io/promql-engine/logicalplan" | ||
| ) | ||
|
|
||
| type EngineFactory struct { | ||
| prometheusEngine *promql.Engine | ||
| thanosEngine *engine.Engine | ||
|
|
||
| fallbackQueriesTotal prometheus.Counter | ||
| } | ||
|
|
||
| func NewEngineFactory(opts promql.EngineOpts, enableThanosEngine bool, reg prometheus.Registerer) *EngineFactory { | ||
| prometheusEngine := promql.NewEngine(opts) | ||
|
|
||
| var thanosEngine *engine.Engine | ||
| if enableThanosEngine { | ||
| thanosEngine = engine.New(engine.Opts{ | ||
| EngineOpts: opts, | ||
| LogicalOptimizers: logicalplan.AllOptimizers, | ||
| EnableAnalysis: true, | ||
| }) | ||
| } | ||
|
|
||
| return &EngineFactory{ | ||
| prometheusEngine: prometheusEngine, | ||
| thanosEngine: thanosEngine, | ||
| fallbackQueriesTotal: promauto.With(reg).NewCounter(prometheus.CounterOpts{ | ||
| Name: "cortex_thanos_engine_fallback_queries_total", | ||
| Help: "Total number of fallback queries due to not implementation in thanos engine", | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| func (qf *EngineFactory) NewInstantQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, ts time.Time) (promql.Query, error) { | ||
| if qf.thanosEngine != nil { | ||
| res, err := qf.thanosEngine.MakeInstantQuery(ctx, q, fromPromQLOpts(opts), qs, ts) | ||
| if err != nil { | ||
| if engine.IsUnimplemented(err) { | ||
| // fallback to use prometheus engine | ||
| qf.fallbackQueriesTotal.Inc() | ||
| goto fallback | ||
| } | ||
| return nil, err | ||
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| fallback: | ||
| return qf.prometheusEngine.NewInstantQuery(ctx, q, opts, qs, ts) | ||
| } | ||
|
|
||
| func (qf *EngineFactory) NewRangeQuery(ctx context.Context, q storage.Queryable, opts promql.QueryOpts, qs string, start, end time.Time, interval time.Duration) (promql.Query, error) { | ||
| if qf.thanosEngine != nil { | ||
| res, err := qf.thanosEngine.MakeRangeQuery(ctx, q, fromPromQLOpts(opts), qs, start, end, interval) | ||
| if err != nil { | ||
| if engine.IsUnimplemented(err) { | ||
| // fallback to use prometheus engine | ||
| qf.fallbackQueriesTotal.Inc() | ||
| goto fallback | ||
| } | ||
| return nil, err | ||
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| fallback: | ||
| return qf.prometheusEngine.NewRangeQuery(ctx, q, opts, qs, start, end, interval) | ||
| } | ||
|
|
||
| func fromPromQLOpts(opts promql.QueryOpts) *engine.QueryOpts { | ||
| if opts == nil { | ||
| return &engine.QueryOpts{} | ||
| } | ||
| return &engine.QueryOpts{ | ||
| LookbackDeltaParam: opts.LookbackDelta(), | ||
| EnablePerStepStatsParam: opts.EnablePerStepStats(), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package querier | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/testutil" | ||
| "github.com/prometheus/prometheus/promql/parser" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/util/flagext" | ||
| "github.com/cortexproject/cortex/pkg/util/validation" | ||
| ) | ||
|
|
||
| func TestEngineFactory_Fallback(t *testing.T) { | ||
| // add unimplemented function | ||
| parser.Functions["unimplemented"] = &parser.Function{ | ||
| Name: "unimplemented", | ||
| ArgTypes: []parser.ValueType{parser.ValueTypeVector}, | ||
| ReturnType: parser.ValueTypeVector, | ||
| } | ||
|
|
||
| cfg := Config{} | ||
| flagext.DefaultValues(&cfg) | ||
| cfg.ThanosEngine = true | ||
| ctx := context.Background() | ||
| reg := prometheus.NewRegistry() | ||
|
|
||
| chunkStore := &emptyChunkStore{} | ||
| distributor := &errDistributor{} | ||
|
|
||
| overrides, err := validation.NewOverrides(DefaultLimitsConfig(), nil) | ||
| require.NoError(t, err) | ||
|
|
||
| now := time.Now() | ||
| start := time.Now().Add(-time.Minute * 5) | ||
| step := time.Minute | ||
| queryable, _, queryEngine := New(cfg, overrides, distributor, []QueryableWithFilter{UseAlwaysQueryable(NewMockStoreQueryable(chunkStore))}, reg, log.NewNopLogger(), nil) | ||
|
|
||
| // instant query, should go to fallback | ||
| _, _ = queryEngine.NewInstantQuery(ctx, queryable, nil, "unimplemented(foo)", now) | ||
| require.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(` | ||
| # HELP cortex_thanos_engine_fallback_queries_total Total number of fallback queries due to not implementation in thanos engine | ||
| # TYPE cortex_thanos_engine_fallback_queries_total counter | ||
| cortex_thanos_engine_fallback_queries_total 1 | ||
| `), "cortex_thanos_engine_fallback_queries_total")) | ||
|
|
||
| // range query, should go to fallback | ||
| _, _ = queryEngine.NewRangeQuery(ctx, queryable, nil, "unimplemented(foo)", start, now, step) | ||
| require.NoError(t, testutil.GatherAndCompare(reg, bytes.NewBufferString(` | ||
| # HELP cortex_thanos_engine_fallback_queries_total Total number of fallback queries due to not implementation in thanos engine | ||
| # TYPE cortex_thanos_engine_fallback_queries_total counter | ||
| cortex_thanos_engine_fallback_queries_total 2 | ||
| `), "cortex_thanos_engine_fallback_queries_total")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 35 additions & 28 deletions
63
vendor/github.com/thanos-io/promql-engine/engine/distributed.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we add a counter metric for number of fallbacks because new engine doesn't support it?
Similar to
https://github.com/thanos-io/promql-engine/pull/518/files#diff-2e6c4934f63ff9b712c2c346b33036af4724adf70b0801fff9b74f71b37fcd89L180
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 add a metric and update the pr.