feat(practice): run practice jobs on the code runner with one stored result per run
This commit is contained in:
1 parent
9385b2ddd4
commit
9344228e84
17 files changed
+1010
-35
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
@@ -153,8 +154,16 @@ func (store *PracticeJobStore) Requeue(ctx context.Context, job outbound.Claimed
|
||||
return updated == 1, nil
|
||||
}
|
||||
|
||||
func (store *PracticeJobStore) Complete(ctx context.Context, job outbound.ClaimedPracticeJob, result domain.PracticeRunResult) (bool, error) {
|
||||
func (store *PracticeJobStore) Complete(
|
||||
ctx context.Context,
|
||||
job outbound.ClaimedPracticeJob,
|
||||
runnerRunID uuid.UUID,
|
||||
result domain.PracticeRunResult,
|
||||
) (bool, error) {
|
||||
const location = "PracticeJobStore.Complete"
|
||||
if runnerRunID == uuid.Nil {
|
||||
return false, errors.New(location + " - runner run ID must be set")
|
||||
}
|
||||
if err := result.Validate(); err != nil {
|
||||
return false, fmt.Errorf("%s - validate result: %w", location, err)
|
||||
}
|
||||
@@ -177,7 +186,7 @@ func (store *PracticeJobStore) Complete(ctx context.Context, job outbound.Claime
|
||||
if stored != 1 {
|
||||
return outbound.ErrPracticeResultExists
|
||||
}
|
||||
if err := finishPracticeJob(ctx, queries, job, practiceJobFinished, ""); err != nil {
|
||||
if err := finishPracticeJob(ctx, queries, job, practiceJobFinished, "", runnerRunID); err != nil {
|
||||
return err
|
||||
}
|
||||
if mode == domain.PracticeModeFinal {
|
||||
@@ -205,7 +214,7 @@ func (store *PracticeJobStore) Fail(ctx context.Context, job outbound.ClaimedPra
|
||||
if err != nil || !locked {
|
||||
return err
|
||||
}
|
||||
if err := finishPracticeJob(ctx, queries, job, practiceJobFailed, class); err != nil {
|
||||
if err := finishPracticeJob(ctx, queries, job, practiceJobFailed, class, uuid.Nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if mode == domain.PracticeModeFinal {
|
||||
@@ -247,9 +256,15 @@ func finishPracticeJob(
|
||||
job outbound.ClaimedPracticeJob,
|
||||
state string,
|
||||
class outbound.PracticeJobErrorClass,
|
||||
runnerRunID uuid.UUID,
|
||||
) error {
|
||||
// A failed job has no run ID: uuid.Nil stores NULL.
|
||||
var runnerRun pgtype.UUID
|
||||
if runnerRunID != uuid.Nil {
|
||||
runnerRun = pgUUID(runnerRunID)
|
||||
}
|
||||
finished, err := queries.FinishAssessmentPracticeJob(ctx, sqlc.FinishAssessmentPracticeJobParams{
|
||||
State: state, ErrorClass: pgOptionalText(string(class)), SubmissionID: pgUUID(job.SubmissionID),
|
||||
State: state, ErrorClass: pgOptionalText(string(class)), RunnerRunID: runnerRun, SubmissionID: pgUUID(job.SubmissionID),
|
||||
LeaseOwner: pgUUID(job.LeaseOwner), StartedAt: timestamp(job.StartedAt),
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -170,31 +170,35 @@ const finishAssessmentPracticeJob = `-- name: FinishAssessmentPracticeJob :execr
|
||||
UPDATE assessment_practice_jobs
|
||||
SET
|
||||
state = $1,
|
||||
runner_run_id = $2,
|
||||
finished_at = greatest(created_at, clock_timestamp()),
|
||||
last_error_class = coalesce($2, last_error_class),
|
||||
last_error_class = coalesce($3, last_error_class),
|
||||
lease_owner = NULL,
|
||||
lease_expires_at = NULL,
|
||||
started_at = NULL,
|
||||
updated_at = greatest(updated_at, clock_timestamp())
|
||||
WHERE
|
||||
submission_id = $3
|
||||
submission_id = $4
|
||||
AND state = 'running'
|
||||
AND lease_owner = $4
|
||||
AND started_at = $5
|
||||
AND lease_owner = $5
|
||||
AND started_at = $6
|
||||
`
|
||||
|
||||
type FinishAssessmentPracticeJobParams struct {
|
||||
State string
|
||||
RunnerRunID pgtype.UUID
|
||||
ErrorClass pgtype.Text
|
||||
SubmissionID pgtype.UUID
|
||||
LeaseOwner pgtype.UUID
|
||||
StartedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
// Ends a running try as finished or failed.
|
||||
// Ends a running try as finished or failed. A finished job keeps the run ID
|
||||
// of the runner run that produced its result.
|
||||
func (q *Queries) FinishAssessmentPracticeJob(ctx context.Context, arg FinishAssessmentPracticeJobParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, finishAssessmentPracticeJob,
|
||||
arg.State,
|
||||
arg.RunnerRunID,
|
||||
arg.ErrorClass,
|
||||
arg.SubmissionID,
|
||||
arg.LeaseOwner,
|
||||
|
||||
@@ -256,6 +256,7 @@ type AssessmentPracticeJob struct {
|
||||
LeaseExpiresAt pgtype.Timestamptz
|
||||
StartedAt pgtype.Timestamptz
|
||||
LastErrorClass pgtype.Text
|
||||
RunnerRunID pgtype.UUID
|
||||
}
|
||||
|
||||
type AssessmentPracticeSubmission struct {
|
||||
|
||||
@@ -307,7 +307,8 @@ type Querier interface {
|
||||
// made at or after reserved_since. A retry of the delivery inside the
|
||||
// provider's idempotency window reuses it instead of counting again.
|
||||
FindOpenMailQuotaReservation(ctx context.Context, arg FindOpenMailQuotaReservationParams) (FindOpenMailQuotaReservationRow, error)
|
||||
// Ends a running try as finished or failed.
|
||||
// Ends a running try as finished or failed. A finished job keeps the run ID
|
||||
// of the runner run that produced its result.
|
||||
FinishAssessmentPracticeJob(ctx context.Context, arg FinishAssessmentPracticeJobParams) (int64, error)
|
||||
// Reads the durable active deletion request when an earlier request already
|
||||
// exists. A completed request does not suppress a later explicit request.
|
||||
|
||||
@@ -97,11 +97,12 @@ type PracticeJobStore interface {
|
||||
// Requeue puts a try back into the queue; held is false when the job no
|
||||
// longer belongs to the try.
|
||||
Requeue(ctx context.Context, job ClaimedPracticeJob, requeue PracticeRequeue) (held bool, err error)
|
||||
// Complete stores the result of the submission once, finishes the job,
|
||||
// and, for the latest final submission of a pending practice, finishes
|
||||
// the practice, all in one transaction. held is false, with nothing
|
||||
// written, when the job no longer belongs to the try.
|
||||
Complete(ctx context.Context, job ClaimedPracticeJob, result domain.PracticeRunResult) (held bool, err error)
|
||||
// Complete stores the result of the submission once, finishes the job
|
||||
// with the opaque ID of the runner run that produced the result, and, for
|
||||
// the latest final submission of a pending practice, finishes the
|
||||
// practice, all in one transaction. held is false, with nothing written,
|
||||
// when the job no longer belongs to the try.
|
||||
Complete(ctx context.Context, job ClaimedPracticeJob, runnerRunID uuid.UUID, result domain.PracticeRunResult) (held bool, err error)
|
||||
// Fail ends the job as failed with the error class and, for the latest
|
||||
// final submission of a pending practice, marks the practice failed with
|
||||
// sandbox_error. It never stores a verdict.
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
@@ -172,19 +174,23 @@ func (executor *Executor) runnerFailure(ctx context.Context, job outbound.Claime
|
||||
}
|
||||
|
||||
func (executor *Executor) complete(ctx context.Context, job outbound.ClaimedPracticeJob, status outbound.CodeRunStatus) (ExecutionOutcome, error) {
|
||||
if status.Result == nil {
|
||||
// A finished run names the runner run that the stored result belongs to;
|
||||
// a status without a result or a valid run ID is outside the contract.
|
||||
runnerRunID, idErr := uuid.Parse(status.RunID)
|
||||
if status.Result == nil || idErr != nil || runnerRunID == uuid.Nil {
|
||||
return executor.runnerFailure(ctx, job, outbound.ErrCodeRunnerBadResponse)
|
||||
}
|
||||
storeCtx, cancel := executor.storeContext(ctx)
|
||||
defer cancel()
|
||||
held, err := executor.store.Complete(storeCtx, job, runResult(*status.Result))
|
||||
held, err := executor.store.Complete(storeCtx, job, runnerRunID, runResult(*status.Result))
|
||||
switch {
|
||||
case err != nil:
|
||||
return "", fmt.Errorf("Executor.complete - store result: %w", err)
|
||||
case !held:
|
||||
return OutcomeLeaseLost, nil
|
||||
}
|
||||
executor.logger.Info("practice job finished", jobAttributes(job, slog.String("outcome", string(status.Result.Outcome)))...)
|
||||
executor.logger.Info("practice job finished", jobAttributes(job,
|
||||
slog.String("outcome", string(status.Result.Outcome)), slog.String("runner_run_id", runnerRunID.String()))...)
|
||||
return OutcomeFinished, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -95,6 +95,33 @@ func TestExecutorHandlesPollFailuresAndShutdown(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutorStoresTheRunnerRunIDWithTheResult(t *testing.T) {
|
||||
now := time.Date(2026, 10, 11, 12, 0, 0, 0, time.UTC)
|
||||
job := outbound.ClaimedPracticeJob{SubmissionID: uuid.New(), StartedAt: now}
|
||||
|
||||
store := &fakeJobStore{}
|
||||
runner := &fakeRunner{submits: []outbound.CodeRunState{outbound.CodeRunFinished}}
|
||||
executor := testExecutor(t, runner, store, now)
|
||||
if outcome, err := executor.Execute(t.Context(), job); err != nil || outcome != OutcomeFinished {
|
||||
t.Fatalf("Execute() = %q, error %v; want finished", outcome, err)
|
||||
}
|
||||
if store.runnerRunID != uuid.MustParse(fakeRunID) {
|
||||
t.Fatalf("stored runner run ID = %s, want %s", store.runnerRunID, fakeRunID)
|
||||
}
|
||||
|
||||
// A finished run without a valid run ID is outside the runner contract,
|
||||
// so no result is stored and the try is retried on the same key.
|
||||
for _, runID := range []string{"", "not-a-uuid", uuid.Nil.String()} {
|
||||
store = &fakeJobStore{}
|
||||
runner = &fakeRunner{submits: []outbound.CodeRunState{outbound.CodeRunFinished}, runID: &runID}
|
||||
executor = testExecutor(t, runner, store, now)
|
||||
outcome, err := executor.Execute(t.Context(), job)
|
||||
if err != nil || outcome != OutcomeRetry || store.completed || store.requeue.Generation != job.Generation {
|
||||
t.Fatalf("Execute() with run ID %q = %q, error %v, completed %v; want a retry without a result", runID, outcome, err, store.completed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testExecutor(t *testing.T, runner outbound.CodeRunner, store outbound.PracticeJobStore, now time.Time) *Executor {
|
||||
t.Helper()
|
||||
executor, err := NewExecutor(runner, store, func() time.Time { return now }, ExecutionSettings{
|
||||
@@ -108,6 +135,8 @@ func testExecutor(t *testing.T, runner outbound.CodeRunner, store outbound.Pract
|
||||
}
|
||||
|
||||
type fakeRunner struct {
|
||||
// runID overrides the run ID of a finished status when set.
|
||||
runID *string
|
||||
submitErr error
|
||||
pollErr error
|
||||
submits []outbound.CodeRunState
|
||||
@@ -121,7 +150,7 @@ func (runner *fakeRunner) Submit(_ context.Context, request outbound.CodeRunRequ
|
||||
return outbound.CodeRunStatus{}, runner.submitErr
|
||||
}
|
||||
state := runner.submits[min(runner.submitCalls, len(runner.submits))-1]
|
||||
return fakeStatus(state, request), nil
|
||||
return runner.status(state, request), nil
|
||||
}
|
||||
|
||||
func (runner *fakeRunner) Poll(_ context.Context, request outbound.CodeRunRequest) (outbound.CodeRunStatus, error) {
|
||||
@@ -129,9 +158,20 @@ func (runner *fakeRunner) Poll(_ context.Context, request outbound.CodeRunReques
|
||||
if runner.pollErr != nil {
|
||||
return outbound.CodeRunStatus{}, runner.pollErr
|
||||
}
|
||||
return fakeStatus(outbound.CodeRunFinished, request), nil
|
||||
return runner.status(outbound.CodeRunFinished, request), nil
|
||||
}
|
||||
|
||||
func (runner *fakeRunner) status(state outbound.CodeRunState, request outbound.CodeRunRequest) outbound.CodeRunStatus {
|
||||
status := fakeStatus(state, request)
|
||||
if runner.runID != nil {
|
||||
status.RunID = *runner.runID
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
// fakeRunID is the opaque run ID that the fake runner reports.
|
||||
const fakeRunID = "0a1b2c3d-4e5f-8a6b-8c7d-9e0f1a2b3c4d"
|
||||
|
||||
func fakeStatus(state outbound.CodeRunState, request outbound.CodeRunRequest) outbound.CodeRunStatus {
|
||||
if state != outbound.CodeRunFinished {
|
||||
return outbound.CodeRunStatus{State: state}
|
||||
@@ -143,13 +183,15 @@ func fakeStatus(state outbound.CodeRunState, request outbound.CodeRunRequest) ou
|
||||
PassedTests: len(group.Tests), TotalTests: len(group.Tests),
|
||||
})
|
||||
}
|
||||
return outbound.CodeRunStatus{State: state, Result: &result}
|
||||
return outbound.CodeRunStatus{State: state, RunID: fakeRunID, Result: &result}
|
||||
}
|
||||
|
||||
type fakeJobStore struct {
|
||||
requeue outbound.PracticeRequeue
|
||||
failedWith outbound.PracticeJobErrorClass
|
||||
completed bool
|
||||
// runnerRunID is the run ID that Complete stored.
|
||||
runnerRunID uuid.UUID
|
||||
}
|
||||
|
||||
func (store *fakeJobStore) Claim(context.Context, outbound.PracticeJobClaim) (outbound.ClaimedPracticeJob, bool, error) {
|
||||
@@ -173,8 +215,9 @@ func (store *fakeJobStore) Requeue(_ context.Context, _ outbound.ClaimedPractice
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (store *fakeJobStore) Complete(context.Context, outbound.ClaimedPracticeJob, domain.PracticeRunResult) (bool, error) {
|
||||
func (store *fakeJobStore) Complete(_ context.Context, _ outbound.ClaimedPracticeJob, runnerRunID uuid.UUID, _ domain.PracticeRunResult) (bool, error) {
|
||||
store.completed = true
|
||||
store.runnerRunID = runnerRunID
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user