public synchronized void graceful_stop() {
    try {
      decommission();
      // Need 30s for stop() to release server's resources
      long maxTimeForWait = HiveConf.getTimeVar(getHiveConf(),
          HiveConf.ConfVars.HIVE_SERVER2_GRACEFUL_STOP_TIMEOUT, TimeUnit.MILLISECONDS) - 30000;
      long timeout = maxTimeForWait, startTime = System.currentTimeMillis();
      try {
        // The service should be started before when reaches here, as decommissioning would throw
        // IllegalStateException otherwise, so both cliService and sessionManager should not be null.
        while (timeout > 0 && !getCliService().getSessionManager().getOperations().isEmpty()) {
          // For gracefully stopping, sleeping some time while looping does not bring much overhead,
          // that is, at most 100ms are wasted for waiting for OperationManager to be done,
          // and this code path will only be executed when HS2 is being terminated.
          Thread.sleep(Math.min(100, timeout));
          timeout = maxTimeForWait + startTime - System.currentTimeMillis();
        }
      } catch (InterruptedException e) {
        LOG.warn("Interrupted while waiting for all live operations to be done");
        Thread.currentThread().interrupt();
      }
      LOG.info("Spent {}ms waiting for live operations to be done, current live operations: {}"
          , System.currentTimeMillis() - startTime
          , getCliService().getSessionManager().getOperations().size());
    } finally {
      stop();
    }
  }
Decommission HiveServer2. As a consequence, SessionManager stops
opening new sessions, OperationManager refuses running new queries and
HiveServer2 deregisters itself from Zookeeper if service discovery is enabled,
but the decommissioning has no effect on the current running queries.
service/src/java/org/apache/hive/service/server/HiveServer2.java