private void retryConnectWithLeastLoadedRouters()

in wrapper/src/main/java/software/amazon/jdbc/plugin/limitless/LimitlessRouterServiceImpl.java [177:240]


  private void retryConnectWithLeastLoadedRouters(
      final LimitlessConnectionContext context) throws SQLException {

    int retryCount = 0;
    final int maxRetries = LimitlessConnectionPlugin.MAX_RETRIES.getInteger(context.getProps());

    while (retryCount++ < maxRetries) {
      if (Utils.isNullOrEmpty(context.getLimitlessRouters())
          || context.getLimitlessRouters()
          .stream()
          .noneMatch(h -> h.getAvailability().equals(HostAvailability.AVAILABLE))) {
        synchronouslyGetLimitlessRoutersWithRetry(context);

        if (Utils.isNullOrEmpty(context.getLimitlessRouters())
            || context.getLimitlessRouters()
              .stream()
              .noneMatch(h -> h.getAvailability().equals(HostAvailability.AVAILABLE))) {
          LOGGER.warning(Messages.get("LimitlessRouterServiceImpl.noRoutersAvailableForRetry"));
          if (context.getConnection() != null && !context.getConnection().isClosed()) {
            return;
          } else {
            try {
              context.setConnection(context.getConnectFunc().call());
              return;
            } catch (final SQLException e) {
              throw new SQLException(Messages.get("LimitlessRouterServiceImpl.noRoutersAvailable"));
            }
          }
        }
      }

      final HostSpec selectedHostSpec;
      try {
        // Select healthiest router for best chance of connection over load-balancing with round-robin
        selectedHostSpec = this.pluginService.getHostSpecByStrategy(context.getLimitlessRouters(),
            HostRole.WRITER, HighestWeightHostSelector.STRATEGY_HIGHEST_WEIGHT);
        LOGGER.finest(Messages.get(
            "LimitlessRouterServiceImpl.selectedHostForRetry",
            new Object[] {selectedHostSpec != null ? selectedHostSpec.getHost() : "null"}));
        if (selectedHostSpec == null) {
          continue;
        }
      } catch (final UnsupportedOperationException e) {
        LOGGER.severe(Messages.get("LimitlessRouterServiceImpl.incorrectConfiguration"));
        throw e;
      } catch (final SQLException e) {
        // error from host selector
        continue;
      }

      try {
        context.setConnection(pluginService.connect(selectedHostSpec, context.getProps(), context.getPlugin()));
        if (context.getConnection() != null) {
          return;
        }
      } catch (final SQLException e) {
        selectedHostSpec.setAvailability(HostAvailability.NOT_AVAILABLE);
        LOGGER.finest(Messages.get(
            "LimitlessRouterServiceImpl.failedToConnectToHost",
            new Object[] {selectedHostSpec.getHost()}));
      }
    }
    throw new SQLException(Messages.get("LimitlessRouterServiceImpl.maxRetriesExceeded"));
  }