@Override
    public void onSuccess(WmTezSession session) {
      SessionInitState oldState;
      SettableFuture<WmTezSession> future = null;
      lock.lock();
      try {
        oldState = state;
        switch (oldState) {
        case GETTING: {
          LOG.info("Received a session from AM pool {}", session);
          assert this.state == SessionInitState.GETTING;
          session.setPoolName(poolName);
          session.setQueueName(yarnQueue);
          session.setQueryId(queryId);
          if (prelocalizedResources != null) {
            pathToDelete = session.replaceHiveResources(prelocalizedResources, true);
          }
          if (wmContext != null) {
            session.setWmContext(wmContext);
          }
          this.session = session;
          this.state = SessionInitState.WAITING_FOR_REGISTRY;
          break;
        }
        case WAITING_FOR_REGISTRY: {
          assert this.session != null;
          this.state = SessionInitState.DONE;
          assert session == this.session;
          future = this.future;
          this.future = null;
          break;
        }
        case CANCELED: {
          future = this.future;
          this.session = null;
          this.future = null;
          break;
        }
        default: {
          future = this.future;
          this.future = null;
          break;
        }
        }
      } finally {
        lock.unlock();
      }
      switch (oldState) {
      case GETTING: {
        ListenableFuture<WmTezSession> waitFuture = session.waitForAmRegistryAsync(
            amRegistryTimeoutMs, timeoutPool);
        Futures.addCallback(waitFuture, this, MoreExecutors.directExecutor());
        break;
      }
      case WAITING_FOR_REGISTRY: {
        // Notify the master thread and the user.
        notifyInitializationCompleted(this);
        future.set(session);
        break;
      }
      case CANCELED: {
        // Return session to the pool; we can do it directly here.
        future.setException(new HiveException(
            "The query was killed by workload management: " + cancelReason));
        session.clearWm();
        session.setQueryId(null);
        session.setWmContext(null);
        tezAmPool.returnSession(session);
        break;
      }
      default: {
        AssertionError error = new AssertionError("Unexpected state " + state);
        future.setException(error);
        throw error;
      }
      }
    }
The class that serves as a synchronization point, and future callback,
for async session initialization, as well as parallel cancellation.
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/WorkloadManager.java