feat(practice): queue practice runs with one running and ten waiting
This commit is contained in:
1 parent
51770eea6c
commit
754c745fba
40 files changed
+3040
-70
No files matched your search
@@ -0,0 +1,192 @@
|
||||
-- The practice queue. Admission and the claim take the same
|
||||
-- transaction-scoped advisory lock, so the count of unfinished jobs and the
|
||||
-- change that depends on it are atomic across backend instances. A command
|
||||
-- takes the lock after its attempt and practice row locks; the claim takes
|
||||
-- it first and then skips job rows that another transaction locks, so a
|
||||
-- claim never waits for a row lock while it holds the queue lock.
|
||||
|
||||
-- name: LockAssessmentPracticeQueue :exec
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('lincodex.assessment.practice_queue', 0));
|
||||
|
||||
-- Counts the queued and running jobs; the caller holds the queue lock.
|
||||
-- name: CountActiveAssessmentPracticeJobs :one
|
||||
SELECT count(*)::integer AS active
|
||||
FROM assessment_practice_jobs AS job
|
||||
WHERE job.state IN ('queued', 'running');
|
||||
|
||||
-- Counts the unfinished debug runs of a practice; the caller holds the
|
||||
-- practice row lock.
|
||||
-- name: CountActiveAssessmentPracticeRuns :one
|
||||
SELECT count(*)::integer AS active
|
||||
FROM assessment_practice_jobs AS job
|
||||
INNER JOIN assessment_practice_submissions AS submission ON job.submission_id = submission.id
|
||||
WHERE
|
||||
submission.practice_id = sqlc.arg(practice_id)
|
||||
AND job.mode = 'public'
|
||||
AND job.state IN ('queued', 'running');
|
||||
|
||||
-- Counts running jobs whose lease has not expired by now, the application
|
||||
-- clock; the caller holds the queue lock.
|
||||
-- name: CountLiveRunningAssessmentPracticeJobs :one
|
||||
SELECT count(*)::integer AS running
|
||||
FROM assessment_practice_jobs AS job
|
||||
WHERE
|
||||
job.state = 'running'
|
||||
AND job.lease_expires_at > sqlc.arg(now)::timestamptz;
|
||||
|
||||
-- Claims one job under the queue lock: first a running job whose lease
|
||||
-- expired (its worker stopped without finishing it), then the next queued
|
||||
-- job, final submissions before debug runs. A takeover keeps the generation,
|
||||
-- so the runner returns the run it already has, and counts one attempt.
|
||||
-- name: ClaimAssessmentPracticeJob :one
|
||||
WITH candidate AS (
|
||||
SELECT
|
||||
job.submission_id,
|
||||
job.state = 'running' AS reclaimed
|
||||
FROM assessment_practice_jobs AS job
|
||||
WHERE
|
||||
(job.state = 'queued' AND job.available_at <= sqlc.arg(now)::timestamptz)
|
||||
OR (job.state = 'running' AND job.lease_expires_at <= sqlc.arg(now)::timestamptz)
|
||||
ORDER BY
|
||||
job.state = 'running' DESC,
|
||||
job.mode = 'final' DESC,
|
||||
job.available_at ASC,
|
||||
job.created_at ASC,
|
||||
job.submission_id ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
|
||||
UPDATE assessment_practice_jobs AS job
|
||||
SET
|
||||
state = 'running',
|
||||
lease_owner = sqlc.arg(lease_owner),
|
||||
lease_expires_at = sqlc.arg(lease_expires_at),
|
||||
started_at = sqlc.arg(now)::timestamptz,
|
||||
attempts = job.attempts + CASE WHEN candidate.reclaimed THEN 1 ELSE 0 END,
|
||||
last_error_class = CASE
|
||||
WHEN candidate.reclaimed THEN 'lease_expired' ELSE job.last_error_class
|
||||
END,
|
||||
updated_at = greatest(job.updated_at, clock_timestamp())
|
||||
FROM candidate
|
||||
WHERE job.submission_id = candidate.submission_id
|
||||
RETURNING
|
||||
job.submission_id,
|
||||
job.mode,
|
||||
job.generation,
|
||||
job.attempts,
|
||||
candidate.reclaimed;
|
||||
|
||||
-- Reads what a claimed job runs: the submission and the task of its
|
||||
-- practice.
|
||||
-- name: GetAssessmentPracticeJobWork :one
|
||||
SELECT
|
||||
submission.id,
|
||||
submission.practice_id,
|
||||
submission.user_id,
|
||||
submission.mode,
|
||||
submission.language,
|
||||
submission.source,
|
||||
submission.custom_tests,
|
||||
submission.created_at,
|
||||
practice.task
|
||||
FROM assessment_practice_submissions AS submission
|
||||
INNER JOIN assessment_practices AS practice ON submission.practice_id = practice.id
|
||||
WHERE submission.id = sqlc.arg(submission_id);
|
||||
|
||||
-- Renews the lease of a running try. The owner and the start time identify
|
||||
-- the try, so a worker whose job was taken over changes nothing.
|
||||
-- name: ExtendAssessmentPracticeJobLease :execrows
|
||||
UPDATE assessment_practice_jobs
|
||||
SET
|
||||
lease_expires_at = sqlc.arg(lease_expires_at),
|
||||
updated_at = greatest(updated_at, clock_timestamp())
|
||||
WHERE
|
||||
submission_id = sqlc.arg(submission_id)
|
||||
AND state = 'running'
|
||||
AND lease_owner = sqlc.arg(lease_owner)
|
||||
AND started_at = sqlc.arg(started_at);
|
||||
|
||||
-- Puts a running try back into the queue: after a busy runner, a failed
|
||||
-- try, or at shutdown. counted adds the try to the attempts.
|
||||
-- name: RequeueAssessmentPracticeJob :execrows
|
||||
UPDATE assessment_practice_jobs
|
||||
SET
|
||||
state = 'queued',
|
||||
available_at = sqlc.arg(available_at),
|
||||
generation = sqlc.arg(generation),
|
||||
attempts = attempts + sqlc.arg(counted)::integer,
|
||||
last_error_class = coalesce(sqlc.narg(error_class), last_error_class),
|
||||
lease_owner = NULL,
|
||||
lease_expires_at = NULL,
|
||||
started_at = NULL,
|
||||
updated_at = greatest(updated_at, clock_timestamp())
|
||||
WHERE
|
||||
submission_id = sqlc.arg(submission_id)
|
||||
AND state = 'running'
|
||||
AND lease_owner = sqlc.arg(lease_owner)
|
||||
AND started_at = sqlc.arg(started_at);
|
||||
|
||||
-- Locks a running try for its completion and checks that it still belongs
|
||||
-- to the worker.
|
||||
-- name: LockClaimedAssessmentPracticeJob :one
|
||||
SELECT job.mode
|
||||
FROM assessment_practice_jobs AS job
|
||||
WHERE
|
||||
job.submission_id = sqlc.arg(submission_id)
|
||||
AND job.state = 'running'
|
||||
AND job.lease_owner = sqlc.arg(lease_owner)
|
||||
AND job.started_at = sqlc.arg(started_at)
|
||||
FOR UPDATE;
|
||||
|
||||
-- Stores the result of a submission once; a second result changes nothing.
|
||||
-- name: StoreAssessmentPracticeResult :execrows
|
||||
UPDATE assessment_practice_submissions
|
||||
SET
|
||||
result = sqlc.arg(result),
|
||||
result_at = greatest(created_at, clock_timestamp())
|
||||
WHERE
|
||||
id = sqlc.arg(id)
|
||||
AND result IS NULL;
|
||||
|
||||
-- Ends a running try as finished or failed.
|
||||
-- name: FinishAssessmentPracticeJob :execrows
|
||||
UPDATE assessment_practice_jobs
|
||||
SET
|
||||
state = sqlc.arg(state),
|
||||
finished_at = greatest(created_at, clock_timestamp()),
|
||||
last_error_class = coalesce(sqlc.narg(error_class), last_error_class),
|
||||
lease_owner = NULL,
|
||||
lease_expires_at = NULL,
|
||||
started_at = NULL,
|
||||
updated_at = greatest(updated_at, clock_timestamp())
|
||||
WHERE
|
||||
submission_id = sqlc.arg(submission_id)
|
||||
AND state = 'running'
|
||||
AND lease_owner = sqlc.arg(lease_owner)
|
||||
AND started_at = sqlc.arg(started_at);
|
||||
|
||||
-- Moves the practice of a final submission out of pending, but only while
|
||||
-- that submission is the latest final one of the practice.
|
||||
-- name: SettleAssessmentPracticeAfterJob :execrows
|
||||
UPDATE assessment_practices AS practice
|
||||
SET
|
||||
status = sqlc.arg(status),
|
||||
failure_reason = sqlc.narg(failure_reason),
|
||||
updated_at = greatest(practice.updated_at, clock_timestamp())
|
||||
WHERE
|
||||
practice.status = 'pending'
|
||||
AND practice.id = (
|
||||
SELECT submission.practice_id
|
||||
FROM assessment_practice_submissions AS submission
|
||||
WHERE submission.id = sqlc.arg(submission_id)
|
||||
)
|
||||
AND sqlc.arg(submission_id)::uuid = (
|
||||
SELECT latest.id
|
||||
FROM assessment_practice_submissions AS latest
|
||||
WHERE
|
||||
latest.practice_id = practice.id
|
||||
AND latest.mode = 'final'
|
||||
ORDER BY latest.created_at DESC, latest.id DESC
|
||||
LIMIT 1
|
||||
);
|
||||
Reference in new issue
Block a user