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

Fix vertical sharding bug in without and union operations #6592

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#6519](https://github.com/thanos-io/thanos/pull/6519) Reloader: Use timeout for initial apply.
- [#6509](https://github.com/thanos-io/thanos/pull/6509) Store Gateway: Remove `memWriter` from `fileWriter` to reduce memory usage when sync index headers.
- [#6556](https://github.com/thanos-io/thanos/pull/6556) Thanos compact: respect block-files-concurrency setting when downsampling
- [#6592](https://github.com/thanos-io/thanos/pull/6592) Query Frontend: fix bugs in vertical sharding `without` and `union` function to allow more queries to be shardable.

### Changed
- [#6049](https://github.com/thanos-io/thanos/pull/6049) Compact: *breaking :warning:* Replace group with resolution in compact metrics to avoid cardinality explosion on compact metrics for large numbers of groups.
Expand Down
14 changes: 11 additions & 3 deletions pkg/querysharding/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ func without(sliceA, sliceB []string) []string {
if sliceA == nil {
return nil
}

if len(sliceA) == 0 || len(sliceB) == 0 {
if len(sliceA) == 0 {
return []string{}
}
if len(sliceB) == 0 {
return sliceA
}

keyMap := make(map[string]struct{}, len(sliceA))
for _, s := range sliceA {
Expand All @@ -123,9 +125,15 @@ func without(sliceA, sliceB []string) []string {
}

func union(sliceA, sliceB []string) []string {
if len(sliceA) == 0 || len(sliceB) == 0 {
if len(sliceA) == 0 && len(sliceB) == 0 {
return []string{}
}
if len(sliceA) == 0 {
return sliceB
}
if len(sliceB) == 0 {
return sliceA
}

keyMap := make(map[string]struct{}, len(sliceA))
for _, s := range sliceA {
Expand Down
25 changes: 25 additions & 0 deletions pkg/querysharding/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ http_requests_total`,
expression: `sum by (cluster, pod) (http_requests_total{code="400"}) / on (pod) sum by (cluster, pod) (http_requests_total)`,
shardingLabels: []string{"pod"},
},
{
name: "binary expression with vector matching with outer aggregation",
expression: `sum(http_requests_total{code="400"} * http_requests_total) by (pod)`,
shardingLabels: []string{"pod"},
},
{
name: "multiple binary expressions with vector matchers",
expression: `
Expand Down Expand Up @@ -180,6 +185,16 @@ sum by (container) (
expression: `sum(sum_over_time(container_memory_working_set_bytes{container_name!="POD",container_name!="",namespace="kube-system"}[1d:5m])) by (instance, cluster) / avg(label_replace(sum(sum_over_time(kube_node_status_capacity_memory_bytes[1d:5m])) by (node, cluster), "instance", "$1", "node", "(.*)")) by (instance, cluster)`,
shardingLabels: []string{"cluster"},
},
{
name: "complex query with label_replace and nested aggregations",
expression: `avg(label_replace(label_replace(avg(count_over_time(kube_pod_container_resource_requests{resource="memory", unit="byte", container!="",container!="POD", node!="", }[1h] )*avg_over_time(kube_pod_container_resource_requests{resource="memory", unit="byte", container!="",container!="POD", node!="", }[1h] )) by (namespace,container,pod,node,cluster_id) , "container_name","$1","container","(.+)"), "pod_name","$1","pod","(.+)")) by (namespace,container_name,pod_name,node,cluster_id)`,
shardingLabels: []string{"namespace", "node", "cluster_id"},
},
{
name: "complex query with label_replace, nested aggregations and binary expressions",
expression: `sort_desc(avg(label_replace(label_replace(label_replace(count_over_time(container_memory_working_set_bytes{container!="", container!="POD", instance!="", }[1h] ), "node", "$1", "instance", "(.+)"), "container_name", "$1", "container", "(.+)"), "pod_name", "$1", "pod", "(.+)")*label_replace(label_replace(label_replace(avg_over_time(container_memory_working_set_bytes{container!="", container!="POD", instance!="", }[1h] ), "node", "$1", "instance", "(.+)"), "container_name", "$1", "container", "(.+)"), "pod_name", "$1", "pod", "(.+)")) by (namespace, container_name, pod_name, node, cluster_id))`,
shardingLabels: []string{"namespace", "cluster_id"},
},
}

shardableWithoutLabels := []testCase{
Expand All @@ -193,6 +208,16 @@ sum by (container) (
expression: "max without (pod) (sum without (pod, cluster) (http_requests_total))",
shardingLabels: []string{"pod", "cluster"},
},
{
name: "binary expression with outer without grouping",
expression: `sum(http_requests_total{code="400"} * http_requests_total) without (pod)`,
shardingLabels: []string{"pod"},
},
{
name: "binary expression with vector matching and outer without grouping",
expression: `sum(http_requests_total{code="400"} * ignoring(cluster) http_requests_total) without ()`,
shardingLabels: []string{"__name__", "cluster"},
},
{
name: "binary expression with without vector matching and grouping",
expression: `sum without (cluster, pod) (http_requests_total{code="400"}) / ignoring (pod) sum without (cluster, pod) (http_requests_total)`,
Expand Down