-
Notifications
You must be signed in to change notification settings - Fork 11
/
judge.go
369 lines (312 loc) · 10.9 KB
/
judge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package logic
import (
"context"
"errors"
"os"
"time"
"github.com/docker/docker/client"
"github.com/gammazero/workerpool"
"gitlab.com/pjrpc/pjrpc/v2"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/tranHieuDev23/cato/internal/configs"
"github.com/tranHieuDev23/cato/internal/dataaccess/db"
"github.com/tranHieuDev23/cato/internal/handlers/http/rpc"
"github.com/tranHieuDev23/cato/internal/handlers/http/rpc/rpcclient"
"github.com/tranHieuDev23/cato/internal/utils"
)
type Judge interface {
JudgeSubmission(ctx context.Context, problem *db.Problem, language string, content string) (db.SubmissionResult, error)
ScheduleJudgeLocalSubmission(id uint64)
JudgeDistributedFirstSubmittedSubmission(ctx context.Context) error
}
type judge struct {
problemDataAccessor db.ProblemDataAccessor
submissionDataAccessor db.SubmissionDataAccessor
testCaseDataAccessor db.TestCaseDataAccessor
problemTestCaseHashDataAccessor db.ProblemTestCaseHashDataAccessor
db *gorm.DB
apiClient rpcclient.APIClient
logger *zap.Logger
logicConfig configs.Logic
appArguments utils.Arguments
workerPool *workerpool.WorkerPool
languageToCompileLogic map[string]Compile
languageToTestCaseRunLogic map[string]TestCaseRun
submissionRetryDelayDuration time.Duration
}
func NewJudge(
problemDataAccessor db.ProblemDataAccessor,
submissionDataAccessor db.SubmissionDataAccessor,
testCaseDataAccessor db.TestCaseDataAccessor,
problemTestCaseHashDataAccessor db.ProblemTestCaseHashDataAccessor,
dockerClient *client.Client,
db *gorm.DB,
apiClient rpcclient.APIClient,
logger *zap.Logger,
logicConfig configs.Logic,
appArguments utils.Arguments,
) (Judge, error) {
submissionRetryDelayDuration, err := logicConfig.Judge.GetSubmissionRetryDelayDuration()
if err != nil {
logger.With(zap.Error(err)).Error("failed to get submission retry delay duration")
return nil, err
}
j := &judge{
problemDataAccessor: problemDataAccessor,
submissionDataAccessor: submissionDataAccessor,
testCaseDataAccessor: testCaseDataAccessor,
problemTestCaseHashDataAccessor: problemTestCaseHashDataAccessor,
db: db,
apiClient: apiClient,
logger: logger,
logicConfig: logicConfig,
appArguments: appArguments,
workerPool: workerpool.New(1),
languageToCompileLogic: make(map[string]Compile),
languageToTestCaseRunLogic: make(map[string]TestCaseRun),
submissionRetryDelayDuration: submissionRetryDelayDuration,
}
if appArguments.Distributed && !appArguments.Worker {
// Skip pulling of Docker image on Distributed host because we are not judging there
return j, nil
}
for _, languageConfig := range logicConfig.Judge.Languages {
compile, compileErr := NewCompile(
dockerClient, logger, languageConfig.Value, languageConfig.Compile, appArguments)
if compileErr != nil {
return nil, compileErr
}
j.languageToCompileLogic[languageConfig.Value] = compile
testCaseRun, testCaseRunErr := NewTestCaseRun(
dockerClient, logger, languageConfig.Value, languageConfig.TestCaseRun, appArguments)
if testCaseRunErr != nil {
return nil, testCaseRunErr
}
j.languageToTestCaseRunLogic[languageConfig.Value] = testCaseRun
}
return j, nil
}
func (j judge) validateProblemHash(ctx context.Context, problem *db.Problem) error {
logger := utils.LoggerWithContext(ctx, j.logger).With(zap.String("problem_uuid", problem.UUID))
getProblemResponse, err := j.apiClient.GetProblem(ctx, &rpc.GetProblemRequest{
UUID: problem.UUID,
})
if err != nil {
return err
}
problemTestCaseHash, err := j.problemTestCaseHashDataAccessor.
GetProblemTestCaseHashOfProblem(ctx, uint64(problem.ID))
if err != nil {
return err
}
if getProblemResponse.Problem.TestCaseHash != problemTestCaseHash.Hash {
logger.Error("test case hash of problem is not equal to the latest from host, will not judge")
return errors.New("test case hash of problem is not equal to the latest from host, will not judge")
}
return nil
}
func (j judge) updateSubmissionStatusAndResult(
ctx context.Context,
submission *db.Submission,
status db.SubmissionStatus,
result db.SubmissionResult,
) error {
submission.Status = status
submission.Result = result
return j.submissionDataAccessor.UpdateSubmission(ctx, submission)
}
func (j judge) judgeDBSubmissionProblemAndTestCase(
ctx context.Context,
problem *db.Problem,
testCase *db.TestCase,
compileOutput CompileOutput,
runLogic TestCaseRun,
) (db.SubmissionResult, error) {
logger := utils.LoggerWithContext(ctx, j.logger).
With(zap.Uint("problem_id", problem.ID)).
With(zap.Uint("test_case_id", testCase.ID))
logger.Info("running submission against test case")
runOutput, err := runLogic.Run(
ctx,
compileOutput.ProgramFilePath,
testCase.Input,
problem.TimeLimitInMillisecond,
problem.MemoryLimitInByte,
)
if err != nil {
return 0, err
}
if runOutput.TimeLimitExceeded {
logger.Info("submission exceeded time limit")
return db.SubmissionResultTimeLimitExceeded, nil
}
if runOutput.MemoryLimitExceeded {
logger.Info("submission exceeded memory limit")
return db.SubmissionResultMemoryLimitExceeded, nil
}
if runOutput.ReturnCode != 0 {
logger.With(zap.Int64("return_code", runOutput.ReturnCode)).Info("submission has runtime error")
return db.SubmissionResultRuntimeError, nil
}
if runOutput.StdOut != testCase.Output {
logger.Info("submission gave incorrect output")
return db.SubmissionResultWrongAnswer, nil
}
return db.SubmissionResultOK, nil
}
func (j judge) JudgeSubmission(
ctx context.Context,
problem *db.Problem,
language string,
content string,
) (db.SubmissionResult, error) {
logger := utils.LoggerWithContext(ctx, j.logger).With(zap.String("problemUUID", problem.UUID))
if j.appArguments.Distributed {
logger.Info("validating problem hash")
if err := j.validateProblemHash(ctx, problem); err != nil {
return 0, err
}
}
logger.Info("compiling submission")
compileLogic, ok := j.languageToCompileLogic[language]
if !ok {
logger.With(zap.String("language", language)).Info("submission has unsupported language")
return db.SubmissionResultUnsupportedLanguage, nil
}
compileOutput, err := compileLogic.Compile(ctx, content)
if err != nil {
logger.With(zap.Error(err)).Error("failed to compile submission")
return 0, err
}
if compileOutput.ProgramFilePath == "" {
logger.With(zap.Any("compile_output", compileOutput)).Info("submission has compile error")
return db.SubmissionResultCompileError, nil
}
defer func() {
err = os.Remove(compileOutput.ProgramFilePath)
if err != nil {
logger.
With(zap.String("program_file_path", compileOutput.ProgramFilePath)).
With(zap.Error(err)).
Error("failed to remove program file")
}
}()
logger.Info("running submission against test cases")
testCaseID, err := j.testCaseDataAccessor.GetTestCaseIDListOfProblem(ctx, uint64(problem.ID))
if err != nil {
return 0, err
}
runLogic := j.languageToTestCaseRunLogic[language]
for _, testCaseID := range testCaseID {
testCase, testCaseErr := j.testCaseDataAccessor.GetTestCase(ctx, testCaseID)
if testCaseErr != nil {
return 0, testCaseErr
}
result, testCaseErr := j.
judgeDBSubmissionProblemAndTestCase(ctx, problem, testCase, compileOutput, runLogic)
if testCaseErr != nil {
return 0, testCaseErr
}
if result != db.SubmissionResultOK {
return result, nil
}
}
logger.Info("submission passed")
return db.SubmissionResultOK, nil
}
func (j judge) judgeLocalSubmission(ctx context.Context, submissionID uint64) {
var (
logger = utils.LoggerWithContext(ctx, j.logger).With(zap.Uint64("submission_id", submissionID))
submission *db.Submission
err error
)
logger.Info("retrieving submission information")
if txErr := j.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
submission, err = j.submissionDataAccessor.WithDB(tx).GetSubmission(ctx, submissionID)
if err != nil {
return err
}
if submission.Status != db.SubmissionStatusSubmitted {
logger.Error("status of submission is not submitted")
return errors.New("status of submission is not submitted")
}
submission.Status = db.SubmissionStatusExecuting
err = j.submissionDataAccessor.WithDB(tx).UpdateSubmission(ctx, submission)
if err != nil {
return err
}
return nil
}); txErr != nil {
logger.With(zap.Error(txErr)).Error("failed to retrieve local submission information")
return
}
revertFunc := func(revertErr error) {
logger.
With(zap.Error(revertErr)).
Error("encountered error while judging submission, reverting status to submitted")
revertErr = j.updateSubmissionStatusAndResult(ctx, submission, db.SubmissionStatusSubmitted, 0)
if revertErr != nil {
logger.With(zap.Error(revertErr)).Error("failed to revert submission status to submitted")
}
time.AfterFunc(j.submissionRetryDelayDuration, func() {
j.ScheduleJudgeLocalSubmission(submissionID)
})
}
problem, err := j.problemDataAccessor.GetProblem(ctx, submission.OfProblemID)
if err != nil {
return
}
result, err := j.JudgeSubmission(ctx, problem, submission.Language, submission.Content)
if err != nil {
revertFunc(err)
return
}
err = j.updateSubmissionStatusAndResult(ctx, submission, db.SubmissionStatusFinished, result)
if err != nil {
revertFunc(err)
}
}
func (j judge) ScheduleJudgeLocalSubmission(submissionID uint64) {
j.workerPool.Submit(func() { j.judgeLocalSubmission(context.Background(), submissionID) })
}
func (j judge) JudgeDistributedFirstSubmittedSubmission(ctx context.Context) error {
logger := utils.LoggerWithContext(ctx, j.logger)
response, err := j.apiClient.GetAndUpdateFirstSubmittedSubmissionToExecuting(
ctx, &rpc.GetAndUpdateFirstSubmittedSubmissionToExecutingRequest{})
if err != nil {
errorResponse := new(pjrpc.ErrorResponse)
if errors.As(errors.Unwrap(err), &errorResponse) {
if errorResponse.Code == int(rpc.ErrorCodeNotFound) {
logger.Info("no submitted problem found, no need to judge")
return nil
}
}
return err
}
problem, err := j.problemDataAccessor.GetProblemByUUID(ctx, response.Submission.Problem.UUID)
if err != nil {
return err
}
if problem == nil {
logger.
With(zap.Uint64("submission_id", response.Submission.ID)).
With(zap.String("problem_uuid", response.Submission.Problem.UUID)).
Error("problem is not yet synced on worker, will not judge")
return err
}
submission := response.Submission
result, err := j.JudgeSubmission(ctx, problem, submission.Language, submission.Content)
if err != nil {
return err
}
_, err = j.apiClient.UpdateSubmission(ctx, &rpc.UpdateSubmissionRequest{
ID: submission.ID,
Status: uint8(rpc.SubmissionStatusFinished),
Result: uint8(result),
})
if err != nil {
return err
}
return nil
}