async failoverReader()

in common/lib/plugins/failover2/failover2_plugin.ts [249:285]


  async failoverReader() {
    const telemetryFactory = this.pluginService.getTelemetryFactory();
    const telemetryContext = telemetryFactory.openTelemetryContext(Failover2Plugin.TELEMETRY_READER_FAILOVER, TelemetryTraceLevel.NESTED);
    this.failoverReaderTriggeredCounter.inc();

    const oldAliases = this.pluginService.getCurrentHostInfo()?.allAliases ?? new Set();
    const failoverEndTimeMs = Date.now() + this.failoverTimeoutSettingMs;

    try {
      logger.debug(Messages.get("Failover.startReaderFailover"));
      await telemetryContext.start(async () => {
        if (!(await this.pluginService.forceMonitoringRefresh(false, 0))) {
          // Unable to establish SQL connection to an instance.
          this.failoverReaderFailedCounter.inc();
          logger.error(Messages.get("Failover2.unableToFetchTopology"));
          throw new FailoverFailedError(Messages.get("Failover2.unableToFetchTopology"));
        }
        try {
          const result: ReaderFailoverResult = await this.getReaderFailoverConnection(failoverEndTimeMs);
          logger.info(Messages.get("Failover.establishedConnection", result.newHost.host));
          this.failoverReaderSuccessCounter.inc();
          await this.pluginService.abortCurrentClient();
          await this.pluginService.setCurrentClient(result.client, result.newHost);
          this.pluginService.getCurrentHostInfo()?.removeAlias(Array.from(oldAliases));
          await this.pluginService.forceRefreshHostList();
        } catch (error) {
          this.failoverReaderFailedCounter.inc();
          logger.error(Messages.get("Failover.unableToConnectToReader"));
          throw new FailoverFailedError(Messages.get("Failover.unableToConnectToReader"));
        }
      });
    } finally {
      if (this.telemetryFailoverAdditionalTopTraceSetting) {
        await telemetryFactory.postCopy(telemetryContext, TelemetryTraceLevel.FORCE_TOP_LEVEL);
      }
    }
  }