| 
									
										
										
										
											2017-04-19 20:43:09 +08:00
										 |  |  | // Copyright 2017 The Prometheus Authors
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | package storage | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"math/rand" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2021-11-18 02:57:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
  be added to it. I moved the new `pkg/histogram` package to `model`
  anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
  abbreviations, there were just too many names for it: SparseHistogram,
  Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
  general. Only add "Sparse" if it is needed to avoid confusion with
  conventional Histograms (which is rare because the TSDB really has no notion
  of conventional Histograms). Use abbreviations only in local scope, and then
  really abbreviate (not just removing three out of seven letters like in
  "Histo"). This is in the spirit of
  https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
  https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
  , but also layout question, anticipating how things will look like
  when rendered by `godoc` (even where `godoc` doesn't render them
  right now because they are for unexported types or not a doc comment
  at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
  leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
  method.
* HistogramChunk.iterator now allows iterator recycling. (I think
  @dieterbe only commented it out because he was confused by the
  question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
  staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
											
										 
											2021-10-09 21:57:07 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/model/histogram" | 
					
						
							| 
									
										
										
										
											2021-06-30 22:48:13 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/chunkenc" | 
					
						
							| 
									
										
										
										
											2023-04-14 17:59:30 +08:00
										 |  |  | 	"github.com/prometheus/prometheus/tsdb/tsdbutil" | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSampleRing(t *testing.T) { | 
					
						
							|  |  |  | 	cases := []struct { | 
					
						
							|  |  |  | 		input []int64 | 
					
						
							|  |  |  | 		delta int64 | 
					
						
							|  |  |  | 		size  int | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, | 
					
						
							|  |  |  | 			delta: 2, | 
					
						
							|  |  |  | 			size:  1, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, | 
					
						
							|  |  |  | 			delta: 2, | 
					
						
							|  |  |  | 			size:  2, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, | 
					
						
							|  |  |  | 			delta: 7, | 
					
						
							|  |  |  | 			size:  3, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: []int64{1, 2, 3, 4, 5, 16, 17, 18, 19, 20}, | 
					
						
							|  |  |  | 			delta: 7, | 
					
						
							|  |  |  | 			size:  1, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-03-12 21:16:59 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			input: []int64{1, 2, 3, 4, 6}, | 
					
						
							|  |  |  | 			delta: 4, | 
					
						
							|  |  |  | 			size:  4, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for _, c := range cases { | 
					
						
							| 
									
										
										
										
											2022-12-09 07:44:48 +08:00
										 |  |  | 		r := newSampleRing(c.delta, c.size, chunkenc.ValFloat) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 		input := []fSample{} | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 		for _, t := range c.input { | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 			input = append(input, fSample{ | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 				t: t, | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 				f: float64(rand.Intn(100)), | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for i, s := range input { | 
					
						
							| 
									
										
										
										
											2021-11-13 02:07:41 +08:00
										 |  |  | 			r.add(s) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 			buffered := r.samples() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for _, sold := range input[:i] { | 
					
						
							|  |  |  | 				found := false | 
					
						
							|  |  |  | 				for _, bs := range buffered { | 
					
						
							| 
									
										
										
										
											2023-03-31 01:50:13 +08:00
										 |  |  | 					if bs.T() == sold.t && bs.F() == sold.f { | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 						found = true | 
					
						
							|  |  |  | 						break | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 13:28:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if found { | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 					require.GreaterOrEqual(t, sold.t, s.t-c.delta, "%d: unexpected sample %d in buffer; buffer %v", i, sold.t, buffered) | 
					
						
							| 
									
										
										
										
											2019-10-02 13:28:08 +08:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 					require.Less(t, sold.t, s.t-c.delta, "%d: expected sample %d to be in buffer but was not; buffer %v", i, sold.t, buffered) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												storage: Fix mixed samples handling in sampleRing
Two issues are fixed here, that lead to the same problem:
1. If `newSampleRing` is called with an unknown ValueType including
   ValueNone, we have initialized the interface buffer (`iBuf`).
   However, we would still use a specialized buffer for the first
   sample, opportunistically assuming that we might still not
   encounter mixed samples and we should go down the more efficient
   road.
2. If the `sampleRing` is `reset`, we leave all buffers alone,
   including `iBuf`, which is generally fine, but not for `iBuf`, see
   below.
In both cases, `iBuf` already contains values, but we will fill one of
the specialized buffers first. Once we then actually encounter mixed
samples, the content of the specialized buffer is copied into `iBuf`
using `append`. That's by itself the right idea because `iBuf` might
be `nil`, and even if not, it might or might not have the right
capacity. However, this approach assumes that `iBuf` is empty, or more
precisely has a length of zero.
This commit makes sure that `iBuf` does not get needlessly initialized
in `newSampleRing` and that it is emptied upon `reset`.
A test case is added to demonstrate both issues above.
Signed-off-by: beorn7 <beorn@grafana.com>
											
										 
											2023-10-31 21:50:26 +08:00
										 |  |  | func TestSampleRingMixed(t *testing.T) { | 
					
						
							|  |  |  | 	h1 := tsdbutil.GenerateTestHistogram(1) | 
					
						
							|  |  |  | 	h2 := tsdbutil.GenerateTestHistogram(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// With ValNone as the preferred type, nothing should be initialized.
 | 
					
						
							|  |  |  | 	r := newSampleRing(10, 2, chunkenc.ValNone) | 
					
						
							|  |  |  | 	require.Zero(t, len(r.fBuf)) | 
					
						
							|  |  |  | 	require.Zero(t, len(r.hBuf)) | 
					
						
							|  |  |  | 	require.Zero(t, len(r.fhBuf)) | 
					
						
							|  |  |  | 	require.Zero(t, len(r.iBuf)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// But then mixed adds should work as expected.
 | 
					
						
							|  |  |  | 	r.addF(fSample{t: 1, f: 3.14}) | 
					
						
							|  |  |  | 	r.addH(hSample{t: 2, h: h1}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it := r.iterator() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Next()) | 
					
						
							|  |  |  | 	ts, f := it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(1), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 3.14, f) | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValHistogram, it.Next()) | 
					
						
							|  |  |  | 	var h *histogram.Histogram | 
					
						
							|  |  |  | 	ts, h = it.AtHistogram() | 
					
						
							|  |  |  | 	require.Equal(t, int64(2), ts) | 
					
						
							|  |  |  | 	require.Equal(t, h1, h) | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValNone, it.Next()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r.reset() | 
					
						
							|  |  |  | 	it = r.iterator() | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValNone, it.Next()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	r.addF(fSample{t: 3, f: 4.2}) | 
					
						
							|  |  |  | 	r.addH(hSample{t: 4, h: h2}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it = r.iterator() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Next()) | 
					
						
							|  |  |  | 	ts, f = it.At() | 
					
						
							|  |  |  | 	require.Equal(t, int64(3), ts) | 
					
						
							|  |  |  | 	require.Equal(t, 4.2, f) | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValHistogram, it.Next()) | 
					
						
							|  |  |  | 	ts, h = it.AtHistogram() | 
					
						
							|  |  |  | 	require.Equal(t, int64(4), ts) | 
					
						
							|  |  |  | 	require.Equal(t, h2, h) | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValNone, it.Next()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | func TestBufferedSeriesIterator(t *testing.T) { | 
					
						
							|  |  |  | 	var it *BufferedSeriesIterator | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 	bufferEq := func(exp []fSample) { | 
					
						
							|  |  |  | 		var b []fSample | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 		bit := it.Buffer() | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 		for bit.Next() == chunkenc.ValFloat { | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 			t, f := bit.At() | 
					
						
							|  |  |  | 			b = append(b, fSample{t: t, f: f}) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.Equal(t, exp, b, "buffer mismatch") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	sampleEq := func(ets int64, ev float64) { | 
					
						
							| 
									
										
										
										
											2021-11-29 18:53:04 +08:00
										 |  |  | 		ts, v := it.At() | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 		require.Equal(t, ets, ts, "timestamp mismatch") | 
					
						
							|  |  |  | 		require.Equal(t, ev, v, "value mismatch") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	prevSampleEq := func(ets int64, ev float64, eok bool) { | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 		s, ok := it.PeekBack(1) | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 		require.Equal(t, eok, ok, "exist mismatch") | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 		require.Equal(t, ets, s.T(), "timestamp mismatch") | 
					
						
							| 
									
										
										
										
											2023-03-31 01:50:13 +08:00
										 |  |  | 		require.Equal(t, ev, s.F(), "value mismatch") | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-24 21:41:52 +08:00
										 |  |  | 	it = NewBufferIterator(NewListSeriesIterator(samples{ | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 		fSample{t: 1, f: 2}, | 
					
						
							|  |  |  | 		fSample{t: 2, f: 3}, | 
					
						
							|  |  |  | 		fSample{t: 3, f: 4}, | 
					
						
							|  |  |  | 		fSample{t: 4, f: 5}, | 
					
						
							|  |  |  | 		fSample{t: 5, f: 6}, | 
					
						
							|  |  |  | 		fSample{t: 99, f: 8}, | 
					
						
							|  |  |  | 		fSample{t: 100, f: 9}, | 
					
						
							|  |  |  | 		fSample{t: 101, f: 10}, | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	}), 2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Seek(-123), "seek failed") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	sampleEq(1, 2) | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	prevSampleEq(0, 0, false) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	bufferEq(nil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Next(), "next failed") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	sampleEq(2, 3) | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	prevSampleEq(1, 2, true) | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 	bufferEq([]fSample{{t: 1, f: 2}}) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Next(), "next failed") | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Next(), "next failed") | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Next(), "next failed") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	sampleEq(5, 6) | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	prevSampleEq(4, 5, true) | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 	bufferEq([]fSample{{t: 2, f: 3}, {t: 3, f: 4}, {t: 4, f: 5}}) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Seek(5), "seek failed") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	sampleEq(5, 6) | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	prevSampleEq(4, 5, true) | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 	bufferEq([]fSample{{t: 2, f: 3}, {t: 3, f: 4}, {t: 4, f: 5}}) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValFloat, it.Seek(101), "seek failed") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 	sampleEq(101, 10) | 
					
						
							| 
									
										
										
										
											2021-03-11 21:32:56 +08:00
										 |  |  | 	prevSampleEq(100, 9, true) | 
					
						
							| 
									
										
										
										
											2022-12-08 20:31:08 +08:00
										 |  |  | 	bufferEq([]fSample{{t: 99, f: 8}, {t: 100, f: 9}}) | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValNone, it.Next(), "next succeeded unexpectedly") | 
					
						
							| 
									
										
										
										
											2021-12-15 20:49:33 +08:00
										 |  |  | 	require.Equal(t, chunkenc.ValNone, it.Seek(1024), "seek succeeded unexpectedly") | 
					
						
							| 
									
										
										
										
											2016-12-25 08:40:28 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-30 17:45:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | // At() should not be called once Next() returns false.
 | 
					
						
							|  |  |  | func TestBufferedSeriesIteratorNoBadAt(t *testing.T) { | 
					
						
							|  |  |  | 	done := false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m := &mockSeriesIterator{ | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 		seek: func(int64) chunkenc.ValueType { return chunkenc.ValNone }, | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | 		at: func() (int64, float64) { | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 			require.False(t, done, "unexpectedly done") | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | 			done = true | 
					
						
							|  |  |  | 			return 0, 0 | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 		next: func() chunkenc.ValueType { | 
					
						
							|  |  |  | 			if done { | 
					
						
							|  |  |  | 				return chunkenc.ValNone | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return chunkenc.ValFloat | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		err: func() error { return nil }, | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 12:10:28 +08:00
										 |  |  | 	it := NewBufferIterator(m, 60) | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | 	it.Next() | 
					
						
							|  |  |  | 	it.Next() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-14 17:59:30 +08:00
										 |  |  | func TestBufferedSeriesIteratorMixedHistograms(t *testing.T) { | 
					
						
							|  |  |  | 	histograms := tsdbutil.GenerateTestHistograms(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it := NewBufferIterator(NewListSeriesIterator(samples{ | 
					
						
							| 
									
										
										
										
											2023-11-29 22:15:57 +08:00
										 |  |  | 		fhSample{t: 1, fh: histograms[0].ToFloat(nil)}, | 
					
						
							| 
									
										
										
										
											2023-04-14 17:59:30 +08:00
										 |  |  | 		hSample{t: 2, h: histograms[1]}, | 
					
						
							|  |  |  | 	}), 2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValNone, it.Seek(3)) | 
					
						
							|  |  |  | 	require.NoError(t, it.Err()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf := it.Buffer() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValFloatHistogram, buf.Next()) | 
					
						
							| 
									
										
										
										
											2023-12-11 04:07:19 +08:00
										 |  |  | 	_, fh := buf.AtFloatHistogram(nil) | 
					
						
							| 
									
										
										
										
											2023-11-29 22:15:57 +08:00
										 |  |  | 	require.Equal(t, histograms[0].ToFloat(nil), fh) | 
					
						
							| 
									
										
										
										
											2023-04-14 17:59:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	require.Equal(t, chunkenc.ValHistogram, buf.Next()) | 
					
						
							| 
									
										
										
										
											2023-12-11 04:07:19 +08:00
										 |  |  | 	_, fh = buf.AtFloatHistogram(nil) | 
					
						
							| 
									
										
										
										
											2023-11-29 22:15:57 +08:00
										 |  |  | 	require.Equal(t, histograms[1].ToFloat(nil), fh) | 
					
						
							| 
									
										
										
										
											2023-04-14 17:59:30 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 17:57:34 +08:00
										 |  |  | func BenchmarkBufferedSeriesIterator(b *testing.B) { | 
					
						
							|  |  |  | 	// Simulate a 5 minute rate.
 | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | 	it := NewBufferIterator(newFakeSeriesIterator(int64(b.N), 30), 5*60) | 
					
						
							| 
									
										
										
										
											2017-03-14 17:57:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:31:37 +08:00
										 |  |  | 	b.SetBytes(16) | 
					
						
							| 
									
										
										
										
											2017-03-14 17:57:34 +08:00
										 |  |  | 	b.ReportAllocs() | 
					
						
							|  |  |  | 	b.ResetTimer() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-31 19:35:13 +08:00
										 |  |  | 	for it.Next() != chunkenc.ValNone { | 
					
						
							| 
									
										
										
										
											2023-04-12 19:05:41 +08:00
										 |  |  | 		// Scan everything.
 | 
					
						
							| 
									
										
										
										
											2017-03-14 17:57:34 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-29 17:43:23 +08:00
										 |  |  | 	require.NoError(b, it.Err()) | 
					
						
							| 
									
										
										
										
											2017-03-14 17:57:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-30 17:45:56 +08:00
										 |  |  | type mockSeriesIterator struct { | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	seek func(int64) chunkenc.ValueType | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | 	at   func() (int64, float64) | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	next func() chunkenc.ValueType | 
					
						
							| 
									
										
										
										
											2017-06-13 13:22:27 +08:00
										 |  |  | 	err  func() error | 
					
						
							| 
									
										
										
										
											2016-12-30 17:45:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | func (m *mockSeriesIterator) Seek(t int64) chunkenc.ValueType { return m.seek(t) } | 
					
						
							|  |  |  | func (m *mockSeriesIterator) At() (int64, float64)            { return m.at() } | 
					
						
							|  |  |  | func (m *mockSeriesIterator) Next() chunkenc.ValueType        { return m.next() } | 
					
						
							|  |  |  | func (m *mockSeriesIterator) Err() error                      { return m.err() } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-13 02:07:41 +08:00
										 |  |  | func (m *mockSeriesIterator) AtHistogram() (int64, *histogram.Histogram) { | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	return 0, nil // Not really mocked.
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (m *mockSeriesIterator) AtFloatHistogram() (int64, *histogram.FloatHistogram) { | 
					
						
							|  |  |  | 	return 0, nil // Not really mocked.
 | 
					
						
							| 
									
										
										
										
											2021-06-29 22:38:46 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-18 02:57:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | func (m *mockSeriesIterator) AtT() int64 { | 
					
						
							|  |  |  | 	return 0 // Not really mocked.
 | 
					
						
							| 
									
										
										
										
											2021-06-30 22:48:13 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-30 17:45:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | type fakeSeriesIterator struct { | 
					
						
							|  |  |  | 	nsamples int64 | 
					
						
							|  |  |  | 	step     int64 | 
					
						
							|  |  |  | 	idx      int64 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newFakeSeriesIterator(nsamples, step int64) *fakeSeriesIterator { | 
					
						
							|  |  |  | 	return &fakeSeriesIterator{nsamples: nsamples, step: step, idx: -1} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (it *fakeSeriesIterator) At() (int64, float64) { | 
					
						
							| 
									
										
										
										
											2021-11-08 00:12:04 +08:00
										 |  |  | 	return it.idx * it.step, 123 // Value doesn't matter.
 | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-13 02:07:41 +08:00
										 |  |  | func (it *fakeSeriesIterator) AtHistogram() (int64, *histogram.Histogram) { | 
					
						
							|  |  |  | 	return it.idx * it.step, &histogram.Histogram{} // Value doesn't matter.
 | 
					
						
							| 
									
										
										
										
											2021-06-29 22:38:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | func (it *fakeSeriesIterator) AtFloatHistogram() (int64, *histogram.FloatHistogram) { | 
					
						
							|  |  |  | 	return it.idx * it.step, &histogram.FloatHistogram{} // Value doesn't matter.
 | 
					
						
							| 
									
										
										
										
											2021-06-30 22:48:13 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | func (it *fakeSeriesIterator) AtT() int64 { | 
					
						
							|  |  |  | 	return it.idx * it.step | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (it *fakeSeriesIterator) Next() chunkenc.ValueType { | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | 	it.idx++ | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	if it.idx >= it.nsamples { | 
					
						
							|  |  |  | 		return chunkenc.ValNone | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return chunkenc.ValFloat | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | func (it *fakeSeriesIterator) Seek(t int64) chunkenc.ValueType { | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | 	it.idx = t / it.step | 
					
						
							| 
									
										
										
										
											2021-11-29 15:54:23 +08:00
										 |  |  | 	if it.idx >= it.nsamples { | 
					
						
							|  |  |  | 		return chunkenc.ValNone | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return chunkenc.ValFloat | 
					
						
							| 
									
										
										
										
											2018-12-18 19:22:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 04:15:47 +08:00
										 |  |  | func (it *fakeSeriesIterator) Err() error { return nil } |