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
5 changes: 5 additions & 0 deletions docs/api/error-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,11 @@
"name": "migration",
"description": null,
"errors": [
{
"code": "actor_deleted",
"message": "The parked actor is being deleted from V2, so it can no longer be restored.",
"statuses": []
},
{
"code": "actor_not_found",
"message": "The actor does not exist in this namespace.",
Expand Down
5 changes: 5 additions & 0 deletions engine/artifacts/errors/migration.actor_deleted.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions engine/packages/api-public/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ pub enum Migration {
"Migration is unavailable until the actor migration fix backfill finishes. It runs on engine start; retry once it completes."
)]
BackfillIncomplete,
#[error(
"actor_deleted",
"The parked actor is being deleted from V2, so it can no longer be restored."
)]
ActorDeleted,
}
22 changes: 20 additions & 2 deletions engine/packages/api-public/src/migration/candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ pub async fn candidates(ctx: ApiCtx, namespace: String, query: CandidatesQuery)
None => None,
};

// The scan covers every actor name and includes sleeping actors.
// The scan includes sleeping actors and covers every actor name, or only `name` when set.
let active_subspace =
keys::subspace().subspace(&(NAMESPACE, namespace_id, ACTOR, BY_NAME, ACTIVE));
let (range_start, range_end) = active_subspace.range();
if let Some(after) = &after {
if !after.starts_with(active_subspace.bytes()) {
return Err(errors::Migration::InvalidRequest {
Expand All @@ -53,6 +52,25 @@ pub async fn candidates(ctx: ApiCtx, namespace: String, query: CandidatesQuery)
.build());
}
}
let scanned_subspace = match &query.name {
Some(name) => active_subspace.subspace(&(name.as_str(),)),
None => active_subspace.clone(),
};
let (range_start, range_end) = scanned_subspace.range();

// A cursor is a position in the namespace's whole index, so one written by an unfiltered
// listing or under another name still resumes a filtered one: a cursor before the name's
// rows starts at its first row, and one past them leaves nothing to list.
let after = match after {
Some(after) if after < range_start => None,
Some(after) if after >= range_end => {
return proto::encode(&proto::CandidatesResponse {
candidates: Vec::new(),
next: None,
});
}
after => after,
};

let hibernating_since_ts = facts::hibernating_since_ts(
ctx.config()
Expand Down
10 changes: 9 additions & 1 deletion engine/packages/api-public/src/migration/facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,16 @@ pub fn decode_parked(raw: &[u8]) -> Result<v2_migration_protocol::ParkedRecord>
.context("decode parked record")
}

/// Encodes a parked record. A record with no delete in progress is written as v1, which every
/// V2 binary reads, so a rollout that adds deletes never leaves an older reader with a record it
/// cannot decode until a delete is first requested.
pub fn encode_parked(record: v2_migration_protocol::ParkedRecord) -> Result<Vec<u8>> {
let version = if record.delete_requested_at.is_some() || record.deleted_at.is_some() {
2
} else {
1
};
v2_migration_protocol::versioned::ParkedRecord::wrap_latest(record)
.serialize_with_embedded_version(1)
.serialize_with_embedded_version(version)
.context("encode parked record")
}
24 changes: 24 additions & 0 deletions engine/packages/api-public/src/migration/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,27 @@ pub fn uncut(actor_id: Id) -> Vec<u8> {
pub fn parked(actor_id: Id) -> Vec<u8> {
pegboard::keys::subspace().pack(&(MIGRATION, PARKED, actor_id))
}

/// Every parked record, in actor id order across all namespaces.
pub fn parked_subspace() -> Subspace {
pegboard::keys::subspace().subspace(&(MIGRATION, PARKED))
}

/// The actor id of a parked record's key, or `None` for a key outside [`parked_subspace`].
pub fn parked_actor_id(raw_key: &[u8]) -> Option<Id> {
parked_subspace()
.unpack::<(Id,)>(raw_key)
.ok()
.map(|(actor_id,)| actor_id)
}

/// The deletion cursor of one namespace in this datacenter: the parked key of the last actor a
/// deletion run finished. Value: the raw parked key.
pub fn delete_cursor(namespace_id: Id) -> Vec<u8> {
pegboard::keys::subspace().pack(&(MIGRATION, DELETE_CURSOR, namespace_id))
}

/// The chunks of the workflow's output, which gasoline writes when the workflow completes.
pub fn output_subspace(workflow_id: Id) -> Subspace {
gasoline_subspace().subspace(&(WORKFLOW, DATA, workflow_id, OUTPUT))
}
47 changes: 45 additions & 2 deletions engine/packages/api-public/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! The `/migration/v1/` API the V3 migrator drives to move one namespace's actors out of V2.
//! The `/migration/` API the V3 migrator drives to move one namespace's actors out of V2.
//!
//! Every route is a stateless handler over existing pegboard, gasoline and depot state, so the
//! module does nothing until called. Bodies are raw BARE from `v2-migration-protocol`, pinned by the
//! `/v1` path.
//! path: the `/v1` routes move actors and the `/v2` routes delete the ones already moved (see
//! [`parked`]).
//!
//! The protocol per actor is list (or a read of one candidate by id), export, cutover, confirm, and
//! optionally restore:
Expand Down Expand Up @@ -39,6 +40,7 @@ mod export;
mod facts;
pub mod keys;
mod park;
mod parked;

#[derive(Debug, Deserialize)]
pub struct NamespacePath {
Expand All @@ -55,6 +57,15 @@ pub struct ActorPath {
pub struct CandidatesQuery {
pub after: Option<String>,
pub limit: Option<usize>,
/// Lists only actors with this name, oldest first.
pub name: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ParkedQuery {
pub after: Option<String>,
pub limit: Option<usize>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -124,6 +135,38 @@ pub async fn restore(
respond(park::restore(ctx, path.actor_id, query.namespace, body).await)
}

pub async fn parked(
Extension(ctx): Extension<ApiCtx>,
Path(path): Path<NamespacePath>,
Query(query): Query<ParkedQuery>,
) -> Response {
respond(parked::list(ctx, path.namespace, query).await)
}

pub async fn get_delete_cursor(
Extension(ctx): Extension<ApiCtx>,
Path(path): Path<NamespacePath>,
) -> Response {
respond(parked::get_delete_cursor(ctx, path.namespace).await)
}

pub async fn put_delete_cursor(
Extension(ctx): Extension<ApiCtx>,
Path(path): Path<NamespacePath>,
body: Bytes,
) -> Response {
respond(parked::put_delete_cursor(ctx, path.namespace, body).await)
}

pub async fn delete(
Extension(ctx): Extension<ApiCtx>,
Path(path): Path<ActorPath>,
Query(query): Query<ActorQuery>,
body: Bytes,
) -> Response {
respond(parked::delete(ctx, path.actor_id, query.namespace, body).await)
}

fn respond(result: Result<Vec<u8>>) -> Response {
match result {
Ok(body) => (
Expand Down
5 changes: 5 additions & 0 deletions engine/packages/api-public/src/migration/park.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ async fn park(
workflow_name,
wake_deadline_ts: actor.wake_deadline_ts,
wake_signals,
delete_requested_at: None,
deleted_at: None,
};
tx.set(
&mkeys::parked(actor.actor_id),
Expand Down Expand Up @@ -290,6 +292,9 @@ pub async fn restore(ctx: ApiCtx, actor_id: Id, namespace: String, body: Bytes)
return Ok(proto::RestoreResponse::NotParked);
};
let parked = facts::decode_parked(&raw)?;
if parked.delete_requested_at.is_some() {
return Err(errors::Migration::ActorDeleted.build());
}
if request
.cutover_ts
.is_some_and(|cutover_ts| cutover_ts != parked.cutover_ts)
Expand Down
Loading
Loading