forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 13
/
metaconn.go
280 lines (245 loc) · 7.03 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
// 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"
"launchpad.net/gozk/zookeeper"
)
type Stat interface {
Czxid() int64
Mzxid() int64
CTime() time.Time
MTime() time.Time
Version() int
CVersion() int
AVersion() int
EphemeralOwner() int64
DataLength() int
NumChildren() int
Pzxid() int64
}
// This interface is really close to the zookeeper connection
// interface. It uses the Stat interface defined here instead of the
// zookeeper.Stat structure for stats. Everything else is the same as
// in zookeeper. 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 string, stat Stat, err error)
GetW(path string) (data string, stat Stat, watch <-chan zookeeper.Event, err error)
Children(path string) (children []string, stat Stat, err error)
ChildrenW(path string) (children []string, stat Stat, watch <-chan zookeeper.Event, err error)
Exists(path string) (stat Stat, err error)
ExistsW(path string) (stat Stat, watch <-chan zookeeper.Event, err error)
Create(path, value string, flags int, aclv []zookeeper.ACL) (pathCreated string, err error)
Set(path, value string, version int) (stat Stat, err error)
Delete(path string, version int) (err error)
Close() error
RetryChange(path string, flags int, acl []zookeeper.ACL, changeFunc ChangeFunc) error
ACL(path string) ([]zookeeper.ACL, Stat, error)
SetACL(path string, aclv []zookeeper.ACL, version int) error
}
type ChangeFunc func(oldValue string, oldStat Stat) (newValue string, err 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.
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 != nil && zookeeper.IsError(err, zookeeper.ZCONNECTIONLOSS) {
// 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
}
func (conn *MetaConn) Get(path string) (data string, stat 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
}
func (conn *MetaConn) GetW(path string) (data string, stat Stat, watch <-chan zookeeper.Event, err error) {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return
}
return zconn.GetW(resolveZkPath(path))
}
func (conn *MetaConn) Children(path string) (children []string, stat 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
}
func (conn *MetaConn) ChildrenW(path string) (children []string, stat Stat, watch <-chan zookeeper.Event, err error) {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return
}
return zconn.ChildrenW(resolveZkPath(path))
}
func (conn *MetaConn) Exists(path string) (stat 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
}
func (conn *MetaConn) ExistsW(path string) (stat Stat, watch <-chan zookeeper.Event, err error) {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return
}
return zconn.ExistsW(resolveZkPath(path))
}
func (conn *MetaConn) Create(path, value string, 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
}
pathCreated, err = zconn.Create(resolveZkPath(path), value, flags, aclv)
if !shouldRetry(err) {
return
}
}
return
}
func (conn *MetaConn) Set(path, value string, version int) (stat 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
}
func (conn *MetaConn) Delete(path string, version int) (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
}
func (conn *MetaConn) Close() error {
return conn.connCache.Close()
}
func (conn *MetaConn) RetryChange(path string, flags int, acl []zookeeper.ACL, changeFunc ChangeFunc) error {
zconn, err := conn.connCache.ConnForPath(path)
if err != nil {
return err
}
return zconn.RetryChange(resolveZkPath(path), flags, acl, changeFunc)
}
func (conn *MetaConn) ACL(path string) (acl []zookeeper.ACL, stat 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
}
func (conn *MetaConn) SetACL(path string, aclv []zookeeper.ACL, version int) (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
}
// Implements expvar.Var()
func (conn *MetaConn) String() string {
return conn.connCache.String()
}
func NewMetaConn() *MetaConn {
return &MetaConn{NewConnCache()}
}