This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathclient.py
1392 lines (1152 loc) · 46.6 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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# python std lib
import json
import logging
import random
import string
import time
# rediscluster imports
from .connection import (
ClusterConnection,
ClusterConnectionPool,
ClusterReadOnlyConnectionPool,
ClusterWithReadReplicasConnectionPool,
SSLClusterConnection,
)
from .exceptions import (
AskError,
ClusterDownError,
ClusterError,
MovedError,
RedisClusterException,
SlotNotCoveredError,
TryAgainError,
)
from .pubsub import ClusterPubSub
from .utils import (
blocked_command,
bool_ok,
dict_merge,
first_key,
merge_result,
parse_cluster_nodes,
parse_cluster_slots,
parse_pubsub_channels,
parse_pubsub_numpat,
parse_pubsub_numsub,
string_keys_to_dict,
)
# 3rd party imports
from redis import Redis
from redis.client import list_or_args, parse_info
from redis.connection import Connection, SSLConnection
from redis._compat import iteritems, nativestr, long
from redis.exceptions import (
BusyLoadingError,
ConnectionError,
RedisError,
ResponseError,
TimeoutError,
)
# Not complete, but covers the major ones
# https://redis.io/commands
READ_COMMANDS = frozenset([
"BITCOUNT",
"BITPOS",
"EXISTS",
"GEODIST",
"GEOHASH",
"GEOPOS",
"GEORADIUS",
"GEORADIUSBYMEMBER",
"GET",
"GETBIT",
"GETRANGE",
"HEXISTS",
"HGET",
"HGETALL",
"HKEYS",
"HLEN",
"HMGET",
"HSTRLEN",
"HVALS",
"KEYS",
"LINDEX",
"LLEN",
"LRANGE",
"MGET",
"PTTL",
"RANDOMKEY",
"SCARD",
"SDIFF",
"SINTER",
"SISMEMBER",
"SMEMBERS",
"SRANDMEMBER",
"STRLEN",
"SUNION",
"TTL",
"ZCARD",
"ZCOUNT",
"ZRANGE",
"ZSCORE",
])
log = logging.getLogger(__name__)
class CaseInsensitiveDict(dict):
"Case insensitive dict implementation. Assumes string keys only."
def __init__(self, data):
for k, v in iteritems(data):
self[k.upper()] = v
def __contains__(self, k):
return super(CaseInsensitiveDict, self).__contains__(k.upper())
def __delitem__(self, k):
super(CaseInsensitiveDict, self).__delitem__(k.upper())
def __getitem__(self, k):
return super(CaseInsensitiveDict, self).__getitem__(k.upper())
def get(self, k, default=None):
return super(CaseInsensitiveDict, self).get(k.upper(), default)
def __setitem__(self, k, v):
super(CaseInsensitiveDict, self).__setitem__(k.upper(), v)
def update(self, data):
data = CaseInsensitiveDict(data)
super(CaseInsensitiveDict, self).update(data)
class RedisCluster(Redis):
"""
If a command is implemented over the one in Redis then it requires some changes compared to
the regular implementation of the method.
"""
RedisClusterRequestTTL = 16
NODES_FLAGS = dict_merge(
string_keys_to_dict([
'ACL CAT',
'ACL DELUSER',
'ACL GENPASS',
'ACL GETUSER',
'ACL HELP',
'ACL LIST',
'ACL LOAD',
'ACL LOG',
'ACL SAVE',
'ACL SETUSER',
'ACL USERS',
'ACL WHOAMI',
'BITOP',
'MOVE',
'SCRIPT KILL',
'SENTINEL GET-MASTER-ADDR-BY-NAME',
'SENTINEL MASTER',
'SENTINEL MASTERS',
'SENTINEL MONITOR',
'SENTINEL REMOVE',
'SENTINEL SENTINELS',
'SENTINEL SET',
'SENTINEL SLAVES',
'SHUTDOWN',
'SLAVEOF',
'WAIT',
], 'blocked'),
string_keys_to_dict([
"BGREWRITEAOF",
"BGSAVE",
"CLIENT GETNAME",
"CLIENT ID",
"CLIENT KILL",
"CLIENT LIST",
"CLIENT SETNAME",
"CLUSTER INFO",
"CONFIG GET",
"CONFIG RESETSTAT",
"CONFIG REWRITE",
"CONFIG SET",
"DBSIZE",
"ECHO",
"INFO",
"KEYS",
"LASTSAVE",
"PING",
"PUBSUB CHANNELS",
"PUBSUB NUMPAT",
"PUBSUB NUMSUB",
"SAVE",
"SLOWLOG GET",
"SLOWLOG LEN",
"SLOWLOG RESET",
"TIME",
], 'all-nodes'),
string_keys_to_dict([
"FLUSHALL",
"FLUSHDB",
"SCAN",
"SCRIPT EXISTS",
"SCRIPT FLUSH",
"SCRIPT LOAD",
"SCRIPT DEBUG",
], 'all-masters'),
string_keys_to_dict([
"CLUSTER NODES",
"CLUSTER SLOTS",
"RANDOMKEY",
], 'random'),
string_keys_to_dict([
"CLUSTER COUNTKEYSINSLOT",
"CLUSTER GETKEYSINSLOT",
], 'slot-id'),
)
RESULT_CALLBACKS = dict_merge(
string_keys_to_dict([
"BGREWRITEAOF",
"BGSAVE",
"CLIENT GETNAME",
"CLIENT ID",
"CLIENT KILL",
"CLIENT LIST",
"CLIENT SETNAME",
"CLUSTER INFO",
"CONFIG GET",
"CONFIG RESETSTAT",
"CONFIG REWRITE",
"CONFIG SET",
"DBSIZE",
"ECHO",
"FLUSHALL",
"FLUSHDB",
"INFO",
"LASTSAVE",
"PING",
"SAVE",
"SCAN",
"SLOWLOG GET",
"SLOWLOG LEN",
"SLOWLOG RESET",
"TIME",
'CLUSTER ADDSLOTS',
'CLUSTER COUNT-FAILURE-REPORTS',
'CLUSTER DELSLOTS',
'CLUSTER FAILOVER',
'CLUSTER FORGET',
], lambda command, res: res),
string_keys_to_dict([
"SCRIPT LOAD",
], lambda command, res: list(res.values()).pop()),
string_keys_to_dict([
"SCRIPT EXISTS",
], lambda command, res: [all(k) for k in zip(*res.values())]),
string_keys_to_dict([
"SCRIPT FLUSH",
], lambda command, res: all(res.values())),
string_keys_to_dict([
"KEYS",
], merge_result),
string_keys_to_dict([
"HSCAN",
"RANDOMKEY",
"SSCAN",
"ZSCAN",
], first_key),
string_keys_to_dict([
"PUBSUB CHANNELS",
], parse_pubsub_channels),
string_keys_to_dict([
"PUBSUB NUMSUB",
], parse_pubsub_numsub),
string_keys_to_dict([
"PUBSUB NUMPAT",
], parse_pubsub_numpat),
)
CLUSTER_COMMANDS_RESPONSE_CALLBACKS = {
'CLUSTER ADDSLOTS': bool_ok,
'CLUSTER COUNT-FAILURE-REPORTS': int,
'CLUSTER COUNTKEYSINSLOT': int,
'CLUSTER DELSLOTS': bool_ok,
'CLUSTER FAILOVER': bool_ok,
'CLUSTER FORGET': bool_ok,
'CLUSTER GETKEYSINSLOT': list,
'CLUSTER INFO': parse_info,
'CLUSTER KEYSLOT': int,
'CLUSTER MEET': bool_ok,
'CLUSTER NODES': parse_cluster_nodes,
'CLUSTER REPLICATE': bool_ok,
'CLUSTER RESET': bool_ok,
'CLUSTER SAVECONFIG': bool_ok,
'CLUSTER SET-CONFIG-EPOCH': bool_ok,
'CLUSTER SETSLOT': bool_ok,
'CLUSTER SLAVES': parse_cluster_nodes,
'CLUSTER SLOTS': parse_cluster_slots,
'ASKING': bool_ok,
'READONLY': bool_ok,
'READWRITE': bool_ok,
}
def __init__(self, host=None, port=None, startup_nodes=None, max_connections=None, max_connections_per_node=False, init_slot_cache=True,
readonly_mode=False, reinitialize_steps=None, skip_full_coverage_check=False, nodemanager_follow_cluster=False,
connection_class=None, read_from_replicas=False, cluster_down_retry_attempts=3, host_port_remap=None, **kwargs):
"""
:startup_nodes:
List of nodes that initial bootstrapping can be done from
:host:
Can be used to point to a startup node
:port:
Can be used to point to a startup node
:max_connections:
Maximum number of connections that should be kept open at one time
:readonly_mode:
enable READONLY mode. You can read possibly stale data from slave.
:skip_full_coverage_check:
Skips the check of cluster-require-full-coverage config, useful for clusters
without the CONFIG command (like aws)
:nodemanager_follow_cluster:
The node manager will during initialization try the last set of nodes that
it was operating on. This will allow the client to drift along side the cluster
if the cluster nodes move around alot.
:**kwargs:
Extra arguments that will be sent into Redis instance when created
(See Official redis-py doc for supported kwargs
[https://github.com/andymccurdy/redis-py/blob/master/redis/client.py])
Some kwargs is not supported and will raise RedisClusterException
- db (Redis do not support database SELECT in cluster mode)
"""
# Tweaks to Redis client arguments when running in cluster mode
log.info("Created new instance of RedisCluster client instance")
log.debug("startup_nodes : " + json.dumps(startup_nodes, indent=2))
if "db" in kwargs:
raise RedisClusterException("Argument 'db' is not possible to use in cluster mode")
# Needs to be removed to avoid exception in redis Connection init
if kwargs.pop('ssl', False):
log.info("Patching connection_class to SSLClusterConnection")
connection_class = SSLClusterConnection
if "connection_pool" in kwargs:
log.info("Using custom created connection pool")
pool = kwargs.pop('connection_pool')
else:
startup_nodes = [] if startup_nodes is None else startup_nodes
# Support host/port as argument
if host:
startup_nodes.append({"host": host, "port": port if port else 7000})
if readonly_mode:
connection_pool_cls = ClusterReadOnlyConnectionPool
log.info("Using ClusterReadOnlyConnectionPool")
elif read_from_replicas:
connection_pool_cls = ClusterWithReadReplicasConnectionPool
log.info("Using ClusterWithReadReplicasConnectionPool")
else:
connection_pool_cls = ClusterConnectionPool
log.info("Using ClusterConnectionPool")
log.debug("Connection pool class " + str(connection_pool_cls))
# If connection pool fails to initialize, parent class (Redis) __del__
# will try to access self.connection before it's defined
# throwing an AttributeError.
self.connection = None
pool = connection_pool_cls(
startup_nodes=startup_nodes,
init_slot_cache=init_slot_cache,
max_connections=max_connections,
reinitialize_steps=reinitialize_steps,
max_connections_per_node=max_connections_per_node,
skip_full_coverage_check=skip_full_coverage_check,
nodemanager_follow_cluster=nodemanager_follow_cluster,
connection_class=connection_class,
host_port_remap=host_port_remap,
**kwargs
)
super(RedisCluster, self).__init__(connection_pool=pool, **kwargs)
self.refresh_table_asap = False
self.nodes_flags = self.__class__.NODES_FLAGS.copy()
self.result_callbacks = self.__class__.RESULT_CALLBACKS.copy()
self.response_callbacks = CaseInsensitiveDict(self.__class__.RESPONSE_CALLBACKS)
self.response_callbacks = CaseInsensitiveDict(dict_merge(self.response_callbacks, self.CLUSTER_COMMANDS_RESPONSE_CALLBACKS))
self.read_from_replicas = read_from_replicas
self.cluster_down_retry_attempts = cluster_down_retry_attempts
@classmethod
def from_url(cls, url, db=None, skip_full_coverage_check=False, readonly_mode=False, read_from_replicas=False, **kwargs):
"""
Return a Redis client object configured from the given URL, which must
use either `the ``redis://`` scheme
<http://www.iana.org/assignments/uri-schemes/prov/redis>`_ for RESP
connections or the ``unix://`` scheme for Unix domain sockets.
For example::
redis://[:password]@localhost:6379/0
unix://[:password]@/path/to/socket.sock?db=0
There are several ways to specify a database number. The parse function
will return the first specified option:
1. A ``db`` querystring option, e.g. redis://localhost?db=0
2. If using the redis:// scheme, the path argument of the url, e.g.
redis://localhost/0
3. The ``db`` argument to this function.
If none of these options are specified, db=0 is used.
Any additional querystring arguments and keyword arguments will be
passed along to the ConnectionPool class's initializer. In the case
of conflicting arguments, querystring arguments always win.
"""
if url.lower().startswith('unix://'):
raise RedisClusterException('Unix sockets do not work in a cluster environment')
if readonly_mode:
connection_pool_cls = ClusterReadOnlyConnectionPool
elif read_from_replicas:
connection_pool_cls = ClusterWithReadReplicasConnectionPool
else:
connection_pool_cls = ClusterConnectionPool
connection_pool = connection_pool_cls.from_url(url, db=db, skip_full_coverage_check=skip_full_coverage_check, **kwargs)
if connection_pool.connection_class == SSLConnection:
connection_pool.connection_class = SSLClusterConnection
if connection_pool.connection_class == Connection:
connection_pool.connection_class = ClusterConnection
return cls(connection_pool=connection_pool, skip_full_coverage_check=skip_full_coverage_check)
def __repr__(self):
"""
"""
servers = list({'{0}:{1}'.format(nativestr(info['host']), info['port']) for info in self.connection_pool.nodes.startup_nodes})
servers.sort()
return "{0}<{1}>".format(type(self).__name__, ', '.join(servers))
def set_result_callback(self, command, callback):
"Set a custom Result Callback"
self.result_callbacks[command] = callback
def pubsub(self, **kwargs):
"""
"""
return ClusterPubSub(self.connection_pool, **kwargs)
def pipeline(self, transaction=None, shard_hint=None, read_from_replicas=False):
"""
Cluster impl:
Pipelines do not work in cluster mode the same way they do in normal mode.
Create a clone of this object so that simulating pipelines will work correctly.
Each command will be called directly when used and when calling execute() will only return the result stack.
"""
if shard_hint:
raise RedisClusterException("shard_hint is deprecated in cluster mode")
if transaction:
raise RedisClusterException("transaction is deprecated in cluster mode")
return ClusterPipeline(
connection_pool=self.connection_pool,
startup_nodes=self.connection_pool.nodes.startup_nodes,
result_callbacks=self.result_callbacks,
response_callbacks=self.response_callbacks,
cluster_down_retry_attempts=self.cluster_down_retry_attempts,
read_from_replicas=read_from_replicas,
)
def transaction(self, *args, **kwargs):
"""
Transaction is not implemented in cluster mode yet.
"""
raise RedisClusterException("method RedisCluster.transaction() is not implemented")
def _determine_slot(self, *args):
"""
figure out what slot based on command and args
"""
if len(args) <= 1:
raise RedisClusterException("No way to dispatch this command to Redis Cluster. Missing key.")
command = args[0]
if command in ['EVAL', 'EVALSHA']:
numkeys = args[2]
keys = args[3: 3 + numkeys]
slots = {self.connection_pool.nodes.keyslot(key) for key in keys}
if len(slots) != 1:
raise RedisClusterException("{0} - all keys must map to the same key slot".format(command))
return slots.pop()
if command in ['XREADGROUP', 'XREAD']:
stream_idx = args.index(b'STREAMS')
keys_ids = list(args[stream_idx + 1:])
idx_split = len(keys_ids) // 2
keys = keys_ids[: idx_split]
slots = {self.connection_pool.nodes.keyslot(key) for key in keys}
if len(slots) != 1:
raise RedisClusterException("{0} - all keys must map to the same key slot".format(command))
return slots.pop()
key = args[1]
# OBJECT command uses a special keyword as first positional argument
if command == 'OBJECT':
key = args[2]
return self.connection_pool.nodes.keyslot(key)
def _merge_result(self, command, res, **kwargs):
"""
`res` is a dict with the following structure Dict(NodeName, CommandResult)
"""
if command in self.result_callbacks:
return self.result_callbacks[command](command, res)
# Default way to handle result
return first_key(command, res)
def determine_node(self, *args, **kwargs):
"""
"""
command = args[0]
node_flag = self.nodes_flags.get(command)
if node_flag == 'blocked':
return blocked_command(self, command)
elif node_flag == 'random':
return [self.connection_pool.nodes.random_node()]
elif node_flag == 'all-masters':
return self.connection_pool.nodes.all_masters()
elif node_flag == 'all-nodes':
return self.connection_pool.nodes.all_nodes()
elif node_flag == 'slot-id':
return [self.connection_pool.nodes.node_from_slot(args[1])]
else:
return None
def execute_command(self, *args, **kwargs):
"""
Wrapper for CLUSTERDOWN error handling.
If the cluster reports it is down it is assumed that:
- connection_pool was disconnected
- connection_pool was reseted
- refereh_table_asap set to True
It will try the number of times specified by the config option "self.cluster_down_retry_attempts"
which defaults to 3 unless manually configured.
If it reaches the number of times, the command will raises ClusterDownException.
"""
for _ in range(0, self.cluster_down_retry_attempts):
try:
return self._execute_command(*args, **kwargs)
except ClusterDownError:
# Try again with the new cluster setup. All other errors
# should be raised.
pass
# If it fails the configured number of times then raise exception back to caller of this method
raise ClusterDownError("CLUSTERDOWN error. Unable to rebuild the cluster")
def _execute_command(self, *args, **kwargs):
"""
Send a command to a node in the cluster
"""
if not args:
raise RedisClusterException("Unable to determine command to use")
command = args[0]
log.debug("Command to execute : " + str(command) + " : " + str(args) + " : " + str(kwargs))
# If set externally we must update it before calling any commands
if self.refresh_table_asap:
self.connection_pool.nodes.initialize()
self.refresh_table_asap = False
node = self.determine_node(*args, **kwargs)
if node:
return self._execute_command_on_nodes(node, *args, **kwargs)
redirect_addr = None
asking = False
is_read_replica = False
try_random_node = False
slot = self._determine_slot(*args)
ttl = int(self.RedisClusterRequestTTL)
connection_error_retry_counter = 0
while ttl > 0:
ttl -= 1
connection = None
try:
if asking:
node = self.connection_pool.nodes.nodes[redirect_addr]
connection = self.connection_pool.get_connection_by_node(node)
elif try_random_node:
connection = self.connection_pool.get_random_connection()
try_random_node = False
else:
if self.refresh_table_asap:
# MOVED
node = self.connection_pool.get_master_node_by_slot(slot)
self.refresh_table_asap = False
else:
node = self.connection_pool.get_node_by_slot(
slot,
self.read_from_replicas and (command in READ_COMMANDS)
)
is_read_replica = node['server_type'] == 'slave'
connection = self.connection_pool.get_connection_by_node(node)
log.debug("Determined node to execute : " + str(node))
if asking:
connection.send_command('ASKING')
self.parse_response(connection, "ASKING", **kwargs)
asking = False
if is_read_replica:
# Ask read replica to accept reads (see https://redis.io/commands/readonly)
# TODO: do we need to handle errors from this response?
connection.send_command('READONLY')
self.parse_response(connection, 'READONLY', **kwargs)
is_read_replica = False
connection.send_command(*args)
return self.parse_response(connection, command, **kwargs)
except SlotNotCoveredError as e:
log.exception("SlotNotCoveredError")
# In some cases during failover to a replica is happening
# a slot sometimes is not covered by the cluster layout and
# we need to attempt to refresh the cluster layout and try again
self.refresh_table_asap = True
time.sleep(0.1)
# This is the last attempt before we run out of TTL, raise the exception
if ttl == 1:
raise e
except (RedisClusterException, BusyLoadingError):
log.exception("RedisClusterException || BusyLoadingError")
raise
except ConnectionError:
log.exception("ConnectionError")
# ConnectionError can also be raised if we couldn't get a connection
# from the pool before timing out, so check that this is an actual
# connection before attempting to disconnect.
if connection is not None:
connection.disconnect()
connection_error_retry_counter += 1
# Give the node 0.1 seconds to get back up and retry again with same
# node and configuration. After 5 attempts then try to reinitialize
# the cluster and see if the nodes configuration has changed or not
if connection_error_retry_counter < 5:
time.sleep(0.25)
else:
# Reset the counter back to 0 as it should have 5 new attempts
# after the client tries to reinitailize the cluster setup to the
# new configuration.
connection_error_retry_counter = 0
self.refresh_table_asap = True
# Hard force of reinitialize of the node/slots setup
self.connection_pool.nodes.increment_reinitialize_counter(
count=self.connection_pool.nodes.reinitialize_steps,
)
except TimeoutError:
log.exception("TimeoutError")
connection.disconnect()
if ttl < self.RedisClusterRequestTTL / 2:
time.sleep(0.05)
else:
try_random_node = True
except ClusterDownError as e:
log.exception("ClusterDownError")
self.connection_pool.disconnect()
self.connection_pool.reset()
self.refresh_table_asap = True
connection = None
raise e
except MovedError as e:
# Reinitialize on ever x number of MovedError.
# This counter will increase faster when the same client object
# is shared between multiple threads. To reduce the frequency you
# can set the variable 'reinitialize_steps' in the constructor.
log.exception("MovedError")
self.refresh_table_asap = True
self.connection_pool.nodes.increment_reinitialize_counter()
node = self.connection_pool.nodes.set_node(e.host, e.port, server_type='master')
self.connection_pool.nodes.slots[e.slot_id][0] = node
except TryAgainError:
log.exception("TryAgainError")
if ttl < self.RedisClusterRequestTTL / 2:
time.sleep(0.05)
except AskError as e:
log.exception("AskError")
redirect_addr, asking = "{0}:{1}".format(e.host, e.port), True
except BaseException as e:
log.exception("BaseException")
connection.disconnect()
raise e
finally:
if connection is not None:
self.connection_pool.release(connection)
log.debug("TTL loop : " + str(ttl))
raise ClusterError('TTL exhausted.')
def _execute_command_on_nodes(self, nodes, *args, **kwargs):
"""
"""
command = args[0]
res = {}
for node in nodes:
connection = self.connection_pool.get_connection_by_node(node)
# copy from redis-py
try:
connection.send_command(*args)
res[node["name"]] = self.parse_response(connection, command, **kwargs)
except (ConnectionError, TimeoutError) as e:
connection.disconnect()
if not connection.retry_on_timeout and isinstance(e, TimeoutError):
raise
connection.send_command(*args)
res[node["name"]] = self.parse_response(connection, command, **kwargs)
except ClusterDownError:
self.connection_pool.disconnect()
self.connection_pool.reset()
self.refresh_table_asap = True
raise
finally:
self.connection_pool.release(connection)
return self._merge_result(command, res, **kwargs)
##########
# Cluster management commands
def _nodes_slots_to_slots_nodes(self, mapping):
"""
Converts a mapping of
{id: <node>, slots: (slot1, slot2)}
to
{slot1: <node>, slot2: <node>}
Operation is expensive so use with caution
"""
out = {}
for node in mapping:
for slot in node['slots']:
out[str(slot)] = node['id']
return out
def cluster_addslots(self, node_id, *slots):
"""
Assign new hash slots to receiving node
Sends to specified node
"""
return self.execute_command('CLUSTER ADDSLOTS', *slots, node_id=node_id)
def cluster_countkeysinslot(self, slot_id):
"""
Return the number of local keys in the specified hash slot
Send to node based on specified slot_id
"""
return self.execute_command('CLUSTER COUNTKEYSINSLOT', slot_id)
def cluster_count_failure_report(self, node_id):
"""
Return the number of failure reports active for a given node
Sends to specified node
"""
return self.execute_command('CLUSTER COUNT-FAILURE-REPORTS', node_id=node_id)
def cluster_delslots(self, *slots):
"""
Set hash slots as unbound in the cluster.
It determines by it self what node the slot is in and sends it there
Returns a list of the results for each processed slot.
"""
cluster_nodes = self._nodes_slots_to_slots_nodes(self.cluster_nodes())
return [
self.execute_command('CLUSTER DELSLOTS', slot, node_id=cluster_nodes[slot])
for slot in slots
]
def cluster_failover(self, node_id, option=None):
"""
Forces a slave to perform a manual failover of its master
Sends to specified node
"""
if option:
if option.upper() not in ['FORCE', 'TAKEOVER']:
raise RedisError('Invalid option for CLUSTER FAILOVER command: {0}'.format(option))
else:
return self.execute_command('CLUSTER FAILOVER', option, node_id=node_id)
else:
return self.execute_command('CLUSTER FAILOVER', node_id=node_id)
def cluster_info(self):
"""
Provides info about Redis Cluster node state
Sends to random node in the cluster
"""
return self.execute_command('CLUSTER INFO')
def cluster_keyslot(self, name):
"""
Returns the hash slot of the specified key
Sends to random node in the cluster
"""
return self.execute_command('CLUSTER KEYSLOT', name)
def cluster_meet(self, node_id, host, port):
"""
Force a node cluster to handshake with another node.
Sends to specified node
"""
return self.execute_command('CLUSTER MEET', host, port, node_id=node_id)
def cluster_nodes(self):
"""
Force a node cluster to handshake with another node
Sends to random node in the cluster
"""
return self.execute_command('CLUSTER NODES')
def cluster_replicate(self, target_node_id):
"""
Reconfigure a node as a slave of the specified master node
Sends to specified node
"""
return self.execute_command('CLUSTER REPLICATE', target_node_id)
def cluster_reset(self, node_id, soft=True):
"""
Reset a Redis Cluster node
If 'soft' is True then it will send 'SOFT' argument
If 'soft' is False then it will send 'HARD' argument
Sends to specified node
"""
return self.execute_command('CLUSTER RESET', b'SOFT' if soft else b'HARD', node_id=node_id)
def cluster_reset_all_nodes(self, soft=True):
"""
Send CLUSTER RESET to all nodes in the cluster
If 'soft' is True then it will send 'SOFT' argument
If 'soft' is False then it will send 'HARD' argument
Sends to all nodes in the cluster
"""
return [
self.execute_command(
'CLUSTER RESET',
'SOFT' if soft else 'HARD',
node_id=node['id'],
)
for node in self.cluster_nodes()
]
def cluster_save_config(self):
"""
Forces the node to save cluster state on disk
Sends to all nodes in the cluster
"""
return self.execute_command('CLUSTER SAVECONFIG')
def cluster_get_keys_in_slot(self, slot, num_keys):
"""
Returns the number of keys in the specified cluster slot
"""
return self.execute_command('CLUSTER GETKEYSINSLOT', slot, num_keys)
def cluster_set_config_epoch(self, node_id, epoch):
"""
Set the configuration epoch in a new node
Sends to specified node
"""
return self.execute_command('CLUSTER SET-CONFIG-EPOCH', epoch, node_id=node_id)
# TODO: Determine what the purpose of bind_to_node_ip is going to be
def cluster_setslot(self, node_id, slot_id, state, bind_to_node_id=None):
"""
Bind an hash slot to a specific node
Sends to specified node
"""
if state.upper() in ('IMPORTING', 'MIGRATING', 'NODE') and node_id is not None:
return self.execute_command('CLUSTER SETSLOT', slot_id, state, node_id)
elif state.upper() == 'STABLE':
return self.execute_command('CLUSTER SETSLOT', slot_id, 'STABLE')
else:
raise RedisError('Invalid slot state: {0}'.format(state))
def cluster_slaves(self, target_node_id):
"""
Force a node cluster to handshake with another node
Sends to targeted cluster node
"""
return self.execute_command('CLUSTER SLAVES', target_node_id)
def cluster_slots(self):
"""
Get array of Cluster slot to node mappings
Sends to random node in the cluster
"""
return self.execute_command('CLUSTER SLOTS')
##########
# All methods that must have custom implementation
def client_kill_filter(self, _id=None, _type=None, addr=None, skipme=None):
raise NotImplementedError('Method not yet implemented')
def _parse_scan(self, response, **options):
"""
Borrowed from redis-py::client.py
"""
cursor, r = response
return long(cursor), r
def scan_iter(self, match=None, count=None, _type=None):
"""
Make an iterator using the SCAN command so that the client doesn't
need to remember the cursor position.
``match`` allows for filtering the keys by pattern
``count`` allows for hint the minimum number of returns
Cluster impl:
Result from SCAN is different in cluster mode.
"""
cursors = {}
nodeData = {}
for master_node in self.connection_pool.nodes.all_masters():
cursors[master_node["name"]] = "0"
nodeData[master_node["name"]] = master_node
while not all(cursors[node] == 0 for node in cursors):
for node in cursors:
if cursors[node] == 0:
continue
conn = self.connection_pool.get_connection_by_node(nodeData[node])
pieces = ['SCAN', cursors[node]]
if match is not None:
pieces.extend([b'MATCH', match])
if count is not None:
pieces.extend([b'COUNT', count])
if _type is not None:
pieces.extend([b'TYPE', _type])
conn.send_command(*pieces)
raw_resp = conn.read_response()
# if you don't release the connection, the driver will make another, and you will hate your life
self.connection_pool.release(conn)
cur, resp = self._parse_scan(raw_resp)
cursors[node] = cur
for r in resp: