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
|
"""Zookeeper Protocol Implementation"""
import logging
from kazoo.exceptions import AuthFailedError
from kazoo.exceptions import ConnectionDropped
from kazoo.exceptions import EXCEPTIONS
from kazoo.protocol.serialization import int_struct
from kazoo.protocol.serialization import int_int_struct
from kazoo.protocol.serialization import deserialize_reply_header
from kazoo.protocol.serialization import Close
from kazoo.protocol.serialization import Connect
from kazoo.protocol.serialization import Ping
from kazoo.protocol.serialization import Watch
from kazoo.protocol.states import KeeperState
from kazoo.protocol.states import WatchedEvent
from kazoo.protocol.states import Callback
from kazoo.protocol.states import EVENT_TYPE_MAP
from kazoo.protocol.paths import _prefix_root
log = logging.getLogger(__name__)
def proto_reader(client, s, reader_started, reader_done, read_timeout):
reader_started.set()
while True:
try:
header, buffer, offset = _read_header(client, s, read_timeout)
if header.xid == -2:
log.debug('Received PING')
continue
elif header.xid == -4:
log.debug('Received AUTH')
continue
elif header.xid == -1:
watch, offset = Watch.deserialize(buffer, offset)
path = watch.path
log.debug('Received EVENT: %s', watch)
watchers = set()
with client._state_lock:
# Ignore watches if we've been stopped
if client._stopped.is_set():
continue
if watch.type in (1, 2, 3):
watchers |= client._data_watchers.pop(path, set())
elif watch.type == 4:
watchers |= client._child_watchers.pop(path, set())
else:
log.warn('Received unknown event %r', watch.type)
continue
ev = WatchedEvent(EVENT_TYPE_MAP[watch.type],
client._state, path)
client.handler.dispatch_callback(
Callback('watch',
lambda: map(lambda w: w(ev), watchers),
()
)
)
else:
log.debug('Reading for header %r', header)
request, async_object, xid = client._pending.get()
if header.zxid and header.zxid > 0:
client.last_zxid = header.zxid
if header.xid != xid:
raise RuntimeError('xids do not match, expected %r '
'received %r', xid, header.xid)
if header.err:
callback_exception = EXCEPTIONS[header.err]()
log.debug('Received error %r', callback_exception)
if async_object:
async_object.set_exception(callback_exception)
elif request and async_object:
response = request.deserialize(buffer, offset)
log.debug('Received response: %r', response)
async_object.set(response)
# Determine if watchers should be registered
with client._state_lock:
if (not client._stopped.is_set() and
hasattr(request, 'watcher')):
path = _prefix_root(client.chroot, request.path)
request.watch_dict[path].add(request.watcher)
if isinstance(request, Close):
log.debug('Read close response')
s.close()
reader_done.set()
break
except ConnectionDropped:
log.debug('Connection dropped for reader')
break
except Exception as e:
log.exception(e)
break
log.debug('Reader stopped')
def proto_writer(client):
log.debug('Starting writer')
retry = client.retry_sleeper.copy()
while not client._stopped.is_set():
log.debug("Client stopped?: %s", client._stopped.is_set())
# If the connect_loop returns False, stop retrying
if connect_loop(client, retry) is False:
break
# Still going, increment our retry then go through the
# list of hosts again
if not client._stopped.is_set():
log.debug("Incrementing and waiting")
retry.increment()
log.debug('Writer stopped')
client._writer_stopped.set()
def connect_loop(client, retry):
writer_done = False
for host, port in client.hosts:
s = client.handler.socket()
if client._state != KeeperState.CONNECTING:
client._session_callback(KeeperState.CONNECTING)
try:
read_timeout, connect_timeout = _connect(
client, s, host, port)
# Now that connection is good, reset the retries
retry.reset()
reader_started = client.handler.event_object()
reader_done = client.handler.event_object()
client.handler.spawn(proto_reader, client, s,
reader_started, reader_done, read_timeout)
reader_started.wait()
xid = 0
while not writer_done:
try:
request, async_object = client._queue.peek(
True, read_timeout / 2000.0)
log.debug('Sending %r', request)
xid += 1
log.debug('xid: %r', xid)
_submit(client, s, request, connect_timeout, xid)
if isinstance(request, Close):
log.debug('Received close request, closing')
writer_done = True
client._queue.get()
client._pending.put((request, async_object, xid))
except client.handler.empty:
log.debug('Queue timeout. Sending PING')
_submit(client, s, Ping, connect_timeout, -2)
except Exception as e:
log.exception(e)
break
log.debug('Waiting for reader to read close response')
reader_done.wait()
log.info('Closing connection to %s:%s', host, port)
if writer_done:
client._close(KeeperState.CLOSED)
return False
except ConnectionDropped:
log.warning('Connection dropped')
client._session_callback(KeeperState.CONNECTING)
except AuthFailedError:
log.warning('AUTH_FAILED closing')
client._session_callback(KeeperState.AUTH_FAILED)
return False
except Exception as e:
log.exception(e)
raise
finally:
if not writer_done:
# The read thread will close the socket since there
# could be a number of pending requests whose response
# still needs to be read from the socket.
s.close()
def _connect(client, s, host, port):
log.info('Connecting to %s:%s', host, port)
log.debug(' Using session_id: %r session_passwd: 0x%s',
client._session_id, client._session_passwd.encode('hex'))
s.connect((host, port))
s.setblocking(0)
log.debug('Connected')
connect = Connect(
0,
client.last_zxid,
client._session_timeout,
client._session_id or 0,
client._session_passwd,
client.read_only)
connect_result, zxid = _invoke(client, s, client._session_timeout, connect)
if connect_result.time_out < 0:
log.error('Session expired')
client._session_callback(KeeperState.EXPIRED_SESSION)
raise RuntimeError('Session expired')
if zxid:
client.last_zxid = zxid
# Load return values
client._session_id = connect_result.session_id
negotiated_session_timeout = connect_result.time_out
connect_timeout = negotiated_session_timeout / len(client.hosts)
read_timeout = negotiated_session_timeout * 2.0 / 3.0
client._session_passwd = connect_result.passwd
log.debug('Session created, session_id: %r session_passwd: 0x%s\n'
' negotiated session timeout: %s\n'
' connect timeout: %s\n'
' read timeout: %s', client._session_id,
client._session_passwd.encode('hex'), negotiated_session_timeout,
connect_timeout, read_timeout)
client._session_callback(KeeperState.CONNECTED)
# for scheme, auth in client.auth_data:
# ap = AuthPacket(0, scheme, auth)
# zxid = _invoke(s, connect_timeout, ap, xid=-4)
# if zxid:
# client.last_zxid = zxid
return read_timeout, connect_timeout
def _invoke(client, socket, timeout, request, xid=None):
b = bytearray()
if xid:
b.extend(int_struct.pack(xid))
if request.type:
b.extend(int_struct.pack(request.type))
b.extend(request.serialize())
buff = int_struct.pack(len(b)) + b
_write(client, socket, buff, timeout)
log.debug("Wrote out initial request")
zxid = None
if xid:
header, buffer, offset = _read_header(client, socket, timeout)
if header.xid != xid:
raise RuntimeError('xids do not match, expected %r received %r',
xid, header.xid)
if header.zxid > 0:
zxid = header.zxid
if header.err:
callback_exception = EXCEPTIONS[header.err]()
log.debug('Received error %r', callback_exception)
raise callback_exception
return zxid
msg = _read(client, socket, 4, timeout)
length = int_struct.unpack(msg)[0]
log.debug("Reading full packet")
msg = _read(client, socket, length, timeout)
if hasattr(request, 'deserialize'):
obj, _ = request.deserialize(msg, 0)
log.debug('Read response %s', obj)
return obj, zxid
return zxid
def _submit(client, socket, request, timeout, xid=None):
b = bytearray()
b.extend(int_struct.pack(xid))
if request.type:
b.extend(int_struct.pack(request.type))
b += request.serialize()
b = int_struct.pack(len(b)) + b
_write(client, socket, b, timeout)
def _write(client, socket, msg, timeout):
sent = 0
msg_length = len(msg)
select = client.handler.select
while sent < msg_length:
_, ready_to_write, _ = select([], [socket], [], timeout)
msg_slice = buffer(msg, sent)
bytes_sent = ready_to_write[0].send(msg_slice)
if not bytes_sent:
raise ConnectionDropped('socket connection broken')
sent += bytes_sent
def _read_header(client, socket, timeout):
b = _read(client, socket, 4, timeout)
length = int_struct.unpack(b)[0]
b = _read(client, socket, length, timeout)
header, offset = deserialize_reply_header(b, 0)
return header, b, offset
def _read(client, socket, length, timeout):
msgparts = []
remaining = length
select = client.handler.select
while remaining > 0:
ready_to_read, _, _ = select([socket], [], [], timeout)
chunk = ready_to_read[0].recv(remaining)
if chunk == '':
raise ConnectionDropped('socket connection broken')
msgparts.append(chunk)
remaining -= len(chunk)
return b"".join(msgparts)
|