private void open()

in kyuubi-relocated-hive-metastore-client/src/main/java/org/apache/kyuubi/shaded/hive/metastore/HiveMetaStoreClient.java [452:566]


  private void open() throws MetaException {
    isConnected = false;
    TTransportException tte = null;
    MetaException recentME = null;
    boolean useSSL = MetastoreConf.getBoolVar(conf, ConfVars.USE_SSL);
    boolean useCompactProtocol =
        MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_COMPACT_PROTOCOL);
    String transportMode =
        MetastoreConf.getVar(conf, ConfVars.METASTORE_CLIENT_THRIFT_TRANSPORT_MODE);
    boolean isHttpTransportMode = transportMode.equalsIgnoreCase("http");

    for (int attempt = 0; !isConnected && attempt < retries; ++attempt) {
      for (URI store : metastoreUris) {
        LOG.info(
            "Trying to connect to metastore with URI ({}) in {} transport mode",
            store,
            transportMode);
        try {
          try {
            if (isHttpTransportMode) {
              transport = createHttpClient(store, useSSL);
            } else {
              transport = createBinaryClient(store, useSSL);
            }
          } catch (TTransportException te) {
            tte = te;
            throw new MetaException(te.toString());
          }

          final TProtocol protocol;
          if (useCompactProtocol) {
            protocol = new TCompactProtocol(transport);
          } else {
            protocol = new TBinaryProtocol(transport);
          }
          client = new ThriftHiveMetastore.Client(protocol);
          try {
            if (!transport.isOpen()) {
              transport.open();
              final int newCount = connCount.incrementAndGet();
              if (useSSL) {
                LOG.info(
                    "Opened an SSL connection to metastore, current connections: {}", newCount);
                if (LOG.isTraceEnabled()) {
                  LOG.trace(
                      "METASTORE SSL CONNECTION TRACE - open [{}]",
                      System.identityHashCode(this),
                      new Exception());
                }
              } else {
                LOG.info(
                    "Opened a connection to metastore, URI ({}) " + "current connections: {}",
                    store,
                    newCount);
                if (LOG.isTraceEnabled()) {
                  LOG.trace(
                      "METASTORE CONNECTION TRACE - open [{}]",
                      System.identityHashCode(this),
                      new Exception());
                }
              }
            }
            isConnected = true;
          } catch (TTransportException e) {
            tte = e;
            String errMsg =
                String.format(
                    "Failed to connect to the MetaStore Server URI (%s) in %s " + "transport mode",
                    store, transportMode);
            LOG.warn(errMsg);
            LOG.debug(errMsg, e);
          }
        } catch (MetaException e) {
          recentME = e;
          String errMsg =
              "Failed to connect to metastore with URI ("
                  + store
                  + ") transport mode:"
                  + transportMode
                  + " in attempt "
                  + attempt;
          LOG.error(errMsg, e);
        }
        if (isConnected) {
          break;
        }
      }
      // Wait before launching the next round of connection retries.
      if (!isConnected && retryDelaySeconds > 0) {
        try {
          LOG.info("Waiting " + retryDelaySeconds + " seconds before next connection attempt.");
          Thread.sleep(retryDelaySeconds * 1000);
        } catch (InterruptedException ignore) {
        }
      }
    }

    if (!isConnected) {
      // Either tte or recentME should be set but protect from a bug which causes both of them to
      // be null. When MetaException wraps TTransportException, tte will be set so stringify that
      // directly.
      String exceptionString = "Unknown exception";
      if (tte != null) {
        exceptionString = StringUtils.stringifyException(tte);
      } else if (recentME != null) {
        exceptionString = StringUtils.stringifyException(recentME);
      }
      throw new MetaException(
          "Could not connect to meta store using any of the URIs provided."
              + " Most recent failure: "
              + exceptionString);
    }

    snapshotActiveConf();
  }