From 4e7db57e76a98060d4a06e8f9d1c50639db5e9d3 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Sat, 16 Mar 2013 01:26:56 -0700 Subject: [PATCH] Fix iterator behavior in view.GetSampleAtTime() --- storage/metric/view.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/storage/metric/view.go b/storage/metric/view.go index f4c4380be..224eb959c 100644 --- a/storage/metric/view.go +++ b/storage/metric/view.go @@ -126,18 +126,22 @@ func (v view) Close() { } func (v view) GetValueAtTime(f model.Fingerprint, t time.Time) (s []model.SamplePair) { - var ( - series, ok = v.fingerprintToSeries[f] - ) + series, ok := v.fingerprintToSeries[f] if !ok { return } - var ( - iterator = series.values.Seek(skipListTime(t)) - ) + iterator := series.values.Seek(skipListTime(t)) if iterator == nil { - return + // If the iterator is nil, it means we seeked past the end of the series, + // so we seek to the last value instead. Due to the reverse ordering + // defined on skipListTime, this corresponds to the sample with the + // earliest timestamp. + iterator = series.values.SeekToLast() + if iterator == nil { + // The list is empty. + return + } } defer iterator.Close()