-
Notifications
You must be signed in to change notification settings - Fork 32
/
zookeeper.go
218 lines (183 loc) · 5.84 KB
/
zookeeper.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
// Copyright 2020 Netflix Inc
// Author: Colin McIntosh (colin@netflix.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package clustering
import (
"fmt"
"strings"
"time"
"github.com/go-zookeeper/zk"
"github.com/openconfig/gnmi-gateway/gateway/configuration"
)
const ClusterMemberPath = "/members"
const DefaultACLPerms = zk.PermAll
type ZookeeperClusterMember struct {
acl []zk.ACL
config *configuration.GatewayConfig
conn *zk.Conn
callbackCancel func()
member MemberID
}
func NewZookeeperClusterMember(config *configuration.GatewayConfig, conn *zk.Conn, member string) *ZookeeperClusterMember {
return &ZookeeperClusterMember{
acl: zk.WorldACL(DefaultACLPerms),
config: config,
conn: conn,
member: MemberID(member),
}
}
func (z *ZookeeperClusterMember) MemberID() MemberID {
return z.member
}
// Register will create an ephemeral Zookeeper node in a cluster member tree. The node
// will be removed automatically by Zookeeper if the session disconnects.
func (z *ZookeeperClusterMember) Register() error {
registrationPath := CleanPath(z.config.ZookeeperPrefix) + CleanPath(ClusterMemberPath) + CleanPath(string(z.member))
_, err := z.conn.Create(registrationPath, []byte{}, zk.FlagEphemeral, z.acl)
if err == zk.ErrNoNode {
// Create parent node.
err = CreateParentPath(z.conn, registrationPath, z.acl)
if err != nil {
return fmt.Errorf("unable to create parent path for cluster member registration: %v", err)
}
// Re attempt to add a sequence now that the parent exists
_, err = z.conn.Create(registrationPath, []byte{}, zk.FlagEphemeral, z.acl)
if err != nil {
return fmt.Errorf("unable to create registration node after parent path created: %v", err)
}
} else if err != nil {
return fmt.Errorf("unable to register cluster member with Zookeeper: %v", err)
}
return nil
}
func (z *ZookeeperClusterMember) MemberListCallback(callback MemberListCallbackFunc) error {
searchPath := CleanPath(z.config.ZookeeperPrefix) + CleanPath(ClusterMemberPath)
if z.callbackCancel != nil {
z.callbackCancel()
}
var stopped bool
go func() {
// TODO (cmcintosh): This whole goroutine has awful error handling and needs to be rewritten.
var previousMembers []MemberID
var consecutiveErrors = 0
for !stopped {
if consecutiveErrors > 3 {
panic(fmt.Errorf("too many consecutive errors trying to watch the cluster member tree"))
}
children, _, memberListChanges, err := z.conn.ChildrenW(searchPath)
if err == zk.ErrNoNode {
createErr := CreatePath(z.conn, searchPath, z.acl)
if createErr != nil {
consecutiveErrors++
z.config.Log.Error().Msgf("error #%d while trying to create path for cluster member tree: %v", consecutiveErrors, err)
continue
}
}
if err != nil {
consecutiveErrors++
z.config.Log.Error().Msgf("error #%d while trying to set Zookeeper watch on cluster member tree: %v", consecutiveErrors, err)
// Sleep to avoid retrying too quickly
// 2*ZookeeperTimeout should be plenty of time if the underlying connection is the issue
time.Sleep(2 * z.config.ZookeeperTimeout)
continue
}
consecutiveErrors = 0
var currentMembers []MemberID
for _, m := range children {
currentMember := MemberID(m)
if currentMember == z.member {
continue
}
if !memberIDInSlice(currentMember, previousMembers) {
callback(currentMember, "") // add
}
currentMembers = append(currentMembers, currentMember)
}
for _, previousMember := range previousMembers {
if previousMember == z.member {
continue
}
if !memberIDInSlice(previousMember, currentMembers) {
callback("", previousMember) // remove
}
}
previousMembers = currentMembers
<-memberListChanges
}
}()
z.callbackCancel = func() {
stopped = true
}
return nil
}
func (z *ZookeeperClusterMember) MemberList() ([]MemberID, error) {
var clusterMembers []MemberID
searchPath := CleanPath(z.config.ZookeeperPrefix) + CleanPath(ClusterMemberPath)
children, _, err := z.conn.Children(searchPath)
if err != nil {
return nil, fmt.Errorf("error while trying to list on cluster members: %v", err)
}
for _, m := range children {
member := MemberID(m)
if member != "" && member != z.member {
clusterMembers = append(clusterMembers, member)
}
}
return clusterMembers, nil
}
func (z *ZookeeperClusterMember) Unregister() error {
// TODO (cmcintosh): implement ZK unregister method.
panic("implement me!")
}
func CreateParentPath(conn *zk.Conn, path string, acl []zk.ACL) error {
parts := strings.Split(path, "/")
return createPath(conn, parts[:len(parts)-1], acl)
}
func CreatePath(conn *zk.Conn, path string, acl []zk.ACL) error {
parts := strings.Split(path, "/")
return createPath(conn, parts, acl)
}
func createPath(conn *zk.Conn, parts []string, acl []zk.ACL) error {
pth := ""
for _, p := range parts {
if p == "" {
continue
}
var exists bool
pth += "/" + p
exists, _, err := conn.Exists(pth)
if err != nil {
return err
}
if exists == true {
continue
}
_, err = conn.Create(pth, []byte{}, 0, acl)
if err != nil && err != zk.ErrNodeExists {
return err
}
}
return nil
}
func CleanPath(path string) string {
return "/" + strings.Trim(path, "/")
}
func memberIDInSlice(s MemberID, list []MemberID) bool {
for _, v := range list {
if v == s {
return true
}
}
return false
}