feat(practice): store code submissions and practice states apart from attempts
This commit is contained in:
1 parent
d5e169e71b
commit
51770eea6c
61 files changed
+8584
-135
No files matched your search
@@ -0,0 +1,209 @@
|
||||
-- +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;
|
||||
@@ -0,0 +1,159 @@
|
||||
-- Lock order of a practice command: the idempotency record, the attempt in
|
||||
-- share mode, then the practice row. The attempt flows lock the attempt
|
||||
-- first as well, so a practice command never waits for them in the opposite
|
||||
-- order.
|
||||
|
||||
-- Locks the owner's attempt in share mode, so its state cannot change while
|
||||
-- a practice command decides on it, and reads the database time after the
|
||||
-- lock.
|
||||
-- name: LockAssessmentAttemptForPractice :one
|
||||
SELECT
|
||||
attempt.state,
|
||||
attempt.expires_at,
|
||||
clock_timestamp()::timestamptz AS database_time
|
||||
FROM assessment_attempts AS attempt
|
||||
WHERE
|
||||
attempt.id = sqlc.arg(attempt_id)
|
||||
AND attempt.user_id = sqlc.arg(user_id)
|
||||
FOR SHARE;
|
||||
|
||||
-- Locks one practice of the owner's attempt for a state change.
|
||||
-- name: LockAssessmentPractice :one
|
||||
SELECT
|
||||
practice.id,
|
||||
practice.attempt_id,
|
||||
practice.user_id,
|
||||
practice.kind,
|
||||
practice.task,
|
||||
practice.status,
|
||||
practice.failure_reason,
|
||||
practice.submitted_at,
|
||||
practice.created_at
|
||||
FROM assessment_practices AS practice
|
||||
WHERE
|
||||
practice.id = sqlc.arg(id)
|
||||
AND practice.attempt_id = sqlc.arg(attempt_id)
|
||||
AND practice.user_id = sqlc.arg(user_id)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: InsertAssessmentPractice :exec
|
||||
INSERT INTO assessment_practices (
|
||||
id,
|
||||
attempt_id,
|
||||
user_id,
|
||||
kind,
|
||||
task,
|
||||
status,
|
||||
failure_reason,
|
||||
submitted_at,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(attempt_id),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(kind),
|
||||
sqlc.arg(task),
|
||||
'not_started',
|
||||
NULL,
|
||||
NULL,
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(created_at)
|
||||
);
|
||||
|
||||
-- name: UpdateAssessmentPracticeState :execrows
|
||||
UPDATE assessment_practices
|
||||
SET
|
||||
status = sqlc.arg(status),
|
||||
failure_reason = sqlc.narg(failure_reason),
|
||||
submitted_at = sqlc.narg(submitted_at),
|
||||
updated_at = greatest(updated_at, sqlc.arg(updated_at)::timestamptz)
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- Finds the latest submission of the owner under an idempotency scope, which
|
||||
-- a replay returns.
|
||||
-- name: FindAssessmentPracticeSubmissionByScope :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.practice_id,
|
||||
submission.mode
|
||||
FROM assessment_practice_submissions AS submission
|
||||
WHERE
|
||||
submission.user_id = sqlc.arg(user_id)
|
||||
AND submission.idempotency_scope = sqlc.arg(idempotency_scope)
|
||||
ORDER BY submission.created_at DESC, submission.id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- The latest final submission of a practice: the solution the practice
|
||||
-- state describes.
|
||||
-- name: GetLatestFinalPracticeSubmission :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.language,
|
||||
submission.source_sha256
|
||||
FROM assessment_practice_submissions AS submission
|
||||
WHERE
|
||||
submission.practice_id = sqlc.arg(practice_id)
|
||||
AND submission.mode = 'final'
|
||||
ORDER BY submission.created_at DESC, submission.id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: InsertAssessmentPracticeSubmission :exec
|
||||
INSERT INTO assessment_practice_submissions (
|
||||
id,
|
||||
practice_id,
|
||||
user_id,
|
||||
mode,
|
||||
language,
|
||||
source,
|
||||
source_sha256,
|
||||
custom_tests,
|
||||
idempotency_scope,
|
||||
created_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(practice_id),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(mode),
|
||||
sqlc.arg(language),
|
||||
sqlc.arg(source),
|
||||
sqlc.arg(source_sha256),
|
||||
sqlc.narg(custom_tests),
|
||||
sqlc.arg(idempotency_scope),
|
||||
sqlc.arg(created_at)
|
||||
);
|
||||
|
||||
-- Queues the execution job of a submission in the transaction that stores
|
||||
-- the submission.
|
||||
-- name: InsertAssessmentPracticeJob :exec
|
||||
INSERT INTO assessment_practice_jobs (
|
||||
submission_id,
|
||||
mode,
|
||||
state,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(submission_id),
|
||||
sqlc.arg(mode),
|
||||
'queued',
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(created_at)
|
||||
);
|
||||
|
||||
-- Reads a debug run of the owner's attempt with the state of its job.
|
||||
-- name: GetAssessmentPracticeRun :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.practice_id,
|
||||
submission.created_at,
|
||||
submission.result,
|
||||
job.state,
|
||||
job.finished_at
|
||||
FROM assessment_practice_submissions AS submission
|
||||
INNER JOIN assessment_practices AS practice ON submission.practice_id = practice.id
|
||||
INNER JOIN assessment_practice_jobs AS job ON submission.id = job.submission_id
|
||||
WHERE
|
||||
submission.id = sqlc.arg(id)
|
||||
AND submission.user_id = sqlc.arg(user_id)
|
||||
AND submission.mode = 'public'
|
||||
AND practice.attempt_id = sqlc.arg(attempt_id);
|
||||
Reference in new issue
Block a user