diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java index 360ff99..3247c3a 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java @@ -40,7 +40,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeoutException; +import com.google.common.base.Supplier; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.DataInputByteBuffer; @@ -52,6 +54,7 @@ import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod; import org.apache.hadoop.security.token.Token; import org.apache.hadoop.security.token.TokenIdentifier; +import org.apache.hadoop.test.GenericTestUtils; import org.apache.hadoop.yarn.api.ApplicationClientProtocol; import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportRequest; import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportResponse; @@ -67,7 +70,6 @@ import org.apache.hadoop.yarn.api.protocolrecords.GetContainersResponse; import org.apache.hadoop.yarn.api.protocolrecords.GetLabelsToNodesRequest; import org.apache.hadoop.yarn.api.protocolrecords.GetLabelsToNodesResponse; -import org.apache.hadoop.yarn.api.protocolrecords.GetNewReservationRequest; import org.apache.hadoop.yarn.api.protocolrecords.GetNewReservationResponse; import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToLabelsRequest; import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToLabelsResponse; @@ -991,16 +993,9 @@ private ApplicationId createApp(YarnClient rmClient, boolean unmanaged) return appId; } - private GetNewReservationResponse getNewReservation(YarnClient rmClient) { - GetNewReservationRequest newReservationRequest = GetNewReservationRequest - .newInstance(); - GetNewReservationResponse getNewReservationResponse = null; - try { - getNewReservationResponse = rmClient.createReservation(); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } - return getNewReservationResponse; + private GetNewReservationResponse getNewReservation(YarnClient rmClient) + throws YarnException, IOException { + return rmClient.createReservation(); } private void waitTillAccepted(YarnClient rmClient, ApplicationId appId, @@ -1193,7 +1188,8 @@ public void testParseTimelineDelegationTokenRenewer() throws Exception { } } - private MiniYARNCluster setupMiniYARNCluster() { + private MiniYARNCluster setupMiniYARNCluster() + throws TimeoutException, InterruptedException { CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); ReservationSystemTestUtil.setupQueueConfiguration(conf); conf.setClass(YarnConfiguration.RM_SCHEDULER, CapacityScheduler.class, @@ -1205,23 +1201,15 @@ private MiniYARNCluster setupMiniYARNCluster() { cluster.init(conf); cluster.start(); - int attempts; - for (attempts = 10; attempts > 0; attempts--) { - if (cluster.getResourceManager().getRMContext().getReservationSystem() - .getPlan(ReservationSystemTestUtil.reservationQ).getTotalCapacity() - .getMemorySize() > 6000) { - break; - } - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); + GenericTestUtils.waitFor(new Supplier() { + @Override + public Boolean get() { + return cluster.getResourceManager().getRMContext() + .getReservationSystem() + .getPlan(ReservationSystemTestUtil.reservationQ) + .getTotalCapacity().getMemorySize() > 6000; } - } - if (attempts <= 0) { - Assert.fail("Exhausted attempts in checking if node capacity was " - + "added to the plan"); - } + }, 10, 10000); return cluster; } @@ -1235,7 +1223,8 @@ private YarnClient setupYarnClient(MiniYARNCluster cluster) { } private ReservationSubmissionRequest submitReservationTestHelper( - YarnClient client, long arrival, long deadline, long duration) { + YarnClient client, long arrival, long deadline, long duration) + throws IOException, YarnException { ReservationId reservationID = getNewReservation(client).getReservationId(); ReservationSubmissionRequest sRequest = createSimpleReservationRequest( reservationID, 4, arrival, deadline, duration); @@ -1253,7 +1242,8 @@ private ReservationSubmissionRequest submitReservationTestHelper( } @Test - public void testCreateReservation() { + public void testCreateReservation() throws TimeoutException, + InterruptedException, YarnException, IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1266,11 +1256,7 @@ public void testCreateReservation() { // Submit the reservation again with the same request and make sure it // passes. - try { - client.submitReservation(sRequest); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + client.submitReservation(sRequest); // Submit the reservation with the same reservation id but different // reservation definition, and ensure YarnException is thrown. @@ -1296,7 +1282,8 @@ public void testCreateReservation() { } @Test - public void testUpdateReservation() { + public void testUpdateReservation() throws TimeoutException, + InterruptedException, YarnException, IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1320,12 +1307,7 @@ public void testUpdateReservation() { rDef.setDeadline(deadline); ReservationUpdateRequest uRequest = ReservationUpdateRequest.newInstance(rDef, reservationID); - ReservationUpdateResponse uResponse = null; - try { - uResponse = client.updateReservation(uRequest); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationUpdateResponse uResponse = client.updateReservation(uRequest); Assert.assertNotNull(uResponse); System.out.println("Update reservation response: " + uResponse); } finally { @@ -1338,7 +1320,8 @@ public void testUpdateReservation() { } @Test - public void testListReservationsByReservationId() { + public void testListReservationsByReservationId() throws TimeoutException, + InterruptedException, YarnException, IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1350,15 +1333,10 @@ public void testListReservationsByReservationId() { submitReservationTestHelper(client, arrival, deadline, duration); ReservationId reservationID = sRequest.getReservationId(); - ReservationListResponse response = null; ReservationListRequest request = ReservationListRequest.newInstance( ReservationSystemTestUtil.reservationQ, reservationID.toString(), -1, -1, false); - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationListResponse response = client.listReservations(request); Assert.assertNotNull(response); Assert.assertEquals(1, response.getReservationAllocationState().size()); Assert.assertEquals(response.getReservationAllocationState().get(0) @@ -1375,7 +1353,8 @@ public void testListReservationsByReservationId() { } @Test - public void testListReservationsByTimeInterval() { + public void testListReservationsByTimeInterval() throws TimeoutException, + InterruptedException, YarnException, IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1394,12 +1373,7 @@ public void testListReservationsByTimeInterval() { ReservationSystemTestUtil.reservationQ, "", arrival + duration / 2, arrival + duration / 2, true); - ReservationListResponse response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationListResponse response = client.listReservations(request); Assert.assertNotNull(response); Assert.assertEquals(1, response.getReservationAllocationState().size()); Assert.assertEquals(response.getReservationAllocationState().get(0) @@ -1408,12 +1382,7 @@ public void testListReservationsByTimeInterval() { request = ReservationListRequest.newInstance( ReservationSystemTestUtil.reservationQ, "", 1, Long.MAX_VALUE, true); - response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + response = client.listReservations(request); Assert.assertNotNull(response); Assert.assertEquals(1, response.getReservationAllocationState().size()); Assert.assertEquals(response.getReservationAllocationState().get(0) @@ -1440,7 +1409,9 @@ public void testListReservationsByTimeInterval() { } @Test - public void testListReservationsByInvalidTimeInterval() { + public void testListReservationsByInvalidTimeInterval() + throws TimeoutException, InterruptedException, YarnException, + IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1455,12 +1426,7 @@ public void testListReservationsByInvalidTimeInterval() { ReservationListRequest request = ReservationListRequest .newInstance(ReservationSystemTestUtil.reservationQ, "", 1, -1, true); - ReservationListResponse response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationListResponse response = client.listReservations(request); Assert.assertNotNull(response); Assert.assertEquals(1, response.getReservationAllocationState().size()); Assert.assertEquals(response.getReservationAllocationState().get(0) @@ -1470,12 +1436,7 @@ public void testListReservationsByInvalidTimeInterval() { request = ReservationListRequest.newInstance( ReservationSystemTestUtil.reservationQ, "", 1, -10, true); - response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + response = client.listReservations(request); Assert.assertNotNull(response); Assert.assertEquals(1, response.getReservationAllocationState().size()); Assert.assertEquals(response.getReservationAllocationState().get(0) @@ -1490,7 +1451,9 @@ public void testListReservationsByInvalidTimeInterval() { } @Test - public void testListReservationsByTimeIntervalContainingNoReservations() { + public void testListReservationsByTimeIntervalContainingNoReservations() + throws TimeoutException, InterruptedException, YarnException, + IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1506,12 +1469,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations() { ReservationSystemTestUtil.reservationQ, "", Long.MAX_VALUE, -1, false); - ReservationListResponse response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationListResponse response = client.listReservations(request); // Ensure all reservations are filtered out. Assert.assertNotNull(response); @@ -1526,12 +1484,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations() { ReservationSystemTestUtil.reservationQ, "", deadline + duration, deadline + 2 * duration, false); - response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + response = client.listReservations(request); // Ensure all reservations are filtered out. Assert.assertNotNull(response); @@ -1544,12 +1497,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations() { ReservationSystemTestUtil.reservationQ, "", 0, arrival - duration, false); - response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + response = client.listReservations(request); // Ensure all reservations are filtered out. Assert.assertNotNull(response); @@ -1559,12 +1507,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations() { request = ReservationListRequest .newInstance(ReservationSystemTestUtil.reservationQ, "", 0, 1, false); - response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + response = client.listReservations(request); // Ensure all reservations are filtered out. Assert.assertNotNull(response); @@ -1580,7 +1523,8 @@ public void testListReservationsByTimeIntervalContainingNoReservations() { } @Test - public void testReservationDelete() { + public void testReservationDelete() throws TimeoutException, + InterruptedException, YarnException, IOException { MiniYARNCluster cluster = setupMiniYARNCluster(); YarnClient client = setupYarnClient(cluster); try { @@ -1595,12 +1539,7 @@ public void testReservationDelete() { // Delete the reservation ReservationDeleteRequest dRequest = ReservationDeleteRequest.newInstance(reservationID); - ReservationDeleteResponse dResponse = null; - try { - dResponse = client.deleteReservation(dRequest); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationDeleteResponse dResponse = client.deleteReservation(dRequest); Assert.assertNotNull(dResponse); System.out.println("Delete reservation response: " + dResponse); @@ -1609,12 +1548,7 @@ public void testReservationDelete() { ReservationSystemTestUtil.reservationQ, reservationID.toString(), -1, -1, false); - ReservationListResponse response = null; - try { - response = client.listReservations(request); - } catch (Exception e) { - Assert.fail(e.getMessage()); - } + ReservationListResponse response = client.listReservations(request); Assert.assertNotNull(response); Assert.assertEquals(0, response.getReservationAllocationState().size()); } finally {