-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmanager.go
391 lines (323 loc) · 8.91 KB
/
manager.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package test161
import (
"errors"
"fmt"
"sync"
"time"
)
// This file defines test161's test manager. The manager is responsible for
// keeping track of the number of running tests and limiting that number to
// a configurable capacity.
//
// There is a global test manager (testManager) that listens for new job
// requests on its SubmitChan. In the current implementation, this can
// only be accessed from within the package by one of the TestRunners.
// A test161Job consists of the test to run, the directory to find the
// binaries, and a channel to communicate the results on.
type test161Job struct {
Test *Test
Env *TestEnvironment
DoneChan chan *Test161JobResult
}
// A Test161JobResult consists of the completed test and any error that
// occurred while running the test.
type Test161JobResult struct {
Test *Test
Err error
}
type manager struct {
SubmitChan chan *test161Job
Capacity uint
// protected by L
statsCond *sync.Cond
queueCond *sync.Cond
isRunning bool
stats ManagerStats
}
type ManagerStats struct {
// protected by manager.L
Running uint `json:"running"`
HighRunning uint `json:"high_running"`
Queued uint `json:"queued"`
HighQueued uint `json:"high_queued"`
Finished uint `json:"finished"`
MaxWait int64 `json:"max_wait_ms"`
AvgWait int64 `json:"avg_wait_ms"`
StartTime time.Time
total int64 // denominator for avg
}
// Combined submission and tests statistics since the service started
type Test161Stats struct {
Status string `json:"status"`
SubmissionStats ManagerStats `json:"submission_stats"`
TestStats ManagerStats `json:"test_stats"`
}
const DEFAULT_MGR_CAPACITY uint = 0
func newManager() *manager {
m := &manager{
SubmitChan: nil,
Capacity: DEFAULT_MGR_CAPACITY,
statsCond: sync.NewCond(&sync.Mutex{}),
queueCond: sync.NewCond(&sync.Mutex{}),
isRunning: false,
}
return m
}
// The global test manager. NewEnvironment has a reference to
// this manager, which should be used by clients of this library.
// Unit tests use their own manager/environment for runner/assertion
// isolation.
var testManager *manager = newManager()
// Clear state and start listening for job requests
func (m *manager) start() {
m.statsCond.L.Lock()
defer m.statsCond.L.Unlock()
if m.isRunning {
return
}
m.stats = ManagerStats{
StartTime: time.Now(),
}
m.SubmitChan = make(chan *test161Job)
m.isRunning = true
// Listener goroutine
go func() {
// We simply spawn a worker that blocks until it can run.
for job := range m.SubmitChan {
go m.runOrQueueJob(job)
}
}()
}
// Queue the job if we're at capacity, and run it once we're under.
func (m *manager) runOrQueueJob(job *test161Job) {
m.statsCond.L.Lock()
queued := false
start := time.Now()
for m.Capacity > 0 && m.stats.Running >= m.Capacity {
if !queued {
queued = true
// Update queued stats
m.stats.Queued += 1
if m.stats.Queued > m.stats.HighQueued {
m.stats.HighQueued = m.stats.Queued
}
}
// Wait for a finished test to signal us
m.statsCond.Wait()
}
// We've got the green light... (and the stats lock)
if queued {
// Update the queue count and signal the submission manager (if there is one)
m.queueCond.L.Lock()
m.stats.Queued -= 1
m.queueCond.Signal()
m.queueCond.L.Unlock()
queued = false
// Max and average waits
curWait := int64(time.Now().Sub(start).Nanoseconds() / 1e6)
if m.stats.MaxWait < curWait {
m.stats.MaxWait = curWait
}
m.stats.AvgWait = (m.stats.total*m.stats.AvgWait + curWait) / (m.stats.total + 1)
m.stats.total += 1
}
m.stats.Running += 1
if m.stats.Running > m.stats.HighRunning {
m.stats.HighRunning = m.stats.Running
}
m.statsCond.L.Unlock()
// Go!
err := job.Test.Run(job.Env)
// And... we're done.
// Update stats
m.statsCond.L.Lock()
m.stats.Running -= 1
m.stats.Finished += 1
m.statsCond.Signal()
m.statsCond.L.Unlock()
// Pass the completed test back to the caller
// (Blocking call, we need to make sure the caller gets the result.)
job.DoneChan <- &Test161JobResult{job.Test, err}
}
// Shut it down
func (m *manager) stop() {
m.statsCond.L.Lock()
defer m.statsCond.L.Unlock()
if !m.isRunning {
return
}
m.isRunning = false
close(m.SubmitChan)
}
// Exported shared test manger functions
// Start the shared test manager
func StartManager() {
testManager.start()
}
// Stop the shared test manager
func StopManager() {
testManager.stop()
}
func SetManagerCapacity(capacity uint) {
testManager.Capacity = capacity
}
func ManagerCapacity() uint {
return testManager.Capacity
}
func (m *manager) Stats() *ManagerStats {
// Lock so we at least get a consistent view of the stats
testManager.statsCond.L.Lock()
defer testManager.statsCond.L.Unlock()
// copy
var res ManagerStats = testManager.stats
return &res
}
// Return a copy of the current shared test manager stats
func GetManagerStats() *ManagerStats {
return testManager.Stats()
}
//////// Submission Manager
//
// SubmissionManager handles running multiple submissions, which is useful for the server.
// The (Test)Manager already handles rate limiting tests, but we also need to be careful
// about rate limiting submissions. In particular, we don't need to build all new
// if we have queued tests. This just wastes cycles and I/O that the tests could use.
// Plus, the student will see the status go from building to running, but the boot test
// will just get queued.
const (
SM_ACCEPTING = iota
SM_NOT_ACCEPTING
SM_STAFF_ONLY
)
type SubmissionManager struct {
env *TestEnvironment
runlock *sync.Mutex // Block everything from running
l *sync.Mutex // Synchronize other state
status int
stats ManagerStats
}
func NewSubmissionManager(env *TestEnvironment) *SubmissionManager {
mgr := &SubmissionManager{
env: env,
runlock: &sync.Mutex{},
l: &sync.Mutex{},
status: SM_ACCEPTING,
stats: ManagerStats{
StartTime: time.Now(),
},
}
return mgr
}
func (sm *SubmissionManager) CombinedStats() *Test161Stats {
stats := &Test161Stats{
SubmissionStats: *sm.Stats(),
TestStats: *sm.env.manager.Stats(),
}
switch sm.Status() {
case SM_ACCEPTING:
stats.Status = "accepting submissions"
case SM_NOT_ACCEPTING:
stats.Status = "not accepting submissions"
case SM_STAFF_ONLY:
stats.Status = "accepting submissions from staff only"
}
return stats
}
func (sm *SubmissionManager) Stats() *ManagerStats {
sm.l.Lock()
defer sm.l.Unlock()
copy := sm.stats
return ©
}
func (sm *SubmissionManager) Run(s *Submission) error {
// The test manager we're associated with
mgr := sm.env.manager
sm.l.Lock()
// Check to see if we've been paused or stopped. The server checks too, but there's delay.
abort := false
abortMsg := ""
if sm.status == SM_NOT_ACCEPTING {
abort = true
abortMsg = "The submission server is not accepting new submissions at this time"
} else if sm.status == SM_STAFF_ONLY {
for _, student := range s.students {
if isStaff, _ := student.IsStaff(sm.env); !isStaff {
abort = true
abortMsg = "The submission server is not accepting new submissions from students at this time"
}
}
}
if abort {
sm.l.Unlock()
s.Status = SUBMISSION_ABORTED
err := errors.New(abortMsg)
s.Errors = append(s.Errors, fmt.Sprintf("%v", err))
sm.env.notifyAndLogErr("Submissions Closed", s, MSG_PERSIST_COMPLETE, 0)
return err
}
// Update Queued
sm.stats.Queued += 1
start := time.Now()
if sm.stats.HighQueued < sm.stats.Queued {
sm.stats.HighQueued = sm.stats.Queued
}
sm.l.Unlock()
///////////
// Queued here
sm.runlock.Lock()
// Still queued, but on deck. Wait on the manager's queue condition variable so we
// get notifications when the count changes.
mgr.queueCond.L.Lock()
for mgr.stats.Queued > 0 {
mgr.queueCond.Wait()
}
mgr.queueCond.L.Unlock()
// We may get a rush of builds here. Eventually we'll get a queue again, and
// the test manager handles this.
// TODO: Consider better build rate limiting
///////////
// Update run stats
sm.l.Lock()
sm.stats.Queued -= 1
sm.stats.Running += 1
if sm.stats.HighRunning < sm.stats.Running {
sm.stats.HighRunning = sm.stats.Running
}
// Max and average waits
curWait := int64(time.Now().Sub(start).Nanoseconds() / 1e6)
if sm.stats.MaxWait < curWait {
sm.stats.MaxWait = curWait
}
sm.stats.AvgWait = (sm.stats.total*sm.stats.AvgWait + curWait) / (sm.stats.total + 1)
sm.stats.total += 1
sm.l.Unlock()
// Run the submission
sm.runlock.Unlock()
err := s.Run()
// Update stats
sm.l.Lock()
sm.stats.Running -= 1
sm.stats.Finished += 1
sm.l.Unlock()
return err
}
func (sm *SubmissionManager) Pause() {
sm.l.Lock()
defer sm.l.Unlock()
sm.status = SM_NOT_ACCEPTING
}
func (sm *SubmissionManager) Resume() {
sm.l.Lock()
defer sm.l.Unlock()
sm.status = SM_ACCEPTING
}
func (sm *SubmissionManager) Status() int {
sm.l.Lock()
defer sm.l.Unlock()
return sm.status
}
func (sm *SubmissionManager) SetStaffOnly() {
sm.l.Lock()
defer sm.l.Unlock()
sm.status = SM_STAFF_ONLY
}