2021-06-29 01:37:41 -07:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
|
|
|
package chunkenc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-17 10:57:31 -08:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2021-06-29 01:37:41 -07: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 06:57:07 -07:00
|
|
|
func TestHistogramChunkSameBuckets(t *testing.T) {
|
|
|
|
c := NewHistogramChunk()
|
2021-08-06 01:56:56 -07:00
|
|
|
var exp []res
|
2021-06-29 01:37:41 -07: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 06:57:07 -07:00
|
|
|
// Create fresh appender and add the first histogram.
|
2021-06-29 01:37:41 -07:00
|
|
|
app, err := c.Appender()
|
|
|
|
require.NoError(t, err)
|
2021-07-01 23:20:30 -07:00
|
|
|
require.Equal(t, 0, c.NumSamples())
|
2021-06-29 01:37:41 -07:00
|
|
|
|
2021-06-29 06:57:59 -07:00
|
|
|
ts := int64(1234567890)
|
2021-11-12 10:07:41 -08:00
|
|
|
h := &histogram.Histogram{
|
2021-08-06 05:38:41 -07:00
|
|
|
Count: 5,
|
|
|
|
ZeroCount: 2,
|
|
|
|
Sum: 18.4,
|
|
|
|
ZeroThreshold: 1e-100,
|
|
|
|
Schema: 1,
|
2021-06-29 01:37:41 -07:00
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 2},
|
|
|
|
{Offset: 1, Length: 2},
|
|
|
|
},
|
2021-06-30 03:45:43 -07:00
|
|
|
PositiveBuckets: []int64{1, 1, -1, 0}, // counts: 1, 2, 1, 1 (total 5)
|
2021-06-29 01:37:41 -07:00
|
|
|
}
|
2021-10-06 03:32:19 -07:00
|
|
|
app.AppendHistogram(ts, h)
|
2021-08-06 01:56:56 -07:00
|
|
|
exp = append(exp, res{t: ts, h: h})
|
2021-07-01 23:20:30 -07:00
|
|
|
require.Equal(t, 1, c.NumSamples())
|
2021-06-29 01:37:41 -07:00
|
|
|
|
2021-08-06 01:56:56 -07:00
|
|
|
// Add an updated histogram.
|
2021-06-30 03:45:43 -07:00
|
|
|
ts += 16
|
2021-11-12 10:07:41 -08:00
|
|
|
h = h.Copy()
|
2021-06-30 03:45:43 -07:00
|
|
|
h.Count += 9
|
|
|
|
h.ZeroCount++
|
|
|
|
h.Sum = 24.4
|
|
|
|
h.PositiveBuckets = []int64{5, -2, 1, -2} // counts: 5, 3, 4, 2 (total 14)
|
2021-10-06 03:32:19 -07:00
|
|
|
app.AppendHistogram(ts, h)
|
2021-06-30 03:45:43 -07:00
|
|
|
exp = append(exp, res{t: ts, h: h})
|
2021-07-01 23:20:30 -07:00
|
|
|
require.Equal(t, 2, c.NumSamples())
|
2021-06-30 03:45:43 -07: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 06:57:07 -07:00
|
|
|
// Add update with new appender.
|
2021-06-30 03:45:43 -07:00
|
|
|
app, err = c.Appender()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ts += 14
|
2021-11-12 10:07:41 -08:00
|
|
|
h = h.Copy()
|
2021-06-30 03:45:43 -07:00
|
|
|
h.Count += 13
|
|
|
|
h.ZeroCount += 2
|
|
|
|
h.Sum = 24.4
|
|
|
|
h.PositiveBuckets = []int64{6, 1, -3, 6} // counts: 6, 7, 4, 10 (total 27)
|
2021-10-06 03:32:19 -07:00
|
|
|
app.AppendHistogram(ts, h)
|
2021-06-30 03:45:43 -07:00
|
|
|
exp = append(exp, res{t: ts, h: h})
|
2021-07-01 23:20:30 -07:00
|
|
|
require.Equal(t, 3, c.NumSamples())
|
2021-06-29 01:37:41 -07:00
|
|
|
|
|
|
|
// 1. Expand iterator in simple case.
|
2021-08-06 01:56:56 -07:00
|
|
|
it := c.iterator(nil)
|
|
|
|
require.NoError(t, it.Err())
|
|
|
|
var act []res
|
2021-11-28 23:54:23 -08:00
|
|
|
for it.Next() == ValHistogram {
|
2021-08-06 01:56:56 -07:00
|
|
|
ts, h := it.AtHistogram()
|
2021-11-28 23:54:23 -08:00
|
|
|
act = append(act, res{t: ts, h: h})
|
2021-06-29 01:37:41 -07:00
|
|
|
}
|
2021-08-06 01:56:56 -07:00
|
|
|
require.NoError(t, it.Err())
|
|
|
|
require.Equal(t, exp, act)
|
2021-06-29 01:37:41 -07:00
|
|
|
|
|
|
|
// 2. Expand second iterator while reusing first one.
|
2021-11-17 10:57:31 -08:00
|
|
|
// it2 := c.Iterator(it1)
|
|
|
|
// var res2 []pair
|
|
|
|
// for it2.Next() {
|
2021-06-29 01:37:41 -07:00
|
|
|
// ts, v := it2.At()
|
|
|
|
// res2 = append(res2, pair{t: ts, v: v})
|
|
|
|
// }
|
|
|
|
// require.NoError(t, it2.Err())
|
|
|
|
// require.Equal(t, exp, res2)
|
|
|
|
|
|
|
|
// 3. Test iterator Seek.
|
|
|
|
// mid := len(exp) / 2
|
|
|
|
|
|
|
|
// it3 := c.Iterator(nil)
|
|
|
|
// var res3 []pair
|
|
|
|
// require.Equal(t, true, it3.Seek(exp[mid].t))
|
|
|
|
// Below ones should not matter.
|
|
|
|
// require.Equal(t, true, it3.Seek(exp[mid].t))
|
|
|
|
// require.Equal(t, true, it3.Seek(exp[mid].t))
|
|
|
|
// ts, v = it3.At()
|
|
|
|
// res3 = append(res3, pair{t: ts, v: v})
|
|
|
|
|
|
|
|
// for it3.Next() {
|
|
|
|
// ts, v := it3.At()
|
|
|
|
// res3 = append(res3, pair{t: ts, v: v})
|
|
|
|
// }
|
|
|
|
// require.NoError(t, it3.Err())
|
|
|
|
// require.Equal(t, exp[mid:], res3)
|
|
|
|
// require.Equal(t, false, it3.Seek(exp[len(exp)-1].t+1))
|
|
|
|
}
|
2021-07-01 23:20:30 -07:00
|
|
|
|
2021-07-15 02:05:35 -07:00
|
|
|
type res struct {
|
|
|
|
t int64
|
2021-11-12 10:07:41 -08:00
|
|
|
h *histogram.Histogram
|
2021-07-15 02:05:35 -07: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 06:57:07 -07:00
|
|
|
// Mimics the scenario described for compareSpans().
|
|
|
|
func TestHistogramChunkBucketChanges(t *testing.T) {
|
|
|
|
c := Chunk(NewHistogramChunk())
|
2021-07-01 23:20:30 -07:00
|
|
|
|
2021-08-06 01:56:56 -07:00
|
|
|
// Create fresh appender and add the first histogram.
|
2021-07-01 23:20:30 -07:00
|
|
|
app, err := c.Appender()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, c.NumSamples())
|
|
|
|
|
|
|
|
ts1 := int64(1234567890)
|
2021-11-12 10:07:41 -08:00
|
|
|
h1 := &histogram.Histogram{
|
2021-08-06 05:38:41 -07:00
|
|
|
Count: 5,
|
|
|
|
ZeroCount: 2,
|
|
|
|
Sum: 18.4,
|
|
|
|
ZeroThreshold: 1e-125,
|
|
|
|
Schema: 1,
|
2021-07-01 23:20:30 -07:00
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 2},
|
|
|
|
{Offset: 2, Length: 1},
|
|
|
|
{Offset: 3, Length: 2},
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []int64{6, -3, 0, -1, 2, 1, -4}, // counts: 6, 3, 3, 2, 4, 5, 1 (total 24)
|
|
|
|
}
|
|
|
|
|
2021-10-06 03:32:19 -07:00
|
|
|
app.AppendHistogram(ts1, h1)
|
2021-07-01 23:20:30 -07:00
|
|
|
require.Equal(t, 1, c.NumSamples())
|
|
|
|
|
2021-08-06 01:56:56 -07:00
|
|
|
// Add a new histogram that has expanded buckets.
|
2021-07-01 23:20:30 -07:00
|
|
|
ts2 := ts1 + 16
|
2021-11-12 10:07:41 -08:00
|
|
|
h2 := h1.Copy()
|
2021-07-01 23:20:30 -07:00
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
}
|
|
|
|
h2.Count += 9
|
|
|
|
h2.ZeroCount++
|
|
|
|
h2.Sum = 30
|
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 06:57:07 -07:00
|
|
|
// Existing histogram should get values converted from the above to:
|
|
|
|
// 6 3 0 3 0 0 2 4 5 0 1 (previous values with some new empty buckets in between)
|
2021-07-01 23:20:30 -07:00
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
h2.PositiveBuckets = []int64{7, -2, -4, 2, -2, -1, 2, 3, 0, -5, 1} // 7 5 1 3 1 0 2 5 5 0 1 (total 30)
|
|
|
|
|
2021-08-06 01:56:56 -07:00
|
|
|
// This is how span changes will be handled.
|
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 06:57:07 -07:00
|
|
|
hApp, _ := app.(*HistogramAppender)
|
|
|
|
posInterjections, negInterjections, ok, cr := hApp.Appendable(h2)
|
2021-08-06 01:56:56 -07:00
|
|
|
require.Greater(t, len(posInterjections), 0)
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
require.True(t, ok) // Only new buckets came in.
|
2021-10-04 06:14:12 -07:00
|
|
|
require.False(t, cr)
|
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 06:57:07 -07:00
|
|
|
c, app = hApp.Recode(posInterjections, negInterjections, h2.PositiveSpans, h2.NegativeSpans)
|
2021-10-06 03:32:19 -07:00
|
|
|
app.AppendHistogram(ts2, h2)
|
2021-07-01 23:20:30 -07:00
|
|
|
|
|
|
|
require.Equal(t, 2, c.NumSamples())
|
|
|
|
|
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 06:57:07 -07:00
|
|
|
// Because the 2nd histogram has expanded buckets, we should expect all
|
|
|
|
// histograms (in particular the first) to come back using the new spans
|
|
|
|
// metadata as well as the expanded buckets.
|
2021-07-01 23:20:30 -07:00
|
|
|
h1.PositiveSpans = h2.PositiveSpans
|
|
|
|
h1.PositiveBuckets = []int64{6, -3, -3, 3, -3, 0, 2, 2, 1, -5, 1}
|
|
|
|
exp := []res{
|
|
|
|
{t: ts1, h: h1},
|
|
|
|
{t: ts2, h: h2},
|
|
|
|
}
|
2021-08-06 01:56:56 -07:00
|
|
|
it := c.Iterator(nil)
|
|
|
|
var act []res
|
2021-11-28 23:54:23 -08:00
|
|
|
for it.Next() == ValHistogram {
|
2021-08-06 01:56:56 -07:00
|
|
|
ts, h := it.AtHistogram()
|
2021-11-28 23:54:23 -08:00
|
|
|
act = append(act, res{t: ts, h: h})
|
2021-07-01 23:20:30 -07:00
|
|
|
}
|
2021-08-06 01:56:56 -07:00
|
|
|
require.NoError(t, it.Err())
|
|
|
|
require.Equal(t, exp, act)
|
2021-07-01 23:20:30 -07:00
|
|
|
}
|
2021-08-17 09:34:45 -07:00
|
|
|
|
|
|
|
func TestHistoChunkAppendable(t *testing.T) {
|
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 06:57:07 -07:00
|
|
|
c := Chunk(NewHistogramChunk())
|
2021-08-17 09:34:45 -07:00
|
|
|
|
|
|
|
// Create fresh appender and add the first histogram.
|
|
|
|
app, err := c.Appender()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, c.NumSamples())
|
|
|
|
|
|
|
|
ts := int64(1234567890)
|
2021-11-12 10:07:41 -08:00
|
|
|
h1 := &histogram.Histogram{
|
2021-08-17 09:34:45 -07:00
|
|
|
Count: 5,
|
|
|
|
ZeroCount: 2,
|
|
|
|
Sum: 18.4,
|
|
|
|
ZeroThreshold: 1e-125,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 2},
|
|
|
|
{Offset: 2, Length: 1},
|
|
|
|
{Offset: 3, Length: 2},
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []int64{6, -3, 0, -1, 2, 1, -4}, // counts: 6, 3, 3, 2, 4, 5, 1 (total 24)
|
|
|
|
}
|
|
|
|
|
2021-10-06 03:32:19 -07:00
|
|
|
app.AppendHistogram(ts, h1)
|
2021-08-17 09:34:45 -07:00
|
|
|
require.Equal(t, 1, c.NumSamples())
|
|
|
|
|
|
|
|
{ // New histogram that has more buckets.
|
|
|
|
h2 := h1
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
}
|
|
|
|
h2.Count += 9
|
|
|
|
h2.ZeroCount++
|
|
|
|
h2.Sum = 30
|
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 06:57:07 -07:00
|
|
|
// Existing histogram should get values converted from the above to:
|
|
|
|
// 6 3 0 3 0 0 2 4 5 0 1 (previous values with some new empty buckets in between)
|
2021-08-17 09:34:45 -07:00
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
h2.PositiveBuckets = []int64{7, -2, -4, 2, -2, -1, 2, 3, 0, -5, 1} // 7 5 1 3 1 0 2 5 5 0 1 (total 30)
|
|
|
|
|
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 06:57:07 -07:00
|
|
|
hApp, _ := app.(*HistogramAppender)
|
|
|
|
posInterjections, negInterjections, ok, cr := hApp.Appendable(h2)
|
2021-08-17 09:34:45 -07:00
|
|
|
require.Greater(t, len(posInterjections), 0)
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
require.True(t, ok) // Only new buckets came in.
|
2021-10-04 06:14:12 -07:00
|
|
|
require.False(t, cr)
|
2021-08-17 09:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // New histogram that has a bucket missing.
|
|
|
|
h2 := h1
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
{Offset: 0, Length: 2},
|
|
|
|
{Offset: 5, Length: 2},
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
}
|
|
|
|
h2.Sum = 21
|
|
|
|
h2.PositiveBuckets = []int64{6, -3, -1, 2, 1, -4} // counts: 6, 3, 2, 4, 5, 1 (total 21)
|
|
|
|
|
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 06:57:07 -07:00
|
|
|
hApp, _ := app.(*HistogramAppender)
|
|
|
|
posInterjections, negInterjections, ok, cr := hApp.Appendable(h2)
|
2021-08-17 09:34:45 -07:00
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
2021-10-06 02:58:10 -07:00
|
|
|
require.True(t, cr)
|
2021-08-17 09:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // New histogram that has a counter reset while buckets are same.
|
|
|
|
h2 := h1
|
|
|
|
h2.Sum = 23
|
|
|
|
h2.PositiveBuckets = []int64{6, -4, 1, -1, 2, 1, -4} // counts: 6, 2, 3, 2, 4, 5, 1 (total 23)
|
|
|
|
|
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 06:57:07 -07:00
|
|
|
hApp, _ := app.(*HistogramAppender)
|
|
|
|
posInterjections, negInterjections, ok, cr := hApp.Appendable(h2)
|
2021-08-17 09:34:45 -07:00
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
2021-10-04 06:14:12 -07:00
|
|
|
require.True(t, cr)
|
2021-08-17 09:34:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // New histogram that has a counter reset while new buckets were added.
|
|
|
|
h2 := h1
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
}
|
|
|
|
h2.Sum = 29
|
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 06:57:07 -07:00
|
|
|
// Existing histogram should get values converted from the above to:
|
|
|
|
// 6 3 0 3 0 0 2 4 5 0 1 (previous values with some new empty buckets in between)
|
2021-08-17 09:34:45 -07:00
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
h2.PositiveBuckets = []int64{7, -2, -4, 2, -2, -1, 2, 3, 0, -5, 0} // 7 5 1 3 1 0 2 5 5 0 0 (total 29)
|
|
|
|
|
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 06:57:07 -07:00
|
|
|
hApp, _ := app.(*HistogramAppender)
|
|
|
|
posInterjections, negInterjections, ok, cr := hApp.Appendable(h2)
|
2021-08-17 09:34:45 -07:00
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
2021-10-04 06:14:12 -07:00
|
|
|
require.True(t, cr)
|
2021-08-17 09:34:45 -07: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 06:57:07 -07:00
|
|
|
{
|
|
|
|
// New histogram that has a counter reset while new buckets were
|
|
|
|
// added before the first bucket and reset on first bucket. (to
|
|
|
|
// catch the edge case where the new bucket should be forwarded
|
|
|
|
// ahead until first old bucket at start)
|
2021-08-17 09:34:45 -07:00
|
|
|
h2 := h1
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
{Offset: -3, Length: 2},
|
|
|
|
{Offset: 1, Length: 2},
|
|
|
|
{Offset: 2, Length: 1},
|
|
|
|
{Offset: 3, Length: 2},
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
}
|
|
|
|
h2.Sum = 26
|
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 06:57:07 -07:00
|
|
|
// Existing histogram should get values converted from the above to:
|
|
|
|
// 0, 0, 6, 3, 3, 2, 4, 5, 1
|
2021-08-17 09:34:45 -07:00
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
h2.PositiveBuckets = []int64{1, 1, 3, -2, 0, -1, 2, 1, -4} // counts: 1, 2, 5, 3, 3, 2, 4, 5, 1 (total 26)
|
|
|
|
|
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 06:57:07 -07:00
|
|
|
hApp, _ := app.(*HistogramAppender)
|
|
|
|
posInterjections, negInterjections, ok, cr := hApp.Appendable(h2)
|
2021-08-17 09:34:45 -07:00
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
2021-10-04 06:14:12 -07:00
|
|
|
require.True(t, cr)
|
2021-08-17 09:34:45 -07:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 10:23:51 -07:00
|
|
|
|
|
|
|
func TestAtFloatHistogram(t *testing.T) {
|
|
|
|
input := []histogram.Histogram{
|
|
|
|
{
|
|
|
|
Schema: 0,
|
|
|
|
Count: 21,
|
|
|
|
Sum: 1234.5,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
ZeroCount: 4,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 0, Length: 0},
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []int64{1, 1, -1, 0, 0, 0, 0},
|
|
|
|
NegativeSpans: []histogram.Span{
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []int64{1, 1, -1, 1, 0, 0, 0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Schema: 0,
|
|
|
|
Count: 36,
|
|
|
|
Sum: 2345.6,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
ZeroCount: 5,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 0, Length: 0},
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []int64{1, 2, -2, 1, -1, 0, 0},
|
|
|
|
NegativeSpans: []histogram.Span{
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []int64{1, 3, -2, 5, -2, 0, -3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Schema: 0,
|
|
|
|
Count: 36,
|
|
|
|
Sum: 1111.1,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
ZeroCount: 5,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 0, Length: 0},
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []int64{1, 2, -2, 2, -1, 0, 0},
|
|
|
|
NegativeSpans: []histogram.Span{
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []int64{1, 3, -2, 5, -1, 0, -3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expOutput := []*histogram.FloatHistogram{
|
|
|
|
{
|
|
|
|
Schema: 0,
|
|
|
|
Count: 21,
|
|
|
|
Sum: 1234.5,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
ZeroCount: 4,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 0, Length: 0},
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{1, 2, 1, 1, 1, 1, 1},
|
|
|
|
NegativeSpans: []histogram.Span{
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{1, 2, 1, 2, 2, 2, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Schema: 0,
|
|
|
|
Count: 36,
|
|
|
|
Sum: 2345.6,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
ZeroCount: 5,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 0, Length: 0},
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{1, 3, 1, 2, 1, 1, 1},
|
|
|
|
NegativeSpans: []histogram.Span{
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{1, 4, 2, 7, 5, 5, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Schema: 0,
|
|
|
|
Count: 36,
|
|
|
|
Sum: 1111.1,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
ZeroCount: 5,
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 0, Length: 0},
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{1, 3, 1, 3, 2, 2, 2},
|
|
|
|
NegativeSpans: []histogram.Span{
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{1, 4, 2, 7, 6, 6, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
chk := NewHistogramChunk()
|
|
|
|
app, err := chk.Appender()
|
|
|
|
require.NoError(t, err)
|
|
|
|
for i := range input {
|
|
|
|
app.AppendHistogram(int64(i), &input[i])
|
|
|
|
}
|
|
|
|
it := chk.Iterator(nil)
|
|
|
|
i := int64(0)
|
|
|
|
for it.Next() != ValNone {
|
|
|
|
ts, h := it.AtFloatHistogram()
|
|
|
|
require.Equal(t, i, ts)
|
|
|
|
require.Equal(t, expOutput[i], h, "histogram %d unequal", i)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|