@@ -30,10 +30,15 @@ import { CacheMap } from "../utils/cache_map";
3030import { isDialectTopologyAware , logTopology } from "../utils/utils" ;
3131import { DatabaseDialect } from "../database_dialect/database_dialect" ;
3232import { ClientWrapper } from "../client_wrapper" ;
33+ import { CoreServicesContainer } from "../utils/core_services_container" ;
34+ import { StorageService } from "../utils/storage/storage_service" ;
35+ import { Topology } from "./topology" ;
36+ import { ExpirationCache } from "../utils/storage/expiration_cache" ;
3337
3438export class RdsHostListProvider implements DynamicHostListProvider {
3539 private readonly originalUrl : string ;
3640 private readonly rdsHelper : RdsUtils ;
41+ private readonly storageService : StorageService ;
3742 protected readonly properties : Map < string , any > ;
3843 private rdsUrlType : RdsUrlType ;
3944 private initialHostList : HostInfo [ ] ;
@@ -45,9 +50,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
4550 protected readonly hostListProviderService : HostListProviderService ;
4651
4752 public static readonly suggestedPrimaryClusterIdCache : CacheMap < string , string > = new CacheMap < string , string > ( ) ;
48-
4953 public static readonly primaryClusterIdCache : CacheMap < string , boolean > = new CacheMap < string , boolean > ( ) ;
50- public static readonly topologyCache : CacheMap < string , HostInfo [ ] > = new CacheMap < string , HostInfo [ ] > ( ) ;
5154 public clusterId : string = Date . now ( ) . toString ( ) ;
5255 public isInitialized : boolean = false ;
5356 public isPrimaryClusterId ?: boolean ;
@@ -59,6 +62,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
5962 this . connectionUrlParser = hostListProviderService . getConnectionUrlParser ( ) ;
6063 this . originalUrl = originalUrl ;
6164 this . properties = properties ;
65+ this . storageService = CoreServicesContainer . getInstance ( ) . getStorageService ( ) ; // TODO: store the service container instead.
6266
6367 let port = WrapperProperties . PORT . get ( properties ) ;
6468 if ( port == null ) {
@@ -197,7 +201,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
197201 this . isPrimaryClusterId = true ;
198202 }
199203
200- const cachedHosts : HostInfo [ ] | null = RdsHostListProvider . topologyCache . get ( this . clusterId ) ;
204+ const cachedHosts : HostInfo [ ] | null = this . getStoredTopology ( ) ;
201205
202206 // This clusterId is a primary one and is about to create a new entry in the cache.
203207 // When a primary entry is created it needs to be suggested for other (non-primary) entries.
@@ -211,7 +215,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
211215
212216 const hosts = await this . queryForTopology ( targetClient , this . hostListProviderService . getDialect ( ) ) ;
213217 if ( hosts && hosts . length > 0 ) {
214- RdsHostListProvider . topologyCache . put ( this . clusterId , hosts , this . refreshRateNano ) ;
218+ this . storageService . set ( this . clusterId , new Topology ( hosts ) ) ;
215219 if ( needToSuggest ) {
216220 this . suggestPrimaryCluster ( hosts ) ;
217221 }
@@ -227,14 +231,18 @@ export class RdsHostListProvider implements DynamicHostListProvider {
227231 }
228232
229233 private getSuggestedClusterId ( hostAndPort : string ) : ClusterSuggestedResult | null {
230- for ( const [ key , hosts ] of RdsHostListProvider . topologyCache . getEntries ( ) ) {
234+ const cache : ExpirationCache < string , Topology > = this . storageService . getAll ( Topology ) as ExpirationCache < string , Topology > ;
235+ if ( ! cache ) {
236+ return null ;
237+ }
238+ for ( const [ key , hosts ] of cache . getEntries ( ) ) {
231239 const isPrimaryCluster : boolean = RdsHostListProvider . primaryClusterIdCache . get ( key , false , this . suggestedClusterIdRefreshRateNano ) ?? false ;
232240 if ( key === hostAndPort ) {
233241 return new ClusterSuggestedResult ( hostAndPort , isPrimaryCluster ) ;
234242 }
235243
236244 if ( hosts ) {
237- for ( const hostInfo of hosts ) {
245+ for ( const hostInfo of hosts . hosts ) {
238246 if ( hostInfo . hostAndPort === hostAndPort ) {
239247 logger . debug ( Messages . get ( "RdsHostListProvider.suggestedClusterId" , key , hostAndPort ) ) ;
240248 return new ClusterSuggestedResult ( key , isPrimaryCluster ) ;
@@ -255,7 +263,11 @@ export class RdsHostListProvider implements DynamicHostListProvider {
255263 primaryClusterHostUrls . add ( hostInfo . url ) ;
256264 } ) ;
257265
258- for ( const [ clusterId , clusterHosts ] of RdsHostListProvider . topologyCache . getEntries ( ) ) {
266+ const cache : ExpirationCache < string , Topology > = this . storageService . getAll ( Topology ) as ExpirationCache < string , Topology > ;
267+ if ( ! cache ) {
268+ return ;
269+ }
270+ for ( const [ clusterId , clusterHosts ] of cache . getEntries ( ) ) {
259271 const isPrimaryCluster : boolean | null = RdsHostListProvider . primaryClusterIdCache . get (
260272 clusterId ,
261273 false ,
@@ -266,7 +278,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
266278 continue ;
267279 }
268280
269- for ( const clusterHost of clusterHosts ) {
281+ for ( const clusterHost of clusterHosts . hosts ) {
270282 if ( primaryClusterHostUrls . has ( clusterHost . url ) ) {
271283 RdsHostListProvider . suggestedPrimaryClusterIdCache . put ( clusterId , this . clusterId , this . suggestedClusterIdRefreshRateNano ) ;
272284 break ;
@@ -343,22 +355,24 @@ export class RdsHostListProvider implements DynamicHostListProvider {
343355 return host . replace ( "?" , hostName ) ;
344356 }
345357
346- getCachedTopology ( ) : HostInfo [ ] | null {
358+ getStoredTopology ( ) : HostInfo [ ] | null {
347359 if ( ! this . clusterId ) {
348360 return null ;
349361 }
350- return RdsHostListProvider . topologyCache . get ( this . clusterId ) ?? null ;
362+
363+ const topology : Topology = this . storageService . get ( Topology , this . clusterId ) ;
364+
365+ return topology == null ? null : topology . hosts ;
351366 }
352367
353368 static clearAll ( ) : void {
354- RdsHostListProvider . topologyCache . clear ( ) ;
355369 RdsHostListProvider . primaryClusterIdCache . clear ( ) ;
356370 RdsHostListProvider . suggestedPrimaryClusterIdCache . clear ( ) ;
357371 }
358372
359373 clear ( ) : void {
360374 if ( this . clusterId ) {
361- RdsHostListProvider . topologyCache . delete ( this . clusterId ) ;
375+ CoreServicesContainer . getInstance ( ) . getStorageService ( ) . remove ( Topology , this . clusterId ) ;
362376 }
363377 }
364378
0 commit comments