-
Notifications
You must be signed in to change notification settings - Fork 21
/
zk.go
132 lines (111 loc) · 3.24 KB
/
zk.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
package zk
import (
"bytes"
"fmt"
"time"
"github.com/funkygao/gafka/zk"
log "github.com/funkygao/log4go"
zklib "github.com/samuel/go-zookeeper/zk"
)
type zkreg struct {
zkzone *zk.ZkZone
}
func New(zkzone *zk.ZkZone) *zkreg {
this := &zkreg{
zkzone: zkzone,
}
return this
}
func (this *zkreg) mypath(id string) string {
return fmt.Sprintf("%s/%s/%s", zk.KatewayIdsRoot, this.zkzone.Name(), id)
}
func (this *zkreg) Name() string {
return "zookeeper"
}
func (this *zkreg) Register(id string, data []byte) {
// https://issues.apache.org/jira/browse/ZOOKEEPER-1740
//
// might cause dead loop, but we accept it
loops := 0
backoffN := 0
path := this.mypath(id)
for {
loops++
if err := this.zkzone.CreateEphemeralZnode(path, data); err != nil {
if err == zklib.ErrNodeExists {
// An ephemeral node may still exist even after its corresponding session has expired
// due to a Zookeeper bug, in this case we need to retry writing until the previous node is deleted
// and hence the write succeeds without ZkNodeExistsException
storedData, _, e := this.zkzone.Conn().Get(path)
log.Trace("#%d get %s: %v", loops, path, e)
if e == nil {
if bytes.Equal(data, storedData) {
backoffN++
if backoffN == 3 {
log.Warn("backoff %d times, give up retrying, might been registered", backoffN-1)
return
}
log.Trace("#%d %s backoff %s awaiting previous node gone", loops, path, this.zkzone.SessionTimeout())
time.Sleep(this.zkzone.SessionTimeout())
} else {
log.Error("conflict[%s] found, give up retry registering. expected: %s, got %s",
id, string(data), string(storedData))
return
}
} else {
log.Error("#%d %s get data: %v", loops, path, e)
continue
}
} else {
// retry
log.Error("#%d %s: %v", loops, path, err)
continue
}
} else {
// didn't encounter zk bug, happy ending
log.Trace("#%d %s created", loops, path)
return
}
}
}
func (this *zkreg) createEphemeralPathExpectConflict(path string, data []byte) error {
err := this.zkzone.CreateEphemeralZnode(path, data)
if err == nil {
return nil
}
log.Warn("%s %v", path, err)
if err == zklib.ErrNodeExists {
_, _, e := this.zkzone.Conn().Get(path)
log.Debug("%s %v", path, e)
if e == zklib.ErrNoNode {
// the node disappeared; treat as if node exists
return err
} else if e != nil {
return e
}
}
return err
}
func (this *zkreg) Deregister(id string, oldData []byte) error {
data, _, err := this.zkzone.Conn().Get(this.mypath(id))
if err != nil {
return fmt.Errorf("%s %v", this.mypath(id), err)
}
// ensure I own this znode
if !bytes.Equal(data, oldData) {
return fmt.Errorf("registry[%s] exp %s, got %s", id, string(oldData), string(data))
}
return this.zkzone.Conn().Delete(this.mypath(id), -1)
}
func (this *zkreg) WatchInstances() ([]string, <-chan zklib.Event, error) {
path := fmt.Sprintf("%s/%s", zk.KatewayIdsRoot, this.zkzone.Name())
ids, _, ch, err := this.zkzone.Conn().ChildrenW(path)
if err != nil {
return nil, nil, fmt.Errorf("%s %v", path, err)
}
instancePaths := make([]string, 0, len(ids))
for _, id := range ids {
instancePaths = append(instancePaths, fmt.Sprintf("%s/%s", path, id))
}
return instancePaths, ch, nil
}