Search in sources :

Example 21 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class UserRecordEntityServiceImpl method tryAgainUpdate.

@Transactional(isolation = Isolation.READ_COMMITTED)
public boolean tryAgainUpdate(String uid, Integer score) {
    boolean retryable;
    int attemptNumber = 0;
    do {
        // 查询最新版本号
        QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
        // 非比赛提交
        judgeQueryWrapper.isNotNull("score").orderByDesc("score").isNull("cid").last("limit 1");
        Judge lastHighScoreJudge = judgeEntityService.getOne(judgeQueryWrapper);
        // 更新
        boolean success = true;
        if (lastHighScoreJudge == null) {
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.set("total_score", score).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        } else if (lastHighScoreJudge.getScore() < score) {
            // 如果之前该题目最高得分的提交比现在得分低,也需要修改
            int addValue = score - lastHighScoreJudge.getScore();
            UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
            userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
            success = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
        }
        if (success) {
            return true;
        } else {
            attemptNumber++;
            retryable = attemptNumber < 8;
            if (attemptNumber == 8) {
                log.error("更新user_record表超过最大重试次数");
                break;
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                log.error(e.getMessage());
            }
        }
    } while (retryable);
    return false;
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class RemoteJudgeToSubmit method process.

public boolean process(RemoteJudgeStrategy remoteJudgeStrategy) {
    RemoteJudgeDTO remoteJudgeDTO = remoteJudgeStrategy.getRemoteJudgeDTO();
    log.info("Ready Send Task to RemoteJudgeDTO => {}", remoteJudgeDTO);
    String errLog = null;
    try {
        remoteJudgeStrategy.submit();
    } catch (Exception e) {
        log.error("Submit Failed! Error:", e);
        errLog = e.getMessage();
    }
    Long submitId = remoteJudgeDTO.getSubmitId();
    // 提交失败 前端手动按按钮再次提交 修改状态 STATUS_SUBMITTED_FAILED
    if (submitId == null || submitId == -1L) {
        // 将使用的账号放回对应列表
        log.error("[{}] Submit Failed! Begin to return the account to other task!", remoteJudgeDTO.getOj());
        remoteJudgeService.changeAccountStatus(remoteJudgeDTO.getOj(), remoteJudgeDTO.getUsername());
        if (RemoteJudgeContext.openCodeforcesFixServer) {
            if (remoteJudgeDTO.getOj().equals(Constants.RemoteJudge.GYM_JUDGE.getName()) || remoteJudgeDTO.getOj().equals(Constants.RemoteJudge.CF_JUDGE.getName())) {
                // 对CF特殊,归还判题机权限
                log.error("[{}] Submit Failed! Begin to return the Server Status to other task!", remoteJudgeDTO.getOj());
                remoteJudgeService.changeServerSubmitCFStatus(remoteJudgeDTO.getServerIp(), remoteJudgeDTO.getServerPort());
            }
        }
        // 更新此次提交状态为提交失败!
        UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
        judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus()).set("error_message", errLog).eq("submit_id", remoteJudgeDTO.getJudgeId());
        judgeEntityService.update(judgeUpdateWrapper);
        // 更新其它表
        judgeContext.updateOtherTable(remoteJudgeDTO.getSubmitId(), Constants.Judge.STATUS_SYSTEM_ERROR.getStatus(), remoteJudgeDTO.getCid(), remoteJudgeDTO.getUid(), remoteJudgeDTO.getPid(), remoteJudgeDTO.getGid(), null, null);
        return false;
    }
    // 提交成功顺便更新状态为-->STATUS_PENDING 等待判题中...
    judgeEntityService.updateById(new Judge().setSubmitId(remoteJudgeDTO.getJudgeId()).setStatus(Constants.Judge.STATUS_PENDING.getStatus()).setVjudgeSubmitId(submitId).setVjudgeUsername(remoteJudgeDTO.getUsername()).setVjudgePassword(remoteJudgeDTO.getPassword()).setJudger(name));
    log.info("[{}] Submit Successfully! The submit_id of remote judge is [{}]. Waiting the result of the task!", submitId, remoteJudgeDTO.getOj());
    return true;
}
Also used : RemoteJudgeDTO(top.hcode.hoj.remoteJudge.entity.RemoteJudgeDTO) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Example 23 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class UserRecordServiceImpl method updateRecord.

