diff --git a/promql/engine.go b/promql/engine.go index 5fbc2dd1d..a5498bf56 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -752,7 +752,7 @@ func (ev *evaluator) vectorSelector(node *VectorSelector) Vector { t, v := it.Values() if !ok || t > refTime { - t, v, ok = it.PeekBack() + t, v, ok = it.PeekBack(1) if !ok || t < refTime-durationMilliseconds(StalenessDelta) { continue } diff --git a/storage/buffer.go b/storage/buffer.go index 01b4f2941..d3687d94f 100644 --- a/storage/buffer.go +++ b/storage/buffer.go @@ -36,10 +36,10 @@ func NewBuffer(it SeriesIterator, delta int64) *BufferedSeriesIterator { return bit } -// PeekBack returns the previous element of the iterator. If there is none buffered, +// PeekBack returns the nth previous element of the iterator. If there is none buffered, // ok is false. -func (b *BufferedSeriesIterator) PeekBack() (t int64, v float64, ok bool) { - return b.buf.last() +func (b *BufferedSeriesIterator) PeekBack(n int) (t int64, v float64, ok bool) { + return b.buf.nthLast(n) } // Buffer returns an iterator over the buffered data. @@ -189,13 +189,13 @@ func (r *sampleRing) add(t int64, v float64) { } } -// last returns the most recent element added to the ring. -func (r *sampleRing) last() (int64, float64, bool) { - if r.l == 0 { +// nthLast returns the nth most recent element added to the ring. +func (r *sampleRing) nthLast(n int) (int64, float64, bool) { + if n > r.l { return 0, 0, false } - s := r.buf[r.i] - return s.t, s.v, true + t, v := r.at(r.l - n) + return t, v, true } func (r *sampleRing) samples() []sample { diff --git a/web/federate.go b/web/federate.go index b69608bd2..80068e85e 100644 --- a/web/federate.go +++ b/web/federate.go @@ -94,7 +94,7 @@ func (h *Handler) federation(w http.ResponseWriter, req *http.Request) { if ok { t, v = it.Values() } else { - t, v, ok = it.PeekBack() + t, v, ok = it.PeekBack(0) if !ok { continue }