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(query engine): Include lines with ts equal to end timestamp of the query range when executing range aggregations #13448

Merged
merged 3 commits into from
Jul 11, 2024
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
28 changes: 6 additions & 22 deletions pkg/logql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestEngine_LogsRateUnwrap(t *testing.T) {
}
}

func TestEngine_LogsInstantQuery(t *testing.T) {
func TestEngine_InstantQuery(t *testing.T) {
t.Parallel()
for _, test := range []struct {
qs string
Expand All @@ -182,26 +182,6 @@ func TestEngine_LogsInstantQuery(t *testing.T) {

expected interface{}
}{
{
`{app="foo"}`, time.Unix(30, 0), logproto.FORWARD, 10,
[][]logproto.Stream{
{newStream(testSize, identity, `{app="foo"}`)},
},
[]SelectLogParams{
{&logproto.QueryRequest{Direction: logproto.FORWARD, Start: time.Unix(0, 0), End: time.Unix(30, 0), Limit: 10, Selector: `{app="foo"}`}},
},
logqlmodel.Streams([]logproto.Stream{newStream(10, identity, `{app="foo"}`)}),
},
{
`{app="bar"} |= "foo" |~ ".+bar"`, time.Unix(30, 0), logproto.BACKWARD, 30,
[][]logproto.Stream{
{newStream(testSize, identity, `{app="bar"}`)},
},
[]SelectLogParams{
{&logproto.QueryRequest{Direction: logproto.BACKWARD, Start: time.Unix(0, 0), End: time.Unix(30, 0), Limit: 30, Selector: `{app="bar"}|="foo"|~".+bar"`}},
},
logqlmodel.Streams([]logproto.Stream{newStream(30, identity, `{app="bar"}`)}),
},
{
`rate({app="foo"} |~".+bar" [1m])`, time.Unix(60, 0), logproto.BACKWARD, 10,
[][]logproto.Series{
Expand Down Expand Up @@ -975,7 +955,6 @@ func TestEngine_LogsInstantQuery(t *testing.T) {
} {
test := test
t.Run(fmt.Sprintf("%s %s", test.qs, test.direction), func(t *testing.T) {
t.Parallel()

eng := NewEngine(EngineOpts{}, newQuerierRecorder(t, test.data, test.params), NoLimits, log.NewNopLogger())

Expand Down Expand Up @@ -2755,6 +2734,11 @@ func (q *querierRecorder) SelectSamples(_ context.Context, p SelectSampleParams)
}

func paramsID(p interface{}) string {
switch params := p.(type) {
case SelectLogParams:
case SelectSampleParams:
return fmt.Sprintf("%d", params.Plan.Hash())
}
b, err := json.Marshal(p)
if err != nil {
panic(err)
Expand Down
18 changes: 12 additions & 6 deletions pkg/logql/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,12 @@ func (ev *DefaultEvaluator) NewStepEvaluator(
nextEvFactory = SampleEvaluatorFunc(func(ctx context.Context, _ SampleEvaluatorFactory, _ syntax.SampleExpr, _ Params) (StepEvaluator, error) {
it, err := ev.querier.SelectSamples(ctx, SelectSampleParams{
&logproto.SampleQueryRequest{
Start: q.Start().Add(-rangExpr.Left.Interval).Add(-rangExpr.Left.Offset),
End: q.End().Add(-rangExpr.Left.Offset),
Selector: e.String(), // intentionally send the vector for reducing labels.
// extend startTs backwards by step
Start: q.Start().Add(-rangExpr.Left.Interval).Add(-rangExpr.Left.Offset),
// add leap nanosecond to endTs to include lines exactly at endTs. range iterators work on start exclusive, end inclusive ranges
End: q.End().Add(-rangExpr.Left.Offset).Add(time.Nanosecond),
// intentionally send the vector for reducing labels.
Selector: e.String(),
Shards: q.Shards(),
Plan: &plan.QueryPlan{
AST: expr,
Expand All @@ -351,9 +354,12 @@ func (ev *DefaultEvaluator) NewStepEvaluator(
case *syntax.RangeAggregationExpr:
it, err := ev.querier.SelectSamples(ctx, SelectSampleParams{
&logproto.SampleQueryRequest{
Start: q.Start().Add(-e.Left.Interval).Add(-e.Left.Offset),
End: q.End().Add(-e.Left.Offset),
Selector: expr.String(),
// extend startTs backwards by step
Start: q.Start().Add(-e.Left.Interval).Add(-e.Left.Offset),
// add leap nanosecond to endTs to include lines exactly at endTs. range iterators work on start exclusive, end inclusive ranges
End: q.End().Add(-e.Left.Offset).Add(time.Nanosecond),
// intentionally send the vector for reducing labels.
Selector: e.String(),
Shards: q.Shards(),
Plan: &plan.QueryPlan{
AST: expr,
Expand Down
13 changes: 9 additions & 4 deletions pkg/storage/stores/shipper/indexshipper/tsdb/bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/prometheus/common/model"
)

// TODO(chaudum): Replace with new v1.Interval struct
type Bounded interface {
Bounds() (model.Time, model.Time)
}
Expand Down Expand Up @@ -34,9 +35,13 @@ func newBounds(mint, maxt model.Time) bounds { return bounds{mint: mint, maxt: m

func (b bounds) Bounds() (model.Time, model.Time) { return b.mint, b.maxt }

func Overlap(a, b Bounded) bool {
aFrom, aThrough := a.Bounds()
bFrom, bThrough := b.Bounds()
// Overlap checks whether the given chunk or index bounds
// overlap with the bounds of a query range.
// chunk/index bounds are defined as [from, through]
// query bounds are defined as [from, through)
func Overlap(chk, qry Bounded) bool {
chkFrom, chkThrough := chk.Bounds()
qryFrom, qryThrough := qry.Bounds()

return aFrom < bThrough && aThrough > bFrom
return chkFrom < qryThrough && chkThrough >= qryFrom
}
6 changes: 6 additions & 0 deletions pkg/storage/stores/shipper/indexshipper/tsdb/bounds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func TestOverlap(t *testing.T) {
// ensure [start,end) inclusivity works as expected
a: newBounds(1, 5),
b: newBounds(5, 6),
overlap: true,
},
{
// ensure [start,end) inclusivity works as expected
a: newBounds(5, 6),
b: newBounds(1, 5),
overlap: false,
},
} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (i *MultiIndex) forMatchingIndices(ctx context.Context, from, through model
queryBounds := newBounds(from, through)

return i.iter.For(ctx, i.maxParallel, func(ctx context.Context, idx Index) error {
if Overlap(queryBounds, idx) {
if Overlap(idx, queryBounds) {
Copy link
Contributor

Choose a reason for hiding this comment

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

q: wondering if any other places of Overlap usage need this swap of arguments as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked, but all other places already use the Overlap function with the correct argument order.


if i.filterer != nil {
// TODO(owen-d): Find a nicer way
Expand Down
Loading