Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Find random corruptions #1009

Merged
merged 8 commits into from
Aug 24, 2018
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions mdata/cache/ccache_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cache
import (
"errors"
"fmt"
"math/rand"
"sort"
"testing"
"time"

"github.com/grafana/metrictank/mdata/cache/accnt"
"github.com/grafana/metrictank/mdata/chunk"
Expand Down Expand Up @@ -203,6 +205,73 @@ func TestCorruptionCase1(t *testing.T) {
})
}

func getRandomNumber(min, max int) int {
return rand.Intn(max-min) + min
}

// getRandomRange returns a range start-end so that
// end >= start and both numbers drawn from [min, max)
func getRandomRange(min, max int) (int, int) {
start := getRandomNumber(min, max)
end := getRandomNumber(start, max)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i just realized that this randomness is skewed to the end of the range because, assuming the distribution of start across min - max is even, and considering that end always must be >= start, then the middle of the resulting range returned from getRandomRange() at average will above the middle of the range min-max.
that's not really a problem, just a note

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could keep a counter, and every other time, draw an end first

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, we could also just get two random numbers and return the lower one as start and the higher as end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's elegant I like that.

return start, end
}

func TestCorruptionCase2(t *testing.T) {
rand.Seed(time.Now().Unix())
_, ccm := getCCM()
iterations := 100000

// 100 chunks, first t0=t10, last is t0=1000
chunks := generateChunks(t, 10, 100, 10)

var opAdd, opAddRange, opDel, opDelRange int
var adds, dels int

for i := 0; i < iterations; i++ {
// 0 = Add
// 1 = AddRange
// 2 = Del
// 2 = Del range (via multi del cals)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be 3

action := getRandomNumber(0, 4)
switch action {
case 0:
chunk := getRandomNumber(0, 100)
t.Logf("adding chunk %d", chunk)
ccm.Add(0, chunks[chunk])
opAdd++
adds++
case 1:
from, to := getRandomRange(0, 100)
t.Logf("adding range %d-%d", from, to)
ccm.AddRange(0, chunks[from:to])
adds += (to - from)
opAddRange++
case 2:
chunk := getRandomNumber(0, 100)
t.Logf("deleting chunk %d", chunk)
ccm.Del(chunks[chunk].Ts) // note: chunk may not exist
opDel++
dels++
case 3:
from, to := getRandomRange(0, 100)
for chunk := from; chunk < to; chunk++ {
t.Logf("deleting chunk %d", chunk)
ccm.Del(chunks[chunk].Ts) // note: chunk may not exist
}
opDelRange++
dels += (to - from)
}

if err := verifyCcm(ccm); err != nil {
t.Fatal(err)
}
}

fmt.Printf("operations: add %d - addRange %d - del %d - delRange %d\n", opAdd, opAddRange, opDel, opDelRange)
fmt.Printf("total chunk adds %d - total chunk deletes %d\n", adds, dels)
}

// verifyCcm verifies the integrity of a CCacheMetric
// it assumes that all itergens are span-aware
func verifyCcm(ccm *CCacheMetric) error {
Expand Down