protected void failoverReader()

in wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java [342:392]


  protected void failoverReader() throws SQLException {
    TelemetryFactory telemetryFactory = this.pluginService.getTelemetryFactory();
    TelemetryContext telemetryContext = telemetryFactory.openTelemetryContext(
        TELEMETRY_READER_FAILOVER, TelemetryTraceLevel.NESTED);
    this.failoverReaderTriggeredCounter.inc();

    final long failoverStartNano = System.nanoTime();
    final long failoverEndNano = failoverStartNano + TimeUnit.MILLISECONDS.toNanos(this.failoverTimeoutMsSetting);

    try {
      LOGGER.fine(() -> Messages.get("Failover.startReaderFailover"));
      // When we pass a timeout of 0, we inform the plugin service that it should update its topology without waiting
      // for it to get updated, since we do not need updated topology to establish a reader connection.
      if (!this.pluginService.forceRefreshHostList(false, 0)) {
        LOGGER.severe(Messages.get("Failover.failoverReaderUnableToRefreshHostList"));
        throw new FailoverFailedSQLException(Messages.get("Failover.failoverReaderUnableToRefreshHostList"));
      }

      try {
        ReaderFailoverResult result = getReaderFailoverConnection(failoverEndNano);
        this.pluginService.setCurrentConnection(result.getConnection(), result.getHostSpec());
      } catch (TimeoutException e) {
        LOGGER.severe(Messages.get("Failover.unableToConnectToReader"));
        throw new FailoverFailedSQLException(Messages.get("Failover.unableToConnectToReader"));
      }

      LOGGER.info(
          () -> Messages.get(
              "Failover.establishedConnection",
              new Object[] {this.pluginService.getCurrentHostSpec()}));
      throwFailoverSuccessException();
    } catch (FailoverSuccessSQLException ex) {
      this.failoverReaderSuccessCounter.inc();
      telemetryContext.setSuccess(true);
      telemetryContext.setException(ex);
      throw ex;
    } catch (Exception ex) {
      telemetryContext.setSuccess(false);
      telemetryContext.setException(ex);
      this.failoverReaderFailedCounter.inc();
      throw ex;
    } finally {
      LOGGER.finest(() -> Messages.get(
              "Failover.readerFailoverElapsed",
              new Object[]{TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - failoverStartNano)}));
      telemetryContext.closeContext();
      if (this.telemetryFailoverAdditionalTopTraceSetting) {
        telemetryFactory.postCopy(telemetryContext, TelemetryTraceLevel.FORCE_TOP_LEVEL);
      }
    }
  }