@@ -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
145152class 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 )
0 commit comments