-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathclient.py
1703 lines (1365 loc) · 59.9 KB
/
client.py
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Kazoo Zookeeper Client"""
from collections import defaultdict, deque
from functools import partial
import inspect
import logging
from os.path import split
import re
import warnings
import six
from kazoo.exceptions import (
AuthFailedError,
ConfigurationError,
ConnectionClosedError,
ConnectionLoss,
KazooException,
NoNodeError,
NodeExistsError,
SessionExpiredError,
WriterNotClosedException,
)
from kazoo.handlers.threading import SequentialThreadingHandler
from kazoo.handlers.utils import capture_exceptions, wrap
from kazoo.hosts import collect_hosts
from kazoo.loggingsupport import BLATHER
from kazoo.protocol.connection import ConnectionHandler
from kazoo.protocol.paths import _prefix_root, normpath
from kazoo.protocol.serialization import (
Auth,
CheckVersion,
CloseInstance,
Create,
Create2,
Delete,
Exists,
GetChildren,
GetChildren2,
GetACL,
SetACL,
GetData,
Reconfig,
SetData,
Sync,
Transaction
)
from kazoo.protocol.states import (
Callback,
EventType,
KazooState,
KeeperState,
WatchedEvent
)
from kazoo.retry import KazooRetry
from kazoo.security import ACL, OPEN_ACL_UNSAFE
# convenience API
from kazoo.recipe.barrier import Barrier, DoubleBarrier
from kazoo.recipe.counter import Counter
from kazoo.recipe.election import Election
from kazoo.recipe.lease import NonBlockingLease, MultiNonBlockingLease
from kazoo.recipe.lock import Lock, ReadLock, WriteLock, Semaphore
from kazoo.recipe.partitioner import SetPartitioner
from kazoo.recipe.party import Party, ShallowParty
from kazoo.recipe.queue import Queue, LockingQueue
from kazoo.recipe.watchers import ChildrenWatch, DataWatch
string_types = six.string_types
bytes_types = (six.binary_type,)
CLOSED_STATES = (
KeeperState.EXPIRED_SESSION,
KeeperState.AUTH_FAILED,
KeeperState.CLOSED
)
ENVI_VERSION = re.compile(r'([\d\.]*).*', re.DOTALL)
ENVI_VERSION_KEY = 'zookeeper.version'
log = logging.getLogger(__name__)
_RETRY_COMPAT_DEFAULTS = dict(
max_retries=None,
retry_delay=0.1,
retry_backoff=2,
retry_max_delay=3600,
)
_RETRY_COMPAT_MAPPING = dict(
max_retries='max_tries',
retry_delay='delay',
retry_backoff='backoff',
retry_max_delay='max_delay',
)
class KazooClient(object):
"""An Apache Zookeeper Python client supporting alternate callback
handlers and high-level functionality.
Watch functions registered with this class will not get session
events, unlike the default Zookeeper watches. They will also be
called with a single argument, a
:class:`~kazoo.protocol.states.WatchedEvent` instance.
"""
def __init__(self, hosts='127.0.0.1:2181',
timeout=10.0, client_id=None, handler=None,
default_acl=None, auth_data=None, sasl_options=None,
read_only=None, randomize_hosts=True, connection_retry=None,
command_retry=None, logger=None, keyfile=None,
keyfile_password=None, certfile=None, ca=None,
use_ssl=False, verify_certs=True, **kwargs):
"""Create a :class:`KazooClient` instance. All time arguments
are in seconds.
:param hosts: Comma-separated list of hosts to connect to
(e.g. 127.0.0.1:2181,127.0.0.1:2182,[::1]:2183).
:param timeout: The longest to wait for a Zookeeper connection.
:param client_id: A Zookeeper client id, used when
re-establishing a prior session connection.
:param handler: An instance of a class implementing the
:class:`~kazoo.interfaces.IHandler` interface
for callback handling.
:param default_acl: A default ACL used on node creation.
:param auth_data:
A list of authentication credentials to use for the
connection. Should be a list of (scheme, credential)
tuples as :meth:`add_auth` takes.
:param sasl_options:
SASL options for the connection, if SASL support is to be used.
Should be a dict of SASL options passed to the underlying
`pure-sasl <https://pypi.org/project/pure-sasl>`_ library.
For example using the DIGEST-MD5 mechnism:
.. code-block:: python
sasl_options = {
'mechanism': 'DIGEST-MD5',
'username': 'myusername',
'password': 'mypassword'
}
For GSSAPI, using the running process' ticket cache:
.. code-block:: python
sasl_options = {
'mechanism': 'GSSAPI',
'service': 'myzk', # optional
'principal': '[email protected]' # optional
}
:param read_only: Allow connections to read only servers.
:param randomize_hosts: By default randomize host selection.
:param connection_retry:
A :class:`kazoo.retry.KazooRetry` object to use for
retrying the connection to Zookeeper. Also can be a dict of
options which will be used for creating one.
:param command_retry:
A :class:`kazoo.retry.KazooRetry` object to use for
the :meth:`KazooClient.retry` method. Also can be a dict of
options which will be used for creating one.
:param logger: A custom logger to use instead of the module
global `log` instance.
:param keyfile: SSL keyfile to use for authentication
:param keyfile_password: SSL keyfile password
:param certfile: SSL certfile to use for authentication
:param ca: SSL CA file to use for authentication
:param use_ssl: argument to control whether SSL is used or not
:param verify_certs: when using SSL, argument to bypass
certs verification
Basic Example:
.. code-block:: python
zk = KazooClient()
zk.start()
children = zk.get_children('/')
zk.stop()
As a convenience all recipe classes are available as attributes
and get automatically bound to the client. For example::
zk = KazooClient()
zk.start()
lock = zk.Lock('/lock_path')
.. versionadded:: 0.6
The read_only option. Requires Zookeeper 3.4+
.. versionadded:: 0.6
The retry_max_delay option.
.. versionadded:: 0.6
The randomize_hosts option.
.. versionchanged:: 0.8
Removed the unused watcher argument (was second argument).
.. versionadded:: 1.2
The connection_retry, command_retry and logger options.
.. versionadded:: 2.7
The sasl_options option.
"""
self.logger = logger or log
# Record the handler strategy used
self.handler = handler if handler else SequentialThreadingHandler()
if inspect.isclass(self.handler):
raise ConfigurationError("Handler must be an instance of a class, "
"not the class: %s" % self.handler)
self.auth_data = auth_data if auth_data else set([])
self.default_acl = default_acl
self.randomize_hosts = randomize_hosts
self.hosts = None
self.chroot = None
self.set_hosts(hosts)
self.use_ssl = use_ssl
self.verify_certs = verify_certs
self.certfile = certfile
self.keyfile = keyfile
self.keyfile_password = keyfile_password
self.ca = ca
# Curator like simplified state tracking, and listeners for
# state transitions
self._state = KeeperState.CLOSED
self.state = KazooState.LOST
self.state_listeners = set()
self._child_watchers = defaultdict(set)
self._data_watchers = defaultdict(set)
self._reset()
self.read_only = read_only
if client_id:
self._session_id = client_id[0]
self._session_passwd = client_id[1]
else:
self._reset_session()
# ZK uses milliseconds
self._session_timeout = int(timeout * 1000)
# We use events like twitter's client to track current and
# desired state (connected, and whether to shutdown)
self._live = self.handler.event_object()
self._writer_stopped = self.handler.event_object()
self._stopped = self.handler.event_object()
self._stopped.set()
self._writer_stopped.set()
self.retry = self._conn_retry = None
if type(connection_retry) is dict:
self._conn_retry = KazooRetry(**connection_retry)
elif type(connection_retry) is KazooRetry:
self._conn_retry = connection_retry
if type(command_retry) is dict:
self.retry = KazooRetry(**command_retry)
elif type(command_retry) is KazooRetry:
self.retry = command_retry
if type(self._conn_retry) is KazooRetry:
if self.handler.sleep_func != self._conn_retry.sleep_func:
raise ConfigurationError("Retry handler and event handler "
" must use the same sleep func")
if type(self.retry) is KazooRetry:
if self.handler.sleep_func != self.retry.sleep_func:
raise ConfigurationError(
"Command retry handler and event handler "
"must use the same sleep func")
if self.retry is None or self._conn_retry is None:
old_retry_keys = dict(_RETRY_COMPAT_DEFAULTS)
for key in old_retry_keys:
try:
old_retry_keys[key] = kwargs.pop(key)
warnings.warn(
'Passing retry configuration param %s to the '
'client directly is deprecated, please pass a '
'configured retry object (using param %s)' % (
key, _RETRY_COMPAT_MAPPING[key]),
DeprecationWarning, stacklevel=2)
except KeyError:
pass
retry_keys = {}
for oldname, value in old_retry_keys.items():
retry_keys[_RETRY_COMPAT_MAPPING[oldname]] = value
if self._conn_retry is None:
self._conn_retry = KazooRetry(
sleep_func=self.handler.sleep_func,
**retry_keys)
if self.retry is None:
self.retry = KazooRetry(
sleep_func=self.handler.sleep_func,
**retry_keys)
# Managing legacy SASL options
for scheme, auth in self.auth_data:
if scheme != 'sasl':
continue
if sasl_options:
raise ConfigurationError(
'Multiple SASL configurations provided'
)
warnings.warn(
'Passing SASL configuration as part of the auth_data is '
'deprecated, please use the sasl_options configuration '
'instead', DeprecationWarning, stacklevel=2
)
username, password = auth.split(':')
# Generate an equivalent SASL configuration
sasl_options = {
'username': username,
'password': password,
'mechanism': 'DIGEST-MD5',
'service': 'zookeeper',
'principal': 'zk-sasl-md5',
}
# Cleanup
self.auth_data = set([
(scheme, auth)
for scheme, auth in self.auth_data
if scheme != 'sasl'
])
self._conn_retry.interrupt = lambda: self._stopped.is_set()
self._connection = ConnectionHandler(
self, self._conn_retry.copy(), logger=self.logger,
sasl_options=sasl_options)
# Every retry call should have its own copy of the retry helper
# to avoid shared retry counts
self._retry = self.retry
def _retry(*args, **kwargs):
return self._retry.copy()(*args, **kwargs)
self.retry = _retry
self.Barrier = partial(Barrier, self)
self.Counter = partial(Counter, self)
self.DoubleBarrier = partial(DoubleBarrier, self)
self.ChildrenWatch = partial(ChildrenWatch, self)
self.DataWatch = partial(DataWatch, self)
self.Election = partial(Election, self)
self.NonBlockingLease = partial(NonBlockingLease, self)
self.MultiNonBlockingLease = partial(MultiNonBlockingLease, self)
self.Lock = partial(Lock, self)
self.ReadLock = partial(ReadLock, self)
self.WriteLock = partial(WriteLock, self)
self.Party = partial(Party, self)
self.Queue = partial(Queue, self)
self.LockingQueue = partial(LockingQueue, self)
self.SetPartitioner = partial(SetPartitioner, self)
self.Semaphore = partial(Semaphore, self)
self.ShallowParty = partial(ShallowParty, self)
# If we got any unhandled keywords, complain like Python would
if kwargs:
raise TypeError('__init__() got unexpected keyword arguments: %s'
% (kwargs.keys(),))
def _reset(self):
"""Resets a variety of client states for a new connection."""
self._queue = deque()
self._pending = deque()
self._reset_watchers()
self._reset_session()
self.last_zxid = 0
self._protocol_version = None
def _reset_watchers(self):
watchers = []
for child_watchers in six.itervalues(self._child_watchers):
watchers.extend(child_watchers)
for data_watchers in six.itervalues(self._data_watchers):
watchers.extend(data_watchers)
self._child_watchers = defaultdict(set)
self._data_watchers = defaultdict(set)
ev = WatchedEvent(EventType.NONE, self._state, None)
for watch in watchers:
self.handler.dispatch_callback(Callback("watch", watch, (ev,)))
def _reset_session(self):
self._session_id = None
self._session_passwd = b'\x00' * 16
@property
def client_state(self):
"""Returns the last Zookeeper client state
This is the non-simplified state information and is generally
not as useful as the simplified KazooState information.
"""
return self._state
@property
def client_id(self):
"""Returns the client id for this Zookeeper session if
connected.
:returns: client id which consists of the session id and
password.
:rtype: tuple
"""
if self._live.is_set():
return (self._session_id, self._session_passwd)
return None
@property
def connected(self):
"""Returns whether the Zookeeper connection has been
established."""
return self._live.is_set()
def set_hosts(self, hosts, randomize_hosts=None):
""" sets the list of hosts used by this client.
This function accepts the same format hosts parameter as the init
function and sets the client to use the new hosts the next time it
needs to look up a set of hosts. This function does not affect the
current connected status.
It is not currently possible to change the chroot with this function,
setting a host list with a new chroot will raise a ConfigurationError.
:param hosts: see description in :meth:`KazooClient.__init__`
:param randomize_hosts: override client default for host randomization
:raises:
:exc:`ConfigurationError` if the hosts argument changes the chroot
.. versionadded:: 1.4
.. warning::
Using this function to point a client to a completely disparate
zookeeper server cluster has undefined behavior.
"""
# Change the client setting for randomization if specified
if randomize_hosts is not None:
self.randomize_hosts = randomize_hosts
# Randomizing the list will be done at connect time
self.hosts, chroot = collect_hosts(hosts)
if chroot:
new_chroot = normpath(chroot)
else:
new_chroot = ''
if self.chroot is not None and new_chroot != self.chroot:
raise ConfigurationError("Changing chroot at runtime is not "
"currently supported")
self.chroot = new_chroot
def add_listener(self, listener):
"""Add a function to be called for connection state changes.
This function will be called with a
:class:`~kazoo.protocol.states.KazooState` instance indicating
the new connection state on state transitions.
.. warning::
This function must not block. If its at all likely that it
might need data or a value that could result in blocking
than the :meth:`~kazoo.interfaces.IHandler.spawn` method
should be used so that the listener can return immediately.
"""
if not (listener and callable(listener)):
raise ConfigurationError("listener must be callable")
self.state_listeners.add(listener)
def remove_listener(self, listener):
"""Remove a listener function"""
self.state_listeners.discard(listener)
def _make_state_change(self, state):
# skip if state is current
if self.state == state:
return
self.state = state
# Create copy of listeners for iteration in case one needs to
# remove itself
for listener in list(self.state_listeners):
try:
remove = listener(state)
if remove is True:
self.remove_listener(listener)
except Exception:
self.logger.exception("Error in connection state listener")
def _session_callback(self, state):
if state == self._state:
return
# Note that we don't check self.state == LOST since that's also
# the client's initial state
closed_state = self._state in CLOSED_STATES
self._state = state
# If we were previously closed or had an expired session, and
# are now connecting, don't bother with the rest of the
# transitions since they only apply after
# we've established a connection
if closed_state and state == KeeperState.CONNECTING:
self.logger.log(BLATHER, "Skipping state change")
return
if state in (KeeperState.CONNECTED, KeeperState.CONNECTED_RO):
self.logger.info("Zookeeper connection established, "
"state: %s", state)
self._live.set()
self._make_state_change(KazooState.CONNECTED)
elif state in CLOSED_STATES:
self.logger.info("Zookeeper session closed, state: %s", state)
self._live.clear()
self._make_state_change(KazooState.LOST)
self._notify_pending(state)
self._reset()
else:
self.logger.info("Zookeeper connection lost")
# Connection lost
self._live.clear()
self._notify_pending(state)
self._make_state_change(KazooState.SUSPENDED)
self._reset_watchers()
def _notify_pending(self, state):
"""Used to clear a pending response queue and request queue
during connection drops."""
if state == KeeperState.AUTH_FAILED:
exc = AuthFailedError()
elif state == KeeperState.EXPIRED_SESSION:
exc = SessionExpiredError()
else:
exc = ConnectionLoss()
while True:
try:
request, async_object, xid = self._pending.popleft()
if async_object:
async_object.set_exception(exc)
except IndexError:
break
while True:
try:
request, async_object = self._queue.popleft()
if async_object:
async_object.set_exception(exc)
except IndexError:
break
def _safe_close(self):
self.handler.stop()
timeout = self._session_timeout // 1000
if timeout < 10:
timeout = 10
if not self._connection.stop(timeout):
raise WriterNotClosedException(
"Writer still open from prior connection "
"and wouldn't close after %s seconds" % timeout)
def _call(self, request, async_object):
"""Ensure the client is in CONNECTED or SUSPENDED state and put the
request in the queue if it is.
Returns False if the call short circuits due to AUTH_FAILED,
CLOSED, or EXPIRED_SESSION state.
"""
if self._state == KeeperState.AUTH_FAILED:
async_object.set_exception(AuthFailedError())
return False
elif self._state == KeeperState.CLOSED:
async_object.set_exception(ConnectionClosedError(
"Connection has been closed"))
return False
elif self._state == KeeperState.EXPIRED_SESSION:
async_object.set_exception(SessionExpiredError())
return False
self._queue.append((request, async_object))
# wake the connection, guarding against a race with close()
write_sock = self._connection._write_sock
if write_sock is None:
async_object.set_exception(ConnectionClosedError(
"Connection has been closed"))
try:
write_sock.send(b'\0')
except: # NOQA
async_object.set_exception(ConnectionClosedError(
"Connection has been closed"))
def start(self, timeout=15):
"""Initiate connection to ZK.
:param timeout: Time in seconds to wait for connection to
succeed.
:raises: :attr:`~kazoo.interfaces.IHandler.timeout_exception`
if the connection wasn't established within `timeout`
seconds.
"""
event = self.start_async()
event.wait(timeout=timeout)
if not self.connected:
# We time-out, ensure we are disconnected
self.stop()
self.close()
raise self.handler.timeout_exception("Connection time-out")
if self.chroot and not self.exists("/"):
warnings.warn("No chroot path exists, the chroot path "
"should be created before normal use.")
def start_async(self):
"""Asynchronously initiate connection to ZK.
:returns: An event object that can be checked to see if the
connection is alive.
:rtype: :class:`~threading.Event` compatible object.
"""
# If we're already connected, ignore
if self._live.is_set():
return self._live
# Make sure we're safely closed
self._safe_close()
# We've been asked to connect, clear the stop and our writer
# thread indicator
self._stopped.clear()
self._writer_stopped.clear()
# Start the handler
self.handler.start()
# Start the connection
self._connection.start()
return self._live
def stop(self):
"""Gracefully stop this Zookeeper session.
This method can be called while a reconnection attempt is in
progress, which will then be halted.
Once the connection is closed, its session becomes invalid. All
the ephemeral nodes in the ZooKeeper server associated with the
session will be removed. The watches left on those nodes (and
on their parents) will be triggered.
"""
if self._stopped.is_set():
return
self._stopped.set()
self._queue.append((CloseInstance, None))
try:
self._connection._write_sock.send(b'\0')
finally:
self._safe_close()
def restart(self):
"""Stop and restart the Zookeeper session."""
self.stop()
self.start()
def close(self):
"""Free any resources held by the client.
This method should be called on a stopped client before it is
discarded. Not doing so may result in filehandles being leaked.
.. versionadded:: 1.0
"""
self._connection.close()
def command(self, cmd=b'ruok'):
"""Sent a management command to the current ZK server.
Examples are `ruok`, `envi` or `stat`.
:returns: An unstructured textual response.
:rtype: str
:raises:
:exc:`ConnectionLoss` if there is no connection open, or
possibly a :exc:`socket.error` if there's a problem with
the connection used just for this command.
.. versionadded:: 0.5
"""
if not self._live.is_set():
raise ConnectionLoss("No connection to server")
peer = self._connection._socket.getpeername()[:2]
sock = self.handler.create_connection(
peer, timeout=self._session_timeout / 1000.0,
use_ssl=self.use_ssl,
ca=self.ca,
certfile=self.certfile,
keyfile=self.keyfile,
keyfile_password=self.keyfile_password,
verify_certs=self.verify_certs,
)
sock.sendall(cmd)
result = sock.recv(8192)
sock.close()
return result.decode('utf-8', 'replace')
def server_version(self, retries=3):
"""Get the version of the currently connected ZK server.
:returns: The server version, for example (3, 4, 3).
:rtype: tuple
.. versionadded:: 0.5
"""
def _try_fetch():
data = self.command(b'envi')
data_parsed = {}
for line in data.splitlines():
try:
k, v = line.split("=", 1)
k = k.strip()
v = v.strip()
except ValueError:
pass
else:
if k:
data_parsed[k] = v
version = data_parsed.get(ENVI_VERSION_KEY, '')
version_digits = ENVI_VERSION.match(version).group(1)
try:
return tuple([int(d) for d in version_digits.split('.')])
except ValueError:
return None
def _is_valid(version):
# All zookeeper versions should have at least major.minor
# version numbers; if we get one that doesn't it is likely not
# correct and was truncated...
if version and len(version) > 1:
return True
return False
# Try 1 + retries amount of times to get a version that we know
# will likely be acceptable...
version = _try_fetch()
if _is_valid(version):
return version
for _i in six.moves.range(0, retries):
version = _try_fetch()
if _is_valid(version):
return version
raise KazooException("Unable to fetch useable server"
" version after trying %s times"
% (1 + max(0, retries)))
def add_auth(self, scheme, credential):
"""Send credentials to server.
:param scheme: authentication scheme (default supported:
"digest").
:param credential: the credential -- value depends on scheme.
:returns: True if it was successful.
:rtype: bool
:raises:
:exc:`~kazoo.exceptions.AuthFailedError` if it failed though
the session state will be set to AUTH_FAILED as well.
"""
return self.add_auth_async(scheme, credential).get()
def add_auth_async(self, scheme, credential):
"""Asynchronously send credentials to server. Takes the same
arguments as :meth:`add_auth`.
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
if not isinstance(scheme, string_types):
raise TypeError("Invalid type for 'scheme' (string expected)")
if not isinstance(credential, string_types):
raise TypeError("Invalid type for 'credential' (string expected)")
# we need this auth data to re-authenticate on reconnect
self.auth_data.add((scheme, credential))
async_result = self.handler.async_result()
self._call(Auth(0, scheme, credential), async_result)
return async_result
def unchroot(self, path):
"""Strip the chroot if applicable from the path."""
if not self.chroot:
return path
if self.chroot == path:
return "/"
if path.startswith(self.chroot):
return path[len(self.chroot):]
else:
return path
def sync_async(self, path):
"""Asynchronous sync.
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
"""
async_result = self.handler.async_result()
@wrap(async_result)
def _sync_completion(result):
return self.unchroot(result.get())
def _do_sync():
result = self.handler.async_result()
self._call(
Sync(_prefix_root(self.chroot, path)),
result
)
result.rawlink(_sync_completion)
_do_sync()
return async_result
def sync(self, path):
"""Sync, blocks until response is acknowledged.
Flushes channel between process and leader.
:param path: path of node.
:returns: The node path that was synced.
:raises:
:exc:`~kazoo.exceptions.ZookeeperError` if the server
returns a non-zero error code.
.. versionadded:: 0.5
"""
return self.sync_async(path).get()
def create(self, path, value=b"", acl=None, ephemeral=False,
sequence=False, makepath=False, include_data=False):
"""Create a node with the given value as its data. Optionally
set an ACL on the node.
The ephemeral and sequence arguments determine the type of the
node.
An ephemeral node will be automatically removed by ZooKeeper
when the session associated with the creation of the node
expires.
A sequential node will be given the specified path plus a
suffix `i` where i is the current sequential number of the
node. The sequence number is always fixed length of 10 digits,
0 padded. Once such a node is created, the sequential number
will be incremented by one.
If a node with the same actual path already exists in
ZooKeeper, a NodeExistsError will be raised. Note that since a
different actual path is used for each invocation of creating
sequential nodes with the same path argument, the call will
never raise NodeExistsError.
If the parent node does not exist in ZooKeeper, a NoNodeError
will be raised. Setting the optional `makepath` argument to
`True` will create all missing parent nodes instead.
An ephemeral node cannot have children. If the parent node of
the given path is ephemeral, a NoChildrenForEphemeralsError
will be raised.
This operation, if successful, will trigger all the watches
left on the node of the given path by :meth:`exists` and
:meth:`get` API calls, and the watches left on the parent node
by :meth:`get_children` API calls.
The maximum allowable size of the node value is 1 MB. Values
larger than this will cause a ZookeeperError to be raised.
:param path: Path of node.
:param value: Initial bytes value of node.
:param acl: :class:`~kazoo.security.ACL` list.
:param ephemeral: Boolean indicating whether node is ephemeral
(tied to this session).
:param sequence: Boolean indicating whether path is suffixed
with a unique index.
:param makepath: Whether the path should be created if it
doesn't exist.
:param include_data:
Include the :class:`~kazoo.protocol.states.ZnodeStat` of
the node in addition to its real path. This option changes
the return value to be a tuple of (path, stat).
:returns: Real path of the new node, or tuple if `include_data`
is `True`
:rtype: str
:raises:
:exc:`~kazoo.exceptions.NodeExistsError` if the node
already exists.
:exc:`~kazoo.exceptions.NoNodeError` if parent nodes are
missing.
:exc:`~kazoo.exceptions.NoChildrenForEphemeralsError` if
the parent node is an ephemeral node.
:exc:`~kazoo.exceptions.ZookeeperError` if the provided
value is too large.
:exc:`~kazoo.exceptions.ZookeeperError` if the server
returns a non-zero error code.
.. versionadded:: 1.1
The `makepath` option.
.. versionadded:: 2.7
The `include_data` option.
"""
acl = acl or self.default_acl
return self.create_async(
path, value, acl=acl, ephemeral=ephemeral,
sequence=sequence, makepath=makepath, include_data=include_data
).get()
def create_async(self, path, value=b"", acl=None, ephemeral=False,
sequence=False, makepath=False, include_data=False):
"""Asynchronously create a ZNode. Takes the same arguments as
:meth:`create`.
:rtype: :class:`~kazoo.interfaces.IAsyncResult`
.. versionadded:: 1.1
The makepath option.
.. versionadded:: 2.7
The `include_data` option.
"""
if acl is None and self.default_acl:
acl = self.default_acl
if not isinstance(path, string_types):
raise TypeError("Invalid type for 'path' (string expected)")
if acl and (isinstance(acl, ACL) or
not isinstance(acl, (tuple, list))):
raise TypeError("Invalid type for 'acl' (acl must be a tuple/list"
" of ACL's")
if value is not None and not isinstance(value, bytes_types):
raise TypeError("Invalid type for 'value' (must be a byte string)")
if not isinstance(ephemeral, bool):
raise TypeError("Invalid type for 'ephemeral' (bool expected)")
if not isinstance(sequence, bool):
raise TypeError("Invalid type for 'sequence' (bool expected)")
if not isinstance(makepath, bool):
raise TypeError("Invalid type for 'makepath' (bool expected)")
if not isinstance(include_data, bool):
raise TypeError("Invalid type for 'include_data' (bool expected)")
flags = 0
if ephemeral:
flags |= 1
if sequence:
flags |= 2
if acl is None:
acl = OPEN_ACL_UNSAFE
async_result = self.handler.async_result()