35
35
SlidingExpirationCacheWithCleanupThread
36
36
from aws_advanced_python_wrapper .utils .telemetry .telemetry import (
37
37
TelemetryContext , TelemetryFactory , TelemetryTraceLevel )
38
+ from aws_advanced_python_wrapper .utils .utils import LogUtils
38
39
39
40
if TYPE_CHECKING :
40
41
from aws_advanced_python_wrapper .driver_dialect import DriverDialect
@@ -50,6 +51,10 @@ class LimitlessConnectionPlugin(Plugin):
50
51
def __init__ (self , plugin_service : PluginService , props : Properties ):
51
52
self ._plugin_service = plugin_service
52
53
self ._properties = props
54
+ self ._limitless_router_service = LimitlessRouterService (
55
+ self ._plugin_service ,
56
+ LimitlessQueryHelper (self ._plugin_service )
57
+ )
53
58
54
59
@property
55
60
def subscribed_methods (self ) -> Set [str ]:
@@ -68,34 +73,27 @@ def connect(
68
73
69
74
dialect : DatabaseDialect = self ._plugin_service .database_dialect
70
75
if not isinstance (dialect , AuroraLimitlessDialect ):
71
- connection = connect_func ()
72
76
refreshed_dialect = self ._plugin_service .database_dialect
73
-
74
77
if not isinstance (refreshed_dialect , AuroraLimitlessDialect ):
75
78
raise UnsupportedOperationError (
76
79
Messages .get_formatted ("LimitlessConnectionPlugin.UnsupportedDialectOrDatabase" ,
77
80
type (refreshed_dialect ).__name__ ))
78
81
79
- limitless_router_service = LimitlessRouterService (
80
- self ._plugin_service ,
81
- LimitlessQueryHelper (self ._plugin_service )
82
- )
83
-
84
82
if is_initial_connection :
85
- limitless_router_service .start_monitoring (host_info , props )
83
+ self . _limitless_router_service .start_monitoring (host_info , props )
86
84
87
- context : LimitlessConnectionContext = LimitlessConnectionContext (
85
+ self . _context : LimitlessConnectionContext = LimitlessConnectionContext (
88
86
host_info ,
89
87
props ,
90
88
connection ,
91
89
connect_func ,
92
90
[],
93
91
self
94
92
)
95
- limitless_router_service . establish_connection (context )
96
- connection = context .get_connection ()
93
+ self . _limitless_router_service . establish_connection (self . _context )
94
+ connection = self . _context .get_connection ()
97
95
if connection is not None and not self ._plugin_service .driver_dialect .is_closed (connection ):
98
- return context . get_connection ()
96
+ return connection
99
97
100
98
raise AwsWrapperError (Messages .get_formatted ("LimitlessConnectionPlugin.FailedToConnectToHost" , host_info .host ))
101
99
@@ -172,6 +170,7 @@ def run(self):
172
170
lambda _ : new_limitless_routers ,
173
171
WrapperProperties .LIMITLESS_MONITOR_DISPOSAL_TIME_MS .get (
174
172
self ._properties ) * 1_000_000 )
173
+ logger .debug (LogUtils .log_topology (tuple (new_limitless_routers ), "[limitlessRouterMonitor] Topology:" ))
175
174
176
175
sleep (self ._interval_ms / 1000 )
177
176
@@ -331,18 +330,19 @@ def establish_connection(self, context: LimitlessConnectionContext) -> None:
331
330
self ._plugin_service .host_list_provider .get_cluster_id (), context .get_props ()))
332
331
333
332
if context .get_limitless_routers () is None or len (context .get_limitless_routers ()) == 0 :
334
- logger .debug ("LimitlessRouterServiceImpl.limitlessRouterCacheEmpty " )
333
+ logger .debug ("LimitlessRouterService.:LimitlessRouterCacheEmpty " )
335
334
336
335
wait_for_router_info = WrapperProperties .WAIT_FOR_ROUTER_INFO .get (context .get_props ())
337
336
if wait_for_router_info :
338
337
self ._synchronously_get_limitless_routers_with_retry (context )
339
338
else :
340
- logger .debug ("LimitlessRouterServiceImpl .UsingProvidedConnectUrl" )
339
+ logger .debug ("LimitlessRouterService .UsingProvidedConnectUrl" )
341
340
if context .get_connection () is None or self ._plugin_service .driver_dialect .is_closed (context .get_connection ()):
342
341
context .set_connection (context .get_connect_func ()())
342
+ return
343
343
344
- if context .get_host_info in context .get_limitless_routers ():
345
- logger .debug ("LimitlessRouterServiceImpl .ConnectWithHost" )
344
+ if context .get_host_info () in context .get_limitless_routers ():
345
+ logger .debug ("LimitlessRouterService .ConnectWithHost" )
346
346
if context .get_connection () is None :
347
347
try :
348
348
context .set_connection (context .get_connect_func ()())
@@ -356,7 +356,7 @@ def establish_connection(self, context: LimitlessConnectionContext) -> None:
356
356
try :
357
357
selected_host_info = self ._plugin_service .get_host_info_by_strategy (
358
358
HostRole .WRITER , "weighted_random" , context .get_limitless_routers ())
359
- logger .debug ("LimitlessRouterServiceImpl .SelectedHost" , "None" if selected_host_info is None else selected_host_info .host )
359
+ logger .debug ("LimitlessRouterService .SelectedHost" , "None" if selected_host_info is None else selected_host_info .host )
360
360
except Exception as e :
361
361
if self ._is_login_exception (e ) or isinstance (e , UnsupportedOperationError ):
362
362
raise e
@@ -375,7 +375,7 @@ def establish_connection(self, context: LimitlessConnectionContext) -> None:
375
375
raise e
376
376
377
377
if selected_host_info is not None :
378
- logger .debug ("LimitlessRouterServiceImpl .FailedToConnectToHost" , selected_host_info .host )
378
+ logger .debug ("LimitlessRouterService .FailedToConnectToHost" , selected_host_info .host )
379
379
selected_host_info .set_availability (HostAvailability .UNAVAILABLE )
380
380
381
381
self ._retry_connection_with_least_loaded_routers (context )
@@ -400,7 +400,7 @@ def _retry_connection_with_least_loaded_routers(self, context: LimitlessConnecti
400
400
if (context .get_limitless_routers () is None
401
401
or len (context .get_limitless_routers ()) == 0
402
402
or not context .is_any_router_available ()):
403
- logger .debug ("LimitlessRouterServiceImpl .NoRoutersAvailableForRetry" )
403
+ logger .debug ("LimitlessRouterService .NoRoutersAvailableForRetry" )
404
404
405
405
if context .get_connection () is not None and not self ._plugin_service .driver_dialect .is_closed (context .get_connection ()):
406
406
return
@@ -417,14 +417,14 @@ def _retry_connection_with_least_loaded_routers(self, context: LimitlessConnecti
417
417
418
418
try :
419
419
selected_host_info = self ._plugin_service .get_host_info_by_strategy (
420
- HostRole .WRITER , "weighted_random " , context .get_limitless_routers ())
421
- logger .debug ("LimitlessRouterServiceImpl .SelectedHostForRetry" ,
420
+ HostRole .WRITER , "highest_weight " , context .get_limitless_routers ())
421
+ logger .debug ("LimitlessRouterService .SelectedHostForRetry" ,
422
422
"None" if selected_host_info is None else selected_host_info .host )
423
423
if selected_host_info is None :
424
424
continue
425
425
426
426
except UnsupportedOperationError as e :
427
- logger .error ("LimitlessRouterServiceImpl .IncorrectConfiguration" )
427
+ logger .error ("LimitlessRouterService .IncorrectConfiguration" )
428
428
raise e
429
429
except AwsWrapperError :
430
430
continue
@@ -438,14 +438,14 @@ def _retry_connection_with_least_loaded_routers(self, context: LimitlessConnecti
438
438
if self ._is_login_exception (e ):
439
439
raise e
440
440
selected_host_info .set_availability (HostAvailability .UNAVAILABLE )
441
- logger .debug ("LimitlessRouterServiceImpl .FailedToConnectToHost" , selected_host_info .host )
441
+ logger .debug ("LimitlessRouterService .FailedToConnectToHost" , selected_host_info .host )
442
442
443
443
raise AwsWrapperError (Messages .get ("LimitlessRouterService.MaxRetriesExceeded" ))
444
444
445
445
def _synchronously_get_limitless_routers_with_retry (self , context : LimitlessConnectionContext ) -> None :
446
- logger .debug ("LimitlessRouterServiceImpl .SynchronouslyGetLimitlessRouters" )
446
+ logger .debug ("LimitlessRouterService .SynchronouslyGetLimitlessRouters" )
447
447
retry_count = - 1
448
- max_retries = WrapperProperties .MAX_RETRIES_MS .get_int (context .get_props ())
448
+ max_retries = WrapperProperties .GET_ROUTER_MAX_RETRIES .get_int (context .get_props ())
449
449
retry_interval_ms = WrapperProperties .GET_ROUTER_RETRY_INTERVAL_MS .get_float (context .get_props ())
450
450
first_iteration = True
451
451
while first_iteration or retry_count < max_retries :
0 commit comments