-
Notifications
You must be signed in to change notification settings - Fork 0
/
polar.go
148 lines (129 loc) · 3.38 KB
/
polar.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
package polar
import (
"context"
"github.com/ArcticOJ/blizzard/v0/db/schema/contest"
"github.com/ArcticOJ/polar/v0/pb"
csmap "github.com/mhmtszr/concurrent-swiss-map"
"sync"
"sync/atomic"
)
type (
Polar struct {
queued *csmap.CsMap[string, *queue]
submissions *csmap.CsMap[uint32, *pb.Submission]
pending *csmap.CsMap[uint32, []contest.CaseResult]
isBound *csmap.CsMap[uint32, struct{}]
// Maximum allowed concurrent submissions.
parallelism uint16
judges map[string]*JudgeObj
// Lock for reading/writing to judges field above.
jm sync.RWMutex
ctx context.Context
// A function which returns another stateful function which handles results for an individual submission when called with a submission ID.
messageHandler func(uint32) func(result *pb.Result) bool
}
queue struct {
count atomic.Uint32
mutex sync.RWMutex
slice []*pb.Submission
waitChan chan string
}
JudgeObj struct {
*pb.Judge
// Internal properties below.
submissions map[uint32]struct{}
m sync.RWMutex
}
)
func NewPolar(ctx context.Context, messageHandler func(id uint32) func(result *pb.Result) bool) (p *Polar) {
p = &Polar{
queued: csmap.Create[string, *queue](),
pending: csmap.Create[uint32, []contest.CaseResult](),
submissions: csmap.Create[uint32, *pb.Submission](),
isBound: csmap.Create[uint32, struct{}](),
ctx: ctx,
judges: make(map[string]*JudgeObj),
messageHandler: messageHandler,
}
return
}
func (p *Polar) RegisterRuntimes(runtimes []*pb.Judge_Runtime) {
for _, rt := range runtimes {
// register a runtime if not present or update it
p.queued.SetIfAbsent(rt.Id, &queue{
waitChan: make(chan string, 1),
})
q, _ := p.queued.Load(rt.Id)
q.count.Add(1)
}
}
func (p *Polar) UpdateResult(id uint32, result contest.CaseResult) bool {
res, ok := p.pending.Load(id)
if !ok {
return false
}
res = append(res, result)
p.pending.Store(id, res)
return true
}
func (p *Polar) GetResults(id uint32) []contest.CaseResult {
r, ok := p.pending.Load(id)
if !ok {
return nil
}
return r
}
func (p *Polar) IsPending(id uint32) bool {
return p.pending.Has(id)
}
func (p *Polar) RuntimeAvailable(runtime string) bool {
q, ok := p.queued.Load(runtime)
if !ok {
return false
}
return q.count.Load() > 0
}
func (p *Polar) Reject(j *JudgeObj, id uint32) {
p.releaseSubmission(j, id)
}
func (p *Polar) StartServer() {
go p.serveRPC()
}
func (p *Polar) GetJudges() map[string]*JudgeObj {
p.jm.RLock()
defer p.jm.RUnlock()
return p.judges
}
// TODO: remove `isPending` check, force callers to choose whether to requeue this submission or simply ignore it
func (p *Polar) releaseSubmission(j *JudgeObj, id uint32) {
p.jm.RLock()
_, isPending := j.submissions[id]
p.jm.RUnlock()
if isPending {
p.jm.Lock()
delete(j.submissions, id)
p.jm.Unlock()
if sub, exist := p.submissions.Load(id); exist && p.IsPending(id) {
p.pending.Delete(id)
p.Push(sub, true)
}
return
}
}
func (p *Polar) destroy(judgeId string) {
p.jm.Lock()
judgeObj := p.judges[judgeId]
p.parallelism -= uint16(judgeObj.Parallelism)
delete(p.judges, judgeId)
p.jm.Unlock()
judgeObj.m.Lock()
for _, rt := range judgeObj.Runtimes {
if q, _ok := p.queued.Load(rt.Id); _ok {
q.count.Add(^uint32(0))
}
}
for id := range judgeObj.submissions {
p.releaseSubmission(judgeObj, id)
}
judgeObj.m.Unlock()
}