protected List openAnyConnectionAndUpdateTopology()

in wrapper/src/main/java/software/amazon/jdbc/hostlistprovider/monitoring/ClusterTopologyMonitorImpl.java [498:570]


  protected List<HostSpec> openAnyConnectionAndUpdateTopology() {
    boolean writerVerifiedByThisThread = false;
    if (this.monitoringConnection.get() == null) {

      Connection conn;

      // open a new connection
      try {
        conn = this.pluginService.forceConnect(this.initialHostSpec, this.monitoringProperties);
      } catch (SQLException ex) {
        // can't connect
        return null;
      }

      if (this.monitoringConnection.compareAndSet(null, conn)) {
        LOGGER.finest(() -> Messages.get(
            "ClusterTopologyMonitorImpl.openedMonitoringConnection",
            new Object[]{this.initialHostSpec.getHost()}));

        try {
          if (!StringUtils.isNullOrEmpty(this.getWriterNodeId(this.monitoringConnection.get()))) {
            this.isVerifiedWriterConnection = true;
            writerVerifiedByThisThread = true;

            if (rdsHelper.isRdsInstance(this.initialHostSpec.getHost())) {
              this.writerHostSpec.set(this.initialHostSpec);
              LOGGER.finest(
                  Messages.get(
                      "ClusterTopologyMonitorImpl.writerMonitoringConnection",
                      new Object[]{this.writerHostSpec.get().getHost()}));
            } else {
              final String nodeId = this.getNodeId(this.monitoringConnection.get());
              if (!StringUtils.isNullOrEmpty(nodeId)) {
                this.writerHostSpec.set(this.createHost(nodeId, true, 0, null));
                LOGGER.finest(
                    Messages.get(
                        "ClusterTopologyMonitorImpl.writerMonitoringConnection",
                        new Object[]{this.writerHostSpec.get().getHost()}));
              }
            }
          }
        } catch (SQLException ex) {
          // do nothing
        }

      } else {
        // monitoring connection has already been set by other thread
        // close new connection as we don't need it
        this.closeConnection(conn);
      }
    }

    final List<HostSpec> hosts = this.fetchTopologyAndUpdateCache(this.monitoringConnection.get());
    if (writerVerifiedByThisThread) {
      // We verify the writer on initial connection and on failover, but we only want to ignore new topology
      // requests after failover. To accomplish this, the first time we verify the writer we set the ignore end
      // time to 0. Any future writer verifications will set it to a positive value.
      if (!this.ignoreNewTopologyRequestsEndTimeNano.compareAndSet(-1, 0)) {
        this.ignoreNewTopologyRequestsEndTimeNano.set(System.nanoTime() + ignoreTopologyRequestNano);
      }
    }

    if (hosts == null) {
      // can't get topology; it might be something's wrong with a connection
      // close connection
      Connection connToClose = this.monitoringConnection.get();
      this.monitoringConnection.set(null);
      this.closeConnection(connToClose);
      this.isVerifiedWriterConnection = false;
    }

    return hosts;
  }