Skip to content

Commit a2ac1ad

Browse files
authored
fix: build the host list provider from a verified database dialect (#1292)
1 parent 7e31ed8 commit a2ac1ad

25 files changed

Lines changed: 2252 additions & 626 deletions

‎aws_advanced_python_wrapper/aio/aurora_connection_tracker.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,18 @@ def _parse_alias(alias: str) -> tuple:
602602
``[::1]:5432`` split correctly on the LAST colon. Falls back to
603603
port 5432 when the alias has no colon (or the port part isn't
604604
numeric).
605+
606+
The trailing ``/`` is stripped first because host-list-change keys
607+
are ``HostInfo.url`` (``host:port/``) while tracking keys are
608+
``host:port``. Without it the port never parses and every
609+
invalidation is aimed at port 5432, so a demoted writer on any
610+
other port keeps its connections.
605611
"""
606-
host_part, sep, port_part = alias.rpartition(":")
612+
stripped = alias.rstrip("/")
613+
host_part, sep, port_part = stripped.rpartition(":")
607614
if not sep:
608615
# No colon -> treat entire alias as host, port defaults.
609-
return alias, 5432
616+
return stripped, 5432
610617
try:
611618
return host_part, int(port_part)
612619
except ValueError:

‎aws_advanced_python_wrapper/aio/dialect_utils.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,33 @@ async def _execute_scalar_query(
171171
except Exception: # noqa: BLE001 - cursor close best-effort
172172
pass
173173

174+
@staticmethod
175+
async def fetch_one(conn: Any, query: str) -> Optional[tuple]:
176+
"""Run the provided query and fetch the first row.
177+
"""
178+
return await AsyncDialectUtils._execute_scalar_query(conn, query)
179+
180+
@staticmethod
181+
async def fetch_all(conn: Any, query: str) -> list:
182+
"""Run the provided query and fetch all rows. Returns an empty list on no results.
183+
"""
184+
cursor_ret = conn.cursor()
185+
cursor = await cursor_ret if asyncio.iscoroutine(cursor_ret) else cursor_ret
186+
try:
187+
await cursor.execute(query)
188+
return list(await cursor.fetchall())
189+
finally:
190+
# Call close on the cursor if it has a close method.
191+
# Some async DBAPI implementations may not have a close method on their cursor.
192+
close = getattr(cursor, "close", None)
193+
if close is not None:
194+
try:
195+
result = close()
196+
if asyncio.iscoroutine(result):
197+
await result
198+
except Exception: # noqa: BLE001 - cursor close best-effort
199+
pass
200+
174201
@staticmethod
175202
async def _execute_is_reader_query(
176203
conn: Any,

‎aws_advanced_python_wrapper/aio/gdb_failover_plugin.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ async def _failover_with_mode(
260260
# deadline (validated on Aurora: 2592 reconnects to the stale
261261
# host over 5 min, then timeout).
262262
# STRICT_WRITER means "reconnect to the new writer" -- exactly
263-
# failover_v2's job. (Requires the real topology provider, i.e.
264-
# gdb_failover present in wrapper._TOPOLOGY_REQUIRING_PLUGINS.)
263+
# failover_v2's job. (Requires a real topology provider, which
264+
# the Global Aurora dialects supply.)
265265
await self._failover_writer(
266266
self._plugin_service.all_hosts, driver_dialect, deadline, None)
267267
case GdbFailoverMode.STRICT_HOME_READER:

‎aws_advanced_python_wrapper/aio/host_list_provider.py‎

Lines changed: 141 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ def initial_connection_host_info(self, value: HostInfo) -> None:
141141
def is_static_host_list_provider(self) -> bool:
142142
...
143143

144+
@property
145+
def is_dialect_confirmed(self) -> bool:
146+
...
147+
148+
def get_target_driver_func(self) -> Optional[Callable]:
149+
...
150+
144151

145152
class AsyncStaticHostListProvider:
146153
"""Static host list -- one host built from connection props.
@@ -221,7 +228,9 @@ def __init__(
221228
cluster_id: Optional[str] = None,
222229
default_port: int = 5432,
223230
monitor_connection_factory: Optional[
224-
Callable[[], Awaitable[Any]]] = None) -> None:
231+
Callable[[], Awaitable[Any]]] = None,
232+
provider_service: Optional[AsyncHostListProviderService] = None) -> None:
233+
self._provider_service = provider_service
225234
self._props = props
226235
self._driver_dialect = driver_dialect
227236
self._topology_query = topology_query
@@ -248,6 +257,30 @@ def __init__(
248257
# aio/wrapper.py and cluster_topology_monitor.build_probe_host.
249258
self._probe_host: Optional[Callable[..., Any]] = None
250259

260+
def _is_dialect_confirmed(self) -> bool:
261+
"""Whether the database dialect is finalized, and so whether topology may be queried yet.
262+
"""
263+
if self._provider_service is None:
264+
return True
265+
return self._provider_service.is_dialect_confirmed
266+
267+
def _seed_topology(self) -> Topology:
268+
"""The host from the connection properties, reported until the dialect
269+
is confirmed.
270+
271+
Built fresh on each call rather than held as one object: the default
272+
availability strategy never expires a mark, so a shared seed host
273+
would let a single failed connect pin it UNAVAILABLE for the life of
274+
the process, with nothing to clear it.
275+
"""
276+
if self._topology_cache:
277+
return self._topology_cache
278+
try:
279+
port = int(self._props["port"])
280+
except (KeyError, TypeError, ValueError):
281+
port = self._default_port
282+
return (HostInfo(host=str(self._props.get("host", "")), port=port),)
283+
251284
def set_probe_host(self, probe_host: Callable[..., Any]) -> None:
252285
"""Arm panic-mode writer discovery on the (future) topology monitor.
253286
@@ -258,25 +291,6 @@ def set_probe_host(self, probe_host: Callable[..., Any]) -> None:
258291
"""
259292
self._probe_host = probe_host
260293

261-
def reconfigure_topology_query(
262-
self, topology_query: str, default_port: Optional[int] = None) -> None:
263-
"""Re-point the provider at a (post-connect) resolved dialect's query.
264-
265-
The provider is built BEFORE the connection exists, so an
266-
instance-endpoint MySQL connection starts with the PostgreSQL-default
267-
topology query + port (the ctor default). Once the database dialect is
268-
resolved/upgraded to the Aurora MySQL dialect we swap in its topology
269-
query and the MySQL port so discovery actually works. Clears the cache
270-
so the next refresh re-probes with the new query (any cached PG-query
271-
result is stale/empty).
272-
"""
273-
if topology_query:
274-
self._topology_query = topology_query
275-
if default_port:
276-
self._default_port = default_port
277-
self._topology_cache = None
278-
self._last_refresh_ns = 0
279-
280294
@staticmethod
281295
def _derive_cluster_id(props: Properties) -> str:
282296
# Only treat `cluster_id` as explicit when present in the props dict;
@@ -320,6 +334,11 @@ async def force_refresh(self, connection: Optional[Any] = None) -> Topology:
320334
if connection is not None:
321335
self._last_conn = connection
322336

337+
if not self._is_dialect_confirmed():
338+
# No topology query and no monitor until the dialect is
339+
# confirmed, so the guessed dialect's SQL is never run.
340+
return self._seed_topology()
341+
323342
if connection is None:
324343
if self._topology_cache is not None:
325344
return self._topology_cache
@@ -366,6 +385,10 @@ async def force_monitoring_refresh(
366385
querying through a caller connection. Falls back to
367386
:meth:`force_refresh` when no monitor can be built (static dialects).
368387
"""
388+
if not self._is_dialect_confirmed():
389+
# No topology query and no monitor until the dialect is
390+
# confirmed, so the guessed dialect's SQL is never run.
391+
return self._seed_topology()
369392
monitor = self._get_or_create_monitor()
370393
if monitor is None:
371394
return await self.force_refresh(self._last_conn)
@@ -708,6 +731,7 @@ def __init__(
708731
monitor_connection_factory: Optional[
709732
Callable[[], Awaitable[Any]]] = None,
710733
writer_host_column_index: int = 0,
734+
provider_service: Optional[AsyncHostListProviderService] = None,
711735
) -> None:
712736
super().__init__(
713737
props=props,
@@ -716,6 +740,7 @@ def __init__(
716740
cluster_id=cluster_id,
717741
default_port=default_port,
718742
monitor_connection_factory=monitor_connection_factory,
743+
provider_service=provider_service,
719744
)
720745
self._writer_host_query = writer_host_query
721746
self._host_id_query = host_id_query
@@ -859,6 +884,7 @@ def __init__(
859884
default_port: int = 5432,
860885
monitor_connection_factory: Optional[
861886
Callable[[], Awaitable[Any]]] = None,
887+
provider_service: Optional[AsyncHostListProviderService] = None,
862888
) -> None:
863889
super().__init__(
864890
props=props,
@@ -867,6 +893,7 @@ def __init__(
867893
cluster_id=cluster_id,
868894
default_port=default_port,
869895
monitor_connection_factory=monitor_connection_factory,
896+
provider_service=provider_service,
870897
)
871898
if instance_templates_by_region is None:
872899
raw = WrapperProperties.GLOBAL_CLUSTER_INSTANCE_HOST_PATTERNS.get(
@@ -954,3 +981,97 @@ def _rows_to_topology(self, rows: List[tuple]) -> Topology:
954981
# Writer first, readers after -- matches the other async providers.
955982
hosts.sort(key=lambda h: 0 if h.role == HostRole.WRITER else 1)
956983
return tuple(hosts)
984+
985+
986+
def build_monitor_connection_factory(
987+
provider_service: AsyncHostListProviderService,
988+
props: Properties) -> Callable[[], Awaitable[Any]]:
989+
"""Build the factory the topology monitor uses for its own connection.
990+
"""
991+
from aws_advanced_python_wrapper.utils.properties import (Properties,
992+
PropertiesUtils)
993+
994+
async def _connect() -> Any:
995+
target_func = provider_service.get_target_driver_func()
996+
if target_func is None:
997+
raise AwsWrapperError(
998+
Messages.get_formatted(
999+
"HostListProvider.MonitorConnectionNoTargetDriverFunc",
1000+
type(provider_service).__name__))
1001+
host_info = (provider_service.initial_connection_host_info
1002+
or provider_service.current_host_info)
1003+
if host_info is None:
1004+
raise AwsWrapperError(
1005+
Messages.get_formatted(
1006+
"HostListProvider.MonitorConnectionNoSeedHost",
1007+
type(provider_service).__name__))
1008+
monitoring_props = PropertiesUtils.create_topology_monitoring_properties(
1009+
Properties(dict(props)))
1010+
return await provider_service.driver_dialect.connect(
1011+
host_info, monitoring_props, target_func)
1012+
1013+
return _connect
1014+
1015+
1016+
def create_static_provider(
1017+
provider_service: AsyncHostListProviderService,
1018+
props: Properties) -> AsyncHostListProvider:
1019+
"""Supplier body for dialects with no topology: a single seed host."""
1020+
return AsyncStaticHostListProvider(props)
1021+
1022+
1023+
def create_aurora_provider(
1024+
provider_service: AsyncHostListProviderService,
1025+
props: Properties,
1026+
*,
1027+
topology_query: str,
1028+
default_port: int) -> AsyncHostListProvider:
1029+
"""Supplier body for the Aurora topology dialects."""
1030+
return AsyncAuroraHostListProvider(
1031+
props,
1032+
provider_service.driver_dialect,
1033+
topology_query=topology_query,
1034+
default_port=default_port,
1035+
monitor_connection_factory=build_monitor_connection_factory(
1036+
provider_service, props),
1037+
provider_service=provider_service)
1038+
1039+
1040+
def create_global_aurora_provider(
1041+
provider_service: AsyncHostListProviderService,
1042+
props: Properties,
1043+
*,
1044+
topology_query: str,
1045+
default_port: int) -> AsyncHostListProvider:
1046+
"""Supplier body for the Aurora Global Database dialects."""
1047+
return AsyncGlobalAuroraHostListProvider(
1048+
props,
1049+
provider_service.driver_dialect,
1050+
topology_query=topology_query,
1051+
default_port=default_port,
1052+
monitor_connection_factory=build_monitor_connection_factory(
1053+
provider_service, props),
1054+
provider_service=provider_service)
1055+
1056+
1057+
def create_multi_az_provider(
1058+
provider_service: AsyncHostListProviderService,
1059+
props: Properties,
1060+
*,
1061+
writer_host_query: str,
1062+
topology_query: str,
1063+
host_id_query: str,
1064+
default_port: int,
1065+
writer_host_column_index: int = 0) -> AsyncHostListProvider:
1066+
"""Supplier body for the RDS Multi-AZ DB cluster dialects."""
1067+
return AsyncMultiAzHostListProvider(
1068+
props,
1069+
provider_service.driver_dialect,
1070+
writer_host_query=writer_host_query,
1071+
topology_query=topology_query,
1072+
host_id_query=host_id_query,
1073+
default_port=default_port,
1074+
writer_host_column_index=writer_host_column_index,
1075+
monitor_connection_factory=build_monitor_connection_factory(
1076+
provider_service, props),
1077+
provider_service=provider_service)

‎aws_advanced_python_wrapper/aio/limitless_plugin.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ async def _run(self) -> None:
345345
raise
346346
except Exception as e: # noqa: BLE001 - keep the loop alive
347347
logger.debug(
348-
"LimitlessRouterMonitor.errorDuringMonitoringStop",
348+
"LimitlessRouterMonitor.ErrorDuringMonitoringStop",
349349
self._host_info.host, e)
350350
if telemetry_context is not None:
351351
telemetry_context.set_exception(e)

0 commit comments

Comments
 (0)