Skip to content

Commit 9f50fcc

Browse files
committed
fix: split interval function should return 4xx
Signed-off-by: Ahmed Hassan <[email protected]>
1 parent fc6c40d commit 9f50fcc

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

pkg/querier/tripperware/queryrange/split_by_interval.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (s splitByInterval) Do(ctx context.Context, r tripperware.Request) (tripper
5454
// to line up the boundaries with step.
5555
interval, err := s.interval(ctx, r)
5656
if err != nil {
57-
return nil, httpgrpc.Errorf(http.StatusInternalServerError, err.Error())
57+
return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
5858
}
5959
reqs, err := splitQuery(r, interval)
6060
if err != nil {
@@ -153,11 +153,38 @@ func nextIntervalBoundary(t, step int64, interval time.Duration) int64 {
153153

154154
// Returns a fixed split interval
155155
func staticIntervalFn(cfg Config) func(ctx context.Context, r tripperware.Request) (time.Duration, error) {
156-
return func(_ context.Context, _ tripperware.Request) (time.Duration, error) {
156+
return func(_ context.Context, r tripperware.Request) (time.Duration, error) {
157+
totalRange, err := getTotalRangeFromRequest(r)
158+
if err != nil {
159+
return cfg.SplitQueriesByInterval, err
160+
}
161+
// Hardcode 7 day here, assuming SplitQueriesByInterval is configured as 1 day.
162+
if totalRange > 7*cfg.SplitQueriesByInterval {
163+
return 7 * cfg.SplitQueriesByInterval, nil
164+
}
157165
return cfg.SplitQueriesByInterval, nil
158166
}
159167
}
160168

169+
// getTotalRangeFromRequest gets MatrixSelector and SubqueryExpr range sum from the query.
170+
func getTotalRangeFromRequest(r tripperware.Request) (time.Duration, error) {
171+
expr, err := parser.ParseExpr(r.GetQuery())
172+
if err != nil {
173+
return 0, err
174+
}
175+
totalRange := time.Duration(0)
176+
parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
177+
switch n := node.(type) {
178+
case *parser.MatrixSelector:
179+
totalRange += n.Range
180+
case *parser.SubqueryExpr:
181+
totalRange += n.Range
182+
}
183+
return nil
184+
})
185+
return totalRange, nil
186+
}
187+
161188
// Returns a dynamic multiple of base interval adjusted depending on configured 'max_shards_per_query' and 'max_fetched_data_duration_per_query'
162189
func dynamicIntervalFn(cfg Config, limits tripperware.Limits, queryAnalyzer querysharding.Analyzer, lookbackDelta time.Duration) func(ctx context.Context, r tripperware.Request) (time.Duration, error) {
163190
return func(ctx context.Context, r tripperware.Request) (time.Duration, error) {

0 commit comments

Comments
 (0)