private ListenableFuture<Boolean> resizeDownInternal(int delta, List<SessionType> toClose) {
    // 1) Cancel the previous expansion, if any.
    while (true) {
      int expansionCount = deltaRemaining.get();
      if (expansionCount <= 0) break;
      int expansionCancelled = Math.min(expansionCount, delta);
      if (deltaRemaining.compareAndSet(expansionCount, expansionCount - expansionCancelled)) {
        delta -= expansionCancelled;
        break;
      }
    }
    // 2) Drain unused sessions; the close() is sync so delegate to the caller.
    while (delta > 0) {
      SessionType session = pool.poll();
      if (session == null) break;
      if (!session.tryUse(true)) continue;
      toClose.add(session);
      --delta;
    }
    // 3) If too many sessions are outstanding (e.g. due to expiration restarts - should
    //    not happen with in-use sessions because WM already kills the extras), we will kill
    //    them as they come back from restarts.
    if (delta > 0) {
      int oldVal;
      do {
        oldVal = deltaRemaining.get();
      } while (!deltaRemaining.compareAndSet(oldVal, oldVal - delta));
    }
    return createDummyFuture();
  }
Create {@code sessionCount} number of sessions, doing so in parallel, using
a maximum of {@code maxParallel} threads.

@param sessionCount The number of sessions to launch
@return A single {@code future} which carries all of the outputs from each
        session launch
@throws IllegalArgumentException if {@code sessionCount} or
          {@code maxParallel} is less than or equal to zero
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/TezSessionPool.java