Skip to content

Commit f7e674b

Browse files
authored
Merge pull request #647 from xrmzju/master
✨ add 10% jitter to ResyncPeriod
2 parents 3817de0 + 5b53c39 commit f7e674b

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

pkg/cache/cache.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ type Options struct {
9393
// Mapper is the RESTMapper to use for mapping GroupVersionKinds to Resources
9494
Mapper meta.RESTMapper
9595

96-
// Resync is the resync period. Defaults to defaultResyncTime.
96+
// Resync is the base frequency the informers are resynced.
97+
// Defaults to defaultResyncTime.
98+
// A 10 percent jitter will be added to the Resync period between informers
99+
// So that all informers will not send list requests simultaneously.
97100
Resync *time.Duration
98101

99102
// Namespace restricts the cache's ListWatch to the desired namespace

pkg/cache/internal/informers_map.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package internal
1818

1919
import (
2020
"fmt"
21+
"math/rand"
2122
"sync"
2223
"time"
2324

@@ -92,7 +93,9 @@ type specificInformersMap struct {
9293
// stop is the stop channel to stop informers
9394
stop <-chan struct{}
9495

95-
// resync is the frequency the informers are resynced
96+
// resync is the base frequency the informers are resynced
97+
// a 10 percent jitter will be added to the resync period between informers
98+
// so that all informers will not send list requests simultaneously.
9699
resync time.Duration
97100

98101
// mu guards access to the map
@@ -188,7 +191,7 @@ func (ip *specificInformersMap) addInformerToMap(gvk schema.GroupVersionKind, ob
188191
if err != nil {
189192
return nil, false, err
190193
}
191-
ni := cache.NewSharedIndexInformer(lw, obj, ip.resync, cache.Indexers{
194+
ni := cache.NewSharedIndexInformer(lw, obj, resyncPeriod(ip.resync)(), cache.Indexers{
192195
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
193196
})
194197
i := &MapEntry{
@@ -274,3 +277,14 @@ func createUnstructuredListWatch(gvk schema.GroupVersionKind, ip *specificInform
274277
},
275278
}, nil
276279
}
280+
281+
// resyncPeriod returns a function which generates a duration each time it is
282+
// invoked; this is so that multiple controllers don't get into lock-step and all
283+
// hammer the apiserver with list requests simultaneously.
284+
func resyncPeriod(resync time.Duration) func() time.Duration {
285+
return func() time.Duration {
286+
// the factor will fall into [0.9, 1.1)
287+
factor := rand.Float64()/5.0 + 0.9
288+
return time.Duration(float64(resync.Nanoseconds()) * factor)
289+
}
290+
}

pkg/manager/manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ type Options struct {
111111
// reconciled. A lower period will correct entropy more quickly, but reduce
112112
// responsiveness to change if there are many watched resources. Change this
113113
// value only if you know what you are doing. Defaults to 10 hours if unset.
114+
// there will a 10 percent jitter between the SyncPeriod of all controllers
115+
// so that all controllers will not send list requests simultaneously.
114116
SyncPeriod *time.Duration
115117

116118
// LeaderElection determines whether or not to use leader election when

0 commit comments

Comments
 (0)