Skip to content

Commit 24974fe

Browse files
authored
Renamed initial health check policies and unhealthy database argument (#3949)
* Renamed initial health check policies and unhealthy database argument * Updated docstring * Renamed argument * Codestyle fixes
1 parent 3d6b571 commit 24974fe

File tree

10 files changed

+57
-37
lines changed

10 files changed

+57
-37
lines changed

redis/asyncio/multidb/client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,15 @@ async def set_active_database(self, database: AsyncDatabase) -> None:
161161
"Cannot set active database, database is unhealthy"
162162
)
163163

164-
async def add_database(self, config: DatabaseConfig, skip_unhealthy: bool = True):
164+
async def add_database(
165+
self, config: DatabaseConfig, skip_initial_health_check: bool = True
166+
):
165167
"""
166168
Adds a new database to the database list.
169+
170+
Args:
171+
config: DatabaseConfig object that contains the database configuration.
172+
skip_initial_health_check: If True, adds the database even if it is unhealthy.
167173
"""
168174
# The retry object is not used in the lower level clients, so we can safely remove it.
169175
# We rely on command_retry in terms of global retries.
@@ -197,7 +203,7 @@ async def add_database(self, config: DatabaseConfig, skip_unhealthy: bool = True
197203
try:
198204
await self._check_db_health(database)
199205
except UnhealthyDatabaseException:
200-
if not skip_unhealthy:
206+
if not skip_initial_health_check:
201207
raise
202208

203209
highest_weighted_db, highest_weight = self._databases.get_top_n(1)[0]
@@ -357,14 +363,16 @@ async def _perform_initial_health_check(self):
357363
results = await self._check_databases_health()
358364
is_healthy = True
359365

360-
if self._config.initial_health_check_policy == InitialHealthCheck.ALL_HEALTHY:
366+
if self._config.initial_health_check_policy == InitialHealthCheck.ALL_AVAILABLE:
361367
is_healthy = False not in results.values()
362368
elif (
363369
self._config.initial_health_check_policy
364-
== InitialHealthCheck.MAJORITY_HEALTHY
370+
== InitialHealthCheck.MAJORITY_AVAILABLE
365371
):
366372
is_healthy = sum(results.values()) > len(results) / 2
367-
elif self._config.initial_health_check_policy == InitialHealthCheck.ANY_HEALTHY:
373+
elif (
374+
self._config.initial_health_check_policy == InitialHealthCheck.ONE_AVAILABLE
375+
):
368376
is_healthy = True in results.values()
369377

370378
if not is_healthy:

redis/asyncio/multidb/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545

4646

4747
class InitialHealthCheck(Enum):
48-
ALL_HEALTHY = "all_healthy"
49-
MAJORITY_HEALTHY = "majority_healthy"
50-
ANY_HEALTHY = "any_healthy"
48+
ALL_AVAILABLE = "all_available"
49+
MAJORITY_AVAILABLE = "majority_available"
50+
ONE_AVAILABLE = "one_available"
5151

5252

5353
def default_event_dispatcher() -> EventDispatcherInterface:
@@ -157,7 +157,7 @@ class MultiDbConfig:
157157
event_dispatcher: EventDispatcherInterface = field(
158158
default_factory=default_event_dispatcher
159159
)
160-
initial_health_check_policy: InitialHealthCheck = InitialHealthCheck.ALL_HEALTHY
160+
initial_health_check_policy: InitialHealthCheck = InitialHealthCheck.ALL_AVAILABLE
161161

162162
def databases(self) -> Databases:
163163
databases = WeightedList()

redis/multidb/client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,15 @@ def set_active_database(self, database: SyncDatabase) -> None:
144144
"Cannot set active database, database is unhealthy"
145145
)
146146

147-
def add_database(self, config: DatabaseConfig, skip_unhealthy: bool = True):
147+
def add_database(
148+
self, config: DatabaseConfig, skip_initial_health_check: bool = True
149+
):
148150
"""
149151
Adds a new database to the database list.
152+
153+
Args:
154+
config: DatabaseConfig object that contains the database configuration.
155+
skip_initial_health_check: If True, adds the database even if it is unhealthy.
150156
"""
151157
# The retry object is not used in the lower level clients, so we can safely remove it.
152158
# We rely on command_retry in terms of global retries.
@@ -187,7 +193,7 @@ def add_database(self, config: DatabaseConfig, skip_unhealthy: bool = True):
187193
try:
188194
self._check_db_health(database)
189195
except UnhealthyDatabaseException:
190-
if not skip_unhealthy:
196+
if not skip_initial_health_check:
191197
raise
192198

