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 bool Check(ReservationAllocation cs, long start, long end, int containers
                           , int mem, int cores)
        {
            bool res = true;

            for (long i = start; i < end; i++)
            {
                res = res && Resources.Equals(cs.GetResourcesAtTime(i), Org.Apache.Hadoop.Yarn.Api.Records.Resource
                                              .NewInstance(mem * containers, cores * containers));
            }
            return(res);
        }
Esempio n. 3
0
        /// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Exceptions.PlanningException
        ///     "/>
        public virtual void Plan(Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Plan
                                 plan, IList <ReservationDefinition> contracts)
        {
            if (contracts != null)
            {
                throw new RuntimeException("SimpleCapacityReplanner cannot handle new reservation contracts"
                                           );
            }
            ResourceCalculator resCalc = plan.GetResourceCalculator();

            Org.Apache.Hadoop.Yarn.Api.Records.Resource totCap = plan.GetTotalCapacity();
            long now = clock.GetTime();

            // loop on all moment in time from now to the end of the check Zone
            // or the end of the planned sessions whichever comes first
            for (long t = now; (t < plan.GetLastEndTime() && t < (now + lengthOfCheckZone));
                 t += plan.GetStep())
            {
                Org.Apache.Hadoop.Yarn.Api.Records.Resource excessCap = Resources.Subtract(plan.GetTotalCommittedResources
                                                                                               (t), totCap);
                // if we are violating
                if (Resources.GreaterThan(resCalc, totCap, excessCap, ZeroResource))
                {
                    // sorted on reverse order of acceptance, so newest reservations first
                    ICollection <ReservationAllocation> curReservations = new TreeSet <ReservationAllocation
                                                                                       >(plan.GetReservationsAtTime(t));
                    for (IEnumerator <ReservationAllocation> resIter = curReservations.GetEnumerator()
                         ; resIter.HasNext() && Resources.GreaterThan(resCalc, totCap, excessCap, ZeroResource
                                                                      );)
                    {
                        ReservationAllocation reservation = resIter.Next();
                        plan.DeleteReservation(reservation.GetReservationId());
                        excessCap = Resources.Subtract(excessCap, reservation.GetResourcesAtTime(t));
                        Log.Info("Removing reservation " + reservation.GetReservationId() + " to repair physical-resource constraints in the plan: "
                                 + plan.GetQueueName());
                    }
                }
            }
        }
Esempio n. 4
0
        /// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Exceptions.PlanningException
        ///     "/>
        public virtual void Validate(Plan plan, ReservationAllocation reservation)
        {
            ReservationAllocation oldReservation = plan.GetReservationById(reservation.GetReservationId
                                                                               ());

            // check updates are using same name
            if (oldReservation != null && !oldReservation.GetUser().Equals(reservation.GetUser
                                                                               ()))
            {
                throw new MismatchedUserException("Updating an existing reservation with mismatching user:"******" != " + reservation.GetUser());
            }
            long startTime = reservation.GetStartTime();
            long endTime   = reservation.GetEndTime();
            long step      = plan.GetStep();

            // for every instant in time, check we are respecting cluster capacity
            for (long t = startTime; t < endTime; t += step)
            {
                Resource currExistingAllocTot = plan.GetTotalCommittedResources(t);
                Resource currNewAlloc         = reservation.GetResourcesAtTime(t);
                Resource currOldAlloc         = Resource.NewInstance(0, 0);
                if (oldReservation != null)
                {
                    oldReservation.GetResourcesAtTime(t);
                }
                // check the cluster is never over committed
                // currExistingAllocTot + currNewAlloc - currOldAlloc >
                // capPlan.getTotalCapacity()
                if (Resources.GreaterThan(plan.GetResourceCalculator(), plan.GetTotalCapacity(),
                                          Resources.Subtract(Resources.Add(currExistingAllocTot, currNewAlloc), currOldAlloc
                                                             ), plan.GetTotalCapacity()))
                {
                    throw new ResourceOverCommitException("Resources at time " + t + " would be overcommitted by "
                                                          + "accepting reservation: " + reservation.GetReservationId());
                }
            }
        }
