Merge pull request #97 from Gouthamve/nit

Fix bug with Seek() and optimise bounding params.
This commit is contained in:
Fabian Reinartz 2017-06-13 10:11:10 +02:00 committed by GitHub
commit a2948f3c5f
2 changed files with 27 additions and 7 deletions

View file

@ -661,10 +661,6 @@ func newChunkSeriesIterator(cs []*ChunkMeta, dranges intervals, mint, maxt int64
} }
} }
func (it *chunkSeriesIterator) inBounds(t int64) bool {
return t >= it.mint && t <= it.maxt
}
func (it *chunkSeriesIterator) Seek(t int64) (ok bool) { func (it *chunkSeriesIterator) Seek(t int64) (ok bool) {
if t > it.maxt { if t > it.maxt {
return false return false
@ -677,7 +673,9 @@ func (it *chunkSeriesIterator) Seek(t int64) (ok bool) {
// Only do binary search forward to stay in line with other iterators // Only do binary search forward to stay in line with other iterators
// that can only move forward. // that can only move forward.
x := sort.Search(len(it.chunks[it.i:]), func(i int) bool { return it.chunks[i].MinTime >= t }) x := sort.Search(len(it.chunks[it.i:]), func(i int) bool {
return it.chunks[it.i+i].MinTime >= t
})
x += it.i x += it.i
// If the timestamp was not found, it might be in the last chunk. // If the timestamp was not found, it might be in the last chunk.
@ -712,9 +710,15 @@ func (it *chunkSeriesIterator) At() (t int64, v float64) {
func (it *chunkSeriesIterator) Next() bool { func (it *chunkSeriesIterator) Next() bool {
for it.cur.Next() { for it.cur.Next() {
t, _ := it.cur.At() t, _ := it.cur.At()
if it.inBounds(t) { if t < it.mint {
return true return it.Seek(it.mint)
} }
if t > it.maxt {
return false
}
return true
} }
if err := it.cur.Err(); err != nil { if err := it.cur.Err(); err != nil {

View file

@ -1027,6 +1027,22 @@ func TestSeriesIterator(t *testing.T) {
return return
} }
// Regression for: https://github.com/prometheus/tsdb/pull/97
func TestCSIteratorDoubleSeek(t *testing.T) {
chkMetas := []*ChunkMeta{
chunkFromSamples([]sample{}),
chunkFromSamples([]sample{{1, 1}, {2, 2}, {3, 3}}),
chunkFromSamples([]sample{{4, 4}, {5, 5}}),
}
res := newChunkSeriesIterator(chkMetas, nil, 2, 8)
require.True(t, res.Seek(1))
require.True(t, res.Seek(2))
ts, v := res.At()
require.Equal(t, int64(2), ts)
require.Equal(t, float64(2), v)
}
func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) { func TestPopulatedCSReturnsValidChunkSlice(t *testing.T) {
lbls := []labels.Labels{labels.New(labels.Label{"a", "b"})} lbls := []labels.Labels{labels.New(labels.Label{"a", "b"})}
chunkMetas := [][]*ChunkMeta{ chunkMetas := [][]*ChunkMeta{