@Override
  public int updateSessionsAsync(Double totalMaxAlloc, List<WmTezSession> sessionsToUpdate) {
    // Do not make a remote call under any circumstances - this is supposed to be async.
    int totalCount = getExecutorCount(false);
    int totalToDistribute = -1;
    if (totalMaxAlloc != null) {
      totalToDistribute = (int)Math.round(totalCount * totalMaxAlloc);
    }
    int totalDistributed = 0;
    double lastDelta = 0;
    for (int i = 0; i < sessionsToUpdate.size(); ++i) {
      WmTezSession session = sessionsToUpdate.get(i);
      int intAlloc = -1;
      if (i + 1 == sessionsToUpdate.size() && totalToDistribute >= 0) {
        intAlloc = totalToDistribute;
        // We rely on the caller to supply a reasonable total; we could log a warning
        // if this doesn't match the allocation of the last session beyond some threshold.
      } else {
        // This ensures we don't create skew, e.g. with 8 ducks and 5 queries with simple rounding
        // we'd produce 2-2-2-2-0 as we round 1.6; whereas adding the last delta to the next query
        // we'd round 1.6-1.2-1.8-1.4-2.0 and thus give out 2-1-2-1-2, as intended.
        // Note that fractions don't have to all be the same like in this example.
        assert session.hasClusterFraction();
        double fraction = session.getClusterFraction();
        double allocation = fraction * totalCount + lastDelta;
        double roundedAlloc = Math.round(allocation);
        lastDelta = allocation - roundedAlloc;
        if (roundedAlloc < 0) {
          roundedAlloc = 0; // Can this happen? Delta cannot exceed 0.5.
        }
        intAlloc = (int)roundedAlloc;
      }
      // Make sure we don't give out more than allowed due to double/rounding artifacts.
      if (totalToDistribute >= 0) {
        if (intAlloc > totalToDistribute) {
          intAlloc = totalToDistribute;
        }
        totalToDistribute -= intAlloc;
      }
      // This will only send update if it's necessary.
      totalDistributed += intAlloc;
      updateSessionAsync(session, intAlloc);
    }
    return totalDistributed;
  }
Implements query resource allocation using guaranteed tasks.
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/GuaranteedTasksAllocator.java