public virtual void TestSimple() { PrepareBasicPlan(); // create a request with a single atomic ask ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(5 * step); rr.SetDeadline(20 * step); ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(2048, 2), 10, 5, 10 * step); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetReservationResources(Collections.SingletonList(r)); rr.SetReservationRequests(reqs); ReservationId reservationID = ReservationSystemTestUtil.GetNewReservationId(); agent.CreateReservation(reservationID, "u1", plan, rr); NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", reservationID != null ); NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", plan.GetAllReservations ().Count == 3); ReservationAllocation cs = plan.GetReservationById(reservationID); System.Console.Out.WriteLine("--------AFTER SIMPLE ALLOCATION (queue: " + reservationID + ")----------"); System.Console.Out.WriteLine(plan.ToString()); System.Console.Out.WriteLine(plan.ToCumulativeString()); for (long i = 10 * step; i < 20 * step; i++) { NUnit.Framework.Assert.IsTrue("Agent-based allocation unexpected", Resources.Equals (cs.GetResourcesAtTime(i), Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (2048 * 10, 2 * 10))); } }
private IDictionary <ReservationInterval, ReservationRequest> GenerateAllocation(int startTime, int[] alloc, bool isStep, bool isGang) { IDictionary <ReservationInterval, ReservationRequest> req = new Dictionary <ReservationInterval , ReservationRequest>(); int numContainers = 0; for (int i = 0; i < alloc.Length; i++) { if (isStep) { numContainers = alloc[i] + i; } else { numContainers = alloc[i]; } ReservationRequest rr = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), (numContainers)); if (isGang) { rr.SetConcurrency(numContainers); } req[new ReservationInterval(startTime + i, startTime + i + 1)] = rr; } return(req); }
public static ReservationDefinition GenerateBigRR(Random rand, long i) { rand.SetSeed(i); long now = Runtime.CurrentTimeMillis(); // start time at random in the next 2 hours long arrival = rand.Next(2 * 3600 * 1000); // deadline at random in the next day long deadline = rand.Next(24 * 3600 * 1000); // create a request with a single atomic ask ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(now + arrival); rr.SetDeadline(now + deadline); int gang = 1; int par = 100000; // 100k tasks long dur = rand.Next(60 * 1000); // 1min tasks ReservationRequest r = ReservationRequest.NewInstance(Resource.NewInstance(1024, 1), par, gang, dur); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetReservationResources(Collections.SingletonList(r)); rand.Next(3); ReservationRequestInterpreter[] type = ReservationRequestInterpreter.Values(); reqs.SetInterpreter(type[rand.Next(type.Length)]); rr.SetReservationRequests(reqs); return(rr); }
private ReservationUpdateRequest CreateSimpleReservationUpdateRequest(int numRequests , int numContainers, long arrival, long deadline, long duration) { // create a request with a single atomic ask ReservationUpdateRequest request = new ReservationUpdateRequestPBImpl(); ReservationDefinition rDef = new ReservationDefinitionPBImpl(); rDef.SetArrival(arrival); rDef.SetDeadline(deadline); if (numRequests > 0) { ReservationRequests reqs = new ReservationRequestsPBImpl(); rDef.SetReservationRequests(reqs); if (numContainers > 0) { ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), numContainers, 1, duration); reqs.SetReservationResources(Sharpen.Collections.SingletonList(r)); reqs.SetInterpreter(ReservationRequestInterpreter.RAll); } } request.SetReservationDefinition(rDef); request.SetReservationId(ReservationSystemTestUtil.GetNewReservationId()); return(request); }
public virtual void TestOrder() { PrepareBasicPlan(); // create a completely utilized segment around time 30 int[] f = new int[] { 100, 100 }; NUnit.Framework.Assert.IsTrue(plan.ToString(), plan.AddReservation(new InMemoryReservationAllocation (ReservationSystemTestUtil.GetNewReservationId(), null, "u1", "dedicated", 30 * step, 30 * step + f.Length * step, ReservationSystemTestUtil.GenerateAllocation( 30 * step, step, f), res, minAlloc))); // create a chain of 4 RR, mixing gang and non-gang ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(0 * step); rr.SetDeadline(70 * step); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetInterpreter(ReservationRequestInterpreter.ROrder); ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(2048, 2), 10, 1, 10 * step); ReservationRequest r2 = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 10, 10, 20 * step); IList <ReservationRequest> list = new AList <ReservationRequest>(); list.AddItem(r); list.AddItem(r2); list.AddItem(r); list.AddItem(r2); reqs.SetReservationResources(list); rr.SetReservationRequests(reqs); // submit to agent ReservationId reservationID = ReservationSystemTestUtil.GetNewReservationId(); agent.CreateReservation(reservationID, "u1", plan, rr); // validate NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", reservationID != null ); NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", plan.GetAllReservations ().Count == 4); ReservationAllocation cs = plan.GetReservationById(reservationID); NUnit.Framework.Assert.IsTrue(cs.ToString(), Check(cs, 0 * step, 10 * step, 20, 1024 , 1)); NUnit.Framework.Assert.IsTrue(cs.ToString(), Check(cs, 10 * step, 30 * step, 10, 1024, 1)); NUnit.Framework.Assert.IsTrue(cs.ToString(), Check(cs, 40 * step, 50 * step, 20, 1024, 1)); NUnit.Framework.Assert.IsTrue(cs.ToString(), Check(cs, 50 * step, 70 * step, 10, 1024, 1)); System.Console.Out.WriteLine("--------AFTER ORDER ALLOCATION (queue: " + reservationID + ")----------"); System.Console.Out.WriteLine(plan.ToString()); System.Console.Out.WriteLine(plan.ToCumulativeString()); }
public virtual void TestOrderNoGapImpossible() { PrepareBasicPlan(); // create a completely utilized segment at time 30 int[] f = new int[] { 100, 100 }; NUnit.Framework.Assert.IsTrue(plan.ToString(), plan.AddReservation(new InMemoryReservationAllocation (ReservationSystemTestUtil.GetNewReservationId(), null, "u1", "dedicated", 30 * step, 30 * step + f.Length * step, ReservationSystemTestUtil.GenerateAllocation( 30 * step, step, f), res, minAlloc))); // create a chain of 4 RR, mixing gang and non-gang ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(0L); rr.SetDeadline(70L); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetInterpreter(ReservationRequestInterpreter.ROrderNoGap); ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(2048, 2), 10, 1, 10); ReservationRequest r2 = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 10, 10, 20); IList <ReservationRequest> list = new AList <ReservationRequest>(); list.AddItem(r); list.AddItem(r2); list.AddItem(r); list.AddItem(r2); reqs.SetReservationResources(list); rr.SetReservationRequests(reqs); ReservationId reservationID = ReservationSystemTestUtil.GetNewReservationId(); bool result = false; try { // submit to agent result = agent.CreateReservation(reservationID, "u1", plan, rr); NUnit.Framework.Assert.Fail(); } catch (PlanningException) { } // expected // validate NUnit.Framework.Assert.IsFalse("Agent-based allocation should have failed", result ); NUnit.Framework.Assert.IsTrue("Agent-based allocation should have failed", plan.GetAllReservations ().Count == 3); System.Console.Out.WriteLine("--------AFTER ORDER_NO_GAP IMPOSSIBLE ALLOCATION (queue: " + reservationID + ")----------"); System.Console.Out.WriteLine(plan.ToString()); System.Console.Out.WriteLine(plan.ToCumulativeString()); }
public static IDictionary <ReservationInterval, ReservationRequest> GenerateAllocation (long startTime, long step, int[] alloc) { IDictionary <ReservationInterval, ReservationRequest> req = new SortedDictionary <ReservationInterval , ReservationRequest>(); for (int i = 0; i < alloc.Length; i++) { req[new ReservationInterval(startTime + i * step, startTime + (i + 1) * step)] = ReservationRequest.NewInstance(Resource.NewInstance(1024, 1), alloc[i]); } return(req); }
private IDictionary <ReservationInterval, ReservationRequest> GenerateAllocation(int startTime, int[] alloc) { IDictionary <ReservationInterval, ReservationRequest> req = new SortedDictionary <ReservationInterval , ReservationRequest>(); for (int i = 0; i < alloc.Length; i++) { req[new ReservationInterval(startTime + i, startTime + i + 1)] = ReservationRequest .NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance(1024, 1), alloc [i]); } return(req); }
// expected /// <exception cref="System.IO.IOException"/> /// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Exceptions.PlanningException /// "/> public virtual void TestFailAvg() { // generate an allocation which violates the 25% average single-shot IDictionary <ReservationInterval, ReservationRequest> req = new SortedDictionary <ReservationInterval , ReservationRequest>(); long win = timeWindow / 2 + 100; int cont = (int)Math.Ceil(0.5 * totCont); req[new ReservationInterval(initTime, initTime + win)] = ReservationRequest.NewInstance (Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance(1024, 1), cont); NUnit.Framework.Assert.IsTrue(plan.ToString(), plan.AddReservation(new InMemoryReservationAllocation (ReservationSystemTestUtil.GetNewReservationId(), null, "u1", "dedicated", initTime , initTime + win, req, res, minAlloc))); }
private ReservationDefinition CreateSimpleReservationDefinition(long arrival, long deadline, long duration) { // create a request with a single atomic ask ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 1, 1, duration); ReservationDefinition rDef = new ReservationDefinitionPBImpl(); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetReservationResources(Sharpen.Collections.SingletonList(r)); reqs.SetInterpreter(ReservationRequestInterpreter.RAll); rDef.SetReservationRequests(reqs); rDef.SetArrival(arrival); rDef.SetDeadline(deadline); return(rDef); }
public virtual void TestAnyImpossible() { PrepareBasicPlan(); // create an ANY request, with all impossible alternatives ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(100L); rr.SetDeadline(120L); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetInterpreter(ReservationRequestInterpreter.RAny); // longer than arrival-deadline ReservationRequest r1 = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 35, 5, 30); // above max cluster size ReservationRequest r2 = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 110, 110, 10); IList <ReservationRequest> list = new AList <ReservationRequest>(); list.AddItem(r1); list.AddItem(r2); reqs.SetReservationResources(list); rr.SetReservationRequests(reqs); ReservationId reservationID = ReservationSystemTestUtil.GetNewReservationId(); bool result = false; try { // submit to agent result = agent.CreateReservation(reservationID, "u1", plan, rr); NUnit.Framework.Assert.Fail(); } catch (PlanningException) { } // expected // validate results, we expect the second one to be accepted NUnit.Framework.Assert.IsFalse("Agent-based allocation should have failed", result ); NUnit.Framework.Assert.IsTrue("Agent-based allocation should have failed", plan.GetAllReservations ().Count == 2); System.Console.Out.WriteLine("--------AFTER ANY IMPOSSIBLE ALLOCATION (queue: " + reservationID + ")----------"); System.Console.Out.WriteLine(plan.ToString()); System.Console.Out.WriteLine(plan.ToCumulativeString()); }
public virtual void TestZeroAlloaction() { ResourceCalculator resCalc = new DefaultResourceCalculator(); Org.Apache.Hadoop.Yarn.Api.Records.Resource minAlloc = Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1, 1); RLESparseResourceAllocation rleSparseVector = new RLESparseResourceAllocation(resCalc , minAlloc); rleSparseVector.AddInterval(new ReservationInterval(0, long.MaxValue), ReservationRequest .NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance(0, 0), (0)) ); Log.Info(rleSparseVector.ToString()); NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (0, 0), rleSparseVector.GetCapacityAtTime(new Random().NextLong())); NUnit.Framework.Assert.IsTrue(rleSparseVector.IsEmpty()); }
public virtual void TestAny() { PrepareBasicPlan(); // create an ANY request, with an impossible step (last in list, first // considered), // and two satisfiable ones. We expect the second one to be returned. ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(100 * step); rr.SetDeadline(120 * step); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetInterpreter(ReservationRequestInterpreter.RAny); ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 5, 5, 10 * step); ReservationRequest r2 = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(2048, 2), 10, 5, 10 * step); ReservationRequest r3 = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 110, 110, 10 * step); IList <ReservationRequest> list = new AList <ReservationRequest>(); list.AddItem(r); list.AddItem(r2); list.AddItem(r3); reqs.SetReservationResources(list); rr.SetReservationRequests(reqs); // submit to agent ReservationId reservationID = ReservationSystemTestUtil.GetNewReservationId(); bool res = agent.CreateReservation(reservationID, "u1", plan, rr); // validate results, we expect the second one to be accepted NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", res); NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", plan.GetAllReservations ().Count == 3); ReservationAllocation cs = plan.GetReservationById(reservationID); NUnit.Framework.Assert.IsTrue(cs.ToString(), Check(cs, 110 * step, 120 * step, 20 , 1024, 1)); System.Console.Out.WriteLine("--------AFTER ANY ALLOCATION (queue: " + reservationID + ")----------"); System.Console.Out.WriteLine(plan.ToString()); System.Console.Out.WriteLine(plan.ToCumulativeString()); }
public virtual void TestSingleSliding() { PrepareBasicPlan(); // create a single request for which we need subsequent (tight) packing. ReservationDefinition rr = new ReservationDefinitionPBImpl(); rr.SetArrival(100 * step); rr.SetDeadline(120 * step); ReservationRequests reqs = new ReservationRequestsPBImpl(); reqs.SetInterpreter(ReservationRequestInterpreter.RAll); ReservationRequest r = ReservationRequest.NewInstance(Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(1024, 1), 200, 10, 10 * step); IList <ReservationRequest> list = new AList <ReservationRequest>(); list.AddItem(r); reqs.SetReservationResources(list); rr.SetReservationRequests(reqs); // submit to agent ReservationId reservationID = ReservationSystemTestUtil.GetNewReservationId(); agent.CreateReservation(reservationID, "u1", plan, rr); // validate results, we expect the second one to be accepted NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", reservationID != null ); NUnit.Framework.Assert.IsTrue("Agent-based allocation failed", plan.GetAllReservations ().Count == 3); ReservationAllocation cs = plan.GetReservationById(reservationID); NUnit.Framework.Assert.IsTrue(cs.ToString(), Check(cs, 100 * step, 120 * step, 100 , 1024, 1)); System.Console.Out.WriteLine("--------AFTER packed ALLOCATION (queue: " + reservationID + ")----------"); System.Console.Out.WriteLine(plan.ToString()); System.Console.Out.WriteLine(plan.ToCumulativeString()); }
public virtual void TestFailAvgBySum() { // generate an allocation which violates the 25% average by sum IDictionary <ReservationInterval, ReservationRequest> req = new SortedDictionary <ReservationInterval , ReservationRequest>(); long win = 86400000 / 4 + 1; int cont = (int)Math.Ceil(0.5 * totCont); req[new ReservationInterval(initTime, initTime + win)] = ReservationRequest.NewInstance (Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance(1024, 1), cont); NUnit.Framework.Assert.IsTrue(plan.ToString(), plan.AddReservation(new InMemoryReservationAllocation (ReservationSystemTestUtil.GetNewReservationId(), null, "u1", "dedicated", initTime , initTime + win, req, res, minAlloc))); try { NUnit.Framework.Assert.IsTrue(plan.ToString(), plan.AddReservation(new InMemoryReservationAllocation (ReservationSystemTestUtil.GetNewReservationId(), null, "u1", "dedicated", initTime , initTime + win, req, res, minAlloc))); NUnit.Framework.Assert.Fail("should not have accepted this"); } catch (PlanningQuotaException) { } }
/// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Exceptions.PlanningException /// "/> /// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Exceptions.ContractValidationException /// "/> private bool ComputeAllocation(ReservationId reservationId, string user, Plan plan , ReservationDefinition contract, ReservationAllocation oldReservation) { Log.Info("placing the following ReservationRequest: " + contract); Resource totalCapacity = plan.GetTotalCapacity(); // Here we can addd logic to adjust the ResourceDefinition to account for // system "imperfections" (e.g., scheduling delays for large containers). // Align with plan step conservatively (i.e., ceil arrival, and floor // deadline) long earliestStart = contract.GetArrival(); long step = plan.GetStep(); if (earliestStart % step != 0) { earliestStart = earliestStart + (step - (earliestStart % step)); } long deadline = contract.GetDeadline() - contract.GetDeadline() % plan.GetStep(); // setup temporary variables to handle time-relations between stages and // intermediate answers long curDeadline = deadline; long oldDeadline = -1; IDictionary <ReservationInterval, ReservationRequest> allocations = new Dictionary <ReservationInterval, ReservationRequest>(); RLESparseResourceAllocation tempAssigned = new RLESparseResourceAllocation(plan.GetResourceCalculator (), plan.GetMinimumAllocation()); IList <ReservationRequest> stages = contract.GetReservationRequests().GetReservationResources (); ReservationRequestInterpreter type = contract.GetReservationRequests().GetInterpreter (); // Iterate the stages in backward from deadline for (ListIterator <ReservationRequest> li = stages.ListIterator(stages.Count); li. HasPrevious();) { ReservationRequest currentReservationStage = li.Previous(); // validate the RR respect basic constraints ValidateInput(plan, currentReservationStage, totalCapacity); // run allocation for a single stage IDictionary <ReservationInterval, ReservationRequest> curAlloc = PlaceSingleStage( plan, tempAssigned, currentReservationStage, earliestStart, curDeadline, oldReservation , totalCapacity); if (curAlloc == null) { // if we did not find an allocation for the currentReservationStage // return null, unless the ReservationDefinition we are placing is of // type ANY if (type != ReservationRequestInterpreter.RAny) { throw new PlanningException("The GreedyAgent" + " couldn't find a valid allocation for your request" ); } else { continue; } } else { // if we did find an allocation add it to the set of allocations allocations.PutAll(curAlloc); // if this request is of type ANY we are done searching (greedy) // and can return the current allocation (break-out of the search) if (type == ReservationRequestInterpreter.RAny) { break; } // if the request is of ORDER or ORDER_NO_GAP we constraint the next // round of allocation to precede the current allocation, by setting // curDeadline if (type == ReservationRequestInterpreter.ROrder || type == ReservationRequestInterpreter .ROrderNoGap) { curDeadline = FindEarliestTime(curAlloc.Keys); // for ORDER_NO_GAP verify that the allocation found so far has no // gap, return null otherwise (the greedy procedure failed to find a // no-gap // allocation) if (type == ReservationRequestInterpreter.ROrderNoGap && oldDeadline > 0) { if (oldDeadline - FindLatestTime(curAlloc.Keys) > plan.GetStep()) { throw new PlanningException("The GreedyAgent" + " couldn't find a valid allocation for your request" ); } } // keep the variable oldDeadline pointing to the last deadline we // found oldDeadline = curDeadline; } } } // / If we got here is because we failed to find an allocation for the // ReservationDefinition give-up and report failure to the user if (allocations.IsEmpty()) { throw new PlanningException("The GreedyAgent" + " couldn't find a valid allocation for your request" ); } // create reservation with above allocations if not null/empty ReservationRequest ZeroRes = ReservationRequest.NewInstance(Resource.NewInstance( 0, 0), 0); long firstStartTime = FindEarliestTime(allocations.Keys); // add zero-padding from arrival up to the first non-null allocation // to guarantee that the reservation exists starting at arrival if (firstStartTime > earliestStart) { allocations[new ReservationInterval(earliestStart, firstStartTime)] = ZeroRes; firstStartTime = earliestStart; } // consider to add trailing zeros at the end for simmetry // Actually add/update the reservation in the plan. // This is subject to validation as other agents might be placing // in parallel and there might be sharing policies the agent is not // aware off. ReservationAllocation capReservation = new InMemoryReservationAllocation(reservationId , contract, user, plan.GetQueueName(), firstStartTime, FindLatestTime(allocations .Keys), allocations, plan.GetResourceCalculator(), plan.GetMinimumAllocation()); if (oldReservation != null) { return(plan.UpdateReservation(capReservation)); } else { return(plan.AddReservation(capReservation)); } }
/// <summary> /// This method actually perform the placement of an atomic stage of the /// reservation. /// </summary> /// <remarks> /// This method actually perform the placement of an atomic stage of the /// reservation. The key idea is to traverse the plan backward for a /// "lease-duration" worth of time, and compute what is the maximum multiple of /// our concurrency (gang) parameter we can fit. We do this and move towards /// previous instant in time until the time-window is exhausted or we placed /// all the user request. /// </remarks> private IDictionary <ReservationInterval, ReservationRequest> PlaceSingleStage(Plan plan, RLESparseResourceAllocation tempAssigned, ReservationRequest rr, long earliestStart , long curDeadline, ReservationAllocation oldResAllocation, Org.Apache.Hadoop.Yarn.Api.Records.Resource totalCapacity) { IDictionary <ReservationInterval, ReservationRequest> allocationRequests = new Dictionary <ReservationInterval, ReservationRequest>(); // compute the gang as a resource and get the duration Org.Apache.Hadoop.Yarn.Api.Records.Resource gang = Resources.Multiply(rr.GetCapability (), rr.GetConcurrency()); long dur = rr.GetDuration(); long step = plan.GetStep(); // ceil the duration to the next multiple of the plan step if (dur % step != 0) { dur += (step - (dur % step)); } // we know for sure that this division has no remainder (part of contract // with user, validate before int gangsToPlace = rr.GetNumContainers() / rr.GetConcurrency(); int maxGang = 0; // loop trying to place until we are done, or we are considering // an invalid range of times while (gangsToPlace > 0 && curDeadline - dur >= earliestStart) { // as we run along we remember how many gangs we can fit, and what // was the most constraining moment in time (we will restart just // after that to place the next batch) maxGang = gangsToPlace; long minPoint = curDeadline; int curMaxGang = maxGang; // start placing at deadline (excluded due to [,) interval semantics and // move backward for (long t = curDeadline - plan.GetStep(); t >= curDeadline - dur && maxGang > 0 ; t = t - plan.GetStep()) { // As we run along we will logically remove the previous allocation for // this reservation // if one existed Org.Apache.Hadoop.Yarn.Api.Records.Resource oldResCap = Org.Apache.Hadoop.Yarn.Api.Records.Resource .NewInstance(0, 0); if (oldResAllocation != null) { oldResCap = oldResAllocation.GetResourcesAtTime(t); } // compute net available resources Org.Apache.Hadoop.Yarn.Api.Records.Resource netAvailableRes = Resources.Clone(totalCapacity ); Resources.AddTo(netAvailableRes, oldResCap); Resources.SubtractFrom(netAvailableRes, plan.GetTotalCommittedResources(t)); Resources.SubtractFrom(netAvailableRes, tempAssigned.GetCapacityAtTime(t)); // compute maximum number of gangs we could fit curMaxGang = (int)Math.Floor(Resources.Divide(plan.GetResourceCalculator(), totalCapacity , netAvailableRes, gang)); // pick the minimum between available resources in this instant, and how // many gangs we have to place curMaxGang = Math.Min(gangsToPlace, curMaxGang); // compare with previous max, and set it. also remember *where* we found // the minimum (useful for next attempts) if (curMaxGang <= maxGang) { maxGang = curMaxGang; minPoint = t; } } // if we were able to place any gang, record this, and decrement // gangsToPlace if (maxGang > 0) { gangsToPlace -= maxGang; ReservationInterval reservationInt = new ReservationInterval(curDeadline - dur, curDeadline ); ReservationRequest reservationRes = ReservationRequest.NewInstance(rr.GetCapability (), rr.GetConcurrency() * maxGang, rr.GetConcurrency(), rr.GetDuration()); // remember occupied space (plan is read-only till we find a plausible // allocation for the entire request). This is needed since we might be // placing other ReservationRequest within the same // ReservationDefinition, // and we must avoid double-counting the available resources tempAssigned.AddInterval(reservationInt, reservationRes); allocationRequests[reservationInt] = reservationRes; } // reset our new starting point (curDeadline) to the most constraining // point so far, we will look "left" of that to find more places where // to schedule gangs (for sure nothing on the "right" of this point can // fit a full gang. curDeadline = minPoint; } // if no gangs are left to place we succeed and return the allocation if (gangsToPlace == 0) { return(allocationRequests); } else { // If we are here is becasue we did not manage to satisfy this request. // So we need to remove unwanted side-effect from tempAssigned (needed // for ANY). foreach (KeyValuePair <ReservationInterval, ReservationRequest> tempAllocation in allocationRequests) { tempAssigned.RemoveInterval(tempAllocation.Key, tempAllocation.Value); } // and return null to signal failure in this allocation return(null); } }