Skip to content

Commit c8138ce

Browse files
sambhav-jain-16dims
authored andcommitted
add initial splay and max jitter factors
1 parent b5f55c0 commit c8138ce

2 files changed

Lines changed: 74 additions & 6 deletions

File tree

manager/container.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ import (
4747
"k8s.io/utils/clock"
4848
)
4949

50+
const jitterDefault = 1.0
51+
5052
// Housekeeping interval.
5153
var enableLoadReader = flag.Bool("enable_load_reader", false, "Whether to enable cpu load reader")
5254
var HousekeepingInterval = flag.Duration("housekeeping_interval", 1*time.Second, "Interval between container housekeepings")
55+
var InitialSplayFactor = flag.Float64("initial_splay_factor", jitterDefault, "Factor for the initial splay b/w the container housekeepings, default is 1.0. If negative value is passed, the value will be reset to default")
56+
var JitterFactor = flag.Float64("jitter_factor", jitterDefault, "Factor for the jitters after the initial splay b/w the container housekeepings, default is 1.0. If negative value is passed, the value will be reset to default")
5357

5458
// TODO: replace regular expressions with something simpler, such as strings.Split().
5559
// cgroup type chosen to fetch the cgroup path of a process.
@@ -91,6 +95,9 @@ type containerData struct {
9195
housekeepingInterval time.Duration
9296
maxHousekeepingInterval time.Duration
9397
allowDynamicHousekeeping bool
98+
firstHousekeeping bool
99+
initialSplayFactor float64
100+
jitterFactor float64
94101
infoLastUpdatedTime atomicTime // Unix nano
95102
statsLastUpdatedTime atomicTime // Unix nano
96103
lastErrorTime time.Time
@@ -121,11 +128,11 @@ type containerData struct {
121128
}
122129

123130
// jitter returns a time.Duration between duration and duration + maxFactor * duration,
124-
// to allow clients to avoid converging on periodic behavior. If maxFactor is 0.0, a
125-
// suggested default value will be chosen.
131+
// to allow clients to avoid converging on periodic behavior. If maxFactor is 0.0, no
132+
// jitter is applied. If maxFactor is negative, a suggested default value will be chosen.
126133
func jitter(duration time.Duration, maxFactor float64) time.Duration {
127-
if maxFactor <= 0.0 {
128-
maxFactor = 1.0
134+
if maxFactor < 0.0 {
135+
maxFactor = jitterDefault
129136
}
130137
wait := duration + time.Duration(rand.Float64()*maxFactor*float64(duration))
131138
return wait
@@ -458,6 +465,9 @@ func newContainerData(containerName string, memoryCache *memory.InMemoryCache, h
458465
housekeepingInterval: *HousekeepingInterval,
459466
maxHousekeepingInterval: maxHousekeepingInterval,
460467
allowDynamicHousekeeping: allowDynamicHousekeeping,
468+
firstHousekeeping: true,
469+
initialSplayFactor: *InitialSplayFactor,
470+
jitterFactor: *JitterFactor,
461471
logUsage: logUsage,
462472
loadAvg: -1.0, // negative value indicates uninitialized.
463473
loadDAvg: -1.0, // negative value indicates uninitialized.
@@ -491,7 +501,6 @@ func newContainerData(containerName string, memoryCache *memory.InMemoryCache, h
491501
cont.summaryReader = nil
492502
klog.V(5).Infof("Failed to create summary reader for %q: %v", ref.Name, err)
493503
}
494-
495504
return cont, nil
496505
}
497506

@@ -519,7 +528,13 @@ func (cd *containerData) nextHousekeepingInterval() time.Duration {
519528
}
520529
}
521530

522-
return jitter(cd.housekeepingInterval, 1.0)
531+
jitterFactor := cd.jitterFactor
532+
if cd.firstHousekeeping {
533+
jitterFactor = cd.initialSplayFactor
534+
cd.firstHousekeeping = false
535+
}
536+
537+
return jitter(cd.housekeepingInterval, jitterFactor)
523538
}
524539

525540
// TODO(vmarmol): Implement stats collecting as a custom collector.

manager/container_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,56 @@ func TestGetCgroupPath(t *testing.T) {
538538
})
539539
}
540540
}
541+
542+
func TestNextHousekeepingInterval(t *testing.T) {
543+
base := 1 * time.Second
544+
tests := []struct {
545+
name string
546+
splayFactor float64
547+
jitterFactor float64
548+
firstHousekeeping bool
549+
expectedMin time.Duration
550+
expectedMax time.Duration
551+
}{
552+
{
553+
name: "first housekeeping uses splay factor",
554+
splayFactor: 2.0,
555+
jitterFactor: 1.0,
556+
firstHousekeeping: true,
557+
expectedMin: base,
558+
expectedMax: base + time.Duration(2.0*float64(base)),
559+
},
560+
{
561+
name: "subsequent housekeeping uses jitter factor",
562+
splayFactor: 1.0,
563+
jitterFactor: 0.5,
564+
firstHousekeeping: false,
565+
expectedMin: base,
566+
expectedMax: base + time.Duration(0.5*float64(base)),
567+
},
568+
{
569+
name: "zero jitter factor results in no jitter",
570+
splayFactor: 1.0,
571+
jitterFactor: 0.0,
572+
firstHousekeeping: false,
573+
expectedMin: base,
574+
expectedMax: base,
575+
},
576+
}
577+
578+
for _, tc := range tests {
579+
t.Run(tc.name, func(t *testing.T) {
580+
cd, _, _, _ := newTestContainerData(t)
581+
cd.allowDynamicHousekeeping = false
582+
cd.housekeepingInterval = base
583+
cd.firstHousekeeping = tc.firstHousekeeping
584+
cd.initialSplayFactor = tc.splayFactor
585+
cd.jitterFactor = tc.jitterFactor
586+
587+
got := cd.nextHousekeepingInterval()
588+
assert.GreaterOrEqual(t, got, tc.expectedMin)
589+
assert.LessOrEqual(t, got, tc.expectedMax)
590+
assert.False(t, cd.firstHousekeeping)
591+
})
592+
}
593+
}

0 commit comments

Comments
 (0)