Repository navigation
[nexus] update instances based on commit-time intended state - #11727
Conversation
Signed-off-by: Eliza Weisman <[email protected]>
6568a1e to
0042474
Compare
|
@claude review |
|
@claude review |
|
@claude review |
This claude review request seems to have raced with a push, btw, and it looks like it reviewed the code prior to 879b2c9, which I'm pretty sure fixed the "still open from earlier reviews" issue. |
There was a problem hiding this comment.
Thanks for working on this! Really appreciated, especially as I know you have so much going on.
I kind of preferred the logic
living in the saga rather than in nexus-db-queries, and second, I didn't
really want to figure out the diesel necessary to express the table of states
in the SQL. If folks would prefer that, though, I could try to figure that out
here.
I agree, I prefer that kind of logic not in the query itself. It's a lot easier to understand if you're new coming into the codebase.
There's still the tiniest gap where there could be a race in instance_stop. I can't comment on the nexus/src/app/instance.rs, so I'll just type it out here.
In lines 1155-1166, we have two db calls in a row:
let mut state = self
.db_datastore
.instance_fetch_with_vmm(opctx, &authz_instance)
.await?;
state.instance = self
.db_datastore
.instance_set_intended_state(
opctx,
&authz_instance,
IntendedState::Stopped,
)
.await?;if the saga commits the state between these two db calls, something like this could happen
self.db_datastore.instance_fetch_with_vmmfetches and instance inVmmstate with its VMMDestroyed- The saga commits. It sees the intended state as
Running, so it sets the state asSledEvacuating state.instance = self.db_datastore.instance_set_intended_statesets the intended state asStopped.- The instance now has intended state
Stoppedand its stateSledEvacuating. To the user it's now stuck inRebooting
I know this PR isn't fixing that specific race, but we could just flip them around to close that gap no? It's the tiniest change that perhaps doesn't merit it's own PR. Thoughts?
self.db_datastore
.instance_set_intended_state(
opctx,
&authz_instance,
IntendedState::Stopped,
)
.await?;
let state = self
.db_datastore
.instance_fetch_with_vmm(opctx, &authz_instance)
.await?;| Failed, | ||
| /// The active VMM was stopped because its sled is being evacuated for an | ||
| /// update. | ||
| StoppedForUpdate, |
There was a problem hiding this comment.
I think this should be SledEvacuating instead, no? We've kept the rest of the enums (Reason::SledEvacuation, InstanceState::SledEvacuating, ReincarnationReason::SledEvacuation, etc) with that term, so I think it makes sense to keep it consistent here.
Co-authored-by: Karen Cárcamo <[email protected]>
Good catch, I think you're right. This has the side benefit that the datastore record will already have the new intended state, too. We may want to give the other instance-state-change APIs a similar treatment to set the intended state first. |
|
Thanks for the review @karencfv --- good catch about the order of setting the intended state in the API endpoints. I think I've addressed all your suggestions! |
Currently, the instance-update saga determines the instance's "resting" state to
commit at the end of the update saga based on the VMM state and the instance's
intended state when the instance and VMM snapshot are read at the beginning of
the saga. When the resting state depends on the instance's intended state, this
can behave incorrectly if an API request changes the instance's intended state
while the saga is running.
#11706 describes one such case. When an instance stopped because its sled is
being evacuated, it transitions to the
SledEvacuatingstate, which externallyappears to be
Rebootingand makes the instance eligible to automaticallyrestart. If, however, a user requests that the instance be stopped while the
saga is running, this will change the intended state to
Stopped. If thisoccurs, we would prefer the instance just come to rest in
Stoppedrather thanremaining
Rebootingand being restarted.This branch changes the instance-update saga to determine the state to commit
based on the instance's intended state at the time the update is committed to
the instance record, rather than when preparing the saga's params based on the
instance snapshot. The database query that commits the new state is now
conditional on the intended state remaining unchanged, so that the saga can
re-determine the proper state if the intended state changes between when it is
read and committed. We retry with the new state if our read was stale. The
little CAS-loop thingy could probably have been avoided if we instead used a SQL
CASEstatement to determine the state to commit, inside the commit query, butI didn't do that for a couple of reasons: first, I kind of preferred the logic
living in the saga rather than in
nexus-db-queries, and second, I didn'treally want to figure out the diesel necessary to express the table of states
in the SQL. If folks would prefer that, though, I could try to figure that out
here.
Fixes #11706