Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

submissions: optimize regrade mechanism #629

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import static judgels.service.ServiceUtils.checkFound;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.dropwizard.hibernate.UnitOfWork;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
Expand Down Expand Up @@ -287,7 +289,7 @@ public void regradeSubmission(

@POST
@Path("/regrade")
@UnitOfWork
@UnitOfWork(transactional = false)
public void regradeSubmissions(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
@QueryParam("containerJid") Optional<String> containerJid,
Expand All @@ -298,20 +300,24 @@ public void regradeSubmissions(
String actorJid = actorChecker.check(authHeader);
checkAllowed(submissionRoleChecker.canManage(actorJid));

Map<String, ProblemSubmissionConfig> configsMap = new HashMap<>();

for (int pageNumber = 1;; pageNumber++) {
List<Submission> submissions = submissionStore.getSubmissions(
containerJid,
byUserJid(username),
byProblemJid(containerJid, problemJid, problemAlias),
pageNumber,
PAGE_SIZE).getPage();
100).getPage();

if (submissions.isEmpty()) {
break;
}

var problemJids = Lists.transform(submissions, Submission::getProblemJid);
Map<String, ProblemSubmissionConfig> configsMap = sandalphonClient.getProgrammingProblemSubmissionConfigs(problemJids);
configsMap.putAll(sandalphonClient.getProgrammingProblemSubmissionConfigs(
Sets.difference(Set.copyOf(problemJids), configsMap.keySet())));

submissionRegrader.regradeSubmissions(submissions, configsMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.hibernate.UnitOfWork;
import java.io.IOException;
import java.time.Duration;
import java.util.Optional;
import judgels.gabriel.api.GradingResponse;
import judgels.messaging.MessageClient;
Expand All @@ -15,9 +14,6 @@
public class GradingResponseProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(GradingResponseProcessor.class);

private static final int MAX_RETRIES = 3;
private static final Duration DELAY_BETWEEN_RETRIES = Duration.ofSeconds(5);

private final ObjectMapper mapper;
private final SubmissionStore submissionStore;
private final MessageClient messageClient;
Expand Down Expand Up @@ -45,28 +41,13 @@ public void process(Message message) {
return;
}

boolean gradingExists = false;

// it is possible that the grading model is not immediately found, because it is not flushed yet.
for (int i = 0; i < MAX_RETRIES; i++) {
Optional<Submission> submission =
submissionStore.updateGrading(response.getGradingJid(), response.getResult());
if (submission.isPresent()) {
gradingExists = true;
submissionConsumer.accept(submission.get());
messageClient.confirmMessage(message.getId());
break;
}

try {
Thread.sleep(DELAY_BETWEEN_RETRIES.toMillis());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

if (!gradingExists) {
Optional<Submission> submission = submissionStore.updateGrading(response.getGradingJid(), response.getResult());
if (submission.isEmpty()) {
LOGGER.error("Failed to find grading jid {}", response.getGradingJid());
return;
}

submissionConsumer.accept(submission.get());
messageClient.confirmMessage(message.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.dropwizard.hibernate.UnitOfWork;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -318,7 +320,7 @@ public void regradeSubmission(

@POST
@Path("/regrade")
@UnitOfWork
@UnitOfWork(transactional = false)
public void regradeSubmissions(
@HeaderParam(AUTHORIZATION) AuthHeader authHeader,
@QueryParam("contestJid") String contestJid,
Expand All @@ -331,20 +333,24 @@ public void regradeSubmissions(

Optional<String> problemJid = byProblemJid(contestJid, problemAlias);

Map<String, ProblemSubmissionConfig> configsMap = new HashMap<>();

for (int pageNumber = 1;; pageNumber++) {
List<Submission> submissions = submissionStore.getSubmissions(
Optional.of(contestJid),
byUserJid(username),
problemJid,
pageNumber,
PAGE_SIZE).getPage();
100).getPage();

if (submissions.isEmpty()) {
break;
}

var problemJids = Lists.transform(submissions, Submission::getProblemJid);
Map<String, ProblemSubmissionConfig> configsMap = sandalphonClient.getProgrammingProblemSubmissionConfigs(problemJids);
configsMap.putAll(sandalphonClient.getProgrammingProblemSubmissionConfigs(
Sets.difference(Set.copyOf(problemJids), configsMap.keySet())));

submissionRegrader.regradeSubmissions(submissions, configsMap);
}
scoreboardIncrementalMarker.invalidateMark(contest.getJid());
Expand Down