forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zkconn.go
374 lines (329 loc) · 9.89 KB
/
zkconn.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
370
371
372
373
374
// 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 (
"errors"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
log "github.com/golang/glog"
zookeeper "github.com/samuel/go-zookeeper/zk"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/netutil"
"github.com/youtube/vitess/go/sync2"
)
// 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)
}
// Every blocking call into CGO causes another thread which blows up
// the virtual memory. It seems better to solve this here at the
// root of the problem rather than forcing all other apps to take into
// account limiting the number of concurrent operations on a zk
// connection. Since this applies to any zookeeper connection, this
// is global.
var sem *sync2.Semaphore
// ErrConnectionClosed is returned if we try to access a closed connection.
var ErrConnectionClosed = errors.New("ZkConn: connection is closed")
func init() {
// The zookeeper C module logs quite a bit of useful information,
// but much of it does not come back in the error API. To aid
// debugging, enable the log to stderr for warnings.
//zookeeper.SetLogLevel(zookeeper.LOG_WARN)
maxConcurrency := 64
x := os.Getenv("ZK_CLIENT_MAX_CONCURRENCY")
if x != "" {
var err error
maxConcurrency, err = strconv.Atoi(x)
if err != nil {
log.Infof("invalid ZK_CLIENT_MAX_CONCURRENCY: %v", err)
}
}
sem = sync2.NewSemaphore(maxConcurrency, 0)
}
// ZkConn is a client class that implements zk.Conn using a zookeeper.Conn.
// The conn member variable is protected by the mutex.
type ZkConn struct {
mu sync.Mutex
conn *zookeeper.Conn
}
// getConn returns the connection in a thread safe way
func (conn *ZkConn) getConn() *zookeeper.Conn {
conn.mu.Lock()
defer conn.mu.Unlock()
return conn.conn
}
// DialZk dials a ZK server and waits for connection event. Returns a ZkConn
// encapsulating the zookeeper.Conn, and the zookeeper session event
// channel to monitor the connection
//
// The value for baseTimeout is used as a session timeout as well, and
// will be used to negotiate a 'good' value with the server. From
// reading the zookeeper source code, it has to be between 6 and 60
// seconds (2x and 20x the tickTime by default, with default tick time
// being 3 seconds). min session time, max session time and ticktime
// can all be overwritten on the zookeeper server side, so these
// numbers may vary.
//
// Then this baseTimeout is used to compute other related timeouts:
// - connect timeout is 1/3 of baseTimeout
// - recv timeout is 2/3 of baseTimeout minus a ping time
// - send timeout is 1/3 of baseTimeout
// - we try to send a ping a least every baseTimeout / 3
//
// Note the baseTimeout has *nothing* to do with the time between we
// call Dial and the maximum time before we receive the event on the
// session. The library will actually try to re-connect in the background
// (after each timeout), and may *never* send an event if the TCP connections
// always fail. Use DialZkTimeout to enforce a timeout for the initial connect.
func DialZk(zkAddr string, baseTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error) {
servers, err := resolveZkAddr(zkAddr)
if err != nil {
return nil, nil, err
}
sem.Acquire()
defer sem.Release()
zconn, session, err := zookeeper.Connect(servers, baseTimeout)
if err != nil {
return nil, nil, err
}
// Wait for connection, possibly forever, skipping transition states
for {
event := <-session
switch event.State {
case zookeeper.StateConnected:
// success
return &ZkConn{conn: zconn}, session, nil
case zookeeper.StateAuthFailed:
// fast fail this one
zconn.Close()
return nil, nil, fmt.Errorf("zk connect failed: StateAuthFailed")
}
}
}
// DialZkTimeout dial the server, and wait up to timeout until connection
func DialZkTimeout(zkAddr string, baseTimeout time.Duration, connectTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error) {
servers, err := resolveZkAddr(zkAddr)
if err != nil {
return nil, nil, err
}
sem.Acquire()
defer sem.Release()
zconn, session, err := zookeeper.Connect(servers, baseTimeout)
if err != nil {
return nil, nil, err
}
// Wait for connection, with a timeout, skipping transition states
timer := time.NewTimer(connectTimeout)
for {
select {
case <-timer.C:
zconn.Close()
return nil, nil, context.DeadlineExceeded
case event := <-session:
switch event.State {
case zookeeper.StateConnected:
// success
return &ZkConn{conn: zconn}, session, nil
case zookeeper.StateAuthFailed:
// fast fail this one
zconn.Close()
return nil, nil, fmt.Errorf("zk connect failed: StateAuthFailed")
}
}
}
}
// resolveZkAddr takes a comma-separated list of host:post 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.Infof("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
}
// Get is part of the ZkConn interface.
func (conn *ZkConn) Get(path string) ([]byte, *zookeeper.Stat, error) {
c := conn.getConn()
if c == nil {
return nil, nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
data, stat, err := c.Get(path)
if err != nil {
return nil, nil, err
}
return data, stat, nil
}
// GetW is part of the ZkConn interface.
func (conn *ZkConn) GetW(path string) ([]byte, *zookeeper.Stat, <-chan zookeeper.Event, error) {
c := conn.getConn()
if c == nil {
return nil, nil, nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
data, stat, watch, err := c.GetW(path)
if err != nil {
return nil, nil, nil, err
}
return data, stat, watch, nil
}
// Children is part of the ZkConn interface.
func (conn *ZkConn) Children(path string) ([]string, *zookeeper.Stat, error) {
c := conn.getConn()
if c == nil {
return nil, nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
children, stat, err := c.Children(path)
if err != nil {
return nil, nil, err
}
return children, stat, nil
}
// ChildrenW is part of the ZkConn interface.
func (conn *ZkConn) ChildrenW(path string) ([]string, *zookeeper.Stat, <-chan zookeeper.Event, error) {
c := conn.getConn()
if c == nil {
return nil, nil, nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
children, stat, watch, err := c.ChildrenW(path)
if err != nil {
return nil, nil, nil, err
}
return children, stat, watch, nil
}
// Exists is part of the ZkConn interface.
func (conn *ZkConn) Exists(path string) (*zookeeper.Stat, error) {
c := conn.getConn()
if c == nil {
return nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
exists, stat, err := c.Exists(path)
if err != nil {
return nil, err
}
if !exists {
return nil, zookeeper.ErrNoNode
}
return stat, nil
}
// ExistsW is part of the ZkConn interface.
func (conn *ZkConn) ExistsW(path string) (*zookeeper.Stat, <-chan zookeeper.Event, error) {
c := conn.getConn()
if c == nil {
return nil, nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
exists, stat, watch, err := c.ExistsW(path)
if err != nil {
return nil, nil, err
}
if !exists {
return nil, watch, nil
}
return stat, watch, nil
}
// Create is part of the ZkConn interface.
func (conn *ZkConn) Create(path string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error) {
c := conn.getConn()
if c == nil {
return "", ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
return c.Create(path, []byte(value), int32(flags), aclv)
}
// Set is part of the ZkConn interface.
func (conn *ZkConn) Set(path string, value []byte, version int32) (*zookeeper.Stat, error) {
c := conn.getConn()
if c == nil {
return nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
stat, err := c.Set(path, []byte(value), version)
if err != nil {
return nil, err
}
return stat, nil
}
// Delete is part of the ZkConn interface.
func (conn *ZkConn) Delete(path string, version int32) (err error) {
c := conn.getConn()
if c == nil {
return ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
return c.Delete(path, version)
}
// Close will close the connection asynchronously. It will never
// fail, even though closing the connection might fail in the
// background. Accessing this ZkConn after Close has been called will
// return ErrConnectionClosed.
func (conn *ZkConn) Close() error {
conn.mu.Lock()
defer conn.mu.Unlock()
if conn.conn == nil {
return nil
}
c := conn.conn
conn.conn = nil
go c.Close()
return nil
}
// ACL is part of the ZkConn interface.
func (conn *ZkConn) ACL(path string) ([]zookeeper.ACL, *zookeeper.Stat, error) {
c := conn.getConn()
if c == nil {
return nil, nil, ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
acls, stat, err := c.GetACL(path)
if err != nil {
return nil, nil, err
}
return acls, stat, nil
}
// SetACL is part of the ZkConn interface.
func (conn *ZkConn) SetACL(path string, aclv []zookeeper.ACL, version int32) error {
c := conn.getConn()
if c == nil {
return ErrConnectionClosed
}
sem.Acquire()
defer sem.Release()
_, err := c.SetACL(path, aclv, version)
return err
}