Comment and "go fmt" fixups in compaction tests.

Change-Id: Iaa0eda6a22a5caa0590bae87ff579f9ace21e80a
This commit is contained in:
Julius Volz 2013-10-30 17:06:17 +01:00
parent 8c08a5031f
commit db015de65b

View file

@ -14,9 +14,9 @@
package metric package metric
import ( import (
"flag"
"fmt" "fmt"
"testing" "testing"
"flag"
"time" "time"
"github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage"
@ -25,10 +25,11 @@ import (
) )
type nopCurationStateUpdater struct{} type nopCurationStateUpdater struct{}
func (n *nopCurationStateUpdater) UpdateCurationState(*CurationState) {} func (n *nopCurationStateUpdater) UpdateCurationState(*CurationState) {}
func generateTestSamples(endTime time.Time, numTs int, samplesPerTs int, interval time.Duration) clientmodel.Samples { func generateTestSamples(endTime time.Time, numTs int, samplesPerTs int, interval time.Duration) clientmodel.Samples {
samples := clientmodel.Samples{} samples := make(clientmodel.Samples, 0, numTs*samplesPerTs)
startTime := endTime.Add(-interval * time.Duration(samplesPerTs-1)) startTime := endTime.Add(-interval * time.Duration(samplesPerTs-1))
for ts := 0; ts < numTs; ts++ { for ts := 0; ts < numTs; ts++ {
@ -37,7 +38,7 @@ func generateTestSamples(endTime time.Time, numTs int, samplesPerTs int, interva
for i := 0; i < samplesPerTs; i++ { for i := 0; i < samplesPerTs; i++ {
sample := &clientmodel.Sample{ sample := &clientmodel.Sample{
Metric: metric, Metric: metric,
Value: clientmodel.SampleValue(ts + 1000 * i), Value: clientmodel.SampleValue(ts + 1000*i),
Timestamp: startTime.Add(interval * time.Duration(i)), Timestamp: startTime.Add(interval * time.Duration(i)),
} }
samples = append(samples, sample) samples = append(samples, sample)
@ -47,9 +48,9 @@ func generateTestSamples(endTime time.Time, numTs int, samplesPerTs int, interva
} }
type compactionChecker struct { type compactionChecker struct {
t *testing.T t *testing.T
sampleIdx int sampleIdx int
numChunks int numChunks int
expectedSamples clientmodel.Samples expectedSamples clientmodel.Samples
} }
@ -57,23 +58,23 @@ func (c *compactionChecker) Operate(key, value interface{}) *storage.OperatorErr
c.numChunks++ c.numChunks++
sampleKey := key.(*SampleKey) sampleKey := key.(*SampleKey)
if sampleKey.FirstTimestamp.After(sampleKey.LastTimestamp) { if sampleKey.FirstTimestamp.After(sampleKey.LastTimestamp) {
c.t.Fatalf("Chunk FirstTimestamp (%v) is after LastTimestamp (%v): %v\n", sampleKey.FirstTimestamp.Unix(), sampleKey.LastTimestamp.Unix(), sampleKey) c.t.Fatalf("Chunk FirstTimestamp (%v) is after LastTimestamp (%v): %v", sampleKey.FirstTimestamp.Unix(), sampleKey.LastTimestamp.Unix(), sampleKey)
} }
fp := new(clientmodel.Fingerprint)
for _, sample := range value.(Values) { for _, sample := range value.(Values) {
if sample.Timestamp.Before(sampleKey.FirstTimestamp) || sample.Timestamp.After(sampleKey.LastTimestamp) { if sample.Timestamp.Before(sampleKey.FirstTimestamp) || sample.Timestamp.After(sampleKey.LastTimestamp) {
c.t.Fatalf("Sample not within chunk boundaries: chunk FirstTimestamp (%v), chunk LastTimestamp (%v) vs. sample Timestamp (%v)\n", sampleKey.FirstTimestamp.Unix(), sampleKey.LastTimestamp.Unix(), sample.Timestamp) c.t.Fatalf("Sample not within chunk boundaries: chunk FirstTimestamp (%v), chunk LastTimestamp (%v) vs. sample Timestamp (%v)", sampleKey.FirstTimestamp.Unix(), sampleKey.LastTimestamp.Unix(), sample.Timestamp)
} }
expected := c.expectedSamples[c.sampleIdx] expected := c.expectedSamples[c.sampleIdx]
fp := &clientmodel.Fingerprint{}
fp.LoadFromMetric(expected.Metric) fp.LoadFromMetric(expected.Metric)
if !sampleKey.Fingerprint.Equal(fp) { if !sampleKey.Fingerprint.Equal(fp) {
c.t.Fatalf("%d. Expected fingerprint %s, got %s", c.sampleIdx, fp, sampleKey.Fingerprint) c.t.Fatalf("%d. Expected fingerprint %s, got %s", c.sampleIdx, fp, sampleKey.Fingerprint)
} }
sp := &SamplePair{ sp := &SamplePair{
Value: expected.Value, Value: expected.Value,
Timestamp: expected.Timestamp, Timestamp: expected.Timestamp,
} }
if !sample.Equal(sp) { if !sample.Equal(sp) {
@ -84,11 +85,10 @@ func (c *compactionChecker) Operate(key, value interface{}) *storage.OperatorErr
return nil return nil
} }
func checkStorageSaneAndEquivalent(t *testing.T, name string, ts *TieredStorage, samples clientmodel.Samples, expectedNumChunks int) { func checkStorageSaneAndEquivalent(t *testing.T, name string, ts *TieredStorage, samples clientmodel.Samples, expectedNumChunks int) {
cc := &compactionChecker{ cc := &compactionChecker{
expectedSamples: samples, expectedSamples: samples,
t: t, t: t,
} }
entire, err := ts.DiskStorage.MetricSamples.ForEach(&MetricSamplesDecoder{}, &AcceptAllFilter{}, cc) entire, err := ts.DiskStorage.MetricSamples.ForEach(&MetricSamplesDecoder{}, &AcceptAllFilter{}, cc)
if err != nil { if err != nil {
@ -104,20 +104,20 @@ func checkStorageSaneAndEquivalent(t *testing.T, name string, ts *TieredStorage,
type compactionTestScenario struct { type compactionTestScenario struct {
leveldbChunkSize int leveldbChunkSize int
numTimeseries int numTimeseries int
samplesPerTs int samplesPerTs int
ignoreYoungerThan time.Duration ignoreYoungerThan time.Duration
maximumMutationPoolBatch int maximumMutationPoolBatch int
minimumGroupSize int minimumGroupSize int
uncompactedChunks int uncompactedChunks int
compactedChunks int compactedChunks int
} }
func (s compactionTestScenario) run(t *testing.T) { func (s compactionTestScenario) test(t *testing.T) {
defer flag.Set("leveldbChunkSize", flag.Lookup("leveldbChunkSize").Value.String())
flag.Set("leveldbChunkSize", fmt.Sprintf("%d", s.leveldbChunkSize)) flag.Set("leveldbChunkSize", fmt.Sprintf("%d", s.leveldbChunkSize))
defer flag.Set("leveldbChunkSize", "200")
ts, closer := NewTestTieredStorage(t) ts, closer := NewTestTieredStorage(t)
defer closer.Close() defer closer.Close()
@ -133,17 +133,16 @@ func (s compactionTestScenario) run(t *testing.T) {
// 3. Compact test storage. // 3. Compact test storage.
processor := NewCompactionProcessor(&CompactionProcessorOptions{ processor := NewCompactionProcessor(&CompactionProcessorOptions{
MaximumMutationPoolBatch: s.maximumMutationPoolBatch, MaximumMutationPoolBatch: s.maximumMutationPoolBatch,
MinimumGroupSize: s.minimumGroupSize, MinimumGroupSize: s.minimumGroupSize,
}) })
defer processor.Close() defer processor.Close()
curator := NewCurator(&CuratorOptions{ curator := NewCurator(&CuratorOptions{
Stop: make(chan bool), Stop: make(chan bool),
ViewQueue: ts.ViewQueue, ViewQueue: ts.ViewQueue,
}) })
defer curator.Close() defer curator.Close()
fmt.Println("test instant:", testInstant)
err := curator.Run(s.ignoreYoungerThan, testInstant, processor, ts.DiskStorage.CurationRemarks, ts.DiskStorage.MetricSamples, ts.DiskStorage.MetricHighWatermarks, &nopCurationStateUpdater{}) err := curator.Run(s.ignoreYoungerThan, testInstant, processor, ts.DiskStorage.CurationRemarks, ts.DiskStorage.MetricSamples, ts.DiskStorage.MetricHighWatermarks, &nopCurationStateUpdater{})
if err != nil { if err != nil {
t.Fatalf("Failed to run curator: %s", err) t.Fatalf("Failed to run curator: %s", err)
@ -179,15 +178,15 @@ func TestCompaction(t *testing.T) {
// 5 | C | 11 .. 15 // 5 | C | 11 .. 15
{ {
leveldbChunkSize: 5, leveldbChunkSize: 5,
numTimeseries: 3, numTimeseries: 3,
samplesPerTs: 15, samplesPerTs: 15,
ignoreYoungerThan: time.Minute, ignoreYoungerThan: time.Minute,
maximumMutationPoolBatch: 30, maximumMutationPoolBatch: 30,
minimumGroupSize: 10, minimumGroupSize: 10,
uncompactedChunks: 9, uncompactedChunks: 9,
compactedChunks: 6, compactedChunks: 6,
}, },
// BEFORE COMPACTION: // BEFORE COMPACTION:
// //
@ -210,15 +209,15 @@ func TestCompaction(t *testing.T) {
// 10 | C | 1 .. 15 // 10 | C | 1 .. 15
{ {
leveldbChunkSize: 5, leveldbChunkSize: 5,
numTimeseries: 3, numTimeseries: 3,
samplesPerTs: 15, samplesPerTs: 15,
ignoreYoungerThan: time.Minute, ignoreYoungerThan: time.Minute,
maximumMutationPoolBatch: 30, maximumMutationPoolBatch: 30,
minimumGroupSize: 30, minimumGroupSize: 30,
uncompactedChunks: 9, uncompactedChunks: 9,
compactedChunks: 3, compactedChunks: 3,
}, },
// BUG: This case crashes! See: // BUG: This case crashes! See:
// https://github.com/prometheus/prometheus/issues/368 // https://github.com/prometheus/prometheus/issues/368
@ -239,6 +238,6 @@ func TestCompaction(t *testing.T) {
} }
for _, s := range scenarios { for _, s := range scenarios {
s.run(t) s.test(t)
} }
} }