Skip to content

Commit 2746dcd

Browse files
committed
Fixing sentinel command response
1 parent 037d108 commit 2746dcd

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

redis/asyncio/sentinel.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import random
33
import weakref
4+
from functools import reduce
45
from typing import AsyncIterator, Iterable, Mapping, Optional, Sequence, Tuple, Type
56

67
from redis.asyncio.client import Redis
@@ -227,16 +228,25 @@ async def execute_command(self, *args, **kwargs):
227228
once = bool(kwargs.get("once", False))
228229
if "once" in kwargs.keys():
229230
kwargs.pop("once")
231+
232+
# Check if command suppose to return boolean response.
233+
bool_resp = bool(kwargs.get("bool_resp", False))
234+
if "bool_resp" in kwargs.keys():
235+
kwargs.pop("bool_resp")
230236

231237
if once:
232-
await random.choice(self.sentinels).execute_command(*args, **kwargs)
233-
else:
234-
tasks = [
235-
asyncio.Task(sentinel.execute_command(*args, **kwargs))
236-
for sentinel in self.sentinels
237-
]
238-
await asyncio.gather(*tasks)
239-
return True
238+
return await random.choice(self.sentinels).execute_command(*args, **kwargs)
239+
240+
tasks = [
241+
asyncio.Task(sentinel.execute_command(*args, **kwargs))
242+
for sentinel in self.sentinels
243+
]
244+
responses = await asyncio.gather(*tasks)
245+
246+
if bool_resp:
247+
return reduce(lambda x, y: x and y, responses)
248+
249+
return responses
240250

241251
def __repr__(self):
242252
sentinel_addresses = []

redis/commands/sentinel.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@ def sentinel(self, *args):
1313

1414
def sentinel_get_master_addr_by_name(self, service_name):
1515
"""Returns a (host, port) pair for the given ``service_name``"""
16-
return self.execute_command("SENTINEL GET-MASTER-ADDR-BY-NAME", service_name)
16+
return self.execute_command("SENTINEL GET-MASTER-ADDR-BY-NAME", service_name, once=True)
1717

1818
def sentinel_master(self, service_name):
1919
"""Returns a dictionary containing the specified masters state."""
2020
return self.execute_command("SENTINEL MASTER", service_name)
2121

2222
def sentinel_masters(self):
2323
"""Returns a list of dictionaries containing each master's state."""
24-
return self.execute_command("SENTINEL MASTERS")
24+
return self.execute_command("SENTINEL MASTERS", once=True)
2525

2626
def sentinel_monitor(self, name, ip, port, quorum):
2727
"""Add a new master to Sentinel to be monitored"""
28-
return self.execute_command("SENTINEL MONITOR", name, ip, port, quorum)
28+
return self.execute_command("SENTINEL MONITOR", name, ip, port, quorum, bool_resp=True)
2929

3030
def sentinel_remove(self, name):
3131
"""Remove a master from Sentinel's monitoring"""
32-
return self.execute_command("SENTINEL REMOVE", name)
32+
return self.execute_command("SENTINEL REMOVE", name, bool_resp=True)
3333

3434
def sentinel_sentinels(self, service_name):
3535
"""Returns a list of sentinels for ``service_name``"""
3636
return self.execute_command("SENTINEL SENTINELS", service_name)
3737

3838
def sentinel_set(self, name, option, value):
3939
"""Set Sentinel monitoring parameters for a given master"""
40-
return self.execute_command("SENTINEL SET", name, option, value)
40+
return self.execute_command("SENTINEL SET", name, option, value, bool_resp=True)
4141

4242
def sentinel_slaves(self, service_name):
4343
"""Returns a list of slaves for ``service_name``"""
44-
return self.execute_command("SENTINEL SLAVES", service_name)
44+
return self.execute_command("SENTINEL SLAVES", service_name, once=True)
4545

4646
def sentinel_reset(self, pattern):
4747
"""
@@ -52,7 +52,7 @@ def sentinel_reset(self, pattern):
5252
failover in progress), and removes every slave and sentinel already
5353
discovered and associated with the master.
5454
"""
55-
return self.execute_command("SENTINEL RESET", pattern, once=True)
55+
return self.execute_command("SENTINEL RESET", pattern, once=True, bool_resp=True)
5656

5757
def sentinel_failover(self, new_master_name):
5858
"""
@@ -61,7 +61,7 @@ def sentinel_failover(self, new_master_name):
6161
configuration will be published so that the other Sentinels will
6262
update their configurations).
6363
"""
64-
return self.execute_command("SENTINEL FAILOVER", new_master_name)
64+
return self.execute_command("SENTINEL FAILOVER", new_master_name, bool_resp=True)
6565

6666
def sentinel_ckquorum(self, new_master_name):
6767
"""
@@ -72,7 +72,7 @@ def sentinel_ckquorum(self, new_master_name):
7272
This command should be used in monitoring systems to check if a
7373
Sentinel deployment is ok.
7474
"""
75-
return self.execute_command("SENTINEL CKQUORUM", new_master_name, once=True)
75+
return self.execute_command("SENTINEL CKQUORUM", new_master_name, once=True, bool_resp=True)
7676

7777
def sentinel_flushconfig(self):
7878
"""
@@ -90,7 +90,7 @@ def sentinel_flushconfig(self):
9090
This command works even if the previous configuration file is
9191
completely missing.
9292
"""
93-
return self.execute_command("SENTINEL FLUSHCONFIG")
93+
return self.execute_command("SENTINEL FLUSHCONFIG", bool_resp=True)
9494

9595

9696
class AsyncSentinelCommands(SentinelCommands):

redis/sentinel.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22
import weakref
3+
from functools import reduce
34
from typing import Optional
45

56
from redis.client import Redis
@@ -254,13 +255,23 @@ def execute_command(self, *args, **kwargs):
254255
once = bool(kwargs.get("once", False))
255256
if "once" in kwargs.keys():
256257
kwargs.pop("once")
258+
259+
# Check if command suppose to return boolean response.
260+
bool_resp = bool(kwargs.get("bool_resp", False))
261+
if "bool_resp" in kwargs.keys():
262+
kwargs.pop("bool_resp")
257263

258264
if once:
259-
random.choice(self.sentinels).execute_command(*args, **kwargs)
260-
else:
261-
for sentinel in self.sentinels:
262-
sentinel.execute_command(*args, **kwargs)
263-
return True
265+
return random.choice(self.sentinels).execute_command(*args, **kwargs)
266+
267+
responses = []
268+
for sentinel in self.sentinels:
269+
responses.append(sentinel.execute_command(*args, **kwargs))
270+
271+
if bool_resp:
272+
return reduce(lambda x, y: x and y, responses)
273+
274+
return responses
264275

265276
def __repr__(self):
266277
sentinel_addresses = []

0 commit comments

Comments
 (0)