Repository navigation
Description
Broken out from #6809 (see this comment and this follow-up).
In various circumstances (typically when a Propolis VM advertises that it has halted, though there are several others), sled-agent's InstanceRunner calls into its terminate function, which (a) ensures that the runner's Propolis zone is shut down, (b) sends a message to the InstanceManager indicating that the VMM can be removed from its tracking table, and (c) sets a flag indicating that the runner's main loop should terminate. Importantly, all of these things happen before the top-level loop in InstanceRunner::run gets to publish any updated VMM state to Nexus.
For example, in the normal "Propolis halted" case, the runner will observe a new state from Propolis that indicates there's no VM there anymore:
omicron/sled-agent/src/instance.rs
Lines 924 to 948 in 6c12e1f
| // Update the Sled Agent's internal state machine. | |
| let action = self.state.apply_propolis_observation(state); | |
| info!( | |
| self.log, | |
| "updated state after observing Propolis state change"; | |
| "propolis_id" => %self.propolis_id, | |
| "new_vmm_state" => ?self.state.vmm() | |
| ); | |
| // If the zone is now safe to terminate, tear it down and discard the | |
| // instance ticket before returning and publishing the new instance | |
| // state to Nexus. This ensures that the instance is actually gone from | |
| // the sled when Nexus receives the state update saying it's actually | |
| // destroyed. | |
| match action { | |
| Some(InstanceAction::Destroy) => { | |
| info!(self.log, "terminating VMM that has exited"; | |
| "instance_id" => %self.instance_id(), | |
| "propolis_id" => %self.propolis_id); | |
| let mark_failed = false; | |
| self.terminate(mark_failed).await; | |
| Reaction::Terminate | |
| } | |
| None => Reaction::Continue, | |
| } |
That call to terminate will yank the zone and signal the instance manager:
omicron/sled-agent/src/instance.rs
Lines 2316 to 2323 in 6c12e1f
| async fn terminate(&mut self, mark_failed: bool) -> SledVmmState { | |
| self.terminate_inner().await; | |
| self.state.terminate_rudely(mark_failed); | |
| // This causes the "run" task to exit on the next iteration. | |
| self.should_terminate = true; | |
| self.state.sled_instance_state() | |
| } |
omicron/sled-agent/src/instance.rs
Lines 1434 to 1475 in 6c12e1f
| // Ensure that no zone exists. This succeeds even if no zone was ever | |
| // created. | |
| // NOTE: we call`Zones::halt_and_remove_logged` directly instead of | |
| // `RunningZone::stop` in case we're called between creating the | |
| // zone and assigning `running_state`. | |
| warn!(self.log, "Halting and removing zone: {}", zname); | |
| let result = tokio::time::timeout( | |
| Duration::from_secs(60 * 5), | |
| omicron_common::backoff::retry( | |
| omicron_common::backoff::retry_policy_local(), | |
| || async { | |
| Zones::halt_and_remove_logged(&self.log, &zname) | |
| .await | |
| .map_err(|e| { | |
| if e.is_invalid_state() { | |
| BackoffError::transient(e) | |
| } else { | |
| BackoffError::permanent(e) | |
| } | |
| }) | |
| }, | |
| ), | |
| ) | |
| .await; | |
| match result { | |
| Ok(Ok(_)) => {} | |
| Ok(Err(e)) => panic!("{e}"), | |
| Err(_) => { | |
| panic!("Zone {zname:?} could not be halted within 5 minutes") | |
| } | |
| } | |
| // Remove ourselves from the instance manager's map of instances. | |
| self.instance_ticket.deregister(); | |
| // See if there are any runtime objects to clean up. | |
| // | |
| // We already removed the zone above but mark it as stopped | |
| running_state.running_zone.stop().await.unwrap(); | |
| // Remove any OPTE ports from the port manager. | |
| running_state.running_zone.release_opte_ports(); |
But the terminal VMM state is published to Nexus only afterward:
omicron/sled-agent/src/instance.rs
Lines 559 to 562 in 6c12e1f
| Some(InstanceMonitorRequest { update: State(state), tx }) => { | |
| let observed = ObservedPropolisState::new(&state); | |
| let reaction = self.observe_state(&observed).await; | |
| self.publish_state_to_nexus().await; |
This creates a window for the following kind of race:
- Nexus (e.g. via the instance watcher) observes that an instance has a VMM that should be alive
- A user asks to stop this VMM
- Sled-agent's VMM shutdown sequence tells the agent's
InstanceManagerto remove the VMM from its tracking table - Nexus calls sled-agent to get the VMM's status; this returns "no such VMM" to Nexus
- Since the VMM state in Nexus is unchanged, the watcher assumes the VMM is missing in error and moves the instance to Failed
- Nexus auto-restarts the instance, even though the user asked for it to be stopped
- Sled-agent finally publishes the "destroyed" state to the now-unused VMM
Sled-agent should avoid this problem by deregistering VMMs from its table only after publishing their terminal states to Nexus. In the example above, Nexus may still see an unexpected "VMM gone" error in this case, but it can figure out what happened by rechecking CRDB and noticing that the VMM's state has changed.1
Note that this problem is distinct from the case where a user asks to stop a VM and sled-agent completely loses track of the VM before publishing a terminal state for it. This can happen if sled-agent reboots while a VM stop request is in flight. Since no "VMM destroyed" update is forthcoming in this case, Nexus needs to do something else to handle this case. This problem is tracked by #6809.
Footnotes
-
In practice Nexus observes this by trying to update the VMM record itself (to change its state to Failed) and seeing that sled-agent changed it first, i.e., Nexus does not actually re-read the record in this case. ↩
Activity