-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
366 lines (320 loc) · 9.73 KB
/
client.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
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/busgo/pink-go/etcd"
"log"
"net"
"strings"
"sync"
"time"
)
const (
InstancePath = "/pink/client/%s/instances/%s"
ExecuteSnapshotPath = "/pink/execute/snapshots/%s/%s/"
ExecuteSnapshotHistoryPath = "/pink/execute/history/snapshots/"
InstanceTTL = 5
)
const (
Init int32 = iota
Doing
Success
Fail
)
// PinkClient
type PinkClient struct {
cli *etcd.Cli
group string
ip string
instancePath string
executeSnapshotPath string
jobs map[string]Job
executingJobs map[string]*ExecuteSnapshot
rw sync.RWMutex
}
type ExecuteSnapshot struct {
Id string `json:"id"`
JobId string `json:"job_id"`
Name string `json:"name"`
Group string `json:"group"`
Cron string `json:"cron"`
Target string `json:"target"`
Ip string `json:"ip"`
Param string `json:"param"`
State int32 `json:"state"`
BeforeTime string `json:"before_time"`
ScheduleTime string `json:"schedule_time"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Times int64 `json:"times"`
Mobile string `json:"mobile"`
Version int32 `json:"version"`
Result string `json:"result"`
Remark string `json:"remark"`
}
func (es *ExecuteSnapshot) Encode() string {
content, _ := json.Marshal(es)
return string(content)
}
func (es *ExecuteSnapshot) Decode(content string) *ExecuteSnapshot {
_ = json.Unmarshal([]byte(content), es)
return es
}
func GetLocalIP() string {
adds, err := net.InterfaceAddrs()
if err != nil {
return "127.0.0.1"
}
for _, addr := range adds {
if ipNet, isIpNet := addr.(*net.IPNet); isIpNet && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
}
return "127.0.0.1"
}
// new PinkClient
func NewPinkClient(cli *etcd.Cli, group string) *PinkClient {
ip := GetLocalIP()
client := &PinkClient{cli: cli, group: group, ip: ip,
instancePath: fmt.Sprintf(InstancePath, group, ip),
executeSnapshotPath: fmt.Sprintf(ExecuteSnapshotPath, group, ip),
jobs: make(map[string]Job),
executingJobs: make(map[string]*ExecuteSnapshot),
rw: sync.RWMutex{},
}
go client.lookup()
go client.subscribeExecuteSnapshot()
return client
}
// lookup
func (client *PinkClient) lookup() {
leaseId := int64(0)
leaseId = client.selfRegister(leaseId)
response := client.cli.Watch(client.instancePath)
log.Printf("the pink client instance %s self register watch to:%s", client.ip, client.instancePath)
for {
select {
case event := <-response.KeyChangeCh:
switch event.Event {
case etcd.KeyDeleteChangeEvent:
log.Printf("the pink client instance %s self register watch to:%s key delete event:%+v", client.ip, client.instancePath, event)
leaseId = client.selfRegister(leaseId)
}
}
}
}
// self register
func (client *PinkClient) selfRegister(leaseId int64) int64 {
RETRY:
log.Printf("the pink client instance %s self register to:%s", client.ip, client.instancePath)
if leaseId > 0 {
_ = client.cli.Revoke(context.Background(), leaseId)
}
leaseId, err := client.cli.Keepalive(context.Background(), client.instancePath, client.ip, InstanceTTL)
if err != nil {
time.Sleep(time.Second)
goto RETRY
}
log.Printf("the pink client instance %s self register to:%s ,leaseId %d success", client.ip, client.instancePath, leaseId)
return leaseId
}
// subscribe the job with target
func (client *PinkClient) Subscribe(target string, job Job) {
client.rw.Lock()
defer client.rw.Unlock()
if strings.TrimSpace(target) == "" || job == nil {
log.Printf("the pink client %s targe is nil or job is nil", client.instancePath)
return
}
if targetJob := client.jobs[target]; targetJob != nil {
log.Printf("the pink client %s target %s has exists", client.instancePath, target)
return
}
client.jobs[target] = job
}
// subscribe execute snapshot
func (client *PinkClient) subscribeExecuteSnapshot() {
keys, values, err := client.cli.GetWithPrefix(context.Background(), client.executeSnapshotPath)
if err != nil {
panic(err)
}
// delete
client.dealSnapshots(keys, values)
resp := client.cli.WatchWithPrefix(client.executeSnapshotPath)
for {
select {
case ch := <-resp.KeyChangeCh:
client.handleSnapshotChange(ch)
}
}
}
// deal snapshots
func (client *PinkClient) dealSnapshots(keys, values []string) {
if len(keys) == 0 {
return
}
for pos := 0; pos < len(keys); pos++ {
value := values[pos]
if strings.TrimSpace(value) == "" {
continue
}
snapshot := new(ExecuteSnapshot).Decode(values[pos])
if snapshot.State == Init {
client.handleCreateSnapshot(&etcd.KeyChange{
Key: keys[pos],
Value: value,
})
} else if snapshot.State == Doing { // doing
snapshot.State = Fail
snapshot.Result = "Doing State"
client.transfer(keys[pos], snapshot)
} else { // success fail
client.transfer(keys[pos], snapshot)
}
}
}
// handle the execute snapshot change
func (client *PinkClient) handleSnapshotChange(kc *etcd.KeyChange) {
switch kc.Event {
case etcd.KeyCreateChangeEvent:
client.handleCreateSnapshot(kc)
case etcd.KeyUpdateChangeEvent:
client.handleUpdateSnapshot(kc)
case etcd.KeyDeleteChangeEvent:
client.handleDeleteSnapshot(kc)
}
}
// handle create snapshot
func (client *PinkClient) handleCreateSnapshot(kc *etcd.KeyChange) {
if strings.TrimSpace(kc.Value) == "" {
client.deleteSnapshot(kc.Key)
return
}
snapshot := new(ExecuteSnapshot).Decode(kc.Value)
targetJob := client.getTargetJob(kc.Key, snapshot)
if targetJob == nil {
log.Printf("the pink client %s target is nil snapshot:%+v", client.instancePath, snapshot)
return
}
if snapshot.State == Init {
go client.execute(kc.Key, targetJob, snapshot)
return
} else if snapshot.State == Doing { //
log.Printf("the pink client %s state is doing set fail state, snapshot:%+v", client.instancePath, snapshot)
snapshot.State = Fail
snapshot.Result = "history snapshot"
}
client.transfer(kc.Key, snapshot)
}
// handle update snapshot
func (client *PinkClient) handleUpdateSnapshot(kc *etcd.KeyChange) {
if strings.TrimSpace(kc.Value) == "" {
client.deleteSnapshot(kc.Key)
return
}
snapshot := new(ExecuteSnapshot).Decode(kc.Value)
targetJob := client.getTargetJob(kc.Key, snapshot)
if targetJob == nil {
log.Printf("the pink client %s target is nil snapshot:%+v", client.instancePath, snapshot)
return
}
if snapshot.State == Init {
go client.execute(kc.Key, targetJob, snapshot)
}
}
// handle delete snapshot
func (client *PinkClient) handleDeleteSnapshot(kc *etcd.KeyChange) {
log.Printf("the pink client %s handle delete snapshot key %s value %s", client.instancePath, kc.Key, kc.Value)
}
// transfer snapshot to history
func (client *PinkClient) transfer(key string, snapshot *ExecuteSnapshot) {
targetKey := fmt.Sprintf("%s%s", ExecuteSnapshotHistoryPath, snapshot.Id)
err := client.cli.Transfer(context.Background(), key, targetKey, snapshot.Encode())
if err != nil {
log.Printf("the pink client %s transfer key %s target key %s snapshot %+v fail %+v", client.instancePath, key, targetKey, snapshot, err)
return
}
}
// sync execute the snapshot to target job
func (client *PinkClient) execute(key string, targetJob Job, snapshot *ExecuteSnapshot) {
s := client.checkExists(snapshot.JobId)
if s != nil {
log.Printf("the pink client %s execute snapshot found job id exists,snapshot:%+v", client.instancePath, snapshot)
if s.Id != snapshot.Id {
snapshot.State = Fail
snapshot.Result = "Job is Doing,not allow repeat execute"
client.transfer(key, snapshot)
}
return
}
client.addDoingSnapshotRecord(snapshot.JobId, snapshot)
defer func() {
client.deleteDoingSnapshotRecord(snapshot.JobId)
}()
now := time.Now()
snapshot.State = Doing
snapshot.StartTime = now.Format("2006-01-02 15:04:05")
err := client.cli.Put(context.Background(), key, snapshot.Encode())
if err != nil {
log.Printf("the pink client %s update snapshot state fail,snapshot:%+v", client.instancePath, snapshot)
return
}
result, err := targetJob.Execute(snapshot.Param)
endTime := time.Now()
durationTime := endTime.Sub(now)
snapshot.Times = int64(durationTime / time.Second)
snapshot.EndTime = endTime.Format("2006-01-02 15:04:05")
if err != nil {
snapshot.State = Fail
snapshot.Result = err.Error()
} else {
snapshot.Result = result
snapshot.State = Success
}
client.transfer(key, snapshot)
}
// delete snapshot to history
func (client *PinkClient) deleteSnapshot(key string) {
err := client.cli.Delete(context.Background(), key)
if err != nil {
log.Printf("the pink client %s delete key %s fail %+v", client.instancePath, key, err)
return
}
}
// get target job
func (client *PinkClient) getTargetJob(key string, snapshot *ExecuteSnapshot) Job {
client.rw.RLock()
defer client.rw.RUnlock()
target := snapshot.Target
targetJob := client.jobs[target]
now := time.Now()
if targetJob != nil {
return targetJob
}
snapshot.StartTime = now.Format("2006-01-02 15:04:05")
snapshot.EndTime = now.Format("2006-01-02 15:04:05")
if snapshot.State == Doing || snapshot.State == Init {
snapshot.State = Fail
snapshot.Result = "not found the target job"
}
client.transfer(key, snapshot)
return nil
}
func (client *PinkClient) addDoingSnapshotRecord(key string, snapshot *ExecuteSnapshot) {
client.rw.Lock()
defer client.rw.Unlock()
client.executingJobs[key] = snapshot
}
func (client *PinkClient) deleteDoingSnapshotRecord(key string) {
client.rw.Lock()
defer client.rw.Unlock()
delete(client.executingJobs, key)
}
func (client *PinkClient) checkExists(jobId string) *ExecuteSnapshot {
client.rw.RLock()
defer client.rw.RUnlock()
return client.executingJobs[jobId]
}