|
49 | 49 | import org.apache.tez.common.counters.TezCounters; |
50 | 50 | import org.apache.tez.dag.api.TezConstants; |
51 | 51 | import org.apache.tez.dag.app.dag.event.DAGEventTerminateDag; |
| 52 | +import org.apache.tez.dag.app.rm.TaskSchedulerManager; |
52 | 53 | import org.apache.tez.hadoop.shim.DefaultHadoopShim; |
53 | 54 | import org.apache.tez.hadoop.shim.HadoopShim; |
54 | 55 | import org.junit.Rule; |
|
60 | 61 | import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; |
61 | 62 | import org.apache.hadoop.yarn.api.records.ApplicationId; |
62 | 63 | import org.apache.hadoop.yarn.api.records.ContainerId; |
| 64 | +import org.apache.hadoop.yarn.api.records.Container; |
| 65 | +import org.apache.hadoop.yarn.api.records.NodeId; |
63 | 66 | import org.apache.hadoop.yarn.api.records.Resource; |
64 | 67 | import org.apache.hadoop.yarn.event.EventHandler; |
65 | 68 | import org.apache.hadoop.yarn.util.Clock; |
@@ -181,6 +184,7 @@ public class TestDAGImpl { |
181 | 184 | private ACLManager aclManager; |
182 | 185 | private ApplicationAttemptId appAttemptId; |
183 | 186 | private DAGImpl dag; |
| 187 | + private TaskSchedulerManager taskSchedulerManager; |
184 | 188 | private TaskEventDispatcher taskEventDispatcher; |
185 | 189 | private VertexEventDispatcher vertexEventDispatcher; |
186 | 190 | private DagEventDispatcher dagEventDispatcher; |
@@ -861,11 +865,12 @@ public void setup() { |
861 | 865 | dispatcher = new DrainDispatcher(); |
862 | 866 | fsTokens = new Credentials(); |
863 | 867 | appContext = mock(AppContext.class); |
| 868 | + taskSchedulerManager = mock(TaskSchedulerManager.class); |
864 | 869 | execService = mock(ListeningExecutorService.class); |
865 | 870 | final ListenableFuture<Void> mockFuture = mock(ListenableFuture.class); |
866 | 871 | when(appContext.getHadoopShim()).thenReturn(defaultShim); |
867 | 872 | when(appContext.getApplicationID()).thenReturn(appAttemptId.getApplicationId()); |
868 | | - |
| 873 | + |
869 | 874 | doAnswer(new Answer() { |
870 | 875 | public ListenableFuture<Void> answer(InvocationOnMock invocation) { |
871 | 876 | Object[] args = invocation.getArguments(); |
@@ -2358,22 +2363,82 @@ public void testCounterLimits() { |
2358 | 2363 |
|
2359 | 2364 | } |
2360 | 2365 |
|
2361 | | - @SuppressWarnings("unchecked") |
2362 | 2366 | @Test(timeout = 5000) |
2363 | 2367 | public void testTotalContainersUsedCounter() { |
| 2368 | + DAGImpl spy = getDagSpy(); |
| 2369 | + |
| 2370 | + spy.addUsedContainer(Container.newInstance(ContainerId.fromString("container_e16_1504924099862_7571_01_000005"), |
| 2371 | + mock(NodeId.class), null, null, null, null)); |
| 2372 | + spy.addUsedContainer(Container.newInstance(ContainerId.fromString("container_e16_1504924099862_7571_01_000006"), |
| 2373 | + mock(NodeId.class), null, null, null, null)); |
| 2374 | + |
| 2375 | + spy.onFinish(); |
| 2376 | + // 2 calls to addUsedContainer |
| 2377 | + verify(spy, times(2)).addUsedContainer(any(Container.class)); |
| 2378 | + // 2 containers were used |
| 2379 | + Assert.assertEquals(2, |
| 2380 | + spy.getAllCounters().getGroup(DAGCounter.class.getName()).findCounter(DAGCounter.TOTAL_CONTAINERS_USED.name()) |
| 2381 | + .getValue()); |
| 2382 | + } |
| 2383 | + |
| 2384 | + @Test(timeout = 5000) |
| 2385 | + public void testNodesUsedCounter() { |
| 2386 | + DAGImpl spy = getDagSpy(); |
| 2387 | + |
| 2388 | + Container containerOnHost = mock(Container.class); |
| 2389 | + when(containerOnHost.getNodeId()).thenReturn(NodeId.fromString("localhost:0")); |
| 2390 | + Container containerOnSameHost = mock(Container.class); |
| 2391 | + when(containerOnSameHost.getNodeId()).thenReturn(NodeId.fromString("localhost:0")); |
| 2392 | + Container containerOnDifferentHost = mock(Container.class); |
| 2393 | + when(containerOnDifferentHost.getNodeId()).thenReturn(NodeId.fromString("otherhost:0")); |
| 2394 | + Container containerOnSameHostWithDifferentPort = mock(Container.class); |
| 2395 | + when(containerOnSameHostWithDifferentPort.getNodeId()).thenReturn(NodeId.fromString("localhost:1")); |
| 2396 | + |
| 2397 | + spy.addUsedContainer(containerOnHost); |
| 2398 | + spy.addUsedContainer(containerOnSameHost); |
| 2399 | + spy.addUsedContainer(containerOnDifferentHost); |
| 2400 | + spy.addUsedContainer(containerOnSameHostWithDifferentPort); |
| 2401 | + |
| 2402 | + when(taskSchedulerManager.getNumClusterNodes()).thenReturn(10); |
| 2403 | + |
| 2404 | + spy.onFinish(); |
| 2405 | + // 4 calls to addUsedContainer |
| 2406 | + verify(spy, times(4)).addUsedContainer(any(Container.class)); |
| 2407 | + // 3 nodes were used: localhost:0, otherhost:0, localhost:1 |
| 2408 | + // localhost:0 and localhost:1 might be on the same physical host, but as long as |
| 2409 | + // yarn considers them different nodes, we consider them different too |
| 2410 | + Assert.assertEquals(3, |
| 2411 | + spy.getAllCounters().getGroup(DAGCounter.class.getName()).findCounter(DAGCounter.NODE_USED_COUNT.name()) |
| 2412 | + .getValue()); |
| 2413 | + |
| 2414 | + Assert.assertTrue(spy.nodesUsedByCurrentDAG.contains(NodeId.fromString("localhost:0"))); |
| 2415 | + Assert.assertTrue(spy.nodesUsedByCurrentDAG.contains(NodeId.fromString("otherhost:0"))); |
| 2416 | + Assert.assertTrue(spy.nodesUsedByCurrentDAG.contains(NodeId.fromString("localhost:1"))); |
| 2417 | + |
| 2418 | + // 2 distinct node hosts were seen: localhost, otherhost |
| 2419 | + Assert.assertEquals(2, |
| 2420 | + spy.getAllCounters().getGroup(DAGCounter.class.getName()) |
| 2421 | + .findCounter(DAGCounter.NODE_HOSTS_USED_COUNT.name()) |
| 2422 | + .getValue()); |
| 2423 | + |
| 2424 | + Assert.assertEquals(10, |
| 2425 | + spy.getAllCounters().getGroup(DAGCounter.class.getName()) |
| 2426 | + .findCounter(DAGCounter.NODE_TOTAL_COUNT.name()) |
| 2427 | + .getValue()); |
| 2428 | + |
| 2429 | + Assert.assertTrue(spy.nodeHostsUsedByCurrentDAG.contains("localhost")); |
| 2430 | + Assert.assertTrue(spy.nodeHostsUsedByCurrentDAG.contains("otherhost")); |
| 2431 | + } |
| 2432 | + |
| 2433 | + private DAGImpl getDagSpy() { |
2364 | 2434 | initDAG(mrrDag); |
2365 | 2435 | dispatcher.await(); |
2366 | 2436 | startDAG(mrrDag); |
2367 | 2437 | dispatcher.await(); |
2368 | 2438 |
|
2369 | | - DAGImpl spy = spy(mrrDag); |
2370 | | - spy.addUsedContainer(mock(ContainerId.class)); |
2371 | | - spy.addUsedContainer(mock(ContainerId.class)); |
| 2439 | + // needed when onFinish() method is called on a DAGImpl |
| 2440 | + when(mrrAppContext.getTaskScheduler()).thenReturn(taskSchedulerManager); |
2372 | 2441 |
|
2373 | | - spy.onFinish(); |
2374 | | - // 2 calls to addUsedContainer, obviously, we did it here |
2375 | | - verify(spy, times(2)).addUsedContainer(any(ContainerId.class)); |
2376 | | - // 1 call to setDagCounter, which happened at dag.onFinish |
2377 | | - verify(spy).setDagCounter(DAGCounter.TOTAL_CONTAINERS_USED, 2); |
| 2442 | + return spy(mrrDag); |
2378 | 2443 | } |
2379 | 2444 | } |
0 commit comments