1515*/
1616
1717import { HostInfo } from "../../host_info" ;
18- import { CacheMap } from "../../utils/cache_map" ;
1918import { PluginService } from "../../plugin_service" ;
2019import { HostAvailability } from "../../host_availability/host_availability" ;
2120import { logTopology , sleep } from "../../utils/utils" ;
@@ -27,7 +26,9 @@ import { MonitoringRdsHostListProvider } from "./monitoring_host_list_provider";
2726import { Messages } from "../../utils/messages" ;
2827import { CoreServicesContainer } from "../../utils/core_services_container" ;
2928import { Topology } from "../topology" ;
30- import { StorageService , StorageServiceImpl } from "../../utils/storage/storage_service" ;
29+ import { StorageService } from "../../utils/storage/storage_service" ;
30+ import { TopologyUtils } from "../topology_utils" ;
31+ import { RdsUtils } from "../../utils/rds_utils" ;
3132
3233export interface ClusterTopologyMonitor {
3334 forceRefresh ( client : ClientWrapper , timeoutMs : number ) : Promise < HostInfo [ ] > ;
@@ -49,6 +50,9 @@ export class ClusterTopologyMonitorImpl implements ClusterTopologyMonitor {
4950 private readonly refreshRateMs : number ;
5051 private readonly highRefreshRateMs : number ;
5152 private readonly storageService : StorageService ;
53+ private readonly topologyUtils : TopologyUtils ;
54+ private readonly rdsUtils : RdsUtils = new RdsUtils ( ) ;
55+ private readonly instanceTemplate : HostInfo ;
5256
5357 private writerHostInfo : HostInfo = null ;
5458 private isVerifiedWriterConnection : boolean = false ;
@@ -74,17 +78,21 @@ export class ClusterTopologyMonitorImpl implements ClusterTopologyMonitor {
7478 private requestToUpdateTopology : boolean = false ;
7579
7680 constructor (
81+ topologyUtils : TopologyUtils ,
7782 clusterId : string ,
7883 initialHostInfo : HostInfo ,
7984 props : Map < string , any > ,
85+ instanceTemplate : HostInfo ,
8086 pluginService : PluginService ,
8187 hostListProvider : MonitoringRdsHostListProvider ,
8288 refreshRateMs : number ,
8389 highRefreshRateMs : number
8490 ) {
91+ this . topologyUtils = topologyUtils ;
8592 this . clusterId = clusterId ;
8693 this . storageService = CoreServicesContainer . getInstance ( ) . getStorageService ( ) ; // TODO: store serviceContainer instead
8794 this . initialHostInfo = initialHostInfo ;
95+ this . instanceTemplate = instanceTemplate ;
8896 this . _pluginService = pluginService ;
8997 this . _hostListProvider = hostListProvider ;
9098 this . refreshRateMs = refreshRateMs ;
@@ -212,12 +220,19 @@ export class ClusterTopologyMonitorImpl implements ClusterTopologyMonitor {
212220 this . monitoringClient = client ;
213221 logger . debug ( Messages . get ( "ClusterTopologyMonitor.openedMonitoringConnection" , this . initialHostInfo . host ) ) ;
214222 try {
215- const writerId = await this . getWriterHostIdIfConnected ( this . monitoringClient , this . initialHostInfo . hostId ) ;
216- if ( writerId ) {
223+ if ( await this . topologyUtils . isWriterInstance ( this . monitoringClient ) ) {
217224 this . isVerifiedWriterConnection = true ;
218- this . writerHostInfo = this . initialHostInfo ;
219- logger . info ( Messages . get ( "ClusterTopologyMonitor.writerMonitoringConnection" , this . initialHostInfo . host ) ) ;
220- writerVerifiedByThisTask = true ;
225+
226+ if ( this . rdsUtils . isRdsInstance ( this . initialHostInfo . host ) ) {
227+ this . writerHostInfo = this . initialHostInfo ;
228+ logger . info ( Messages . get ( "ClusterTopologyMonitor.writerMonitoringConnection" , this . writerHostInfo . host ) ) ;
229+ writerVerifiedByThisTask = true ;
230+ } else {
231+ const pair : [ string , string ] = await this . topologyUtils . getInstanceId ( this . monitoringClient ) ;
232+ const instanceTemplate : HostInfo = await this . getInstanceTemplate ( pair [ 1 ] , this . monitoringClient ) ;
233+ this . writerHostInfo = this . topologyUtils . createHost ( pair [ 0 ] , pair [ 1 ] , true , 0 , Date . now ( ) , this . initialHostInfo , instanceTemplate ) ;
234+ logger . debug ( Messages . get ( "ClusterTopologyMonitor.writerMonitoringConnection" , this . writerHostInfo . host ) ) ;
235+ }
221236 }
222237 } catch ( error ) {
223238 // Do nothing.
@@ -245,6 +260,10 @@ export class ClusterTopologyMonitorImpl implements ClusterTopologyMonitor {
245260 return hosts ;
246261 }
247262
263+ protected getInstanceTemplate ( hostId : string , targetClient : ClientWrapper ) : Promise < HostInfo > {
264+ return Promise . resolve ( this . instanceTemplate ) ;
265+ }
266+
248267 updateTopologyCache ( hosts : HostInfo [ ] ) : void {
249268 this . storageService . set ( this . clusterId , new Topology ( hosts ) ) ;
250269 this . requestToUpdateTopology = false ;
@@ -513,7 +532,7 @@ export class HostMonitor {
513532 let hosts : HostInfo [ ] ;
514533 try {
515534 hosts = await this . monitor . hostListProvider . sqlQueryForTopology ( client ) ;
516- if ( hosts === null || hosts . length === 0 ) {
535+ if ( hosts === null ) {
517536 return ;
518537 }
519538 this . monitor . hostMonitorsLatestTopology = hosts ;
0 commit comments