forked from vitessio/vitess
/
conn.go
369 lines (318 loc) · 10.9 KB
/
conn.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
// 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 zk2topo
import (
"flag"
"fmt"
"math/rand"
"strings"
"sync"
"time"
log "github.com/golang/glog"
"github.com/samuel/go-zookeeper/zk"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/netutil"
"github.com/youtube/vitess/go/sync2"
)
const (
// maxAttempts is how many times we retry queries. At 2 for
// now, so if a query fails because the session expired, we
// just try to reconnect once and go on.
maxAttempts = 2
// PermDirectory are default permissions for a node.
PermDirectory = zk.PermAdmin | zk.PermCreate | zk.PermDelete | zk.PermRead | zk.PermWrite
// PermFile allows a zk node to emulate file behavior by
// disallowing child nodes.
PermFile = zk.PermAdmin | zk.PermRead | zk.PermWrite
)
var (
maxConcurrency = flag.Int("topo_zk_max_concurrency", 64, "maximum number of pending requests to send to a Zookeeper server.")
baseTimeout = flag.Duration("topo_zk_base_timeout", 30*time.Second, "zk base timeout (see zk.Connect)")
)
// 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(ctx context.Context, path string) (data []byte, stat *zk.Stat, err error)
GetW(ctx context.Context, path string) (data []byte, stat *zk.Stat, watch <-chan zk.Event, err error)
Children(ctx context.Context, path string) (children []string, stat *zk.Stat, err error)
ChildrenW(ctx context.Context, path string) (children []string, stat *zk.Stat, watch <-chan zk.Event, err error)
Exists(ctx context.Context, path string) (exists bool, stat *zk.Stat, err error)
ExistsW(ctx context.Context, path string) (exists bool, stat *zk.Stat, watch <-chan zk.Event, err error)
Create(ctx context.Context, path string, value []byte, flags int32, aclv []zk.ACL) (pathCreated string, err error)
Set(ctx context.Context, path string, value []byte, version int32) (stat *zk.Stat, err error)
Delete(ctx context.Context, path string, version int32) (err error)
GetACL(ctx context.Context, path string) ([]zk.ACL, *zk.Stat, error)
SetACL(ctx context.Context, path string, aclv []zk.ACL, version int32) error
Close() error
}
// Time returns a time.Time from a ZK int64 milliseconds since Epoch time.
func Time(i int64) time.Time {
return time.Unix(i/1000, i%1000*1000000)
}
// ZkTime returns a ZK time (int64) from a time.Time
func ZkTime(t time.Time) int64 {
return t.Unix()*1000 + int64(t.Nanosecond()/1000000)
}
// Connect returns a Conn connecting to a real Zookeeper server.
func Connect(addr string) Conn {
return newRealConn(addr)
}
// connImpl is a class that implements the Conn interface using a zk.Conn.
// It will do a few things for us:
// - add the context parameter. However, we do not enforce its deadlines
// necessarily.
// - enforce a max concurrency of access to Zookeeper. We just don't
// want to make too many calls concurrently, to not take too many resources.
// - retry some calls to Zookeeper. If we were disconnected from the
// server, we want to try connecting again before failing.
type connImpl struct {
// addr is set at construction time, and immutable.
addr string
// sem protects concurrent calls to Zookeeper.
sem *sync2.Semaphore
// mu protects the following fields.
mu sync.Mutex
conn *zk.Conn
}
func newRealConn(addr string) *connImpl {
return &connImpl{
addr: addr,
sem: sync2.NewSemaphore(*maxConcurrency, 0),
}
}
// Get is part of the Conn interface.
func (c *connImpl) Get(ctx context.Context, path string) (data []byte, stat *zk.Stat, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
data, stat, err = conn.Get(path)
return err
})
return
}
// GetW is part of the Conn interface.
func (c *connImpl) GetW(ctx context.Context, path string) (data []byte, stat *zk.Stat, watch <-chan zk.Event, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
data, stat, watch, err = conn.GetW(path)
return err
})
return
}
// Children is part of the Conn interface.
func (c *connImpl) Children(ctx context.Context, path string) (children []string, stat *zk.Stat, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
children, stat, err = conn.Children(path)
return err
})
return
}
// ChildrenW is part of the Conn interface.
func (c *connImpl) ChildrenW(ctx context.Context, path string) (children []string, stat *zk.Stat, watch <-chan zk.Event, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
children, stat, watch, err = conn.ChildrenW(path)
return err
})
return
}
// Exists is part of the Conn interface.
func (c *connImpl) Exists(ctx context.Context, path string) (exists bool, stat *zk.Stat, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
exists, stat, err = conn.Exists(path)
return err
})
return
}
// ExistsW is part of the Conn interface.
func (c *connImpl) ExistsW(ctx context.Context, path string) (exists bool, stat *zk.Stat, watch <-chan zk.Event, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
exists, stat, watch, err = conn.ExistsW(path)
return err
})
return
}
// Create is part of the Conn interface.
func (c *connImpl) Create(ctx context.Context, path string, value []byte, flags int32, aclv []zk.ACL) (pathCreated string, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
pathCreated, err = conn.Create(path, value, flags, aclv)
return err
})
return
}
// Set is part of the Conn interface.
func (c *connImpl) Set(ctx context.Context, path string, value []byte, version int32) (stat *zk.Stat, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
stat, err = conn.Set(path, value, version)
return err
})
return
}
// Delete is part of the Conn interface.
func (c *connImpl) Delete(ctx context.Context, path string, version int32) error {
return c.withRetry(ctx, func(conn *zk.Conn) error {
return conn.Delete(path, version)
})
}
// GetACL is part of the Conn interface.
func (c *connImpl) GetACL(ctx context.Context, path string) (aclv []zk.ACL, stat *zk.Stat, err error) {
err = c.withRetry(ctx, func(conn *zk.Conn) error {
aclv, stat, err = conn.GetACL(path)
return err
})
return
}
// SetACL is part of the Conn interface.
func (c *connImpl) SetACL(ctx context.Context, path string, aclv []zk.ACL, version int32) error {
return c.withRetry(ctx, func(conn *zk.Conn) error {
_, err := conn.SetACL(path, aclv, version)
return err
})
}
// Close is part of the Conn interface.
func (c *connImpl) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
c.conn.Close()
}
return nil
}
// withRetry encapsulates the retry logic and concurrent access to
// Zookeeper.
//
// Some errors are not handled gracefully by the 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 client.
//
// 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.
//
// https://issues.apache.org/jira/browse/ZOOKEEPER-22
func (c *connImpl) withRetry(ctx context.Context, action func(conn *zk.Conn) error) (err error) {
// Handle concurrent access to a Zookeeper server here.
c.sem.Acquire()
defer c.sem.Release()
for i := 0; i < maxAttempts; i++ {
if i > 0 {
// Add a bit of backoff time before retrying:
// 1 second base + up to 5 seconds.
time.Sleep(1*time.Second + time.Duration(rand.Int63n(5e9)))
}
// Get the current connection, or connect.
var conn *zk.Conn
conn, err = c.getConn(ctx)
if err != nil {
// We can't connect, try again.
continue
}
// Execute the action.
err = action(conn)
if err != zk.ErrConnectionClosed {
// It worked, or it failed for another reason
// than connection related.
return
}
// We got an error, because the connection was closed.
// Let's clear up our errored connection and try again.
c.mu.Lock()
if c.conn == conn {
c.conn = nil
}
c.mu.Unlock()
}
return
}
// getConn returns the connection in a thread safe way. It will try to connect
// if not connected yet.
func (c *connImpl) getConn(ctx context.Context) (*zk.Conn, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn == nil {
conn, events, err := dialZk(ctx, c.addr)
if err != nil {
return nil, err
}
c.conn = conn
go c.handleSessionEvents(conn, events)
}
return c.conn, nil
}
// handleSessionEvents is processing events from the session channel.
// When it detects that the connection is not working any more, it
// clears out the connection record.
func (c *connImpl) handleSessionEvents(conn *zk.Conn, session <-chan zk.Event) {
for event := range session {
closeRequired := false
switch event.State {
case zk.StateExpired, zk.StateConnecting:
closeRequired = true
fallthrough
case zk.StateDisconnected:
c.mu.Lock()
if c.conn == conn {
// The connImpl still references this
// connection, let's nil it.
c.conn = nil
}
c.mu.Unlock()
if closeRequired {
conn.Close()
}
log.Infof("zk conn: session for addr %v ended: %v", c.addr, event)
return
}
log.Infof("zk conn: session for addr %v event: %v", c.addr, event)
}
}
// dialZk dials the server, and waits until connection.
func dialZk(ctx context.Context, addr string) (*zk.Conn, <-chan zk.Event, error) {
servers, err := resolveZkAddr(addr)
if err != nil {
return nil, nil, err
}
zconn, session, err := zk.Connect(servers, *baseTimeout)
if err != nil {
return nil, nil, err
}
// Wait for connection, skipping transition states.
for {
select {
case <-ctx.Done():
zconn.Close()
return nil, nil, ctx.Err()
case event := <-session:
switch event.State {
case zk.StateConnected:
// success
return zconn, session, nil
case zk.StateAuthFailed:
// fast fail this one
zconn.Close()
return nil, nil, fmt.Errorf("zk connect failed: StateAuthFailed")
}
}
}
}
// resolveZkAddr takes a comma-separated list of host:port addresses,
// and resolves the host to replace it with the IP address.
// If a resolution fails, the host is skipped.
// If no host can be resolved, an error is returned.
// This is different from the Zookeeper library, that insists on resolving
// *all* hosts successfully before it starts.
func resolveZkAddr(zkAddr string) ([]string, error) {
parts := strings.Split(zkAddr, ",")
resolved := make([]string, 0, len(parts))
for _, part := range parts {
// The Zookeeper client cannot handle IPv6 addresses before version 3.4.x.
if r, err := netutil.ResolveIPv4Addr(part); err != nil {
log.Warningf("cannot resolve %v, will not use it: %v", part, err)
} else {
resolved = append(resolved, r)
}
}
if len(resolved) == 0 {
return nil, fmt.Errorf("no valid address found in %v", zkAddr)
}
return resolved, nil
}