/// <summary>Add a resource for the specified interval</summary> /// <param name="reservationInterval"> /// the interval for which the resource is to be /// added /// </param> /// <param name="capacity">the resource to be added</param> /// <returns>true if addition is successful, false otherwise</returns> public virtual bool AddInterval(ReservationInterval reservationInterval, ReservationRequest capacity) { Org.Apache.Hadoop.Yarn.Api.Records.Resource totCap = Resources.Multiply(capacity. GetCapability(), (float)capacity.GetNumContainers()); if (totCap.Equals(ZeroResource)) { return(true); } writeLock.Lock(); try { long startKey = reservationInterval.GetStartTime(); long endKey = reservationInterval.GetEndTime(); NavigableMap <long, Org.Apache.Hadoop.Yarn.Api.Records.Resource> ticks = cumulativeCapacity .HeadMap(endKey, false); if (ticks != null && !ticks.IsEmpty()) { Org.Apache.Hadoop.Yarn.Api.Records.Resource updatedCapacity = Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(0, 0); KeyValuePair <long, Org.Apache.Hadoop.Yarn.Api.Records.Resource> lowEntry = ticks. FloorEntry(startKey); if (lowEntry == null) { // This is the earliest starting interval cumulativeCapacity[startKey] = totCap; } else { updatedCapacity = Resources.Add(lowEntry.Value, totCap); // Add a new tick only if the updated value is different // from the previous tick if ((startKey == lowEntry.Key) && (IsSameAsPrevious(lowEntry.Key, updatedCapacity ))) { Sharpen.Collections.Remove(cumulativeCapacity, lowEntry.Key); } else { cumulativeCapacity[startKey] = updatedCapacity; } } // Increase all the capacities of overlapping intervals ICollection <KeyValuePair <long, Org.Apache.Hadoop.Yarn.Api.Records.Resource> > overlapSet = ticks.TailMap(startKey, false); foreach (KeyValuePair <long, Org.Apache.Hadoop.Yarn.Api.Records.Resource> entry in overlapSet) { updatedCapacity = Resources.Add(entry.Value, totCap); entry.SetValue(updatedCapacity); } } else { // This is the first interval to be added cumulativeCapacity[startKey] = totCap; } Org.Apache.Hadoop.Yarn.Api.Records.Resource nextTick = cumulativeCapacity[endKey]; if (nextTick != null) { // If there is overlap, remove the duplicate entry if (IsSameAsPrevious(endKey, nextTick)) { Sharpen.Collections.Remove(cumulativeCapacity, endKey); } } else { // Decrease capacity as this is end of the interval cumulativeCapacity[endKey] = Resources.Subtract(cumulativeCapacity.FloorEntry(endKey ).Value, totCap); } return(true); } finally { writeLock.Unlock(); } }