mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-17 20:00:07 +00:00
feat: improve the backoff calculation to o(1)
This commit is contained in:
@@ -239,15 +239,11 @@ func (bq *backoffQueue) calculateBackoffDuration(podInfo *framework.QueuedPodInf
|
||||
return 0
|
||||
}
|
||||
|
||||
duration := bq.podInitialBackoff
|
||||
for i := 1; i < podInfo.Attempts; i++ {
|
||||
// Use subtraction instead of addition or multiplication to avoid overflow.
|
||||
if duration > bq.podMaxBackoff-duration {
|
||||
return bq.podMaxBackoff
|
||||
}
|
||||
duration += duration
|
||||
shift := podInfo.Attempts - 1
|
||||
if bq.podInitialBackoff > bq.podMaxBackoff>>shift {
|
||||
return bq.podMaxBackoff
|
||||
}
|
||||
return duration
|
||||
return time.Duration(bq.podInitialBackoff << shift)
|
||||
}
|
||||
|
||||
func (bq *backoffQueue) popAllBackoffCompletedWithQueue(logger klog.Logger, queue *heap.Heap[*framework.QueuedPodInfo]) []*framework.QueuedPodInfo {
|
||||
|
||||
@@ -48,6 +48,13 @@ func TestBackoffQueue_calculateBackoffDuration(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "normal",
|
||||
initialBackoffDuration: 3 * time.Nanosecond,
|
||||
maxBackoffDuration: 1000 * time.Nanosecond,
|
||||
podInfo: &framework.QueuedPodInfo{Attempts: 5},
|
||||
want: 48 * time.Nanosecond, // 3 * 2^4 = 48
|
||||
},
|
||||
{
|
||||
name: "hitting max backoff duration",
|
||||
initialBackoffDuration: 1 * time.Nanosecond,
|
||||
maxBackoffDuration: 32 * time.Nanosecond,
|
||||
podInfo: &framework.QueuedPodInfo{Attempts: 16},
|
||||
|
||||
Reference in New Issue
Block a user