Skip to content

Commit eeb18b0

Browse files
committed
YARN-5560. Clean up bad exception catching practices in TestYarnClient. Contributed by Sean Po
(cherry picked from commit 4cbe614)
1 parent 039c831 commit eeb18b0

File tree

1 file changed

+38
-134
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl

1 file changed

+38
-134
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestYarnClient.java

Lines changed: 38 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@
6969
import org.apache.hadoop.yarn.api.protocolrecords.GetContainersResponse;
7070
import org.apache.hadoop.yarn.api.protocolrecords.GetLabelsToNodesRequest;
7171
import org.apache.hadoop.yarn.api.protocolrecords.GetLabelsToNodesResponse;
72-
import org.apache.hadoop.yarn.api.protocolrecords.GetNewReservationRequest;
73-
import org.apache.hadoop.yarn.api.protocolrecords.GetNewReservationResponse;
7472
import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToLabelsRequest;
7573
import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToLabelsResponse;
7674
import org.apache.hadoop.yarn.api.protocolrecords.KillApplicationRequest;
@@ -158,7 +156,7 @@ public void testClientStop() {
158156

159157
@SuppressWarnings("deprecation")
160158
@Test (timeout = 30000)
161-
public void testSubmitApplication() {
159+
public void testSubmitApplication() throws Exception {
162160
Configuration conf = new Configuration();
163161
conf.setLong(YarnConfiguration.YARN_CLIENT_APP_SUBMISSION_POLL_INTERVAL_MS,
164162
100); // speed up tests
@@ -184,8 +182,6 @@ public void testSubmitApplication() {
184182
Assert.assertTrue(e instanceof ApplicationIdNotProvidedException);
185183
Assert.assertTrue(e.getMessage().contains(
186184
"ApplicationId is not provided in ApplicationSubmissionContext"));
187-
} catch (IOException e) {
188-
Assert.fail("IOException is not expected.");
189185
}
190186

191187
// Submit the application with applicationId provided
@@ -197,13 +193,7 @@ public void testSubmitApplication() {
197193
System.currentTimeMillis(), i);
198194
when(context.getApplicationId()).thenReturn(applicationId);
199195
((MockYarnClient) client).setYarnApplicationState(exitStates[i]);
200-
try {
201-
client.submitApplication(context);
202-
} catch (YarnException e) {
203-
Assert.fail("Exception is not expected.");
204-
} catch (IOException e) {
205-
Assert.fail("Exception is not expected.");
206-
}
196+
client.submitApplication(context);
207197
verify(((MockYarnClient) client).mockReport,times(4 * i + 4))
208198
.getYarnApplicationState();
209199
}
@@ -583,12 +573,11 @@ public void start() {
583573
.thenReturn(mockNodeToLabelsResponse);
584574

585575
historyClient = mock(AHSClient.class);
586-
587-
} catch (YarnException e) {
588-
Assert.fail("Exception is not expected.");
589-
} catch (IOException e) {
590-
Assert.fail("Exception is not expected.");
576+
577+
} catch (Exception e) {
578+
Assert.fail("Unexpected exception caught: " + e);
591579
}
580+
592581
when(mockResponse.getApplicationReport()).thenReturn(mockReport);
593582
}
594583

@@ -993,36 +982,20 @@ private ApplicationId createApp(YarnClient rmClient, boolean unmanaged)
993982
return appId;
994983
}
995984

