Skip to content

Commit

Permalink
fix(db): Fix read replica strategy (#3426)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Feb 15, 2024
1 parent 16a2468 commit d63a289
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/app/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):

def _get_replica(self, replicas: list[str]) -> None | str:
while replicas:
if settings.REPLICA_READ_STRATEGY == ReplicaReadStrategy.DISTRIBUTED.value:
if settings.REPLICA_READ_STRATEGY == ReplicaReadStrategy.DISTRIBUTED:
database = random.choice(replicas)
elif settings.REPLICA_READ_STRATEGY == ReplicaReadStrategy.SEQUENTIAL.value:
elif settings.REPLICA_READ_STRATEGY == ReplicaReadStrategy.SEQUENTIAL:
database = replicas[0]
else:
raise ImproperlyConfiguredError(
Expand Down
8 changes: 4 additions & 4 deletions api/tests/unit/app/test_unit_app_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_replica_router_db_for_read_with_one_offline_replica(

# Set unused cross regional db for testing non-inclusion.
settings.NUM_CROSS_REGION_DB_REPLICAS = 2
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.DISTRIBUTED.value
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.DISTRIBUTED

conn_patch = mocker.MagicMock()
conn_patch.is_usable.side_effect = (False, True)
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_replica_router_db_for_read_with_local_offline_replicas(

# Use cross regional db for fallback after replicas.
settings.NUM_CROSS_REGION_DB_REPLICAS = 2
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.DISTRIBUTED.value
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.DISTRIBUTED

conn_patch = mocker.MagicMock()

Expand Down Expand Up @@ -107,7 +107,7 @@ def test_replica_router_db_for_read_with_all_offline_replicas(
# Given
settings.NUM_DB_REPLICAS = 4
settings.NUM_CROSS_REGION_DB_REPLICAS = 2
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.DISTRIBUTED.value
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.DISTRIBUTED

conn_patch = mocker.MagicMock()

Expand Down Expand Up @@ -141,7 +141,7 @@ def test_replica_router_db_with_sequential_read(
# Given
settings.NUM_DB_REPLICAS = 100
settings.NUM_CROSS_REGION_DB_REPLICAS = 2
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.SEQUENTIAL.value
settings.REPLICA_READ_STRATEGY = ReplicaReadStrategy.SEQUENTIAL

conn_patch = mocker.MagicMock()

Expand Down

0 comments on commit d63a289

Please sign in to comment.