Skip to content

sled-agent: clean up and reorder VMM termination logic - #7928

Merged
gjcolombo merged 10 commits into
mainfrom
gjcolombo/sled-agent-vmm-termination
Apr 8, 2025
Merged

gjcolombo merged 10 commits into
mainfrom
gjcolombo/sled-agent-vmm-termination

Conversation

@gjcolombo

@gjcolombo gjcolombo commented Apr 5, 2025 •

Copy link
Copy Markdown
Contributor

Try to clean up sled-agent's logic for terminating Propolis zones. Specifically, change InstanceRunner::terminate and its callees so that they no longer call InstanceTicket::deregister before the runner gets a chance to publish a terminal VMM state to Nexus. This fixes a race in which Nexus could call sled-agent, get a "VMM gone" response, and move the relevant VMM to Failed when sled-agent actually intended to move it to Destroyed.

Do some housekeeping to (try to) make this code easier to reason about:

  • Rename various types and routines to make their purposes and semantics a little clearer. (In particular, try not to have so many routines named "terminate" or variations thereon.)
  • Clean up some code and comments that are obsolete now that the instance monitor and instance runner tasks pass messages instead of sharing state.
  • Make explicit termination requests specify who is responsible for updating the terminated VMM's state: sometimes it's sled-agent (e.g. when terminating a Propolis because its U.2 is gone), but sometimes it's Nexus (e.g. when unwinding an instance start saga). This allows the instance runner to avoid updating a VMM that Nexus expects to be able to control.
  • Remove InstanceTicket::deregister and move its implementation into InstanceTicket::drop. This prevents the runner from sending a "deregister me" message "too early"; it can still forget to send a VMM's terminal state to Nexus, but the structure of the main runner loop should help ensure that this last update always gets sent.

Fixes #7927.

Try to clean up sled-agent's logic for terminating Propolis zones.
Specifically, change `InstanceRunner::terminate` and its callees so that
they no longer call `InstanceTicket::deregister` before the runner gets
a chance to publish a terminal VMM state to Nexus. This fixes a race in
which Nexus could call sled-agent, get a "VMM gone" response, and move
the relevant VMM to Failed when sled-agent actually intended to move it
to Destroyed. 

Do some housekeeping to (try to) make this code easier to reason about:

- Rename various types and routines to make their purposes and semantics
  a little clearer. (In particular, try not to have so many routines
  named "terminate" or variations thereon.)
- Clean up some code and comments that are obsolete now that the
  instance monitor and instance runner tasks pass messages instead of
  sharing state.
- Make explicit termination requests specify who is responsible for
  updating the terminated VMM's state: sometimes it's sled-agent (e.g.
  when terminating a Propolis because its U.2 is gone), but sometimes
  it's Nexus (e.g. when unwinding an instance start saga). This allows
  the instance runner to avoid updating a VMM that Nexus expects to be
  able to control.
- Remove `InstanceTicket::deregister` and move its implementation into
  `InstanceTicket::drop`. This prevents the runner from sending a
  "deregister me" message "too early"; it can still forget to send a
  VMM's terminal state to Nexus, but the structure of the main runner
  loop should help ensure that this last update always gets sent.
@gjcolombo
gjcolombo force-pushed the gjcolombo/sled-agent-vmm-termination branch from 967b91b to 4845b9a Compare April 7, 2025 16:06
Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
I don't think this is actually going to shorten the tests I intend to
write, but maybe it will.
There is a deadlock between the instance runner and instance manager in
the current code:

- `InstanceRunner` waits for its command request queue to yield `None`
  before returning
- `InstanceManager` won't drop the sender side of the queue until the
  instance's ticket is deregistered, which doesn't happen until the
  runner returns

This will be fixed in a subsequent commit.
@gjcolombo

Copy link
Copy Markdown
Contributor Author

This PR doesn't quite work as-is because of a deadlock between the new InstanceRunner and the InstanceManager: the runner needs the manager to drop the tx side of the instance request queue before the runner will return, but the runner needs to return in order to drop its instance ticket, which is what tells the manager to drop its record of the instance.

This is easy enough to sort out; I'll push some new commits (including a unit test that helped catch this) in a bit.

Comment thread sled-agent/src/common/instance.rs Resolved
Comment thread sled-agent/src/common/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Resolved
Instead of making a VM's InstanceTicket a member of InstanceRunner and
relying on it being dropped when `InstanceRunner::run` returns, pass the
ticket as a parameter to `run` and drop it explicitly at the appropriate
moment. This is needed to avoid deadlocking the runner when it wants to
exit (it needs to see that the sender side of its request queue is
dropped, and this requires the controlling instance manager to release
the relevant instance).
@gjcolombo

Copy link
Copy Markdown
Contributor Author

I still need to test this in a dev cluster, but now that I have a couple of targeted tests for instance monitor/instance runner interactions I feel somewhat better about this--good enough, at any rate, to take off the draft tag.

@gjcolombo
gjcolombo marked this pull request as ready for review April 7, 2025 22:01
@gjcolombo
gjcolombo requested a review from hawkw April 7, 2025 22:01
@gjcolombo gjcolombo added this to the 14 milestone Apr 7, 2025
Comment thread sled-agent/src/instance.rs Outdated
Comment on lines +756 to +767
// Drop the instance ticket so that this instance will be removed from
// the instance manager. This ensures that no more requests will be
// queued to the runner, which allows this routine to send replies to
// those requests in an orderly fashion.
//
// This needs to be done only after publishing the final VMM state to
// Nexus so that Nexus is guaranteed to have received notice of the
// VMM's terminal state before it begins to see "no such VMM" errors
// from the instance manager. Otherwise it is possible for Nexus to
// conclude that a cleanly-stopped instance has gone missing and should
// be restarted.
drop(ticket);

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.

