/
controller.go
208 lines (186 loc) · 4.91 KB
/
controller.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
package controller
import (
"context"
"fmt"
"path/filepath"
"runtime"
"sync"
"github.com/VOID001/D-judge/config"
"github.com/VOID001/D-judge/downloader"
"github.com/VOID001/D-judge/request"
log "github.com/Sirupsen/logrus"
"github.com/docker/engine-api/client"
"github.com/pkg/errors"
//"github.com/docker/engine-api/types"
//"github.com/docker/engine-api/types/container"
"net/http"
)
var mu sync.Mutex
var cpuMap []bool
var cpuCount int
const (
ErrMaxWorkerExceed = "max worker exceed"
)
type Daemon struct {
MaxWorker int
CurrentWorker int
WorkerState []string
workerChan chan Worker
resultChan chan RunResult
}
type RunResult struct {
Stage string
RunID int
err error
}
var httpcli http.Client
func init() {
cpuCount = runtime.NumCPU()
cpuMap = make([]bool, cpuCount)
httpcli = http.Client{}
}
func Ping(ctx context.Context) (err error) {
cli, err := client.NewClient(config.GlobalConfig.DockerServer, config.GlobalConfig.DockerVersion, nil, nil)
if err != nil {
err = errors.Wrap(err, "create docker client error")
return err
}
_, err = cli.Info(ctx)
if err != nil {
err = errors.Wrap(err, "ping docker server error")
return err
}
return
}
func (d *Daemon) AddTask(ctx context.Context, jinfo config.JudgeInfo, dir string, img string) (err error) {
log.Debugf("call AddTask(context, jinfo = %+v, dir = %+v, img = %+v)", jinfo, dir, img)
w := Worker{}
w.JudgeInfo = jinfo
w.WorkDir = dir
w.RunUser = "root"
w.DockerImage = img
d.workerChan <- w
return
}
func (d *Daemon) Run(ctx context.Context) {
d.workerChan = make(chan Worker, 100)
for i := 0; i < d.MaxWorker; i++ {
go d.run(ctx, i)
}
return
}
func (d *Daemon) run(ctx context.Context, cpuid int) {
for {
// Only Judge Error Will Processed here, other error will process
// in the worker function
if w, ok := <-d.workerChan; ok {
log.Infof("Started Judging RunID #%d, running on CPU %d", w.JudgeInfo.SubmitID, cpuid)
w.CPUID = cpuid
err := w.prepare(ctx)
if err != nil {
log.Error(err)
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
continue // Future will change to continue
}
log.Infof("RunID #%d prepare OK", w.JudgeInfo.SubmitID)
ok, err := w.build(ctx)
if err != nil {
w.cleanup(ctx)
log.Error(err)
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
continue
}
// Compile Error, stop the current test
if !ok {
w.cleanup(ctx)
continue
}
log.Infof("RunID #%d compile OK", w.JudgeInfo.SubmitID)
for {
// Request for testcase
tinfo := config.TestcaseInfo{}
err = request.Do(ctx, http.MethodGet, fmt.Sprintf("/testcases?judgingid=%d", w.JudgeInfo.SubmitID), nil, "", &tinfo)
if err != nil {
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
break
// Return Judge Error
}
if tinfo.TestcaseID == 0 {
break
}
log.Debugf("Testcase info %+v", tinfo)
dl := downloader.Downloader{}
dl.FileType = "testcase"
dl.Destination = filepath.Join(w.WorkDir, fmt.Sprintf("testcase%03d.in", tinfo.Rank))
dl.FileName = fmt.Sprintf("%d-%s.in", tinfo.TestcaseID, tinfo.MD5SumInput)
dl.SkipMD5Check = false
dl.MD5 = tinfo.MD5SumInput
dl.UseCache = true
dl.Params = []string{fmt.Sprintf("%d", tinfo.TestcaseID), "input"}
err = dl.Do(ctx)
if err != nil {
err = errors.Wrap(err, "worker error: downloading testcase error")
log.Error(err)
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
break
// Return Judge Error
}
dl.Destination = filepath.Join(w.WorkDir, fmt.Sprintf("testcase%03d.out", tinfo.Rank))
dl.FileName = fmt.Sprintf("%d-%s.out", tinfo.TestcaseID, tinfo.MD5SumInput)
dl.MD5 = tinfo.MD5SumOutput
dl.Params = []string{fmt.Sprintf("%d", tinfo.TestcaseID), "output"}
err = dl.Do(ctx)
if err != nil {
err = errors.Wrap(err, "worker error: downloading testcase error")
log.Error(err)
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
break
}
// Run testcase
ok, err = w.run(ctx, tinfo.Rank, tinfo.TestcaseID)
if err != nil {
w.cleanup(ctx)
err = errors.Wrap(err, "worker error")
log.Error(err)
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
break
}
if !ok {
break
}
log.Infof("Run Testcase %d OK", tinfo.Rank)
// Judge testcase
err = w.judge(ctx, tinfo.Rank, tinfo.TestcaseID)
if err != nil {
w.cleanup(ctx)
err = errors.Wrap(err, "worker error")
log.Error(err)
request.JudgeError(ctx, err, w.JudgeInfo.JudgingID)
break
}
log.Infof("Judge Testcase %d OK", tinfo.Rank)
}
err = w.cleanup(ctx)
if err != nil {
log.Error(err)
continue
}
} else {
break
}
}
return
}
func GetAvailableCPU(ctx context.Context) (cpuid int, err error) {
for i := 0; i < cpuCount; i++ {
if cpuMap[i] != true {
mu.Lock()
cpuMap[i] = true
cpuid = i
defer mu.Unlock()
return
}
}
cpuid = -1
return
}