193199
highest_weighted_db, highest_weight = self._databases.get_top_n(1)[0]
@@ -343,14 +349,16 @@ def _perform_initial_health_check(self):
343349
results = self._check_databases_health()
344350
is_healthy = True
345351

346-
if self._config.initial_health_check_policy == InitialHealthCheck.ALL_HEALTHY:
352+
if self._config.initial_health_check_policy == InitialHealthCheck.ALL_AVAILABLE:
347353
is_healthy = False not in results.values()
348354
elif (
349355
self._config.initial_health_check_policy
350-
== InitialHealthCheck.MAJORITY_HEALTHY
356+
== InitialHealthCheck.MAJORITY_AVAILABLE
351357
):
352358
is_healthy = sum(results.values()) > len(results) / 2
353-
elif self._config.initial_health_check_policy == InitialHealthCheck.ANY_HEALTHY:
359+
elif (
360+
self._config.initial_health_check_policy == InitialHealthCheck.ONE_AVAILABLE
361+
):
354362
is_healthy = True in results.values()
355363

356364
if not is_healthy:

redis/multidb/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444

4545

4646
class InitialHealthCheck(Enum):
47-
ALL_HEALTHY = "all_healthy"
48-
MAJORITY_HEALTHY = "majority_healthy"
49-
ANY_HEALTHY = "any_healthy"
47+
ALL_AVAILABLE = "all_available"
48+
MAJORITY_AVAILABLE = "majority_available"
49+
ONE_AVAILABLE = "one_available"
5050

5151

5252
def default_event_dispatcher() -> EventDispatcherInterface:
@@ -157,7 +157,7 @@ class MultiDbConfig:
157157
event_dispatcher: EventDispatcherInterface = field(
158158
default_factory=default_event_dispatcher
159159
)
160-
initial_health_check_policy: InitialHealthCheck = InitialHealthCheck.ALL_HEALTHY
160+
initial_health_check_policy: InitialHealthCheck = InitialHealthCheck.ALL_AVAILABLE
161161

162162
def databases(self) -> Databases:
163163
databases = WeightedList()

tests/test_asyncio/test_multidb/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def mock_multi_db_config(request, mock_fd, mock_fs, mock_hc, mock_ed) -> MultiDb
108108
"health_check_probes", DEFAULT_HEALTH_CHECK_PROBES
109109
)
110110
initial_health_check_policy = request.param.get(
111-
"initial_health_check_policy", InitialHealthCheck.ALL_HEALTHY
111+
"initial_health_check_policy", InitialHealthCheck.ALL_AVAILABLE
112112
)
113113

