From adb7e59bb3ea342891287f5eb7ac37526ea32f90 Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Fri, 11 Nov 2022 09:59:03 -0800 Subject: [PATCH 1/2] update Thanos to get latest querysharding fix Signed-off-by: Ben Ye --- go.mod | 2 +- go.sum | 2 + .../tripperware/test_shard_by_query_utils.go | 7 ++- .../thanos/pkg/promclient/promclient.go | 23 ++++++++++ .../thanos/pkg/querysharding/analysis.go | 28 ++++++------ .../thanos/pkg/querysharding/analyzer.go | 43 +++---------------- vendor/modules.txt | 2 +- 7 files changed, 55 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index 926cdf911b4..468dba1bf3d 100644 --- a/go.mod +++ b/go.mod @@ -48,7 +48,7 @@ require ( github.com/spf13/afero v1.6.0 github.com/stretchr/testify v1.8.0 github.com/thanos-io/objstore v0.0.0-20221006135717-79dcec7fe604 - github.com/thanos-io/thanos v0.27.0-rc.0.0.20221102162214-50b41561b178 + github.com/thanos-io/thanos v0.29.1-0.20221111094505-b2badad930d6 github.com/uber/jaeger-client-go v2.30.0+incompatible github.com/weaveworks/common v0.0.0-20220706100410-67d27ed40fae go.etcd.io/etcd/api/v3 v3.5.4 diff --git a/go.sum b/go.sum index 1d5620dccb8..0323b849c7f 100644 --- a/go.sum +++ b/go.sum @@ -929,6 +929,8 @@ github.com/thanos-io/objstore v0.0.0-20221006135717-79dcec7fe604 h1:9dceDSKKsLWN github.com/thanos-io/objstore v0.0.0-20221006135717-79dcec7fe604/go.mod h1:Vx5dZs9ElxEhNLnum/OgB0pNTqNdI2zdXL82BeJr3T4= github.com/thanos-io/thanos v0.27.0-rc.0.0.20221102162214-50b41561b178 h1:rZTdKdZyyG+6Yi50mOkRmw7BFmZhdnFr/N+8o+Enadw= github.com/thanos-io/thanos v0.27.0-rc.0.0.20221102162214-50b41561b178/go.mod h1:odqdxSO+o/UaVgNpdkYYaQUW/JpT7LByXyZmxoe6uoc= +github.com/thanos-io/thanos v0.29.1-0.20221111094505-b2badad930d6 h1:8q0LB3XOhscrqEYU0g5+ekB1ZHERk8tUl1l/y9DbeXA= +github.com/thanos-io/thanos v0.29.1-0.20221111094505-b2badad930d6/go.mod h1:odqdxSO+o/UaVgNpdkYYaQUW/JpT7LByXyZmxoe6uoc= github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab h1:7ZR3hmisBWw77ZpO1/o86g+JV3VKlk3d48jopJxzTjU= github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab/go.mod h1:eheTFp954zcWZXCU8d0AT76ftsQOTo4DTqkN/h3k1MY= github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= diff --git a/pkg/querier/tripperware/test_shard_by_query_utils.go b/pkg/querier/tripperware/test_shard_by_query_utils.go index 18fad7cd7c3..0564cf22b81 100644 --- a/pkg/querier/tripperware/test_shard_by_query_utils.go +++ b/pkg/querier/tripperware/test_shard_by_query_utils.go @@ -140,7 +140,7 @@ sum by (container) ( { name: "multiple binary expressions with grouping", expression: `(http_requests_total{code="400"} + on (pod) http_requests_total{code="500"}) / on (cluster, pod) http_requests_total`, - shardingLabels: []string{"cluster", "pod"}, + shardingLabels: []string{"pod"}, }, { name: "histogram quantile", @@ -161,6 +161,11 @@ sum by (container) ( expression: "increase(sum(http_requests_total) by (pod, cluster) [1h:1m])", shardingLabels: []string{"cluster", "pod"}, }, + { + name: "ignore vector matching with 2 aggregations", + expression: `sum(rate(node_cpu_seconds_total[3h])) by (cluster_id, mode) / ignoring(mode) group_left sum(rate(node_cpu_seconds_total[3h])) by (cluster_id)`, + shardingLabels: []string{"cluster_id"}, + }, } shardableWithoutLabels := []queries{ diff --git a/vendor/github.com/thanos-io/thanos/pkg/promclient/promclient.go b/vendor/github.com/thanos-io/thanos/pkg/promclient/promclient.go index 22e48b41aae..748ebf189b4 100644 --- a/vendor/github.com/thanos-io/thanos/pkg/promclient/promclient.go +++ b/vendor/github.com/thanos-io/thanos/pkg/promclient/promclient.go @@ -780,6 +780,29 @@ func (c *Client) RulesInGRPC(ctx context.Context, base *url.URL, typeRules strin return m.Data.Groups, nil } +// AlertsInGRPC returns the rules from Prometheus alerts API. It uses gRPC errors. +// NOTE: This method is tested in pkg/store/prometheus_test.go against Prometheus. +func (c *Client) AlertsInGRPC(ctx context.Context, base *url.URL) ([]*rulespb.AlertInstance, error) { + u := *base + u.Path = path.Join(u.Path, "/api/v1/alerts") + + var m struct { + Data struct { + Alerts []*rulespb.AlertInstance `json:"alerts"` + } `json:"data"` + } + + if err := c.get2xxResultWithGRPCErrors(ctx, "/prom_alerts HTTP[client]", &u, &m); err != nil { + return nil, err + } + + // Prometheus does not support PartialResponseStrategy, and probably would never do. Make it Abort by default. + for _, g := range m.Data.Alerts { + g.PartialResponseStrategy = storepb.PartialResponseStrategy_ABORT + } + return m.Data.Alerts, nil +} + // MetricMetadataInGRPC returns the metadata from Prometheus metric metadata API. It uses gRPC errors. func (c *Client) MetricMetadataInGRPC(ctx context.Context, base *url.URL, metric string, limit int) (map[string][]metadatapb.Meta, error) { u := *base diff --git a/vendor/github.com/thanos-io/thanos/pkg/querysharding/analysis.go b/vendor/github.com/thanos-io/thanos/pkg/querysharding/analysis.go index d46a0048c60..c06215fa397 100644 --- a/vendor/github.com/thanos-io/thanos/pkg/querysharding/analysis.go +++ b/vendor/github.com/thanos-io/thanos/pkg/querysharding/analysis.go @@ -20,15 +20,6 @@ func nonShardableQuery() QueryAnalysis { } } -func newShardableByLabels(labels []string, by bool) QueryAnalysis { - labels = without(labels, excludedLabels) - - return QueryAnalysis{ - shardBy: by, - shardingLabels: labels, - } -} - func (q *QueryAnalysis) scopeToLabels(labels []string, by bool) QueryAnalysis { labels = without(labels, excludedLabels) @@ -39,16 +30,29 @@ func (q *QueryAnalysis) scopeToLabels(labels []string, by bool) QueryAnalysis { } } - if by { + if q.shardBy && by { return QueryAnalysis{ shardBy: true, shardingLabels: intersect(q.shardingLabels, labels), } } + if !q.shardBy && !by { + return QueryAnalysis{ + shardBy: false, + shardingLabels: union(q.shardingLabels, labels), + } + } + + // If we are sharding by and without the same time, + // keep the sharding by labels that are not in the without labels set. + labelsBy, labelsWithout := q.shardingLabels, labels + if !q.shardBy { + labelsBy, labelsWithout = labelsWithout, labelsBy + } return QueryAnalysis{ - shardBy: false, - shardingLabels: union(q.shardingLabels, labels), + shardBy: true, + shardingLabels: without(labelsBy, labelsWithout), } } diff --git a/vendor/github.com/thanos-io/thanos/pkg/querysharding/analyzer.go b/vendor/github.com/thanos-io/thanos/pkg/querysharding/analyzer.go index 70bb113a8b0..1009af25751 100644 --- a/vendor/github.com/thanos-io/thanos/pkg/querysharding/analyzer.go +++ b/vendor/github.com/thanos-io/thanos/pkg/querysharding/analyzer.go @@ -31,16 +31,14 @@ var nonShardableFuncs = []string{ } // NewQueryAnalyzer creates a new QueryAnalyzer. -func NewQueryAnalyzer() (*CachedQueryAnalyzer, error) { - cache, err := lru.New(256) - if err != nil { - return nil, err - } - +func NewQueryAnalyzer() *CachedQueryAnalyzer { + // Ignore the error check since it throws error + // only if size is <= 0. + cache, _ := lru.New(256) return &CachedQueryAnalyzer{ analyzer: &QueryAnalyzer{}, cache: cache, - }, nil + } } type cachedValue struct { @@ -70,10 +68,7 @@ func (a *CachedQueryAnalyzer) Analyze(query string) (QueryAnalysis, error) { // Analyze 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, -// then treat the query as shardable by those labels. -// - if the query's root expression has no grouping labels, -// then walk the query and find the least common labelset +// - Walk the query and find the least common labelset // used in grouping expressions. If non-empty, treat the query // as shardable by those labels. // - otherwise, treat the query as non-shardable. @@ -117,35 +112,9 @@ func (a *QueryAnalyzer) Analyze(query string) (QueryAnalysis, error) { return nonShardableQuery(), nil } - rootAnalysis := analyzeRootExpression(expr) - if rootAnalysis.IsShardable() && rootAnalysis.shardBy { - return rootAnalysis, nil - } - return analysis, nil } -func analyzeRootExpression(node parser.Node) QueryAnalysis { - switch n := node.(type) { - case *parser.BinaryExpr: - if n.VectorMatching != nil && n.VectorMatching.On { - shardingLabels := without(n.VectorMatching.MatchingLabels, []string{"le"}) - return newShardableByLabels(shardingLabels, n.VectorMatching.On) - } else { - return nonShardableQuery() - } - case *parser.AggregateExpr: - if len(n.Grouping) == 0 { - return nonShardableQuery() - } - - shardingLabels := without(n.Grouping, []string{"le"}) - return newShardableByLabels(shardingLabels, !n.Without) - } - - return nonShardableQuery() -} - func contains(needle string, haystack []string) bool { for _, item := range haystack { if needle == item { diff --git a/vendor/modules.txt b/vendor/modules.txt index 1470ef0f7e1..3d1baf6e572 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -775,7 +775,7 @@ github.com/thanos-io/objstore/providers/gcs github.com/thanos-io/objstore/providers/s3 github.com/thanos-io/objstore/providers/swift github.com/thanos-io/objstore/tracing -# github.com/thanos-io/thanos v0.27.0-rc.0.0.20221102162214-50b41561b178 +# github.com/thanos-io/thanos v0.29.1-0.20221111094505-b2badad930d6 ## explicit; go 1.18 github.com/thanos-io/thanos/pkg/block github.com/thanos-io/thanos/pkg/block/indexheader From ee43ab866f610f59fcb5fad03e14dc1207ebd827 Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Fri, 11 Nov 2022 10:11:23 -0800 Subject: [PATCH 2/2] update modules Signed-off-by: Ben Ye --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 0323b849c7f..700ca3adf0e 100644 --- a/go.sum +++ b/go.sum @@ -927,8 +927,6 @@ github.com/thanos-community/galaxycache v0.0.0-20211122094458-3a32041a1f1e h1:f1 github.com/thanos-community/galaxycache v0.0.0-20211122094458-3a32041a1f1e/go.mod h1:jXcofnrSln/cLI6/dhlBxPQZEEQHVPCcFaH75M+nSzM= github.com/thanos-io/objstore v0.0.0-20221006135717-79dcec7fe604 h1:9dceDSKKsLWNHjrMpyzK1t7eVcAZv9Dp3FX+uokUS2Y= github.com/thanos-io/objstore v0.0.0-20221006135717-79dcec7fe604/go.mod h1:Vx5dZs9ElxEhNLnum/OgB0pNTqNdI2zdXL82BeJr3T4= -github.com/thanos-io/thanos v0.27.0-rc.0.0.20221102162214-50b41561b178 h1:rZTdKdZyyG+6Yi50mOkRmw7BFmZhdnFr/N+8o+Enadw= -github.com/thanos-io/thanos v0.27.0-rc.0.0.20221102162214-50b41561b178/go.mod h1:odqdxSO+o/UaVgNpdkYYaQUW/JpT7LByXyZmxoe6uoc= github.com/thanos-io/thanos v0.29.1-0.20221111094505-b2badad930d6 h1:8q0LB3XOhscrqEYU0g5+ekB1ZHERk8tUl1l/y9DbeXA= github.com/thanos-io/thanos v0.29.1-0.20221111094505-b2badad930d6/go.mod h1:odqdxSO+o/UaVgNpdkYYaQUW/JpT7LByXyZmxoe6uoc= github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab h1:7ZR3hmisBWw77ZpO1/o86g+JV3VKlk3d48jopJxzTjU=