Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyring: fixes for keyring replication on cluster join #14987

Merged
merged 4 commits into from
Oct 21, 2022

Conversation

tgross
Copy link
Member

@tgross tgross commented Oct 20, 2022

Fixes #14981. Note to reviewers: these are definitely bugs but I don't have 100% confidence we've solved the problem because there's a log line missing in the reports from both the issue author and our internal user that I would expect to see. So I'm still trying to repro exactly, but it's worth getting some early eyes on this PR anyways.

  • Don't unblock early if rate limit burst exceeded. The rate limiter returns an error and unblocks early if its burst limit is exceeded (unless the burst limit is Inf). Ensure we're not unblocking early, otherwise we'll only slow down the cases where we're already pausing to make external RPC requests.

  • Set MinQueryIndex on stale queries. When keyring replication makes a stale query to non-leader peers to find a key the leader doesn't have, we need to make sure the peer we're querying has had a chance to catch up to the most current index for that key. Otherwise it's possible for newly-added servers to query another newly-added server and get a non-error nil response for that key ID.

  • Note that the "not found" case does not return an error, just an empty key. Update the handling of empty responses so that we don't break the loop early if we hit a server that doesn't have the key. (Peers aren't shuffled so we'd expect to hit the same server repeatedly.)

  • Move the keyring initialize step to wait until we're sure the FSM is current.

  • If a key is rotated immediately following a leader election, plans that are in-flight may get signed before the new leader has the key. Allow for a short timeout-and-retry to avoid rejecting plans

The rate limiter returns an error and unblocks early if its burst limit is
exceeded (unless the burst limit is Inf). Ensure we're not unblocking early,
otherwise we'll only slow down the cases where we're already pausing to make
external RPC requests.
@tgross
Copy link
Member Author

tgross commented Oct 20, 2022

Don't unblock early if rate limit burst exceeded. The rate limiter returns an error and unblocks early if its burst limit is exceeded (unless the burst limit is Inf).

Aside: there's a couple other existing cases of this in leader.go which I want to fix as well, but I don't want to muddy up this PR with that.

@tgross
Copy link
Member Author

tgross commented Oct 20, 2022

https://app.circleci.com/pipelines/github/hashicorp/nomad/33354/workflows/dbbd75a4-25cc-4d3d-9205-a817f7d68159/jobs/376989 shows what might be a reproduction of the original problem. I'm investigating that. No, this is a bug in the new code 🤦 Fixed!

When keyring replication makes a stale query to non-leader peers to find a key
the leader doesn't have, we need to make sure the peer we're querying has had a
chance to catch up to the most current index for that key. Otherwise it's
possible for newly-added servers to query another newly-added server and get a
non-error nil response for that key ID.

Ensure that we're setting the correct reply index in the blocking query.

Note that the "not found" case does not return an error, just an empty key. So
as a belt-and-suspenders, update the handling of empty responses so that we
don't break the loop early if we hit a server that doesn't have the key.
Wait until we're sure the FSM is current before we try to initialize the
keyring.

Also, if a key is rotated immediately following a leader election, plans that
are in-flight may get signed before the new leader has the key. Allow for a
short timeout-and-retry to avoid rejecting plans
Copy link
Member

@shoenig shoenig left a comment

Choose a reason for hiding this comment

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

LGTM, just thinking about Limiters

Comment on lines 458 to +462
// Rate limit how often we attempt replication
limiter.Wait(ctx)
err := limiter.Wait(ctx)
if err != nil {
goto ERR_WAIT // rate limit exceeded
}
Copy link
Member

@shoenig shoenig Oct 21, 2022

Choose a reason for hiding this comment

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

Seems like if we have our own blocking mechanism, we should be using Allow instead? Otherwise we are kind of double-waiting, which is fine but awkward given the error. (no need to change anything now)

https://pkg.go.dev/golang.org/x/time/rate#Limiter

edit: actually no, Wait has the nice property of waiting a minimal amount of time, unblocking once a resource becomes free

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this API is weirdly hard to use correctly and the error case is only for exceeding the burst. I first thought we probably wanted to use Reserve but that has the same double-waiting problem. Maybe we should just remove the burst (set it to Inf) so that we guarantee we'll wait and not have to handle this error so awkwardly?

Copy link
Member Author

Choose a reason for hiding this comment

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

I misread the API again 😊 . Only when the rate is set to rate.Inf is the burst-limit ignored. So this is good as-is, awkward as it seems.

@tgross
Copy link
Member Author

tgross commented Oct 21, 2022

I've got a clue that the core problem here is actually related to bugs in garbage collection, but I'm going to merge this in and push up a new PR with that fix once I've got that pinned down.

@tgross tgross merged commit 5732eb2 into main Oct 21, 2022
@tgross tgross deleted the b-keyring-replication-limit branch October 21, 2022 16:33
@tgross
Copy link
Member Author

tgross commented Oct 21, 2022

Oops, forgot the changelog entry. Will add that to #15009

@zansity
Copy link

zansity commented Oct 24, 2022

Hello! Any idea when 1.4.2 will be released? One of my environments is currently unstable due to key rotations becoming "stuck", and would like to test the new fix. Thank you!

@tgross
Copy link
Member Author

tgross commented Oct 24, 2022

Hello! Any idea when 1.4.2 will be released? One of my environments is currently unstable due to key rotations becoming "stuck", and would like to test the new fix. Thank you!

I can't commit to exact dates but I can tell you we're in the process of finalizing the release now, so it'll be "very soon."

Comment on lines +159 to +164
getActiveKeyset := func() (*keyset, error) {
e.lock.RLock()
defer e.lock.RUnlock()
keyset, err := e.activeKeySetLocked()
return keyset, err
}
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the after-the-fact drive-by review:

Looks like we could replace activeKeySetLocked with this func as the only other place the key is read is Encrypt and it similarly only needs the lock for the duration of fetching the key.

The fact neither reader of this field need the lock other than for this field signals to me that the lock should be encapsulated in the getter and not the caller's concern.

Not a big deal though.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good catch. activeKeySetLocked ends up holding onto the lock longer than it needs to as well -- it doesn't need to hold the lock while querying the state store. I'll open another quickie PR for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Follow-up PR: #15026

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/1.4.x backport to 1.4.x release line theme/keyring type/bug
Projects
None yet
4 participants