protected void failoverWriter()

in wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java [677:746]


  protected void failoverWriter() throws SQLException {
    TelemetryFactory telemetryFactory = this.pluginService.getTelemetryFactory();
    TelemetryContext telemetryContext = telemetryFactory.openTelemetryContext(
        TELEMETRY_WRITER_FAILOVER, TelemetryTraceLevel.NESTED);
    this.failoverWriterTriggeredCounter.inc();

    long failoverStartTimeNano = System.nanoTime();

    try {
      LOGGER.info(() -> Messages.get("Failover.startWriterFailover"));
      final WriterFailoverResult failoverResult = this.writerFailoverHandler.failover(this.pluginService.getAllHosts());
      if (failoverResult != null) {
        final SQLException exception = failoverResult.getException();
        if (exception != null) {
          throw exception;
        }
      }

      if (failoverResult == null || !failoverResult.isConnected()) {
        throwFailoverFailedException(Messages.get("Failover.unableToConnectToWriter"));
        return;
      }

      List<HostSpec> hosts = failoverResult.getTopology();
      final HostSpec writerHostSpec = getWriter(hosts);
      if (writerHostSpec == null) {
        throwFailoverFailedException(
            Messages.get(
                "Failover.noWriterHostAfterReconnecting",
                new Object[]{Utils.logTopology(hosts, "")}));
        return;
      }

      final List<HostSpec> allowedHosts = this.pluginService.getHosts();
      if (!Utils.containsUrl(allowedHosts, writerHostSpec.getUrl())) {
        throwFailoverFailedException(
            Messages.get("Failover.newWriterNotAllowed",
                new Object[] {writerHostSpec.getUrl(), Utils.logTopology(allowedHosts, "")}));
        return;
      }

      this.pluginService.setCurrentConnection(failoverResult.getNewConnection(), writerHostSpec);

      LOGGER.fine(
          () -> Messages.get(
              "Failover.establishedConnection",
              new Object[]{this.pluginService.getCurrentHostSpec()}));

      this.pluginService.refreshHostList();
      throwFailoverSuccessException();
    } catch (FailoverSuccessSQLException ex) {
      telemetryContext.setSuccess(true);
      telemetryContext.setException(ex);
      this.failoverWriterSuccessCounter.inc();
      throw ex;
    } catch (Exception ex) {
      telemetryContext.setSuccess(false);
      telemetryContext.setException(ex);
      this.failoverWriterFailedCounter.inc();
      throw ex;
    } finally {
      LOGGER.finest(() -> Messages.get(
          "Failover.writerFailoverElapsed",
          new Object[]{TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - failoverStartTimeNano)}));
      telemetryContext.closeContext();
      if (this.telemetryFailoverAdditionalTopTraceSetting) {
        telemetryFactory.postCopy(telemetryContext, TelemetryTraceLevel.FORCE_TOP_LEVEL);
      }
    }
  }