@@ -169,7 +169,7 @@ func newWatchCache(
169
169
getAttrsFunc : getAttrsFunc ,
170
170
cache : make ([]* watchCacheEvent , defaultLowerBoundCapacity ),
171
171
lowerBoundCapacity : defaultLowerBoundCapacity ,
172
- upperBoundCapacity : defaultUpperBoundCapacity ,
172
+ upperBoundCapacity : capacityUpperBound ( eventFreshDuration ) ,
173
173
startIndex : 0 ,
174
174
endIndex : 0 ,
175
175
store : newStoreIndexer (indexers ),
@@ -189,6 +189,30 @@ func newWatchCache(
189
189
return wc
190
190
}
191
191
192
+ // capacityUpperBound denotes the maximum possible capacity of the watch cache
193
+ // to which it can resize.
194
+ func capacityUpperBound (eventFreshDuration time.Duration ) int {
195
+ if eventFreshDuration <= DefaultEventFreshDuration {
196
+ return defaultUpperBoundCapacity
197
+ }
198
+ // eventFreshDuration determines how long the watch events are supposed
199
+ // to be stored in the watch cache.
200
+ // In very high churn situations, there is a need to store more events
201
+ // in the watch cache, hence it would have to be upsized accordingly.
202
+ // Because of that, for larger values of eventFreshDuration, we set the
203
+ // upper bound of the watch cache's capacity proportionally to the ratio
204
+ // between eventFreshDuration and DefaultEventFreshDuration.
205
+ // Given that the watch cache size can only double, we round up that
206
+ // proportion to the next power of two.
207
+ exponent := int (math .Ceil ((math .Log2 (eventFreshDuration .Seconds () / DefaultEventFreshDuration .Seconds ()))))
208
+ if maxExponent := int (math .Floor ((math .Log2 (math .MaxInt32 / defaultUpperBoundCapacity )))); exponent > maxExponent {
209
+ // Making sure that the capacity's upper bound fits in a 32-bit integer.
210
+ exponent = maxExponent
211
+ klog .Warningf ("Capping watch cache capacity upper bound to %v" , defaultUpperBoundCapacity << exponent )
212
+ }
213
+ return defaultUpperBoundCapacity << exponent
214
+ }
215
+
192
216
// Add takes runtime.Object as an argument.
193
217
func (w * watchCache ) Add (obj interface {}) error {
194
218
object , resourceVersion , err := w .objectToVersionedRuntimeObject (obj )
0 commit comments