-
Notifications
You must be signed in to change notification settings - Fork 24
/
worker.go
434 lines (357 loc) · 9.36 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2020 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package job
import (
"context"
"encoding/json"
"strconv"
"strings"
"github.com/douyu/juno-agent/pkg/job/etcd"
"github.com/douyu/juno-agent/util"
"github.com/douyu/jupiter/pkg/client/etcdv3"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/sony/sonyflake"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
)
// Node 执行 cron 命令服务的结构体
type Worker struct {
*Config
*etcdv3.Client
*Cron
ID string
ImmediatelyRun bool // 是否立即执行
jobs Jobs // 和结点相关的任务
cmds map[string]*Cmd
runningJobs map[string]context.CancelFunc
done chan struct{}
taskIdGen *sonyflake.Sonyflake
}
func NewWorker(conf *Config) (w *Worker) {
w = &Worker{
Config: conf,
ID: conf.HostName,
Client: etcdv3.StdConfig("default").MustBuild(),
ImmediatelyRun: false,
cmds: make(map[string]*Cmd),
runningJobs: make(map[string]context.CancelFunc),
done: make(chan struct{}),
taskIdGen: sonyflake.NewSonyflake(sonyflake.Settings{}), // default setting
}
w.Cron = newCron(w)
w.logger.Info("agent info :", xlog.String("name", conf.AppIP+":"+conf.HostName))
return
}
func (w *Worker) Run() error {
w.logger.Info("Worker run...")
w.Cron.Run()
lockWCh := w.Client.Watch(context.Background(), LockKeyPrefix, clientv3.WithPrefix())
onceWch := w.Client.Watch(context.Background(), OnceKeyPrefix+w.HostName, clientv3.WithPrefix())
procWch := w.Client.Watch(context.Background(), ProcKeyPrefix, clientv3.WithPrefix())
jobWch, err := etcd.WatchPrefix(w.Client, context.Background(), JobsKeyPrefix)
if err != nil {
panic(err)
}
// load prev jobs
w.loadJobs(jobWch.IncipientKeyValues())
for {
select {
case ev := <-lockWCh:
w.handleLockEv(ev)
case ev := <-onceWch:
w.handleOnceEv(ev)
case ev := <-procWch:
w.handleProcEv(ev)
case ev := <-jobWch.C():
w.handleJobEv(ev)
}
}
return nil
}
func (w *Worker) loadJobs(keyValue []*mvccpb.KeyValue) {
w.jobs = make(map[string]*Job)
if len(keyValue) == 0 {
return
}
for _, val := range keyValue {
job, err := w.GetJobContentFromKv(val.Key, val.Value)
if err != nil {
w.logger.Warnf("job[%s] is invalid: %s", val.Key, err.Error())
continue
}
job.runOn = w.ID
if _, ok := w.jobs[job.ID]; !ok {
w.addJob(job)
}
}
return
}
func (w *Worker) delJob(id string) {
job, ok := w.jobs[id]
// 之前此任务没有在当前结点执行
if !ok {
return
}
xlog.Error("Worker.delJob:delete a job", xlog.String("jobId", id))
delete(w.jobs, id)
job.Unlock()
cmds := job.Cmds()
if len(cmds) == 0 {
return
}
for _, cmd := range cmds {
w.delCmd(cmd)
}
return
}
func (w *Worker) modJob(job *Job) {
oJob, ok := w.jobs[job.ID]
if !ok {
w.addJob(job)
return
}
job.Worker = w
job.locked = oJob.locked
if util.InStringArray(job.Nodes, w.HostName) < 0 {
w.delJob(job.ID)
return
}
if job.JobType != oJob.JobType { // if job-type modified
if job.JobType == TypeNormal {
if job.mutex != nil && job.locked {
job.mutex.Unlock()
}
} else if job.JobType == TypeAlone {
w.delJob(job.ID)
w.addJob(job)
return
}
}
prevCmds := oJob.Cmds()
*oJob = *job
cmds := oJob.Cmds()
// 筛选出需要删除的任务
for id, cmd := range cmds {
w.modCmd(cmd)
delete(prevCmds, id)
}
for _, cmd := range prevCmds {
w.delCmd(cmd)
}
}
func (w *Worker) addJob(job *Job) {
job.Worker = w
if util.InStringArray(job.Nodes, w.HostName) < 0 {
// ignore
xlog.Info("Worker.addJob: Nodes do not contain current node, skip it.", xlog.String("jobId", job.ID))
return
}
if job.JobType == TypeAlone {
err := job.Lock()
if err != nil {
xlog.Info("failed to lock job. ignore it", xlog.String("jobId", job.ID))
return
}
}
xlog.Info("Worker.addJob: add a job", xlog.String("jobId", job.ID), xlog.Any("job", job))
// 添加任务到当前节点
w.jobs[job.ID] = job
cmds := job.Cmds()
if len(cmds) == 0 {
return
}
for _, cmd := range cmds {
w.addCmd(cmd)
}
return
}
func (w *Worker) delCmd(cmd *Cmd) {
c, ok := w.cmds[cmd.GetID()]
if ok {
delete(w.cmds, cmd.GetID())
w.Cron.Remove(c.schEntryID)
}
w.logger.Infof("job[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.Timer.ID, cmd.Timer.Cron)
}
func (w *Worker) modCmd(cmd *Cmd) {
c, ok := w.cmds[cmd.GetID()]
if !ok {
w.addCmd(cmd)
return
}
entryID := c.schEntryID
sch := c.Timer.Cron
*c = *cmd
c.schEntryID = entryID
// 节点执行时间改变,更新 cron
// 否则不用更新 cron
if c.Timer.Cron != sch {
w.Cron.Remove(entryID)
c.schEntryID = w.Cron.Schedule(c.Timer.Schedule, c)
}
w.logger.Infof("job[%s]rule[%s] timer[%s] has updated", c.Job.ID, c.Timer.ID, c.Timer.Cron)
}
func (w *Worker) addCmd(cmd *Cmd) {
cmd.schEntryID = w.Cron.Schedule(cmd.Timer.Schedule, cmd)
w.cmds[cmd.GetID()] = cmd
w.logger.Infof("job[%s] rule[%s] timer[%s] has added",
cmd.Job.ID, cmd.Timer.ID, cmd.Timer.Cron)
return
}
func (w *Worker) GetJobContentFromKv(key []byte, value []byte) (*Job, error) {
var err error
job := &Job{}
if err := json.Unmarshal(value, job); err != nil {
w.logger.Warnf("job[%s] unmarshal err: %s", key, err.Error())
return nil, err
}
if err := job.ValidRules(); err != nil {
w.logger.Warnf("valid rules [%s] err: %s", key, err.Error())
return nil, err
}
job.mutex, err = w.Client.NewMutex(LockKeyPrefix+job.ID, concurrency.WithTTL(30))
if err != nil {
return nil, err
}
return job, nil
}
func (w *Worker) GetOnceJobFromKv(key []byte, value []byte) (*OnceJob, error) {
job := &OnceJob{}
if err := json.Unmarshal(value, job); err != nil {
w.logger.Warnf("job[%s] unmarshal err: %s", key, err.Error())
return nil, err
}
if err := job.ValidRules(); err != nil {
w.logger.Warnf("valid rules [%s] err: %s", key, err.Error())
return nil, err
}
return job, nil
}
func (w *Worker) KillExecutingProc(process *Process) {
pid, _ := strconv.Atoi(process.ID)
if err := killProcess(pid); err != nil {
w.logger.Warnf("process:[%d] force kill failed, error:[%s]", pid, err)
return
}
}
func (w *Worker) handleLockEv(ev clientv3.WatchResponse) {
for _, ev := range ev.Events {
switch {
case ev.Type == clientv3.EventTypeDelete:
// watch deleted job and try to lock that job
jobId := getJobIDFromLockKey(string(ev.Kv.Key))
resp, err := w.Client.Get(context.Background(), LockKeyPrefix+jobId+"/", clientv3.WithPrefix())
if err != nil {
return
}
if resp.Count > 0 {
return
}
resp, err = w.Client.Get(context.Background(), JobsKeyPrefix+jobId)
if err != nil {
return
}
if len(resp.Kvs) == 0 {
return
}
jobKv := resp.Kvs[0]
job, err := w.GetJobContentFromKv(jobKv.Key, jobKv.Value)
if err != nil {
return
}
job.runOn = w.ID
if _, ok := w.jobs[jobId]; !ok { // job not exists
w.addJob(job)
}
}
}
}
func (w *Worker) handleOnceEv(ev clientv3.WatchResponse) {
for _, event := range ev.Events {
switch {
case event.IsCreate(), event.IsModify():
w.logger.Info("once task...")
job, err := w.GetOnceJobFromKv(event.Kv.Key, event.Kv.Value)
if err != nil {
xlog.Error("get job from kv failed", xlog.String("err", err.Error()))
continue
}
job.Worker = w
go job.RunWithRecovery(WithTaskID(job.TaskID))
}
}
}
func (w *Worker) handleProcEv(ev clientv3.WatchResponse) {
for _, event := range ev.Events {
switch {
case event.IsModify():
w.logger.Info("exec process task...")
key := string(event.Kv.Key)
process, err := GetProcFromKey(key)
if err != nil {
w.logger.Warnf("err: %s, kv: %s", err.Error(), event.Kv.String())
continue
}
if process.NodeID != w.ID {
continue
}
val := string(event.Kv.Value)
pv := &ProcessVal{}
err = json.Unmarshal([]byte(val), pv)
if err != nil {
continue
}
process.ProcessVal = *pv
if process.Killed {
w.KillExecutingProc(process)
}
}
}
}
func (w *Worker) handleJobEv(event *clientv3.Event) {
switch {
case event.IsCreate():
w.logger.Info("is create..")
job, err := w.GetJobContentFromKv(event.Kv.Key, event.Kv.Value)
if err != nil {
return
}
job.runOn = w.ID
w.addJob(job)
case event.IsModify():
w.logger.Info("is IsModify..")
job, err := w.GetJobContentFromKv(event.Kv.Key, event.Kv.Value)
if err != nil {
return
}
job.runOn = w.ID
w.modJob(job)
case event.Type == clientv3.EventTypeDelete:
w.logger.Info("is EventTypeDelete..")
w.delJob(GetIDFromKey(string(event.Kv.Key)))
default:
w.logger.Warnf("unknown event type[%v] from job[%s]", event.Type, string(event.Kv.Key))
}
}
func (w *Worker) CleanJobs() {
w.logger.Info("Worker: start clean jobs")
for _, job := range w.jobs {
w.delJob(job.ID)
}
}
func getJobIDFromLockKey(key string) (jobId string) {
key = strings.TrimLeft(key, LockKeyPrefix)
return strings.Split(key, "/")[0]
}