Skip to content

Commit

Permalink
Try to fix randomness issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
bob committed Mar 8, 2018
1 parent 694ba37 commit 1b4c795
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion helpers.go
Expand Up @@ -11,5 +11,5 @@ var R = rand.New(rand.NewSource(time.Now().UnixNano()))
// ApplyJitter return a random number
func ApplyJitter(input int) (output int) {
deviation := int(0.25 * float64(input))
return input - deviation + rand.Intn(2*deviation)
return input - deviation + R.Intn(2*deviation)
}
4 changes: 2 additions & 2 deletions helpers_test.go
Expand Up @@ -9,7 +9,7 @@ func TestApplyJitter(t *testing.T) {
R = rand.New(rand.NewSource(0))

var output = ApplyJitter(100)
if output != 106 {
t.Errorf("ApplyJitter, expected 10 got %d", output)
if output != 99 {
t.Errorf("ApplyJitter, expected 99 got %d", output)
}
}
6 changes: 5 additions & 1 deletion main.go
Expand Up @@ -67,6 +67,7 @@ var (
revision string
buildDate string
goVersion = runtime.Version()
randomEstafette = rand.New(rand.NewSource(time.Now().UnixNano()))
)

func init() {
Expand Down Expand Up @@ -187,7 +188,10 @@ func getCurrentNodeState(node *apiv1.Node) (state GKEPreemptibleKillerState) {
// getDesiredNodeState define the state of the node, update node annotations if not present
func getDesiredNodeState(k KubernetesClient, node *apiv1.Node) (state GKEPreemptibleKillerState, err error) {
t := time.Unix(*node.Metadata.CreationTimestamp.Seconds, 0)
expiryDatetime := t.Add(24*time.Hour - time.Duration(*drainTimeout)*time.Second - time.Duration(rand.Int63n(24-12))*time.Hour).UTC()
drainTimeoutTime := time.Duration(*drainTimeout)*time.Second
// 43200 = 12h * 60m * 60s
randomTimeBetween0to12 := time.Duration(randomEstafette.Intn((43200)-*drainTimeout))*time.Second
expiryDatetime := t.Add(12*time.Hour + drainTimeoutTime + randomTimeBetween0to12).UTC()

state.ExpiryDatetime = expiryDatetime.Format(time.RFC3339)

Expand Down
19 changes: 15 additions & 4 deletions main_test.go
@@ -1,6 +1,7 @@
package main

import (
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -63,20 +64,30 @@ func TestGetCurrentNodeState(t *testing.T) {
func TestGetDesiredNodeState(t *testing.T) {
zerolog.SetGlobalLevel(zerolog.Disabled)

creationTimestamp := time.Date(2017, 11, 11, 11, 11, 11, 0, time.UTC).Unix()
creationTimestamp := time.Date(2017, 11, 11, 12, 00, 00, 0, time.UTC)
creationTimestampUnix := creationTimestamp.Unix()
creationTimestamp12HoursLater := creationTimestamp.Add(12*time.Hour)
creationTimestamp24HoursLater := creationTimestamp.Add(24*time.Hour)

node := &apiv1.Node{
Metadata: &metav1.ObjectMeta{
Name: k8s.String("node-1"),
CreationTimestamp: &metav1.Time{Seconds: &creationTimestamp},
CreationTimestamp: &metav1.Time{Seconds: &creationTimestampUnix},
},
}

client := FakeNewKubernetesClient()

state, _ := getDesiredNodeState(client, node)
stateTS,_ := time.Parse(time.RFC3339,state.ExpiryDatetime)

if state.ExpiryDatetime != "2017-11-12T04:11:11Z" {
t.Errorf("Expect expiry date time to be 2017-11-12T04:11:11Z, instead got %s", state.ExpiryDatetime)
if !creationTimestamp12HoursLater.Before(stateTS) && !creationTimestamp24HoursLater.After(stateTS) {
t.Errorf("Expect expiry date time to be between 12 and 24h after the creation date %s, instead got %s", creationTimestamp, state.ExpiryDatetime)
}

randomEstafette = rand.New(rand.NewSource(0))
stateWithPreseed, _ := getDesiredNodeState(client, node)
if stateWithPreseed.ExpiryDatetime != "2017-11-12T11:27:54Z" {
t.Errorf("Expect expiry date time to be 2017-11-12T11:27:54Z, instead got %s", stateWithPreseed.ExpiryDatetime)
}
}

0 comments on commit 1b4c795

Please sign in to comment.