Skip to content

Commit 8e0b84d

Browse files
scoopexMarc Schöchlin
and
Marc Schöchlin
authored
Improve error output for master discovery (#2720)
Make MasterNotFoundError exception more precise in the case of ConnectionError and TimeoutError to help the user to identify configuration errors Co-authored-by: Marc Schöchlin <[email protected]>
1 parent fddd3d6 commit 8e0b84d

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Fix Sentinel.execute_command doesn't execute across the entire sentinel cluster bug (#2458)
4343
* Added a replacement for the default cluster node in the event of failure (#2463)
4444
* Fix for Unhandled exception related to self.host with unix socket (#2496)
45+
* Improve error output for master discovery
4546

4647
* 4.1.3 (Feb 8, 2022)
4748
* Fix flushdb and flushall (#1926)

redis/asyncio/sentinel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,12 @@ async def discover_master(self, service_name: str):
254254
Returns a pair (address, port) or raises MasterNotFoundError if no
255255
master is found.
256256
"""
257+
collected_errors = list()
257258
for sentinel_no, sentinel in enumerate(self.sentinels):
258259
try:
259260
masters = await sentinel.sentinel_masters()
260-
except (ConnectionError, TimeoutError):
261+
except (ConnectionError, TimeoutError) as e:
262+
collected_errors.append(f"{sentinel} - {e!r}")
261263
continue
262264
state = masters.get(service_name)
263265
if state and self.check_master_state(state, service_name):
@@ -267,7 +269,11 @@ async def discover_master(self, service_name: str):
267269
self.sentinels[0],
268270
)
269271
return state["ip"], state["port"]
270-
raise MasterNotFoundError(f"No master found for {service_name!r}")
272+
273+
error_info = ""
274+
if len(collected_errors) > 0:
275+
error_info = f" : {', '.join(collected_errors)}"
276+
raise MasterNotFoundError(f"No master found for {service_name!r}{error_info}")
271277

272278
def filter_slaves(
273279
self, slaves: Iterable[Mapping]

redis/sentinel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ def discover_master(self, service_name):
230230
Returns a pair (address, port) or raises MasterNotFoundError if no
231231
master is found.
232232
"""
233+
collected_errors = list()
233234
for sentinel_no, sentinel in enumerate(self.sentinels):
234235
try:
235236
masters = sentinel.sentinel_masters()
236-
except (ConnectionError, TimeoutError):
237+
except (ConnectionError, TimeoutError) as e:
238+
collected_errors.append(f"{sentinel} - {e!r}")
237239
continue
238240
state = masters.get(service_name)
239241
if state and self.check_master_state(state, service_name):
@@ -243,7 +245,11 @@ def discover_master(self, service_name):
243245
self.sentinels[0],
244246
)
245247
return state["ip"], state["port"]
246-
raise MasterNotFoundError(f"No master found for {service_name!r}")
248+
249+
error_info = ""
250+
if len(collected_errors) > 0:
251+
error_info = f" : {', '.join(collected_errors)}"
252+
raise MasterNotFoundError(f"No master found for {service_name!r}{error_info}")
247253

248254
def filter_slaves(self, slaves):
249255
"Remove slaves that are in an ODOWN or SDOWN state"

0 commit comments

Comments
 (0)