/
registry.go
490 lines (437 loc) · 10.7 KB
/
registry.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package zk
import (
"errors"
"github.com/golang/glog"
"github.com/qorio/maestro/pkg/registry"
"github.com/samuel/go-zookeeper/zk"
"time"
)
var (
ErrNotInitialized = errors.New("not-initialized")
ErrNotWatching = errors.New("not-watching")
ErrInvalidState = errors.New("invalid-state")
ErrTimeout = errors.New("timeout")
)
type watch interface {
String() string
SetTimeout(time.Duration) error
Apply(func(k registry.Key, before, after *Node) bool) error
SetGroupChan(chan<- watch)
Wait() error
}
// Implements some utilities for the registry types
type base struct {
zk ZK
stop chan<- bool
timeout time.Duration
timer *time.Timer
before *Node
after *Node
done chan error
group chan<- watch // for sending to the group
error error
persistent bool
}
type Conditions struct {
registry.Conditions
watches map[watch]bool
group chan watch
timer *time.Timer
persistent bool
}
func (this *Conditions) Pending() []watch {
pending := []watch{}
for k, _ := range this.watches {
pending = append(pending, k)
}
return pending
}
type Delete struct {
base
registry.Delete
}
type Create struct {
base
registry.Create
}
type Change struct {
base
registry.Change
}
type Members struct {
base
registry.Members
}
func (this *base) Init(zkc ZK) {
this.zk = zkc
this.done = make(chan error)
}
func (this *base) wait() error {
for {
return <-this.done
}
}
func (this *Conditions) Init(zkc ZK) *Conditions {
if this.watches == nil {
this.watches = map[watch]bool{}
}
if this.Delete != nil {
w := NewDelete(*this.Delete, zkc, this.persistent)
w.Apply(func(k registry.Key, before, after *Node) bool {
glog.V(100).Infoln("Delete:", k.Path(), "Before=", before, "After=", after)
return true
})
this.watches[w] = false
}
if this.Create != nil {
w := NewCreate(*this.Create, zkc, this.persistent)
w.Apply(func(k registry.Key, before, after *Node) bool {
glog.V(100).Infoln("Create:", k.Path(), "Before=", before, "After=", after)
return true
})
this.watches[w] = false
}
if this.Change != nil {
w := NewChange(*this.Change, zkc, this.persistent)
w.Apply(func(k registry.Key, before, after *Node) bool {
glog.V(100).Infoln("Change:", k.Path(), "Before=", before, "After=", after)
return true
})
this.watches[w] = false
}
if this.Members != nil {
w := NewMembers(*this.Members, zkc, this.persistent)
w.Apply(func(k registry.Key, before, after *Node) bool {
glog.V(100).Infoln("Members:", k.Path(), "Before=", before.Stats.NumChildren, "After=", after.Stats.NumChildren)
met := false
switch {
case this.Members.Max != nil && this.Members.Min != nil:
if this.Members.OutsideRange {
met = after.Stats.NumChildren < *this.Members.Min || after.Stats.NumChildren >= *this.Members.Max
} else {
met = after.Stats.NumChildren >= *this.Members.Min && after.Stats.NumChildren < *this.Members.Max
}
case this.Members.Max != nil:
if this.Members.OutsideRange {
met = after.Stats.NumChildren >= *this.Members.Max
} else {
met = after.Stats.NumChildren < *this.Members.Max
}
case this.Members.Min != nil:
if this.Members.OutsideRange {
met = after.Stats.NumChildren < *this.Members.Min
} else {
met = after.Stats.NumChildren >= *this.Members.Min
}
case this.Members.Delta != nil:
met = after.Stats.NumChildren-before.Stats.NumChildren == *this.Members.Delta
case this.Members.Equals != nil:
met = after.Stats.NumChildren == *this.Members.Equals
}
return met
})
this.watches[w] = false
}
this.timer = time.NewTimer(1 * time.Second)
this.timer.Stop()
if this.Timeout != nil {
this.timer.Reset(time.Duration(*this.Timeout))
}
// for group synchronization
this.group = make(chan watch)
for w, _ := range this.watches {
w.SetGroupChan(this.group)
}
return this
}
// Simply blocks until it's either true or a timeout occurs.
// The error will indicate whether the condition is met or a timeout took place.
func (this *Conditions) Wait() error {
for {
select {
case w := <-this.group:
if _, has := this.watches[w]; has {
delete(this.watches, w)
} else {
panic(ErrInvalidState)
}
if !this.All || len(this.watches) == 0 {
return nil
}
case <-this.timer.C:
return ErrTimeout
}
}
}
func (this *Create) Wait() error {
return this.base.wait()
}
func (this *Delete) Wait() error {
return this.base.wait()
}
func (this *Change) Wait() error {
return this.base.wait()
}
func (this *Members) Wait() error {
return this.base.wait()
}
func (this *Create) String() string {
return this.Create.Path()
}
func (this *Delete) String() string {
return this.Delete.Path()
}
func (this *Change) String() string {
return this.Change.Path()
}
func (this *Members) String() string {
return this.Members.Path()
}
func is_true(b ...bool) bool {
return len(b) > 0 && b[0]
}
func NewConditions(c registry.Conditions, zkc ZK, persistent ...bool) *Conditions {
conditions := &Conditions{Conditions: c, persistent: is_true(persistent...)}
conditions.Init(zkc)
return conditions
}
func NewCreate(c registry.Create, zkc ZK, persistent ...bool) watch {
create := &Create{Create: c}
create.base.persistent = is_true(persistent...)
create.base.Init(zkc)
return create
}
func NewDelete(d registry.Delete, zkc ZK, persistent ...bool) watch {
delete := &Delete{Delete: d}
delete.base.persistent = is_true(persistent...)
delete.base.Init(zkc)
return delete
}
func NewChange(c registry.Change, zkc ZK, persistent ...bool) watch {
change := &Change{Change: c}
change.base.persistent = is_true(persistent...)
change.base.Init(zkc)
return change
}
func NewMembers(m registry.Members, zkc ZK, persistent ...bool) watch {
members := &Members{Members: m}
members.base.persistent = is_true(persistent...)
members.base.Init(zkc)
return members
}
func (this *Delete) SetGroupChan(c chan<- watch) {
this.base.group = c
}
func (this *Create) SetGroupChan(c chan<- watch) {
this.base.group = c
}
func (this *Change) SetGroupChan(c chan<- watch) {
this.base.group = c
}
func (this *Members) SetGroupChan(c chan<- watch) {
this.base.group = c
}
func (this *base) SetTimeout(t time.Duration) error {
if this.zk == nil {
return ErrNotInitialized
}
this.timeout = t
if this.timer == nil {
this.timer = time.AfterFunc(this.timeout, func() {
this.cancel() // when timer fires
})
if this.stop != nil {
this.timer.Reset(this.timeout) // reset as we are already watching
} else {
this.timer.Stop() // dont start until we are watching
}
}
return nil
}
func (this *base) notify(w watch) {
if this.group != nil {
this.group <- w
}
}
func (this *base) cancel() error {
if this.zk == nil {
return ErrNotInitialized
}
if this.stop == nil {
return ErrNotWatching
}
this.stop <- true
return nil
}
// This does not require the node at path to exist
func (this *base) watch(path registry.Key, handler func(Event)) (node *Node, err error) {
if this.zk == nil {
return nil, ErrNotInitialized
}
// Get the node
node, err = this.zk.Get(path.Path())
switch {
case err == nil:
switch path.(type) {
// Specical case. Node exists but watching for create
case registry.Create:
this.before = nil
return nil, ErrInvalidState
case registry.Delete, registry.Change:
this.stop, err = node.Watch(handler)
if err != nil {
return
}
this.before = node
case registry.Members:
this.stop, err = node.WatchChildren(handler)
if err != nil {
return
}
this.before = node
}
if this.timer != nil {
this.timer.Reset(this.timeout)
}
case err == ErrNotExist:
switch path.(type) {
// Specical case. Node does not exist but watching for delete
case registry.Delete:
return nil, ErrInvalidState
case registry.Create, registry.Change:
this.stop, err = this.zk.Watch(path.Path(), handler)
if err != nil {
return
}
case registry.Members:
this.stop, err = this.zk.WatchChildren(path.Path(), handler)
if err != nil {
return
}
}
if this.timer != nil {
this.timer.Reset(this.timeout)
}
default:
return
}
return
}
func (this *Delete) Apply(handler func(k registry.Key, before, after *Node) bool) error {
if n, err := this.base.watch(this.Delete,
func(e Event) {
switch e.Type {
case zk.EventNodeDeleted:
met := handler(*this, this.before, nil)
if met {
this.base.notify(this)
if this.base.persistent {
this.Apply(handler)
} else {
this.done <- nil
}
} else {
this.Apply(handler)
}
}
}); err != nil {
return err
} else {
this.before = n
return nil
}
}
func (this *Create) Apply(handler func(k registry.Key, before, after *Node) bool) error {
if _, err := this.base.watch(this.Create,
func(e Event) {
switch e.Type {
case zk.EventNodeCreated:
after, err := this.zk.Get(this.Create.Path())
if err != nil {
this.error = err
this.done <- err
}
met := handler(*this, nil, after)
this.before = after
if met {
this.base.notify(this)
if this.base.persistent {
this.Apply(handler)
} else {
this.done <- nil
}
} else {
this.Apply(handler)
}
}
}); err != nil {
return err
} else {
return nil
}
}
// Change notification occurs when node is CREATED or when the value is CHANGED
func (this *Change) Apply(handler func(k registry.Key, before, after *Node) bool) error {
if n, err := this.base.watch(this.Change,
func(e Event) {
switch e.Type {
// Created nil->!nil or change in value
case zk.EventNodeDataChanged, zk.EventNodeCreated:
after, err := this.zk.Get(this.Path())
if err != nil {
this.error = err
this.done <- err
}
met := handler(*this, this.before, after)
this.before = this.after
if met {
this.base.notify(this)
if this.base.persistent {
this.Apply(handler)
} else {
this.done <- nil
}
} else {
this.Apply(handler)
}
}
}); err != nil {
return err
} else {
this.before = n
return nil
}
}
func (this *Members) Apply(handler func(k registry.Key, before, after *Node) bool) error {
if n, err := this.base.watch(this.Members,
func(e Event) {
glog.Infoln(">>>>>EVENT=", e)
switch e.Type {
case zk.EventNodeChildrenChanged:
after, err := this.zk.Get(this.Top.Path())
if err != nil {
this.error = err
this.done <- err
}
met := handler(*this, this.before, after)
this.before = after
if met {
this.base.notify(this)
if this.base.persistent {
// Need to register to receive the event again.
this.Apply(handler)
} else {
this.done <- nil
}
} else {
// Need to register to receive the event again.
this.Apply(handler)
}
}
}); err != nil {
return err
} else {
this.before = n
return nil
}
}