rename lastSum -> lastCount && enrich test case with expectBucketCount and expectSchema

Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
This commit is contained in:
Ziqi Zhao 2023-11-16 13:00:11 +08:00
parent b94f32f6fa
commit 10ebeb0a62
2 changed files with 37 additions and 21 deletions

View File

@ -367,18 +367,18 @@ type bucketLimitAppender struct {
func (app *bucketLimitAppender) AppendHistogram(ref storage.SeriesRef, lset labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram) (storage.SeriesRef, error) {
if h != nil {
for len(h.PositiveBuckets)+len(h.NegativeBuckets) > app.limit {
lastSum := len(h.PositiveBuckets) + len(h.NegativeBuckets)
lastCount := len(h.PositiveBuckets) + len(h.NegativeBuckets)
h = h.ReduceResolution(h.Schema - 1)
if len(h.PositiveBuckets)+len(h.NegativeBuckets) == lastSum {
if len(h.PositiveBuckets)+len(h.NegativeBuckets) == lastCount {
return 0, errBucketLimit
}
}
}
if fh != nil {
for len(fh.PositiveBuckets)+len(fh.NegativeBuckets) > app.limit {
lastSum := len(fh.PositiveBuckets) + len(fh.NegativeBuckets)
lastCount := len(fh.PositiveBuckets) + len(fh.NegativeBuckets)
fh = fh.ReduceResolution(fh.Schema - 1)
if len(fh.PositiveBuckets)+len(fh.NegativeBuckets) == lastSum {
if len(fh.PositiveBuckets)+len(fh.NegativeBuckets) == lastCount {
return 0, errBucketLimit
}
}

View File

@ -508,9 +508,11 @@ func TestBucketLimitAppender(t *testing.T) {
}
cases := []struct {
h histogram.Histogram
limit int
expectError bool
h histogram.Histogram
limit int
expectError bool
expectBucketCount int
expectSchema int32
}{
{
h: example,
@ -518,14 +520,18 @@ func TestBucketLimitAppender(t *testing.T) {
expectError: true,
},
{
h: example,
limit: 4,
expectError: false,
h: example,
limit: 4,
expectError: false,
expectBucketCount: 4,
expectSchema: -1,
},
{
h: example,
limit: 10,
expectError: false,
h: example,
limit: 10,
expectError: false,
expectBucketCount: 6,
expectSchema: 0,
},
}
@ -536,18 +542,28 @@ func TestBucketLimitAppender(t *testing.T) {
t.Run(fmt.Sprintf("floatHistogram=%t", floatHisto), func(t *testing.T) {
app := &bucketLimitAppender{Appender: resApp, limit: c.limit}
ts := int64(10 * time.Minute / time.Millisecond)
h := c.h
lbls := labels.FromStrings("__name__", "sparse_histogram_series")
var err error
if floatHisto {
_, err = app.AppendHistogram(0, lbls, ts, nil, h.Copy().ToFloat())
fh := c.h.Copy().ToFloat()
_, err = app.AppendHistogram(0, lbls, ts, nil, fh)
if c.expectError {
require.Error(t, err)
} else {
require.Equal(t, c.expectSchema, fh.Schema)
require.Equal(t, c.expectBucketCount, len(fh.NegativeBuckets)+len(fh.PositiveBuckets))
require.NoError(t, err)
}
} else {
_, err = app.AppendHistogram(0, lbls, ts, h.Copy(), nil)
}
if c.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
h := c.h.Copy()
_, err = app.AppendHistogram(0, lbls, ts, h, nil)
if c.expectError {
require.Error(t, err)
} else {
require.Equal(t, c.expectSchema, h.Schema)
require.Equal(t, c.expectBucketCount, len(h.NegativeBuckets)+len(h.PositiveBuckets))
require.NoError(t, err)
}
}
require.NoError(t, app.Commit())
})