Skip to content

[nexus] update instances based on commit-time intended state - #11727

Merged
hawkw merged 8 commits into
mainfrom
eliza/commit-evacuated-instance-update
Nov 12, 2026
Merged

hawkw merged 8 commits into
mainfrom
eliza/commit-evacuated-instance-update

Conversation

@hawkw

@hawkw hawkw commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

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 SledEvacuating state, which externally
appears to be Rebooting and makes the instance eligible to automatically
restart. If, however, a user requests that the instance be stopped while the
saga is running, this will change the intended state to Stopped. If this
occurs, we would prefer the instance just come to rest in Stopped rather than
remaining Rebooting and 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
CASE statement to determine the state to commit, inside the commit query, but
I 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'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.

Fixes #11706

@hawkw
hawkw force-pushed the eliza/commit-evacuated-instance-update branch from 6568a1e to 0042474 Compare November 10, 2026 19:59
@hawkw

hawkw commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

@hawkw hawkw changed the title [nexus] conditional instance updates to fix #11706 [nexus] update instances based on commit-time intended state Nov 11, 2026
@hawkw

hawkw commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

@claude review

@hawkw
hawkw marked this pull request as ready for review November 11, 2026 20:24

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

Still open from earlier reviews (1):

  • Unresolved: 1 minor or pre-existing.

@hawkw

hawkw commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@hawkw
hawkw requested review from jgallagher and karencfv November 11, 2026 20:49
@hawkw

hawkw commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

Code review found no issues

No high-confidence issues detected in this change.

Still open from earlier reviews (1):

* _Unresolved: 1 minor or pre-existing._

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.

@karencfv karencfv left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

  1. self.db_datastore.instance_fetch_with_vmm fetches and instance in Vmm state with its VMM Destroyed
  2. The saga commits. It sees the intended state as Running, so it sets the state as SledEvacuating
  3. state.instance = self.db_datastore.instance_set_intended_state sets the intended state as Stopped.
  4. The instance now has intended state Stopped and its state SledEvacuating. To the user it's now stuck in Rebooting

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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.

Done in 29a06fb, thanks for the note!

Co-authored-by: Karen Cárcamo <[email protected]>
@hawkw

hawkw commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

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

  1. self.db_datastore.instance_fetch_with_vmm fetches and instance in Vmm state with its VMM Destroyed

  2. The saga commits. It sees the intended state as Running, so it sets the state as SledEvacuating

  3. state.instance = self.db_datastore.instance_set_intended_state sets the intended state as Stopped.

  4. The instance now has intended state Stopped and its state SledEvacuating. To the user it's now stuck in Rebooting

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

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.

@hawkw
hawkw requested a review from karencfv November 12, 2026 17:24
@hawkw

hawkw commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

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!

@karencfv karencfv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great, thanks!

@hawkw
hawkw merged commit f59b3f0 into main Nov 12, 2026
19 checks passed
@hawkw
hawkw deleted the eliza/commit-evacuated-instance-update branch November 12, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix possible race when stopping sled evacuating instance during reincarnation

2 participants