-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
165 lines (103 loc) · 4.28 KB
/
doc.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
/*
Curator.go is a Golang porting for Curator, make it easy to access Zookeeper
Learn ZooKeeper
Curator.go users are assumed to know ZooKeeper. A good place to start is http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html
Using Curator
The Curator.go are available from github.com.
$ go get github.com/flier/curator.go
You can easily include Curator.go into your code.
import (
"github.com/flier/curator.go"
)
Getting a Connection
Curator uses Fluent Style. If you haven't used this before, it might seem odd so it's suggested that you familiarize yourself with the style.
Curator connection instances (CuratorFramework) are allocated from the CuratorFrameworkBuilder. You only need one CuratorFramework object for each ZooKeeper cluster you are connecting to:
curator.NewClient(connString, retryPolicy)
This will create a connection to a ZooKeeper cluster using default values. The only thing that you need to specify is the retry policy. For most cases, you should use:
retryPolicy := curator.NewExponentialBackoffRetry(time.Second, 3, 15*time.Second)
client := curator.NewClient(connString, retryPolicy)
client.Start()
defer client.Close()
The client must be started (and closed when no longer needed).
Calling ZooKeeper Directly
Once you have a CuratorFramework instance, you can make direct calls to ZooKeeper in a similar way to using the raw ZooKeeper object provided in the ZooKeeper distribution. E.g.:
client.Create().ForPathWithData(path, payload)
The benefit here is that Curator manages the ZooKeeper connection and will retry operations if there are connection problems.
Recipes
Distributed Lock
lock := curator.NewInterProcessMutex(client, lockPath)
if ( lock.Acquire(maxWait, waitUnit) )
{
defer lock.Release()
// do some work inside of the critical section here
}
Leader Election
listener := curator.NewLeaderSelectorListener(func(CuratorFramework client) error {
// this callback will get called when you are the leader
// do whatever leader work you need to and only exit
// this method when you want to relinquish leadership
}))
selector := curator.NewLeaderSelector(client, path, listener)
selector.AutoRequeue() // not required, but this is behavior that you will probably expect
selector.Start()
Generic API
Curator provides generic API for builder
type Pathable[T] interface {
// Commit the currently building operation using the given path
ForPath(path string) (T, error)
}
type PathAndBytesable[T] interface {
Pathable[T]
// Commit the currently building operation using the given path and data
ForPathWithData(path string, payload []byte) (T, error)
}
type Compressible[T] interface {
// Cause the data to be compressed using the configured compression provider
Compressed() T
}
type Decompressible[T] interface {
// Cause the data to be de-compressed using the configured compression provider
Decompressed() T
}
type CreateModable[T] interface {
// Set a create mode - the default is CreateMode.PERSISTENT
WithMode(mode CreateMode) T
}
type ACLable[T] interface {
// Set an ACL list
WithACL(acl ...zk.ACL) T
}
type Versionable[T] interface {
// Use the given version (the default is -1)
WithVersion(version int) T
}
type Statable[T] interface {
// Have the operation fill the provided stat object
StoringStatIn(*zk.Stat) T
}
type ParentsCreatable[T] interface {
// Causes any parent nodes to get created if they haven't already been
CreatingParentsIfNeeded() T
}
type ChildrenDeletable[T] interface {
// Will also delete children if they exist.
DeletingChildrenIfNeeded() T
}
type Watchable[T] interface {
// Have the operation set a watch
Watched() T
// Set a watcher for the operation
UsingWatcher(watcher Watcher) T
}
type Backgroundable[T] interface {
// Perform the action in the background
InBackground() T
// Perform the action in the background
InBackgroundWithContext(context interface{}) T
// Perform the action in the background
InBackgroundWithCallback(callback BackgroundCallback) T
// Perform the action in the background
InBackgroundWithCallbackAndContext(callback BackgroundCallback, context interface{}) T
}
*/
package curator