Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db): Fix read replica strategy #3426

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading