Lines
83.65 %
Functions
66.67 %
Branches
100 %
//! Bringing every tenant database to the current schema.
//!
//! `boot()` migrates only the global database, and provisioning migrates a
//! tenant once, at creation. Nothing else ever touched an existing tenant — so
//! before this module the first tenant-facing migration after launch reached
//! the global database and newly registered users, and no existing user, ever.
//! CI stayed green (fresh databases), the deploy stayed green (global
//! migrates), and each pre-existing user broke at runtime, one feature at a
//! time.
//! This runs as its own process, never inside a serving one. `boot()` is
//! awaited before either binary serves and a boot error exits, so a fan-out
//! there would let one unreachable tenant crash-loop web and sshd for every
//! user.
//! Reconciliation is against each tenant's own `_sqlx_migrations`, via the
//! ordinary migrator. There is deliberately no central version table: applied
//! state is already recorded per tenant, a mirror can drift, and under the
//! shared DDL-only rule its `CREATE TABLE` would land in every tenant database
//! anyway.
use crate::db::{DBError, migrate_on};
use crate::provision::dsn_for;
use sqlx::types::Uuid;
use sqlx::{Connection, PgConnection};
use std::time::Duration;
mod directory;
pub(crate) mod jobs;
mod schema;
pub(crate) use directory::inventory;
use directory::{database_now, publish};
#[cfg(test)]
pub(crate) use jobs::run as run_tenant_jobs;
use schema::assert_global_schema_current;
use schema::schema_drift;
mod tests;
// Tenants are migrated ONE AT A TIME, deliberately.
//
// Every concurrent worker costs a tenant connection on top of the admin pool,
// and ADR 0032 reserves five connections for this whole process; four workers
// spent about thirteen. Sequential also keeps the migration on the very
// connection that holds the claim, which a spawned worker cannot do — sqlx's
// migrator future is not `Send` while it borrows a connection.
// The cost is wall-clock on a large fleet. Revisit by giving the migrator its
// own capped pool and a `Send`-safe worker, not by adding threads here.
/// Ceiling on one tenant's migration. Without it a single tenant holding a lock
/// stalls the whole run.
const TENANT_DEADLINE: Duration = Duration::from_secs(120);
/// Bound on reaching a tenant at all, so connect cannot eat the outer budget.
const CONNECT_DEADLINE: Duration = Duration::from_secs(10);
/// Backstop around connect, claim and migration together. Looser than
/// [`TENANT_DEADLINE`] so the inner deadline normally fires first and gets to
/// close the connection.
const VISIT_DEADLINE: Duration = Duration::from_secs(200);
/// Connection attempts before a tenant is quarantined.
const CONNECT_ATTEMPTS: u32 = 2;
/// Pause between those attempts.
const CONNECT_RETRY_DELAY: Duration = Duration::from_secs(3);
/// Advisory-lock key, so two migrator processes cannot work one tenant at once.
///
/// Taken inside the *tenant's own* database rather than a shared table: there
/// is no central state to drift, and a lock dies with the connection holding
/// it, so a killed worker frees its claim without a lease to expire.
const CLAIM_LOCK: i64 = 0x6e6f_6d69_0001;
/// How often to re-attempt a claim another worker currently holds.
const CLAIM_POLL: Duration = Duration::from_secs(2);
/// How many times the directory is re-read looking for tenants that appeared
/// mid-run. Bounded so a steady stream of registrations cannot keep the Job
/// alive forever; anything still missed is caught by the next release's run.
const INVENTORY_ROUNDS: usize = 3;
/// How long to wait out a peer's claim before giving up on this tenant.
/// Bounded well inside [`TENANT_DEADLINE`] so the migration itself still has
/// room once the claim is won.
const CLAIM_DEADLINE: Duration = Duration::from_secs(45);
/// What became of one tenant.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
/// Verified at the current schema by *this* run.
Migrated,
/// Another worker held the claim for the whole deadline, so this run never
/// saw the tenant's schema. Not a success: the holder may since have timed
/// out, panicked or been killed, and nothing would say so.
Unresolved,
/// Failed, and recorded in the global directory.
Quarantined(String),
/// The schema is current but a data job failed. Not a quarantine: the
/// tenant is served normally and the job is retried by the next run.
JobFailed(String),
}
#[derive(Debug, Default)]
pub struct FleetReport {
pub migrated: usize,
/// Tenants this run could not verify because another worker held the claim.
pub unresolved: Vec<Uuid>,
pub quarantined: Vec<(Uuid, String)>,
/// Tenants at the current schema whose data job failed.
pub job_failed: Vec<(Uuid, String)>,
/// Tenants whose verdict could not be written to the directory. The
/// dangerous case: a tenant that failed but whose quarantine did not stick
/// is still being served.
pub unpublished: Vec<(Uuid, String)>,
impl FleetReport {
/// Whether every tenant was accounted for.
/// A quarantined tenant does not stop the *fleet* — that is the whole point
/// of quarantine — but it does mean this run left work undone, and the exit
/// code is what a deployment gate reads. Unresolved and unpublished tenants
/// count too: in both cases the run finished without knowing, or without
/// recording, the state of a customer's database.
#[must_use]
pub const fn is_healthy(&self) -> bool {
self.quarantined.is_empty()
&& self.job_failed.is_empty()
&& self.unresolved.is_empty()
&& self.unpublished.is_empty()
pub fn summary(&self) -> String {
format!(
"migrated {}, quarantined {}, job failed {}, unresolved {}, unpublished {}",
self.migrated,
self.quarantined.len(),
self.job_failed.len(),
self.unresolved.len(),
self.unpublished.len()
)
/// Migrates one tenant, holding a claim for the duration.
/// Waits for a claim another worker holds rather than declaring victory over a
/// tenant it never looked at. Once the claim is won the migrator runs
/// regardless of what the previous holder did: it is idempotent, so running it
/// after a peer's success costs a no-op, and running it after a peer *died*
/// mid-way is exactly the repair that is needed.
async fn migrate_tenant(database_name: &str) -> Result<Outcome, DBError> {
let dsn = dsn_for(database_name)?;
let mut conn = connect(&dsn).await?;
if !claim(&mut conn, CLAIM_DEADLINE).await? {
let _ = conn.close().await;
return Ok(Outcome::Unresolved);
// Migrated on the claim connection itself. A second connection per worker
// would put the run at roughly thirteen against the five ADR 0032 reserves
// for it — a budget breach during exactly the operation that must not fail.
// The deadline is applied HERE rather than around the whole call: cancelling
// this future from outside would drop the connection without closing it.
let migrated = tokio::time::timeout(TENANT_DEADLINE, migrate_on(&mut conn)).await;
let outcome = match migrated {
Ok(Ok(())) => Ok(run_jobs(&mut conn).await),
Ok(Err(e)) => Err(e),
Err(_) => Ok(Outcome::Quarantined(format!(
"migration exceeded {}s",
TENANT_DEADLINE.as_secs()
))),
};
// Closing the connection drops the lock; do it whether or not we succeeded,
// and whether or not we ran out of time.
outcome
/// The data jobs of a tenant whose schema is already verified: a failure here
/// leaves the schema intact, so it must not quarantine.
async fn run_jobs(conn: &mut PgConnection) -> Outcome {
match tokio::time::timeout(TENANT_DEADLINE, jobs::run(conn)).await {
Ok(Ok(())) => Outcome::Migrated,
Ok(Err(e)) => Outcome::JobFailed(e.to_string()),
Err(_) => Outcome::JobFailed(format!("data jobs exceeded {}s", TENANT_DEADLINE.as_secs())),
/// Connects to a tenant, retrying a refused or slow connection once.
/// The only retry in a visit, and deliberately before any claim is taken: a
/// migration that failed may still hold its advisory lock, so retrying one
/// would find its own claim and learn nothing.
async fn connect(dsn: &str) -> Result<PgConnection, DBError> {
let mut attempt = 1;
loop {
match tokio::time::timeout(CONNECT_DEADLINE, PgConnection::connect(dsn)).await {
Ok(Ok(conn)) => return Ok(conn),
_ if attempt < CONNECT_ATTEMPTS => {
tokio::time::sleep(CONNECT_RETRY_DELAY).await;
attempt += 1;
Ok(Err(e)) => return Err(DBError::Sqlx(e)),
Err(_) => return Err(DBError::ConnectTimedOut),
/// Takes the tenant's claim, waiting out a peer that already holds it.
/// Polls rather than using the blocking `pg_advisory_lock`, so the surrounding
/// timeout can actually interrupt it — a blocked lock wait inside Postgres
/// would ignore a cancelled future and hold the connection open.
async fn claim(conn: &mut PgConnection, deadline: Duration) -> Result<bool, DBError> {
let give_up_at = tokio::time::Instant::now() + deadline;
let taken: bool = sqlx::query_scalar("SELECT pg_try_advisory_lock($1)")
.bind(CLAIM_LOCK)
.fetch_one(&mut *conn)
.await
.map_err(DBError::Sqlx)?;
if taken {
return Ok(true);
if tokio::time::Instant::now() >= give_up_at {
return Ok(false);
tokio::time::sleep(CLAIM_POLL).await;
/// Runs one tenant to a verdict, converting any failure into a quarantine
/// reason rather than propagating it — one sick tenant must not end the run.
/// A timeout quarantines rather than passing: the migration was cancelled
/// mid-flight, so its transaction rolled back and the tenant is on whatever
/// schema it had before. Unknown is not the same as fine.
async fn visit(database_name: &str) -> Outcome {
// The backstop around connect, claim and migration together; looser than
// `TENANT_DEADLINE` so the inner deadline normally fires first.
match tokio::time::timeout(VISIT_DEADLINE, migrate_tenant(database_name)).await {
Ok(Ok(outcome)) => outcome,
Ok(Err(e)) => Outcome::Quarantined(e.to_string()),
Err(_) => Outcome::Quarantined(format!("tenant exceeded {}s", VISIT_DEADLINE.as_secs())),
/// Migrates every tenant in the directory.
/// Skips `admin_user_id`, whose row points at the global database that the
/// ordinary boot migration already handled. Quarantined tenants are retried:
/// quarantine records a failure, it is not a tombstone.
/// Never seeds. `server/seed.sql` must not run against a per-user database —
/// an implementer reusing boot-shaped logic here would insert the admin user
/// and global config into every tenant.
/// # Errors
/// Only for failures that prevent the run itself: an unreachable *global*
/// directory, or a missing `DATABASE_URL`. A tenant that cannot be migrated is
/// quarantined and reported, not raised.
pub async fn migrate_fleet(admin_user_id: Uuid) -> Result<FleetReport, DBError> {
// Before anything: never fan out from a directory older than this binary.
assert_global_schema_current().await?;
// The database's clock, not this host's. The fence compares against
// `quarantined_at`, written by `now()` inside Postgres; comparing that to a
// migrator host's clock makes correctness a function of clock skew between
// two machines.
let started = database_now().await?;
let mut report = FleetReport::default();
let mut seen: Vec<Uuid> = Vec::new();
// Re-inventoried until it stops growing. A single snapshot would miss a
// tenant registered while the run was in progress — by an old pod still
// serving, whose provisioning carries *its* migration set — and the run
// would still exit 0 over a tenant nobody brought current.
// This NARROWS the window; it does not close it. A tenant registered after
// the last read is still missed, and the run still exits 0. Closing it needs
// a barrier between provisioning and this run — a shared lock, or a schema
// generation that provisioning stamps and this reconciles against. Until
// then the guarantee rests where ADR 0032 puts it: expand/contract, so the
// serving code works on both schemas across a release.
for round in 1..=INVENTORY_ROUNDS {
let pending: Vec<(Uuid, String)> = inventory(admin_user_id)
.await?
.into_iter()
.filter(|(id, _)| !seen.contains(id))
.collect();
if pending.is_empty() {
break;
tracing::info!(
"fleet migration round {round}: {} tenant(s) to visit",
pending.len()
);
for (id, database_name) in pending {
seen.push(id);
let outcome = visit(&database_name).await;
// Published as each tenant lands. Collecting verdicts first would
// leave a tenant that failed early still being served for as long
// as the rest of the fleet takes.
if let Err(e) = publish(id, &outcome, started).await {
tracing::error!("could not record the verdict for {database_name}: {e}");
report.unpublished.push((id, e.to_string()));
match outcome {
Outcome::Migrated => report.migrated += 1,
Outcome::Unresolved => {
tracing::warn!(
"{database_name} stayed claimed by another worker; this run did not verify it"
report.unresolved.push(id);
Outcome::JobFailed(reason) => {
tracing::error!("data job failed on {database_name}: {reason}");
report.migrated += 1;
report.job_failed.push((id, reason));
Outcome::Quarantined(reason) => {
tracing::error!("quarantining {database_name}: {reason}");
report.quarantined.push((id, reason));
tracing::info!("fleet migration finished: {}", report.summary());
Ok(report)