private TezSessionState getSession(HiveConf conf, boolean doOpen) throws Exception {
    // NOTE: this can be called outside of HS2, without calling setupPool. Basically it should be
    //       able to handle not being initialized. Perhaps we should get rid of the instance and
    //       move the setupPool code to ctor. For now, at least hasInitialSessions will be false.
    String queueName = conf.get(TezConfiguration.TEZ_QUEUE_NAME);
    boolean hasQueue = (queueName != null) && !queueName.isEmpty();
    if (hasQueue) {
      switch (customQueueAllowed) {
      case FALSE: throw new HiveException("Specifying "
          + TezConfiguration.TEZ_QUEUE_NAME + " is not allowed");
      case IGNORE: {
        LOG.warn("User has specified " + queueName + " queue; ignoring the setting");
        queueName = null;
        hasQueue = false;
        conf.unset(TezConfiguration.TEZ_QUEUE_NAME);
      }
      default: // All good.
      }

      if (yarnQueueChecker != null) {
        SessionState ss = SessionState.get();
        String userName = null;
        if (ss != null) {
          userName = ss.getAuthenticator() != null
              ? ss.getAuthenticator().getUserName() : ss.getUserName();
        }
        if (userName == null) {
          userName = Utils.getUGI().getShortUserName();
          LOG.info("No session user set; using the UGI user " + userName);
        }
        yarnQueueChecker.checkQueueAccess(queueName, userName);
      }
    }

    // Check the restricted configs that the users cannot set.
    if (restrictedConfig != null) {
      restrictedConfig.validate(conf);
    }

    // Propagate this value from HS2; don't allow users to set it.
    // In HS2, initConf will be set; it won't be set otherwise as noone calls setupPool.
    // TODO: add a section like the restricted configs for overrides when there's more than one.
    if (initConf != null) {
      conf.set(ConfVars.LLAP_CACHE_DEFAULT_FS_FILE_ID.varname,
          HiveConf.getVarWithoutType(initConf, ConfVars.LLAP_CACHE_DEFAULT_FS_FILE_ID));
    }

    // TODO Session re-use completely disabled for doAs=true. Always launches a new session.
    boolean nonDefaultUser = conf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS);

    boolean jobNameSet = !HiveConf.getVar(conf, ConfVars.HIVE_TEZ_JOB_NAME).equals("HIVE-%s");

    /*
     * if the user has specified a queue name themselves or job name is set, we create a new
     * session. also a new session is created if the user tries to submit to a queue using
     * their own credentials. We expect that with the new security model, things will
     * run as user hive in most cases.
     */
    if (nonDefaultUser || !hasInitialSessions || hasQueue || jobNameSet) {
      LOG.info("QueueName: {} nonDefaultUser: {} defaultQueuePool: {} hasInitialSessions: {}" +
                      " jobNameSet: {}.", queueName, nonDefaultUser, defaultSessionPool,
              hasInitialSessions, jobNameSet);
      return getNewSessionState(conf, queueName, doOpen);
    }

    LOG.info("Choosing a session from the defaultQueuePool");
    return defaultSessionPool.getSession();
  }
Note: this is not thread-safe.
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/TezSessionPoolManager.java