public virtual void ArchiveCompletedReservations(long tick) { // Since we are looking for old reservations, read lock is optimal Log.Debug("Running archival at time: {}", tick); IList <InMemoryReservationAllocation> expiredReservations = new AList <InMemoryReservationAllocation >(); readLock.Lock(); // archive reservations and delete the ones which are beyond // the reservation policy "window" try { long archivalTime = tick - policy.GetValidWindow(); ReservationInterval searchInterval = new ReservationInterval(archivalTime, archivalTime ); SortedDictionary <ReservationInterval, ICollection <InMemoryReservationAllocation> > reservations = currentReservations.HeadMap(searchInterval, true); if (!reservations.IsEmpty()) { foreach (ICollection <InMemoryReservationAllocation> reservationEntries in reservations .Values) { foreach (InMemoryReservationAllocation reservation in reservationEntries) { if (reservation.GetEndTime() <= archivalTime) { expiredReservations.AddItem(reservation); } } } } } finally { readLock.Unlock(); } if (expiredReservations.IsEmpty()) { return; } // Need write lock only if there are any reservations to be deleted writeLock.Lock(); try { foreach (InMemoryReservationAllocation expiredReservation in expiredReservations) { RemoveReservation(expiredReservation); } } finally { writeLock.Unlock(); } }
public virtual void TestArchiveCompletedReservations() { Plan plan = new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, resCalc , minAlloc, maxAlloc, planName, replanner, true); ReservationId reservationID1 = ReservationSystemTestUtil.GetNewReservationId(); // First add a reservation int[] alloc1 = new int[] { 10, 10, 10, 10, 10, 10 }; int start = 100; IDictionary <ReservationInterval, ReservationRequest> allocations1 = GenerateAllocation (start, alloc1, false); ReservationDefinition rDef1 = CreateSimpleReservationDefinition(start, start + alloc1 .Length, alloc1.Length, allocations1.Values); ReservationAllocation rAllocation = new InMemoryReservationAllocation(reservationID1 , rDef1, user, planName, start, start + alloc1.Length, allocations1, resCalc, minAlloc ); NUnit.Framework.Assert.IsNull(plan.GetReservationById(reservationID1)); try { plan.AddReservation(rAllocation); } catch (PlanningException e) { NUnit.Framework.Assert.Fail(e.Message); } DoAssertions(plan, rAllocation); for (int i = 0; i < alloc1.Length; i++) { NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (1024 * (alloc1[i]), (alloc1[i])), plan.GetTotalCommittedResources(start + i)); NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (1024 * (alloc1[i]), (alloc1[i])), plan.GetConsumptionForUser(user, start + i)); } // Now add another one ReservationId reservationID2 = ReservationSystemTestUtil.GetNewReservationId(); int[] alloc2 = new int[] { 0, 5, 10, 5, 0 }; IDictionary <ReservationInterval, ReservationRequest> allocations2 = GenerateAllocation (start, alloc2, true); ReservationDefinition rDef2 = CreateSimpleReservationDefinition(start, start + alloc2 .Length, alloc2.Length, allocations2.Values); rAllocation = new InMemoryReservationAllocation(reservationID2, rDef2, user, planName , start, start + alloc2.Length, allocations2, resCalc, minAlloc); NUnit.Framework.Assert.IsNull(plan.GetReservationById(reservationID2)); try { plan.AddReservation(rAllocation); } catch (PlanningException e) { NUnit.Framework.Assert.Fail(e.Message); } NUnit.Framework.Assert.IsNotNull(plan.GetReservationById(reservationID2)); for (int i_1 = 0; i_1 < alloc2.Length; i_1++) { NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (1024 * (alloc1[i_1] + alloc2[i_1] + i_1), alloc1[i_1] + alloc2[i_1] + i_1), plan .GetTotalCommittedResources(start + i_1)); NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (1024 * (alloc1[i_1] + alloc2[i_1] + i_1), alloc1[i_1] + alloc2[i_1] + i_1), plan .GetConsumptionForUser(user, start + i_1)); } // Now archive completed reservations Org.Mockito.Mockito.When(clock.GetTime()).ThenReturn(106L); Org.Mockito.Mockito.When(policy.GetValidWindow()).ThenReturn(1L); try { // will only remove 2nd reservation as only that has fallen out of the // archival window plan.ArchiveCompletedReservations(clock.GetTime()); } catch (PlanningException e) { NUnit.Framework.Assert.Fail(e.Message); } NUnit.Framework.Assert.IsNotNull(plan.GetReservationById(reservationID1)); NUnit.Framework.Assert.IsNull(plan.GetReservationById(reservationID2)); for (int i_2 = 0; i_2 < alloc1.Length; i_2++) { NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (1024 * (alloc1[i_2]), (alloc1[i_2])), plan.GetTotalCommittedResources(start + i_2 )); NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (1024 * (alloc1[i_2]), (alloc1[i_2])), plan.GetConsumptionForUser(user, start + i_2)); } Org.Mockito.Mockito.When(clock.GetTime()).ThenReturn(107L); try { // will remove 1st reservation also as it has fallen out of the archival // window plan.ArchiveCompletedReservations(clock.GetTime()); } catch (PlanningException e) { NUnit.Framework.Assert.Fail(e.Message); } NUnit.Framework.Assert.IsNull(plan.GetReservationById(reservationID1)); for (int i_3 = 0; i_3 < alloc1.Length; i_3++) { NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (0, 0), plan.GetTotalCommittedResources(start + i_3)); NUnit.Framework.Assert.AreEqual(Org.Apache.Hadoop.Yarn.Api.Records.Resource.NewInstance (0, 0), plan.GetConsumptionForUser(user, start + i_3)); } }