/**
 * @MethodNameupdateRecord
 * @Params  * @param null
 * @Description 本方法不启用,不适合数据一致性
 * @Return
 * @Since 2021/6/2
 */
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
@Async
@Override
@Deprecated
public void updateRecord(String uid, Long submitId, Long pid, Integer score) {
    QueryWrapper<Judge> judgeQueryWrapper = new QueryWrapper<>();
    judgeQueryWrapper.isNotNull("score").eq("cid", // 非比赛提交
    0).eq("pid", pid).eq("uid", uid).ne("submit_id", submitId).orderByDesc("score").last("limit 1");
    Judge lastHighScoreJudge = judgeService.getOne(judgeQueryWrapper);
    // 之前没有提交过,那么就需要修改
    boolean result = true;
    if (lastHighScoreJudge == null) {
        UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
        userRecordUpdateWrapper.setSql("total_score=total_score+" + score).eq("uid", uid);
        result = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
    } else if (lastHighScoreJudge.getScore() < score) {
        // 如果之前该题目最高得分的提交比现在得分低,也需要修改
        int addValue = score - lastHighScoreJudge.getScore();
        UpdateWrapper<UserRecord> userRecordUpdateWrapper = new UpdateWrapper<>();
        userRecordUpdateWrapper.setSql("total_score=total_score+" + addValue).eq("uid", uid);
        result = userRecordMapper.update(null, userRecordUpdateWrapper) == 1;
    }
    if (result) {
        return;
    } else {
        // 失败则开始尝试
        tryAgainUpdate(uid, score);
    }
}
Also used : UserRecord(top.hcode.hoj.pojo.entity.user.UserRecord) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Judge(top.hcode.hoj.pojo.entity.judge.Judge) Async(org.springframework.scheduling.annotation.Async) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class TrainingRecordServiceImpl method saveBatchNewRecordByJudgeList.

private void saveBatchNewRecordByJudgeList(List<Judge> judgeList, Long tid, Long tpId, HashMap<Long, Long> pidMapTPid) {
    if (!CollectionUtils.isEmpty(judgeList)) {
        List<TrainingRecord> trainingRecordList = new ArrayList<>();
        for (Judge judge : judgeList) {
            TrainingRecord trainingRecord = new TrainingRecord().setPid(judge.getPid()).setSubmitId(judge.getSubmitId()).setTid(tid).setUid(judge.getUid());
            if (pidMapTPid != null) {
                trainingRecord.setTpid(pidMapTPid.get(judge.getPid()));
            }
            if (tpId != null) {
                trainingRecord.setTpid(tpId);
            }
            trainingRecordList.add(trainingRecord);
        }
        saveBatch(trainingRecordList);
    }
}
Also used : TrainingRecord(top.hcode.hoj.pojo.entity.training.TrainingRecord) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Example 25 with Judge

use of top.hcode.hoj.pojo.entity.judge.Judge in project HOJ by HimitZH.

the class JudgeDispatcher method sendTask.

public void sendTask(Judge judge, String token, Boolean isContest) {
    JSONObject task = new JSONObject();
    task.set("judge", judge);
    task.set("token", token);
    task.set("isContest", isContest);
    try {
        boolean isOk;
        if (isContest) {
            isOk = redisUtils.llPush(Constants.Queue.CONTEST_JUDGE_WAITING.getName(), JSONUtil.toJsonStr(task));
        } else {
            isOk = redisUtils.llPush(Constants.Queue.GENERAL_JUDGE_WAITING.getName(), JSONUtil.toJsonStr(task));
        }
        if (!isOk) {
            judgeService.updateById(new Judge().setSubmitId(judge.getSubmitId()).setStatus(Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus()).setErrorMessage("Please try to submit again!"));
        }
        // 调用判题任务处理
        judgeReceiver.processWaitingTask();
    } catch (Exception e) {
        log.error("调用redis将判题纳入判题等待队列异常,此次判题任务判为系统错误--------------->{}", e.getMessage());
        judgeService.failToUseRedisPublishJudge(judge.getSubmitId(), judge.getPid(), isContest);
    }
}
Also used : JSONObject(cn.hutool.json.JSONObject) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Aggregations

Judge (top.hcode.hoj.pojo.entity.judge.Judge)50 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)29 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)21 Transactional (org.springframework.transaction.annotation.Transactional)18 Problem (top.hcode.hoj.pojo.entity.problem.Problem)18 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)14 Contest (top.hcode.hoj.pojo.entity.contest.Contest)13 Session (org.apache.shiro.session.Session)11 HttpSession (javax.servlet.http.HttpSession)10 ContestRecord (top.hcode.hoj.pojo.entity.contest.ContestRecord)10 JudgeCase (top.hcode.hoj.pojo.entity.judge.JudgeCase)10 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)8 UserAcproblem (top.hcode.hoj.pojo.entity.user.UserAcproblem)8 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)7 java.util (java.util)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 SecurityUtils (org.apache.shiro.SecurityUtils)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Constants (top.hcode.hoj.utils.Constants)6 JSONObject (cn.hutool.json.JSONObject)4