private void openSession()

in kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java [792:896]


  private void openSession() throws SQLException {
    TOpenSessionReq openReq = new TOpenSessionReq();

    Map<String, String> openConf = new HashMap<>();
    // for remote JDBC client, try to set the conf var using 'set foo=bar'
    for (Entry<String, String> hiveConf : connParams.getHiveConfs().entrySet()) {
      openConf.put("set:hiveconf:" + hiveConf.getKey(), hiveConf.getValue());
    }
    // For remote JDBC client, try to set the hive var using 'set hivevar:key=value'
    for (Entry<String, String> hiveVar : connParams.getHiveVars().entrySet()) {
      openConf.put("set:hivevar:" + hiveVar.getKey(), hiveVar.getValue());
    }
    // switch the catalog
    if (connParams.getCatalogName() != null) {
      openConf.put("use:catalog", connParams.getCatalogName());
    }
    // switch the database
    openConf.put("use:database", connParams.getDbName());
    if (wmPool != null) {
      openConf.put("set:hivevar:wmpool", wmPool);
    }
    if (wmApp != null) {
      openConf.put("set:hivevar:wmapp", wmApp);
    }

    // set the session configuration
    Map<String, String> sessVars = connParams.getSessionVars();
    if (sessVars.containsKey(HS2_PROXY_USER)) {
      openConf.put(HS2_PROXY_USER, sessVars.get(HS2_PROXY_USER));
    }
    String clientProtocolStr =
        sessVars.getOrDefault(
            CLIENT_PROTOCOL_VERSION, openReq.getClient_protocol().getValue() + "");
    TProtocolVersion clientProtocol =
        TProtocolVersion.findByValue(Integer.parseInt(clientProtocolStr));
    if (clientProtocol == null) {
      throw new IllegalArgumentException(
          String.format(
              "Unsupported Hive2 protocol version %s specified by session conf key %s",
              clientProtocolStr, CLIENT_PROTOCOL_VERSION));
    }
    openReq.setClient_protocol(clientProtocol);
    // HIVE-14901: set the fetchSize
    if (clientProtocol.compareTo(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V10) >= 0) {
      openConf.put(
          "set:hiveconf:hive.server2.thrift.resultset.default.fetch.size",
          Integer.toString(fetchSize));
    }
    openConf.put("kyuubi.client.ipAddress", Utils.CLIENT_IP_ADDRESS);
    openConf.put(Utils.KYUUBI_CLIENT_VERSION_KEY, Utils.getVersion());
    openReq.setConfiguration(openConf);

    // Store the user name in the open request in case no non-sasl authentication
    if (AUTH_SIMPLE.equals(sessConfMap.get(AUTH_TYPE))) {
      openReq.setUsername(sessConfMap.get(AUTH_USER));
      openReq.setPassword(sessConfMap.get(AUTH_PASSWD));
    }

    try {
      TOpenSessionResp openResp = client.OpenSession(openReq);

      // validate connection
      Utils.verifySuccess(openResp.getStatus());
      if (!supportedProtocols.contains(openResp.getServerProtocolVersion())) {
        throw new TException("Unsupported Hive2 protocol");
      }
      protocol = openResp.getServerProtocolVersion();
      sessHandle = openResp.getSessionHandle();

      Map<String, String> openRespConf = openResp.getConfiguration();
      // Update fetchSize if modified by server
      String serverFetchSize = openRespConf.get("hive.server2.thrift.resultset.default.fetch.size");
      if (serverFetchSize != null) {
        fetchSize = Integer.parseInt(serverFetchSize);
      }

      // Get launch engine operation handle
      String launchEngineOpHandleGuid =
          openRespConf.get("kyuubi.session.engine.launch.handle.guid");
      String launchEngineOpHandleSecret =
          openRespConf.get("kyuubi.session.engine.launch.handle.secret");

      launchEngineOpSupportResult =
          Boolean.parseBoolean(
              openRespConf.getOrDefault("kyuubi.session.engine.launch.support.result", "false"));

      if (launchEngineOpHandleGuid != null && launchEngineOpHandleSecret != null) {
        try {
          byte[] guidBytes = Base64.getDecoder().decode(launchEngineOpHandleGuid);
          byte[] secretBytes = Base64.getDecoder().decode(launchEngineOpHandleSecret);
          THandleIdentifier handleIdentifier =
              new THandleIdentifier(ByteBuffer.wrap(guidBytes), ByteBuffer.wrap(secretBytes));
          launchEngineOpHandle =
              new TOperationHandle(handleIdentifier, TOperationType.UNKNOWN, false);
        } catch (Exception e) {
          LOG.error("Failed to decode launch engine operation handle from open session resp", e);
        }
      }
    } catch (TException e) {
      LOG.error("Error opening session", e);
      throw new KyuubiSQLException(
          "Could not establish connection to " + jdbcUriString + ": " + e.getMessage(), "08S01", e);
    }
    isClosed = false;
  }