996-
private GetNewReservationResponse getNewReservation(YarnClient rmClient) {
997-
GetNewReservationRequest newReservationRequest = GetNewReservationRequest
998-
.newInstance();
999-
GetNewReservationResponse getNewReservationResponse = null;
1000-
try {
1001-
getNewReservationResponse = rmClient.createReservation();
1002-
} catch (Exception e) {
1003-
Assert.fail(e.getMessage());
1004-
}
1005-
return getNewReservationResponse;
1006-
}
1007-
1008985
private void waitTillAccepted(YarnClient rmClient, ApplicationId appId,
1009986
boolean unmanagedApplication)
1010987
throws Exception {
1011-
try {
1012-
long start = System.currentTimeMillis();
1013-
ApplicationReport report = rmClient.getApplicationReport(appId);
1014-
while (YarnApplicationState.ACCEPTED != report.getYarnApplicationState()) {
1015-
if (System.currentTimeMillis() - start > 20 * 1000) {
1016-
throw new Exception("App '" + appId +
1017-
"' time out, failed to reach ACCEPTED state");
1018-
}
1019-
Thread.sleep(200);
1020-
report = rmClient.getApplicationReport(appId);
988+
long start = System.currentTimeMillis();
989+
ApplicationReport report = rmClient.getApplicationReport(appId);
990+
while (YarnApplicationState.ACCEPTED != report.getYarnApplicationState()) {
991+
if (System.currentTimeMillis() - start > 20 * 1000) {
992+
throw new Exception(
993+
"App '" + appId + "' time out, failed to reach ACCEPTED state");
1021994
}
1022-
Assert.assertEquals(unmanagedApplication, report.isUnmanagedApp());
1023-
} catch (Exception ex) {
1024-
throw new Exception(ex);
995+
Thread.sleep(200);
996+
report = rmClient.getApplicationReport(appId);
1025997
}
998+
Assert.assertEquals(unmanagedApplication, report.isUnmanagedApp());
1026999
}
10271000

10281001
@Test
@@ -1071,13 +1044,11 @@ TimelineClient createTimelineClient() throws IOException, YarnException {
10711044
});
10721045

10731046
client.init(conf);
1074-
try {
1075-
conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_CLIENT_BEST_EFFORT, true);
1076-
client.serviceInit(conf);
1077-
client.getTimelineDelegationToken();
1078-
} catch (Exception e) {
1079-
Assert.fail("Should not have thrown an exception");
1080-
}
1047+
conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_CLIENT_BEST_EFFORT,
1048+
true);
1049+
client.serviceInit(conf);
1050+
client.getTimelineDelegationToken();
1051+
10811052
try {
10821053
conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_CLIENT_BEST_EFFORT, false);
10831054
client.serviceInit(conf);
@@ -1229,16 +1200,13 @@ private YarnClient setupYarnClient(MiniYARNCluster cluster) {
12291200
}
12301201

