-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce staggered exit when caps change
This patch changes the behavior of how the pod is exited when the capabilities have changed. Instead of all the replicas exiting at the same time, the exits are staggered. If the pod is the leader or no leader election is configured, the pod is exited after a brief delay. Otherwise the pod is exited immediately. This should allow the non-leaders to come back online before the leader is exited, enabling one of the restarted pods to take over as the leader when it exits.
- Loading branch information
Showing
17 changed files
with
209 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2024 Broadcom. All Rights Reserved. | ||
// Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc. | ||
// and/or its subsidiaries. | ||
|
||
package exit | ||
|
||
import ( | ||
"context" | ||
"math/rand/v2" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
pkgcfg "github.com/vmware-tanzu/vm-operator/pkg/config" | ||
) | ||
|
||
// ExitFn is used in testing to assert the Exit() function is called. | ||
// The default value is a no-op. | ||
var ExitFn func(exitDelay time.Duration) | ||
|
||
func init() { | ||
ExitFn = func(_ time.Duration) {} | ||
} | ||
|
||
// Exit will exit the pod immediately if it is not the leader, otherwise if | ||
// the pod is the leader or leader election is not enabled, the pod will exit | ||
// after a brief delay. | ||
func Exit( | ||
ctx context.Context, | ||
reason string, | ||
elected <-chan struct{}) { | ||
|
||
var exitDelay time.Duration | ||
select { | ||
case <-elected: | ||
// When the leader or when there is no leader election, delay the | ||
// exit up to some random amount of time. | ||
n := rand.Float64() //nolint:gosec | ||
exitDelay = time.Duration(n * float64(pkgcfg.FromContext(ctx).ExitDelay)) | ||
default: | ||
// When not the leader, exit immediately. This should allow one of | ||
// the non-leader pods to come back online to become the leader when | ||
// the leader pod exits. | ||
} | ||
if exitDelay > 0 { | ||
time.Sleep(exitDelay) | ||
} | ||
|
||
logr.FromContextOrDiscard(ctx). | ||
Info("Exiting pod", "exitDelay", exitDelay, "reason", reason) | ||
|
||
ExitFn(exitDelay) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// © Broadcom. All Rights Reserved. | ||
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package exit_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestExit(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Exit Suite") | ||
} |
Oops, something went wrong.