Skip to content

Commit

Permalink
submissions: optimize regrade mechanism (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar authored May 26, 2024
1 parent 8fc165a commit 7f9c664
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
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

0 comments on commit 7f9c664

Please sign in to comment.