Skip to content

Commit

Permalink
draft: removed hardcoding from two groups test
Browse files Browse the repository at this point in the history
Signed-off-by: metonymic-smokey <ahuja.aditi@gmail.com>
  • Loading branch information
metonymic-smokey committed Nov 4, 2021
1 parent 35c4ff7 commit af188aa
Showing 1 changed file with 26 additions and 100 deletions.
126 changes: 26 additions & 100 deletions pkg/compact/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ func TestCompactProgressCalculate(t *testing.T) {
int64(2 * 24 * time.Hour / time.Millisecond),
})

extLabels := labels.FromMap(map[string]string{"a": "1", "b": "2"})
var bkt objstore.Bucket
temp := prometheus.NewCounter(prometheus.CounterOpts{})
grouper := NewDefaultGrouper(logger, bkt, false, false, reg, temp, temp, temp, "")

for _, tcase := range []struct {
testName string
Expand Down Expand Up @@ -390,7 +392,7 @@ func TestCompactProgressCalculate(t *testing.T) {
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"a": "1"},
Labels: map[string]string{"b": "2"},
},
},
{
Expand All @@ -401,7 +403,7 @@ func TestCompactProgressCalculate(t *testing.T) {
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"a": "1"},
Labels: map[string]string{"b": "2"},
},
},
{
Expand All @@ -412,7 +414,7 @@ func TestCompactProgressCalculate(t *testing.T) {
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"a": "1"},
Labels: map[string]string{"b": "2"},
},
},
{
Expand All @@ -423,7 +425,7 @@ func TestCompactProgressCalculate(t *testing.T) {
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"a": "1"},
Labels: map[string]string{"b": "2"},
},
},
{
Expand All @@ -434,7 +436,7 @@ func TestCompactProgressCalculate(t *testing.T) {
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"a": "1"},
Labels: map[string]string{"b": "2"},
},
},
},
Expand All @@ -451,7 +453,7 @@ func TestCompactProgressCalculate(t *testing.T) {
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(0, nil),
MinTime: 0,
MinTime: int64(0 * time.Hour / time.Millisecond),
MaxTime: int64(2 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Expand Down Expand Up @@ -496,18 +498,7 @@ func TestCompactProgressCalculate(t *testing.T) {
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(4, nil),
MinTime: int64(8 * time.Hour / time.Millisecond),
MaxTime: int64(16 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"b": "2"},
},
},
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(6, nil),
MinTime: int64(16 * time.Hour / time.Millisecond),
MaxTime: int64(18 * time.Hour / time.Millisecond),
MaxTime: int64(10 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Expand All @@ -516,9 +507,9 @@ func TestCompactProgressCalculate(t *testing.T) {
},
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(7, nil),
MinTime: int64(18 * time.Hour / time.Millisecond),
MaxTime: int64(26 * time.Hour / time.Millisecond),
ULID: ulid.MustNew(5, nil),
MinTime: int64(10 * time.Hour / time.Millisecond),
MaxTime: int64(12 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Expand All @@ -528,111 +519,46 @@ func TestCompactProgressCalculate(t *testing.T) {
},
expected: planResult{
compactionRuns: 1.0,
compactionBlocks: 4.0,
compactionBlocks: 2.0,
},
},
} {
if ok := t.Run(tcase.testName, func(t *testing.T) {
groups := []*Group{
{
key: "b",
labels: extLabels,
resolution: 0,
metasByMinTime: tcase.input,
},
}
groups := make([]*Group, 2)

if tcase.testName == "two_groups_test" {
groups = append(groups, &Group{
key: "a",
metasByMinTime: []*metadata.Meta{
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(0, nil),
MinTime: int64(0 * time.Hour / time.Millisecond),
MaxTime: int64(2 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"b": "2"},
},
},
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(1, nil),
MinTime: int64(2 * time.Hour / time.Millisecond),
MaxTime: int64(4 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"b": "2"},
},
},
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(2, nil),
MinTime: int64(4 * time.Hour / time.Millisecond),
MaxTime: int64(6 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"b": "2"},
},
},
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(3, nil),
MinTime: int64(6 * time.Hour / time.Millisecond),
MaxTime: int64(8 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"b": "2"},
},
},
{
BlockMeta: tsdb.BlockMeta{
ULID: ulid.MustNew(4, nil),
MinTime: int64(8 * time.Hour / time.Millisecond),
MaxTime: int64(10 * time.Hour / time.Millisecond),
},
Thanos: metadata.Thanos{
Version: 1,
Labels: map[string]string{"b": "2"},
},
},
},
resolution: 0,
})
blocks := make(map[ulid.ULID]*metadata.Meta, len(tcase.input))
for _, meta := range tcase.input {
blocks[meta.ULID] = meta
}

groups, _ = grouper.Groups(blocks)
ps := NewCompactionProgressCalculator(unRegisterer, planner)
err := ps.ProgressCalculate(context.Background(), groups)
metrics := ps.CompactProgressMetrics
testutil.Ok(t, err)
a, err := metrics.NumberOfCompactionBlocks.GetMetricWithLabelValues("b")
a, err := metrics.NumberOfCompactionBlocks.GetMetricWithLabelValues(groups[0].key)
if err != nil {
level.Warn(logger).Log("msg", "could not get number of blocks")
}
testutil.Equals(t, tcase.expected.compactionBlocks, promtestutil.ToFloat64(a))

b, err := metrics.NumberOfCompactionRuns.GetMetricWithLabelValues("b")
b, err := metrics.NumberOfCompactionRuns.GetMetricWithLabelValues(groups[0].key)
if err != nil {
level.Warn(logger).Log("msg", "could not get number of runs")
}
testutil.Equals(t, tcase.expected.compactionRuns, promtestutil.ToFloat64(b))

if tcase.testName == "two_groups_test" {
a, err = metrics.NumberOfCompactionBlocks.GetMetricWithLabelValues("a")
a, err = metrics.NumberOfCompactionBlocks.GetMetricWithLabelValues(groups[1].key) // use the group key(groups[1].key) here instead of hardcoding
// elsewhere, use group[0].key
if err != nil {
level.Warn(logger).Log("msg", "could not get number of blocks for two groups test")
}
testutil.Equals(t, 4.0, promtestutil.ToFloat64(a))
b, err := metrics.NumberOfCompactionRuns.GetMetricWithLabelValues("a")
testutil.Equals(t, 0.0, promtestutil.ToFloat64(a))
b, err := metrics.NumberOfCompactionRuns.GetMetricWithLabelValues(groups[1].key)
if err != nil {
level.Warn(logger).Log("msg", "could not get number of runs")
}
testutil.Equals(t, 1.0, promtestutil.ToFloat64(b))
testutil.Equals(t, 0.0, promtestutil.ToFloat64(b))

}
}); !ok {
Expand Down

0 comments on commit af188aa

Please sign in to comment.