This somewhat makes me wonder if tying this to dropping the ticket was a mistake, but ... yeah, seems reasonable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah... I think the important property here is that the ticket is no longer a member of InstanceRunner, which makes it much harder for run's callees to call deregister when they shouldn't. I also sort of prefer the aesthetics of an explicit deregister routine if we're not going to rely on auto-drop, so I'll see about switching this back.

Comment thread sled-agent/src/instance.rs Resolved
Comment thread sled-agent/src/instance.rs Outdated
if !self.state.vmm_is_halted() {
self.publish_state_to_nexus().await;
InstanceMonitorResponse::Continue
std::ops::ControlFlow::Continue(())

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.

maybe we should import std::ops::ControlFlow?

@gjcolombo

Copy link
Copy Markdown
Contributor Author

I've booted up a dev cluster and tested a few things ad hoc:

  • Instance start/stop via the API leave the instance in a Stopped state
  • Instance stop from within the guest also leaves the instance in a Stopped state
  • Restarting a running instance's propolis-server causes its VMM to go to Failed and the instance to automatically restart

I've checked with omdb that the VMM records reach the correct terminal states and that the relevant sled resource reservations get cleaned up in all these cases. #7927 is at bottom a race condition, so we'll want to continue testing this on rack2 once it's deployed there, but this at least means I haven't obviously broken anything.

@gjcolombo
gjcolombo merged commit dbcd860 into main Apr 8, 2025
@gjcolombo
gjcolombo deleted the gjcolombo/sled-agent-vmm-termination branch April 8, 2025 15:44
hawkw added a commit that referenced this pull request Oct 9, 2026
…#11462)

The sled-agent has a mechanism to request the "rude" or "forceful"
termination of a VMM process. Forceful termination does not send a
request to Propolis to gracefully stop the instance, and instead
attempts to immediately destroy the Propolis zone and tear down all
sled-agent-internal state related to that instance. Unlike other
requests to operate on an instance, forceful termination bypasses the
queue of requests to the VMM's `InstanceRunner` task, and if a request
to the propolis-server is currently in progress, it will cancel that
request immediately. This is intended to ensure that even if the
propolis-server process has gotten stuck handling a request for some
reason, and therefore, the `InstanceRunner` is in turn stuck waiting
for that request to complete, the stuck VMM process can still be killed
and its zone cleaned up regardless. See PRs #6915, #7928, and #7548 for
additional context around why this mechanism is necessary.

Unfortunately, the way this is currently implemented has some...issues,
which came up while reviewing PR #11456. The termination request is
currently sent using a `tokio::sync::mpsc` channel, which means that the
channel can become full, and callers must handle backpressure. This is
necessary because callers currently wait for the termination to
*complete* by sending a `tokio::sync::oneshot::Sender` over the
termination-request channel, which the `InstanceRunner` will send to
once the VMM has been terminated. #11456 introduces an infinite retry
loop to the termination sequence in order to ensure the zone is
destroyed successfully. Because backpressure is exerted on callers who
request termination if the termination request channel has filled up,
this means that the infinite retry loop there will put callers in a
position where they must either load-shed (and therefore be unable to
*guarantee* that the termination request has ever made it through), or
wait indefinitely for their request to be sent. This was scary to see,
so I wanted to see if it could be avoided.

This branch changes the implementation of the termination mechanism to
remove the use of the `mpsc` channel. This is based on the observation
that the goal here is really just to communicate that "you need to shut
down", and if multiple callers request that, their requests need not be
handled individually: in practice, in the current design, the first such
request will actually result in termination, and all the others will sit
in the queue until the termination sequence has completed and the
`InstanceRunner` drains any other requests. The only reason it works
this way today is that a similar `mpsc` request channel with `oneshot`
responses is used to place HTTP requests in the queue of the
`InstanceManager` for dispatching to the individual `InstanceRunner`
tasks, so the same oneshot `tx`en could be sent all the way through to
the runner.

Since there is no actual need to handle these requests individually,
though, we can replace the `mpsc` and `oneshot` with a pair of
`tokio::sync::SetOnce` channels: one to signal the termination request,
and another to publish that it has completed. This way, the first
caller to request termination sets the `signalled` `SetOnce`, and
subsequent callers can just see that it has already been signalled
without having to wait on a `mpsc` channel. Callers which need to wait
for termination to complete and receive the final state (in this case,
the `ensure_unregistered` HTTP endpoint) can then wait for the other
`SetOnce`. Meanwhile, callers that *don't* actually need to wait for
the termination sequence to complete can now choose *not* to wait for
that while still knowing that it has been successfully signalled. This
means `InstanceManager::use_only_currently_managed_zpools` can no
longer block the entire `InstanceManager` from making progress forever,
even once #11456 changes the termination process to retry forever when
destroying the zone.

Other than the change to stop waiting for the termination to complete
in `InstanceManager::use_only_currently_managed_zpools`, I've tried to
preserve the existing behavior as closely as possible. Just like in the
previous code, the first caller to request termination gets to
determine the `VmmStateOwner` (i.e. whether or not the final state is
published to Nexus). This may deserve to be revisited, and perhaps
simplified, later, but this change should leave it the way it was until
then.

---------

Signed-off-by: Eliza Weisman <[email protected]>
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.

sled-agent shouldn't deregister instances before publishing their terminal VMM states

2 participants