This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
/
judge.go
309 lines (294 loc) · 9.63 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
package app
import (
"context"
"encoding/json"
"regexp"
"strconv"
"sync"
"time"
"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/gorilla/websocket"
"github.com/syzoj/syzoj-ng-go/lib/rediskey"
"github.com/syzoj/syzoj-ng-go/models"
"github.com/syzoj/syzoj-ng-go/svc/judge"
svcredis "github.com/syzoj/syzoj-ng-go/svc/redis"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries/qm"
)
// # Judge core
// This part of code is designed to be stateless and all state is stored in Redis for persistence.
// List of redis keys used:
// 1. rediskey.CORE_QUEUE: A Redis 5 stream where each entry is a submission. Every entry has the following keys:
// - sid: Submission id.
// - data: additional related data in JSON format.
// It has a consumer group called "judger". Judgers are identified by their username in HTTP basic auth. The username is used as the Redis consumer name.
// Redis mechanisms should be used to handle failures.
// 2. rediskey.CORE_SUBMISSION_PROGRESS: A Redis 5 stream containing submission progress. Every entry has the following keys:
// - type: One of "reset", "progress", "done".
// - data: JSON data if type is "progress".
// ## Judger protocol
// The judger endpoints use HTTP basic authentication. Username must match [0-9A-Za-z_]{3,16} and password is the judger token in App.JudgeToken.
// The judger connects to /judger/wait-for-task and upgrades the connection to a WebSocket. Then it receives tasks, process the task, send progress and result, and receive another task and loop.
// While processing a task, the judger sends messages with type="progress" to the broker. When the judger has done with the task, it sends a message with type="finish".
// The broker saves the result to the database and emits a type="done" event.
const GIN_JUDGER = "github.com/syzoj/syzoj-ng-go/svc/app.judger"
func (a *App) ensureQueue(ctx context.Context, queueName string) error {
_, err := a.Redis.DoContext(ctx, "XGROUP", "CREATE", rediskey.CORE_QUEUE.Format(queueName), "judger", "$", "MKSTREAM")
if rerr, ok := err.(redis.Error); ok {
if rerr[:10] == "BUSYGROUP " {
return nil
}
}
return err
}
var judgerRegexp = regexp.MustCompile("[0-9A-Za-z_]{3,16}")
func (a *App) useCheckJudgeToken(c *gin.Context) {
judger := c.Query("user")
token := c.Query("token")
if a.JudgeToken == "" || token != a.JudgeToken || !judgerRegexp.Match([]byte(judger)) {
c.JSON(403, gin.H{"error": "Token doesn't match or invalid judger name (judger name must be [0-9A-Za-z_]{3,16})"})
return
}
c.Set(GIN_JUDGER, judger)
}
var wsUpgrader = websocket.Upgrader{} // TODO: configure ReadBufferSize and WriteBufferSize (seems to default to 4096)
type TaskResult struct {
Id string `json:"id"`
Sid string `json:"sid"`
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}
// TODO: move logic to judge
func (a *App) getJudgeWaitForTask(c *gin.Context) {
ctx, cancel := context.WithCancel(c.Request.Context())
defer cancel()
conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.WithError(err).Error("failed to upgrade websocket")
return
}
defer conn.Close()
pipeline, err := a.Redis.NewPipeline(ctx)
if err != nil {
log.WithError(err).Error("failed to create redis pipeline")
return
}
defer pipeline.Close()
key := rediskey.CORE_QUEUE.Format("default")
judger := c.GetString(GIN_JUDGER)
sema := make(chan struct{}, 1) // queue capacity is 1
sema <- struct{}{}
var wg sync.WaitGroup
wg.Add(2)
// Fetch tasks
go func() {
defer wg.Done()
msgCh, errCh := a.Redis.ReadStreamGroup(ctx, key, "judger", judger, sema)
for {
select {
case <-ctx.Done():
return
case msg := <-msgCh:
sid := msg.Data["sid"]
data, err := redis.Bytes(a.Redis.DoContext(ctx, "GET", rediskey.CORE_SUBMISSION_DATA.Format(sid)))
if err != nil {
log.WithError(err).Error("failed to call GET")
return
}
pipeline.Do(a.checkRedis, "XADD", rediskey.CORE_SUBMISSION_PROGRESS.Format(sid), "*", "type", "reset")
if err := pipeline.Flush(ctx); err != nil {
log.WithError(err).Error("failed to call XADD")
return
}
if err := conn.WriteJSON(gin.H{
"id": msg.ID,
"sid": sid,
"data": json.RawMessage(data),
}); err != nil {
log.WithError(err).Error("failed to send data")
return
}
case err := <-errCh:
if err != nil {
log.WithError(err).Error("failed to read from Redis")
}
return
}
}
}()
// Receive results
go func() {
defer cancel()
defer wg.Done()
for {
data := &TaskResult{}
if err := conn.ReadJSON(data); err != nil {
log.WithError(err).Error("failed to read data")
return
}
skey := rediskey.CORE_SUBMISSION_PROGRESS.Format(data.Sid)
switch data.Type {
case "progress":
pipeline.Do(a.checkRedis, "XADD", skey, "*", "type", "progress", "data", []byte(data.Data))
case "finish":
res := &judge.Judge{}
if err := json.Unmarshal(data.Data, res); err != nil {
log.WithField("sid", data.Sid).WithError(err).Error("failed to parse data")
return
}
if res.Type == null.IntFrom(4) {
// Save submission result
sum := judge.GetSummary(res)
subm, err := models.JudgeStates(qm.Where("task_id=?", data.Sid)).One(ctx, a.Db)
if err != nil {
log.WithError(err).Error("failed to query db")
return
}
subm.Score = null.IntFrom(int(sum.Score.Float64))
subm.Pending = null.Int8From(0)
subm.Status = null.StringFrom(sum.Status)
subm.TotalTime = null.IntFrom(int(sum.Time.Float64))
subm.MaxMemory = null.IntFrom(int(sum.Memory.Float64))
progBytes, err := json.Marshal(res.Progress)
if err != nil {
panic(err)
}
subm.Result = null.StringFrom(string(progBytes))
if _, err := subm.Update(ctx, a.Db, boil.Blacklist()); err != nil {
log.WithError(err).Error("failed to update db")
return
}
pipeline.Do(a.checkRedis, "SET", rediskey.CORE_SUBMISSION_RESULT.Format(data.Sid), data.Data)
if err := pipeline.Flush(ctx); err != nil {
log.WithError(err).Error("failed to send redis")
return
}
if err := a.JudgeService.SaveTask(ctx, data.Sid, res); err != nil {
log.WithField("sid", data.Sid).WithError(err).Error("failed to save submission")
return
}
pipeline.Do(a.checkRedis, "XADD", skey, "*", "type", "done")
var res svcredis.RedisResult
pipeline.Do(res.Save, "XACK", key, "judger", data.Id)
if err := pipeline.Flush(ctx); err != nil {
log.WithError(err).Error("failed to flush to Redis")
return
}
n, err := redis.Int64(res.Result, res.Err)
if err != nil {
log.WithError(err).Error("failed to call XACK")
return
}
for ; n > 0; n-- {
select {
case <-ctx.Done():
return
case sema <- struct{}{}:
}
}
}
}
}
}()
wg.Wait()
}
func (a *App) getTaskProgress(c *gin.Context) {
ctx, cancel := context.WithCancel(c.Request.Context())
defer cancel()
key := rediskey.CORE_SUBMISSION_PROGRESS.Format(c.Param("sid"))
lastId := c.Request.Header.Get("Last-Event-Id")
if lastId == "" {
lastId = "0"
}
c.Header("X-Accel-Buffering", "no") // need this to make SSE work through nginx
c.Render(200, sse.Event{Event: "start"})
ping := time.NewTicker(time.Second * 30)
defer ping.Stop()
chMsg, chErr := a.Redis.ReadStream(ctx, key, lastId)
for {
select {
case <-ctx.Done():
return
case err := <-chErr:
c.AbortWithError(500, err)
return
case msg := <-chMsg:
var ev sse.Event
ev.Id = msg.ID
ev.Event = msg.Data["type"]
if d, ok := msg.Data["data"]; ok {
ev.Data = json.RawMessage(d)
}
c.Render(-1, ev)
c.Writer.Flush()
case <-ping.C:
c.Render(-1, sse.Event{Event: "ping"})
c.Writer.Flush()
}
}
}
// Handle judge done events and update stats.
func (a *App) handleJudgeDone(ctx context.Context) error {
pipe, err := a.Redis.NewPipeline(ctx)
if err != nil {
return err
}
pipe.Do(nil, "XGROUP", "CREATE", rediskey.MAIN_JUDGE_DONE, "main", "$", "MKSTREAM")
if err := pipe.Flush(ctx); err != nil {
return err
}
sema := make(chan struct{}, 1)
sema <- struct{}{}
chMsg, chErr := a.Redis.ReadStreamGroup(ctx, rediskey.MAIN_JUDGE_DONE, "main", "main", sema)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
pipe.Do(nil, "PING")
if err := pipe.Flush(ctx); err != nil {
log.WithError(err).Error("failed to flush to redis")
return
}
}
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-chErr:
return err
case msg := <-chMsg:
sid := msg.Data["sid"]
if sid == "" {
log.WithField("id", msg.ID).Error("judge done stream: missing sid field")
continue
}
subm, err := models.JudgeStates(qm.Where("task_id=?", sid)).One(ctx, a.Db)
if err != nil {
log.WithField("sid", sid).Error("judge done stream: failed to get submission")
continue
}
if subm.ProblemID.Valid {
pid := strconv.Itoa(subm.ProblemID.Int)
pipe.Do(a.checkRedis, "INCR", rediskey.MAIN_PROBLEM_SUBMITS.Format(pid))
if subm.Status == null.StringFrom("Accepted") {
pipe.Do(a.checkRedis, "INCR", rediskey.MAIN_PROBLEM_ACCEPTS.Format(pid))
}
if subm.UserID.Valid {
uid := strconv.Itoa(subm.UserID.Int)
pipe.Do(a.checkRedis, "HSET", rediskey.MAIN_USER_LAST_SUBMISSION.Format(uid), pid, sid)
if subm.Status == null.StringFrom("Accepted") {
pipe.Do(a.checkRedis, "HSET", rediskey.MAIN_USER_LAST_ACCEPT.Format(uid), pid, sid)
}
}
}
pipe.Do(a.checkRedis, "XACK", rediskey.MAIN_JUDGE_DONE, "main", msg.ID)
}
}
}