This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Find random corruptions #1009
Merged
Merged
Find random corruptions #1009
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8fb9f88
find random corruptions
replay 4fc9b84
more reasonable of number of iterations (by default)
Dieterbe ed8e3e6
t.Logf is your friend
Dieterbe 7809098
simplify getRandomRange
Dieterbe 7acd2c4
comments
Dieterbe 0752325
add "delete range" and clearer stats
Dieterbe 028a275
minor improvements
replay d6e6b05
verify which chunks should and should not be cached
Dieterbe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
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 { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
acrossmin - max
is even, and considering thatend
always must be>= start
, then the middle of the resulting range returned fromgetRandomRange()
at average will above the middle of the rangemin-max
.that's not really a problem, just a note
There was a problem hiding this comment.
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
firstThere was a problem hiding this comment.
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 asend
There was a problem hiding this comment.
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.