210 lines
9.4 KiB
PL/PgSQL
210 lines
9.4 KiB
PL/PgSQL
-- +goose Up
|
|
-- Code practice keeps its own records next to the attempt and its result:
|
|
-- the practice items of an attempt with their private task content and
|
|
-- state, the submissions of candidate code, and one execution job per
|
|
-- submission. None of these rows is part of the attempt snapshot, the
|
|
-- idempotent attempt responses, or the result.
|
|
--
|
|
-- A submission and its execution job are inserted in one transaction, so a
|
|
-- job exists exactly when its submission committed; the practice queue
|
|
-- worker picks the job later. The job table is that transactional queue: a
|
|
-- separate event is not needed because nothing outside PostgreSQL has to
|
|
-- learn about the submission before the worker runs it.
|
|
|
|
-- One practice item of an attempt: the main practice or the additional one.
|
|
-- The ID is the item ID the candidate sees and the attempt item of the run
|
|
-- key. task is the private task content (languages, limits, checker, public
|
|
-- examples, and the hidden test groups); it never leaves the backend except
|
|
-- in a request to the code runner.
|
|
CREATE TABLE assessment_practices (
|
|
id uuid NOT NULL,
|
|
attempt_id uuid NOT NULL,
|
|
user_id uuid NOT NULL,
|
|
kind text NOT NULL,
|
|
task jsonb NOT NULL,
|
|
-- not_started: nothing submitted; pending: the latest final submission
|
|
-- waits for its verdict; finished: it has one; failed: the sandbox
|
|
-- infrastructure could not run it, and the practice can be submitted
|
|
-- again.
|
|
status text NOT NULL,
|
|
failure_reason text,
|
|
-- Time of the latest final submission, or NULL before the first one.
|
|
submitted_at timestamptz,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
CONSTRAINT assessment_practices_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_practices_id_owner_key UNIQUE (id, user_id),
|
|
CONSTRAINT assessment_practices_attempt_kind_key UNIQUE (attempt_id, kind),
|
|
CONSTRAINT assessment_practices_attempt_owner_fk FOREIGN KEY (attempt_id, user_id)
|
|
REFERENCES assessment_attempts (id, user_id) ON DELETE CASCADE,
|
|
CONSTRAINT assessment_practices_kind CHECK (kind IN ('main', 'additional')),
|
|
CONSTRAINT assessment_practices_task_object CHECK (
|
|
jsonb_typeof(task) = 'object' AND octet_length(task::text) <= 8388608
|
|
),
|
|
CONSTRAINT assessment_practices_status CHECK (
|
|
status IN ('not_started', 'pending', 'finished', 'failed')
|
|
),
|
|
CONSTRAINT assessment_practices_failure CHECK (
|
|
(status = 'failed') = (failure_reason IS NOT NULL)
|
|
AND (failure_reason IS NULL OR failure_reason IN ('sandbox_busy', 'sandbox_error'))
|
|
),
|
|
CONSTRAINT assessment_practices_submitted CHECK (
|
|
(status = 'not_started') = (submitted_at IS NULL)
|
|
),
|
|
CONSTRAINT assessment_practices_timestamps CHECK (
|
|
created_at <= updated_at
|
|
AND (submitted_at IS NULL OR submitted_at >= created_at)
|
|
)
|
|
);
|
|
|
|
-- The identity, owner, kind, and task content of a practice never change;
|
|
-- only its state moves.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_practice_immutable_task() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF NEW.id <> OLD.id OR NEW.attempt_id <> OLD.attempt_id OR NEW.user_id <> OLD.user_id OR
|
|
NEW.kind <> OLD.kind OR NEW.task <> OLD.task OR NEW.created_at <> OLD.created_at OR
|
|
NEW.updated_at < OLD.updated_at THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_practice_task_immutable',
|
|
MESSAGE = 'assessment practice identity and task are immutable';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_practice_immutable_task
|
|
BEFORE UPDATE ON assessment_practices
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_practice_immutable_task();
|
|
|
|
-- One submission of candidate code. public is a debug run against the
|
|
-- public examples and the candidate's own tests; final is the solution that
|
|
-- the hidden groups grade. The request part of a row is immutable: once
|
|
-- stored, the source of a submission can never be replaced, and the result
|
|
-- is written once.
|
|
--
|
|
-- idempotency_scope is the keyed digest of the operation, the
|
|
-- Idempotency-Key, and the account; it does not reveal the key. The generic
|
|
-- idempotency record of the same scope, written in the same transaction,
|
|
-- holds the request fingerprint and decides a replay for 24 hours; this
|
|
-- column only finds the submission that a replay returns.
|
|
CREATE TABLE assessment_practice_submissions (
|
|
id uuid NOT NULL,
|
|
practice_id uuid NOT NULL,
|
|
user_id uuid NOT NULL,
|
|
mode text NOT NULL,
|
|
language text NOT NULL,
|
|
source text NOT NULL,
|
|
source_sha256 bytea NOT NULL,
|
|
-- The candidate's own tests of a public run, or NULL.
|
|
custom_tests jsonb,
|
|
idempotency_scope bytea NOT NULL,
|
|
-- The validated run result, written once by the queue worker.
|
|
result jsonb,
|
|
result_at timestamptz,
|
|
created_at timestamptz NOT NULL,
|
|
CONSTRAINT assessment_practice_submissions_pkey PRIMARY KEY (id),
|
|
CONSTRAINT assessment_practice_submissions_practice_owner_fk FOREIGN KEY (practice_id, user_id)
|
|
REFERENCES assessment_practices (id, user_id) ON DELETE CASCADE,
|
|
CONSTRAINT assessment_practice_submissions_mode CHECK (mode IN ('public', 'final')),
|
|
CONSTRAINT assessment_practice_submissions_language CHECK (language IN ('go', 'python')),
|
|
CONSTRAINT assessment_practice_submissions_source CHECK (
|
|
char_length(source) >= 1 AND octet_length(source) <= 65536
|
|
),
|
|
CONSTRAINT assessment_practice_submissions_digests CHECK (
|
|
octet_length(source_sha256) = 32
|
|
AND octet_length(idempotency_scope) = 32
|
|
),
|
|
CONSTRAINT assessment_practice_submissions_custom_tests CHECK (
|
|
custom_tests IS NULL OR (
|
|
mode = 'public'
|
|
AND jsonb_typeof(custom_tests) = 'array'
|
|
AND octet_length(custom_tests::text) <= 262144
|
|
)
|
|
),
|
|
CONSTRAINT assessment_practice_submissions_result CHECK (
|
|
(result IS NULL) = (result_at IS NULL)
|
|
AND (result IS NULL OR (jsonb_typeof(result) = 'object' AND octet_length(result::text) <= 1048576))
|
|
AND (result_at IS NULL OR result_at >= created_at)
|
|
)
|
|
);
|
|
|
|
CREATE INDEX assessment_practice_submissions_practice_idx
|
|
ON assessment_practice_submissions (practice_id, mode, created_at, id);
|
|
|
|
-- Serves the replay of a submission by its idempotency scope.
|
|
CREATE INDEX assessment_practice_submissions_scope_idx
|
|
ON assessment_practice_submissions (user_id, idempotency_scope, created_at);
|
|
|
|
-- A submission is immutable except for its one-time result. Rows are removed
|
|
-- only by the cascade of an account, attempt, or practice deletion.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION assessment_practice_submission_guard() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' THEN
|
|
IF pg_trigger_depth() > 1 THEN
|
|
RETURN OLD;
|
|
END IF;
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_practice_submission_delete_forbidden',
|
|
MESSAGE = 'assessment practice submissions are removed only by cascade';
|
|
END IF;
|
|
IF (to_jsonb(NEW) - 'result' - 'result_at') IS DISTINCT FROM (to_jsonb(OLD) - 'result' - 'result_at') OR
|
|
OLD.result IS NOT NULL OR NEW.result IS NULL THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = '23000', CONSTRAINT = 'assessment_practice_submission_immutable',
|
|
MESSAGE = 'assessment practice submissions are immutable and get one result';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER assessment_practice_submission_guard
|
|
BEFORE UPDATE OR DELETE ON assessment_practice_submissions
|
|
FOR EACH ROW EXECUTE FUNCTION assessment_practice_submission_guard();
|
|
|
|
-- The execution job of a submission. mode is copied from the submission so
|
|
-- the worker can run final submissions before debug runs without a join.
|
|
CREATE TABLE assessment_practice_jobs (
|
|
submission_id uuid NOT NULL,
|
|
mode text NOT NULL,
|
|
-- queued: waits for the worker; running: the worker holds it; finished:
|
|
-- the result is stored; failed: the infrastructure gave up on it, which
|
|
-- is never a verdict on the code.
|
|
state text NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
finished_at timestamptz,
|
|
CONSTRAINT assessment_practice_jobs_pkey PRIMARY KEY (submission_id),
|
|
CONSTRAINT assessment_practice_jobs_submission_fk FOREIGN KEY (submission_id)
|
|
REFERENCES assessment_practice_submissions (id) ON DELETE CASCADE,
|
|
CONSTRAINT assessment_practice_jobs_mode CHECK (mode IN ('public', 'final')),
|
|
CONSTRAINT assessment_practice_jobs_state CHECK (
|
|
state IN ('queued', 'running', 'finished', 'failed')
|
|
),
|
|
CONSTRAINT assessment_practice_jobs_finished CHECK (
|
|
(state IN ('finished', 'failed')) = (finished_at IS NOT NULL)
|
|
AND created_at <= updated_at
|
|
AND (finished_at IS NULL OR finished_at >= created_at)
|
|
)
|
|
);
|
|
|
|
-- Serves the scan of unfinished jobs, which stays small while finished rows
|
|
-- accumulate.
|
|
CREATE INDEX assessment_practice_jobs_active_idx
|
|
ON assessment_practice_jobs (created_at, submission_id)
|
|
WHERE state IN ('queued', 'running');
|
|
|
|
-- +goose Down
|
|
DROP TABLE assessment_practice_jobs;
|
|
DROP TRIGGER assessment_practice_submission_guard ON assessment_practice_submissions;
|
|
DROP FUNCTION assessment_practice_submission_guard();
|
|
DROP TABLE assessment_practice_submissions;
|
|
DROP TRIGGER assessment_practice_immutable_task ON assessment_practices;
|
|
DROP FUNCTION assessment_practice_immutable_task();
|
|
DROP TABLE assessment_practices;
|