Skip to content

Commit 76befb4

Browse files
vladvildanovpetyaslavova
authored andcommitted
Added logging for MultiDBClients (#3896)
* Added logging for MultiDBClients * Codestyle changes * Codestyle changes
1 parent faee0fb commit 76befb4

5 files changed

Lines changed: 28 additions & 4 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Multi-database client (Active-Active)
1+
Client-side geographic failover (Active-Active)
22
=====================================
33

44
The multi-database client allows your application to connect to multiple Redis databases, which are typically replicas of each other.

redis/asyncio/multidb/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class MultiDBClient(AsyncRedisModuleCommands, AsyncCoreCommands):
2424
"""
2525
Client that operates on multiple logical Redis databases.
26-
Should be used in Active-Active database setups.
26+
Should be used in Client-side geographic failover database setups.
2727
"""
2828

2929
def __init__(self, config: MultiDbConfig):
@@ -299,7 +299,7 @@ async def _check_databases_health(
299299
unhealthy_db = result.database
300300
unhealthy_db.circuit.state = CBState.OPEN
301301

302-
logger.exception(
302+
logger.debug(
303303
"Health check failed, due to exception",
304304
exc_info=result.original_exception,
305305
)
@@ -337,8 +337,14 @@ def _on_circuit_state_change_callback(
337337
return
338338

339339
if old_state == CBState.CLOSED and new_state == CBState.OPEN:
340+
logger.error(
341+
f"Database {circuit.database} is unreachable. Failover has been initiated."
342+
)
340343
loop.call_later(DEFAULT_GRACE_PERIOD, _half_open_circuit, circuit)
341344

345+
if old_state != CBState.CLOSED and new_state == CBState.CLOSED:
346+
logger.info(f"Database {circuit.database} is reachable again.")
347+
342348
async def aclose(self):
343349
if self.command_executor.active_database:
344350
await self.command_executor.active_database.client.aclose()

redis/asyncio/multidb/database.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ def circuit(self) -> CircuitBreaker:
6767
@circuit.setter
6868
def circuit(self, circuit: CircuitBreaker):
6969
self._cb = circuit
70+
71+
def __repr__(self):
72+
return f"Database(client={self.client}, weight={self.weight})"

redis/multidb/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class MultiDBClient(RedisModuleCommands, CoreCommands):
2525
"""
2626
Client that operates on multiple logical Redis databases.
27-
Should be used in Active-Active database setups.
27+
Should be used in Client-side geographic failover database setups.
2828
"""
2929

3030
def __init__(self, config: MultiDbConfig):
@@ -276,6 +276,11 @@ def _check_databases_health(self, on_error: Callable[[Exception], None] = None):
276276
unhealthy_db = e.database
277277
unhealthy_db.circuit.state = CBState.OPEN
278278

279+
logger.debug(
280+
"Health check failed, due to exception",
281+
exc_info=e.original_exception,
282+
)
283+
279284
if on_error:
280285
on_error(e.original_exception)
281286
except TimeoutError:
@@ -291,10 +296,17 @@ def _on_circuit_state_change_callback(
291296
return
292297

293298
if old_state == CBState.CLOSED and new_state == CBState.OPEN:
299+
logger.error(
300+
f"Database {circuit.database} is unreachable. Failover has been initiated."
301+
)
302+
294303
self._bg_scheduler.run_once(
295304
DEFAULT_GRACE_PERIOD, _half_open_circuit, circuit
296305
)
297306

307+
if old_state != CBState.CLOSED and new_state == CBState.CLOSED:
308+
logger.info(f"Database {circuit.database} is reachable again.")
309+
298310
def close(self):
299311
"""
300312
Closes the client and all its resources.

redis/multidb/database.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,6 @@ def circuit(self) -> CircuitBreaker:
128128
@circuit.setter
129129
def circuit(self, circuit: CircuitBreaker):
130130
self._cb = circuit
131+
132+
def __repr__(self):
133+
return f"Database(client={self.client}, weight={self.weight})"

0 commit comments

Comments
 (0)