114114
config = MultiDbConfig(

tests/test_asyncio/test_multidb/test_client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def test_execute_command_against_correct_db_on_successful_initialization(
6464
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
6565
[
6666
(
67-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
67+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
6868
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
6969
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
7070
{"weight": 0.7, "circuit": {"state": CBState.OPEN}},
@@ -486,7 +486,9 @@ async def mock_check_health(database):
486486
):
487487
# With skip_unhealthy=False, should raise exception
488488
with pytest.raises(UnhealthyDatabaseException):
489-
await client.add_database(new_db_config, skip_unhealthy=False)
489+
await client.add_database(
490+
new_db_config, skip_initial_health_check=False
491+
)
490492

491493
# Database list should remain unchanged
492494
assert len(client.get_databases()) == 2
@@ -789,7 +791,7 @@ async def mock_check_health(database):
789791
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
790792
[
791793
(
792-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
794+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
793795
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
794796
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
795797
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -825,7 +827,7 @@ async def mock_check_health(database):
825827
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
826828
[
827829
(
828-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
830+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
829831
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
830832
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
831833
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -863,7 +865,7 @@ async def mock_check_health(database):
863865
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
864866
[
865867
(
866-
{"initial_health_check_policy": InitialHealthCheck.ANY_HEALTHY},
868+
{"initial_health_check_policy": InitialHealthCheck.ONE_AVAILABLE},
867869
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
868870
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
869871
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -898,7 +900,7 @@ async def mock_check_health(database):
898900
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
899901
[
900902
(
901-
{"initial_health_check_policy": InitialHealthCheck.ANY_HEALTHY},
903+
{"initial_health_check_policy": InitialHealthCheck.ONE_AVAILABLE},
902904
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
903905
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
904906
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},

tests/test_asyncio/test_multidb/test_pipeline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def test_executes_pipeline_against_correct_db(
6969
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
7070
[
7171
(
72-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
72+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
7373
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
7474
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
7575
{"weight": 0.7, "circuit": {"state": CBState.OPEN}},
@@ -325,7 +325,7 @@ async def callback(pipe: Pipeline):
325325
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
326326
[
327327
(
328-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
328+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
329329
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
330330
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
331331
{"weight": 0.7, "circuit": {"state": CBState.OPEN}},

tests/test_multidb/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def mock_multi_db_config(request, mock_fd, mock_fs, mock_hc, mock_ed) -> MultiDb
108108
"health_check_probes", DEFAULT_HEALTH_CHECK_PROBES
109109
)
110110
initial_health_check_policy = request.param.get(
111-
"initial_health_check_policy", InitialHealthCheck.ALL_HEALTHY
111+
"initial_health_check_policy", InitialHealthCheck.ALL_AVAILABLE
112112
)
113113

114114
config = MultiDbConfig(

tests/test_multidb/test_client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_execute_command_against_correct_db_on_successful_initialization(
6565
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
6666
[
6767
(
68-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
68+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
6969
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
7070
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
7171
{"weight": 0.7, "circuit": {"state": CBState.OPEN}},
@@ -519,7 +519,9 @@ def mock_check_health(database):
519519
):
520520
# With skip_unhealthy=False, should raise exception
521521
with pytest.raises(UnhealthyDatabaseException):
522-
client.add_database(new_db_config, skip_unhealthy=False)
522+
client.add_database(
523+
new_db_config, skip_initial_health_check=False
524+
)
523525

524526
# Database list should remain unchanged
525527
assert len(client.get_databases()) == 2
@@ -797,7 +799,7 @@ def test_all_healthy_policy_succeeds_when_all_databases_healthy(
797799
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
798800
[
799801
(
800-
{"initial_health_check_policy": InitialHealthCheck.ALL_HEALTHY},
802+
{"initial_health_check_policy": InitialHealthCheck.ALL_AVAILABLE},
801803
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
802804
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
803805
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -837,7 +839,7 @@ def mock_check_health(database):
837839
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
838840
[
839841
(
840-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
842+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
841843
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
842844
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
843845
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -876,7 +878,7 @@ def mock_check_health(database):
876878
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
877879
[
878880
(
879-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
881+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
880882
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
881883
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
882884
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -917,7 +919,7 @@ def mock_check_health(database):
917919
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
918920
[
919921
(
920-
{"initial_health_check_policy": InitialHealthCheck.ANY_HEALTHY},
922+
{"initial_health_check_policy": InitialHealthCheck.ONE_AVAILABLE},
921923
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
922924
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
923925
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
@@ -955,7 +957,7 @@ def mock_check_health(database):
955957
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
956958
[
957959
(
958-
{"initial_health_check_policy": InitialHealthCheck.ANY_HEALTHY},
960+
{"initial_health_check_policy": InitialHealthCheck.ONE_AVAILABLE},
959961
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
960962
{"weight": 0.7, "circuit": {"state": CBState.CLOSED}},
961963
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},

tests/test_multidb/test_pipeline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_executes_pipeline_against_correct_db(
7272
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
7373
[
7474
(
75-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
75+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
7676
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
7777
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
7878
{"weight": 0.7, "circuit": {"state": CBState.OPEN}},
@@ -331,7 +331,7 @@ def callback(pipe: Pipeline):
331331
"mock_multi_db_config,mock_db, mock_db1, mock_db2",
332332
[
333333
(
334-
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_HEALTHY},
334+
{"initial_health_check_policy": InitialHealthCheck.MAJORITY_AVAILABLE},
335335
{"weight": 0.2, "circuit": {"state": CBState.CLOSED}},
336336
{"weight": 0.5, "circuit": {"state": CBState.CLOSED}},
337337
{"weight": 0.7, "circuit": {"state": CBState.OPEN}},

0 commit comments

Comments
 (0)