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: Fix panic on requesting out-of-order Pattern samples #13010

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion pkg/pattern/drain/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c Chunk) ForRange(start, end, step model.Time) []logproto.PatternSample {
}
first := c.Samples[0].Timestamp
last := c.Samples[len(c.Samples)-1].Timestamp
if start >= end || first >= end || last < start {
if last < first || start >= end || first >= end || last < start {
Copy link
Contributor

Choose a reason for hiding this comment

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

this might not cover all the cases since the in-between samples could be out-of-order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point - I'll add some more test cases

return nil
}
var lo int
Expand Down Expand Up @@ -114,6 +114,11 @@ func (c *Chunks) Add(ts model.Time) {
Timestamp: t,
Value: 1,
})
if len(last.Samples) > 1 && last.Samples[len(last.Samples)-2].Timestamp > t {
sort.SliceStable(last.Samples, func(i, j int) bool {
return last.Samples[i].Timestamp < last.Samples[j].Timestamp
})
}
}

func (c Chunks) Iterator(pattern string, from, through, step model.Time) iter.Iterator {
Expand Down
15 changes: 15 additions & 0 deletions pkg/pattern/drain/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func TestAdd(t *testing.T) {
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) + TimeResolution + 1)
require.Equal(t, 2, len(cks))
require.Equal(t, 1, len(cks[1].Samples))
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) - TimeResolution)
require.Equal(t, 2, len(cks))
require.Equal(t, 2, len(cks[1].Samples))
require.Lessf(t, cks[1].Samples[0].Timestamp, cks[1].Samples[1].Timestamp, "Samples should be sorted if inserted out of order")
}

func TestIterator(t *testing.T) {
Expand Down Expand Up @@ -52,6 +56,7 @@ func TestForRange(t *testing.T) {
c *Chunk
start model.Time
end model.Time
step model.Time
expected []logproto.PatternSample
}{
{
Expand Down Expand Up @@ -180,6 +185,16 @@ func TestForRange(t *testing.T) {
{Timestamp: 4, Value: 10},
},
},
{
name: "Out-of-order samples generate nil result",
c: &Chunk{Samples: []logproto.PatternSample{
{Timestamp: 5, Value: 2},
{Timestamp: 3, Value: 2},
}},
start: 4,
end: 6,
expected: nil,
},
}

for _, tc := range testCases {
Expand Down
Loading