This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
framework.go
441 lines (335 loc) · 12.8 KB
/
framework.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package curator
import (
"fmt"
"log"
"time"
"github.com/samuel/go-zookeeper/zk"
)
const (
DEFAULT_SESSION_TIMEOUT = 60 * time.Second
DEFAULT_CONNECTION_TIMEOUT = 15 * time.Second
DEFAULT_CLOSE_WAIT = 1 * time.Second
)
// Zookeeper framework-style client
type CuratorFramework interface {
// Start the client.
// Most mutator methods will not work until the client is started
Start() error
// Stop the client
Close() error
// Returns the state of this instance
State() State
// Return true if the client is started, not closed, etc.
Started() bool
// Start a create builder
Create() CreateBuilder
// Start a delete builder
Delete() DeleteBuilder
// Start an exists builder
CheckExists() CheckExistsBuilder
// Start a get data builder
GetData() GetDataBuilder
// Start a set data builder
SetData() SetDataBuilder
// Start a get children builder
GetChildren() GetChildrenBuilder
// Start a get ACL builder
GetACL() GetACLBuilder
// Start a set ACL builder
SetACL() SetACLBuilder
// Start a transaction builder
InTransaction() Transaction
// Perform a sync on the given path - syncs are always in the background
DoSync(path string, backgroundContextObject interface{})
// Start a sync builder. Note: sync is ALWAYS in the background even if you don't use one of the background() methods
Sync() SyncBuilder
// Returns the listenable interface for the Connect State
ConnectionStateListenable() ConnectionStateListenable
// Returns the listenable interface for events
CuratorListenable() CuratorListenable
// Returns the listenable interface for unhandled errors
UnhandledErrorListenable() UnhandledErrorListenable
// Returns a facade of the current instance that does _not_ automatically pre-pend the namespace to all paths
NonNamespaceView() CuratorFramework
// Returns a facade of the current instance that uses the specified namespace
// or no namespace if newNamespace is empty.
UsingNamespace(newNamespace string) CuratorFramework
// Return the current namespace or "" if none
Namespace() string
// Return the managed zookeeper client
ZookeeperClient() CuratorZookeeperClient
// Allocates an ensure path instance that is namespace aware
NewNamespaceAwareEnsurePath(path string) EnsurePath
// Block until a connection to ZooKeeper is available.
BlockUntilConnected() error
// Block until a connection to ZooKeeper is available or the maxWaitTime has been exceeded
BlockUntilConnectedTimeout(maxWaitTime time.Duration) error
}
// Create a new client with default session timeout and default connection timeout
func NewClient(connString string, retryPolicy RetryPolicy) CuratorFramework {
return NewClientTimeout(connString, DEFAULT_SESSION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT, retryPolicy)
}
// Create a new client
func NewClientTimeout(connString string, sessionTimeout, connectionTimeout time.Duration, retryPolicy RetryPolicy) CuratorFramework {
builder := &CuratorFrameworkBuilder{
ConnectionTimeout: connectionTimeout,
SessionTimeout: sessionTimeout,
RetryPolicy: retryPolicy,
}
return builder.ConnectString(connString).Build()
}
type CuratorFrameworkBuilder struct {
AuthInfos []AuthInfo // the connection authorization
ZookeeperDialer ZookeeperDialer // the zookeeper dialer to use
EnsembleProvider EnsembleProvider // the list ensemble provider.
DefaultData []byte // the data to use when PathAndBytesable.ForPath(String) is used.
Namespace string // as ZooKeeper is a shared space, users of a given cluster should stay within a pre-defined namespace
SessionTimeout time.Duration // the session timeout
ConnectionTimeout time.Duration // the connection timeout
MaxCloseWait time.Duration // the time to wait during close to wait background tasks
RetryPolicy RetryPolicy // the retry policy to use
CompressionProvider CompressionProvider // the compression provider
AclProvider ACLProvider // the provider for ACLs
CanBeReadOnly bool // allow ZooKeeper client to enter read only mode in case of a network partition.
}
// Apply the current values and build a new CuratorFramework
func (b *CuratorFrameworkBuilder) Build() CuratorFramework {
if b.EnsembleProvider == nil {
panic("missed ensemble provider")
}
builder := *b
if builder.SessionTimeout == 0 {
builder.SessionTimeout = DEFAULT_SESSION_TIMEOUT
}
if builder.ConnectionTimeout == 0 {
builder.ConnectionTimeout = DEFAULT_CONNECTION_TIMEOUT
}
if builder.MaxCloseWait == 0 {
builder.MaxCloseWait = DEFAULT_CLOSE_WAIT
}
if builder.CompressionProvider == nil {
builder.CompressionProvider = NewGzipCompressionProvider()
}
if builder.AclProvider == nil {
builder.AclProvider = NewDefaultACLProvider()
}
return newCuratorFramework(&builder)
}
// Set the list of servers to connect to.
func (b *CuratorFrameworkBuilder) ConnectString(connectString string) *CuratorFrameworkBuilder {
b.EnsembleProvider = &FixedEnsembleProvider{connectString}
return b
}
// Add connection authorization
func (b *CuratorFrameworkBuilder) Authorization(scheme string, auth []byte) *CuratorFrameworkBuilder {
b.AuthInfos = append(b.AuthInfos, AuthInfo{scheme, auth})
return b
}
// Add compression provider
func (b *CuratorFrameworkBuilder) Compression(name string) *CuratorFrameworkBuilder {
if provider, exists := CompressionProviders[name]; exists {
b.CompressionProvider = provider
}
return b
}
type curatorFramework struct {
client *curatorZookeeperClient
stateManager *connectionStateManager
state State
listeners CuratorListenable
unhandledErrorListeners UnhandledErrorListenable
defaultData []byte
namespace *namespaceImpl
namespaceFacadeCache *namespaceFacadeCache
fixForNamespace func(path string, isSequential bool) string
unfixForNamespace func(path string) string
retryPolicy RetryPolicy
compressionProvider CompressionProvider
aclProvider ACLProvider
}
func newCuratorFramework(b *CuratorFrameworkBuilder) *curatorFramework {
c := &curatorFramework{
listeners: &curatorListenerContainer{},
unhandledErrorListeners: &UnhandledErrorListenerContainer{},
defaultData: b.DefaultData,
retryPolicy: b.RetryPolicy,
compressionProvider: b.CompressionProvider,
aclProvider: b.AclProvider,
}
watcher := NewWatcher(func(event *zk.Event) {
c.processEvent(&curatorEvent{
eventType: WATCHED,
err: event.Err,
path: c.unfixForNamespace(event.Path),
watchedEvent: event,
})
})
c.client = NewCuratorZookeeperClient(b.ZookeeperDialer, b.EnsembleProvider, b.SessionTimeout, b.ConnectionTimeout, watcher, b.RetryPolicy, b.CanBeReadOnly, b.AuthInfos)
c.stateManager = newConnectionStateManager(c)
c.namespace = newNamespace(c, b.Namespace)
c.namespaceFacadeCache = newNamespaceFacadeCache(c)
c.fixForNamespace = c.namespace.fixForNamespace
c.unfixForNamespace = c.namespace.unfixForNamespace
return c
}
func (c *curatorFramework) Start() error {
if !c.state.Change(LATENT, STARTED) {
return fmt.Errorf("Cannot be started more than once")
} else if err := c.stateManager.Start(); err != nil {
return fmt.Errorf("fail to start state manager, %s", err)
} else if err := c.client.Start(); err != nil {
return fmt.Errorf("fail to start client, %s", err)
}
return nil
}
func (c *curatorFramework) Close() error {
if !c.state.Change(STARTED, STOPPED) {
return nil
}
evt := &curatorEvent{eventType: CLOSING}
c.listeners.ForEach(func(listener interface{}) {
listener.(CuratorListener).EventReceived(c, evt)
})
c.listeners.Clear()
c.unhandledErrorListeners.Clear()
c.stateManager.Close()
return c.client.Close()
}
func (c *curatorFramework) State() State {
return c.state.Value()
}
func (c *curatorFramework) Started() bool {
return c.State() == STARTED
}
func (c *curatorFramework) Create() CreateBuilder {
c.state.Check(STARTED, "instance must be started before calling Create")
return &createBuilder{client: c, acling: acling{aclProvider: c.aclProvider}}
}
func (c *curatorFramework) Delete() DeleteBuilder {
c.state.Check(STARTED, "instance must be started before calling Delete")
return &deleteBuilder{client: c, version: AnyVersion}
}
func (c *curatorFramework) CheckExists() CheckExistsBuilder {
c.state.Check(STARTED, "instance must be started before calling CheckExists")
return &checkExistsBuilder{client: c}
}
func (c *curatorFramework) GetData() GetDataBuilder {
c.state.Check(STARTED, "instance must be started before calling GetData")
return &getDataBuilder{client: c}
}
func (c *curatorFramework) SetData() SetDataBuilder {
c.state.Check(STARTED, "instance must be started before calling SetData")
return &setDataBuilder{client: c, version: AnyVersion}
}
func (c *curatorFramework) GetChildren() GetChildrenBuilder {
c.state.Check(STARTED, "instance must be started before calling GetChildren")
return &getChildrenBuilder{client: c}
}
func (c *curatorFramework) GetACL() GetACLBuilder {
c.state.Check(STARTED, "instance must be started before calling GetACL")
return &getACLBuilder{client: c}
}
func (c *curatorFramework) SetACL() SetACLBuilder {
c.state.Check(STARTED, "instance must be started before calling SetACL")
return &setACLBuilder{client: c, version: AnyVersion, acling: acling{aclProvider: c.aclProvider}}
}
func (c *curatorFramework) InTransaction() Transaction {
c.state.Check(STARTED, "instance must be started before calling InTransaction")
return &curatorTransaction{client: c}
}
func (c *curatorFramework) DoSync(path string, context interface{}) {
c.Sync().InBackgroundWithContext(context).ForPath(path)
}
func (c *curatorFramework) Sync() SyncBuilder {
c.state.Check(STARTED, "instance must be started before calling this method")
return &syncBuilder{client: c}
}
func (c *curatorFramework) ConnectionStateListenable() ConnectionStateListenable {
return c.stateManager.Listenable()
}
func (c *curatorFramework) CuratorListenable() CuratorListenable {
return c.listeners
}
func (c *curatorFramework) UnhandledErrorListenable() UnhandledErrorListenable {
return c.unhandledErrorListeners
}
func (c *curatorFramework) processEvent(event CuratorEvent) {
if event.Type() == WATCHED {
c.validateConnection(event.WatchedEvent().State)
}
c.listeners.ForEach(func(l interface{}) {
tracer := c.client.StartTracer("EventListener")
defer tracer.Commit()
if err := l.(CuratorListener).EventReceived(c, event); err != nil {
c.logError(fmt.Errorf("Event listener threw exception, %s", err))
}
})
}
func (c *curatorFramework) validateConnection(state zk.State) {
switch state {
case zk.StateDisconnected:
c.suspendConnection()
case zk.StateExpired:
c.stateManager.AddStateChange(LOST)
case zk.StateConnected:
c.stateManager.AddStateChange(RECONNECTED)
case zk.StateConnectedReadOnly:
c.stateManager.AddStateChange(READ_ONLY)
}
}
func (c *curatorFramework) suspendConnection() {
if !c.stateManager.SetToSuspended() {
return
}
go c.doSyncForSuspendedConnection(c.client.InstanceIndex())
}
func (c *curatorFramework) doSyncForSuspendedConnection(instanceIndex int64) {
tracer := c.client.StartTracer("BackgroundSyncImpl")
defer tracer.Commit()
if conn, err := c.client.Conn(); err == nil {
if path, err := conn.Sync("/"); err == nil {
c.processEvent(&curatorEvent{
eventType: SYNC,
err: err,
path: path,
})
return
}
}
if instanceIndex < 0 || instanceIndex == c.client.InstanceIndex() {
c.stateManager.AddStateChange(LOST)
} else {
go c.doSyncForSuspendedConnection(-1)
}
}
func (c *curatorFramework) logError(err error) {
log.Printf("error: %s", err)
c.unhandledErrorListeners.ForEach(func(listener interface{}) {
listener.(UnhandledErrorListener).UnhandledError(err)
})
}
func (c *curatorFramework) NonNamespaceView() CuratorFramework {
return c.UsingNamespace("")
}
func (c *curatorFramework) UsingNamespace(newNamespace string) CuratorFramework {
c.state.Check(STARTED, "instance must be started before calling this method")
return c.namespaceFacadeCache.Get(newNamespace)
}
func (c *curatorFramework) Namespace() string {
return c.namespace.namespace
}
func (c *curatorFramework) getNamespaceWatcher(watcher Watcher) Watcher {
return watcher
}
func (c *curatorFramework) ZookeeperClient() CuratorZookeeperClient {
return c.client
}
func (c *curatorFramework) NewNamespaceAwareEnsurePath(path string) EnsurePath {
return NewEnsurePathWithAcl(c.fixForNamespace(path, false), c.aclProvider)
}
func (c *curatorFramework) BlockUntilConnected() error {
return c.BlockUntilConnectedTimeout(0)
}
func (c *curatorFramework) BlockUntilConnectedTimeout(maxWaitTime time.Duration) error {
return c.stateManager.BlockUntilConnected(maxWaitTime)
}