-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
365 lines (328 loc) · 7.6 KB
/
worker.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
package coco
import (
"context"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/imagvfx/coco/lib/container"
"github.com/imagvfx/coco/pb"
"github.com/imagvfx/coco/service"
"google.golang.org/grpc"
)
type WorkerStatus int
const (
WorkerNotFound = WorkerStatus(iota)
WorkerReady
WorkerRunning
WorkerCooling
)
// Worker is a worker who continuously takes a task and run it's commands.
type Worker struct {
sync.Mutex
// addr is the worker's listen addr.
addr string
// status indicates the worker's current status.
status WorkerStatus
// task directs a task the worker is currently working.
// It is nil if the worker is in idle.
task *TaskID
group *WorkerGroup
update func(service.WorkerUpdater) error
}
func NewWorker(addr string) *Worker {
return &Worker{addr: addr, status: WorkerReady}
}
// ToSQL converts a Worker into a SQLWorker.
func (w *Worker) ToSQL() *service.Worker {
var maybeJob *int
var maybeTask *int
if w.task != nil {
maybeJob = &w.task[0]
maybeTask = &w.task[1]
}
sw := &service.Worker{
Addr: w.addr,
Status: int(w.status),
Job: maybeJob,
Task: maybeTask,
}
return sw
}
// FromSQL converts a SQLWorker into a Worker.
func (w *Worker) FromSQL(sw *service.Worker) {
w.addr = sw.Addr
w.status = WorkerStatus(sw.Status)
w.task = nil
if sw.Job != nil {
w.task = &TaskID{*sw.Job, *sw.Task}
}
}
// Update updates a worker in both the program and the db.
func (w *Worker) Update(u service.WorkerUpdater) error {
u.Addr = w.addr
err := w.update(u)
if err != nil {
return err
}
if u.UpdateStatus {
w.status = WorkerStatus(u.Status)
}
if u.UpdateTask {
w.task = nil
if u.Job != nil {
w.task = &TaskID{*u.Job, *u.Task}
}
}
return nil
}
type WorkerGroup struct {
Name string
Matchers []AddressMatcher
ServeTargets []string
}
func (g WorkerGroup) Match(addr string) bool {
for _, m := range g.Matchers {
if m.Match(addr) {
return true
}
}
return false
}
type WorkerManager struct {
sync.Mutex
WorkerService service.WorkerService
worker map[string]*Worker
workers *container.UniqueQueue
workerGroups []*WorkerGroup
nForTag map[string]int
// ReadyCh tries fast matching of a worker and a task.
ReadyCh chan struct{}
}
// NewWorkerManager create a new WorkerManager.
// It restores previous data with WorkerService.
func NewWorkerManager(ws service.WorkerService, wgrps []*WorkerGroup) (*WorkerManager, error) {
m := &WorkerManager{}
m.WorkerService = ws
m.worker = make(map[string]*Worker)
m.workers = container.NewUniqueQueue()
m.workerGroups = wgrps
m.nForTag = make(map[string]int)
m.ReadyCh = make(chan struct{})
err := m.restore()
if err != nil {
return nil, err
}
return m, nil
}
// restore restores a WorkerManager from a db with WorkerService.
func (m *WorkerManager) restore() error {
sqlWorkers, err := m.WorkerService.FindWorkers(service.WorkerFilter{})
if err != nil {
return err
}
for _, sw := range sqlWorkers {
w := &Worker{}
w.FromSQL(sw)
m.add(w)
}
return nil
}
func (m *WorkerManager) ServableTargets() []string {
servable := make([]string, 0)
for t, n := range m.nForTag {
if n != 0 {
servable = append(servable, t)
}
}
return servable
}
func (m *WorkerManager) Add(w *Worker) error {
_, ok := m.worker[w.addr]
if ok {
return fmt.Errorf("worker already exists: %v", w.addr)
}
err := m.WorkerService.AddWorker(w.ToSQL())
if err != nil {
return err
}
m.add(w)
return nil
}
// add adds a worker to the WorkerManager without update a db.
// Use Add instead of this, except when it is really needed.
func (m *WorkerManager) add(w *Worker) {
addr := strings.Split(w.addr, ":")[0] // remove port
for _, g := range m.workerGroups {
if g.Match(addr) {
w.group = g
break
}
}
w.update = m.WorkerService.UpdateWorker
m.worker[w.addr] = w
}
func (m *WorkerManager) Bye(workerAddr string) error {
w, ok := m.worker[workerAddr]
if !ok {
return fmt.Errorf("worker not found: %v", workerAddr)
}
if w.task != nil {
// TODO: how to cancel the task?
}
olds := w.status
if olds == WorkerNotFound {
return fmt.Errorf("worker already logged off: %v", workerAddr)
}
var maybeJob *int
var maybeTask *int
if w.task != nil {
maybeJob = &w.task[0]
maybeTask = &w.task[1]
}
err := w.Update(service.WorkerUpdater{
UpdateStatus: true,
Status: int(WorkerNotFound),
UpdateTask: true,
Job: maybeJob,
Task: maybeTask,
})
if err != nil {
return err
}
m.workers.Remove(w)
for _, t := range w.group.ServeTargets {
m.nForTag[t] -= 1
if m.nForTag[t] < 0 {
panic("shouldn't happen")
}
}
return nil
}
func (m *WorkerManager) FindByAddr(addr string) *Worker {
return m.worker[addr]
}
// Ready reports that a worker is ready for a new task.
// NOTE: It should be only called by the worker through workerFarm.
func (m *WorkerManager) Ready(w *Worker) error {
if w == nil {
return fmt.Errorf("nil worker")
}
err := w.Update(service.WorkerUpdater{
UpdateStatus: true,
Status: int(WorkerReady),
UpdateTask: true,
Job: nil,
Task: nil,
})
if err != nil {
return err
}
m.workers.Push(w)
if w.group != nil {
for _, t := range w.group.ServeTargets {
m.nForTag[t] += 1
}
}
go func() { m.ReadyCh <- struct{}{} }()
return nil
}
func (m *WorkerManager) Pop(target string) *Worker {
var w *Worker
for {
v := m.workers.Pop()
if v == nil {
return nil
}
w = v.(*Worker)
servable := false
for _, t := range w.group.ServeTargets {
if t == "*" || t == target {
servable = true
break
}
}
if servable {
break
}
defer func() { m.workers.Push(w) }()
}
for _, t := range w.group.ServeTargets {
m.nForTag[t] -= 1
if m.nForTag[t] < 0 {
panic("shouldn't happen")
}
}
return w
}
func (m *WorkerManager) Push(w *Worker) {
m.workers.Push(w)
}
// SendPing sends a ping to a worker.
// The worker will let us know the task id the worker is currently working on.
// It returns a pointer of the task id. Or returns nil if the worker is currently in idle.
// It will return an error if the communication is failed.
func (m *WorkerManager) SendPing(w *Worker) (*TaskID, error) {
conn, err := grpc.Dial(w.addr, grpc.WithInsecure(), grpc.WithTimeout(time.Second))
if err != nil {
return nil, err
}
defer conn.Close()
c := pb.NewWorkerClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := c.Ping(ctx, &pb.PingRequest{})
if err != nil {
return nil, err
}
if resp.TaskId == "" {
return nil, nil
}
tid, err := TaskIDFromString(resp.TaskId)
if err != nil {
return nil, err
}
return &tid, nil
}
func (m *WorkerManager) SendTask(w *Worker, t *Task) error {
conn, err := grpc.Dial(w.addr, grpc.WithInsecure(), grpc.WithTimeout(time.Second))
if err != nil {
return err
}
defer conn.Close()
c := pb.NewWorkerClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req := &pb.RunRequest{}
req.Id = t.ID.String()
for _, c := range t.Commands {
reqCmd := &pb.Command{
Args: c,
}
req.Cmds = append(req.Cmds, reqCmd)
}
_, err = c.Run(ctx, req)
if err != nil {
return err
}
return nil
}
func (m *WorkerManager) SendCancelTask(w *Worker, t *Task) (err error) {
log.Printf("cancel: %v %v", w.addr, t.ID)
conn, err := grpc.Dial(w.addr, grpc.WithInsecure(), grpc.WithTimeout(time.Second))
if err != nil {
return err
}
defer conn.Close()
c := pb.NewWorkerClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req := &pb.CancelRequest{}
req.Id = t.ID.String()
_, err = c.Cancel(ctx, req)
if err != nil {
return err
}
return nil
}