diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceOption.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceOption.java index e9de05227ec..add44a39103 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceOption.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceOption.java @@ -55,12 +55,16 @@ public static ResourceOption newInstance(Resource resource, * Get timeout for tolerant of resource over-commitment * Note: negative value means no timeout so that allocated containers will * keep running until the end even under resource over-commitment cases. - * @return overCommitTimeout of the ResourceOption + * @return overCommitTimeout of the ResourceOption in milliseconds */ @Private @Evolving public abstract int getOverCommitTimeout(); - + + /** + * Set the over commit timeout. + * @param overCommitTimeout Timeout in ms. Negative means no timeout. + */ @Private @Evolving protected abstract void setOverCommitTimeout(int overCommitTimeout); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceTrackerService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceTrackerService.java index c50950bbd9a..e82cd3cff15 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceTrackerService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceTrackerService.java @@ -677,6 +677,11 @@ public NodeHeartbeatResponse nodeHeartbeat(NodeHeartbeatRequest request) if (capability != null) { nodeHeartBeatResponse.setResource(capability); } + // Check if we got an event (AdminService) that updated the resources + if (rmNode.isUpdatedCapability()) { + nodeHeartBeatResponse.setResource(rmNode.getTotalCapability()); + rmNode.resetUpdatedCapability(); + } // 7. Send Container Queuing Limits back to the Node. This will be used by // the node to truncate the number of Containers queued for execution. diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNode.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNode.java index c77d29c89ae..857048bd987 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNode.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNode.java @@ -104,6 +104,17 @@ */ public Resource getTotalCapability(); + /** + * If the total available resources has been updated. + * @return If the capability has been updated. + */ + public boolean isUpdatedCapability(); + + /** + * Mark that the updated event has been processed. + */ + public void resetUpdatedCapability(); + /** * the aggregated resource utilization of the containers. * @return the aggregated resource utilization of the containers. diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNodeImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNodeImpl.java index d33ee44de4d..fc220363b4c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNodeImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/rmnode/RMNodeImpl.java @@ -125,6 +125,7 @@ /* Snapshot of total resources before receiving decommissioning command */ private volatile Resource originalTotalCapability; private volatile Resource totalCapability; + private volatile boolean updatedCapability = false; private final Node node; private String healthReport; @@ -455,6 +456,16 @@ public Resource getTotalCapability() { return this.totalCapability; } + @Override + public boolean isUpdatedCapability() { + return this.updatedCapability; + } + + @Override + public void resetUpdatedCapability() { + this.updatedCapability = false; + } + @Override public String getRackName() { return node.getNetworkLocation(); @@ -818,6 +829,7 @@ private static void updateNodeResourceFromEvent(RMNodeImpl rmNode, ResourceOption resourceOption = event.getResourceOption(); // Set resource on RMNode rmNode.totalCapability = resourceOption.getResource(); + rmNode.updatedCapability = true; } private static NodeHealthStatus updateRMNodeFromStatusEvents( 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/AbstractYarnScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java index a798b97af5f..4ab9574393f 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java @@ -92,10 +92,9 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.activities.ActivitiesManager; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.ContainerRequest; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.common.QueueEntitlement; - - - +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.event.ContainerPreemptEvent; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.event.ReleaseContainerEvent; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.event.SchedulerEventType; import org.apache.hadoop.yarn.server.scheduler.OpportunisticContainerContext; import org.apache.hadoop.yarn.server.scheduler.SchedulerRequestKey; import org.apache.hadoop.yarn.server.utils.BuilderUtils; @@ -830,6 +829,7 @@ public void updateNodeResource(RMNode nm, writeLock.lock(); SchedulerNode node = getSchedulerNode(nm.getNodeID()); Resource newResource = resourceOption.getResource(); + final int timeout = resourceOption.getOverCommitTimeout(); Resource oldResource = node.getTotalResource(); if (!oldResource.equals(newResource)) { // Notify NodeLabelsManager about this change @@ -838,14 +838,16 @@ public void updateNodeResource(RMNode nm, // Log resource change LOG.info("Update resource on node: " + node.getNodeName() + " from: " - + oldResource + ", to: " + newResource); + + oldResource + ", to: " + newResource + " in " + timeout + " ms"); nodeTracker.removeNode(nm.getNodeID()); // update resource to node node.updateTotalResource(newResource); + node.setOvercommitTimeout(timeout); nodeTracker.addNode((N) node); + } else{ // Log resource change LOG.warn("Update resource on node: " + node.getNodeName() @@ -1188,6 +1190,10 @@ protected void nodeUpdate(RMNode nm) { updateNodeResourceUtilization(nm, schedulerNode); } + if (schedulerNode != null) { + killContainersIfOvercommitted(schedulerNode); + } + // Now node data structures are up-to-date and ready for scheduling. if(LOG.isDebugEnabled()) { LOG.debug( @@ -1197,6 +1203,34 @@ protected void nodeUpdate(RMNode nm) { } } + /** + * Check if the node is over committed and needs to kill containers. + * @param schedulerNode The node to check whether is over committed. + */ + private void killContainersIfOvercommitted(SchedulerNode schedulerNode) { + if (schedulerNode.isOvercommitted()) { + LOG.debug(schedulerNode.getNodeID() + " is over committed, free up resources"); + List containers = + schedulerNode.getRunningContainersToKill(); + Resource unallocated = Resource.newInstance( + schedulerNode.getUnallocatedResource()); + final Resource ZERO_RESOURCE = Resource.newInstance(0, 0); + for (RMContainer container : containers) { + if (Resources.fitsIn(ZERO_RESOURCE, unallocated)) { + LOG.debug("Enough free resources " + unallocated); + break; + } + Resources.addTo(unallocated, container.getAllocatedResource()); + LOG.info("Kill " + container.getContainerId() + " to free up " + + container.getAllocatedResource()); + ApplicationAttemptId appAttemptId = container.getApplicationAttemptId(); + this.rmContext.getDispatcher().getEventHandler().handle( + new ContainerPreemptEvent(appAttemptId, container, + SchedulerEventType.MARK_CONTAINER_FOR_KILLABLE)); + } + } + } + @Override public Resource getNormalizedResource(Resource requestedResource, Resource maxResourceCapability) { 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/SchedulerNode.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerNode.java index b35aeba83b3..cc0c4659806 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerNode.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerNode.java @@ -19,6 +19,8 @@ package org.apache.hadoop.yarn.server.resourcemanager.scheduler; import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; import java.util.LinkedList; import java.util.HashMap; import java.util.List; @@ -69,6 +71,8 @@ ResourceUtilization.newInstance(0, 0, 0f); private volatile ResourceUtilization nodeUtilization = ResourceUtilization.newInstance(0, 0, 0f); + /** Time stamp for over committed resources to time out. */ + private long overcommitTimeout = -1; /* set of containers that are allocated containers */ private final Map launchedContainers = @@ -118,6 +122,37 @@ public synchronized void updateTotalResource(Resource resource){ this.allocatedResource); } + /** + * Set the timeout for the node to stop over committing the resources. After + * this time the scheduler will start killing containers until the resources + * are not over committed anymore. This may reset a previous timeout. + * + * @param timeout Timeout in milliseconds. + */ + public synchronized void setOvercommitTimeout(long timeout) { + if (timeout > 0) { + if (this.overcommitTimeout != -1) { + LOG.debug("The over commit timeout for " + getNodeID() + + " was already set to " + this.overcommitTimeout); + } + this.overcommitTimeout = Time.now() + timeout; + } + } + + /** + * Check if the node is over committed. It check the time out to consider the + * node over committed and the amount of unallocated resources. + * + * @return If the node is over committed. + */ + public synchronized boolean isOvercommitted() { + if (this.overcommitTimeout == -1 || Time.now() < this.overcommitTimeout) { + return false; + } + final Resource ZERO_RESOURCE = Resource.newInstance(0, 0); + return !Resources.fitsIn(ZERO_RESOURCE, this.unallocatedResource); + } + /** * Get the ID of the node which contains both its hostname and port. * @return The ID of the node. @@ -371,6 +406,29 @@ public int getNumContainers() { return result; } + /** + * Get the containers running on the node ordered by which to kill first. + * @return A copy of the running containers ordered by which to kill first. + */ + public synchronized List getRunningContainersToKill() { + List result = new ArrayList<>(); + for (ContainerInfo info : launchedContainers.values()) { + result.add(info.container); + } + Collections.sort(result, (c1, c2) -> { + int cmp = Boolean.compare(c1.isAMContainer(), c2.isAMContainer()); + if (cmp != 0) { + return cmp; + } + cmp = c1.getExecutionType().compareTo(c2.getExecutionType()); + if (cmp != 0) { + return cmp; + } + return Long.compare(c1.getCreationTime(), c1.getCreationTime()); + }); + return result; + } + /** * Get the container for the specified container ID. * @param containerId The container ID diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockNodes.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockNodes.java index c0af0413a0f..3b72ca1c0e2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockNodes.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockNodes.java @@ -190,6 +190,15 @@ public Resource getTotalCapability() { return this.perNode; } + @Override + public boolean isUpdatedCapability() { + return false; + } + + @Override + public void resetUpdatedCapability() { + } + @Override public String getRackName() { return this.rackName; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestCapacityScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestCapacityScheduler.java index aac7f15a5a5..31d04b86e4b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestCapacityScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/TestCapacityScheduler.java @@ -45,8 +45,6 @@ import java.util.concurrent.CyclicBarrier; import com.google.common.collect.Sets; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.net.NetworkTopology; @@ -57,6 +55,7 @@ import org.apache.hadoop.security.token.Token; import org.apache.hadoop.security.token.TokenIdentifier; import org.apache.hadoop.test.GenericTestUtils; +import org.apache.hadoop.util.Time; import org.apache.hadoop.yarn.LocalConfigurationProvider; import org.apache.hadoop.yarn.api.ApplicationMasterProtocol; import org.apache.hadoop.yarn.api.protocolrecords.AllocateRequest; @@ -68,6 +67,7 @@ import org.apache.hadoop.yarn.api.records.ApplicationResourceUsageReport; import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext; import org.apache.hadoop.yarn.api.records.Container; +import org.apache.hadoop.yarn.api.records.ContainerExitStatus; import org.apache.hadoop.yarn.api.records.ContainerId; import org.apache.hadoop.yarn.api.records.ContainerState; import org.apache.hadoop.yarn.api.records.ContainerStatus; @@ -178,9 +178,12 @@ import com.google.common.collect.ImmutableSet; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TestCapacityScheduler extends CapacitySchedulerTestBase { - private static final Log LOG = LogFactory.getLog(TestCapacityScheduler.class); + private static final Logger LOG = + LoggerFactory.getLogger(TestCapacityScheduler.class); private final static ContainerUpdates NULL_UPDATE_REQUESTS = new ContainerUpdates(); private ResourceManager resourceManager = null; @@ -1309,110 +1312,173 @@ public void testAllocateReorder() throws Exception { @Test public void testResourceOverCommit() throws Exception { - int waitCount; Configuration conf = new Configuration(); conf.setClass(YarnConfiguration.RM_SCHEDULER, CapacityScheduler.class, ResourceScheduler.class); + @SuppressWarnings("resource") MockRM rm = new MockRM(conf); rm.start(); + ResourceScheduler scheduler = rm.getResourceScheduler(); + AdminService admin = rm.getAdminService(); - MockNM nm1 = rm.registerNode("127.0.0.1:1234", 4 * GB); - RMApp app1 = rm.submitApp(2048); + MockNM nm = rm.registerNode("127.0.0.1:1234", 4 * GB); + NodeId nmId = nm.getNodeId(); + RMApp app = rm.submitApp(2048); // kick the scheduling, 2 GB given to AM1, remaining 2GB on nm1 - nm1.nodeHeartbeat(true); - RMAppAttempt attempt1 = app1.getCurrentAppAttempt(); - MockAM am1 = rm.sendAMLaunched(attempt1.getAppAttemptId()); - am1.registerAppAttempt(); - SchedulerNodeReport report_nm1 = rm.getResourceScheduler().getNodeReport( - nm1.getNodeId()); + nm.nodeHeartbeat(true); + RMAppAttempt attempt1 = app.getCurrentAppAttempt(); + MockAM am = rm.sendAMLaunched(attempt1.getAppAttemptId()); + am.registerAppAttempt(); + SchedulerNodeReport nmReport = scheduler.getNodeReport(nmId); // check node report, 2 GB used and 2 GB available - Assert.assertEquals(2 * GB, report_nm1.getUsedResource().getMemorySize()); - Assert.assertEquals(2 * GB, report_nm1.getAvailableResource().getMemorySize()); + assertEquals(2 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(2 * GB, nmReport.getAvailableResource().getMemorySize()); - // add request for containers - am1.addRequests(new String[] { "127.0.0.1", "127.0.0.2" }, 2 * GB, 1, 1); - AllocateResponse alloc1Response = am1.schedule(); // send the request + // add request for 1 container of 2 GB + am.addRequests(new String[] { "127.0.0.1", "127.0.0.2" }, 2 * GB, 1, 1); + AllocateResponse alloc1Response = am.schedule(); // send the request // kick the scheduler, 2 GB given to AM1, resource remaining 0 - nm1.nodeHeartbeat(true); - while (alloc1Response.getAllocatedContainers().size() < 1) { + nm.nodeHeartbeat(true); + while (alloc1Response.getAllocatedContainers().isEmpty()) { LOG.info("Waiting for containers to be created for app 1..."); Thread.sleep(100); - alloc1Response = am1.schedule(); + alloc1Response = am.schedule(); } List allocated1 = alloc1Response.getAllocatedContainers(); - Assert.assertEquals(1, allocated1.size()); - Assert.assertEquals(2 * GB, allocated1.get(0).getResource().getMemorySize()); - Assert.assertEquals(nm1.getNodeId(), allocated1.get(0).getNodeId()); - - report_nm1 = rm.getResourceScheduler().getNodeReport(nm1.getNodeId()); - // check node report, 4 GB used and 0 GB available - Assert.assertEquals(0, report_nm1.getAvailableResource().getMemorySize()); - Assert.assertEquals(4 * GB, report_nm1.getUsedResource().getMemorySize()); - - // check container is assigned with 2 GB. + assertEquals(1, allocated1.size()); Container c1 = allocated1.get(0); - Assert.assertEquals(2 * GB, c1.getResource().getMemorySize()); - - // update node resource to 2 GB, so resource is over-consumed. - Map nodeResourceMap = - new HashMap(); - nodeResourceMap.put(nm1.getNodeId(), - ResourceOption.newInstance(Resource.newInstance(2 * GB, 1), -1)); - UpdateNodeResourceRequest request = - UpdateNodeResourceRequest.newInstance(nodeResourceMap); - AdminService as = ((MockRM)rm).getAdminService(); - as.updateNodeResource(request); - - waitCount = 0; - while (waitCount++ != 20) { - report_nm1 = rm.getResourceScheduler().getNodeReport(nm1.getNodeId()); - if (report_nm1.getAvailableResource().getMemorySize() != 0) { - break; - } - LOG.info("Waiting for RMNodeResourceUpdateEvent to be handled... Tried " - + waitCount + " times already.."); - Thread.sleep(1000); - } - // Now, the used resource is still 4 GB, and available resource is minus value. - report_nm1 = rm.getResourceScheduler().getNodeReport(nm1.getNodeId()); - Assert.assertEquals(4 * GB, report_nm1.getUsedResource().getMemorySize()); - Assert.assertEquals(-2 * GB, report_nm1.getAvailableResource().getMemorySize()); + assertEquals(2 * GB, c1.getResource().getMemorySize()); + assertEquals(nmId, c1.getNodeId()); - // Check container can complete successfully in case of resource over-commitment. + nmReport = scheduler.getNodeReport(nmId); + // check node report, 4 GB used and 0 GB available + assertEquals(0, nmReport.getAvailableResource().getMemorySize()); + assertEquals(4 * GB, nmReport.getUsedResource().getMemorySize()); + nm.nodeHeartbeat(true); + assertEquals(4 * GB, nm.getCapability().getMemorySize()); + + // update node resource to 2 GB, so resource is over-consumed + admin.updateNodeResource(UpdateNodeResourceRequest.newInstance( + Collections.singletonMap(nmId, ResourceOption.newInstance( + Resource.newInstance(2 * GB, 1), -1)))); + + LOG.info("Waiting for RMNodeResourceUpdateEvent to be handled..."); + GenericTestUtils.waitFor(() -> { + SchedulerNodeReport report = scheduler.getNodeReport(nmId); + return report.getAvailableResource().getMemorySize() != 0; + }, 100, 10 * 1000); + + // the used resource should still 4 GB and available resource is negative + nmReport = scheduler.getNodeReport(nmId); + assertEquals(4 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(-2 * GB, nmReport.getAvailableResource().getMemorySize()); + nm.nodeHeartbeat(true); + assertEquals(2 * GB, nm.getCapability().getMemorySize()); + + // check container can complete successfully with resource over-commitment ContainerStatus containerStatus = BuilderUtils.newContainerStatus( c1.getId(), ContainerState.COMPLETE, "", 0, c1.getResource()); - nm1.containerStatus(containerStatus); - waitCount = 0; - while (attempt1.getJustFinishedContainers().size() < 1 - && waitCount++ != 20) { - LOG.info("Waiting for containers to be finished for app 1... Tried " - + waitCount + " times already.."); + nm.containerStatus(containerStatus); + + LOG.info("Waiting for containers to be finished for app 1..."); + GenericTestUtils.waitFor( + () -> attempt1.getJustFinishedContainers().size() == 1, 100, 2000); + assertEquals(1, am.schedule().getCompletedContainersStatuses().size()); + nmReport = scheduler.getNodeReport(nmId); + assertEquals(2 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(0 * GB, nmReport.getAvailableResource().getMemorySize()); + + // verify no NPE is trigger in schedule after resource is updated + am.addRequests(new String[] { "127.0.0.1", "127.0.0.2" }, 3 * GB, 1, 1); + AllocateResponse allocResponse2 = am.schedule(); + assertTrue("Shouldn't have enough resource to allocate containers", + allocResponse2.getAllocatedContainers().isEmpty()); + // try 10 times as scheduling is an async process + for (int i = 0; i < 10; i++) { + LOG.info("Waiting for containers to be allocated for app 1... Tried " + + "{} times already..", i); Thread.sleep(100); + allocResponse2 = am.schedule(); + assertTrue("Shouldn't have enough resource to allocate containers", + allocResponse2.getAllocatedContainers().isEmpty()); } - Assert.assertEquals(1, attempt1.getJustFinishedContainers().size()); - Assert.assertEquals(1, am1.schedule().getCompletedContainersStatuses().size()); - report_nm1 = rm.getResourceScheduler().getNodeReport(nm1.getNodeId()); - Assert.assertEquals(2 * GB, report_nm1.getUsedResource().getMemorySize()); - // As container return 2 GB back, the available resource becomes 0 again. - Assert.assertEquals(0 * GB, report_nm1.getAvailableResource().getMemorySize()); - - // Verify no NPE is trigger in schedule after resource is updated. - am1.addRequests(new String[] { "127.0.0.1", "127.0.0.2" }, 3 * GB, 1, 1); - alloc1Response = am1.schedule(); - Assert.assertEquals("Shouldn't have enough resource to allocate containers", - 0, alloc1Response.getAllocatedContainers().size()); - int times = 0; - // try 10 times as scheduling is async process. - while (alloc1Response.getAllocatedContainers().size() < 1 - && times++ < 10) { - LOG.info("Waiting for containers to be allocated for app 1... Tried " - + times + " times already.."); + + // increase the resources again to 5 GB to schedule the 3GB container + admin.updateNodeResource(UpdateNodeResourceRequest.newInstance( + Collections.singletonMap(nmId, ResourceOption.newInstance( + Resource.newInstance(5 * GB, 1), -1)))); + GenericTestUtils.waitFor(() -> { + SchedulerNodeReport report = scheduler.getNodeReport(nmId); + return report.getAvailableResource().getMemorySize() > 0; + }, 100, 5 * 1000); + nmReport = scheduler.getNodeReport(nmId); + assertEquals(2 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(3 * GB, nmReport.getAvailableResource().getMemorySize()); + + // kick the scheduling and check it took effect + nm.nodeHeartbeat(true); + while (allocResponse2.getAllocatedContainers().isEmpty()) { + LOG.info("Waiting for containers to be created for app 1..."); Thread.sleep(100); + allocResponse2 = am.schedule(); } - Assert.assertEquals("Shouldn't have enough resource to allocate containers", - 0, alloc1Response.getAllocatedContainers().size()); + assertEquals(1, allocResponse2.getAllocatedContainers().size()); + Container c2 = allocResponse2.getAllocatedContainers().get(0); + assertEquals(3 * GB, c2.getResource().getMemorySize()); + assertEquals(nmId, c2.getNodeId()); + + nmReport = scheduler.getNodeReport(nmId); + assertEquals(5 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(0 * GB, nmReport.getAvailableResource().getMemorySize()); + + // reduce the resources and wait for the container to be preempted + long t0 = Time.now(); + admin.updateNodeResource(UpdateNodeResourceRequest.newInstance( + Collections.singletonMap(nmId, ResourceOption.newInstance( + Resource.newInstance(3 * GB, 1), 2000)))); + // the container should be running for a couple seconds until timing out + GenericTestUtils.waitFor(() -> { + SchedulerNodeReport report = scheduler.getNodeReport(nmId); + if (report == null) { + return false; + } + Resource avail = report.getAvailableResource(); + return avail.getMemorySize() < 0; + }, 200, 5 * 1000); + nmReport = scheduler.getNodeReport(nmId); + assertEquals(5 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(-2 * GB, nmReport.getAvailableResource().getMemorySize()); + + // wait until the scheduler preempts the container + GenericTestUtils.waitFor(() -> { + try { + nm.nodeHeartbeat(true); // trigger preemption in the NM + } catch (Exception e) { + LOG.error("Cannot heartbeat", e); + } + SchedulerNodeReport report = scheduler.getNodeReport(nmId); + return report.getAvailableResource().getMemorySize() > 0; + }, 200, 5 * 1000); + + nmReport = scheduler.getNodeReport(nmId); + assertEquals(2 * GB, nmReport.getUsedResource().getMemorySize()); + assertEquals(1 * GB, nmReport.getAvailableResource().getMemorySize()); + AllocateResponse allocResponse3 = am.schedule(); + List completedContainers = + allocResponse3.getCompletedContainersStatuses(); + assertEquals(1, completedContainers.size()); + ContainerStatus c2status = completedContainers.get(0); + assertEquals(c2.getId(), c2status.getContainerId()); + assertEquals(ContainerState.COMPLETE, c2status.getState()); + assertEquals(ContainerExitStatus.PREEMPTED, c2status.getExitStatus()); + assertEquals("Container preempted by scheduler", c2status.getDiagnostics()); + + long timeToKill = Time.now() - t0; + assertTrue("Took too short to kill: " + timeToKill, timeToKill > 2000); + assertTrue("Took too long to kill: " + timeToKill, timeToKill < 2500); + rm.stop(); }