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(approx_topk): Map approx_topk operation in all cases #16131

Merged
merged 4 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 28 additions & 0 deletions pkg/logql/shardmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,34 @@ func (m ShardMapper) mapVectorAggregationExpr(expr *syntax.VectorAggregationExpr
}
return expr, exprStats.Bytes, nil
}
} else {
// if this AST contains unshardable operations, we still need to rewrite some operations (e.g. approx_topk) as if it had 0 shards as they are not supported on the querier
level.Error(util_log.Logger).Log(
"msg", "unshardable operation which is not supported by downstream querier",
"operation", expr.Operation,
)
switch expr.Operation {
case syntax.OpTypeApproxTopK:
_, bytesPerShard, err := m.shards.Resolver().Shards(expr)
if err != nil {
return nil, 0, err
}

countMinSketchExpr := syntax.MustClone(expr)
countMinSketchExpr.Operation = syntax.OpTypeCountMinSketch
countMinSketchExpr.Params = 0

return &syntax.VectorAggregationExpr{
Left: &CountMinSketchEvalExpr{
downstreams: []DownstreamSampleExpr{{
SampleExpr: countMinSketchExpr,
}},
},
Grouping: expr.Grouping,
Operation: syntax.OpTypeTopK,
Params: expr.Params,
}, bytesPerShard, nil
}
benclive marked this conversation as resolved.
Show resolved Hide resolved
}

