| 1 |
package txn |
| 2 |
|
| 3 |
import ( |
| 4 |
mrand "math/rand" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
var chaosEnabled = false |
| 9 |
var chaosSetting Chaos |
| 10 |
|
| 11 |
|
| 12 |
type Chaos struct { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
KillChance float64 |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
SlowdownChance float64 |
| 22 |
Slowdown time.Duration |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
Breakpoint string |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func SetChaos(c Chaos) { |
| 31 |
chaosSetting = c |
| 32 |
chaosEnabled = c.KillChance > 0 || c.SlowdownChance > 0 |
| 33 |
} |
| 34 |
|
| 35 |
func chaos(bpname string) { |
| 36 |
if !chaosEnabled { |
| 37 |
return |
| 38 |
} |
| 39 |
switch chaosSetting.Breakpoint { |
| 40 |
case "", bpname: |
| 41 |
kc := chaosSetting.KillChance |
| 42 |
if kc > 0 && mrand.Intn(1000) < int(kc*1000) { |
| 43 |
panic(chaosError{}) |
| 44 |
} |
| 45 |
if bpname == "insert" { |
| 46 |
return |
| 47 |
} |
| 48 |
sc := chaosSetting.SlowdownChance |
| 49 |
if sc > 0 && mrand.Intn(1000) < int(sc*1000) { |
| 50 |
time.Sleep(chaosSetting.Slowdown) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
type chaosError struct{} |
| 56 |
|
| 57 |
func (f *flusher) handleChaos(err *error) { |
| 58 |
v := recover() |
| 59 |
if v == nil { |
| 60 |
return |
| 61 |
} |
| 62 |
if _, ok := v.(chaosError); ok { |
| 63 |
f.debugf("Killed by chaos!") |
| 64 |
*err = ErrChaos |
| 65 |
return |
| 66 |
} |
| 67 |
panic(v) |
| 68 |
} |