@SuppressWarnings({
    "squid:S3776"
  }) // ignore Cognitive Complexity of methods should not be too high
  private boolean reconnect() {
    boolean connectedSuccess = false;
    SecureRandom random = new SecureRandom();
    for (int i = 1; i <= SessionConfig.RETRY_NUM; i++) {
      if (transport != null) {
        transport.close();
        endPointList = availableNodes.get();
        int currHostIndex = random.nextInt(endPointList.size());
        int tryHostNum = 0;
        for (int j = currHostIndex; j < endPointList.size(); j++) {
          if (tryHostNum == endPointList.size()) {
            break;
          }
          this.endPoint = endPointList.get(j);
          if (j == endPointList.size() - 1) {
            j = -1;
          }
          tryHostNum++;
          try {
            init(endPoint, session.useSSL, session.trustStore, session.trustStorePwd);
            connectedSuccess = true;
          } catch (IoTDBConnectionException e) {
            logger.warn("The current node may have been down {}, try next node", endPoint);
            continue;
          } catch (StatementExecutionException e) {
            logger.warn("login in failed, because {}", e.getMessage());
          }
          break;
        }
      }
      if (connectedSuccess) {
        // remove the broken end point
        session.removeBrokenSessionConnection(this);
        session.defaultEndPoint = this.endPoint;
        session.defaultSessionConnection = this;
        if (session.endPointToSessionConnection == null) {
          session.endPointToSessionConnection = new ConcurrentHashMap<>();
        }
        session.endPointToSessionConnection.compute(
            session.defaultEndPoint,
            (k, v) -> {
              if (v != null && v.transport != null && v.transport.isOpen()) {
                try {
                  v.close();
                } catch (IoTDBConnectionException e) {
                  logger.warn("close connection failed, {}", e.getMessage());
                }
              }
              return this;
            });
        break;
      }
    }
    return connectedSuccess;
  }
reconnect if the remote datanode is unreachable retry if the status is set to needRetry
iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java