74 lines
3.0 KiB
SQL
74 lines
3.0 KiB
SQL
-- +goose Up
|
|
-- The practice queue: the execution jobs of practice submissions become
|
|
-- durable work items that one worker at a time runs on the code runner.
|
|
--
|
|
-- The queue holds at most one running and ten queued jobs. Admission and
|
|
-- the claim both take the transaction-scoped advisory lock of the queue and
|
|
-- count the unfinished jobs, so the limits hold across backend instances. A
|
|
-- job beyond them is never inserted: the command answers busy and stores
|
|
-- nothing that would consume an attempt.
|
|
--
|
|
-- A running job holds a lease. A worker that stops without finishing leaves
|
|
-- the lease to expire, and the next claim takes the job over with the same
|
|
-- generation, so the runner, which is idempotent by run ID, returns the run
|
|
-- it already has instead of starting a second one. Lease and retry times come
|
|
-- from the application clock.
|
|
ALTER TABLE assessment_practice_jobs
|
|
-- Generation of the run key: raised after the runner could not evaluate
|
|
-- the source, so the next try is a new run.
|
|
ADD COLUMN generation integer NOT NULL DEFAULT 0,
|
|
-- Failed tries and expired leases counted so far. A busy runner counts
|
|
-- nothing.
|
|
ADD COLUMN attempts integer NOT NULL DEFAULT 0,
|
|
-- When a worker may claim a queued job: at once, after the wait the runner
|
|
-- asked for when it was busy, or after the backoff of a failed try.
|
|
ADD COLUMN available_at timestamptz,
|
|
ADD COLUMN lease_owner uuid,
|
|
ADD COLUMN lease_expires_at timestamptz,
|
|
-- When the running try was claimed; it bounds how long a run may stay
|
|
-- unfinished on the runner.
|
|
ADD COLUMN started_at timestamptz,
|
|
-- Bounded cause of the last failed try; never runner or candidate text.
|
|
ADD COLUMN last_error_class text;
|
|
|
|
UPDATE assessment_practice_jobs SET available_at = created_at;
|
|
|
|
ALTER TABLE assessment_practice_jobs
|
|
ALTER COLUMN available_at SET NOT NULL,
|
|
ADD CONSTRAINT assessment_practice_jobs_lease_when_running CHECK (
|
|
(state = 'running') = (lease_owner IS NOT NULL)
|
|
AND (lease_owner IS NULL) = (lease_expires_at IS NULL)
|
|
AND (lease_owner IS NULL) = (started_at IS NULL)
|
|
),
|
|
ADD CONSTRAINT assessment_practice_jobs_counters CHECK (
|
|
generation >= 0 AND attempts BETWEEN 0 AND 100
|
|
),
|
|
ADD CONSTRAINT assessment_practice_jobs_error_class CHECK (
|
|
last_error_class IS NULL OR last_error_class IN (
|
|
'runner_unavailable',
|
|
'runner_infrastructure_failure',
|
|
'invalid_request',
|
|
'run_timeout',
|
|
'lease_expired'
|
|
)
|
|
);
|
|
|
|
-- Serves the claim of the next queued job.
|
|
CREATE INDEX assessment_practice_jobs_queued_idx
|
|
ON assessment_practice_jobs (available_at, created_at)
|
|
WHERE state = 'queued';
|
|
|
|
-- +goose Down
|
|
DROP INDEX assessment_practice_jobs_queued_idx;
|
|
ALTER TABLE assessment_practice_jobs
|
|
DROP CONSTRAINT assessment_practice_jobs_error_class,
|
|
DROP CONSTRAINT assessment_practice_jobs_counters,
|
|
DROP CONSTRAINT assessment_practice_jobs_lease_when_running,
|
|
DROP COLUMN last_error_class,
|
|
DROP COLUMN started_at,
|
|
DROP COLUMN lease_expires_at,
|
|
DROP COLUMN lease_owner,
|
|
DROP COLUMN available_at,
|
|
DROP COLUMN attempts,
|
|
DROP COLUMN generation;
|