From 251f528814c4a4647cac0af6effb9a73135db180 Mon Sep 17 00:00:00 2001 From: Weiwei Yang Date: Wed, 2 May 2018 17:54:46 +0800 Subject: [PATCH] YARN-8222. Fix potential NPE when gets RMApp from RM context. Contributed by Tao Yang. --- .../rmcontainer/RMContainerImpl.java | 30 ++++++++++--------- .../SchedulerApplicationAttempt.java | 13 ++++---- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmcontainer/RMContainerImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmcontainer/RMContainerImpl.java index 541621b5b97..b5c8e7cb8e9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmcontainer/RMContainerImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmcontainer/RMContainerImpl.java @@ -44,6 +44,7 @@ import org.apache.hadoop.yarn.event.EventHandler; import org.apache.hadoop.yarn.server.api.protocolrecords.NMContainerStatus; import org.apache.hadoop.yarn.server.resourcemanager.RMContext; import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp; import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppRunningOnNodeEvent; import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttempt; import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptEvent; @@ -737,21 +738,22 @@ public class RMContainerImpl implements RMContainer { private static void updateAttemptMetrics(RMContainerImpl container) { Resource resource = container.getContainer().getResource(); - RMAppAttempt rmAttempt = container.rmContext.getRMApps() - .get(container.getApplicationAttemptId().getApplicationId()) - .getCurrentAppAttempt(); - - if (rmAttempt != null) { - long usedMillis = container.finishTime - container.creationTime; - rmAttempt.getRMAppAttemptMetrics() - .updateAggregateAppResourceUsage(resource, usedMillis); - // If this is a preempted container, update preemption metrics - if (ContainerExitStatus.PREEMPTED == container.finishedStatus - .getExitStatus()) { + RMApp app = container.rmContext.getRMApps() + .get(container.getApplicationAttemptId().getApplicationId()); + if (app != null) { + RMAppAttempt rmAttempt = app.getCurrentAppAttempt(); + if (rmAttempt != null) { + long usedMillis = container.finishTime - container.creationTime; rmAttempt.getRMAppAttemptMetrics() - .updatePreemptionInfo(resource, container); - rmAttempt.getRMAppAttemptMetrics() - .updateAggregatePreemptedAppResourceUsage(resource, usedMillis); + .updateAggregateAppResourceUsage(resource, usedMillis); + // If this is a preempted container, update preemption metrics + if (ContainerExitStatus.PREEMPTED == container.finishedStatus + .getExitStatus()) { + rmAttempt.getRMAppAttemptMetrics() + .updatePreemptionInfo(resource, container); + rmAttempt.getRMAppAttemptMetrics() + .updateAggregatePreemptedAppResourceUsage(resource, usedMillis); + } } } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java index 857e7369803..005569cf252 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java @@ -1241,12 +1241,13 @@ public class SchedulerApplicationAttempt implements SchedulableEntity { return; } - RMAppAttempt attempt = - rmContext.getRMApps().get(attemptId.getApplicationId()) - .getCurrentAppAttempt(); - if (attempt != null) { - attempt.getRMAppAttemptMetrics().incNumAllocatedContainers(containerType, - requestType); + RMApp app = rmContext.getRMApps().get(attemptId.getApplicationId()); + if (app != null) { + RMAppAttempt attempt = app.getCurrentAppAttempt(); + if (attempt != null) { + attempt.getRMAppAttemptMetrics() + .incNumAllocatedContainers(containerType, requestType); + } } }