async getVerifiedConnection()

in common/lib/plugins/stale_dns/stale_dns_helper.ts [49:144]


  async getVerifiedConnection<Type>(
    host: string,
    isInitialConnection: boolean,
    hostListProviderService: HostListProviderService,
    props: Map<string, any>,
    connectFunc: () => Promise<ClientWrapper>
  ): Promise<ClientWrapper> {
    if (!this.rdsUtils.isWriterClusterDns(host)) {
      return connectFunc();
    }

    const currentTargetClient = await connectFunc();

    let clusterInetAddress = "";
    try {
      const lookupResult = await this.lookupResult(host);
      clusterInetAddress = lookupResult.address;
    } catch (error) {
      // ignore
    }

    const hostInetAddress = clusterInetAddress;
    logger.debug(Messages.get("StaleDnsHelper.clusterEndpointDns", hostInetAddress));

    if (!clusterInetAddress) {
      return currentTargetClient;
    }

    const currentHostInfo = this.pluginService.getCurrentHostInfo();
    if (!currentHostInfo) {
      throw new AwsWrapperError("Stale DNS Helper: Current hostInfo was null.");
    }

    if (currentHostInfo && currentHostInfo.role === HostRole.READER) {
      // This is if-statement is only reached if the connection url is a writer cluster endpoint.
      // If the new connection resolves to a reader instance, this means the topology is outdated.
      // Force refresh to update the topology.
      await this.pluginService.forceRefreshHostList(currentTargetClient);
    } else {
      await this.pluginService.refreshHostList(currentTargetClient);
    }

    logger.debug(logTopology(this.pluginService.getAllHosts(), "[StaleDnsHelper.getVerifiedConnection] "));

    if (!this.writerHostInfo) {
      const writerCandidate = getWriter(this.pluginService.getHosts());
      if (writerCandidate && this.rdsUtils.isRdsClusterDns(writerCandidate.host)) {
        return currentTargetClient;
      }
      this.writerHostInfo = writerCandidate;
    }

    logger.debug(Messages.get("StaleDnsHelper.writerHostInfo", this.writerHostInfo?.host ?? ""));

    if (!this.writerHostInfo) {
      return currentTargetClient;
    }

    if (!this.writerHostAddress) {
      try {
        const lookupResult = await this.lookupResult(this.writerHostInfo.host);
        this.writerHostAddress = lookupResult.address;
      } catch (error) {
        // ignore
      }
    }

    logger.debug(Messages.get("StaleDnsHelper.writerInetAddress", this.writerHostAddress));

    if (!this.writerHostAddress) {
      return currentTargetClient;
    }

    if (this.writerHostAddress !== clusterInetAddress) {
      // DNS resolves a cluster endpoint to a wrong writer
      // opens a connection to a proper writer host
      logger.debug(Messages.get("StaleDnsHelper.staleDnsDetected", this.writerHostInfo.host));
      this.staleDNSDetectedCounter.inc();

      let targetClient = null;
      try {
        const newProps = new Map<string, any>(props);
        newProps.set(WrapperProperties.HOST.name, this.writerHostInfo.host);
        targetClient = await this.pluginService.connect(this.writerHostInfo, newProps);
        await this.pluginService.abortTargetClient(currentTargetClient);

        if (isInitialConnection) {
          hostListProviderService.setInitialConnectionHostInfo(this.writerHostInfo);
        }
        return targetClient;
      } catch (error: any) {
        await this.pluginService.abortTargetClient(targetClient);
      }
    }
    return currentTargetClient;
  }