12311202
private ReservationSubmissionRequest submitReservationTestHelper(
1232-
YarnClient client, long arrival, long deadline, long duration) {
1233-
ReservationId reservationID = getNewReservation(client).getReservationId();
1203+
YarnClient client, long arrival, long deadline, long duration)
1204+
throws IOException, YarnException {
1205+
ReservationId reservationID = client.createReservation().getReservationId();
12341206
ReservationSubmissionRequest sRequest = createSimpleReservationRequest(
12351207
reservationID, 4, arrival, deadline, duration);
1236-
ReservationSubmissionResponse sResponse = null;
1237-
try {
1238-
sResponse = client.submitReservation(sRequest);
1239-
} catch (Exception e) {
1240-
Assert.fail(e.getMessage());
1241-
}
1208+
ReservationSubmissionResponse sResponse =
1209+
client.submitReservation(sRequest);
12421210
Assert.assertNotNull(sResponse);
12431211
Assert.assertNotNull(reservationID);
12441212
System.out.println("Submit reservation response: " + reservationID);
@@ -1260,11 +1228,7 @@ public void testCreateReservation() throws Exception {
12601228

12611229
// Submit the reservation again with the same request and make sure it
12621230
// passes.
1263-
try {
1264-
client.submitReservation(sRequest);
1265-
} catch (Exception e) {
1266-
Assert.fail(e.getMessage());
1267-
}
1231+
client.submitReservation(sRequest);
12681232

12691233
// Submit the reservation with the same reservation id but different
12701234
// reservation definition, and ensure YarnException is thrown.
@@ -1314,12 +1278,7 @@ public void testUpdateReservation() throws Exception {
13141278
rDef.setDeadline(deadline);
13151279
ReservationUpdateRequest uRequest =
13161280
ReservationUpdateRequest.newInstance(rDef, reservationID);
1317-
ReservationUpdateResponse uResponse = null;
1318-
try {
1319-
uResponse = client.updateReservation(uRequest);
1320-
} catch (Exception e) {
1321-
Assert.fail(e.getMessage());
1322-
}
1281+
ReservationUpdateResponse uResponse = client.updateReservation(uRequest);
13231282
Assert.assertNotNull(uResponse);
13241283
System.out.println("Update reservation response: " + uResponse);
13251284
} finally {
@@ -1344,15 +1303,10 @@ public void testListReservationsByReservationId() throws Exception{
13441303
submitReservationTestHelper(client, arrival, deadline, duration);
13451304

13461305
ReservationId reservationID = sRequest.getReservationId();
1347-
ReservationListResponse response = null;
13481306
ReservationListRequest request = ReservationListRequest.newInstance(
13491307
ReservationSystemTestUtil.reservationQ, reservationID.toString(), -1,
13501308
-1, false);
1351-
try {
1352-
response = client.listReservations(request);
1353-
} catch (Exception e) {
1354-
Assert.fail(e.getMessage());
1355-
}
1309+
ReservationListResponse response = client.listReservations(request);
13561310
Assert.assertNotNull(response);
13571311
Assert.assertEquals(1, response.getReservationAllocationState().size());
13581312
Assert.assertEquals(response.getReservationAllocationState().get(0)
@@ -1388,12 +1342,7 @@ public void testListReservationsByTimeInterval() throws Exception {
13881342
ReservationSystemTestUtil.reservationQ, "", arrival + duration / 2,
13891343
arrival + duration / 2, true);
13901344

1391-
ReservationListResponse response = null;
1392-
try {
1393-
response = client.listReservations(request);
1394-
} catch (Exception e) {
1395-
Assert.fail(e.getMessage());
1396-
}
1345+
ReservationListResponse response = client.listReservations(request);
13971346
Assert.assertNotNull(response);
13981347
Assert.assertEquals(1, response.getReservationAllocationState().size());
13991348
Assert.assertEquals(response.getReservationAllocationState().get(0)
@@ -1402,12 +1351,7 @@ public void testListReservationsByTimeInterval() throws Exception {
14021351
request = ReservationListRequest.newInstance(
14031352
ReservationSystemTestUtil.reservationQ, "", 1, Long.MAX_VALUE, true);
14041353

1405-
response = null;
1406-
try {
1407-
response = client.listReservations(request);
1408-
} catch (Exception e) {
1409-
Assert.fail(e.getMessage());
1410-
}
1354+
response = client.listReservations(request);
14111355
Assert.assertNotNull(response);
14121356
Assert.assertEquals(1, response.getReservationAllocationState().size());
14131357
Assert.assertEquals(response.getReservationAllocationState().get(0)
@@ -1449,12 +1393,7 @@ public void testListReservationsByInvalidTimeInterval() throws Exception {
14491393
ReservationListRequest request = ReservationListRequest
14501394
.newInstance(ReservationSystemTestUtil.reservationQ, "", 1, -1, true);
14511395

1452-
ReservationListResponse response = null;
1453-
try {
1454-
response = client.listReservations(request);
1455-
} catch (Exception e) {
1456-
Assert.fail(e.getMessage());
1457-
}
1396+
ReservationListResponse response = client.listReservations(request);
14581397
Assert.assertNotNull(response);
14591398
Assert.assertEquals(1, response.getReservationAllocationState().size());
14601399
Assert.assertEquals(response.getReservationAllocationState().get(0)
@@ -1464,12 +1403,7 @@ public void testListReservationsByInvalidTimeInterval() throws Exception {
14641403
request = ReservationListRequest.newInstance(
14651404
ReservationSystemTestUtil.reservationQ, "", 1, -10, true);
14661405

1467-
response = null;
1468-
try {
1469-
response = client.listReservations(request);
1470-
} catch (Exception e) {
1471-
Assert.fail(e.getMessage());
1472-
}
1406+
response = client.listReservations(request);
14731407
Assert.assertNotNull(response);
14741408
Assert.assertEquals(1, response.getReservationAllocationState().size());
14751409
Assert.assertEquals(response.getReservationAllocationState().get(0)
@@ -1501,12 +1435,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations()
15011435
ReservationSystemTestUtil.reservationQ, "", Long.MAX_VALUE, -1,
15021436
false);
15031437

1504-
ReservationListResponse response = null;
1505-
try {
1506-
response = client.listReservations(request);
1507-
} catch (Exception e) {
1508-
Assert.fail(e.getMessage());
1509-
}
1438+
ReservationListResponse response = client.listReservations(request);
15101439

15111440
// Ensure all reservations are filtered out.
15121441
Assert.assertNotNull(response);
@@ -1521,12 +1450,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations()
15211450
ReservationSystemTestUtil.reservationQ, "", deadline + duration,
15221451
deadline + 2 * duration, false);
15231452

1524-
response = null;
1525-
try {
1526-
response = client.listReservations(request);
1527-
} catch (Exception e) {
1528-
Assert.fail(e.getMessage());
1529-
}
1453+
response = client.listReservations(request);
15301454

15311455
// Ensure all reservations are filtered out.
15321456
Assert.assertNotNull(response);
@@ -1539,12 +1463,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations()
15391463
ReservationSystemTestUtil.reservationQ, "", 0, arrival - duration,
15401464
false);
15411465

1542-
response = null;
1543-
try {
1544-
response = client.listReservations(request);
1545-
} catch (Exception e) {
1546-
Assert.fail(e.getMessage());
1547-
}
1466+
response = client.listReservations(request);
15481467

15491468
// Ensure all reservations are filtered out.
15501469
Assert.assertNotNull(response);
@@ -1554,12 +1473,7 @@ public void testListReservationsByTimeIntervalContainingNoReservations()
15541473
request = ReservationListRequest
15551474
.newInstance(ReservationSystemTestUtil.reservationQ, "", 0, 1, false);
15561475

1557-
response = null;
1558-
try {
1559-
response = client.listReservations(request);
1560-
} catch (Exception e) {
1561-
Assert.fail(e.getMessage());
1562-
}
1476+
response = client.listReservations(request);
15631477

15641478
// Ensure all reservations are filtered out.
15651479
Assert.assertNotNull(response);
@@ -1590,12 +1504,7 @@ public void testReservationDelete() throws Exception {
15901504
// Delete the reservation
15911505
ReservationDeleteRequest dRequest =
15921506
ReservationDeleteRequest.newInstance(reservationID);
1593-
ReservationDeleteResponse dResponse = null;
1594-
try {
1595-
dResponse = client.deleteReservation(dRequest);
1596-
} catch (Exception e) {
1597-
Assert.fail(e.getMessage());
1598-
}
1507+
ReservationDeleteResponse dResponse = client.deleteReservation(dRequest);
15991508
Assert.assertNotNull(dResponse);
16001509
System.out.println("Delete reservation response: " + dResponse);
16011510

@@ -1604,12 +1513,7 @@ public void testReservationDelete() throws Exception {
16041513
ReservationSystemTestUtil.reservationQ, reservationID.toString(), -1,
16051514
-1, false);
16061515

1607-
ReservationListResponse response = null;
1608-
try {
1609-
response = client.listReservations(request);
1610-
} catch (Exception e) {
1611-
Assert.fail(e.getMessage());
1612-
}
1516+
ReservationListResponse response = client.listReservations(request);
16131517
Assert.assertNotNull(response);
16141518
Assert.assertEquals(0, response.getReservationAllocationState().size());
16151519
} finally {

0 commit comments

Comments
 (0)