forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconstants.go
390 lines (347 loc) · 11.1 KB
/
constants.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
package zk
import (
"errors"
"fmt"
)
const (
protocolVersion = 0
// DefaultPort is the default port listened by server.
DefaultPort = 2181
)
const (
opNotify = 0
opCreate = 1
opDelete = 2
opExists = 3
opGetData = 4
opSetData = 5
opGetACL = 6
opSetACL = 7
opGetChildren = 8
opSync = 9
opPing = 11
opGetChildren2 = 12
opCheck = 13
opMulti = 14
opReconfig = 16
opRemoveWatches = 18
opCreateContainer = 19
opCreateTTL = 21
opMultiRead = 22
opClose = -11
opSetAuth = 100
opSetWatches = 101
opSetWatches2 = 105
opAddWatch = 106
opError = -1
// Not in protocol, used internally
opWatcherEvent = -2
)
// Events that can be received from the server.
const (
EventNodeCreated EventType = 1
EventNodeDeleted EventType = 2
EventNodeDataChanged EventType = 3
EventNodeChildrenChanged EventType = 4
)
// Events for internal use.
const (
EventSession EventType = -1 // EventSession represents a session event.
EventNotWatching EventType = -2 // EventNotWatching indicates a watch has aborted.
EventWatcherStalled EventType = -3 // EventWatcherStalled indicates a watcher has stalled.
)
var (
eventNames = map[EventType]string{
EventNodeCreated: "EventNodeCreated",
EventNodeDeleted: "EventNodeDeleted",
EventNodeDataChanged: "EventNodeDataChanged",
EventNodeChildrenChanged: "EventNodeChildrenChanged",
EventSession: "EventSession",
EventNotWatching: "EventNotWatching",
EventWatcherStalled: "EventWatcherStalled",
}
)
const (
// StateUnknown means the session state is unknown.
StateUnknown State = -1
StateDisconnected State = 0
StateConnecting State = 1
StateAuthFailed State = 4
StateConnectedReadOnly State = 5
StateSaslAuthenticated State = 6
StateExpired State = -112
StateConnected = State(100)
StateHasSession = State(101)
)
var (
stateNames = map[State]string{
StateUnknown: "StateUnknown",
StateDisconnected: "StateDisconnected",
StateConnectedReadOnly: "StateConnectedReadOnly",
StateSaslAuthenticated: "StateSaslAuthenticated",
StateExpired: "StateExpired",
StateAuthFailed: "StateAuthFailed",
StateConnecting: "StateConnecting",
StateConnected: "StateConnected",
StateHasSession: "StateHasSession",
}
)
const (
// FlagEphemeral means the node is ephemeral.
FlagEphemeral = 1
FlagSequence = 2
FlagTTL = 4
)
const (
addWatchModePersistent addWatchMode = 0
addWatchModePersistentRecursive addWatchMode = 1
)
var (
addWatchModeNames = map[addWatchMode]string{
addWatchModePersistent: "addWatchModePersistent",
addWatchModePersistentRecursive: "addWatchModePersistentRecursive",
}
)
const (
removeWatchTypeChildren removeWatchType = 1
removeWatchTypeData removeWatchType = 2
removeWatchTypePersistent removeWatchType = 3
)
var (
removeWatchTypeNames = map[removeWatchType]string{
removeWatchTypeChildren: "removeWatchTypeChildren",
removeWatchTypeData: "removeWatchTypeData",
removeWatchTypePersistent: "removeWatchTypePersistent",
}
)
const (
watcherKindData watcherKind = iota
watcherKindExist
watcherKindChildren
watcherKindPersistent
watcherKindPersistentRecursive
)
var (
watcherKindNames = map[watcherKind]string{
watcherKindData: "watcherKindData",
watcherKindExist: "watcherKindExist",
watcherKindChildren: "watcherKindChildren",
watcherKindPersistent: "watcherKindPersistent",
watcherKindPersistentRecursive: "watcherKindPersistentRecursive",
}
)
// ErrCode is the error code defined by server. Refer to ZK documentations for more specifics.
type ErrCode int32
var (
// ErrConnectionClosed means the connection has been closed.
ErrConnectionClosed = errors.New("zk: connection closed")
ErrUnknown = errors.New("zk: unknown error")
ErrAPIError = errors.New("zk: api error")
ErrNoNode = errors.New("zk: node does not exist")
ErrNoAuth = errors.New("zk: not authenticated")
ErrBadVersion = errors.New("zk: version conflict")
ErrNoChildrenForEphemerals = errors.New("zk: ephemeral nodes may not have children")
ErrNodeExists = errors.New("zk: node already exists")
ErrNotEmpty = errors.New("zk: node has children")
ErrSessionExpired = errors.New("zk: session has been expired by the server")
ErrInvalidACL = errors.New("zk: invalid ACL specified")
ErrInvalidFlags = errors.New("zk: invalid flags specified")
ErrAuthFailed = errors.New("zk: client authentication failed")
ErrClosing = errors.New("zk: zookeeper is closing")
ErrNothing = errors.New("zk: no server responses to process")
ErrSessionMoved = errors.New("zk: session moved to another server, so operation is ignored")
ErrReconfigDisabled = errors.New("attempts to perform a reconfiguration operation when reconfiguration feature is disabled")
ErrBadArguments = errors.New("invalid arguments")
ErrNoWatcher = errors.New("watcher does not exist")
ErrInvalidCallback = errors.New("zk: invalid callback specified")
errCodeToError = map[ErrCode]error{
errOk: nil,
errAPIError: ErrAPIError,
errNoNode: ErrNoNode,
errNoAuth: ErrNoAuth,
errBadVersion: ErrBadVersion,
errNoChildrenForEphemerals: ErrNoChildrenForEphemerals,
errNodeExists: ErrNodeExists,
errNotEmpty: ErrNotEmpty,
errSessionExpired: ErrSessionExpired,
errInvalidCallback: ErrInvalidCallback,
errInvalidAcl: ErrInvalidACL,
errAuthFailed: ErrAuthFailed,
errClosing: ErrClosing,
errNothing: ErrNothing,
errSessionMoved: ErrSessionMoved,
errZReconfigDisabled: ErrReconfigDisabled,
errBadArguments: ErrBadArguments,
errNoWatcher: ErrNoWatcher,
}
)
func (e ErrCode) toError() error {
if err, ok := errCodeToError[e]; ok {
return err
}
return fmt.Errorf("unknown error: %v", e)
}
const (
errOk ErrCode = 0
// System and server-side errors
errSystemError ErrCode = -1 // nolint: unused
errRuntimeInconsistency ErrCode = -2 // nolint: unused
errDataInconsistency ErrCode = -3 // nolint: unused
errConnectionLoss ErrCode = -4 // nolint: unused
errMarshallingError ErrCode = -5 // nolint: unused
errUnimplemented ErrCode = -6 // nolint: unused
errOperationTimeout ErrCode = -7 // nolint: unused
errBadArguments ErrCode = -8
errInvalidState ErrCode = -9 // nolint: unused
// API errors
errAPIError ErrCode = -100
errNoNode ErrCode = -101 // *
errNoAuth ErrCode = -102
errBadVersion ErrCode = -103 // *
errNoChildrenForEphemerals ErrCode = -108
errNodeExists ErrCode = -110 // *
errNotEmpty ErrCode = -111
errSessionExpired ErrCode = -112
errInvalidCallback ErrCode = -113
errInvalidAcl ErrCode = -114 // nolint: revive, stylecheck
errAuthFailed ErrCode = -115
errClosing ErrCode = -116
errNothing ErrCode = -117
errSessionMoved ErrCode = -118
errNoWatcher ErrCode = -122
// Attempts to perform a reconfiguration operation when reconfiguration feature is disabled
errZReconfigDisabled ErrCode = -123
)
// Constants for ACL permissions
const (
// PermRead represents the permission needed to read a znode.
PermRead = 1 << iota
PermWrite
PermCreate
PermDelete
PermAdmin
PermAll = 0x1f
)
var (
emptyPassword = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
opNames = map[int32]string{
opNotify: "notify",
opCreate: "create",
opCreateContainer: "createContainer",
opCreateTTL: "createTTL",
opDelete: "delete",
opExists: "exists",
opGetData: "getData",
opSetData: "setData",
opGetACL: "getACL",
opSetACL: "setACL",
opGetChildren: "getChildren",
opSync: "sync",
opPing: "ping",
opGetChildren2: "getChildren2",
opCheck: "check",
opMulti: "multi",
opMultiRead: "multiRead",
opReconfig: "reconfig",
opClose: "close",
opSetAuth: "setAuth",
opSetWatches: "setWatches",
opSetWatches2: "setWatches2",
opAddWatch: "addWatch",
opRemoveWatches: "removeWatches",
opWatcherEvent: "watcherEvent",
}
)
// EventType represents the event type sent by server.
type EventType int32
func (t EventType) String() string {
if name := eventNames[t]; name != "" {
return name
}
return "Unknown"
}
// Mode is used to build custom server modes (leader|follower|standalone).
type Mode uint8
func (m Mode) String() string {
if name := modeNames[m]; name != "" {
return name
}
return "unknown"
}
const (
ModeUnknown Mode = iota
ModeLeader Mode = iota
ModeFollower Mode = iota
ModeStandalone Mode = iota
)
var (
modeNames = map[Mode]string{
ModeLeader: "leader",
ModeFollower: "follower",
ModeStandalone: "standalone",
}
)
// State is the session state.
type State int32
// String converts State to a readable string.
func (s State) String() string {
if name := stateNames[s]; name != "" {
return name
}
return "Unknown"
}
// addWatchMode defines the mode used to create a persistent watch
type addWatchMode int32
func (m addWatchMode) String() string {
if name := addWatchModeNames[m]; name != "" {
return name
}
return "Unknown"
}
// removeWatchType represents a type of watch known to the server.
type removeWatchType int32
func (m removeWatchType) String() string {
if name := removeWatchTypeNames[m]; name != "" {
return name
}
return "Unknown"
}
// watcherKind represents the kind of watcher known to the client.
// Combines a watch type and watch mode.
type watcherKind int
func (wk watcherKind) String() string {
if name := watcherKindNames[wk]; name != "" {
return name
}
return "Unknown"
}
func (wk watcherKind) isPersistent() bool {
switch wk {
case watcherKindPersistent, watcherKindPersistentRecursive:
return true
}
return false
}
func (wk watcherKind) isRecursive() bool {
switch wk {
case watcherKindPersistentRecursive:
return true
}
return false
}
func (wk watcherKind) handlesEventType(eventType EventType) bool {
if wk.isPersistent() {
return true // Persistent watchers handle all event types.
}
switch eventType {
case EventNodeCreated:
return wk == watcherKindExist
case EventNodeDeleted:
return wk == watcherKindExist || wk == watcherKindData || wk == watcherKindChildren
case EventNodeDataChanged:
return wk == watcherKindExist || wk == watcherKindData
case EventNodeChildrenChanged:
return wk == watcherKindChildren
}
return false
}