Esempio n. 5
0
        /// <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);
            }
        }
        /// <exception cref="Org.Apache.Hadoop.Yarn.Server.Resourcemanager.Reservation.Exceptions.PlanningException
        ///     "/>
        public virtual void Validate(Plan plan, ReservationAllocation reservation)
        {
            // this is entire method invoked under a write-lock on the plan, no need
            // to synchronize accesses to the plan further
            // Try to verify whether there is already a reservation with this ID in
            // the system (remove its contribution during validation to simulate a
            // try-n-swap
            // update).
            ReservationAllocation oldReservation = plan.GetReservationById(reservation.GetReservationId
                                                                               ());

            // sanity check that the update of a reservation is not changing username
            if (oldReservation != null && !oldReservation.GetUser().Equals(reservation.GetUser
                                                                               ()))
            {
                throw new MismatchedUserException("Updating an existing reservation with mismatched user:"******" != " + reservation.GetUser());
            }
            long     startTime         = reservation.GetStartTime();
            long     endTime           = reservation.GetEndTime();
            long     step              = plan.GetStep();
            Resource planTotalCapacity = plan.GetTotalCapacity();

            Org.Apache.Hadoop.Yarn.Api.Records.Resource maxAvgRes = Resources.Multiply(planTotalCapacity
                                                                                       , maxAvg);
            Org.Apache.Hadoop.Yarn.Api.Records.Resource maxInsRes = Resources.Multiply(planTotalCapacity
                                                                                       , maxInst);
            // define variable that will store integral of resources (need diff class to
            // avoid overflow issues for long/large allocations)
            CapacityOverTimePolicy.IntegralResource runningTot = new CapacityOverTimePolicy.IntegralResource
                                                                     (0L, 0L);
            CapacityOverTimePolicy.IntegralResource maxAllowed = new CapacityOverTimePolicy.IntegralResource
                                                                     (maxAvgRes);
            maxAllowed.MultiplyBy(validWindow / step);
            // check that the resources offered to the user during any window of length
            // "validWindow" overlapping this allocation are within maxAllowed
            // also enforce instantaneous and physical constraints during this pass
            for (long t = startTime - validWindow; t < endTime + validWindow; t += step)
            {
                Org.Apache.Hadoop.Yarn.Api.Records.Resource currExistingAllocTot = plan.GetTotalCommittedResources
                                                                                       (t);
                Org.Apache.Hadoop.Yarn.Api.Records.Resource currExistingAllocForUser = plan.GetConsumptionForUser
                                                                                           (reservation.GetUser(), t);
                Org.Apache.Hadoop.Yarn.Api.Records.Resource currNewAlloc = reservation.GetResourcesAtTime
                                                                               (t);
                Org.Apache.Hadoop.Yarn.Api.Records.Resource currOldAlloc = Resources.None();
                if (oldReservation != null)
                {
                    currOldAlloc = oldReservation.GetResourcesAtTime(t);
                }
                // throw exception if the cluster is overcommitted
                // tot_allocated - old + new > capacity
                Org.Apache.Hadoop.Yarn.Api.Records.Resource inst = Resources.Subtract(Resources.Add
                                                                                          (currExistingAllocTot, currNewAlloc), currOldAlloc);
                if (Resources.GreaterThan(plan.GetResourceCalculator(), planTotalCapacity, inst,
                                          planTotalCapacity))
                {
                    throw new ResourceOverCommitException(" Resources at time " + t + " would be overcommitted ("
                                                          + inst + " over " + plan.GetTotalCapacity() + ") by accepting reservation: " +
                                                          reservation.GetReservationId());
                }
                // throw exception if instantaneous limits are violated
                // tot_alloc_to_this_user - old + new > inst_limit
                if (Resources.GreaterThan(plan.GetResourceCalculator(), planTotalCapacity, Resources
                                          .Subtract(Resources.Add(currExistingAllocForUser, currNewAlloc), currOldAlloc),
                                          maxInsRes))
                {
                    throw new PlanningQuotaException("Instantaneous quota capacity " + maxInst + " would be passed at time "
                                                     + t + " by accepting reservation: " + reservation.GetReservationId());
                }
                // throw exception if the running integral of utilization over validWindow
                // is violated. We perform a delta check, adding/removing instants at the
                // boundary of the window from runningTot.
                // runningTot = previous_runningTot + currExistingAllocForUser +
                // currNewAlloc - currOldAlloc - pastNewAlloc - pastOldAlloc;
                // Where:
                // 1) currNewAlloc, currExistingAllocForUser represent the contribution of
                // the instant in time added in this pass.
                // 2) pastNewAlloc, pastOldAlloc are the contributions relative to time
                // instants that are being retired from the the window
                // 3) currOldAlloc is the contribution (if any) of the previous version of
                // this reservation (the one we are updating)
                runningTot.Add(currExistingAllocForUser);
                runningTot.Add(currNewAlloc);
                runningTot.Subtract(currOldAlloc);
                // expire contributions from instant in time before (t - validWindow)
                if (t > startTime)
                {
                    Org.Apache.Hadoop.Yarn.Api.Records.Resource pastOldAlloc = plan.GetConsumptionForUser
                                                                                   (reservation.GetUser(), t - validWindow);
                    Org.Apache.Hadoop.Yarn.Api.Records.Resource pastNewAlloc = reservation.GetResourcesAtTime
                                                                                   (t - validWindow);
                    // runningTot = runningTot - pastExistingAlloc - pastNewAlloc;
                    runningTot.Subtract(pastOldAlloc);
                    runningTot.Subtract(pastNewAlloc);
                }
                // check integral
                // runningTot > maxAvg * validWindow
                // NOTE: we need to use comparator of IntegralResource directly, as
                // Resource and ResourceCalculator assume "int" amount of resources,
                // which is not sufficient when comparing integrals (out-of-bound)
                if (maxAllowed.CompareTo(runningTot) < 0)
                {
                    throw new PlanningQuotaException("Integral (avg over time) quota capacity " + maxAvg
                                                     + " over a window of " + validWindow / 1000 + " seconds, " + " would be passed at time "
                                                     + t + "(" + Sharpen.Extensions.CreateDate(t) + ") by accepting reservation: " +
                                                     reservation.GetReservationId());
                }
            }
        }