forked from shunfei/cronsun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.go
277 lines (231 loc) · 4.92 KB
/
proc.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
package cronsun
import (
"encoding/json"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
client "github.com/coreos/etcd/clientv3"
"cronsun/conf"
"cronsun/log"
)
var (
lID *leaseID
)
// 维持 lease id 服务
func StartProc() error {
lID = &leaseID{
ttl: conf.Config.ProcTtl,
lk: new(sync.RWMutex),
done: make(chan struct{}),
}
if lID.ttl == 0 {
return nil
}
err := lID.set()
go lID.keepAlive()
return err
}
func Reload(i interface{}) {
if lID.ttl == conf.Config.ProcTtl {
return
}
close(lID.done)
lID.done, lID.ttl = make(chan struct{}), conf.Config.ProcTtl
if conf.Config.ProcTtl == 0 {
return
}
if err := lID.set(); err != nil {
log.Warnf("proc lease id set err: %s", err.Error())
}
go lID.keepAlive()
}
func Exit(i interface{}) {
if lID.done != nil {
close(lID.done)
}
}
type leaseID struct {
ttl int64
ID client.LeaseID
lk *sync.RWMutex
done chan struct{}
}
func (l *leaseID) get() client.LeaseID {
if l.ttl == 0 {
return -1
}
l.lk.RLock()
id := l.ID
l.lk.RUnlock()
return id
}
func (l *leaseID) set() error {
id := client.LeaseID(-1)
resp, err := DefalutClient.Grant(l.ttl + 2)
if err == nil {
id = resp.ID
}
l.lk.Lock()
l.ID = id
l.lk.Unlock()
return err
}
func (l *leaseID) keepAlive() {
duration := time.Duration(l.ttl) * time.Second
timer := time.NewTimer(duration)
for {
select {
case <-l.done:
return
case <-timer.C:
if l.ttl == 0 {
return
}
id := l.get()
if id > 0 {
_, err := DefalutClient.KeepAliveOnce(l.ID)
if err == nil {
timer.Reset(duration)
continue
}
log.Warnf("proc lease id[%x] keepAlive err: %s, try to reset...", id, err.Error())
}
if err := l.set(); err != nil {
log.Warnf("proc lease id set err: %s, try to reset after %d seconds...", err.Error(), l.ttl)
} else {
log.Infof("proc set lease id[%x] success", l.get())
}
timer.Reset(duration)
}
}
}
// 当前执行中的任务信息
// key: /cronsun/proc/node/group/jobId/pid
// value: 开始执行时间
// key 会自动过期,防止进程意外退出后没有清除相关 key,过期时间可配置
type Process struct {
// parse from key path
ID string `json:"id"` // pid
JobID string `json:"jobId"`
Group string `json:"group"`
NodeID string `json:"nodeId"`
// parse from value
ProcessVal
running int32
hasPut int32
wg sync.WaitGroup
done chan struct{}
}
type ProcessVal struct {
Time time.Time `json:"time"` // 开始执行时间
Killed bool `json:"killed"` // 是否强制杀死
}
func GetProcFromKey(key string) (proc *Process, err error) {
ss := strings.Split(key, "/")
var sslen = len(ss)
if sslen < 5 {
err = fmt.Errorf("invalid proc key [%s]", key)
return
}
proc = &Process{
ID: ss[sslen-1],
JobID: ss[sslen-2],
Group: ss[sslen-3],
NodeID: ss[sslen-4],
}
return
}
func (p *Process) Key() string {
return conf.Config.Proc + p.NodeID + "/" + p.Group + "/" + p.JobID + "/" + p.ID
}
func (p *Process) Val() (string, error) {
b, err := json.Marshal(&p.ProcessVal)
if err != nil {
return "", err
}
return string(b), nil
}
// 获取节点正在执行任务的数量
func (j *Job) CountRunning() (int64, error) {
resp, err := DefalutClient.Get(conf.Config.Proc+j.runOn+"/"+j.Group+"/"+j.ID, client.WithPrefix(), client.WithCountOnly())
if err != nil {
return 0, err
}
return resp.Count, nil
}
// put 出错也进行 del 操作
// 有可能某种原因,put 命令已经发送到 etcd server
// 目前已知的 deadline 会出现此情况
func (p *Process) put() (err error) {
if atomic.LoadInt32(&p.running) != 1 {
return
}
if !atomic.CompareAndSwapInt32(&p.hasPut, 0, 1) {
return
}
id := lID.get()
val, err := p.Val()
if err != nil {
return err
}
if id < 0 {
if _, err = DefalutClient.Put(p.Key(), val); err != nil {
return
}
}
_, err = DefalutClient.Put(p.Key(), val, client.WithLease(id))
return
}
func (p *Process) del() error {
if atomic.LoadInt32(&p.hasPut) != 1 {
return nil
}
_, err := DefalutClient.Delete(p.Key())
return err
}
func (p *Process) Start() {
if p == nil {
return
}
if !atomic.CompareAndSwapInt32(&p.running, 0, 1) {
return
}
if conf.Config.ProcReq == 0 {
if err := p.put(); err != nil {
log.Warnf("proc put[%s] err: %s", p.Key(), err.Error())
}
return
}
p.done = make(chan struct{})
p.wg.Add(1)
go func() {
select {
case <-p.done:
case <-time.After(time.Duration(conf.Config.ProcReq) * time.Second):
if err := p.put(); err != nil {
log.Warnf("proc put[%s] err: %s", p.Key(), err.Error())
}
}
p.wg.Done()
}()
}
func (p *Process) Stop() {
if p == nil {
return
}
if !atomic.CompareAndSwapInt32(&p.running, 1, 0) {
return
}
if p.done != nil {
close(p.done)
}
p.wg.Wait()
if err := p.del(); err != nil {
log.Warnf("proc del[%s] err: %s", p.Key(), err.Error())
}
}
func WatchProcs(nid string) client.WatchChan {
return DefalutClient.Watch(conf.Config.Proc+nid, client.WithPrefix())
}