YARN-8222. Fix potential NPE when gets RMApp from RM context. Contributed by Tao Yang.

This commit is contained in:
Weiwei Yang 2018-05-02 17:54:46 +08:00
parent 2741af505f
commit 6fce88765a
2 changed files with 23 additions and 20 deletions

View File

@ -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.api.protocolrecords.NMContainerStatus;
import org.apache.hadoop.yarn.server.resourcemanager.RMContext; import org.apache.hadoop.yarn.server.resourcemanager.RMContext;
import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; 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.RMAppRunningOnNodeEvent;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttempt; import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttempt;
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptEvent; 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) { private static void updateAttemptMetrics(RMContainerImpl container) {
Resource resource = container.getContainer().getResource(); Resource resource = container.getContainer().getResource();
RMAppAttempt rmAttempt = container.rmContext.getRMApps() RMApp app = container.rmContext.getRMApps()
.get(container.getApplicationAttemptId().getApplicationId()) .get(container.getApplicationAttemptId().getApplicationId());
.getCurrentAppAttempt(); if (app != null) {
RMAppAttempt rmAttempt = app.getCurrentAppAttempt();
if (rmAttempt != null) { if (rmAttempt != null) {
long usedMillis = container.finishTime - container.creationTime; 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()) {
rmAttempt.getRMAppAttemptMetrics() rmAttempt.getRMAppAttemptMetrics()
.updatePreemptionInfo(resource, container); .updateAggregateAppResourceUsage(resource, usedMillis);
rmAttempt.getRMAppAttemptMetrics() // If this is a preempted container, update preemption metrics
.updateAggregatePreemptedAppResourceUsage(resource, usedMillis); if (ContainerExitStatus.PREEMPTED == container.finishedStatus
.getExitStatus()) {
rmAttempt.getRMAppAttemptMetrics()
.updatePreemptionInfo(resource, container);
rmAttempt.getRMAppAttemptMetrics()
.updateAggregatePreemptedAppResourceUsage(resource, usedMillis);
}
} }
} }
} }

View File

@ -1241,12 +1241,13 @@ public class SchedulerApplicationAttempt implements SchedulableEntity {
return; return;
} }
RMAppAttempt attempt = RMApp app = rmContext.getRMApps().get(attemptId.getApplicationId());
rmContext.getRMApps().get(attemptId.getApplicationId()) if (app != null) {
.getCurrentAppAttempt(); RMAppAttempt attempt = app.getCurrentAppAttempt();
if (attempt != null) { if (attempt != null) {
attempt.getRMAppAttemptMetrics().incNumAllocatedContainers(containerType, attempt.getRMAppAttemptMetrics()
requestType); .incNumAllocatedContainers(containerType, requestType);
}
} }
} }