NDOJ/judge/judgeapi.py

159 lines
5.4 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
import json
import logging
import socket
import struct
import zlib
from django.conf import settings
from judge import event_poster as event
2022-05-14 17:57:27 +00:00
logger = logging.getLogger("judge.judgeapi")
size_pack = struct.Struct("!I")
2020-01-21 06:35:58 +00:00
def _post_update_submission(submission, done=False):
if submission.problem.is_public:
2022-05-14 17:57:27 +00:00
event.post(
"submissions",
{
"type": "done-submission" if done else "update-submission",
"id": submission.id,
"contest": submission.contest_key,
"user": submission.user_id,
"problem": submission.problem_id,
"status": submission.status,
"language": submission.language.key,
},
)
2020-01-21 06:35:58 +00:00
def judge_request(packet, reply=True):
2022-05-14 17:57:27 +00:00
sock = socket.create_connection(
settings.BRIDGED_DJANGO_CONNECT or settings.BRIDGED_DJANGO_ADDRESS[0]
)
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
output = json.dumps(packet, separators=(",", ":"))
output = zlib.compress(output.encode("utf-8"))
writer = sock.makefile("wb")
2020-01-21 06:35:58 +00:00
writer.write(size_pack.pack(len(output)))
writer.write(output)
writer.close()
if reply:
2022-05-14 17:57:27 +00:00
reader = sock.makefile("rb", -1)
2020-01-21 06:35:58 +00:00
input = reader.read(size_pack.size)
if not input:
2022-05-14 17:57:27 +00:00
raise ValueError("Judge did not respond")
2020-01-21 06:35:58 +00:00
length = size_pack.unpack(input)[0]
input = reader.read(length)
if not input:
2022-05-14 17:57:27 +00:00
raise ValueError("Judge did not respond")
2020-01-21 06:35:58 +00:00
reader.close()
sock.close()
2022-05-14 17:57:27 +00:00
result = json.loads(zlib.decompress(input).decode("utf-8"))
2020-01-21 06:35:58 +00:00
return result
def judge_submission(submission, rejudge=False, batch_rejudge=False, judge_id=None):
2020-01-21 06:35:58 +00:00
from .models import ContestSubmission, Submission, SubmissionTestCase
CONTEST_SUBMISSION_PRIORITY = 0
DEFAULT_PRIORITY = 1
REJUDGE_PRIORITY = 2
BATCH_REJUDGE_PRIORITY = 3
2022-05-14 17:57:27 +00:00
updates = {
"time": None,
"memory": None,
"points": None,
"result": None,
"error": None,
"was_rejudged": rejudge,
"status": "QU",
}
2020-01-21 06:35:58 +00:00
try:
# This is set proactively; it might get unset in judgecallback's on_grading_begin if the problem doesn't
# actually have pretests stored on the judge.
2022-05-14 17:57:27 +00:00
updates["is_pretested"] = all(
ContestSubmission.objects.filter(submission=submission).values_list(
"problem__contest__run_pretests_only", "problem__is_pretested"
)[0]
)
2020-01-21 06:35:58 +00:00
except IndexError:
priority = DEFAULT_PRIORITY
else:
priority = CONTEST_SUBMISSION_PRIORITY
# This should prevent double rejudge issues by permitting only the judging of
# QU (which is the initial state) and D (which is the final state).
# Even though the bridge will not queue a submission already being judged,
# we will destroy the current state by deleting all SubmissionTestCase objects.
# However, we can't drop the old state immediately before a submission is set for judging,
# as that would prevent people from knowing a submission is being scheduled for rejudging.
# It is worth noting that this mechanism does not prevent a new rejudge from being scheduled
# while already queued, but that does not lead to data corruption.
2022-05-14 17:57:27 +00:00
if (
not Submission.objects.filter(id=submission.id)
.exclude(status__in=("P", "G"))
.update(**updates)
):
2020-01-21 06:35:58 +00:00
return False
SubmissionTestCase.objects.filter(submission_id=submission.id).delete()
2020-01-21 06:35:58 +00:00
try:
2022-05-14 17:57:27 +00:00
response = judge_request(
{
"name": "submission-request",
"submission-id": submission.id,
"problem-id": submission.problem.code,
"language": submission.language.key,
"source": submission.source.source,
"judge-id": judge_id,
"priority": BATCH_REJUDGE_PRIORITY
if batch_rejudge
else REJUDGE_PRIORITY
if rejudge
else priority,
}
)
2020-01-21 06:35:58 +00:00
except BaseException:
2022-05-14 17:57:27 +00:00
logger.exception("Failed to send request to judge")
Submission.objects.filter(id=submission.id).update(status="IE", result="IE")
2020-01-21 06:35:58 +00:00
success = False
else:
2022-05-14 17:57:27 +00:00
if (
response["name"] != "submission-received"
or response["submission-id"] != submission.id
):
Submission.objects.filter(id=submission.id).update(status="IE", result="IE")
2020-01-21 06:35:58 +00:00
_post_update_submission(submission)
success = True
return success
def disconnect_judge(judge, force=False):
2022-05-14 17:57:27 +00:00
judge_request(
{"name": "disconnect-judge", "judge-id": judge.name, "force": force},
reply=False,
)
2020-01-21 06:35:58 +00:00
def abort_submission(submission):
from .models import Submission
2022-05-14 17:57:27 +00:00
response = judge_request(
{"name": "terminate-submission", "submission-id": submission.id}
)
2020-07-19 21:27:14 +00:00
# This defaults to true, so that in the case the JudgeList fails to remove the submission from the queue,
2020-01-21 06:35:58 +00:00
# and returns a bad-request, the submission is not falsely shown as "Aborted" when it will still be judged.
2022-05-14 17:57:27 +00:00
if not response.get("judge-aborted", True):
Submission.objects.filter(id=submission.id).update(status="AB", result="AB")
event.post(
"sub_%s" % Submission.get_id_secret(submission.id),
{"type": "aborted-submission"},
)
_post_update_submission(submission, done=True)