diff --git a/internal/pkg/calendar/calendar.go b/internal/pkg/calendar/calendar.go index ab301e51..2afd0379 100644 --- a/internal/pkg/calendar/calendar.go +++ b/internal/pkg/calendar/calendar.go @@ -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 } } } diff --git a/internal/pkg/schedule/schedule.go b/internal/pkg/schedule/schedule.go index 69d84baf..62354797 100644 --- a/internal/pkg/schedule/schedule.go +++ b/internal/pkg/schedule/schedule.go @@ -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)) } } @@ -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() -} diff --git a/internal/pkg/schedule/schedule_test.go b/internal/pkg/schedule/schedule_test.go index 3af46394..c8ac6f41 100644 --- a/internal/pkg/schedule/schedule_test.go +++ b/internal/pkg/schedule/schedule_test.go @@ -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)) -}