diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainersMonitorImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainersMonitorImpl.java index dda55d24fc..718604d3bc 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainersMonitorImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/ContainersMonitorImpl.java @@ -536,15 +536,21 @@ private void initializeProcessTrees( } Container container = context.getContainers().get(containerId); - String[] ipAndHost = containerExecutor.getIpAndHost(container); - if (ipAndHost != null && ipAndHost[0] != null - && ipAndHost[1] != null) { - container.setIpAndHost(ipAndHost); - LOG.info(containerId + "'s ip = " + ipAndHost[0] - + ", and hostname = " + ipAndHost[1]); + + if (container != null) { + String[] ipAndHost = containerExecutor.getIpAndHost(container); + + if ((ipAndHost != null) && (ipAndHost[0] != null) && + (ipAndHost[1] != null)) { + container.setIpAndHost(ipAndHost); + LOG.info(containerId + "'s ip = " + ipAndHost[0] + + ", and hostname = " + ipAndHost[1]); + } else { + LOG.info("Can not get both ip and hostname: " + + Arrays.toString(ipAndHost)); + } } else { - LOG.info("Can not get both ip and hostname: " + Arrays - .toString(ipAndHost)); + LOG.info(containerId + " is missing. Not setting ip and hostname"); } } } @@ -683,11 +689,15 @@ private void reportResourceUsage(ContainerId containerId, long currentPmemUsage, float cpuUsagePercentPerCore) { ContainerImpl container = (ContainerImpl) context.getContainers().get(containerId); - NMTimelinePublisher nmMetricsPublisher = - container.getNMTimelinePublisher(); - if (nmMetricsPublisher != null) { - nmMetricsPublisher.reportContainerResourceUsage(container, - currentPmemUsage, cpuUsagePercentPerCore); + if (container != null) { + NMTimelinePublisher nmMetricsPublisher = + container.getNMTimelinePublisher(); + if (nmMetricsPublisher != null) { + nmMetricsPublisher.reportContainerResourceUsage(container, + currentPmemUsage, cpuUsagePercentPerCore); + } + } else { + LOG.info(containerId + " does not exist to report"); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/TestContainersMonitorResourceChange.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/TestContainersMonitorResourceChange.java index d24f89db0d..29a60f7ba6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/TestContainersMonitorResourceChange.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/monitor/TestContainersMonitorResourceChange.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentSkipListMap; import org.apache.hadoop.conf.Configuration; @@ -37,6 +38,7 @@ import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container; import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerEvent; import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerEventType; +import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerImpl; import org.apache.hadoop.yarn.server.nodemanager.containermanager.monitor.ContainersMonitorImpl.ProcessTreeInfo; import org.apache.hadoop.yarn.server.nodemanager.executor.ContainerLivenessContext; import org.apache.hadoop.yarn.server.nodemanager.executor.ContainerSignalContext; @@ -64,6 +66,7 @@ public class TestContainersMonitorResourceChange { private AsyncDispatcher dispatcher; private Context context; private MockContainerEventHandler containerEventHandler; + private ConcurrentMap containerMap; static final int WAIT_MS_PER_LOOP = 20; // 20 milli seconds @@ -131,8 +134,10 @@ public void setup() { executor = new MockExecutor(); dispatcher = new AsyncDispatcher(); context = Mockito.mock(Context.class); - Mockito.doReturn(new ConcurrentSkipListMap()) - .when(context).getContainers(); + containerMap = new ConcurrentSkipListMap<>(); + Container container = Mockito.mock(ContainerImpl.class); + containerMap.put(getContainerId(1), container); + Mockito.doReturn(containerMap).when(context).getContainers(); conf = new Configuration(); conf.set( YarnConfiguration.NM_CONTAINER_MON_RESOURCE_CALCULATOR, @@ -181,7 +186,12 @@ public void testContainersResourceChange() throws Exception { getContainerId(1)).getProcessTree(); mockTree.setRssMemorySize(2500L); // verify that this container is killed - Thread.sleep(200); + for (int waitMs = 0; waitMs < 5000; waitMs += 50) { + if (containerEventHandler.isContainerKilled(getContainerId(1))) { + break; + } + Thread.sleep(50); + } assertTrue(containerEventHandler .isContainerKilled(getContainerId(1))); // create container 2