// if this AST contains unshardable operations, don't shard this at this level,
Expand Down
145 changes: 144 additions & 1 deletion pkg/logql/shardmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func TestMappingStrings(t *testing.T) {
sum by (foo_extracted) (
downstream<sumby(foo_extracted)(quantile_over_time(0.95,{foo="baz"}|logfmt|unwrapfoo_extracted|__error__=""[5m])),shard=0_of_2>++downstream<sumby(foo_extracted)(quantile_over_time(0.95,{foo="baz"}|logfmt|unwrapfoo_extracted|__error__=""[5m])),shard=1_of_2>
)
/
/
sum by (foo_extracted) (
downstream<countby(foo_extracted)(quantile_over_time(0.95,{foo="baz"}|logfmt|unwrapfoo_extracted|__error__=""[5m])),shard=0_of_2>++downstream<countby(foo_extracted)(quantile_over_time(0.95,{foo="baz"}|logfmt|unwrapfoo_extracted|__error__=""[5m])),shard=1_of_2>
)
Expand Down Expand Up @@ -652,6 +652,52 @@ func TestMapping(t *testing.T) {
},
},
},
{
in: `rate({foo="bar"}[5m]) > 3`,
expr: &syntax.BinOpExpr{
Op: syntax.OpTypeGT,
SampleExpr: &ConcatSampleExpr{
DownstreamSampleExpr: DownstreamSampleExpr{
shard: NewPowerOfTwoShard(index.ShardAnnotation{
Shard: 0,
Of: 2,
}).Bind(nil),
SampleExpr: &syntax.RangeAggregationExpr{
Operation: syntax.OpRangeTypeRate,
Left: &syntax.LogRangeExpr{
Left: &syntax.MatchersExpr{
Mts: []*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")},
},
Interval: 5 * time.Minute,
},
},
},
next: &ConcatSampleExpr{
DownstreamSampleExpr: DownstreamSampleExpr{
shard: NewPowerOfTwoShard(index.ShardAnnotation{
Shard: 1,
Of: 2,
}).Bind(nil),
SampleExpr: &syntax.RangeAggregationExpr{
Operation: syntax.OpRangeTypeRate,
Left: &syntax.LogRangeExpr{
Left: &syntax.MatchersExpr{
Mts: []*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")},
},
Interval: 5 * time.Minute,
},
},
},
next: nil,
},
},
RHS: &syntax.LiteralExpr{Val: 3},
Opts: &syntax.BinOpOptions{
ReturnBool: false,
VectorMatching: &syntax.VectorMatching{},
},
},
},
{
in: `count_over_time({foo="bar"}[5m])`,
expr: &ConcatSampleExpr{
Expand Down Expand Up @@ -833,6 +879,103 @@ func TestMapping(t *testing.T) {
},
},
},
{
in: `approx_topk(3, rate({foo="bar"}[5m]) > 3)`,
expr: &syntax.VectorAggregationExpr{
Grouping: &syntax.Grouping{},
Params: 3,
Operation: syntax.OpTypeTopK,
Left: &CountMinSketchEvalExpr{
downstreams: []DownstreamSampleExpr{
{
shard: NewPowerOfTwoShard(index.ShardAnnotation{
Shard: 0,
Of: 2,
}).Bind(nil),
SampleExpr: &syntax.VectorAggregationExpr{
Operation: syntax.OpTypeCountMinSketch,
Left: &syntax.BinOpExpr{
Op: syntax.OpTypeGT,
SampleExpr: &syntax.RangeAggregationExpr{
Operation: syntax.OpRangeTypeRate,
Left: &syntax.LogRangeExpr{
Left: &syntax.MatchersExpr{
Mts: []*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")},
},
Interval: 5 * time.Minute,
},
},
RHS: &syntax.LiteralExpr{Val: 3},
Opts: &syntax.BinOpOptions{
ReturnBool: false,
VectorMatching: &syntax.VectorMatching{},
},
},
Grouping: &syntax.Grouping{},
},
},
{
shard: NewPowerOfTwoShard(index.ShardAnnotation{
Shard: 1,
Of: 2,
}).Bind(nil),
SampleExpr: &syntax.VectorAggregationExpr{
Operation: syntax.OpTypeCountMinSketch,
Left: &syntax.BinOpExpr{
Op: syntax.OpTypeGT,
SampleExpr: &syntax.RangeAggregationExpr{
Operation: syntax.OpRangeTypeRate,
Left: &syntax.LogRangeExpr{
Left: &syntax.MatchersExpr{
Mts: []*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")},
},
Interval: 5 * time.Minute,
},
},
RHS: &syntax.LiteralExpr{Val: 3},
Opts: &syntax.BinOpOptions{
ReturnBool: false,
VectorMatching: &syntax.VectorMatching{},
},
},
Grouping: &syntax.Grouping{},
},
},
},
},
},
},
{
// A contrived query that is not shardable: but we must rewrite `approx_topk` as if it had 0 shards because the approx_topk operation is not supported on the querier
in: `approx_topk(3, topk(5, rate({foo="bar"}[5m])))`,
expr: &syntax.VectorAggregationExpr{
Left: &CountMinSketchEvalExpr{
downstreams: []DownstreamSampleExpr{{
SampleExpr: &syntax.VectorAggregationExpr{
Operation: syntax.OpTypeCountMinSketch,
Left: &syntax.VectorAggregationExpr{Operation: syntax.OpTypeTopK,
Params: 5,
Left: &syntax.RangeAggregationExpr{
Operation: syntax.OpRangeTypeRate,
Left: &syntax.LogRangeExpr{
Left: &syntax.MatchersExpr{
Mts: []*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")},
},
Interval: 5 * time.Minute,
},
},
Grouping: &syntax.Grouping{},
},
Grouping: &syntax.Grouping{},
},
shard: nil,
}},
},
Grouping: &syntax.Grouping{},
Operation: syntax.OpTypeTopK,
Params: 3,
},
},
{
in: `count(rate({foo="bar"}[5m]))`,
expr: &syntax.VectorAggregationExpr{
Expand Down
6 changes: 6 additions & 0 deletions pkg/logql/syntax/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,12 @@ var shardableOps = map[string]bool{
// binops - arith
OpTypeAdd: true,
OpTypeMul: true,

// binops - comparison
OpTypeGT: true,
OpTypeGTE: true,
OpTypeLT: true,
OpTypeLTE: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure these are shardable? Is this an orthogonal change to the above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, we should treat them the same way as the arithmetic operations above. There's no special logic for those operations, they just get mapped to queriers and combined as usual.
I don't think sharding a approx_topk query containing a comparison will change the result, as far as I can see, though I don't have any proof beyond the existing tests that it is valid to do so.
If you prefer I can remove this change and those types of queries will fall into the "not shardable" logic instead. I've tested it locally and it does seem to give correct results - WDYT?

Copy link
Contributor

@jeschkies jeschkies Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, so I'd push the operator don to each shard because it's filtering out the metrics. Does that makes sense? As in:

count_over_timer({...}[...]) > 0

should be

downstream<count_over_timer({..., __shard__=0}[...]) > 0>
++ ...
++ downstream<count_over_timer({..., __shard__=n}[...]) > 0>

and not

(downstream<count_over_timer({..., __shard__=0}[...])> 
 ++ ...
 ++ downstream<count_over_timer({..., __shard__=n}[...])>
) > 0

I think sharding these is a little more evolved and should be a separarte PR.

}

type MatcherRange struct {
Expand Down