|
11 | 11 | try: |
12 | 12 | from jsonpickle import decode, encode |
13 | 13 | from redis import StrictRedis |
| 14 | + from redis.sentinel import Sentinel |
14 | 15 | except ImportError: |
15 | 16 | def missing_redis_dependencies(*args, **kwargs): |
16 | 17 | raise NotImplementedError('Missing Redis support dependencies.') |
@@ -38,6 +39,10 @@ def missing_redis_dependencies(*args, **kwargs): |
38 | 39 | } |
39 | 40 |
|
40 | 41 |
|
| 42 | +class SentinelConfigurationException(Exception): |
| 43 | + pass |
| 44 | + |
| 45 | + |
41 | 46 | class RedisSegmentCache(SegmentCache): |
42 | 47 | ''' |
43 | 48 | ''' |
@@ -66,21 +71,25 @@ def register_segment(self, segment_name): |
66 | 71 | :param segment_name: Name of the segment. |
67 | 72 | :type segment_name: str |
68 | 73 | ''' |
69 | | - self._redis.sadd( |
70 | | - RedisSegmentCache._KEY_TEMPLATE.format(suffix='registered'), |
71 | | - segment_name |
72 | | - ) |
| 74 | + # self._redis.sadd( |
| 75 | + # RedisSegmentCache._KEY_TEMPLATE.format(suffix='registered'), |
| 76 | + # segment_name |
| 77 | + # ) |
| 78 | + # @TODO The Segment logic for redis should be removed. |
| 79 | + pass |
73 | 80 |
|
74 | 81 | def unregister_segment(self, segment_name): |
75 | 82 | ''' |
76 | 83 | Unregister a segment from the automatic update process. |
77 | 84 | :param segment_name: Name of the segment. |
78 | 85 | :type segment_name: str |
79 | 86 | ''' |
80 | | - self._redis.srem( |
81 | | - RedisSegmentCache._KEY_TEMPLATE.format(suffix='registered'), |
82 | | - segment_name |
83 | | - ) |
| 87 | + # self._redis.srem( |
| 88 | + # RedisSegmentCache._KEY_TEMPLATE.format(suffix='registered'), |
| 89 | + # segment_name |
| 90 | + # ) |
| 91 | + # @TODO The Segment logic for redis should be removed. |
| 92 | + pass |
84 | 93 |
|
85 | 94 | def get_registered_segments(self): |
86 | 95 | ''' |
@@ -799,8 +808,11 @@ def get_redis(config): |
799 | 808 | config['redisFactory'], 'redisFactory' |
800 | 809 | ) |
801 | 810 | return redis_factory() |
802 | | - |
803 | | - return default_redis_factory(config) |
| 811 | + else: |
| 812 | + if 'redisSentinels' in config: |
| 813 | + return default_redis_sentinel_factory(config) |
| 814 | + else: |
| 815 | + return default_redis_factory(config) |
804 | 816 |
|
805 | 817 |
|
806 | 818 | def default_redis_factory(config): |
@@ -860,3 +872,82 @@ def default_redis_factory(config): |
860 | 872 | max_connections=max_connections |
861 | 873 | ) |
862 | 874 | return PrefixDecorator(redis, prefix=prefix) |
| 875 | + |
| 876 | + |
| 877 | +def default_redis_sentinel_factory(config): |
| 878 | + ''' |
| 879 | + Default redis client factory for sentinel mode. |
| 880 | + :param config: A dict with the Redis configuration parameters |
| 881 | + :type config: dict |
| 882 | + :return: A Sentinel object using the provided config values |
| 883 | + :rtype: Sentinel |
| 884 | + ''' |
| 885 | + sentinels = config.get('redisSentinels') |
| 886 | + |
| 887 | + if (sentinels is None): |
| 888 | + raise SentinelConfigurationException('redisSentinels must be specified.') |
| 889 | + if (not isinstance(sentinels, list)): |
| 890 | + raise SentinelConfigurationException('Sentinels must be an array of elements in the form of' |
| 891 | + ' [(ip, port)].') |
| 892 | + if (len(sentinels) == 0): |
| 893 | + raise SentinelConfigurationException('It must be at least one sentinel.') |
| 894 | + if not all(isinstance(s, tuple) for s in sentinels): |
| 895 | + raise SentinelConfigurationException('Sentinels must respect the tuple structure' |
| 896 | + '[(ip, port)].') |
| 897 | + |
| 898 | + master_service = config.get('redisMasterService') |
| 899 | + |
| 900 | + if (master_service is None): |
| 901 | + raise SentinelConfigurationException('redisMasterService must be specified.') |
| 902 | + |
| 903 | + db = config.get('redisDb', 0) |
| 904 | + password = config.get('redisPassword', None) |
| 905 | + socket_timeout = config.get('redisSocketTimeout', None) |
| 906 | + socket_connect_timeout = config.get('redisSocketConnectTimeout', None) |
| 907 | + socket_keepalive = config.get('redisSocketKeepalive', None) |
| 908 | + socket_keepalive_options = config.get('redisSocketKeepaliveOptions', None) |
| 909 | + connection_pool = config.get('redisConnectionPool', None) |
| 910 | + unix_socket_path = config.get('redisUnixSocketPath', None) |
| 911 | + encoding = config.get('redisEncoding', 'utf-8') |
| 912 | + encoding_errors = config.get('redisEncodingErrors', 'strict') |
| 913 | + charset = config.get('redisCharset', None) |
| 914 | + errors = config.get('redisErrors', None) |
| 915 | + decode_responses = config.get('redisDecodeResponses', False) |
| 916 | + retry_on_timeout = config.get('redisRetryOnTimeout', False) |
| 917 | + ssl = config.get('redisSsl', False) |
| 918 | + ssl_keyfile = config.get('redisSslKeyfile', None) |
| 919 | + ssl_certfile = config.get('redisSslCertfile', None) |
| 920 | + ssl_cert_reqs = config.get('redisSslCertReqs', None) |
| 921 | + ssl_ca_certs = config.get('redisSslCaCerts', None) |
| 922 | + max_connections = config.get('redisMaxConnections', None) |
| 923 | + prefix = config.get('redisPrefix') |
| 924 | + |
| 925 | + sentinel = Sentinel( |
| 926 | + sentinels, |
| 927 | + 0, |
| 928 | + { |
| 929 | + 'db': db, |
| 930 | + 'password': password, |
| 931 | + 'socket_timeout': socket_timeout, |
| 932 | + 'socket_connect_timeout': socket_connect_timeout, |
| 933 | + 'socket_keepalive': socket_keepalive, |
| 934 | + 'socket_keepalive_options': socket_keepalive_options, |
| 935 | + 'connection_pool': connection_pool, |
| 936 | + 'unix_socket_path': unix_socket_path, |
| 937 | + 'encoding': encoding, |
| 938 | + 'encoding_errors': encoding_errors, |
| 939 | + 'charset': charset, |
| 940 | + 'errors': errors, |
| 941 | + 'decode_responses': decode_responses, |
| 942 | + 'retry_on_timeout': retry_on_timeout, |
| 943 | + 'ssl': ssl, |
| 944 | + 'ssl_keyfile': ssl_keyfile, |
| 945 | + 'ssl_certfile': ssl_certfile, |
| 946 | + 'ssl_cert_reqs': ssl_cert_reqs, |
| 947 | + 'ssl_ca_certs': ssl_ca_certs, |
| 948 | + 'max_connections': max_connections |
| 949 | + } |
| 950 | + ) |
| 951 | + |
| 952 | + redis = sentinel.master_for(master_service) |
| 953 | + return PrefixDecorator(redis, prefix=prefix) |
0 commit comments