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
68 changes: 50 additions & 18 deletions crates/nibrunnerd/src/adapters/volumes/customer/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;

use protocol::{AppId, DeploymentId, DesiredInstanceState, DesiredPresence, HostDesiredState, StateMessage};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use super::profile::{failed, io_error, sync_dir, Placement};
use super::runtime::CustomerVolumes;
Expand Down Expand Up @@ -34,6 +35,8 @@ pub(super) struct Migration {
attempt: String,
#[serde(default)]
source: SourceState,
#[serde(default)]
request_hash: Option<String>,
verified: bool,
#[serde(default)]
checking_device: bool,
Expand All @@ -50,6 +53,7 @@ impl Migration {
Self {
attempt: uuid::Uuid::new_v4().to_string(),
source: SourceState::Checking,
request_hash: None,
verified: false,
checking_device: false,
discard_candidate: false,
Expand All @@ -58,6 +62,10 @@ impl Migration {
}
}

pub(super) fn rejected(&self) -> bool {
self.serves_shared() && matches!(self.source, SourceState::Failed { .. })
}

pub(super) fn fenced(&self) -> bool {
self.retained.is_none() && !self.serves_shared()
}
Expand Down Expand Up @@ -189,35 +197,43 @@ impl CustomerVolumes {
.iter()
.find(|i| i.app_id == p.app)
.map(|i| i.deployment_id.clone());
match Migration::load(&p.directory(&self.placements.root)) {
Ok(Some(Migration {
source:
SourceState::Failed {
deployment: previous, ..
},
verified: false,
..
})) if previous == deployment => continue,
Ok(Some(migration))
if migration.verified
|| desired.volumes.iter().any(|v| {
v.app_id == p.app
&& v.storage.is_some()
&& v.desired_state == DesiredPresence::Present
}) => {}
let migration = match Migration::load(&p.directory(&self.placements.root)) {
Ok(Some(m)) => m,
Ok(None) => continue,
Err(error) => {
tracing::error!(app_id = %p.app, %error, "cannot resume volume migration");
continue;
}
_ => continue,
};
let mut request = None;
if !migration.verified {
let Some(storage) = desired
.volumes
.iter()
.find(|v| v.app_id == p.app && v.desired_state == DesiredPresence::Present)
.and_then(|v| v.storage.as_ref())
else {
continue;
};
let Ok(config) = p.config(storage) else { continue };
let hash = hex::encode(Sha256::digest(config.as_bytes()));
if let SourceState::Failed {
deployment: previous, ..
} = &migration.source
{
if *previous == deployment && migration.request_hash.as_ref() == Some(&hash) {
continue;
}
}
request = Some((config, hash));
}
let Some(operation) = self.try_operation(&p.app) else {
continue;
};
let (host, volumes) = (host.clone(), self.clone());
tokio::spawn(async move {
let _operation = operation;
if let Err(error) = volumes.migrate(&host, &p, deployment).await {
if let Err(error) = volumes.migrate(&host, &p, deployment, request).await {
tracing::warn!(app_id = %p.app, %error, "volume migration will retry");
host.state
.modify(|state| {
Expand Down Expand Up @@ -379,6 +395,7 @@ impl CustomerVolumes {
host: &Host,
p: &Placement,
deployment: Option<DeploymentId>,
request: Option<(String, String)>,
) -> Result<(), VolumeError> {
let dir = p.directory(&self.placements.root);
let Some(mut migration) = Migration::load(&dir)? else {
Expand All @@ -389,6 +406,21 @@ impl CustomerVolumes {
if !migration.verified && matches!(migration.source, SourceState::Restoring { .. }) {
return self.restore_source(p, &dir, &mut migration).await;
}
if let Some((config, hash)) = request {
if migration.rejected() {
let current = dir.join(super::profile::WRITER_CONFIG);
if std::fs::read_to_string(&current).map_err(io_error)? != config {
let mut updated = tempfile::NamedTempFile::new_in(&dir).map_err(io_error)?;
updated
.write_all(config.as_bytes())
.and_then(|()| updated.as_file().sync_all())
.map_err(io_error)?;
updated.persist(current).map_err(|e| io_error(e.error))?;
sync_dir(&dir)?;
}
}
migration.request_hash = Some(hash);
}
if migration.serves_shared() {
let probe = async {
let writer = self.active(p).await?;
Expand Down
4 changes: 3 additions & 1 deletion crates/nibrunnerd/src/adapters/volumes/customer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ impl CustomerPlacements {
if volume.desired_state == DesiredPresence::Present {
return Err(failed("this customer volume has been retired"));
}
} else if std::fs::read_to_string(dir.join(WRITER_CONFIG)).map_err(io_error)? != config {
} else if std::fs::read_to_string(dir.join(WRITER_CONFIG)).map_err(io_error)? != config
&& !migration::Migration::load(&dir)?.is_some_and(|m| m.rejected())
{
return Err(failed(
"the requested writer settings differ from the staged customer writer",
));
Expand Down
12 changes: 10 additions & 2 deletions docs/content/docs/(docs)/guides/volumes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,21 @@ customer's bucket credentials. Both are needed for independent recovery.
1. Assign an exclusive customer bucket prefix and keep the existing app ID and volume ID.
In the desired document, change that volume’s `storage` from `null` to the customer settings.
Finish in-flight exports first. Existing checkpoint declarations may remain during the move.
2. Allow downtime for the copy. The host drains cron runs, freezes the guest filesystem, and
2. The host checks the customer writer while the shared disk continues serving. It writes,
flushes, reads back, and removes a probe. A rejected preflight leaves the shared disk active.
3. Allow downtime for the copy. The host drains cron runs, freezes the guest filesystem, and
stops the guest. It streams the disk through the two ZeroFS writers, checks the bytes,
and flushes the destination. A conflicting destination image leaves the source intact.
3. The host records cutover durably, removes the source image from shared ZeroFS, flushes the
4. The host records cutover durably, removes the source image from shared ZeroFS, flushes the
removal, and resumes the app under its existing identity. An interrupted transfer retries.
Check the volume and instance reports for readiness.

If a copy or destination attachment fails before commit, the host releases any destination
attachment and resumes the original shared disk. Check the volume report for the failure.
Correct the writer settings for the same bucket and prefix, or request a new deployment to
retry after a transient outage. A restart of the host preserves the failed request. After
commit the customer disk stays authoritative, including while shared history is being cleaned up.

Shared ZeroFS checkpoints cover every volume on that writer. Cleanup deletes checkpoints
whose recorded volume and generation match the source. The guest can run on its new disk
while the volume report says that historical shared checkpoints remain. Checkpoints owned
Expand Down