Repository navigation
Fix flaky leader election healthz tests - #6022
jetstack-bot merged 1 commit into
Conversation
| HolderIdentity: localIdentity, | ||
| }, | ||
| }, | ||
| resourceLock: &fakeResourceLock{}, |
There was a problem hiding this comment.
This change is not related to the fix, but I think it's easier to read, in that supplying an empty resource lock here simulates the local node initially winning the election and holding it for the duration of the test.
| livezURL := "http://" + l.Addr().String() + "/livez/leaderElection" | ||
|
|
||
| const leaderElectionHealthzAdaptorTimeout = time.Millisecond | ||
| const leaderElectionHealthzAdaptorTimeout = 0 |
There was a problem hiding this comment.
This was not related to the flaky tests, but I think it makes the test easier to read.
The original 1ms value was arbitrary.
The intent is simply to make healthz adaptor start reporting an error immediately after the current lease has expired.
In production, this defaults to 20s beyond the lease expiry deadline, which allows enough time for another node to take over the lease.
| "renewDeadline", renewDeadline, | ||
| "retryPeriod", retryPeriod, | ||
| ) | ||
| tc.in.resourceLock.lockName = t.Name() |
There was a problem hiding this comment.
This is not related to the flaky test,
but when the test was panicing, it makes it clearer in the logs which test had paniced.
| if o.record.HolderIdentity == remoteIdentity { | ||
| // Always return unique leader election record bytes, | ||
| // to simulate an always updated remote leader election record. | ||
| lerByte = []byte(uuid.NewUUID()) | ||
| } |
There was a problem hiding this comment.
This is the key part.
The leader election library treats the leader election record (whether it be a lease a configmap or something else) as an opaque blob.
The content of the record is not considered.
Only whether the bytes of the record have changed since it was last observed.
So here I've made it so that the bytes always change are always random (uuid4) when the test is simulating a remote leader.
That way the leader election library always thinks that remote leader is active.
The problem in the original code was that the bytes were not changing between successive Gets and with the new version of client-go, the leader election library is much quicker to attempt to acquire the lease in that case.
| if o.record.HolderIdentity == remoteIdentity { | ||
| // Always return unique leader election record bytes, | ||
| // to simulate an always updated remote leader election record. | ||
| lerByte = []byte(uuid.NewUUID()) |
There was a problem hiding this comment.
if o.record == nil {
err := errors.NewNotFound(schema.ParseGroupResource("configmap"), "foo")
return nil, nil, err
}
// If the current leader is not the local node, return a unique leader election record.
// This simulates the case where the current leader is active.
if o.record.HolderIdentity != localIdentity {
o.record.RenewTime = metav1.NewTime(o.record.RenewTime.Add(time.Second))
}
lerByte, err := json.Marshal(*o.record)
if err != nil {
return nil, nil, err
}@wallrj could we do something like this instead? It makes it less abstract for me & easier to understand.
There was a problem hiding this comment.
I considered that, but had assumed that it would trigger the OnNewLeader callback each time, which I wanted to avoid doing.
UUID is reasonable here, and aligns with the comments in the leader election source code.
// > LeaderElectionRecord is the record that is stored in the leader election annotation.
// > This information should be used for observational purposes only and could be replaced
// > with a random string (e.g. UUID) with only slight modification of this code.
// > -- https://github.com/kubernetes/kubernetes/blob/7e25f1232a9f89875641431ae011c916f0376c57/staging/src/k8s.io/client-go/tools/leaderelection/re
But looking again at the code, I see that that callback is triggered only by the change of the leader name:
So yes, that would probably also work.
There was a problem hiding this comment.
Great, I don't think I care much about which becomes the final solution. I do think we need to explain better in the comments why we do what we do.
Fixes test flakes caused by the local node taking over leadership, because it did not observe any change in the leader election record held by the remote node. Signed-off-by: Richard Wall <[email protected]>
7ea6d13 to
83ce550
Compare
inteon
left a comment
There was a problem hiding this comment.
/approve
/lgtm
Thank you for the new comments, that makes your solution a lot easier to understand!
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: inteon, wallrj The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
| // See the function documentation above for a fuller explanation. | ||
| if o.record.HolderIdentity == remoteIdentity { | ||
| o.record.LeaderTransitions++ | ||
| } |
There was a problem hiding this comment.
This is Tim's suggested change.
And I added some locking too, in case multiple threads should happen to try to simultaneously update the state of the fake resource lock....although I don't think that is ever the case in the current tests.
|
/hold until I've checked this works with new client-go.... |
@wallrj do you want me to rebase the kube library update off this PR and see that it passes in CI? |
|
/unhold |
|
Also tested locally |
Simulate a remote leader that always updates its lease
Fixes test flakes caused by the local node taking over leadership, because it did not observe any change in the leader election record held by the remote node. These flakey tests were introduced in: #5962
The test flakes only became apparent during the upgrade of k8s.io/client-go in #5961.
Which includes the following bug fix: client-go: fix the wait time for trying to acquire the leader lease
To demonstrate that this will indeed fix the flakes, I created a PR where this change is rebased on #5961
/kind bug