Skip to content

Commit c4d1583

Browse files
committed
Add logs
1 parent 0dc541a commit c4d1583

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/common/core/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import enum
22
import json
3+
import logging
34
import pathlib
45
import random
56
from functools import lru_cache
@@ -13,6 +14,8 @@
1314
from django.db.models import Manager, Model
1415
from django.db.utils import OperationalError
1516

17+
logger = logging.getLogger(__name__)
18+
1619
UNKNOWN = "unknown"
1720
VERSIONS_INFO_FILE_LOCATION = ".versions.json"
1821

@@ -138,34 +141,38 @@ def using_database_replica(
138141
local_replicas = [name for name in connections if name.startswith(replica_prefix)]
139142

140143
if not local_replicas:
144+
logger.info("No replicas set up.")
141145
return manager
142146

143147
chosen_replica = None
144148

145149
if settings.REPLICA_READ_STRATEGY == ReplicaReadStrategy.SEQUENTIAL:
146150
_sequential_replica_manager.setdefault(replica_prefix, cycle(local_replicas))
147-
for attempt in range(len(local_replicas)):
151+
for _ in range(len(local_replicas)):
148152
attempted_replica = next(_sequential_replica_manager[replica_prefix])
149153
try:
150154
connections[attempted_replica].ensure_connection()
151155
chosen_replica = attempted_replica
152156
break
153157
except OperationalError:
158+
logger.warning(f"Replica '{attempted_replica}' is not available.")
154159
continue
155160

156161
if settings.REPLICA_READ_STRATEGY == ReplicaReadStrategy.DISTRIBUTED:
157-
for attempt in range(len(local_replicas)):
162+
for _ in range(len(local_replicas)):
158163
attempted_replica = random.choice(local_replicas)
159164
try:
160165
connections[attempted_replica].ensure_connection()
161166
chosen_replica = attempted_replica
162167
break
163168
except OperationalError:
169+
logger.warning(f"Replica '{attempted_replica}' is not available.")
164170
local_replicas.remove(attempted_replica)
165171
continue
166172

167173
if not chosen_replica:
168174
if replica_prefix == "replica_":
175+
logger.warning("Falling back to cross-region replicas, if any.")
169176
return using_database_replica(manager, "cross_region_replica_")
170177
raise OperationalError("No available replicas")
171178

0 commit comments

Comments
 (0)