-- Schema version 1 of the hosted controller store. Controller times (columns -- ending _t) are integer nanoseconds of controller time. Wall times (columns -- ending _unix_ns) are Unix nanoseconds. Migrations are additive: a later -- version adds a new file and never edits this one. -- One row per attempt, created as the launch intent before any provider call. CREATE TABLE jobs ( attempt_id TEXT PRIMARY KEY, launch_id TEXT NOT NULL UNIQUE, vm_ref TEXT UNIQUE, -- absent until the provider answers task_id TEXT NOT NULL, account_id TEXT NOT NULL, actor_key TEXT NOT NULL, -- the verified key that asked for the launch purpose TEXT NOT NULL, replaces_attempt TEXT, -- absent unless this attempt replaces another spec_json TEXT NOT NULL, spec_sha256 TEXT NOT NULL, provider_fingerprint TEXT NOT NULL DEFAULT '', -- the provider settings the spec launches with (Config.ProviderFingerprint) state TEXT NOT NULL, launched_t INTEGER NOT NULL, deadline_t INTEGER NOT NULL, drain_t INTEGER NOT NULL, stop_t INTEGER NOT NULL, epoch INTEGER NOT NULL DEFAULT 0, pause_generation INTEGER NOT NULL DEFAULT 0, pause_started_t INTEGER, -- absent unless a pause is open pause_quiescent INTEGER NOT NULL DEFAULT 0 CHECK (pause_quiescent IN (0, 1)), pause_cancelled INTEGER NOT NULL DEFAULT 0 CHECK (pause_cancelled IN (0, 1)), stop_requested_t INTEGER, -- absent until a stop is asked for stop_reason TEXT, stop_proof_json TEXT, -- absent until a stop is proven notfound_since_t INTEGER, -- first not-found from Get since the VM was last seen live; absent otherwise last_dispatch_t INTEGER, -- when the last provider launch call for this attempt was about to be made drain_marked_t INTEGER, -- when the reaper found the job past drain_t; absent until then revision INTEGER NOT NULL DEFAULT 0 ); CREATE INDEX jobs_state ON jobs(state); -- The launch spec is what the provider was asked for, with the provider -- settings it launches under; it never changes. CREATE TRIGGER jobs_spec_immutable BEFORE UPDATE OF spec_json, spec_sha256, provider_fingerprint ON jobs WHEN NEW.spec_json IS NOT OLD.spec_json OR NEW.spec_sha256 IS NOT OLD.spec_sha256 OR NEW.provider_fingerprint IS NOT OLD.provider_fingerprint BEGIN SELECT RAISE(ABORT, 'spec is immutable'); END; -- Jobs rows are never deleted: a deleted row would lose its spec and its -- resume history. The store turns on recursive_triggers, so the implicit -- delete of INSERT OR REPLACE fires this trigger too. CREATE TRIGGER jobs_never_deleted BEFORE DELETE ON jobs BEGIN SELECT RAISE(ABORT, 'jobs are never deleted'); END; -- Resume operations, kept for the attempt's lifetime so a retried operation -- ID returns its original epoch. CREATE TABLE resume_ops ( attempt_id TEXT NOT NULL REFERENCES jobs(attempt_id), operation_id TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('pending', 'done', 'failed')), prepared_epoch INTEGER NOT NULL, result_epoch INTEGER, -- absent until done prepared_t INTEGER NOT NULL, completed_t INTEGER, -- absent until done or failed PRIMARY KEY (attempt_id, operation_id) ); -- At most one pending resume per attempt. CREATE UNIQUE INDEX resume_ops_one_pending ON resume_ops(attempt_id) WHERE status = 'pending'; -- VMs the provider lists that match no job, with when the controller first -- saw each. CREATE TABLE unmatched_vms ( vm_ref TEXT PRIMARY KEY, first_seen_t INTEGER NOT NULL ); -- Request nonces held until they expire, so a replay is refused. CREATE TABLE nonces ( key_id TEXT NOT NULL, nonce TEXT NOT NULL, until_unix_ns INTEGER NOT NULL, PRIMARY KEY (key_id, nonce) ); CREATE INDEX nonces_until ON nonces(until_unix_ns); -- The latest expiry among pruned nonces: one row. A nonce valid only up to -- this time may already have been forgotten, so Remember denies it. CREATE TABLE nonce_pruned ( id INTEGER PRIMARY KEY CHECK (id = 1), through_unix_ns INTEGER NOT NULL ); -- Every decision the controller takes. Never holds credentials. CREATE TABLE audit ( id INTEGER PRIMARY KEY AUTOINCREMENT, actor_key TEXT NOT NULL, -- empty for the controller itself account_id TEXT NOT NULL, task_id TEXT NOT NULL, attempt_id TEXT NOT NULL, action TEXT NOT NULL CHECK (action <> ''), reason TEXT NOT NULL, at_t INTEGER NOT NULL ); -- The last controller-time checkpoint: one row. CREATE TABLE clock ( id INTEGER PRIMARY KEY CHECK (id = 1), t_ns INTEGER NOT NULL CHECK (t_ns >= 0), wall_unix_ns INTEGER NOT NULL ); -- The provider namespace (for AWS, account and region) the VM references in -- this database belong to: one row, written by the first controller and -- never changed. A controller set up for another namespace refuses to start. CREATE TABLE provider_binding ( id INTEGER PRIMARY KEY CHECK (id = 1), namespace TEXT NOT NULL CHECK (namespace <> '') ); CREATE TRIGGER provider_binding_fixed BEFORE UPDATE ON provider_binding BEGIN SELECT RAISE(ABORT, 'provider binding is fixed'); END; -- Alerts raised where no Alerter exists yet (Open), kept until delivered. -- Delivery is at least once; the id is the alert's stable event ID. CREATE TABLE alert_outbox ( id INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, detail TEXT NOT NULL, at_t INTEGER NOT NULL, delivered INTEGER NOT NULL DEFAULT 0 CHECK (delivered IN (0, 1)) );