Repository navigation
Clean up Propolis zones even if we fail to ensure the instance - #11419
Conversation
384c9c9 to
f9ca374
Compare
hawkw
left a comment
There was a problem hiding this comment.
looks good to me, though you may want to wait for Sean or John to have a second look!
jgallagher
left a comment
There was a problem hiding this comment.
With apologies - I think all my comments are concerns with preexisting stuff that's either exposed or changed in some way I don't understand hence the questions.
There was a problem hiding this comment.
Does the take() here mean we could observe a self.running_state of None in put_state() that'd make us think there wasn't a zone, but actually there was (and maybe still is)? This is kind of hard to follow but I think async cancellation may also be on the scene here. Suppose we have this sequence:
- We receive a
PutStaterequest with aStoppedrequest. This builds a future inopthat callsself.put_state(). - We then go into a
select!that waits for either the put_state to finish or for a terminate request. - We start polling
opand get to the point of callingself.terminate(), which callsself.remove_propolis_zone(), which removesself.running_statehere. opgoes into any of the await points below (collecting a zone bundle or shutting down the zone).- While
opis still pending, we receive a termination request. This will dropop, cancelling it (!), and then call handle_termination_request() which will end up callingself.remove_propolis_zone()again, except we were already partway through this and cancelled it (and removedself.running_state, which has now been dropped by the cancellation?).
All that said... I think this was already a problem? I'm not sure if this PR made it better or worse or unchanged.
There was a problem hiding this comment.
This seems right to me. In this case, I think the zone would eventually be cleaned up by the Drop implementation. Until that happens, it does look like the second request (or any request) would think there's no zone.
I also don't know if this is better or worse than it was before. One option is to not call take() until the end of the method, which would at least prevent new requests from seeing running_state.is_none().
There was a problem hiding this comment.
Heh, the doc comment on remove_propolis_zone claims that:
omicron/sled-agent/src/instance.rs
Lines 1303 to 1304 in afabf3b
but I am not sure if this has been the case for at least as long as it has called self.running_zone.take() before it has made it past every await point in the function. Or, really, perhaps the comment is right that it is safe to call multiple times...as long as those calls all run to completion and are not canceled; otherwise we may end up in a case where we think the zone is gone prematurely. I think not setting running_state to None until the end of the method might solve this, as long as all the other things it tries to do are idempotent. Hmm...
There was a problem hiding this comment.
This is implemented in bf37595, please look closely :)
| "Stopped Propolis zone"; | ||
| "zone_name" => zname, | ||
| ), | ||
| Ok(Err(e)) => panic!("{e}"), |
There was a problem hiding this comment.
I think this PR adds another way to get to this point, which seems not great but I'm not sure what to do about it. The first bullet in the PR description includes this:
Callers then go through the same
fail_vmm_and_terminate()path that they do when making a state-change request fails on an existing instance, which ensures that we destroy the zone before notifying Nexus.
means that if we fail to start but then get here and encounter an error stopping the zone, we'll panic sled-agent?
There was a problem hiding this comment.
Yeah, I think that would panic the sled-agent. That was the case before too, except now we can also get to this code from a new path like you said: starting the zone, but failing to get Propolis to start it for some reason. I agree it's pretty terrible, and we shouldn't panic in any case.
I'd also like to change this, but I think that might come as part of resolving #7881. At least, not this PR, right?
There was a problem hiding this comment.
Definitely not in this PR. I don't know whether we should block this PR on resolving this though; this PR is fixing a leak if we fail to ensure an instance, but might be introducing a panic if we fail to ensure this instance?
There was a problem hiding this comment.
John and I talked a bit directly about this. It's all quite convoluted.
- The earlier call directly to
Zones::halt_and_remove_logged()could actually remove the zone, but it's out of band in the sense that it doesn't update the in-memory state of theRunningZoneobject. That's why we callRunningZone::stop()a little later, which does update that state. - We could remove this
panic!(), which seems good at first blush. But then we also need to remove theunwrap()on theRunningZone::stop()call, since that would presumably also fail. We'd also need to live with the inevitable leak in the call torelease_opte_ports()just below that, because the zone is still alive! - Also, even if we blow past failures to stop the zone here, we have another problem: we could incorrectly report to Nexus that the instance has been terminated, when the Propolis zone is still around. Nexus might then ask to recreate the instance, which would obviously fail.
The summary of all this is that we probably want yet another reconciler here. We're trying to continually ensure that this zone is really gone, and the OPTE ports are deleted. But doing that with a Drop impl or anything that removes the zone from a map or this option is a problem -- if that fails, you have no way of retrying it because you've dropped the object that was your handle to effect that retry.
But, that's a huge change, and not something we want to take right now. So in the interim, it seems plausible to change the panic!() here to an infinite retry loop. We have documented cases where 5 minutes isn't enough (see #7881), and a panic could land the sled-agent in maintenance. But retrying forever ensures that we never stop the InstanceRunner, and hopefully never report anything to Nexus, until the zone itself is definitively gone. I'm slightly concerned that the InstanceMonitor task will sidestep that, but I can't prove it yet.
@hawkw could you weigh in on this idea?
There was a problem hiding this comment.
But retrying forever ensures that we never stop the
InstanceRunner, and hopefully never report anything to Nexus, until the zone itself is definitively gone. I'm slightly concerned that theInstanceMonitortask will sidestep that, but I can't prove it yet.
I don't think the InstanceMonitor task is a problem here: it doesn't send observed states to nexus directly. Instead, it sends them over a channel to the InstanceRunner task. Those are only read from the channel in the select! loop, here:
omicron/sled-agent/src/instance.rs
Lines 633 to 636 in afabf3b
and published to Nexus in handle_monitor_message:
omicron/sled-agent/src/instance.rs
Line 1027 in afabf3b
So, if the InstanceRunner task is already in remove_propolis_zone, here, it isn't going to read any messages from the InstanceMonitor channel, so we won't tell Nexus that the zone has gone away prematurely while this code is still retrying. I think the infinite retry loop is safe, at least from that perspective. I don't love retrying forever, but I think it is probably better than panicking in this particular case...
There was a problem hiding this comment.
I don't love retrying forever, but I think it is probably better than panicking in this particular case...
We're all agreed then :)
There was a problem hiding this comment.
I think I've implemented this in bf37595. It obviously deserves a careful look.
| }) | ||
| }, | ||
| ), | ||
| omicron_common::backoff::retry( |
There was a problem hiding this comment.
perhaps this ought to use retry_notify_ext, and have it log something if we have been trying to halt and remove the zone for 5 minutes and it has kept failing?
|
I talked over some more details with John this morning. In particular, I found another possible way that the infinite retry loop we're adding here can block all instances on a sled. The This is in I'm going to try to split this PR up into a few smaller ones. First, I'll remove the panic and retry forever. That might include fixing this TODO in the zpool-handling code, I'm not sure yet. Then once we're fairly confident in that, I'll rebase this PR onto it, which should hopefully make the cleanup more obviously-correct. We can follow up with any additional PRs to clean things up if we need. |
Oh, yuck, that's really unfortunate. Sigh. I'm not sure how far you and John got in your discussion of this, but I think we're going to want to be pretty careful here. The good news is that if the only thing that can retry forever is destroying the Propolis zone, and the thing that |
53a7e68 to
fa1c868
Compare
fa1c868 to
cb352d4
Compare
- Adds a regression test that intentionally causes failures when starting an instance through the `Instance::put_state()` API. This exercises the situation in #7726, where we created a zone, but failed to start the instance through Propolis. That causes us to stop the zone through the `RunningZone::drop()` implementation, which _spawns a task_ to actually halt the zone, but drops the OPTE ports synchronously. That latter part fails, leaking the ports forever. - Add some optional instrumentation and control into the fake `Zones` API implementation, so that we can reliably tell when we've _started_ stopping the Propolis zone, and then release it later after we've run some other test assertions. - Move the newly-booted zone and other state into the `InstanceRunner` as soon as possible, so that we always go through the normal shutdown path if there's a failure to create an instance. This path retries shutdown indefinitely, and then releases the OPTE ports in the zone. - In the fallback drop impl for Propolis zones, we spawn a task to shut the zone down. Move the OPTE ports into this task, and drop them _after_ the zone, to ensure we don't hit permissions errors deleting them inline in the drop impl. The tickets are still dropped right away, which removes them from the port manager's map. Add a test for this.
cb352d4 to
d2fbb54
Compare
|
Ok after the hullabaloo of the last few weeks, I think this is ready for a review again. It's basically a brand-new PR, given how much underneath it changed, so just it probably only makes sense to look at the last commit from scratch. |
| /// # Important | ||
| /// | ||
| /// As soon as the zone is booted, this stores the `RunningZone` in `self`. | ||
| /// That means that the zone will _not_ be torn down if an internal call in | ||
| /// the function fails after that (e.g., waiting for Propolis's HTTP server | ||
| /// to start). Callers must destroy the instance themselves, though it will | ||
| /// be eventually destroyed by the drop impl on `RunningZone` as a | ||
| /// last-resort. |
There was a problem hiding this comment.
IIUC, this is essentially saying "this method is not cancel safe", right?
There was a problem hiding this comment.
I'm trying to make clear that it's the caller's job to make sure we explicitly stop the zone. It's true that it's not cancel-safe, but I feel like some notes here are still helpful.
There was a problem hiding this comment.
Yeah, I agree , I think this comment is better than a generic "not cancel safe", and I didn't mean to imply that we should get rid of it.
Mostly, I was thinking we might want to note that this also happens if the call is canceled externally, which as far as I can tell, is by design. We might even go as far as to say that because the act of starting a zone external to the process is inherently not cancel-safe, the function is more cancel-correct because it mutates state immediately --- this function should not try to be "cancel safe" in the sense that it has no internally visible side effects if it is canceled before it completes, because the internal state must match that of the external side effect of booting the zone.
There was a problem hiding this comment.
Ah! I see. I agree that we're being more explicit and correct here about the relationship with the zone. I'll add a little more color on this part, thanks.
There was a problem hiding this comment.
This brings up yet another edge case I hadn't quite considered. This PR will correctly tear down the zone in the case where we boot it successfully, but fail to ensure the instance. That's good, and what I was trying to fix. It will not destroy the zone if we partially configure it, or configure it but fail to boot it. We drop any partially-configured stuff without storing it in self, so the current version of remove_propolis_zone() will see self.running_state.is_none() and return early without actually deleting the zone.
I'd like to defer that, since this has already lingered and is big enough as-is. What we could do is store an enum over the zone state, with variants for "not started", "installed", and "running". That always gives us a place to store the OPTE ports, so they're not dropped before the zone, and a way to figure out which state the system is in and so how to clean up along the way.
- watch channel -> set once - clearer docstring
InstanceRunneras soon as it's booted, rather than waiting for the instance ensure request to succeed. This catches failures at the instance ensure step, e.g., we asked Propolis to create the VM but it panicked while doing so. Callers then go through the samefail_vmm_and_terminate()path that they do when making a state-change request fails on an existing instance, which ensures that we destroy the zone before notifying Nexus. That means Nexus shouldn't attempt to recreate the zone until after the failed one is completely gone, an attempt which would always fail anyway because the OPTE ports cannot be deleted and recreated until the zone and then ports are deleted.RunningZone::drop()that asynchronously drops the zone. This is a backstop against future changes that don't callfail_vmm_and_terminate(), but instead rely on the drop impl itself. The OPTE ports aren't destroyed until the zone is gone, so we should get fewer or ideally no failures to delete them in the future.