Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 80 additions & 11 deletions crates/protrus-gate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,35 @@ impl<S: ModuleStore, B: RuntimeBackend> DeploymentService<S, B> {
revision: &Revision,
attempt: &str,
) -> Result<DeploymentOutcome, DeploymentError> {
let record = self
self.restart_crashed(module_id, revision, attempt, true)?
.ok_or_else(|| {
DeploymentError::InvalidState(format!(
"{module_id} revision {} is no longer the active revision",
revision.0
))
})
}
/// `redeploy_after_crash` for a runtime its owner has already reaped, as the OCI pair watcher
/// drops the dead pair before it asks for recovery: there is nothing left to stop. Only this
/// module is brought back; the other modules' runtimes are untouched, as Protean's container
/// watcher restarts only the container that exited. `None` when the revision is no longer the
/// active one (uninstalled or replaced since), so there is nothing to restore.
pub fn redeploy_reaped_after_crash(
&self,
module_id: &ModuleId,
revision: &Revision,
attempt: &str,
) -> Result<Option<DeploymentOutcome>, DeploymentError> {
self.restart_crashed(module_id, revision, attempt, false)
}
fn restart_crashed(
&self,
module_id: &ModuleId,
revision: &Revision,
attempt: &str,
stop: bool,
) -> Result<Option<DeploymentOutcome>, DeploymentError> {
let Some(record) = self
.store
.load(module_id)
.map_err(Self::store_error)?
Expand All @@ -1303,15 +1331,14 @@ impl<S: ModuleStore, B: RuntimeBackend> DeploymentService<S, B> {
&& record.observed_state == ObservedState::Active
&& &record.revision == revision
})
.ok_or_else(|| {
DeploymentError::InvalidState(format!(
"{module_id} revision {} is no longer the active revision",
revision.0
))
})?;
self.backend
.drain_and_stop(&record)
.map_err(|error| DeploymentError::Runtime(error.to_string()))?;
else {
return Ok(None);
};
if stop {
self.backend
.drain_and_stop(&record)
.map_err(|error| DeploymentError::Runtime(error.to_string()))?;
}
self.deactivate_data_plane(&record);
let result = self.deploy(self.stored_request(
&record,
Expand All @@ -1329,7 +1356,7 @@ impl<S: ModuleStore, B: RuntimeBackend> DeploymentService<S, B> {
Err(error) => format!("failed: {error}"),
},
);
result
result.map(Some)
}

/// The deploy request that brings a stored record back: its own revision, routes, and signed
Expand Down Expand Up @@ -2719,6 +2746,48 @@ mod tests {
assert_eq!(*stops.lock().unwrap(), 2);
}
#[test]
fn a_reaped_crash_brings_back_only_that_module() {
let stops = Arc::new(Mutex::new(0));
let service = DeploymentService::new(
MemoryStore::default(),
CountingBackend {
stops: stops.clone(),
},
Arc::new(RouteRegistry::default()),
GatePipeline::default(),
);
service.deploy(request("crashed", 1, "/crashed")).unwrap();
let mut neighbour = request("neighbour", 1, "/neighbour");
neighbour.record.module_id = ModuleId::parse("neighbour").unwrap();
service.deploy(neighbour).unwrap();
let crashed = ModuleId::parse("hello").unwrap();

let outcome = service
.redeploy_reaped_after_crash(&crashed, &Revision(1), "pair-a")
.unwrap()
.expect("the active revision is brought back");
assert_eq!(outcome.record.observed_state, ObservedState::Active);
assert_eq!(*stops.lock().unwrap(), 0, "the reaped runtime is not stopped again");
let neighbour = service
.store
.load(&ModuleId::parse("neighbour").unwrap())
.unwrap()
.unwrap();
assert_eq!(
(neighbour.observed_state, neighbour.sticky_reason),
(ObservedState::Active, None),
"the other module is not redeployed"
);

// Nothing to restore once the revision is no longer the active one.
assert!(
service
.redeploy_reaped_after_crash(&crashed, &Revision(7), "pair-b")
.unwrap()
.is_none()
);
}
#[test]
fn successful_replacement_stops_the_previous_active_target() {
let stops = Arc::new(Mutex::new(0));
let service = DeploymentService::new(
Expand Down
23 changes: 12 additions & 11 deletions crates/protrus-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use std::{
path::PathBuf,
sync::{
Arc, Mutex,
atomic::{AtomicBool, Ordering},
atomic::{AtomicBool, AtomicU64, Ordering},
},
thread::{self, JoinHandle},
time::Duration,
Expand Down Expand Up @@ -2215,8 +2215,8 @@ where

/// Crash callback for the production OCI runtime factory. It first drops the
/// dead pair's local lifecycle and proxy state, then asks the deployment owner
/// to reconcile only persisted signed records. The watcher never launches a
/// replacement directly.
/// to bring that module back from its persisted signed record. The watcher
/// never launches a replacement directly.
pub struct ReapingDeploymentReconcileTrigger<S, E, R>
where
S: ModuleStore,
Expand Down Expand Up @@ -2253,16 +2253,17 @@ where
E: OciEngine + Clone,
R: OciLaunchResolver,
{
/// Brings back only the crashed module from its persisted signed record. Reconciling every
/// stored record here would prepare the live pairs of the other modules again, which fails
/// with "runtime already exists" and leaves them degraded.
fn reconcile_stored(&self) -> Result<(), String> {
static ATTEMPTS: AtomicU64 = AtomicU64::new(0);
self.factory.reap_crashed(&self.record)?;
let outcomes = self
.service
.reconcile_stored()
.map_err(|error| error.to_string())?;
if let Some(error) = outcomes.into_iter().find_map(Result::err) {
return Err(error.to_string());
}
Ok(())
let attempt = format!("pair-{}", ATTEMPTS.fetch_add(1, Ordering::Relaxed));
self.service
.redeploy_reaped_after_crash(&self.record.module_id, &self.record.revision, &attempt)
.map(|_| ())
.map_err(|error| error.to_string())
}
}

Expand Down
Loading