Skip to content

Commit

Permalink
Remove unnecessary code that only complicates the implementation. The…
Browse files Browse the repository at this point in the history
… function RandomTimeInRange completely replaces the ShouldScheduleChaos functionality.
  • Loading branch information
snarlistic committed Mar 23, 2024
1 parent 541761d commit 03a90f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 83 deletions.
70 changes: 24 additions & 46 deletions internal/pkg/calendar/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,54 +89,32 @@ func RandomTimeInRange(mtbf string, startHour int, endHour int, loc *time.Locati
return []time.Time{time.Now().Add(time.Duration(24*365*10) * time.Hour)}
}

one_day, _ := time.ParseDuration("24h")
startTime := time.Now().In(loc)

// If the mtbf is bigger or equal to one day we will calculate one
// random time in the range. If not we will calculate several random
// times.
if tmptimeDuration >= one_day {
// calculate the number of minutes in the range
minutesInRange := (endHour - startHour) * 60

// calculate a random minute-offset in range [0, minutesInRange)
for {
//time range should be twice of the input mean time between failure value
timeDuration := tmptimeDuration * 2
//compute random offset time
mtbfEndTime := startTime.Add(timeDuration)
subSecond := int64(mtbfEndTime.Sub(startTime) / time.Second)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randMinuteOffset := r.Intn(minutesInRange)
offsetDuration := time.Duration(randMinuteOffset) * time.Minute

// Add the minute offset to the start of the range to get a random
// time within the range
year, month, date := time.Now().Date()
rangeStart := time.Date(year, month, date, startHour, 0, 0, 0, loc)
times = append(times, rangeStart.Add(offsetDuration))
return times
} else {
startTime := time.Now().In(loc)

for {
//time range should be twice of the input mean time between failure value
timeDuration := tmptimeDuration * 2
//compute random offset time
mtbfEndTime := startTime.Add(timeDuration)
subSecond := int64(mtbfEndTime.Sub(startTime) / time.Second)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randSecondOffset := r.Int63n(subSecond)
randCalTime := startTime.Add(time.Duration(randSecondOffset) * time.Second)

// compute randSecondOffset between start and end hour
year, month, date := startTime.Date()
todayEndTime := time.Date(year, month, date, endHour, 0, 0, 0, loc)
todayStartTime := time.Date(year, month, date, startHour, 0, 0, 0, loc)
if startTime.Before(todayStartTime) { // now is earlier then start hour, only for test pass, normal process won't run into this condition
return []time.Time{todayStartTime}
}
if randCalTime.Before(todayEndTime) { // time offset before today's endHour
glog.V(1).Infof("RandomTimeInRange calculate time %s", randCalTime)
times = append(times, randCalTime)
// Move start time up to the calculated random time
startTime = randCalTime
} else {
return times
}
randSecondOffset := r.Int63n(subSecond)
randCalTime := startTime.Add(time.Duration(randSecondOffset) * time.Second)

// compute randSecondOffset between start and end hour
year, month, date := startTime.Date()
todayEndTime := time.Date(year, month, date, endHour, 0, 0, 0, loc)
todayStartTime := time.Date(year, month, date, startHour, 0, 0, 0, loc)
if startTime.Before(todayStartTime) { // now is earlier then start hour, only for test pass, normal process won't run into this condition
return []time.Time{todayStartTime}
}
if randCalTime.Before(todayEndTime) { // time offset before today's endHour
glog.V(1).Infof("RandomTimeInRange calculate time %s", randCalTime)
times = append(times, randCalTime)
// Move start time up to the calculated random time
startTime = randCalTime
} else {
return times
}
}
}
27 changes: 3 additions & 24 deletions internal/pkg/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,9 @@ func New() (*Schedule, error) {
}

for _, victim := range victims {
mtbf := victim.Mtbf()
parsed_mtbf, err := calendar.ParseMtbf(mtbf)
if err != nil {
glog.Errorf("error parsing customized mtbf for %s/%s in namespace %s - %s: %v", victim.Kind(), victim.Name(), victim.Namespace(), mtbf, err)
continue
}
killtimes := CalculateKillTimes(mtbf)
one_day, _ := time.ParseDuration("24h")
// If the parsed mtbf value is less than one day we want to add the calculated kill times no matter
// what and otherwise we use probability to decide if we will schedule the calculated kill time.
if parsed_mtbf < one_day || ShouldScheduleChaos(float64(parsed_mtbf / one_day)) {
for _, killtime := range killtimes {
schedule.Add(chaos.New(killtime, victim))
}
killtimes := CalculateKillTimes(victim.Mtbf())
for _, killtime := range killtimes {
schedule.Add(chaos.New(killtime, victim))
}
}

Expand All @@ -111,13 +100,3 @@ func CalculateKillTimes(mtbf string) []time.Time {
}
return calendar.RandomTimeInRange(mtbf, config.StartHour(), config.EndHour(), loc)
}

func ShouldScheduleChaos(mtbf float64) bool {
if config.DebugEnabled() && config.DebugForceShouldKill() {
return true
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
probability := 1 / mtbf
return probability > r.Float64()
}
13 changes: 0 additions & 13 deletions internal/pkg/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,3 @@ func TestCalculateKillTimeNow(t *testing.T) {
assert.WithinDuration(t, killtimes[0], time.Now(), time.Second*time.Duration(60))
config.SetDefaults()
}

func TestShouldScheduleChaosNow(t *testing.T) {
config.SetDefaults()
viper.SetDefault(param.DebugEnabled, true)
viper.SetDefault(param.DebugForceShouldKill, true)
assert.True(t, ShouldScheduleChaos(100000000000))
config.SetDefaults()
}

func TestShouldScheduleChaosMtbf(t *testing.T) {
assert.False(t, ShouldScheduleChaos(100000000000))
assert.True(t, ShouldScheduleChaos(1))
}

0 comments on commit 03a90f0

Please sign in to comment.