Repository: hadoop Updated Branches: refs/heads/branch-2.8 9862879e2 -> c487453b9
http://git-wip-us.apache.org/repos/asf/hadoop/blob/c487453b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestInMemoryPlan.java ---------------------------------------------------------------------- diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestInMemoryPlan.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestInMemoryPlan.java index 1756e86..bc98e2f 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestInMemoryPlan.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestInMemoryPlan.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.apache.hadoop.yarn.api.records.ReservationDefinition; import org.apache.hadoop.yarn.api.records.ReservationId; @@ -98,19 +99,11 @@ public class TestInMemoryPlan { new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, resCalc, minAlloc, maxAlloc, planName, replanner, true, context); ReservationId reservationID = - ReservationSystemTestUtil.getNewReservationId(); + ReservationSystemTestUtil.getNewReservationId(); int[] alloc = { 10, 10, 10, 10, 10, 10 }; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations = - generateAllocation(start, alloc, false); - ReservationDefinition rDef = - createSimpleReservationDefinition(start, start + alloc.length, - alloc.length, allocations.values()); - Map<ReservationInterval, Resource> allocs = - ReservationSystemUtil.toResources(allocations); - ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + alloc.length, allocs, resCalc, minAlloc); + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); Assert.assertNull(plan.getReservationById(reservationID)); try { plan.addReservation(rAllocation, false); @@ -139,19 +132,11 @@ public class TestInMemoryPlan { new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, resCalc, minAlloc, maxAlloc, planName, replanner, true, context); ReservationId reservationID = - ReservationSystemTestUtil.getNewReservationId(); + ReservationSystemTestUtil.getNewReservationId(); int[] alloc = {}; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations = - new HashMap<ReservationInterval, ReservationRequest>(); - ReservationDefinition rDef = - createSimpleReservationDefinition(start, start + alloc.length, - alloc.length, allocations.values()); - Map<ReservationInterval, Resource> allocs = ReservationSystemUtil.toResources - (allocations); - ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + alloc.length, allocs, resCalc, minAlloc); + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); Assert.assertNull(plan.getReservationById(reservationID)); try { plan.addReservation(rAllocation, false); @@ -167,19 +152,11 @@ public class TestInMemoryPlan { new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, resCalc, minAlloc, maxAlloc, planName, replanner, true, context); ReservationId reservationID = - ReservationSystemTestUtil.getNewReservationId(); + ReservationSystemTestUtil.getNewReservationId(); int[] alloc = { 10, 10, 10, 10, 10, 10 }; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations = - generateAllocation(start, alloc, false); - ReservationDefinition rDef = - createSimpleReservationDefinition(start, start + alloc.length, - alloc.length, allocations.values()); - Map<ReservationInterval, Resource> allocs = ReservationSystemUtil.toResources - (allocations); - ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + alloc.length, allocs, resCalc, minAlloc); + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); Assert.assertNull(plan.getReservationById(reservationID)); try { plan.addReservation(rAllocation, false); @@ -211,16 +188,8 @@ public class TestInMemoryPlan { // First add a reservation int[] alloc = { 10, 10, 10, 10, 10, 10 }; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations = - generateAllocation(start, alloc, false); - ReservationDefinition rDef = - createSimpleReservationDefinition(start, start + alloc.length, - alloc.length, allocations.values()); - Map<ReservationInterval, Resource> allocs = ReservationSystemUtil.toResources - (allocations); - ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + alloc.length, allocs, resCalc, minAlloc); + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); Assert.assertNull(plan.getReservationById(reservationID)); try { plan.addReservation(rAllocation, false); @@ -241,16 +210,8 @@ public class TestInMemoryPlan { // Now update it start = 110; int[] updatedAlloc = { 0, 5, 10, 10, 5, 0 }; - allocations = generateAllocation(start, updatedAlloc, true); - rDef = - createSimpleReservationDefinition(start, start + updatedAlloc.length, - updatedAlloc.length, allocations.values()); - Map<ReservationInterval, Resource> updatedAllocs = - ReservationSystemUtil.toResources(allocations); - rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + updatedAlloc.length, updatedAllocs, resCalc, - minAlloc); + rAllocation = createReservationAllocation(reservationID, start, + updatedAlloc, true); try { plan.updateReservation(rAllocation); } catch (PlanningException e) { @@ -282,16 +243,8 @@ public class TestInMemoryPlan { // Try to update a reservation without adding int[] alloc = { 10, 10, 10, 10, 10, 10 }; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations = - generateAllocation(start, alloc, false); - ReservationDefinition rDef = - createSimpleReservationDefinition(start, start + alloc.length, - alloc.length, allocations.values()); - Map<ReservationInterval, Resource> allocs = - ReservationSystemUtil.toResources(allocations); ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + alloc.length, allocs, resCalc, minAlloc); + createReservationAllocation(reservationID, start, alloc); Assert.assertNull(plan.getReservationById(reservationID)); try { plan.updateReservation(rAllocation); @@ -314,16 +267,8 @@ public class TestInMemoryPlan { ReservationSystemTestUtil.getNewReservationId(); int[] alloc = { 10, 10, 10, 10, 10, 10 }; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations = - generateAllocation(start, alloc, true); - ReservationDefinition rDef = - createSimpleReservationDefinition(start, start + alloc.length, - alloc.length, allocations.values()); - Map<ReservationInterval, Resource> allocs = - ReservationSystemUtil.toResources(allocations); ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID, rDef, user, planName, - start, start + alloc.length, allocs, resCalc, minAlloc); + createReservationAllocation(reservationID, start, alloc, true); Assert.assertNull(plan.getReservationById(reservationID)); try { plan.addReservation(rAllocation, false); @@ -391,17 +336,8 @@ public class TestInMemoryPlan { // First add a reservation int[] alloc1 = { 10, 10, 10, 10, 10, 10 }; int start = 100; - Map<ReservationInterval, ReservationRequest> allocations1 = - generateAllocation(start, alloc1, false); - ReservationDefinition rDef1 = - createSimpleReservationDefinition(start, start + alloc1.length, - alloc1.length, allocations1.values()); - Map<ReservationInterval, Resource> allocs1 = - ReservationSystemUtil.toResources(allocations1); ReservationAllocation rAllocation = - new InMemoryReservationAllocation(reservationID1, rDef1, user, - planName, start, start + alloc1.length, allocs1, resCalc, - minAlloc); + createReservationAllocation(reservationID1, start, alloc1); Assert.assertNull(plan.getReservationById(reservationID1)); try { plan.addReservation(rAllocation, false); @@ -416,17 +352,8 @@ public class TestInMemoryPlan { ReservationId reservationID2 = ReservationSystemTestUtil.getNewReservationId(); int[] alloc2 = { 0, 5, 10, 5, 0 }; - Map<ReservationInterval, ReservationRequest> allocations2 = - generateAllocation(start, alloc2, true); - ReservationDefinition rDef2 = - createSimpleReservationDefinition(start, start + alloc2.length, - alloc2.length, allocations2.values()); - Map<ReservationInterval, Resource> allocs2 = - ReservationSystemUtil.toResources(allocations2); rAllocation = - new InMemoryReservationAllocation(reservationID2, rDef2, user, - planName, start, start + alloc2.length, allocs2, resCalc, - minAlloc); + createReservationAllocation(reservationID2, start, alloc2, true); Assert.assertNull(plan.getReservationById(reservationID2)); try { plan.addReservation(rAllocation, false); @@ -482,6 +409,192 @@ public class TestInMemoryPlan { } } + @Test + public void testGetReservationsById() { + Plan plan = + new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, + resCalc, minAlloc, maxAlloc, planName, replanner, true, context); + ReservationId reservationID = + ReservationSystemTestUtil.getNewReservationId(); + int[] alloc = {10, 10, 10, 10, 10, 10}; + int start = 100; + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); + Assert.assertNull(plan.getReservationById(reservationID)); + try { + plan.addReservation(rAllocation, false); + } catch (PlanningException e) { + Assert.fail(e.getMessage()); + } + + // Verify that get by reservation id works. + Set<ReservationAllocation> rAllocations = + plan.getReservations(reservationID, null, ""); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + + // Verify that get by reservation id works even when time range + // and user is invalid. + ReservationInterval interval = new ReservationInterval(0, 0); + rAllocations = plan.getReservations(reservationID, interval, "invalid"); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + } + + @Test + public void testGetReservationsByInvalidId() { + Plan plan = + new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, + resCalc, minAlloc, maxAlloc, planName, replanner, true, context); + ReservationId reservationID = + ReservationSystemTestUtil.getNewReservationId(); + int[] alloc = {10, 10, 10, 10, 10, 10}; + int start = 100; + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); + Assert.assertNull(plan.getReservationById(reservationID)); + try { + plan.addReservation(rAllocation, false); + } catch (PlanningException e) { + Assert.fail(e.getMessage()); + } + + // If reservationId is null, then nothing is returned. + ReservationId invalidReservationID = + ReservationSystemTestUtil.getNewReservationId(); + Set<ReservationAllocation> rAllocations = + plan.getReservations(invalidReservationID, null, ""); + Assert.assertTrue(rAllocations.size() == 0); + } + + @Test + public void testGetReservationsByTimeInterval() { + Plan plan = + new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, + resCalc, minAlloc, maxAlloc, planName, replanner, true, context); + ReservationId reservationID = + ReservationSystemTestUtil.getNewReservationId(); + int[] alloc = {10, 10, 10, 10, 10, 10}; + int start = 100; + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); + Assert.assertNull(plan.getReservationById(reservationID)); + try { + plan.addReservation(rAllocation, false); + } catch (PlanningException e) { + Assert.fail(e.getMessage()); + } + + // Verify that get by time interval works if the selection interval + // completely overlaps with the allocation. + ReservationInterval interval = new ReservationInterval(rAllocation + .getStartTime(), rAllocation.getEndTime()); + Set<ReservationAllocation> rAllocations = + plan.getReservations(null, interval, ""); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + + // Verify that get by time interval works if the selection interval + // falls within the allocation + long duration = rAllocation.getEndTime() - rAllocation.getStartTime(); + interval = new ReservationInterval(rAllocation.getStartTime() + duration + * (long)0.3, rAllocation.getEndTime() - duration * (long)0.3); + rAllocations = plan.getReservations(null, interval, ""); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + + // Verify that get by time interval selects 1 allocation if the end + // time of the selection interval falls right at the start of the + // allocation. + interval = new ReservationInterval(0, rAllocation.getStartTime()); + rAllocations = plan.getReservations(null, interval, ""); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + + // Verify that get by time interval selects no reservations if the start + // time of the selection interval falls right at the end of the allocation. + interval = new ReservationInterval(rAllocation + .getEndTime(), Long.MAX_VALUE); + rAllocations = plan.getReservations(null, interval, ""); + Assert.assertTrue(rAllocations.size() == 0); + + // Verify that get by time interval selects no reservations if the + // selection interval and allocation interval do not overlap. + interval = new ReservationInterval(0, rAllocation.getStartTime() / 2); + rAllocations = plan.getReservations(null, interval, ""); + Assert.assertTrue(rAllocations.size() == 0); + } + + @Test + public void testGetReservationsAtTime() { + Plan plan = + new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, + resCalc, minAlloc, maxAlloc, planName, replanner, true, context); + ReservationId reservationID = + ReservationSystemTestUtil.getNewReservationId(); + int[] alloc = {10, 10, 10, 10, 10, 10}; + int start = 100; + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); + Assert.assertNull(plan.getReservationById(reservationID)); + try { + plan.addReservation(rAllocation, false); + } catch (PlanningException e) { + Assert.fail(e.getMessage()); + } + + Set<ReservationAllocation> rAllocations = + plan.getReservationsAtTime(rAllocation.getStartTime()); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + } + + @Test + public void testGetReservationsWithNoInput() { + Plan plan = + new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, + resCalc, minAlloc, maxAlloc, planName, replanner, true, context); + ReservationId reservationID = + ReservationSystemTestUtil.getNewReservationId(); + int[] alloc = {10, 10, 10, 10, 10, 10}; + int start = 100; + ReservationAllocation rAllocation = createReservationAllocation + (reservationID, start, alloc); + Assert.assertNull(plan.getReservationById(reservationID)); + try { + plan.addReservation(rAllocation, false); + } catch (PlanningException e) { + Assert.fail(e.getMessage()); + } + + // Verify that getReservations defaults to getting all reservations if no + // reservationID, time interval, and user is provided, + Set<ReservationAllocation> rAllocations = + plan.getReservations(null, null, ""); + Assert.assertTrue(rAllocations.size() == 1); + Assert.assertTrue(rAllocation.compareTo( + (ReservationAllocation) rAllocations.toArray()[0]) == 0); + } + + @Test + public void testGetReservationsWithNoReservation() { + Plan plan = + new InMemoryPlan(queueMetrics, policy, agent, totalCapacity, 1L, + resCalc, minAlloc, maxAlloc, planName, replanner, true, context); + // Verify that get reservation returns no entries if no queries are made. + + ReservationInterval interval = new ReservationInterval(0, Long.MAX_VALUE); + Set<ReservationAllocation> rAllocations = + plan.getReservations(null, interval, ""); + Assert.assertTrue(rAllocations.size() == 0); + } + private void doAssertions(Plan plan, ReservationAllocation rAllocation) { ReservationId reservationID = rAllocation.getReservationId(); Assert.assertNotNull(plan.getReservationById(reservationID)); @@ -528,4 +641,23 @@ public class TestInMemoryPlan { return req; } + private ReservationAllocation createReservationAllocation(ReservationId + reservationID, int start, int[] alloc) { + return createReservationAllocation(reservationID, start, alloc, false); + } + + private ReservationAllocation createReservationAllocation(ReservationId + reservationID, int start, int[] alloc, boolean isStep) { + Map<ReservationInterval, ReservationRequest> allocations = + generateAllocation(start, alloc, isStep); + ReservationDefinition rDef = + createSimpleReservationDefinition(start, start + alloc.length, + alloc.length, allocations.values()); + Map<ReservationInterval, Resource> allocs = + ReservationSystemUtil.toResources(allocations); + return new InMemoryReservationAllocation(reservationID, rDef, user, + planName, + start, start + alloc.length, allocs, resCalc, minAlloc); + } + } http://git-wip-us.apache.org/repos/asf/hadoop/blob/c487453b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationInputValidator.java ---------------------------------------------------------------------- diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationInputValidator.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationInputValidator.java index 93adf74..bd40ccd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationInputValidator.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationInputValidator.java @@ -29,9 +29,11 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.yarn.api.protocolrecords.ReservationDeleteRequest; +import org.apache.hadoop.yarn.api.protocolrecords.ReservationListRequest; import org.apache.hadoop.yarn.api.protocolrecords.ReservationSubmissionRequest; import org.apache.hadoop.yarn.api.protocolrecords.ReservationUpdateRequest; import org.apache.hadoop.yarn.api.protocolrecords.impl.pb.ReservationDeleteRequestPBImpl; +import org.apache.hadoop.yarn.api.protocolrecords.impl.pb.ReservationListRequestPBImpl; import org.apache.hadoop.yarn.api.protocolrecords.impl.pb.ReservationSubmissionRequestPBImpl; import org.apache.hadoop.yarn.api.protocolrecords.impl.pb.ReservationUpdateRequestPBImpl; import org.apache.hadoop.yarn.api.records.ReservationDefinition; @@ -105,7 +107,7 @@ public class TestReservationInputValidator { } @Test - public void testSubmitReservationDoesnotExist() { + public void testSubmitReservationDoesNotExist() { ReservationSubmissionRequest request = new ReservationSubmissionRequestPBImpl(); Plan plan = null; @@ -119,7 +121,7 @@ public class TestReservationInputValidator { String message = e.getMessage(); Assert .assertTrue(message - .equals("The queue to submit is not specified. Please try again with a valid reservable queue.")); + .equals("The queue is not specified. Please try again with a valid reservable queue.")); LOG.info(message); } } @@ -523,6 +525,103 @@ public class TestReservationInputValidator { } } + @Test + public void testListReservationsNormal() { + ReservationListRequest request = new ReservationListRequestPBImpl(); + request.setQueue(ReservationSystemTestUtil.reservationQ); + request.setEndTime(1000); + request.setStartTime(0); + when(rSystem.getPlan(ReservationSystemTestUtil.reservationQ)).thenReturn + (this.plan); + Plan plan = null; + try { + plan = rrValidator.validateReservationListRequest(rSystem, request); + } catch (YarnException e) { + Assert.fail(e.getMessage()); + } + Assert.assertNotNull(plan); + } + + @Test + public void testListReservationsInvalidTimeIntervalDefaults() { + ReservationListRequest request = new ReservationListRequestPBImpl(); + request.setQueue(ReservationSystemTestUtil.reservationQ); + // Negative time gets converted to default values for Start Time and End + // Time which are 0 and Long.MAX_VALUE respectively. + request.setEndTime(-2); + request.setStartTime(-1); + when(rSystem.getPlan(ReservationSystemTestUtil.reservationQ)).thenReturn + (this.plan); + Plan plan = null; + try { + plan = rrValidator.validateReservationListRequest(rSystem, request); + } catch (YarnException e) { + Assert.fail(e.getMessage()); + } + Assert.assertNotNull(plan); + } + + @Test + public void testListReservationsInvalidTimeInterval() { + ReservationListRequest request = new ReservationListRequestPBImpl(); + request.setQueue(ReservationSystemTestUtil.reservationQ); + request.setEndTime(1000); + request.setStartTime(2000); + when(rSystem.getPlan(ReservationSystemTestUtil.reservationQ)).thenReturn + (this.plan); + Plan plan = null; + try { + plan = rrValidator.validateReservationListRequest(rSystem, request); + Assert.fail(); + } catch (YarnException e) { + Assert.assertNull(plan); + String message = e.getMessage(); + Assert.assertTrue(message.equals("The specified end time must be " + + "greater than the specified start time.")); + LOG.info(message); + } + } + + @Test + public void testListReservationsEmptyQueue() { + ReservationListRequest request = new ReservationListRequestPBImpl(); + request.setQueue(""); + Plan plan = null; + try { + plan = rrValidator.validateReservationListRequest(rSystem, request); + Assert.fail(); + } catch (YarnException e) { + Assert.assertNull(plan); + String message = e.getMessage(); + Assert.assertTrue(message.equals( + "The queue is not specified. Please try again with a valid " + + "reservable queue.")); + LOG.info(message); + } + } + + @Test + public void testListReservationsNullPlan() { + ReservationListRequest request = new ReservationListRequestPBImpl(); + request.setQueue(ReservationSystemTestUtil.reservationQ); + when(rSystem.getPlan(ReservationSystemTestUtil.reservationQ)).thenReturn + (null); + Plan plan = null; + try { + plan = rrValidator.validateReservationListRequest(rSystem, request); + Assert.fail(); + } catch (YarnException e) { + Assert.assertNull(plan); + String message = e.getMessage(); + Assert.assertTrue(message.equals( + "The specified queue: " + ReservationSystemTestUtil.reservationQ + + " is not managed by reservation system." + + " Please try again with a valid reservable queue." + )); + LOG.info(message); + } + } + private ReservationSubmissionRequest createSimpleReservationSubmissionRequest( int numRequests, int numContainers, long arrival, long deadline, long duration) { http://git-wip-us.apache.org/repos/asf/hadoop/blob/c487453b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationSystemUtil.java ---------------------------------------------------------------------- diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationSystemUtil.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationSystemUtil.java new file mode 100644 index 0000000..4cb235f --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/reservation/TestReservationSystemUtil.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package org.apache.hadoop.yarn.server.resourcemanager.reservation; + +import static org.mockito.Mockito.mock; + +import org.apache.hadoop.yarn.api.records.ReservationAllocationState; +import org.apache.hadoop.yarn.api.records.ReservationDefinition; +import org.apache.hadoop.yarn.api.records.ReservationId; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; +import org.apache.hadoop.yarn.util.resource.ResourceCalculator; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; + +public class TestReservationSystemUtil { + + @Test + public void testConvertAllocationsToReservationInfo() { + long startTime = new Date().getTime(); + long step = 10000; + int[] alloc = {10, 10, 10}; + ReservationId id = ReservationSystemTestUtil.getNewReservationId(); + ReservationAllocation allocation = createReservationAllocation( + startTime, startTime + 10 * step, step, alloc, id, + createResource(4000, 2)); + + List<ReservationAllocationState> infoList = ReservationSystemUtil + .convertAllocationsToReservationInfo( + Collections.singleton(allocation), true); + + Assert.assertEquals(infoList.size(), 1); + Assert.assertEquals(infoList.get(0).getReservationId().toString(), + id.toString()); + Assert.assertFalse(infoList.get(0).getResourceAllocationRequests() + .isEmpty()); + } + + @Test + public void testConvertAllocationsToReservationInfoNoAllocations() { + long startTime = new Date().getTime(); + long step = 10000; + int[] alloc = {10, 10, 10}; + ReservationId id = ReservationSystemTestUtil.getNewReservationId(); + ReservationAllocation allocation = createReservationAllocation( + startTime, startTime + 10 * step, step, alloc, id, + createResource(4000, 2)); + + List<ReservationAllocationState> infoList = ReservationSystemUtil + .convertAllocationsToReservationInfo( + Collections.singleton(allocation), false); + + Assert.assertEquals(infoList.size(), 1); + Assert.assertEquals(infoList.get(0).getReservationId().toString(), + id.toString()); + Assert.assertTrue(infoList.get(0).getResourceAllocationRequests() + .isEmpty()); + } + + @Test + public void testConvertAllocationsToReservationInfoEmptyAllocations() { + long startTime = new Date().getTime(); + long step = 10000; + int[] alloc = {}; + ReservationId id = ReservationSystemTestUtil.getNewReservationId(); + ReservationAllocation allocation = createReservationAllocation( + startTime, startTime + 10 * step, step, alloc, id, + createResource(4000, 2)); + + List<ReservationAllocationState> infoList = ReservationSystemUtil + .convertAllocationsToReservationInfo( + Collections.singleton(allocation), false); + + Assert.assertEquals(infoList.size(), 1); + Assert.assertEquals(infoList.get(0).getReservationId().toString(), + id.toString()); + Assert.assertTrue(infoList.get(0).getResourceAllocationRequests() + .isEmpty()); + } + + @Test + public void testConvertAllocationsToReservationInfoEmptySet() { + List<ReservationAllocationState> infoList = ReservationSystemUtil + .convertAllocationsToReservationInfo( + Collections.<ReservationAllocation>emptySet(), false); + + Assert.assertEquals(infoList.size(), 0); + } + + private ReservationAllocation createReservationAllocation(long startTime, + long deadline, long step, int[] alloc, ReservationId id, Resource + minAlloc) { + + Map<ReservationInterval, Resource> allocations = ReservationSystemTestUtil + .generateAllocation(startTime, step, alloc); + + + ResourceCalculator rs = mock(ResourceCalculator.class); + + ReservationDefinition definition = ReservationSystemTestUtil + .createSimpleReservationDefinition(startTime, deadline, step); + + return new InMemoryReservationAllocation(id, + definition, "user", ReservationSystemTestUtil.reservationQ, + startTime, startTime + step, allocations, rs, minAlloc, false); + } + + public Resource createResource(int memory, int vCores) { + Resource resource = new ResourcePBImpl(); + resource.setMemory(memory); + resource.setVirtualCores(vCores); + return resource; + } +}
