Skip to content

Commit 2171f64

Browse files
authored
fix: Fix panic on requesting out-of-order Pattern samples (#13010)
1 parent f6529c2 commit 2171f64

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pkg/pattern/drain/chunk.go

+8
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func (c Chunk) ForRange(start, end, step model.Time) []logproto.PatternSample {
6666
return c.Samples[i].Timestamp >= end
6767
})
6868
}
69+
70+
if c.Samples[lo].Timestamp > c.Samples[hi-1].Timestamp {
71+
return nil
72+
}
73+
6974
if step == TimeResolution {
7075
return c.Samples[lo:hi]
7176
}
@@ -110,6 +115,9 @@ func (c *Chunks) Add(ts model.Time) {
110115
*c = append(*c, newChunk(t))
111116
return
112117
}
118+
if ts.Before(last.Samples[len(last.Samples)-1].Timestamp) {
119+
return
120+
}
113121
last.Samples = append(last.Samples, logproto.PatternSample{
114122
Timestamp: t,
115123
Value: 1,

pkg/pattern/drain/chunk_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func TestAdd(t *testing.T) {
2121
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) + TimeResolution + 1)
2222
require.Equal(t, 2, len(cks))
2323
require.Equal(t, 1, len(cks[1].Samples))
24+
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) - TimeResolution)
25+
require.Equal(t, 2, len(cks))
26+
require.Equalf(t, 1, len(cks[1].Samples), "Older samples should not be added if they arrive out of order")
2427
}
2528

2629
func TestIterator(t *testing.T) {
@@ -52,6 +55,7 @@ func TestForRange(t *testing.T) {
5255
c *Chunk
5356
start model.Time
5457
end model.Time
58+
step model.Time
5559
expected []logproto.PatternSample
5660
}{
5761
{
@@ -180,6 +184,28 @@ func TestForRange(t *testing.T) {
180184
{Timestamp: 4, Value: 10},
181185
},
182186
},
187+
{
188+
name: "Out-of-order samples generate nil result",
189+
c: &Chunk{Samples: []logproto.PatternSample{
190+
{Timestamp: 5, Value: 2},
191+
{Timestamp: 3, Value: 2},
192+
}},
193+
start: 4,
194+
end: 6,
195+
expected: nil,
196+
},
197+
{
198+
name: "Internally out-of-order samples generate nil result",
199+
c: &Chunk{Samples: []logproto.PatternSample{
200+
{Timestamp: 1, Value: 2},
201+
{Timestamp: 5, Value: 2},
202+
{Timestamp: 3, Value: 2},
203+
{Timestamp: 7, Value: 2},
204+
}},
205+
start: 2,
206+
end: 6,
207+
expected: nil,
208+
},
183209
}
184210

185211
for _, tc := range testCases {

0 commit comments

Comments
 (0)