async getTopology()

in common/lib/host_list_provider/rds_host_list_provider.ts [187:227]


  async getTopology(targetClient: ClientWrapper | undefined, forceUpdate: boolean): Promise<FetchTopologyResult> {
    this.init();

    if (!this.clusterId) {
      throw new AwsWrapperError("no cluster id");
    }

    const suggestedPrimaryClusterId: string | null = RdsHostListProvider.suggestedPrimaryClusterIdCache.get(this.clusterId);
    if (suggestedPrimaryClusterId && this.clusterId !== suggestedPrimaryClusterId) {
      this.clusterId = suggestedPrimaryClusterId;
      this.isPrimaryClusterId = true;
    }

    const cachedHosts: HostInfo[] | null = RdsHostListProvider.topologyCache.get(this.clusterId);

    // This clusterId is a primary one and is about to create a new entry in the cache.
    // When a primary entry is created it needs to be suggested for other (non-primary) entries.
    // Remember a flag to do suggestion after cache is updated.
    const needToSuggest: boolean = !cachedHosts && this.isPrimaryClusterId === true;
    if (!cachedHosts || forceUpdate) {
      // need to re-fetch the topology.
      if (!targetClient || !(await this.hostListProviderService.isClientValid(targetClient))) {
        return new FetchTopologyResult(false, this.initialHostList);
      }

      const hosts = await this.queryForTopology(targetClient, this.hostListProviderService.getDialect());
      if (hosts && hosts.length > 0) {
        RdsHostListProvider.topologyCache.put(this.clusterId, hosts, this.refreshRateNano);
        if (needToSuggest) {
          this.suggestPrimaryCluster(hosts);
        }
        return new FetchTopologyResult(false, hosts);
      }
    }

    if (!cachedHosts) {
      return new FetchTopologyResult(false, this.initialHostList);
    } else {
      return new FetchTopologyResult(true, cachedHosts);
    }
  }