Repository navigation
[nexus] unwind instance-start sagas if they race with the VMM going away - #11608
Conversation
|
There may be a less horrific way to fix this, but, this certainly seems to work, and it seemed like it was a solution that would be robust with regards to other changes in this vicinity, even though all this code is kind of a general pile of broken glass and rusty nails. I can try to explain the thinking here better tomorrow, but for now I really need to step away from the computer. |
|
After thinking it over a bit more, I think it might be somewhat neater to:
This way we don't need the kinda weird separate "did it actually work?" node. |
0e2f609 to
c5d4cfe
Compare
e5ff334 to
b515d16
Compare
b515d16 to
5948001
Compare
5ba368a to
9192e45
Compare
9192e45 to
67543a0
Compare
Regarding this: I think this might be as simple as:
But, it's complex enough that I want to think a bit harder about it. I don't think what I'm describing here will introduce any new races, but... |
67543a0 to
14ee0ec
Compare
jgallagher
left a comment
There was a problem hiding this comment.
Regarding this: I think this might be as simple as:
- if the VMM is Creating then try to advance its state to Stopping, conditional on the generation number not having advanced
- if the state advances to Stopping, then the start saga will try to advance it to Starting after registering the VMM with the sled, which will fail, and the start saga unwinds, destroying the VMM. The instance will then move to Stopped successfully.
- if the state is no longer Creating, then the VMM is now present on the sled, so send the stop request to the sled-agent. This will stop the VMM normally.
If the state advances to Stopping in this scenario, will that kick off another saga (instance update?) that's now racing with the still-running start saga? If so, will the instance + VMM end up in the expected state regardless of which one manages to tell sled-agent to stop first?
| paused_registration.resume(); | ||
| let saga_result = | ||
| running_saga.wait_until_stopped().await.into_raw_result(); | ||
| match dbg!(saga_result.kind) { |
There was a problem hiding this comment.
I like leaving dbg! macros like this in tests, since if the test fails, nextest prints them all out to stdout without requiring you to track down the log file in /tmp to see what happened...but I can remove it if you really don't like that.
There was a problem hiding this comment.
I'm not a huge fan; if I see it I assume it was leftover, and if someone wanted intentional output they use {e,}println!(). But it's really more of a "oh this looks like an accident" than any preference.
| // caller. We shall check whether the state we wrote back to the | ||
| // database is the VMM's current state. If it is not in the expected | ||
| // state, then something else has acted on that VMM record, with which | ||
| // we may have raced! |
There was a problem hiding this comment.
This feels pretty sketchy to me, but some of that is certainly from a mismatch between what I (naively) expected notify_vmm_updated() to do and what it actually does, so maybe this is all fine. I'll just ramble here and see if anything makes sense.
At a glance it doesn't seem like fetching the state here to see if it matches what we wrote can really fix any race, because the db state could change between when we fetch it and when we return Ok(_) to the caller, so an Ok(_) return can't mean "the db state, if you were to read it right now, would match what I just wrote". If that's a thing the caller cares about, they would need to fetch it themselves.
However, all that reasoning is based on "see if it matches what we wrote", because I assumed notify_vmm_updated() would fail if we failed to write to the db (i.e., because our data is stale). That's also supported by the "We shall check whether the state we wrote back to the database is the VMM's current state." in this comment block.
But notify_vmm_updated() doesn't fail even if it didn't write to the db - if our generation is out of date, we end up returning Ok(UpdateAndQueryResult { status: UpdateStatus::NotUpdatedButExists, found }) from vmm_update_runtime_on_connection(). That bubbles out into an Ok(VmmStateUpdateResult { found_vmm, vmm_updated: false, ..migration fields.. }) from vmm_and_migration_update_runtime(). That's used to decide whether process_vmm_update() returns an update saga that's kicked off by notify_vmm_updated(), but by that point we've lost the found_vmm we loaded from the db and the details about whether or not our write actually landed.
Given all of that, I think my mild order of preference (ignoring work involved) would be something like:
- Change the call stack of
notify_vmm_updated()so that at this point, we know whether our write landed. Maybe we also want thefound_vmmrecord to decide what kind of error to return? - Reword the comment here to try to summarize the above - it's not that we need to see if what we wrote is still there (which is inherently racy) - it's that we need to see whether our write landed at all. I don't like this as much because I think this will also fail if our write did land, but then another write landed afterwards - failing here just because we didn't read our write "fast enough" doesn't seem correct.
There was a problem hiding this comment.
Yeah, so, you are correct that the behavior of notify_vmm_updated kind of hiding whether the write succeeded or failed makes this all a bit trickier. I had originally wanted to do, essentially, your option (1) here myself, rather than doing a second read afterwards. The tricky bit is that the other callers don't want it to return an error if the write was a stale generation, so we would want to bubble up the found_vmm and allow the caller to compare it with the generation they were trying to write, instead. That's where I...started getting sketched out by the semantics of check_if_exists, since if the write succeeds, it actually returns the previous value, rather than the one that was written...
There was a problem hiding this comment.
Okay, so it ended up changing a lot more code than I had hoped, but a7ebf22 changes things based on the thread I started pulling while thinking through this suggestion. I think it makes this all a lot less sketchy. In particular, I've done the following:
Nexus::notify_vmm_updatedand its callstack (i.e.process_vmm_update,
DataStore::vmm_and_migrations_update_runtime) to return the updated VMM state, so we can eliminate the re-query. This is done by changing the return value to a structProcessedVmmUpdatethat contains any needed update saga alongside the database update result.- Then, I've changed the check for whether or not
Nexus::instance_ensure_registeredshould return an error to specifically check for whether the requested state is now in the database based on that return value. This is not done using thecheck_if_existsvalue that indicates whether this write was the one that wrote the expected state, because the function must still succeed if called idempotently in a saga.
There was a problem hiding this comment.
It's possible some of this refactoring could be pulled out into its own PR, but I don't really know if it's worth it at this point.
No, However, I've been continuing to push on this in a separate branch, and I think it will probably end up having to be a new VMM state, since I'm hoping to have a PR up for these proposed changes soon, but it's hairy enough that I want to make sure there are a lot more tests first. I'm also pretty leery of making the (already quite complex) state machine even more complicated. There is always the alternative approach of just saying that |
This more or less addresses the suggestions from #11608 (comment). It ends up being a somewhat larger change than I had hoped, but I think it makes this all a lot less sketchy. In particular, I've changed `Nexus::notify_vmm_updated` and its callstack (i.e. `process_vmm_update`, `DataStore::vmm_and_migrations_update_runtime`) to return the updated VMM state, so we can eliminate the re-query. This is done by changing the return value to a struct `ProcessedVmmUpdate` that contains any needed update saga alongside the database update result. Then, I've changed the check for whether or not `Nexus::instance_ensure_registered` should return an error to specifically check for whether the requested state is now in the database based on that return value. This is *not* done using the `check_if_exists` value that indicates whether *this* write was the one that wrote the expected state, because the function must still succeed if called idempotently in a saga.
jgallagher
left a comment
There was a problem hiding this comment.
Thanks, I like the revised changes rather a lot more. 👍 Just a handful more questions - thanks for your patience.
| } else { | ||
| // If we *didn't* update the VMM state, we can just return | ||
| // the value that `check_if_exists` gave us. | ||
| found_vmm |
There was a problem hiding this comment.
Since TIL check_if_exists doesn't check for soft-deleted rows, if it was soft-deleted when we ran the update, I think the behavior would be:
- our update wouldn't apply because it has a
filter(dsl::time_deleted.is_null()) - but
check_if_exists's CTE would still find the row since it doesn't filter out soft deletes
and we'd return it here. Maybe that's fine? But it seems weird - should we check for found_vmm.time_deleted.is_some() and do... something different (???) if it is?
There was a problem hiding this comment.
Yeah, so, hm. If this could get called in an unwinding saga, this behavior would, unfortunately, be correct, since we would want undo actions that try to mutate the state of a VMM record that has been soft-deleted to succeed, rather than erroring out and getting the unwinding saga stuck. If that were the case, I think we would have to make it the caller's responsibility to decide if "VMM record was soft-deleted" should be an error or not.
However, I don't think this method is called in an undo action, but...I'll have to double check. If it isn't, we could probably make it return an error if the VMM has been deleted.
There was a problem hiding this comment.
Hmm, actually, if we returned an error here if the VMM record has been soft-deleted, that would also bubble-up into an external API request to stop the instance, if some other actor has already observed the VMM go to the Destroyed state and deleted it before the API endpoint returned. In that case, we would potentially turn a successful stop into an error, which seems bad. 😬
There was a problem hiding this comment.
Returning an error from trying to stop an instance that someone else deleted seems okay? Oh, or is the order there something like:
- stop request comes in
- we stop the VMM but are still running the saga
- someone else successfully soft deletes the instance
- we get here and have to decide what to do
? So we did successfully stop it, then someone else deleted it before we returned? Yeah that seems unfortunate 😬
There was a problem hiding this comment.
It's not the instance record that would be soft-deleted, but the VMM record. The VMM record for a Destroyed or Failed VMM may be soft-deleted after the corresponding instance record has been marked as Stopped and has had the VMM record. detached from it. In that case, the instance has not been deleted, just the VMM that it was previously using, and we don't want to return an error that incorrectly suggests the instance has been deleted.
There was a problem hiding this comment.
The important thing here is to remember that records in the VMM table don't represent externally visible API objects, so the caller doesn't care whether or not they have been deleted (since they don't directly correspond to a resource the caller can view or interact with).
A deleted VMM doesn't mean the instance is deleted, it means that the instance is no longer running on that VMM (either because it has no VMM and is stopped, or because it is running someplace else), and the control plane has completed cleaning up the networking state and virtual resource provisioning records associated with that VMM, so it deleted the VMM record after transitioning the instance to Stopped (if the VMM was destroyed due to stopping), restarted the instance (if the VMM failed), or updated the instance to point at the VMM it migrated to (if the instance migrated out).
thanks to @jgallagher for clarifying my thinking here a bit in #11608 (comment)
|
@claude review |
This branch fixes #11560, a race condition which occurs when an instance
is requested to stop through the external API while an
instance_startsaga for that instance is in progress.
The race occurs when the stop request is received while the VMM record
is
Creating, and is forwarded to the sled-agent. TheCreatingstaterepresents a VMM where we have allocated a sled to run the instance and
created a VMM record for the Propolis process we intend to start, but
have not yet sent a
vmm_ensurerequest to the sled-agent.When the sled-agent receives the request to stop the VMM, it has not yet
received an ensure request to create that VMM, and therefore
(correctly) indicates that it does not exist, which results in Nexus
moving the VMM to
Failedand kicking off aninstance_updatesaga totransition the instance to
Failedand delete thevmmrecord.However, this is invisible to the start saga, which continues its
attempt to register and run the VMM, rather than unwinding. This
ultimately results in the sled-agent running a VMM that the control
plane considers dead. The control plane may then attempt to restart the
instance elsewhere, which gets stuck when the new VMM tries to activate
the same crucible disk(s) that the zombie VMM is already using.
This branch closes the race by having the
Nexus::instance_ensure_registeredmethod read back the database recordfor the VMM to ensure that it successfully advanced the state to
Startingafter sending thevmm_ensurerequest to the sled-agent.I've included a test reproducing the race, which uses the new test
utilities I added in #11606, to temporarily interrupt the sled-agent
simulator and advance the VMM to
Failedbefore responding to thevmm_ensurerequest. Prior to this change, the start saga continues,now it unwinds correctly.
In my opinion, the behavior of forwarding requests to the sled-agent
when a VMM is in the
Creatingstate remains wrong. We allow a userto "cancel" starting an instance when it is in the
Startingstate, andthe
CreatingVMM state maps to theStartinginstance state in theexternal API. Therefore, we shouldn't just change the API to reject
requests to stop an instance with a
CreatingVMM: I really want toavoid leaking the fact that some external-API states may represent
multiple internal states, and therefore really dislike having
Startingsometimes let you stop the instance but other times not let you. Now
that we fixed the race, the current behavior does do more or less the
correct thing, it just does it in a kind of awful way. When the VMM
has not yet been started on the sled, we shouldn't send a request to the
sled to stop it and rely on the fact that the sled-agent will mark it as
failed to unwind the start saga. But, this PR doesn't change any of
that; it just fixes the dangerous consequences of this behavior.
Subsequently, we may want to change how instances are stopped when the
VMM is in the
Creatingstate. I'd prefer to do that in a separatechange.
Fixes #11560.