async failoverReader()

in common/lib/plugins/failover/failover_plugin.ts [327:373]


  async failoverReader(failedHostInfo: HostInfo) {
    logger.debug(Messages.get("Failover.startReaderFailover"));

    const telemetryFactory = this.pluginService.getTelemetryFactory();
    const telemetryContext = telemetryFactory.openTelemetryContext(FailoverPlugin.TELEMETRY_READER_FAILOVER, TelemetryTraceLevel.NESTED);
    this.failoverReaderTriggeredCounter.inc();

    const oldAliases = this.pluginService.getCurrentHostInfo()?.allAliases ?? new Set();

    let failedHost = null;
    if (failedHostInfo && failedHostInfo.getRawAvailability() === HostAvailability.AVAILABLE) {
      failedHost = failedHostInfo;
    }

    try {
      await telemetryContext.start(async () => {
        try {
          const result = await this._readerFailoverHandler.failover(this.pluginService.getHosts(), failedHost);

          if (result) {
            const error = result.error;
            if (error) {
              throw error;
            }
          }

          if (!result || !result.isConnected || !result.newHost || !result.client) {
            // "Unable to establish SQL connection to reader instance"
            throw new FailoverFailedError(Messages.get("Failover.unableToConnectToReader"));
          }

          await this.pluginService.abortCurrentClient();
          await this.pluginService.setCurrentClient(result.client, result.newHost);
          this.pluginService.getCurrentHostInfo()?.removeAlias(Array.from(oldAliases));
          await this.updateTopology(true);
          this.failoverWriterSuccessCounter.inc();
        } catch (error: any) {
          this.failoverReaderFailedCounter.inc();
          throw error;
        }
      });
    } finally {
      if (this.telemetryFailoverAdditionalTopTraceSetting) {
        await telemetryFactory.postCopy(telemetryContext, TelemetryTraceLevel.FORCE_TOP_LEVEL);
      }
    }
  }