YARN-3993. Changed to use the AM flag in ContainerContext determine AM container in TestPerNodeTimelineCollectorsAuxService. Contributed by Sunil G.

(cherry picked from commit 9e48f9ff2ce08f3dcdd8d60bacb697664b92196f)
This commit is contained in:
Zhijie Shen 2015-08-03 16:55:44 -07:00 committed by Sangjin Lee
parent a9fab9b644
commit 57e2498cd4
2 changed files with 8 additions and 10 deletions

View File

@ -38,6 +38,7 @@ import org.apache.hadoop.yarn.server.api.AuxiliaryService;
import org.apache.hadoop.yarn.server.api.ContainerContext; import org.apache.hadoop.yarn.server.api.ContainerContext;
import org.apache.hadoop.yarn.server.api.ContainerInitializationContext; import org.apache.hadoop.yarn.server.api.ContainerInitializationContext;
import org.apache.hadoop.yarn.server.api.ContainerTerminationContext; import org.apache.hadoop.yarn.server.api.ContainerTerminationContext;
import org.apache.hadoop.yarn.server.api.ContainerType;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
@ -119,7 +120,7 @@ public class PerNodeTimelineCollectorsAuxService extends AuxiliaryService {
public void initializeContainer(ContainerInitializationContext context) { public void initializeContainer(ContainerInitializationContext context) {
// intercept the event of the AM container being created and initialize the // intercept the event of the AM container being created and initialize the
// app level collector service // app level collector service
if (isApplicationMaster(context)) { if (context.getContainerType() == ContainerType.APPLICATION_MASTER) {
ApplicationId appId = context.getContainerId(). ApplicationId appId = context.getContainerId().
getApplicationAttemptId().getApplicationId(); getApplicationAttemptId().getApplicationId();
addApplication(appId); addApplication(appId);
@ -135,21 +136,13 @@ public class PerNodeTimelineCollectorsAuxService extends AuxiliaryService {
public void stopContainer(ContainerTerminationContext context) { public void stopContainer(ContainerTerminationContext context) {
// intercept the event of the AM container being stopped and remove the app // intercept the event of the AM container being stopped and remove the app
// level collector service // level collector service
if (isApplicationMaster(context)) { if (context.getContainerType() == ContainerType.APPLICATION_MASTER) {
ApplicationId appId = context.getContainerId(). ApplicationId appId = context.getContainerId().
getApplicationAttemptId().getApplicationId(); getApplicationAttemptId().getApplicationId();
removeApplication(appId); removeApplication(appId);
} }
} }
private boolean isApplicationMaster(ContainerContext context) {
// TODO this is based on a (shaky) assumption that the container id (the
// last field of the full container id) for an AM is always 1
// we want to make this much more reliable
ContainerId containerId = context.getContainerId();
return containerId.getContainerId() == 1L;
}
@VisibleForTesting @VisibleForTesting
boolean hasApplication(ApplicationId appId) { boolean hasApplication(ApplicationId appId) {
return collectorManager.containsTimelineCollector(appId); return collectorManager.containsTimelineCollector(appId);

View File

@ -39,6 +39,7 @@ import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.hadoop.yarn.server.api.CollectorNodemanagerProtocol; import org.apache.hadoop.yarn.server.api.CollectorNodemanagerProtocol;
import org.apache.hadoop.yarn.server.api.ContainerInitializationContext; import org.apache.hadoop.yarn.server.api.ContainerInitializationContext;
import org.apache.hadoop.yarn.server.api.ContainerTerminationContext; import org.apache.hadoop.yarn.server.api.ContainerTerminationContext;
import org.apache.hadoop.yarn.server.api.ContainerType;
import org.apache.hadoop.yarn.server.api.protocolrecords.GetTimelineCollectorContextRequest; import org.apache.hadoop.yarn.server.api.protocolrecords.GetTimelineCollectorContextRequest;
import org.apache.hadoop.yarn.server.api.protocolrecords.GetTimelineCollectorContextResponse; import org.apache.hadoop.yarn.server.api.protocolrecords.GetTimelineCollectorContextResponse;
import org.junit.After; import org.junit.After;
@ -94,6 +95,8 @@ public class TestPerNodeTimelineCollectorsAuxService {
ContainerTerminationContext context = ContainerTerminationContext context =
mock(ContainerTerminationContext.class); mock(ContainerTerminationContext.class);
when(context.getContainerId()).thenReturn(containerId); when(context.getContainerId()).thenReturn(containerId);
when(context.getContainerType()).thenReturn(
ContainerType.APPLICATION_MASTER);
auxService.stopContainer(context); auxService.stopContainer(context);
// auxService should not have that app // auxService should not have that app
assertFalse(auxService.hasApplication(appAttemptId.getApplicationId())); assertFalse(auxService.hasApplication(appAttemptId.getApplicationId()));
@ -138,6 +141,8 @@ public class TestPerNodeTimelineCollectorsAuxService {
ContainerInitializationContext context = ContainerInitializationContext context =
mock(ContainerInitializationContext.class); mock(ContainerInitializationContext.class);
when(context.getContainerId()).thenReturn(containerId); when(context.getContainerId()).thenReturn(containerId);
when(context.getContainerType()).thenReturn(
ContainerType.APPLICATION_MASTER);
auxService.initializeContainer(context); auxService.initializeContainer(context);
return auxService; return auxService;
} }