-
Notifications
You must be signed in to change notification settings - Fork 54
/
group.go
343 lines (253 loc) · 6.06 KB
/
group.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
package forest
import (
"context"
"errors"
"fmt"
"github.com/coreos/etcd/clientv3"
"github.com/labstack/gommon/log"
"math/rand"
"sync"
"time"
)
const (
GroupConfPath = "/forest/server/group/"
ClientPath = "/forest/client/%s/clients/"
)
type JobGroupManager struct {
node *JobNode
groups map[string]*Group
lk *sync.RWMutex
}
func NewJobGroupManager(node *JobNode) (mgr *JobGroupManager) {
mgr = &JobGroupManager{
node: node,
groups: make(map[string]*Group),
lk: &sync.RWMutex{},
}
go mgr.watchGroupPath()
go mgr.loopLoadGroups()
return
}
// watch the group path
func (mgr *JobGroupManager) watchGroupPath() {
keyChangeEventResponse := mgr.node.etcd.WatchWithPrefixKey(GroupConfPath)
for ch := range keyChangeEventResponse.Event {
mgr.handleGroupChangeEvent(ch)
}
}
func (mgr *JobGroupManager) loopLoadGroups() {
RETRY:
var (
keys [][]byte
values [][]byte
err error
)
if keys, values, err = mgr.node.etcd.GetWithPrefixKey(GroupConfPath); err != nil {
goto RETRY
}
if len(keys) == 0 {
return
}
for i := 0; i < len(keys); i++ {
path := string(keys[i])
groupConf, err := UParkGroupConf(values[i])
if err != nil {
log.Warnf("upark the group conf error:%#v", err)
continue
}
mgr.addGroup(groupConf.Name, path)
}
}
func (mgr *JobGroupManager) addGroup(name, path string) {
mgr.lk.Lock()
defer mgr.lk.Unlock()
if _, ok := mgr.groups[path]; ok {
return
}
group := NewGroup(name, path, mgr.node)
mgr.groups[path] = group
log.Infof("add a new group:%s,for path:%s", name, path)
}
// delete a group for path
func (mgr *JobGroupManager) deleteGroup(path string) {
var (
group *Group
ok bool
)
mgr.lk.Lock()
defer mgr.lk.Unlock()
if group, ok = mgr.groups[path]; ok {
return
}
// cancel watch the clients
_ = group.watcher.Close()
group.cancelFunc()
delete(mgr.groups, path)
log.Infof("delete a group:%s,for path:%s", group.name, path)
}
// handle the group change event
func (mgr *JobGroupManager) handleGroupChangeEvent(changeEvent *KeyChangeEvent) {
switch changeEvent.Type {
case KeyCreateChangeEvent:
mgr.handleGroupCreateEvent(changeEvent)
case KeyUpdateChangeEvent:
// ignore
case KeyDeleteChangeEvent:
mgr.handleGroupDeleteEvent(changeEvent)
}
}
func (mgr *JobGroupManager) handleGroupCreateEvent(changeEvent *KeyChangeEvent) {
groupConf, err := UParkGroupConf(changeEvent.Value)
if err != nil {
log.Warnf("upark the group conf error:%#v", err)
return
}
path := changeEvent.Key
mgr.addGroup(groupConf.Name, path)
}
func (mgr *JobGroupManager) handleGroupDeleteEvent(changeEvent *KeyChangeEvent) {
path := changeEvent.Key
mgr.deleteGroup(path)
}
func (mgr *JobGroupManager) selectClient(name string) (client *Client, err error) {
var (
group *Group
ok bool
)
if group, ok = mgr.groups[GroupConfPath+name]; !ok {
err = errors.New(fmt.Sprintf("the group:%s not found", name))
return
}
return group.selectClient()
}
type Group struct {
path string
name string
node *JobNode
watchPath string
clients map[string]*Client
watcher clientv3.Watcher
cancelFunc context.CancelFunc
lk *sync.RWMutex
}
// create a new group
func NewGroup(name, path string, node *JobNode) (group *Group) {
group = &Group{
name: name,
path: path,
node: node,
watchPath: fmt.Sprintf(ClientPath, name),
clients: make(map[string]*Client),
lk: &sync.RWMutex{},
}
go group.watchClientPath()
go group.loopLoadAllClient()
return
}
// watch the client path
func (group *Group) watchClientPath() {
keyChangeEventResponse := group.node.etcd.WatchWithPrefixKey(group.watchPath)
group.watcher = keyChangeEventResponse.Watcher
group.cancelFunc = keyChangeEventResponse.CancelFunc
for ch := range keyChangeEventResponse.Event {
group.handleClientChangeEvent(ch)
}
}
// loop load all client
func (group *Group) loopLoadAllClient() {
RETRY:
var (
keys [][]byte
values [][]byte
err error
)
prefix := fmt.Sprintf(ClientPath, group.name)
if keys, values, err = group.node.etcd.GetWithPrefixKey(prefix); err != nil {
time.Sleep(time.Second)
goto RETRY
}
if len(keys) == 0 {
return
}
for i := 0; i < len(keys); i++ {
path := string(keys[i])
value := string(values[i])
if value == "" {
log.Warnf("the client value is nil for path:%s", path)
continue
}
group.addClient(value, path)
}
}
// handle the client change event
func (group *Group) handleClientChangeEvent(changeEvent *KeyChangeEvent) {
switch changeEvent.Type {
case KeyCreateChangeEvent:
path := changeEvent.Key
name := string(changeEvent.Value)
group.addClient(name, path)
case KeyUpdateChangeEvent:
//ignore
case KeyDeleteChangeEvent:
path := changeEvent.Key
group.deleteClient(path)
}
}
// add a new client
func (group *Group) addClient(name, path string) {
group.lk.Lock()
defer group.lk.Unlock()
if _, ok := group.clients[path]; ok {
log.Warnf("name:%s,path:%s,the client exist", name, path)
return
}
client := &Client{
name: name,
path: path,
}
group.clients[path] = client
log.Printf("add a new client for path:%s", path)
}
// delete a client for path
func (group *Group) deleteClient(path string) {
var (
client *Client
ok bool
)
group.lk.Lock()
defer group.lk.Unlock()
if client, ok = group.clients[path]; !ok {
log.Warnf("path:%s,the client not exist", path)
return
}
delete(group.clients, path)
log.Printf("delete a client for path:%s", path)
// fail over
if group.node.state == NodeLeaderState {
group.node.failOver.deleteClientEventChans <- &JobClientDeleteEvent{Group: group, Client: client}
}
}
func (group *Group) selectClient() (client *Client, err error) {
group.lk.RLock()
defer group.lk.RUnlock()
if len(group.clients) == 0 {
err = errors.New(fmt.Sprintf("the group:%s,has no client to select", group.name))
return
}
num := len(group.clients)
pos := rand.Intn(num)
index := 0
for _, c := range group.clients {
if index == pos {
client = c
return
}
index++
}
return
}
// client
type Client struct {
name string
path string
}