Merge pull request #10030 from prometheus/beorn7/iterators

storage: expose and fix bug #10027 in iterators' Seek method
This commit is contained in:
Björn Rabenstein 2021-12-16 14:35:40 +01:00 committed by GitHub
commit 565ee356df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 0 deletions

View File

@ -360,6 +360,9 @@ func (c *concreteSeriesIterator) Seek(t int64) bool {
if c.cur == -1 { if c.cur == -1 {
c.cur = 0 c.cur = 0
} }
if c.cur >= len(c.series.samples) {
return false
}
// No-op check. // No-op check.
if s := c.series.samples[c.cur]; s.Timestamp >= t { if s := c.series.samples[c.cur]; s.Timestamp >= t {
return true return true

View File

@ -233,6 +233,11 @@ func TestConcreteSeriesIterator(t *testing.T) {
ts, v = it.At() ts, v = it.At()
require.Equal(t, int64(3), ts) require.Equal(t, int64(3), ts)
require.Equal(t, 3., v) require.Equal(t, 3., v)
// Seek beyond the end.
require.False(t, it.Seek(5))
// And we don't go back. (This exposes issue #10027.)
require.False(t, it.Seek(2))
} }
func TestFromQueryResultWithDuplicates(t *testing.T) { func TestFromQueryResultWithDuplicates(t *testing.T) {

View File

@ -99,6 +99,9 @@ func (it *listSeriesIterator) Seek(t int64) bool {
if it.idx == -1 { if it.idx == -1 {
it.idx = 0 it.idx = 0
} }
if it.idx >= it.samples.Len() {
return false
}
// No-op check. // No-op check.
if s := it.samples.Get(it.idx); s.T() >= t { if s := it.samples.Get(it.idx); s.T() >= t {
return true return true

View File

@ -51,4 +51,9 @@ func TestListSeriesIterator(t *testing.T) {
ts, v = it.At() ts, v = it.At()
require.Equal(t, int64(3), ts) require.Equal(t, int64(3), ts)
require.Equal(t, 3., v) require.Equal(t, 3., v)
// Seek beyond the end.
require.False(t, it.Seek(5))
// And we don't go back. (This exposes issue #10027.)
require.False(t, it.Seek(2))
} }

View File

@ -159,6 +159,9 @@ func (it *listSeriesIterator) Seek(t int64) bool {
if it.idx == -1 { if it.idx == -1 {
it.idx = 0 it.idx = 0
} }
if it.idx >= len(it.list) {
return false
}
// No-op check. // No-op check.
if s := it.list[it.idx]; s.T() >= t { if s := it.list[it.idx]; s.T() >= t {
return true return true