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
442
443
444
445
446
447
448
449
|
"""Zookeeper Serializers, Deserializers, and NamedTuple objects"""
from collections import namedtuple
import struct
import six
from kazoo.exceptions import EXCEPTIONS
from kazoo.protocol.states import ZnodeStat
from kazoo.security import ACL
from kazoo.security import Id
# Struct objects with formats compiled
bool_struct = struct.Struct('B')
int_struct = struct.Struct('!i')
int_int_struct = struct.Struct('!ii')
int_int_long_struct = struct.Struct('!iiq')
int_long_int_long_struct = struct.Struct('!iqiq')
long_struct = struct.Struct('!q')
multiheader_struct = struct.Struct('!iBi')
reply_header_struct = struct.Struct('!iqi')
stat_struct = struct.Struct('!qqqqiiiqiiq')
def read_string(buffer, offset):
"""Reads an int specified buffer into a string and returns the
string and the new offset in the buffer"""
length = int_struct.unpack_from(buffer, offset)[0]
offset += int_struct.size
if length < 0:
return None, offset
else:
index = offset
offset += length
return buffer[index:index + length].decode('utf-8'), offset
def read_acl(bytes, offset):
perms = int_struct.unpack_from(bytes, offset)[0]
offset += int_struct.size
scheme, offset = read_string(bytes, offset)
id, offset = read_string(bytes, offset)
return ACL(perms, Id(scheme, id)), offset
def write_string(bytes):
if not bytes:
return int_struct.pack(-1)
else:
utf8_str = bytes.encode('utf-8')
return int_struct.pack(len(utf8_str)) + utf8_str
def write_buffer(bytes):
if bytes is None:
return int_struct.pack(-1)
else:
return int_struct.pack(len(bytes)) + bytes
def read_buffer(bytes, offset):
length = int_struct.unpack_from(bytes, offset)[0]
offset += int_struct.size
if length < 0:
return None, offset
else:
index = offset
offset += length
return bytes[index:index + length], offset
class Close(namedtuple('Close', '')):
type = -11
@classmethod
def serialize(cls):
return b''
CloseInstance = Close()
class Ping(namedtuple('Ping', '')):
type = 11
@classmethod
def serialize(cls):
return b''
PingInstance = Ping()
class Connect(namedtuple('Connect', 'protocol_version last_zxid_seen'
' time_out session_id passwd read_only')):
type = None
def serialize(self):
b = bytearray()
b.extend(int_long_int_long_struct.pack(
self.protocol_version, self.last_zxid_seen, self.time_out,
self.session_id))
b.extend(write_buffer(self.passwd))
b.extend([1 if self.read_only else 0])
return b
@classmethod
def deserialize(cls, bytes, offset):
proto_version, timeout, session_id = int_int_long_struct.unpack_from(
bytes, offset)
offset += int_int_long_struct.size
password, offset = read_buffer(bytes, offset)
try:
read_only = bool_struct.unpack_from(bytes, offset)[0] is 1
offset += bool_struct.size
except struct.error:
read_only = False
return cls(proto_version, 0, timeout, session_id, password,
read_only), offset
class Create(namedtuple('Create', 'path data acl flags')):
type = 1
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend(write_buffer(self.data))
b.extend(int_struct.pack(len(self.acl)))
for acl in self.acl:
b.extend(int_struct.pack(acl.perms) +
write_string(acl.id.scheme) + write_string(acl.id.id))
b.extend(int_struct.pack(self.flags))
return b
@classmethod
def deserialize(cls, bytes, offset):
return read_string(bytes, offset)[0]
class Delete(namedtuple('Delete', 'path version')):
type = 2
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend(int_struct.pack(self.version))
return b
@classmethod
def deserialize(self, bytes, offset):
return True
class Exists(namedtuple('Exists', 'path watcher')):
type = 3
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend([1 if self.watcher else 0])
return b
@classmethod
def deserialize(cls, bytes, offset):
stat = ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
return stat if stat.czxid != -1 else None
class GetData(namedtuple('GetData', 'path watcher')):
type = 4
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend([1 if self.watcher else 0])
return b
@classmethod
def deserialize(cls, bytes, offset):
data, offset = read_buffer(bytes, offset)
stat = ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
return data, stat
class SetData(namedtuple('SetData', 'path data version')):
type = 5
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend(write_buffer(self.data))
b.extend(int_struct.pack(self.version))
return b
@classmethod
def deserialize(cls, bytes, offset):
return ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
class GetACL(namedtuple('GetACL', 'path')):
type = 6
def serialize(self):
return bytearray(write_string(self.path))
@classmethod
def deserialize(cls, bytes, offset):
count = int_struct.unpack_from(bytes, offset)[0]
offset += int_struct.size
if count == -1: # pragma: nocover
return []
acls = []
for c in range(count):
acl, offset = read_acl(bytes, offset)
acls.append(acl)
stat = ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
return acls, stat
class SetACL(namedtuple('SetACL', 'path acls version')):
type = 7
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend(int_struct.pack(len(self.acls)))
for acl in self.acls:
b.extend(int_struct.pack(acl.perms) +
write_string(acl.id.scheme) + write_string(acl.id.id))
b.extend(int_struct.pack(self.version))
return b
@classmethod
def deserialize(cls, bytes, offset):
return ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
class GetChildren(namedtuple('GetChildren', 'path watcher')):
type = 8
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend([1 if self.watcher else 0])
return b
@classmethod
def deserialize(cls, bytes, offset):
count = int_struct.unpack_from(bytes, offset)[0]
offset += int_struct.size
if count == -1: # pragma: nocover
return []
children = []
for c in range(count):
child, offset = read_string(bytes, offset)
children.append(child)
return children
class Sync(namedtuple('Sync', 'path')):
type = 9
def serialize(self):
return write_string(self.path)
@classmethod
def deserialize(cls, buffer, offset):
return read_string(buffer, offset)[0]
class GetChildren2(namedtuple('GetChildren2', 'path watcher')):
type = 12
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend([1 if self.watcher else 0])
return b
@classmethod
def deserialize(cls, bytes, offset):
count = int_struct.unpack_from(bytes, offset)[0]
offset += int_struct.size
if count == -1: # pragma: nocover
return []
children = []
for c in range(count):
child, offset = read_string(bytes, offset)
children.append(child)
stat = ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
return children, stat
class CheckVersion(namedtuple('CheckVersion', 'path version')):
type = 13
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend(int_struct.pack(self.version))
return b
class Transaction(namedtuple('Transaction', 'operations')):
type = 14
def serialize(self):
b = bytearray()
for op in self.operations:
b.extend(MultiHeader(op.type, False, -1).serialize() +
op.serialize())
return b + multiheader_struct.pack(-1, True, -1)
@classmethod
def deserialize(cls, bytes, offset):
header = MultiHeader(None, False, None)
results = []
response = None
while not header.done:
if header.type == Create.type:
response, offset = read_string(bytes, offset)
elif header.type == Delete.type:
response = True
elif header.type == SetData.type:
response = ZnodeStat._make(
stat_struct.unpack_from(bytes, offset))
offset += stat_struct.size
elif header.type == CheckVersion.type:
response = True
elif header.type == -1:
err = int_struct.unpack_from(bytes, offset)[0]
offset += int_struct.size
response = EXCEPTIONS[err]()
if response:
results.append(response)
header, offset = MultiHeader.deserialize(bytes, offset)
return results
@staticmethod
def unchroot(client, response):
resp = []
for result in response:
if isinstance(result, six.string_types):
resp.append(client.unchroot(result))
else:
resp.append(result)
return resp
class Create2(namedtuple('Create2', 'path data acl flags')):
type = 15
def serialize(self):
b = bytearray()
b.extend(write_string(self.path))
b.extend(write_buffer(self.data))
b.extend(int_struct.pack(len(self.acl)))
for acl in self.acl:
b.extend(int_struct.pack(acl.perms) +
write_string(acl.id.scheme) + write_string(acl.id.id))
b.extend(int_struct.pack(self.flags))
return b
@classmethod
def deserialize(cls, bytes, offset):
path, offset = read_string(bytes, offset)
stat = ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
return path, stat
class Reconfig(namedtuple('Reconfig',
'joining leaving new_members config_id')):
type = 16
def serialize(self):
b = bytearray()
b.extend(write_string(self.joining))
b.extend(write_string(self.leaving))
b.extend(write_string(self.new_members))
b.extend(long_struct.pack(self.config_id))
return b
@classmethod
def deserialize(cls, bytes, offset):
data, offset = read_buffer(bytes, offset)
stat = ZnodeStat._make(stat_struct.unpack_from(bytes, offset))
return data, stat
class Auth(namedtuple('Auth', 'auth_type scheme auth')):
type = 100
def serialize(self):
return (int_struct.pack(self.auth_type) + write_string(self.scheme) +
write_string(self.auth))
class SASL(namedtuple('SASL', 'challenge')):
type = 102
def serialize(self):
b = bytearray()
b.extend(write_buffer(self.challenge))
return b
@classmethod
def deserialize(cls, bytes, offset):
challenge, offset = read_buffer(bytes, offset)
return challenge, offset
class Watch(namedtuple('Watch', 'type state path')):
@classmethod
def deserialize(cls, bytes, offset):
"""Given bytes and the current bytes offset, return the
type, state, path, and new offset"""
type, state = int_int_struct.unpack_from(bytes, offset)
offset += int_int_struct.size
path, offset = read_string(bytes, offset)
return cls(type, state, path), offset
class ReplyHeader(namedtuple('ReplyHeader', 'xid, zxid, err')):
@classmethod
def deserialize(cls, bytes, offset):
"""Given bytes and the current bytes offset, return a
:class:`ReplyHeader` instance and the new offset"""
new_offset = offset + reply_header_struct.size
return cls._make(
reply_header_struct.unpack_from(bytes, offset)), new_offset
class MultiHeader(namedtuple('MultiHeader', 'type done err')):
def serialize(self):
b = bytearray()
b.extend(int_struct.pack(self.type))
b.extend([1 if self.done else 0])
b.extend(int_struct.pack(self.err))
return b
@classmethod
def deserialize(cls, bytes, offset):
t, done, err = multiheader_struct.unpack_from(bytes, offset)
offset += multiheader_struct.size
return cls(t, done is 1, err), offset
|