forked from dolthub/vitess
/
metaconn.go
283 lines (253 loc) · 7.57 KB
/
metaconn.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zk
import (
"math/rand"
"strings"
"time"
zookeeper "github.com/samuel/go-zookeeper/zk"
)
// Conn is really close to the zookeeper library connection interface.
// So refer to the zookeeper docs for the conventions
// used here (for instance, using -1 as version to specify any
// version)
type Conn interface {
Get(path string) (data []byte, stat *zookeeper.Stat, err error)
GetW(path string) (data []byte, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)
Children(path string) (children []string, stat *zookeeper.Stat, err error)
ChildrenW(path string) (children []string, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)
Exists(path string) (stat *zookeeper.Stat, err error)
ExistsW(path string) (stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)
Create(path string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error)
Set(path string, value []byte, version int32) (stat *zookeeper.Stat, err error)
Delete(path string, version int32) (err error)
Close() error
ACL(path string) ([]zookeeper.ACL, *zookeeper.Stat, error)
SetACL(path string, aclv []zookeeper.ACL, version int32) error
}
// Smooth API to talk to any zk path in the global system. Emulates
// "/zk/local" paths by guessing and substituting the correct cell for
// your current environment.
// MetaConn is an implementation of Conn that routes to multiple cells.
// It uses the <cell> in /zk/<cell>/... paths to decide which ZK cluster to
// send a given request to.
type MetaConn struct {
connCache *ConnCache
}
func resolveZkPath(path string) string {
cell, err := ZkCellFromZkPath(path)
if err != nil {
// ConnForPath was already called on path
panic(err) // should never happen
}
if cell != "local" {
return path
}
parts := strings.Split(path, "/")
parts[2] = GuessLocalCell()
return strings.Join(parts, "/")
}
const (
maxAttempts = 2
)
// Some errors are not gracefully handled by zookeeper client. This is
// sort of odd, but in general it doesn't affect the kind of code you
// need to have a truly reliable watcher.
//
// However, it can manifest itself as an annoying transient error that
// is likely avoidable when trying simple operations like Get.
// To that end, we retry when possible to minimize annoyance at
// higher levels.
//
// Metaconn is a better place to do this than the actual ZkConn glue
// because you are already doing an implicit connect per call.
//
// https://issues.apache.org/jira/browse/ZOOKEEPER-22
func shouldRetry(err error) bool {
if err == zookeeper.ErrConnectionClosed {
// This is slightly gross, but we should inject a bit of backoff
// here to give zk a chance to correct itself.
time.Sleep(1*time.Second + time.Duration(rand.Int63n(5e9)))
return true
}
return false
}
// Get implements Conn.
func (conn *MetaConn) Get(path string) (data []byte, stat *zookeeper.Stat, err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
data, stat, err = zconn.Get(resolveZkPath(path))
if !shouldRetry(err) {
return
}
}
return
}
// GetW implements Conn.
func (conn *MetaConn) GetW(path string) (data []byte, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error) {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return
}
return zconn.GetW(resolveZkPath(path))
}
// Children implements Conn.
func (conn *MetaConn) Children(path string) (children []string, stat *zookeeper.Stat, err error) {
if path == ("/" + MagicPrefix) {
// NOTE(msolo) There is a slight hack there - but there really is
// no valid stat for the top level path.
children, err = ZkKnownCells()
return
}
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
children, stat, err = zconn.Children(resolveZkPath(path))
if !shouldRetry(err) {
return
}
}
return
}
// ChildrenW implements Conn.
func (conn *MetaConn) ChildrenW(path string) (children []string, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error) {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return
}
return zconn.ChildrenW(resolveZkPath(path))
}
// Exists implements Conn.
func (conn *MetaConn) Exists(path string) (stat *zookeeper.Stat, err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
stat, err = zconn.Exists(resolveZkPath(path))
if !shouldRetry(err) {
return
}
}
return
}
// ExistsW implements Conn.
func (conn *MetaConn) ExistsW(path string) (stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error) {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return
}
return zconn.ExistsW(resolveZkPath(path))
}
// Create implements Conn.
func (conn *MetaConn) Create(path string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
path = resolveZkPath(path)
pathCreated, err = zconn.Create(path, value, flags, aclv)
if err == zookeeper.ErrNoNode {
parts := strings.Split(path, "/")
if len(parts) == 3 && parts[0] == "" && parts[1] == MagicPrefix {
// We were asked to create a /zk/<cell> path, but /zk doesn't exist.
// We should create /zk automatically in this case, because it's
// impossible to create /zk via MetaConn, since there's no cell name.
_, err = zconn.Create("/"+MagicPrefix, nil, 0, zookeeper.WorldACL(zookeeper.PermAll))
if err != nil {
if shouldRetry(err) {
continue
}
if err != zookeeper.ErrNodeExists {
return "", err
}
}
// Now try the original path again.
pathCreated, err = zconn.Create(path, value, flags, aclv)
}
}
if !shouldRetry(err) {
return
}
}
return
}
// Set implements Conn.
func (conn *MetaConn) Set(path string, value []byte, version int32) (stat *zookeeper.Stat, err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
stat, err = zconn.Set(resolveZkPath(path), value, version)
if !shouldRetry(err) {
return
}
}
return
}
// Delete implements Conn.
func (conn *MetaConn) Delete(path string, version int32) (err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
err = zconn.Delete(resolveZkPath(path), version)
if !shouldRetry(err) {
return
}
}
return
}
// Close implements Conn.
func (conn *MetaConn) Close() error {
return conn.connCache.Close()
}
// ACL implements Conn.
func (conn *MetaConn) ACL(path string) (acl []zookeeper.ACL, stat *zookeeper.Stat, err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
acl, stat, err = zconn.ACL(path)
if !shouldRetry(err) {
return
}
}
return
}
// SetACL implements Conn.
func (conn *MetaConn) SetACL(path string, aclv []zookeeper.ACL, version int32) (err error) {
var zconn Conn
for i := 0; i < maxAttempts; i++ {
zconn, err = conn.connCache.ConnForPath(path)
if err != nil {
return
}
err = zconn.SetACL(path, aclv, version)
if !shouldRetry(err) {
return
}
}
return
}
// NewMetaConn creates a MetaConn.
func NewMetaConn() *MetaConn {
return &MetaConn{NewConnCache()}
}