Skip to content

Fix flaky leader election healthz tests - #6022

Merged
jetstack-bot merged 1 commit into
cert-manager:masterfrom
wallrj:fix-flaky-leader-election-healthz-tests
May 5, 2023
Merged

jetstack-bot merged 1 commit into
cert-manager:masterfrom
wallrj:fix-flaky-leader-election-healthz-tests

Conversation

@wallrj

@wallrj wallrj commented May 5, 2023 •

Copy link
Copy Markdown
Member

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

NONE

@jetstack-bot jetstack-bot added release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 5, 2023
@wallrj
wallrj requested a review from irbekrm May 5, 2023 13:13
@wallrj wallrj mentioned this pull request May 5, 2023
HolderIdentity: localIdentity,
},
},
resourceLock: &fakeResourceLock{},

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/healthz/healthz_test.go Outdated
Comment on lines +338 to +342
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())
}

@wallrj wallrj May 5, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/healthz/healthz_test.go Resolved
Comment thread pkg/healthz/healthz_test.go Outdated
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())

@inteon inteon May 5, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

https://github.com/kubernetes/kubernetes/blob/7e25f1232a9f89875641431ae011c916f0376c57/staging/src/k8s.io/client-go/tools/leaderelection/leaderelection.go#L372-L380

So yes, that would probably also work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@wallrj
wallrj force-pushed the fix-flaky-leader-election-healthz-tests branch from 7ea6d13 to 83ce550 Compare May 5, 2023 14:56
@jetstack-bot jetstack-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 5, 2023

@inteon inteon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
/lgtm
Thank you for the new comments, that makes your solution a lot easier to understand!

@jetstack-bot jetstack-bot added the lgtm Indicates that a PR is ready to be merged. label May 5, 2023
@jetstack-bot

Copy link
Copy Markdown
Contributor

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

// See the function documentation above for a fuller explanation.
if o.record.HolderIdentity == remoteIdentity {
o.record.LeaderTransitions++
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wallrj

wallrj commented May 5, 2023

Copy link
Copy Markdown
Member Author

/hold until I've checked this works with new client-go....

@jetstack-bot jetstack-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 5, 2023
@irbekrm

irbekrm commented May 5, 2023

Copy link
Copy Markdown
Contributor

/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?

@wallrj

wallrj commented May 5, 2023

Copy link
Copy Markdown
Member Author

/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?

@irbe I did that here: #6020 and the Healthz tests passed.

@wallrj

wallrj commented May 5, 2023

Copy link
Copy Markdown
Member Author

/unhold

@jetstack-bot jetstack-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 5, 2023
@jetstack-bot
jetstack-bot merged commit ab44158 into cert-manager:master May 5, 2023
@jetstack-bot jetstack-bot added this to the v1.12 milestone May 5, 2023
@wallrj

wallrj commented May 5, 2023

Copy link
Copy Markdown
Member Author

Also tested locally

$ ~/go/bin/go test ./pkg/healthz/... -count 100
ok      github.com/cert-manager/cert-manager/pkg/healthz        88.434s

@wallrj
wallrj deleted the fix-flaky-leader-election-healthz-tests branch May 5, 2023 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants