-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker_pool.go
162 lines (135 loc) · 3.27 KB
/
worker_pool.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
package module
import (
"github.com/TD-Hackathon-2022/DCoB-Scheduler/api"
"github.com/pkg/errors"
"sync"
"sync/atomic"
"unsafe"
)
var notOccupied = "not_occupied"
type worker struct {
id string
status api.WorkerStatus
occupiedBy *string
task *Task
ch chan *api.Msg
statusNotify func(*worker, *api.StatusPayload)
exitNotify func(*worker)
}
func (w *worker) atomicGetOccupiedBy() *string {
return (*string)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&w.occupiedBy))))
}
func (w *worker) assign(t *Task, notify func(*worker, *api.StatusPayload), exitNotify func(*worker)) (success bool) {
occupiedBy := w.atomicGetOccupiedBy()
if occupiedBy == ¬Occupied || *occupiedBy != t.JobId || w.task != nil {
return false
}
w.statusNotify = notify
w.exitNotify = exitNotify
w.task = t
w.ch <- &api.Msg{
Cmd: api.CMD_Assign,
Payload: &api.Msg_Assign{
Assign: &api.AssignPayload{
TaskId: t.Id,
Data: t.Ctx.InitData.(string),
FuncId: t.FuncId,
},
},
}
return true
}
func (w *worker) interrupt() {
w.ch <- &api.Msg{
Cmd: api.CMD_Interrupt,
Payload: &api.Msg_Interrupt{
Interrupt: &api.InterruptPayload{
TaskId: w.task.Id,
},
},
}
}
func (w *worker) occupied() bool {
return w.atomicGetOccupiedBy() != ¬Occupied
}
func (w *worker) occupy(jobId string) (success bool) {
// TODO: consider closing status
return atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&w.occupiedBy)),
unsafe.Pointer(¬Occupied),
unsafe.Pointer(&jobId))
}
func (w *worker) release() {
w.task = nil
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&w.occupiedBy)), unsafe.Pointer(¬Occupied))
}
type WorkerPool struct {
pool sync.Map
}
func (w *WorkerPool) Add(id string, ch chan *api.Msg) {
_, exist := w.pool.Load(id)
if exist {
return
}
w.pool.Store(id, &worker{
id: id,
status: api.WorkerStatus_Idle,
occupiedBy: ¬Occupied,
ch: ch,
})
}
func (w *WorkerPool) Remove(id string) {
wkrOri, exist := w.pool.Load(id)
if !exist {
return
}
wkr := wkrOri.(*worker)
if wkr.occupied() {
wkr.exitNotify(wkr)
}
w.pool.Delete(id)
}
func (w *WorkerPool) apply(jobId string) (wkr *worker, found bool) {
w.pool.Range(func(_, v interface{}) bool {
wkr = v.(*worker)
if wkr.occupy(jobId) {
return false
}
// clear wkr
wkr = nil
return true
})
return wkr, wkr != nil
}
func (w *WorkerPool) returnBack(wkr *worker) {
wkr.status = api.WorkerStatus_Idle
wkr.release()
}
func (w *WorkerPool) UpdateStatus(id string, payload *api.StatusPayload) error {
wkrOri, exist := w.pool.Load(id)
if !exist {
return errors.Errorf("Worker id: %s not regsitered, no context found.", id)
}
wkr := wkrOri.(*worker)
if !wkr.occupied() {
return errors.Errorf("Worker id: %s not occupied", id)
}
if wkr.task == nil || (wkr.task.Id != payload.TaskId) {
return errors.Errorf("Task id: %s not assigned to worker %s", payload.TaskId, id)
}
wkr.status = payload.WorkStatus
wkr.statusNotify(wkr, payload)
return nil
}
func (w *WorkerPool) InterruptJobTasks(jobId string) {
w.pool.Range(func(_, v interface{}) bool {
wkr := v.(*worker)
if wkr.task.JobId == jobId {
wkr.interrupt()
}
return true
})
}
func NewWorkerPool() *WorkerPool {
return &WorkerPool{}
}