From 08d5d74ae29a9877d771d13b9c7799e8e4eb59e7 Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Wed, 16 Aug 2023 10:22:24 -0700 Subject: [PATCH] Vertical shard binary expression using metric name when no matching label (#6605) * vertically shard binary expression even if no matching labels Signed-off-by: Ben Ye * add changelog Signed-off-by: Ben Ye * fix test Signed-off-by: Ben Ye --------- Signed-off-by: Ben Ye --- CHANGELOG.md | 2 ++ pkg/querysharding/analyzer.go | 2 +- pkg/querysharding/analyzer_test.go | 30 +++++++++++++++++++++--------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75185bddc46..b28329da173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Added +- [#6605](https://github.com/thanos-io/thanos/pull/6605) Query Frontend: Support vertical sharding binary expression with metric name when no matching labels specified. + ### Changed ### Removed diff --git a/pkg/querysharding/analyzer.go b/pkg/querysharding/analyzer.go index 12cb1b9a1a0..5dcfdb07fe1 100644 --- a/pkg/querysharding/analyzer.go +++ b/pkg/querysharding/analyzer.go @@ -113,7 +113,7 @@ func (a *QueryAnalyzer) Analyze(query string) (QueryAnalysis, error) { case *parser.BinaryExpr: if n.VectorMatching != nil { shardingLabels := without(n.VectorMatching.MatchingLabels, []string{"le"}) - if !n.VectorMatching.On && len(shardingLabels) > 0 { + if !n.VectorMatching.On { shardingLabels = append(shardingLabels, model.MetricNameLabel) } analysis = analysis.scopeToLabels(shardingLabels, n.VectorMatching.On) diff --git a/pkg/querysharding/analyzer_test.go b/pkg/querysharding/analyzer_test.go index bd999397db7..ba47b6638cf 100644 --- a/pkg/querysharding/analyzer_test.go +++ b/pkg/querysharding/analyzer_test.go @@ -32,10 +32,6 @@ func TestAnalyzeQuery(t *testing.T) { name: "outer aggregation with without grouping", expression: "count(sum without (pod) (http_requests_total))", }, - { - name: "binary expression", - expression: `http_requests_total{code="400"} / http_requests_total`, - }, { name: "binary expression with constant", expression: `http_requests_total{code="400"} / 4`, @@ -48,10 +44,6 @@ func TestAnalyzeQuery(t *testing.T) { name: "binary aggregation with different grouping labels", expression: `sum by (pod) (http_requests_total{code="400"}) / sum by (cluster) (http_requests_total)`, }, - { - name: "multiple binary expressions", - expression: `(http_requests_total{code="400"} + http_requests_total{code="500"}) / http_requests_total`, - }, { name: "multiple binary expressions with empty vector matchers", expression: ` @@ -211,7 +203,7 @@ sum by (container) ( { name: "binary expression with outer without grouping", expression: `sum(http_requests_total{code="400"} * http_requests_total) without (pod)`, - shardingLabels: []string{"pod"}, + shardingLabels: []string{"__name__", "pod"}, }, { name: "binary expression with vector matching and outer without grouping", @@ -256,6 +248,26 @@ http_requests_total`, expression: `sum without (pod) (label_replace(metric, "dst_label", "$1", "src_label", "re"))`, shardingLabels: []string{"pod", "dst_label"}, }, + { + name: "binary expression", + expression: `http_requests_total{code="400"} / http_requests_total`, + shardingLabels: []string{model.MetricNameLabel}, + }, + { + name: "binary expression among vector and scalar", + expression: `aaaa - bbb > 1000`, + shardingLabels: []string{model.MetricNameLabel}, + }, + { + name: "binary expression with set operation", + expression: `aaaa and bbb`, + shardingLabels: []string{model.MetricNameLabel}, + }, + { + name: "multiple binary expressions", + expression: `(http_requests_total{code="400"} + http_requests_total{code="500"}) / http_requests_total`, + shardingLabels: []string{model.MetricNameLabel}, + }, } for _, test := range nonShardable {