修改bug

This commit is contained in:
Howie 2021-02-14 22:08:13 +08:00
parent d3530fb9eb
commit 791970a177
13 changed files with 115 additions and 58 deletions

View File

@ -110,39 +110,65 @@ public class JudgeController {
}
@PostMapping(value = "/remote-judge")
public CommonResult remoteJudge(@RequestBody ToJudge toJudge) {
public CommonResult remoteJudge() {
if (!toJudge.getToken().equals(judgeToken)) {
return CommonResult.errorResponse("对不起!您使用的判题服务调用凭证不正确!访问受限!", CommonResult.STATUS_ACCESS_DENIED);
}
// if (!toJudge.getToken().equals(judgeToken)) {
// return CommonResult.errorResponse("对不起!您使用的判题服务调用凭证不正确!访问受限!", CommonResult.STATUS_ACCESS_DENIED);
// }
Long submitId = toJudge.getJudge().getSubmitId();
String uid = toJudge.getJudge().getUid();
Long cid = toJudge.getJudge().getCid();
Long pid = toJudge.getJudge().getPid();
// Long submitId = toJudge.getJudge().getSubmitId();
// String uid = toJudge.getJudge().getUid();
// Long cid = toJudge.getJudge().getCid();
// Long pid = toJudge.getJudge().getPid();
Long submitId = 1L;
String uid = "1";
Long cid = 1L;
Long pid = 1L;
// 发送消息
try {
String[] source = toJudge.getRemoteJudge().split("-");
Long remotePid = Long.valueOf(source[1]);
String remoteJudge = source[0];
String userCode = toJudge.getJudge().getCode();
String language = toJudge.getJudge().getLanguage();
// String[] source = toJudge.getRemoteJudge().split("-");
// Long remotePid = Long.valueOf(source[1]);
// String remoteJudge = source[0];
// String userCode = toJudge.getJudge().getCode();
// String language = toJudge.getJudge().getLanguage();
Long remotePid = 1090L;
String remoteJudge = "HDU";
String userCode = "#include<iostream>\n" +
"using namespace std;\n" +
"\n" +
"int main()\n" +
"{\n" +
"\tint t;\n" +
"\tcin >> t;\n" +
"\twhile (t--) {\n" +
"\t\tint a, b;\n" +
"\t\tcin >> a >> b;\n" +
"\t\tcout << a + b;\n" +
"\t\tif (t == 0){\n" +
"\t\t\tcout << endl;\n" +
"\t\t} else {\n" +
"\t\t\tcout << endl;\n" +
"\t\t}\n" +
"\t}\n" +
"\treturn 1;\n" +
"}";
String language = "G++";
remoteJudgeSubmitDispatcher.sendTask(remoteJudge, remotePid, submitId, uid, cid, pid, language, userCode);
return CommonResult.successResponse(null, "提交成功");
} catch (Exception e) {
// TODO 如果为了测试是否可行请把数据库更新注释掉若正常运行则需要保证数据一致性
Judge judge = new Judge();
judge.setSubmitId(submitId)
.setStatus(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus())
.setErrorMessage("Oops, something has gone wrong with the judgeServer. Please report this to administrator.");
judgeService.updateById(judge);
// 更新其它表
judgeService.updateOtherTable(submitId,
Constants.Judge.STATUS_SYSTEM_ERROR.getStatus(),
cid,
uid,
pid,
null);
// Judge judge = new Judge();
// judge.setSubmitId(submitId)
// .setStatus(Constants.Judge.STATUS_SYSTEM_ERROR.getStatus())
// .setErrorMessage("Oops, something has gone wrong with the judgeServer. Please report this to administrator.");
// judgeService.updateById(judge);
// // 更新其它表
// judgeService.updateOtherTable(submitId,
// Constants.Judge.STATUS_SYSTEM_ERROR.getStatus(),
// cid,
// uid,
// pid,
// null);
log.error("调用redis消息发布异常,此次远程判题任务判为系统错误--------------->{}", e.getMessage());
return CommonResult.errorResponse(e.getMessage(), CommonResult.STATUS_ERROR);
}

View File

@ -55,8 +55,8 @@ public class RemoteJudgeSubscriberConfig {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
// 添加频道名字
redisMessageListenerContainer.addMessageListener(toSubmitMessageListenerAdapter, new PatternTopic(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName()));
redisMessageListenerContainer.addMessageListener(getResultMessageListenerAdapter, new PatternTopic(Constants.RemoteJudge.JUDGE_WAITING_RESULT_QUEUE.getName()));
redisMessageListenerContainer.addMessageListener(toSubmitMessageListenerAdapter, new PatternTopic(Constants.RemoteJudge.JUDGE_SUBMIT_HANDLER.getName()));
redisMessageListenerContainer.addMessageListener(getResultMessageListenerAdapter, new PatternTopic(Constants.RemoteJudge.JUDGE_RESULT_HANDLER.getName()));
return redisMessageListenerContainer;
}

View File

@ -2,6 +2,7 @@ package top.hcode.hoj.remoteJudge.result;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@ -18,6 +19,7 @@ import top.hcode.hoj.util.IpUtils;
import top.hcode.hoj.util.RedisUtils;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@ -26,6 +28,9 @@ public class RemoteJudgeResultReceiver implements MessageListener {
@Autowired
private RedisUtils redisUtils;
@Autowired
private RemoteJudgeResultDispatcher remoteJudgeResultDispatcher;
@Autowired
private JudgeServiceImpl judgeService;
@ -34,12 +39,13 @@ public class RemoteJudgeResultReceiver implements MessageListener {
@Override
public void onMessage(Message message, byte[] bytes) {
String source = (String) redisUtils.lrPop(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName());
String source = (String) redisUtils.lrPop(Constants.RemoteJudge.JUDGE_WAITING_RESULT_QUEUE.getName());
// 如果竞争不到查询队列结束
if (source == null) {
return;
}
JSONObject task = JSONUtil.parseObj(source);
log.info(source);
Long resultSubmitId = task.getLong("resultSubmitId");
Long submitId = task.getLong("submitId");
String uid = task.getStr("uid");
@ -55,38 +61,49 @@ public class RemoteJudgeResultReceiver implements MessageListener {
// TODO 获取对应的result修改到数据库
try {
log.info("resultSubmitId:" + resultSubmitId);
Map<String, Object> result = remoteJudgeStrategy.result(resultSubmitId);
Judge judge = new Judge();
judge.setSubmitId(submitId);
Integer status = (Integer) result.getOrDefault("status", Constants.Judge.STATUS_SYSTEM_ERROR.getStatus());
// TODO 如果结果没出来重新放入队列并更新状态为Waiting
if (status.equals(Constants.Judge.STATUS_PENDING.getStatus())) {
try {
TimeUnit.SECONDS.sleep(2);
remoteJudgeResultDispatcher.sendTask(remoteJudge, submitId, uid, cid, pid, resultSubmitId);
} catch (Exception e) {
log.error("重新查询结果任务出错------{}", e.getMessage());
}
return;
}
Integer time = (Integer) result.getOrDefault("time", null);
Integer memory = (Integer) result.getOrDefault("memory", null);
String CEInfo = (String) result.getOrDefault("CEInfo", null);
judge.setStatus(status)
.setTime(time)
.setMemory(memory);
// 获取当前判题系统所在ip写入数据库
if (ip.equals("-1")) {
judge.setJudger(IpUtils.getLocalIpv4Address());
} else {
judge.setJudger(ip);
}
if (status.intValue() == Constants.Judge.STATUS_COMPILE_ERROR.getStatus()) {
judge.setErrorMessage(CEInfo);
} else if (status.intValue() == Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()) {
judge.setErrorMessage("There is something wrong with the " + remoteJudge + ", please try again later");
}
log.info(status.toString() + " " + time + " " + memory + " " + CEInfo);
// judge.setStatus(status)
// .setTime(time)
// .setMemory(memory);
//
// // 获取当前判题系统所在ip写入数据库
// if (ip.equals("-1")) {
// judge.setJudger(IpUtils.getLocalIpv4Address());
// } else {
// judge.setJudger(ip);
// }
//
// if (status.intValue() == Constants.Judge.STATUS_COMPILE_ERROR.getStatus()) {
// judge.setErrorMessage(CEInfo);
// } else if (status.intValue() == Constants.Judge.STATUS_SYSTEM_ERROR.getStatus()) {
// judge.setErrorMessage("There is something wrong with the " + remoteJudge + ", please try again later");
// }
// 写回数据库
judgeService.updateById(judge);
// judgeService.updateById(judge);
/**
* @Description TODO 注意 如果是OI题目肯定score得分若不是则请传入null该方法是为了更新关联表保持数据一致
* @Since 2021/2/12
*/
judgeService.updateOtherTable(submitId, status, cid, uid, pid, null);
**/
// judgeService.updateOtherTable(submitId, status, cid, uid, pid, null);
} catch (Exception e) {
log.error("获取结果出错------------>{}", e.getMessage());

View File

@ -29,5 +29,6 @@ public class RemoteJudgeSubmitDispatcher {
task.set("language", language);
redisUtils.sendMessage(Constants.RemoteJudge.JUDGE_SUBMIT_HANDLER.getName(), "New Problem Added");
redisUtils.lrPush(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName(), JSONUtil.toJsonStr(task));
log.info("发送数据");
}
}

View File

@ -33,9 +33,12 @@ public class RemoteJudgeSubmitReceiver implements MessageListener {
@Override
public void onMessage(Message message, byte[] bytes) {
// log.debug("RemoteJudgeSubmitReceiver获取到消息{}", Arrays.toString(message.getBody()));
log.info("开始竞争");
String source = (String) redisUtils.lrPop(Constants.RemoteJudge.JUDGE_WAITING_SUBMIT_QUEUE.getName());
// 如果竞争不到提交队列结束
log.info(source);
if (source == null) {
log.info("竞争不到");
return;
}
JSONObject task = JSONUtil.parseObj(source);
@ -62,17 +65,19 @@ public class RemoteJudgeSubmitReceiver implements MessageListener {
// TODO 提交失败 前端手动按按钮再次提交 修改状态 STATUS_SUBMITTED_FAILED
if (resultSubmitId < 0) {
// 更新此次提交状态为提交失败
UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus())
.eq("submit_id", submitId);
judgeService.update(judgeUpdateWrapper);
log.info("查询id失败");
// UpdateWrapper<Judge> judgeUpdateWrapper = new UpdateWrapper<>();
// judgeUpdateWrapper.set("status", Constants.Judge.STATUS_SUBMITTED_FAILED.getStatus())
// .eq("submit_id", submitId);
// judgeService.update(judgeUpdateWrapper);
log.error("网络错误---------------->获取不到提交ID");
return;
}
// 提交成功顺便更新状态为-->STATUS_JUDGING 判题中...
judgeService.updateById(new Judge().setSubmitId(submitId).setStatus(Constants.Judge.STATUS_JUDGING.getStatus()));
// judgeService.updateById(new Judge().setSubmitId(submitId).setStatus(Constants.Judge.STATUS_JUDGING.getStatus()));
try {
remoteJudgeResultDispatcher.sendTask(remoteJudge, submitId, uid, cid, pid, resultSubmitId);
log.info("查询结果信息发送");
} catch (Exception e) {
log.error("调用redis消息发布异常,此次远程查询结果任务判为系统错误--------------->{}", e.getMessage());
}

View File

@ -3,6 +3,7 @@ package top.hcode.hoj.remoteJudge.task.Impl;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONUtil;
import jdk.nashorn.internal.runtime.regexp.RegExp;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
@ -73,14 +74,18 @@ public class HduJudge implements RemoteJudgeStrategy {
String rawStatus = matcher.group(2).replaceAll("<[\\s\\S]*?>", "").trim();
System.out.println(rawStatus);
Constants.Judge statusType = statusTypeMap.get(rawStatus);
if (statusType == Constants.Judge.STATUS_PENDING) {
return MapUtil.builder(new HashMap<String, Object>())
.put("status", statusType.getStatus()).build();
}
// 返回的结果map
Map<String, Object> result = MapUtil.builder(new HashMap<String, Object>())
.put("status", statusType).build();
.put("status", statusType.getStatus()).build();
// 获取其他信息
String executionTime = matcher.group(3);
result.put("time", executionTime);
result.put("time", Integer.parseInt(executionTime));
String executionMemory = matcher.group(4);
result.put("memory", executionMemory);
result.put("memory", Integer.parseInt(executionMemory));
// 如果CE了则还需要获得错误信息
if (statusType == Constants.Judge.STATUS_COMPILE_ERROR) {
connection.url(host + String.format(errorUrl, submitId));
@ -97,8 +102,8 @@ public class HduJudge implements RemoteJudgeStrategy {
Connection.Response response = JsoupUtils.postResponse(connection, MapUtil
.builder(new HashMap<String, String>())
// TODO 添加账号密码 暂时写死测试后续将在队列中获取空闲账号
.put("username", "11")
.put("userpass", "11").map());
.put("username", "2018030402055")
.put("userpass", "zsqfhy0804").map());
return response.cookies();
}
@ -141,7 +146,10 @@ public class HduJudge implements RemoteJudgeStrategy {
put("Accepted", Constants.Judge.STATUS_ACCEPTED);
put("Wrong Answer", Constants.Judge.STATUS_WRONG_ANSWER);
put("Compilation Error", Constants.Judge.STATUS_COMPILE_ERROR);
put("Queuing", Constants.Judge.STATUS_PENDING);
put("Compiling", Constants.Judge.STATUS_PENDING);
put("Time Limit Exceeded", Constants.Judge.STATUS_TIME_LIMIT_EXCEEDED);
put("Presentation Error", Constants.Judge.STATUS_PRESENTATION_ERROR);
}
};
}