From fd6be5898ad1a650e3bceacb8169a53520da57e5 Mon Sep 17 00:00:00 2001 From: Weiwei Yang Date: Sat, 29 Sep 2018 17:47:12 +0800 Subject: [PATCH] =?UTF-8?q?YARN-8468.=20Enable=20the=20use=20of=20queue=20?= =?UTF-8?q?based=20maximum=20container=20allocation=20limit=20and=20implem?= =?UTF-8?q?ent=20it=20in=20FairScheduler.=20Contributed=20by=20Antal=20B?= =?UTF-8?q?=C3=A1lint=20Steinbach.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/resourcemanager/RMAppManager.java | 11 +- .../server/resourcemanager/RMServerUtils.java | 9 +- .../scheduler/AbstractYarnScheduler.java | 21 ++- .../scheduler/SchedulerUtils.java | 62 +++--- .../scheduler/YarnScheduler.java | 9 +- .../scheduler/capacity/CapacityScheduler.java | 7 +- .../PlacementConstraintProcessor.java | 12 +- .../fair/AllocationConfiguration.java | 13 ++ .../scheduler/fair/FSLeafQueue.java | 10 + .../scheduler/fair/FSParentQueue.java | 15 +- .../scheduler/fair/FSQueue.java | 7 + .../scheduler/fair/FairScheduler.java | 52 +++-- .../allocation/AllocationFileQueueParser.java | 7 + .../fair/allocation/QueueProperties.java | 16 +- .../webapp/FairSchedulerPage.java | 4 + .../webapp/dao/FairSchedulerQueueInfo.java | 9 +- .../resourcemanager/AppManagerTestBase.java | 107 +++++++++++ .../yarn/server/resourcemanager/MockRM.java | 8 + .../resourcemanager/TestAppManager.java | 89 ++------- .../TestAppManagerWithFairScheduler.java | 177 ++++++++++++++++++ .../resourcemanager/TestClientRMService.java | 104 +++++----- .../resourcemanager/TestRMServerUtils.java | 102 +++++++++- .../scheduler/TestSchedulerUtils.java | 144 ++++++++------ .../capacity/TestCapacityScheduler.java | 39 +++- .../scheduler/fair/FairSchedulerTestBase.java | 4 +- .../fair/TestAllocationFileLoaderService.java | 37 +++- .../TestApplicationMasterServiceWithFS.java | 174 +++++++++++++++++ .../scheduler/fair/TestFairScheduler.java | 140 ++++++++++++-- .../allocationfile/AllocationFileQueue.java | 6 +- .../AllocationFileQueueBuilder.java | 6 + .../AllocationFileQueueProperties.java | 12 ++ .../src/site/markdown/FairScheduler.md | 2 + 32 files changed, 1117 insertions(+), 298 deletions(-) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/AppManagerTestBase.java create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManagerWithFairScheduler.java create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestApplicationMasterServiceWithFS.java diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMAppManager.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMAppManager.java index d0f2ce626d5..783fab0bc05 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMAppManager.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMAppManager.java @@ -38,6 +38,7 @@ import org.apache.hadoop.yarn.api.records.ExecutionType; import org.apache.hadoop.yarn.api.records.ExecutionTypeRequest; import org.apache.hadoop.yarn.api.records.Priority; import org.apache.hadoop.yarn.api.records.QueueACL; +import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.ResourceRequest; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.event.EventHandler; @@ -567,13 +568,13 @@ public class RMAppManager implements EventHandler, // Normalize all requests String queue = submissionContext.getQueue(); + Resource maxAllocation = scheduler.getMaximumResourceCapability(queue); for (ResourceRequest amReq : amReqs) { - SchedulerUtils.normalizeAndValidateRequest(amReq, - scheduler.getMaximumResourceCapability(queue), - queue, scheduler, isRecovery, rmContext); + SchedulerUtils.normalizeAndValidateRequest(amReq, maxAllocation, + queue, scheduler, isRecovery, rmContext, null); - amReq.setCapability( - scheduler.getNormalizedResource(amReq.getCapability())); + amReq.setCapability(scheduler.getNormalizedResource( + amReq.getCapability(), maxAllocation)); } return amReqs; } catch (InvalidResourceRequestException e) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMServerUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMServerUtils.java index ab6bbcf3355..b18b12e8457 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMServerUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/RMServerUtils.java @@ -97,7 +97,7 @@ public class RMServerUtils { "INCORRECT_CONTAINER_VERSION_ERROR"; private static final String INVALID_CONTAINER_ID = "INVALID_CONTAINER_ID"; - private static final String RESOURCE_OUTSIDE_ALLOWED_RANGE = + public static final String RESOURCE_OUTSIDE_ALLOWED_RANGE = "RESOURCE_OUTSIDE_ALLOWED_RANGE"; protected static final RecordFactory RECORD_FACTORY = @@ -235,7 +235,7 @@ public class RMServerUtils { * requested memory/vcore is non-negative and not greater than max */ public static void normalizeAndValidateRequests(List ask, - Resource maximumResource, String queueName, YarnScheduler scheduler, + Resource maximumAllocation, String queueName, YarnScheduler scheduler, RMContext rmContext) throws InvalidResourceRequestException { // Get queue from scheduler QueueInfo queueInfo = null; @@ -247,7 +247,7 @@ public class RMServerUtils { } for (ResourceRequest resReq : ask) { - SchedulerUtils.normalizeAndvalidateRequest(resReq, maximumResource, + SchedulerUtils.normalizeAndValidateRequest(resReq, maximumAllocation, queueName, scheduler, rmContext, queueInfo); } } @@ -338,7 +338,8 @@ public class RMServerUtils { return false; } ResourceScheduler scheduler = rmContext.getScheduler(); - request.setCapability(scheduler.getNormalizedResource(request.getCapability())); + request.setCapability(scheduler + .getNormalizedResource(request.getCapability(), maximumAllocation)); return true; } 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 9d2b0586dc8..0acfca79110 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 @@ -1159,11 +1159,12 @@ public abstract class AbstractYarnScheduler } @Override - public Resource getNormalizedResource(Resource requestedResource) { + public Resource getNormalizedResource(Resource requestedResource, + Resource maxResourceCapability) { return SchedulerUtils.getNormalizedResource(requestedResource, getResourceCalculator(), getMinimumResourceCapability(), - getMaximumResourceCapability(), + maxResourceCapability, getMinimumResourceCapability()); } @@ -1173,8 +1174,20 @@ public abstract class AbstractYarnScheduler * @param asks resource requests */ protected void normalizeResourceRequests(List asks) { - for (ResourceRequest ask: asks) { - ask.setCapability(getNormalizedResource(ask.getCapability())); + normalizeResourceRequests(asks, null); + } + + /** + * Normalize a list of resource requests + * using queue maximum resource allocations. + * @param asks resource requests + */ + protected void normalizeResourceRequests(List asks, + String queueName) { + Resource maxAllocation = getMaximumResourceCapability(queueName); + for (ResourceRequest ask : asks) { + ask.setCapability( + getNormalizedResource(ask.getCapability(), maxAllocation)); } } 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/SchedulerUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerUtils.java index 9b07d37de52..9a02b6e75ca 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerUtils.java @@ -114,19 +114,19 @@ public class SchedulerUtils { public static final String UPDATED_CONTAINER = "Temporary container killed by application for ExeutionType update"; - - public static final String LOST_CONTAINER = + + public static final String LOST_CONTAINER = "Container released on a *lost* node"; - - public static final String PREEMPTED_CONTAINER = + + public static final String PREEMPTED_CONTAINER = "Container preempted by scheduler"; - - public static final String COMPLETED_APPLICATION = + + public static final String COMPLETED_APPLICATION = "Container of a completed application"; - + public static final String EXPIRED_CONTAINER = "Container expired since it was unused"; - + public static final String UNRESERVED_CONTAINER = "Container reservation no longer required."; @@ -141,7 +141,7 @@ public class SchedulerUtils { */ public static ContainerStatus createAbnormalContainerStatus( ContainerId containerId, String diagnostics) { - return createAbnormalContainerStatus(containerId, + return createAbnormalContainerStatus(containerId, ContainerExitStatus.ABORTED, diagnostics); } @@ -169,14 +169,14 @@ public class SchedulerUtils { */ public static ContainerStatus createPreemptedContainerStatus( ContainerId containerId, String diagnostics) { - return createAbnormalContainerStatus(containerId, + return createAbnormalContainerStatus(containerId, ContainerExitStatus.PREEMPTED, diagnostics); } /** * Utility to create a {@link ContainerStatus} during exceptional * circumstances. - * + * * @param containerId {@link ContainerId} of returned/released/lost container. * @param diagnostics diagnostic message * @return ContainerStatus for an returned/released/lost @@ -184,7 +184,7 @@ public class SchedulerUtils { */ private static ContainerStatus createAbnormalContainerStatus( ContainerId containerId, int exitStatus, String diagnostics) { - ContainerStatus containerStatus = + ContainerStatus containerStatus = recordFactory.newRecordInstance(ContainerStatus.class); containerStatus.setContainerId(containerId); containerStatus.setDiagnostics(diagnostics); @@ -254,23 +254,14 @@ public class SchedulerUtils { labelExp = RMNodeLabelsManager.NO_LABEL; } - if ( labelExp != null) { + if (labelExp != null) { resReq.setNodeLabelExpression(labelExp); } } public static void normalizeAndValidateRequest(ResourceRequest resReq, - Resource maximumResource, String queueName, YarnScheduler scheduler, - boolean isRecovery, RMContext rmContext) - throws InvalidResourceRequestException { - normalizeAndValidateRequest(resReq, maximumResource, queueName, scheduler, - isRecovery, rmContext, null); - } - - - private static void normalizeAndValidateRequest(ResourceRequest resReq, - Resource maximumResource, String queueName, YarnScheduler scheduler, - boolean isRecovery, RMContext rmContext, QueueInfo queueInfo) + Resource maximumAllocation, String queueName, YarnScheduler scheduler, + boolean isRecovery, RMContext rmContext, QueueInfo queueInfo) throws InvalidResourceRequestException { Configuration conf = rmContext.getYarnConfiguration(); // If Node label is not enabled throw exception @@ -299,37 +290,30 @@ public class SchedulerUtils { SchedulerUtils.normalizeNodeLabelExpressionInRequest(resReq, queueInfo); if (!isRecovery) { - validateResourceRequest(resReq, maximumResource, queueInfo, rmContext); + validateResourceRequest(resReq, maximumAllocation, queueInfo, rmContext); } } - public static void normalizeAndvalidateRequest(ResourceRequest resReq, - Resource maximumResource, String queueName, YarnScheduler scheduler, - RMContext rmContext) throws InvalidResourceRequestException { - normalizeAndvalidateRequest(resReq, maximumResource, queueName, scheduler, - rmContext, null); - } - - public static void normalizeAndvalidateRequest(ResourceRequest resReq, - Resource maximumResource, String queueName, YarnScheduler scheduler, + public static void normalizeAndValidateRequest(ResourceRequest resReq, + Resource maximumAllocation, String queueName, YarnScheduler scheduler, RMContext rmContext, QueueInfo queueInfo) throws InvalidResourceRequestException { - normalizeAndValidateRequest(resReq, maximumResource, queueName, scheduler, + normalizeAndValidateRequest(resReq, maximumAllocation, queueName, scheduler, false, rmContext, queueInfo); } /** * Utility method to validate a resource request, by insuring that the * requested memory/vcore is non-negative and not greater than max - * + * * @throws InvalidResourceRequestException when there is invalid request */ private static void validateResourceRequest(ResourceRequest resReq, - Resource maximumResource, QueueInfo queueInfo, RMContext rmContext) + Resource maximumAllocation, QueueInfo queueInfo, RMContext rmContext) throws InvalidResourceRequestException { final Resource requestedResource = resReq.getCapability(); checkResourceRequestAgainstAvailableResource(requestedResource, - maximumResource); + maximumAllocation); String labelExp = resReq.getNodeLabelExpression(); // we don't allow specify label expression other than resourceName=ANY now @@ -535,7 +519,7 @@ public class SchedulerUtils { if (!str.trim().isEmpty()) { // check queue label if (queueLabels == null) { - return false; + return false; } else { if (!queueLabels.contains(str) && !queueLabels.contains(RMNodeLabelsManager.ANY)) { 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/YarnScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/YarnScheduler.java index 0f7a5b5b3ed..d95fe7d0fd6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/YarnScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/YarnScheduler.java @@ -390,12 +390,17 @@ public interface YarnScheduler extends EventHandler { SchedulerNode getSchedulerNode(NodeId nodeId); /** - * Normalize a resource request. + * Normalize a resource request using scheduler level maximum resource or + * queue based maximum resource. * * @param requestedResource the resource to be normalized + * @param maxResourceCapability Maximum container allocation value, if null or + * empty scheduler level maximum container allocation value will be + * used * @return the normalized resource */ - Resource getNormalizedResource(Resource requestedResource); + Resource getNormalizedResource(Resource requestedResource, + Resource maxResourceCapability); /** * Verify whether a submitted application lifetime is valid as per configured 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/capacity/CapacityScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacityScheduler.java index 4b274df956f..75d61442577 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacityScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/CapacityScheduler.java @@ -1159,10 +1159,12 @@ public class CapacityScheduler extends if (asks == null) { return; } + Resource maxAllocation = getMaximumResourceCapability(); for (SchedulingRequest ask: asks) { ResourceSizing sizing = ask.getResourceSizing(); if (sizing != null && sizing.getResources() != null) { - sizing.setResources(getNormalizedResource(sizing.getResources())); + sizing.setResources( + getNormalizedResource(sizing.getResources(), maxAllocation)); } } } @@ -2567,6 +2569,9 @@ public class CapacityScheduler extends @Override public Resource getMaximumResourceCapability(String queueName) { + if(queueName == null || queueName.isEmpty()) { + return getMaximumResourceCapability(); + } CSQueue queue = getQueue(queueName); if (queue == null) { LOG.error("Unknown queue: " + queueName); 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/constraint/processor/PlacementConstraintProcessor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/constraint/processor/PlacementConstraintProcessor.java index cf944a6213a..c4c95741df4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/constraint/processor/PlacementConstraintProcessor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/constraint/processor/PlacementConstraintProcessor.java @@ -175,11 +175,19 @@ public class PlacementConstraintProcessor extends AbstractPlacementProcessor { private void dispatchRequestsForPlacement(ApplicationAttemptId appAttemptId, List schedulingRequests) { if (schedulingRequests != null && !schedulingRequests.isEmpty()) { + SchedulerApplicationAttempt appAttempt = + scheduler.getApplicationAttempt(appAttemptId); + String queueName = null; + if(appAttempt != null) { + queueName = appAttempt.getQueueName(); + } + Resource maxAllocation = + scheduler.getMaximumResourceCapability(queueName); // Normalize the Requests before dispatching schedulingRequests.forEach(req -> { Resource reqResource = req.getResourceSizing().getResources(); - req.getResourceSizing() - .setResources(this.scheduler.getNormalizedResource(reqResource)); + req.getResourceSizing().setResources( + this.scheduler.getNormalizedResource(reqResource, maxAllocation)); }); this.placementDispatcher.dispatch(new BatchedRequests(iteratorType, appAttemptId.getApplicationId(), schedulingRequests, 1)); 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/fair/AllocationConfiguration.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/AllocationConfiguration.java index e48e04b486c..826d9f523eb 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/AllocationConfiguration.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/AllocationConfiguration.java @@ -91,6 +91,9 @@ public class AllocationConfiguration extends ReservationSchedulerConfiguration { private final SchedulingPolicy defaultSchedulingPolicy; + //Map for maximum container resource allocation per queues by queue name + private final Map queueMaxContainerAllocationMap; + // Policy for mapping apps to queues @VisibleForTesting QueuePlacementPolicy placementPolicy; @@ -138,6 +141,8 @@ public class AllocationConfiguration extends ReservationSchedulerConfiguration { this.placementPolicy = newPlacementPolicy; this.configuredQueues = queueProperties.getConfiguredQueues(); this.nonPreemptableQueues = queueProperties.getNonPreemptableQueues(); + this.queueMaxContainerAllocationMap = + queueProperties.getMaxContainerAllocation(); } public AllocationConfiguration(Configuration conf) { @@ -167,6 +172,7 @@ public class AllocationConfiguration extends ReservationSchedulerConfiguration { placementPolicy = QueuePlacementPolicy.fromConfiguration(conf, configuredQueues); nonPreemptableQueues = new HashSet<>(); + queueMaxContainerAllocationMap = new HashMap<>(); } /** @@ -272,6 +278,12 @@ public class AllocationConfiguration extends ReservationSchedulerConfiguration { return maxQueueResource; } + @VisibleForTesting + Resource getQueueMaxContainerAllocation(String queue) { + Resource resource = queueMaxContainerAllocationMap.get(queue); + return resource == null ? Resources.unbounded() : resource; + } + /** * Get the maximum resource allocation for children of the given queue. * @@ -375,6 +387,7 @@ public class AllocationConfiguration extends ReservationSchedulerConfiguration { queue.setMaxRunningApps(getQueueMaxApps(name)); queue.setMaxAMShare(getQueueMaxAMShare(name)); queue.setMaxChildQueueResource(getMaxChildResources(name)); + queue.setMaxContainerAllocation(getQueueMaxContainerAllocation(name)); // Set queue metrics. queue.getMetrics().setMinShare(queue.getMinShare()); 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/fair/FSLeafQueue.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSLeafQueue.java index cbc74d25345..7e4dab8088d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSLeafQueue.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSLeafQueue.java @@ -547,6 +547,16 @@ public class FSLeafQueue extends FSQueue { this.weights = weight; } + @Override + public Resource getMaximumContainerAllocation() { + if (maxContainerAllocation.equals(Resources.unbounded()) + && getParent() != null) { + return getParent().getMaximumContainerAllocation(); + } else { + return maxContainerAllocation; + } + } + /** * Helper method to compute the amount of minshare starvation. * 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/fair/FSParentQueue.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSParentQueue.java index d5df549b282..e9f4af67450 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSParentQueue.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSParentQueue.java @@ -59,7 +59,20 @@ public class FSParentQueue extends FSQueue { FSParentQueue parent) { super(name, scheduler, parent); } - + + @Override + public Resource getMaximumContainerAllocation() { + if (getName().equals("root")) { + return maxContainerAllocation; + } + if (maxContainerAllocation.equals(Resources.unbounded()) + && getParent() != null) { + return getParent().getMaximumContainerAllocation(); + } else { + return maxContainerAllocation; + } + } + void addChildQueue(FSQueue child) { writeLock.lock(); try { 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/fair/FSQueue.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSQueue.java index 6b88a329fa3..6217f550f8b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSQueue.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSQueue.java @@ -84,6 +84,7 @@ public abstract class FSQueue implements Queue, Schedulable { private float fairSharePreemptionThreshold = 0.5f; private boolean preemptable = true; private boolean isDynamic = true; + protected Resource maxContainerAllocation; public FSQueue(String name, FairScheduler scheduler, FSParentQueue parent) { this.name = name; @@ -163,6 +164,12 @@ public abstract class FSQueue implements Queue, Schedulable { this.maxShare = maxShare; } + public void setMaxContainerAllocation(Resource maxContainerAllocation){ + this.maxContainerAllocation = maxContainerAllocation; + } + + public abstract Resource getMaximumContainerAllocation(); + @Override public Resource getMaxShare() { Resource maxResource = maxShare.getResource(scheduler.getClusterResource()); 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/fair/FairScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FairScheduler.java index 43a47ae65fe..da5e4c9347e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FairScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FairScheduler.java @@ -192,6 +192,7 @@ public class FairScheduler extends protected long rackLocalityDelayMs; // Delay for rack locality protected boolean assignMultiple; // Allocate multiple containers per // heartbeat + @VisibleForTesting boolean maxAssignDynamic; protected int maxAssign; // Max containers to assign per heartbeat @@ -227,12 +228,12 @@ public class FairScheduler extends private void validateConf(FairSchedulerConfiguration config) { // validate scheduler memory allocation setting - int minMem = config.getInt( - YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, - YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB); - int maxMem = config.getInt( - YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, - YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB); + int minMem = + config.getInt(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, + YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB); + int maxMem = + config.getInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB); if (minMem < 0 || minMem > maxMem) { throw new YarnRuntimeException("Invalid resource scheduler memory" @@ -254,12 +255,12 @@ public class FairScheduler extends } // validate scheduler vcores allocation setting - int minVcores = config.getInt( - YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES, - YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); - int maxVcores = config.getInt( - YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, - YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); + int minVcores = + config.getInt(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES, + YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); + int maxVcores = + config.getInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); if (minVcores < 0 || minVcores > maxVcores) { throw new YarnRuntimeException("Invalid resource scheduler vcores" @@ -833,14 +834,35 @@ public class FairScheduler extends } @Override - public Resource getNormalizedResource(Resource requestedResource) { + public Resource getNormalizedResource(Resource requestedResource, + Resource maxResourceCapability) { return SchedulerUtils.getNormalizedResource(requestedResource, DOMINANT_RESOURCE_CALCULATOR, minimumAllocation, - getMaximumResourceCapability(), + maxResourceCapability, incrAllocation); } + @Override + public Resource getMaximumResourceCapability(String queueName) { + if(queueName == null || queueName.isEmpty()) { + return getMaximumResourceCapability(); + } + FSQueue queue = queueMgr.getQueue(queueName); + Resource schedulerLevelMaxResourceCapability = + getMaximumResourceCapability(); + if (queue == null) { + return schedulerLevelMaxResourceCapability; + } + Resource queueMaxResourceCapability = queue.getMaximumContainerAllocation(); + if (queueMaxResourceCapability.equals(Resources.unbounded())) { + return schedulerLevelMaxResourceCapability; + } else { + return Resources.componentwiseMin(schedulerLevelMaxResourceCapability, + queueMaxResourceCapability); + } + } + @VisibleForTesting @Override public void killContainer(RMContainer container) { @@ -897,7 +919,7 @@ public class FairScheduler extends handleContainerUpdates(application, updateRequests); // Sanity check - normalizeResourceRequests(ask); + normalizeResourceRequests(ask, queue.getName()); // TODO, normalize SchedulingRequest 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/fair/allocation/AllocationFileQueueParser.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocation/AllocationFileQueueParser.java index 441c34a1aa1..72c6c6801b3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocation/AllocationFileQueueParser.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocation/AllocationFileQueueParser.java @@ -51,6 +51,8 @@ public class AllocationFileQueueParser { private static final String MAX_CHILD_RESOURCES = "maxChildResources"; private static final String MAX_RUNNING_APPS = "maxRunningApps"; private static final String MAX_AMSHARE = "maxAMShare"; + public static final String MAX_CONTAINER_ALLOCATION = + "maxContainerAllocation"; private static final String WEIGHT = "weight"; private static final String MIN_SHARE_PREEMPTION_TIMEOUT = "minSharePreemptionTimeout"; @@ -155,6 +157,11 @@ public class AllocationFileQueueParser { float val = Float.parseFloat(text); val = Math.min(val, 1.0f); builder.queueMaxAMShares(queueName, val); + } else if (MAX_CONTAINER_ALLOCATION.equals(field.getTagName())) { + String text = getTrimmedTextData(field); + ConfigurableResource val = + FairSchedulerConfiguration.parseResourceConfigValue(text); + builder.queueMaxContainerAllocation(queueName, val.getResource()); } else if (WEIGHT.equals(field.getTagName())) { String text = getTrimmedTextData(field); double val = Double.parseDouble(text); 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/fair/allocation/QueueProperties.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocation/QueueProperties.java index ee5f1790237..badf05f0092 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocation/QueueProperties.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocation/QueueProperties.java @@ -53,6 +53,7 @@ public class QueueProperties { private final Set reservableQueues; private final Set nonPreemptableQueues; private final Map> configuredQueues; + private final Map queueMaxContainerAllocation; QueueProperties(Builder builder) { this.reservableQueues = builder.reservableQueues; @@ -70,6 +71,7 @@ public class QueueProperties { this.maxChildQueueResources = builder.maxChildQueueResources; this.reservationAcls = builder.reservationAcls; this.queueAcls = builder.queueAcls; + this.queueMaxContainerAllocation = builder.queueMaxContainerAllocation; } public Map> getConfiguredQueues() { @@ -133,7 +135,11 @@ public class QueueProperties { return nonPreemptableQueues; } - /** + public Map getMaxContainerAllocation() { + return queueMaxContainerAllocation; + } + + /** * Builder class for {@link QueueProperties}. * All methods are adding queue properties to the maps of this builder * keyed by the queue's name except some methods @@ -149,6 +155,7 @@ public class QueueProperties { new HashMap<>(); private Map queueMaxApps = new HashMap<>(); private Map queueMaxAMShares = new HashMap<>(); + private Map queueMaxContainerAllocation = new HashMap<>(); private Map queueWeights = new HashMap<>(); private Map queuePolicies = new HashMap<>(); private Map minSharePreemptionTimeouts = new HashMap<>(); @@ -253,6 +260,12 @@ public class QueueProperties { return this; } + public Builder queueMaxContainerAllocation(String queueName, + Resource value) { + queueMaxContainerAllocation.put(queueName, value); + return this; + } + public void configuredQueues(FSQueueType queueType, String queueName) { this.configuredQueues.get(queueType).add(queueName); } @@ -275,6 +288,5 @@ public class QueueProperties { public QueueProperties build() { return new QueueProperties(this); } - } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/FairSchedulerPage.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/FairSchedulerPage.java index ef417d4760f..7f31defa066 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/FairSchedulerPage.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/FairSchedulerPage.java @@ -78,6 +78,8 @@ public class FairSchedulerPage extends RmView { __("Num Pending Applications:", qinfo.getNumPendingApplications()). __("Min Resources:", qinfo.getMinResources().toString()). __("Max Resources:", qinfo.getMaxResources().toString()). + __("Max Container Allocation:", + qinfo.getMaxContainerAllocation().toString()). __("Reserved Resources:", qinfo.getReservedResources().toString()); int maxApps = qinfo.getMaxApplications(); if (maxApps < Integer.MAX_VALUE) { @@ -107,6 +109,8 @@ public class FairSchedulerPage extends RmView { __("Used Resources:", qinfo.getUsedResources().toString()). __("Min Resources:", qinfo.getMinResources().toString()). __("Max Resources:", qinfo.getMaxResources().toString()). + __("Max Container Allocation:", + qinfo.getMaxContainerAllocation().toString()). __("Reserved Resources:", qinfo.getReservedResources().toString()); int maxApps = qinfo.getMaxApplications(); if (maxApps < Integer.MAX_VALUE) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/FairSchedulerQueueInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/FairSchedulerQueueInfo.java index 913513c52ae..70c5fd04882 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/FairSchedulerQueueInfo.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/FairSchedulerQueueInfo.java @@ -60,6 +60,7 @@ public class FairSchedulerQueueInfo { private ResourceInfo fairResources; private ResourceInfo clusterResources; private ResourceInfo reservedResources; + private ResourceInfo maxContainerAllocation; private long allocatedContainers; private long reservedContainers; @@ -99,6 +100,8 @@ public class FairSchedulerQueueInfo { maxResources = new ResourceInfo( Resources.componentwiseMin(queue.getMaxShare(), scheduler.getClusterResource())); + maxContainerAllocation = + new ResourceInfo(scheduler.getMaximumResourceCapability(queueName)); reservedResources = new ResourceInfo(queue.getReservedResource()); fractionMemSteadyFairShare = @@ -186,7 +189,11 @@ public class FairSchedulerQueueInfo { public ResourceInfo getMaxResources() { return maxResources; } - + + public ResourceInfo getMaxContainerAllocation() { + return maxContainerAllocation; + } + public ResourceInfo getReservedResources() { return reservedResources; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/AppManagerTestBase.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/AppManagerTestBase.java new file mode 100644 index 00000000000..36258b431f9 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/AppManagerTestBase.java @@ -0,0 +1,107 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager; + +import static java.util.stream.Collectors.toSet; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.List; +import java.util.Set; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.yarn.api.records.ApplicationId; +import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext; +import org.apache.hadoop.yarn.exceptions.YarnException; +import org.apache.hadoop.yarn.server.resourcemanager.recovery.RMStateStore; +import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.YarnScheduler; +import org.apache.hadoop.yarn.server.resourcemanager.security.ClientToAMTokenSecretManagerInRM; +import org.apache.hadoop.yarn.server.security.ApplicationACLsManager; +import org.mockito.ArgumentCaptor; + +/** + * Base class for AppManager related test. + * + */ +public class AppManagerTestBase { + + // Extend and make the functions we want to test public + protected class TestRMAppManager extends RMAppManager { + private final RMStateStore stateStore; + + public TestRMAppManager(RMContext context, Configuration conf) { + super(context, null, null, new ApplicationACLsManager(conf), conf); + this.stateStore = context.getStateStore(); + } + + public TestRMAppManager(RMContext context, + ClientToAMTokenSecretManagerInRM clientToAMSecretManager, + YarnScheduler scheduler, ApplicationMasterService masterService, + ApplicationACLsManager applicationACLsManager, Configuration conf) { + super(context, scheduler, masterService, applicationACLsManager, conf); + this.stateStore = context.getStateStore(); + } + + public void checkAppNumCompletedLimit() { + super.checkAppNumCompletedLimit(); + } + + public void finishApplication(ApplicationId appId) { + super.finishApplication(appId); + } + + public int getCompletedAppsListSize() { + return super.getCompletedAppsListSize(); + } + + public int getNumberOfCompletedAppsInStateStore() { + return this.completedAppsInStateStore; + } + + public List getCompletedApps() { + return completedApps; + } + + public Set getFirstNCompletedApps(int n) { + return getCompletedApps().stream().limit(n).collect(toSet()); + } + + public Set getCompletedAppsWithEvenIdsInRange(int n) { + return getCompletedApps().stream().limit(n) + .filter(app -> app.getId() % 2 == 0).collect(toSet()); + } + + public Set getRemovedAppsFromStateStore(int numRemoves) { + ArgumentCaptor argumentCaptor = + ArgumentCaptor.forClass(RMApp.class); + verify(stateStore, times(numRemoves)) + .removeApplication(argumentCaptor.capture()); + return argumentCaptor.getAllValues().stream().map(RMApp::getApplicationId) + .collect(toSet()); + } + + public void submitApplication( + ApplicationSubmissionContext submissionContext, String user) + throws YarnException { + super.submitApplication(submissionContext, System.currentTimeMillis(), + user); + } + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockRM.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockRM.java index df86f28c059..408cb187619 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockRM.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/MockRM.java @@ -513,6 +513,14 @@ public class MockRM extends ResourceManager { return submitApp(masterMemory, false); } + public RMApp submitApp(int masterMemory, String queue) throws Exception { + return submitApp(masterMemory, "", + UserGroupInformation.getCurrentUser().getShortUserName(), null, false, + queue, super.getConfig().getInt(YarnConfiguration.RM_AM_MAX_ATTEMPTS, + YarnConfiguration.DEFAULT_RM_AM_MAX_ATTEMPTS), + null); + } + public RMApp submitApp(int masterMemory, Set appTags) throws Exception { Resource resource = Resource.newInstance(masterMemory, 0); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManager.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManager.java index 0bd5372806d..8f482938cfa 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManager.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManager.java @@ -68,7 +68,6 @@ import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.AMLivelinessM import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptImpl; import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.ContainerAllocationExpirer; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; -import org.apache.hadoop.yarn.server.resourcemanager.scheduler.YarnScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacityScheduler; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.ManagedParentQueue; @@ -82,12 +81,10 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import java.io.IOException; -import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; @@ -99,10 +96,10 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentMap; -import static java.util.stream.Collectors.toSet; import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.PREFIX; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.isA; import static org.mockito.Matchers.matches; import static org.mockito.Mockito.doAnswer; @@ -117,7 +114,7 @@ import static org.mockito.Mockito.when; * */ -public class TestAppManager{ +public class TestAppManager extends AppManagerTestBase{ private Log LOG = LogFactory.getLog(TestAppManager.class); private static RMAppEventType appEventType = RMAppEventType.KILL; @@ -234,70 +231,6 @@ public class TestAppManager{ setAppEventType(event.getType()); System.out.println("in handle routine " + getAppEventType().toString()); } - } - - - // Extend and make the functions we want to test public - public class TestRMAppManager extends RMAppManager { - private final RMStateStore stateStore; - - public TestRMAppManager(RMContext context, Configuration conf) { - super(context, null, null, new ApplicationACLsManager(conf), conf); - this.stateStore = context.getStateStore(); - } - - public TestRMAppManager(RMContext context, - ClientToAMTokenSecretManagerInRM clientToAMSecretManager, - YarnScheduler scheduler, ApplicationMasterService masterService, - ApplicationACLsManager applicationACLsManager, Configuration conf) { - super(context, scheduler, masterService, applicationACLsManager, conf); - this.stateStore = context.getStateStore(); - } - - public void checkAppNumCompletedLimit() { - super.checkAppNumCompletedLimit(); - } - - public void finishApplication(ApplicationId appId) { - super.finishApplication(appId); - } - - public int getCompletedAppsListSize() { - return super.getCompletedAppsListSize(); - } - - public int getNumberOfCompletedAppsInStateStore() { - return this.completedAppsInStateStore; - } - - List getCompletedApps() { - return completedApps; - } - - Set getFirstNCompletedApps(int n) { - return getCompletedApps().stream().limit(n).collect(toSet()); - } - - Set getCompletedAppsWithEvenIdsInRange(int n) { - return getCompletedApps().stream().limit(n) - .filter(app -> app.getId() % 2 == 0).collect(toSet()); - } - - Set getRemovedAppsFromStateStore(int numRemoves) { - ArgumentCaptor argumentCaptor = - ArgumentCaptor.forClass(RMApp.class); - verify(stateStore, times(numRemoves)) - .removeApplication(argumentCaptor.capture()); - return argumentCaptor.getAllValues().stream().map(RMApp::getApplicationId) - .collect(toSet()); - } - - public void submitApplication( - ApplicationSubmissionContext submissionContext, String user) - throws YarnException, IOException { - super.submitApplication(submissionContext, System.currentTimeMillis(), - user); - } } private void addToCompletedApps(TestRMAppManager appMonitor, @@ -1213,17 +1146,21 @@ public class TestAppManager{ Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB)); + when(scheduler.getMaximumResourceCapability(anyString())).thenReturn( + Resources.createResource( + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB)); + ResourceCalculator rs = mock(ResourceCalculator.class); when(scheduler.getResourceCalculator()).thenReturn(rs); - when(scheduler.getNormalizedResource(any())) + when(scheduler.getNormalizedResource(any(), any())) .thenAnswer(new Answer() { - @Override - public Resource answer(InvocationOnMock invocationOnMock) - throws Throwable { - return (Resource) invocationOnMock.getArguments()[0]; - } - }); + @Override + public Resource answer(InvocationOnMock invocationOnMock) + throws Throwable { + return (Resource) invocationOnMock.getArguments()[0]; + } + }); return scheduler; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManagerWithFairScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManagerWithFairScheduler.java new file mode 100644 index 00000000000..feb7ed246ce --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestAppManagerWithFairScheduler.java @@ -0,0 +1,177 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager; + +import static org.apache.hadoop.yarn.exceptions.InvalidResourceRequestException.InvalidResourceType.GREATER_THEN_MAX_ALLOCATION; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.matches; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; + +import org.apache.hadoop.test.GenericTestUtils; +import org.apache.hadoop.yarn.MockApps; +import org.apache.hadoop.yarn.api.records.ApplicationAccessType; +import org.apache.hadoop.yarn.api.records.ApplicationId; +import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext; +import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; +import org.apache.hadoop.yarn.api.records.Priority; +import org.apache.hadoop.yarn.api.records.QueueInfo; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.exceptions.InvalidResourceRequestException; +import org.apache.hadoop.yarn.exceptions.YarnException; +import org.apache.hadoop.yarn.factories.RecordFactory; +import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider; +import org.apache.hadoop.yarn.server.resourcemanager.placement.ApplicationPlacementContext; +import org.apache.hadoop.yarn.server.resourcemanager.placement.PlacementManager; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairSchedulerConfiguration; +import org.apache.hadoop.yarn.server.resourcemanager.security.ClientToAMTokenSecretManagerInRM; +import org.apache.hadoop.yarn.server.security.ApplicationACLsManager; +import org.apache.hadoop.yarn.util.resource.Resources; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +/** + * Testing applications being retired from RM with fair scheduler. + * + */ +public class TestAppManagerWithFairScheduler extends AppManagerTestBase{ + + private static final String TEST_FOLDER = "test-queues"; + + private static YarnConfiguration conf = new YarnConfiguration(); + + @BeforeClass + public static void setup() throws IOException { + String allocFile = + GenericTestUtils.getTestDir(TEST_FOLDER).getAbsolutePath(); + + int queueMaxAllocation = 512; + + PrintWriter out = new PrintWriter(new FileWriter(allocFile)); + out.println(""); + out.println(""); + out.println(" "); + out.println(" " + queueMaxAllocation + + " mb 1 vcores" + ""); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(""); + out.close(); + + conf.setClass(YarnConfiguration.RM_SCHEDULER, FairScheduler.class, + ResourceScheduler.class); + + conf.set(FairSchedulerConfiguration.ALLOCATION_FILE, allocFile); + } + + @AfterClass + public static void teardown(){ + File allocFile = GenericTestUtils.getTestDir(TEST_FOLDER); + allocFile.delete(); + } + + @Test + public void testQueueSubmitWithHighQueueContainerSize() + throws YarnException { + + ApplicationId appId = MockApps.newAppID(1); + RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null); + + Resource resource = Resources.createResource( + YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB); + + ApplicationSubmissionContext asContext = + recordFactory.newRecordInstance(ApplicationSubmissionContext.class); + asContext.setApplicationId(appId); + asContext.setResource(resource); + asContext.setPriority(Priority.newInstance(0)); + asContext.setAMContainerSpec(mockContainerLaunchContext(recordFactory)); + asContext.setQueue("queueA"); + QueueInfo mockDefaultQueueInfo = mock(QueueInfo.class); + + // Setup a PlacementManager returns a new queue + PlacementManager placementMgr = mock(PlacementManager.class); + doAnswer(new Answer() { + + @Override + public ApplicationPlacementContext answer(InvocationOnMock invocation) + throws Throwable { + return new ApplicationPlacementContext("queueA"); + } + + }).when(placementMgr).placeApplication( + any(ApplicationSubmissionContext.class), matches("test1")); + doAnswer(new Answer() { + + @Override + public ApplicationPlacementContext answer(InvocationOnMock invocation) + throws Throwable { + return new ApplicationPlacementContext("queueB"); + } + + }).when(placementMgr).placeApplication( + any(ApplicationSubmissionContext.class), matches("test2")); + + MockRM newMockRM = new MockRM(conf); + RMContext newMockRMContext = newMockRM.getRMContext(); + newMockRMContext.setQueuePlacementManager(placementMgr); + ApplicationMasterService masterService = new ApplicationMasterService( + newMockRMContext, newMockRMContext.getScheduler()); + + TestRMAppManager newAppMonitor = new TestRMAppManager(newMockRMContext, + new ClientToAMTokenSecretManagerInRM(), newMockRMContext.getScheduler(), + masterService, new ApplicationACLsManager(conf), conf); + + // only user test has permission to submit to 'test' queue + + try { + newAppMonitor.submitApplication(asContext, "test1"); + Assert.fail("Test should fail on too high allocation!"); + } catch (InvalidResourceRequestException e) { + Assert.assertEquals(GREATER_THEN_MAX_ALLOCATION, + e.getInvalidResourceType()); + } + + // Should not throw exception + newAppMonitor.submitApplication(asContext, "test2"); + } + + private static ContainerLaunchContext mockContainerLaunchContext( + RecordFactory recordFactory) { + ContainerLaunchContext amContainer = recordFactory.newRecordInstance( + ContainerLaunchContext.class); + amContainer + .setApplicationACLs(new HashMap()); + return amContainer; + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestClientRMService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestClientRMService.java index 61048a51d81..3fe46b1f607 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestClientRMService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestClientRMService.java @@ -362,9 +362,9 @@ public class TestClientRMService { @Test public void testGetApplicationReport() throws Exception { - YarnScheduler yarnScheduler = mock(YarnScheduler.class); + ResourceScheduler scheduler = mock(ResourceScheduler.class); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); ApplicationId appId1 = getApplicationId(1); @@ -373,7 +373,7 @@ public class TestClientRMService { mockAclsManager.checkAccess(UserGroupInformation.getCurrentUser(), ApplicationAccessType.VIEW_APP, null, appId1)).thenReturn(true); - ClientRMService rmService = new ClientRMService(rmContext, yarnScheduler, + ClientRMService rmService = new ClientRMService(rmContext, scheduler, null, mockAclsManager, null, null); try { RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null); @@ -456,9 +456,9 @@ public class TestClientRMService { public void testGetApplicationResourceUsageReportDummy() throws YarnException, IOException { ApplicationAttemptId attemptId = getApplicationAttemptId(1); - YarnScheduler yarnScheduler = mockYarnScheduler(); + ResourceScheduler scheduler = mockResourceScheduler(); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); when(rmContext.getDispatcher().getEventHandler()).thenReturn( new EventHandler() { public void handle(Event event) { @@ -468,7 +468,7 @@ public class TestClientRMService { mock(ApplicationSubmissionContext.class); YarnConfiguration config = new YarnConfiguration(); RMAppAttemptImpl rmAppAttemptImpl = new RMAppAttemptImpl(attemptId, - rmContext, yarnScheduler, null, asContext, config, null, null); + rmContext, scheduler, null, asContext, config, null, null); ApplicationResourceUsageReport report = rmAppAttemptImpl .getApplicationResourceUsageReport(); assertEquals(report, RMServerUtils.DUMMY_APPLICATION_RESOURCE_USAGE_REPORT); @@ -537,14 +537,14 @@ public class TestClientRMService { } public ClientRMService createRMService() throws IOException, YarnException { - YarnScheduler yarnScheduler = mockYarnScheduler(); + ResourceScheduler scheduler = mockResourceScheduler(); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); ConcurrentHashMap apps = getRMApps(rmContext, - yarnScheduler); + scheduler); when(rmContext.getRMApps()).thenReturn(apps); when(rmContext.getYarnConfiguration()).thenReturn(new Configuration()); - RMAppManager appManager = new RMAppManager(rmContext, yarnScheduler, null, + RMAppManager appManager = new RMAppManager(rmContext, scheduler, null, mock(ApplicationACLsManager.class), new Configuration()); when(rmContext.getDispatcher().getEventHandler()).thenReturn( new EventHandler() { @@ -558,7 +558,7 @@ public class TestClientRMService { mockQueueACLsManager.checkAccess(any(UserGroupInformation.class), any(QueueACL.class), any(RMApp.class), any(String.class), any())).thenReturn(true); - return new ClientRMService(rmContext, yarnScheduler, appManager, + return new ClientRMService(rmContext, scheduler, appManager, mockAclsManager, mockQueueACLsManager, null); } @@ -907,9 +907,9 @@ public class TestClientRMService { @Test public void testGetQueueInfo() throws Exception { - YarnScheduler yarnScheduler = mock(YarnScheduler.class); + ResourceScheduler scheduler = mock(ResourceScheduler.class); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); ApplicationACLsManager mockAclsManager = mock(ApplicationACLsManager.class); QueueACLsManager mockQueueACLsManager = mock(QueueACLsManager.class); @@ -921,7 +921,7 @@ public class TestClientRMService { any(ApplicationAccessType.class), anyString(), any(ApplicationId.class))).thenReturn(true); - ClientRMService rmService = new ClientRMService(rmContext, yarnScheduler, + ClientRMService rmService = new ClientRMService(rmContext, scheduler, null, mockAclsManager, mockQueueACLsManager, null); GetQueueInfoRequest request = recordFactory .newRecordInstance(GetQueueInfoRequest.class); @@ -960,7 +960,7 @@ public class TestClientRMService { any(ApplicationAccessType.class), anyString(), any(ApplicationId.class))).thenReturn(false); - ClientRMService rmService1 = new ClientRMService(rmContext, yarnScheduler, + ClientRMService rmService1 = new ClientRMService(rmContext, scheduler, null, mockAclsManager1, mockQueueACLsManager1, null); request.setQueueName("testqueue"); request.setIncludeApplications(true); @@ -974,12 +974,12 @@ public class TestClientRMService { @Test (timeout = 30000) @SuppressWarnings ("rawtypes") public void testAppSubmit() throws Exception { - YarnScheduler yarnScheduler = mockYarnScheduler(); + ResourceScheduler scheduler = mockResourceScheduler(); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); RMStateStore stateStore = mock(RMStateStore.class); when(rmContext.getStateStore()).thenReturn(stateStore); - RMAppManager appManager = new RMAppManager(rmContext, yarnScheduler, + RMAppManager appManager = new RMAppManager(rmContext, scheduler, null, mock(ApplicationACLsManager.class), new Configuration()); when(rmContext.getDispatcher().getEventHandler()).thenReturn( new EventHandler() { @@ -1001,7 +1001,7 @@ public class TestClientRMService { any())) .thenReturn(true); ClientRMService rmService = - new ClientRMService(rmContext, yarnScheduler, appManager, + new ClientRMService(rmContext, scheduler, appManager, mockAclsManager, mockQueueACLsManager, null); rmService.init(new Configuration()); @@ -1085,15 +1085,15 @@ public class TestClientRMService { * 2. Test each of the filters */ // Basic setup - YarnScheduler yarnScheduler = mockYarnScheduler(); + ResourceScheduler scheduler = mockResourceScheduler(); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); RMStateStore stateStore = mock(RMStateStore.class); when(rmContext.getStateStore()).thenReturn(stateStore); doReturn(mock(RMTimelineCollectorManager.class)).when(rmContext) .getRMTimelineCollectorManager(); - RMAppManager appManager = new RMAppManager(rmContext, yarnScheduler, + RMAppManager appManager = new RMAppManager(rmContext, scheduler, null, mock(ApplicationACLsManager.class), new Configuration()); when(rmContext.getDispatcher().getEventHandler()).thenReturn( new EventHandler() { @@ -1107,7 +1107,7 @@ public class TestClientRMService { any())) .thenReturn(true); ClientRMService rmService = - new ClientRMService(rmContext, yarnScheduler, appManager, + new ClientRMService(rmContext, scheduler, appManager, mockAclsManager, mockQueueACLsManager, null); rmService.init(new Configuration()); @@ -1238,12 +1238,12 @@ public class TestClientRMService { public void testConcurrentAppSubmit() throws IOException, InterruptedException, BrokenBarrierException, YarnException { - YarnScheduler yarnScheduler = mockYarnScheduler(); + ResourceScheduler scheduler = mockResourceScheduler(); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); RMStateStore stateStore = mock(RMStateStore.class); when(rmContext.getStateStore()).thenReturn(stateStore); - RMAppManager appManager = new RMAppManager(rmContext, yarnScheduler, + RMAppManager appManager = new RMAppManager(rmContext, scheduler, null, mock(ApplicationACLsManager.class), new Configuration()); final ApplicationId appId1 = getApplicationId(100); @@ -1280,7 +1280,7 @@ public class TestClientRMService { .getRMTimelineCollectorManager(); final ClientRMService rmService = - new ClientRMService(rmContext, yarnScheduler, appManager, null, null, + new ClientRMService(rmContext, scheduler, appManager, null, null, null); rmService.init(new Configuration()); @@ -1339,7 +1339,7 @@ public class TestClientRMService { return submitRequest; } - private void mockRMContext(YarnScheduler yarnScheduler, RMContext rmContext) + private void mockRMContext(ResourceScheduler scheduler, RMContext rmContext) throws IOException { Dispatcher dispatcher = mock(Dispatcher.class); when(rmContext.getDispatcher()).thenReturn(dispatcher); @@ -1361,22 +1361,21 @@ public class TestClientRMService { queueConfigsByPartition.put("*", queueConfigs); queInfo.setQueueConfigurations(queueConfigsByPartition); - when(yarnScheduler.getQueueInfo(eq("testqueue"), anyBoolean(), anyBoolean())) + when(scheduler.getQueueInfo(eq("testqueue"), anyBoolean(), anyBoolean())) .thenReturn(queInfo); - when(yarnScheduler.getQueueInfo(eq("nonexistentqueue"), anyBoolean(), anyBoolean())) - .thenThrow(new IOException("queue does not exist")); + when(scheduler.getQueueInfo(eq("nonexistentqueue"), anyBoolean(), + anyBoolean())).thenThrow(new IOException("queue does not exist")); RMApplicationHistoryWriter writer = mock(RMApplicationHistoryWriter.class); when(rmContext.getRMApplicationHistoryWriter()).thenReturn(writer); SystemMetricsPublisher publisher = mock(SystemMetricsPublisher.class); when(rmContext.getSystemMetricsPublisher()).thenReturn(publisher); when(rmContext.getYarnConfiguration()).thenReturn(new YarnConfiguration()); - ConcurrentHashMap apps = getRMApps(rmContext, - yarnScheduler); + ConcurrentHashMap apps = + getRMApps(rmContext, scheduler); when(rmContext.getRMApps()).thenReturn(apps); - when(yarnScheduler.getAppsInQueue(eq("testqueue"))).thenReturn( + when(scheduler.getAppsInQueue(eq("testqueue"))).thenReturn( getSchedulerApps(apps)); - ResourceScheduler rs = mock(ResourceScheduler.class); - when(rmContext.getScheduler()).thenReturn(rs); + when(rmContext.getScheduler()).thenReturn(scheduler); } private ConcurrentHashMap getRMApps( @@ -1480,31 +1479,32 @@ public class TestClientRMService { return app; } - private static YarnScheduler mockYarnScheduler() throws YarnException { - YarnScheduler yarnScheduler = mock(YarnScheduler.class); - when(yarnScheduler.getMinimumResourceCapability()).thenReturn( + private static ResourceScheduler mockResourceScheduler() + throws YarnException { + ResourceScheduler scheduler = mock(ResourceScheduler.class); + when(scheduler.getMinimumResourceCapability()).thenReturn( Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB)); - when(yarnScheduler.getMaximumResourceCapability()).thenReturn( + when(scheduler.getMaximumResourceCapability()).thenReturn( Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB)); - when(yarnScheduler.getMaximumResourceCapability(any(String.class))) - .thenReturn(Resources.createResource( + when(scheduler.getMaximumResourceCapability(anyString())).thenReturn( + Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB)); - when(yarnScheduler.getAppsInQueue(QUEUE_1)).thenReturn( + when(scheduler.getAppsInQueue(QUEUE_1)).thenReturn( Arrays.asList(getApplicationAttemptId(101), getApplicationAttemptId(102))); - when(yarnScheduler.getAppsInQueue(QUEUE_2)).thenReturn( + when(scheduler.getAppsInQueue(QUEUE_2)).thenReturn( Arrays.asList(getApplicationAttemptId(103))); ApplicationAttemptId attemptId = getApplicationAttemptId(1); - when(yarnScheduler.getAppResourceUsageReport(attemptId)).thenReturn(null); + when(scheduler.getAppResourceUsageReport(attemptId)).thenReturn(null); ResourceCalculator rs = mock(ResourceCalculator.class); - when(yarnScheduler.getResourceCalculator()).thenReturn(rs); + when(scheduler.getResourceCalculator()).thenReturn(rs); - when(yarnScheduler.checkAndGetApplicationPriority(any(Priority.class), + when(scheduler.checkAndGetApplicationPriority(any(Priority.class), any(UserGroupInformation.class), anyString(), any(ApplicationId.class))) .thenReturn(Priority.newInstance(0)); - return yarnScheduler; + return scheduler; } private ResourceManager setupResourceManager() { @@ -2427,15 +2427,15 @@ public class TestClientRMService { * Submit 3 applications alternately in two queues */ // Basic setup - YarnScheduler yarnScheduler = mockYarnScheduler(); + ResourceScheduler scheduler = mockResourceScheduler(); RMContext rmContext = mock(RMContext.class); - mockRMContext(yarnScheduler, rmContext); + mockRMContext(scheduler, rmContext); RMStateStore stateStore = mock(RMStateStore.class); when(rmContext.getStateStore()).thenReturn(stateStore); doReturn(mock(RMTimelineCollectorManager.class)).when(rmContext) .getRMTimelineCollectorManager(); - RMAppManager appManager = new RMAppManager(rmContext, yarnScheduler, null, + RMAppManager appManager = new RMAppManager(rmContext, scheduler, null, mock(ApplicationACLsManager.class), new Configuration()); when(rmContext.getDispatcher().getEventHandler()) .thenReturn(new EventHandler() { @@ -2454,7 +2454,7 @@ public class TestClientRMService { when(appAclsManager.checkAccess(eq(UserGroupInformation.getCurrentUser()), any(ApplicationAccessType.class), any(String.class), any(ApplicationId.class))).thenReturn(false); - ClientRMService rmService = new ClientRMService(rmContext, yarnScheduler, + ClientRMService rmService = new ClientRMService(rmContext, scheduler, appManager, appAclsManager, queueAclsManager, null); rmService.init(new Configuration()); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMServerUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMServerUtils.java index 078b8fd3290..c80469bfe90 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMServerUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/TestRMServerUtils.java @@ -18,16 +18,8 @@ package org.apache.hadoop.yarn.server.resourcemanager; -import org.apache.hadoop.yarn.api.records.NodeId; -import org.apache.hadoop.yarn.api.records.Priority; -import org.apache.hadoop.yarn.api.records.Resource; -import org.apache.hadoop.yarn.api.records.ResourceRequest; -import org.apache.hadoop.yarn.conf.YarnConfiguration; -import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; -import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; -import org.junit.Assert; -import org.junit.Test; -import org.mockito.Mockito; +import static org.apache.hadoop.yarn.api.records.ContainerUpdateType.INCREASE_RESOURCE; +import static org.apache.hadoop.yarn.server.resourcemanager.RMServerUtils.RESOURCE_OUTSIDE_ALLOWED_RANGE; import java.util.ArrayList; import java.util.Collections; @@ -37,7 +29,97 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.hadoop.yarn.api.protocolrecords.AllocateRequest; +import org.apache.hadoop.yarn.api.records.Container; +import org.apache.hadoop.yarn.api.records.ContainerId; +import org.apache.hadoop.yarn.api.records.NodeId; +import org.apache.hadoop.yarn.api.records.Priority; +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.ResourceRequest; +import org.apache.hadoop.yarn.api.records.UpdateContainerError; +import org.apache.hadoop.yarn.api.records.UpdateContainerRequest; +import org.apache.hadoop.yarn.api.records.impl.pb.UpdateContainerRequestPBImpl; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.event.Dispatcher; +import org.apache.hadoop.yarn.server.resourcemanager.nodelabels.RMNodeLabelsManager; +import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ContainerUpdates; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + public class TestRMServerUtils { + + @Test + public void testValidateAndSplitUpdateResourceRequests() { + List updateRequests = new ArrayList<>(); + int containerVersion = 10; + int resource = 10; + Resource maxAllocation = Resource.newInstance(resource, resource); + + UpdateContainerRequestPBImpl updateContainerRequestPBFail = + new UpdateContainerRequestPBImpl(); + updateContainerRequestPBFail.setContainerVersion(containerVersion); + updateContainerRequestPBFail + .setCapability(Resource.newInstance(resource + 1, resource + 1)); + updateContainerRequestPBFail + .setContainerId(Mockito.mock(ContainerId.class)); + + ContainerId containerIdOk = Mockito.mock(ContainerId.class); + Resource capabilityOk = Resource.newInstance(resource - 1, resource - 1); + UpdateContainerRequestPBImpl updateContainerRequestPBOk = + new UpdateContainerRequestPBImpl(); + updateContainerRequestPBOk.setContainerVersion(containerVersion); + updateContainerRequestPBOk.setCapability(capabilityOk); + updateContainerRequestPBOk.setContainerUpdateType(INCREASE_RESOURCE); + updateContainerRequestPBOk.setContainerId(containerIdOk); + + updateRequests.add(updateContainerRequestPBOk); + updateRequests.add(updateContainerRequestPBFail); + + Dispatcher dispatcher = Mockito.mock(Dispatcher.class); + RMContext rmContext = Mockito.mock(RMContext.class); + ResourceScheduler scheduler = Mockito.mock(ResourceScheduler.class); + + Mockito.when(rmContext.getScheduler()).thenReturn(scheduler); + Mockito.when(rmContext.getDispatcher()).thenReturn(dispatcher); + + RMContainer rmContainer = Mockito.mock(RMContainer.class); + Mockito.when(scheduler.getRMContainer(Mockito.any())) + .thenReturn(rmContainer); + Container container = Mockito.mock(Container.class); + Mockito.when(container.getVersion()).thenReturn(containerVersion); + Mockito.when(rmContainer.getContainer()).thenReturn(container); + Mockito.when(scheduler.getNormalizedResource(capabilityOk, maxAllocation)) + .thenReturn(capabilityOk); + + AllocateRequest allocateRequest = + AllocateRequest.newInstance(1, 0.5f, new ArrayList(), + new ArrayList(), updateRequests, null); + + List updateErrors = new ArrayList<>(); + ContainerUpdates containerUpdates = + RMServerUtils.validateAndSplitUpdateResourceRequests(rmContext, + allocateRequest, maxAllocation, updateErrors); + Assert.assertEquals(1, updateErrors.size()); + Assert.assertEquals(resource + 1, updateErrors.get(0) + .getUpdateContainerRequest().getCapability().getMemorySize()); + Assert.assertEquals(resource + 1, updateErrors.get(0) + .getUpdateContainerRequest().getCapability().getVirtualCores()); + Assert.assertEquals(RESOURCE_OUTSIDE_ALLOWED_RANGE, + updateErrors.get(0).getReason()); + + Assert.assertEquals(1, containerUpdates.getIncreaseRequests().size()); + UpdateContainerRequest increaseRequest = + containerUpdates.getIncreaseRequests().get(0); + Assert.assertEquals(capabilityOk.getVirtualCores(), + increaseRequest.getCapability().getVirtualCores()); + Assert.assertEquals(capabilityOk.getMemorySize(), + increaseRequest.getCapability().getMemorySize()); + Assert.assertEquals(containerIdOk, increaseRequest.getContainerId()); + } + @Test public void testGetApplicableNodeCountForAMLocality() throws Exception { List rack1Nodes = new ArrayList<>(); 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/TestSchedulerUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestSchedulerUtils.java index 2ec2de29ff7..696924203de 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestSchedulerUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/TestSchedulerUtils.java @@ -106,6 +106,7 @@ import org.junit.Test; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import org.junit.rules.ExpectedException; +import org.mockito.Mockito; public class TestSchedulerUtils { @@ -271,7 +272,7 @@ public class TestSchedulerUtils { public void testValidateResourceRequestWithErrorLabelsPermission() throws IOException { // mock queue and scheduler - YarnScheduler scheduler = mock(YarnScheduler.class); + ResourceScheduler scheduler = mock(ResourceScheduler.class); Set queueAccessibleNodeLabels = Sets.newHashSet(); QueueInfo queueInfo = mock(QueueInfo.class); when(queueInfo.getQueueName()).thenReturn("queue"); @@ -280,6 +281,8 @@ public class TestSchedulerUtils { when(scheduler.getQueueInfo(any(String.class), anyBoolean(), anyBoolean())) .thenReturn(queueInfo); + when(rmContext.getScheduler()).thenReturn(scheduler); + Resource maxResource = Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); @@ -298,20 +301,20 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression("y"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(""); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(" "); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when request labels is a subset of queue labels"); @@ -332,8 +335,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { @@ -354,8 +357,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("z"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { @@ -379,8 +382,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x && y"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { @@ -399,16 +402,16 @@ public class TestSchedulerUtils { YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(""); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression(" "); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when request labels is empty"); @@ -428,8 +431,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidLabelResourceRequestException e) { invalidlabelexception = true; @@ -456,16 +459,16 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression("y"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); resReq.setNodeLabelExpression("z"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { e.printStackTrace(); fail("Should be valid when queue can access any labels"); @@ -486,8 +489,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } @@ -507,8 +510,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), "rack", resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { @@ -532,8 +535,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), "rack", resource, 1); resReq.setNodeLabelExpression("x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { } finally { @@ -545,8 +548,8 @@ public class TestSchedulerUtils { YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq1 = BuilderUtils .newResourceRequest(mock(Priority.class), "*", resource, 1, "x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq1, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq1, "queue", + scheduler, rmContext, maxResource); fail("Should fail"); } catch (InvalidResourceRequestException e) { assertEquals("Invalid label resource request, cluster do not contain , " @@ -560,8 +563,8 @@ public class TestSchedulerUtils { YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq1 = BuilderUtils .newResourceRequest(mock(Priority.class), "*", resource, 1, "x"); - SchedulerUtils.normalizeAndvalidateRequest(resReq1, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq1, "queue", + scheduler, rmContext, maxResource); Assert.assertEquals(RMNodeLabelsManager.NO_LABEL, resReq1.getNodeLabelExpression()); } catch (InvalidResourceRequestException e) { @@ -571,14 +574,21 @@ public class TestSchedulerUtils { } @Test(timeout = 30000) - public void testValidateResourceRequest() { - YarnScheduler mockScheduler = mock(YarnScheduler.class); + public void testValidateResourceRequest() throws IOException { + ResourceScheduler mockScheduler = mock(ResourceScheduler.class); + + QueueInfo queueInfo = mock(QueueInfo.class); + when(queueInfo.getQueueName()).thenReturn("queue"); Resource maxResource = Resources.createResource( YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); + when(rmContext.getScheduler()).thenReturn(mockScheduler); + when(mockScheduler.getQueueInfo(Mockito.anyString(), Mockito.anyBoolean(), + Mockito.anyBoolean())).thenReturn(queueInfo); + // zero memory try { Resource resource = @@ -587,8 +597,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { fail("Zero memory should be accepted"); } @@ -601,8 +611,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { fail("Zero vcores should be accepted"); } @@ -616,8 +626,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { fail("Max memory should be accepted"); } @@ -631,8 +641,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); } catch (InvalidResourceRequestException e) { fail("Max vcores should not be accepted"); } @@ -645,8 +655,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); fail("Negative memory should not be accepted"); } catch (InvalidResourceRequestException e) { assertEquals(LESS_THAN_ZERO, e.getInvalidResourceType()); @@ -660,8 +670,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); fail("Negative vcores should not be accepted"); } catch (InvalidResourceRequestException e) { assertEquals(LESS_THAN_ZERO, e.getInvalidResourceType()); @@ -676,8 +686,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); fail("More than max memory should not be accepted"); } catch (InvalidResourceRequestException e) { assertEquals(GREATER_THEN_MAX_ALLOCATION, e.getInvalidResourceType()); @@ -691,8 +701,8 @@ public class TestSchedulerUtils { ResourceRequest resReq = BuilderUtils.newResourceRequest(mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, null, - mockScheduler, rmContext); + normalizeAndvalidateRequest(resReq, null, + mockScheduler, rmContext, maxResource); fail("More than max vcores should not be accepted"); } catch (InvalidResourceRequestException e) { assertEquals(GREATER_THEN_MAX_ALLOCATION, e.getInvalidResourceType()); @@ -805,7 +815,7 @@ public class TestSchedulerUtils { public void testNormalizeNodeLabelExpression() throws IOException { // mock queue and scheduler - YarnScheduler scheduler = mock(YarnScheduler.class); + ResourceScheduler scheduler = mock(ResourceScheduler.class); Set queueAccessibleNodeLabels = Sets.newHashSet(); QueueInfo queueInfo = mock(QueueInfo.class); when(queueInfo.getQueueName()).thenReturn("queue"); @@ -818,6 +828,8 @@ public class TestSchedulerUtils { YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES); + when(rmContext.getScheduler()).thenReturn(scheduler); + // queue has labels, success cases try { // set queue accessible node labels to [x, y] @@ -831,13 +843,13 @@ public class TestSchedulerUtils { YarnConfiguration.DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES); ResourceRequest resReq = BuilderUtils.newResourceRequest( mock(Priority.class), ResourceRequest.ANY, resource, 1); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); Assert.assertEquals("x", resReq.getNodeLabelExpression()); resReq.setNodeLabelExpression(" y "); - SchedulerUtils.normalizeAndvalidateRequest(resReq, maxResource, "queue", - scheduler, rmContext); + normalizeAndvalidateRequest(resReq, "queue", + scheduler, rmContext, maxResource); Assert.assertEquals("y", resReq.getNodeLabelExpression()); } catch (InvalidResourceRequestException e) { e.printStackTrace(); @@ -1033,6 +1045,14 @@ public class TestSchedulerUtils { return rmContext; } + private static void normalizeAndvalidateRequest(ResourceRequest resReq, + String queueName, YarnScheduler scheduler, RMContext rmContext, + Resource maxAllocation) + throws InvalidResourceRequestException { + SchedulerUtils.normalizeAndValidateRequest(resReq, maxAllocation, queueName, + scheduler, rmContext, null); + } + private static class InvalidResourceRequestExceptionMessageGenerator { private StringBuilder sb; 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 e77d8e21264..f32b467c49e 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 @@ -18,6 +18,8 @@ package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.MAXIMUM_ALLOCATION_MB; +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration.MAXIMUM_ALLOCATION_VCORES; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -835,6 +837,41 @@ public class TestCapacityScheduler extends CapacitySchedulerTestBase { assertEquals(CapacitySchedulerConfiguration.MAXIMUM_CAPACITY_VALUE,conf.getNonLabeledQueueMaximumCapacity(A),delta); } + @Test + public void testQueueMaximumAllocations() { + CapacityScheduler scheduler = new CapacityScheduler(); + scheduler.setConf(new YarnConfiguration()); + scheduler.setRMContext(resourceManager.getRMContext()); + CapacitySchedulerConfiguration conf = new CapacitySchedulerConfiguration(); + + setupQueueConfiguration(conf); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(A1) + + MAXIMUM_ALLOCATION_MB, "1024"); + conf.set(CapacitySchedulerConfiguration.getQueuePrefix(A1) + + MAXIMUM_ALLOCATION_VCORES, "1"); + + scheduler.init(conf); + scheduler.start(); + + Resource maxAllocationForQueue = + scheduler.getMaximumResourceCapability("a1"); + Resource maxAllocation1 = scheduler.getMaximumResourceCapability(""); + Resource maxAllocation2 = scheduler.getMaximumResourceCapability(null); + Resource maxAllocation3 = scheduler.getMaximumResourceCapability(); + + Assert.assertEquals(maxAllocation1, maxAllocation2); + Assert.assertEquals(maxAllocation1, maxAllocation3); + Assert.assertEquals( + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, + maxAllocation1.getMemorySize()); + Assert.assertEquals( + YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, + maxAllocation1.getVirtualCores()); + + Assert.assertEquals(1024, maxAllocationForQueue.getMemorySize()); + Assert.assertEquals(1, maxAllocationForQueue.getVirtualCores()); + } + @Test public void testRefreshQueues() throws Exception { @@ -4009,7 +4046,7 @@ public class TestCapacityScheduler extends CapacitySchedulerTestBase { private void setMaxAllocMb(CapacitySchedulerConfiguration conf, String queueName, int maxAllocMb) { String propName = CapacitySchedulerConfiguration.getQueuePrefix(queueName) - + CapacitySchedulerConfiguration.MAXIMUM_ALLOCATION_MB; + + MAXIMUM_ALLOCATION_MB; conf.setInt(propName, maxAllocMb); } 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/fair/FairSchedulerTestBase.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FairSchedulerTestBase.java index 3ac3849cf73..4f1f20b942b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FairSchedulerTestBase.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FairSchedulerTestBase.java @@ -77,6 +77,7 @@ public class FairSchedulerTestBase { public static final float TEST_RESERVATION_THRESHOLD = 0.09f; private static final int SLEEP_DURATION = 10; private static final int SLEEP_RETRIES = 1000; + protected static final int RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE = 10240; final static ContainerUpdates NULL_UPDATE_REQUESTS = new ContainerUpdates(); @@ -93,7 +94,8 @@ public class FairSchedulerTestBase { conf.setInt(YarnConfiguration.RM_SCHEDULER_MINIMUM_ALLOCATION_MB, 0); conf.setInt(FairSchedulerConfiguration.RM_SCHEDULER_INCREMENT_ALLOCATION_MB, 1024); - conf.setInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, 10240); + conf.setInt(YarnConfiguration.RM_SCHEDULER_MAXIMUM_ALLOCATION_MB, + RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE); conf.setBoolean(FairSchedulerConfiguration.ASSIGN_MULTIPLE, false); conf.setLong(FairSchedulerConfiguration.UPDATE_INTERVAL_MS, 10); conf.setFloat(FairSchedulerConfiguration.PREEMPTION_THRESHOLD, 0f); 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/fair/TestAllocationFileLoaderService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestAllocationFileLoaderService.java index 50a003ecd11..ac30b237472 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestAllocationFileLoaderService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestAllocationFileLoaderService.java @@ -23,6 +23,7 @@ import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.UnsupportedFileSystemException; import org.apache.hadoop.hdfs.MiniDFSCluster; +import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.reservation.ReservationSchedulerConfiguration; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.QueuePlacementRule.NestedUserQueue; @@ -30,7 +31,9 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.allocationfi import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.policies.DominantResourceFairnessPolicy; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.policies.FairSharePolicy; import org.apache.hadoop.yarn.util.ControlledClock; +import org.apache.hadoop.yarn.util.resource.ResourceUtils; import org.apache.hadoop.yarn.util.resource.Resources; +import org.apache.hadoop.yarn.util.resource.TestResourceUtils; import org.junit.Test; import java.io.File; import java.io.FileOutputStream; @@ -41,6 +44,7 @@ import java.io.PrintWriter; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -53,6 +57,8 @@ import static org.junit.Assert.fail; public class TestAllocationFileLoaderService { + private static final String A_CUSTOM_RESOURCE = "a-custom-resource"; + final static String TEST_DIR = new File(System.getProperty("test.build.data", "/tmp")).getAbsolutePath(); @@ -203,7 +209,8 @@ public class TestAllocationFileLoaderService { @Test public void testAllocationFileParsing() throws Exception { - Configuration conf = new Configuration(); + Configuration conf = new YarnConfiguration(); + TestResourceUtils.addNewTypesToResources(A_CUSTOM_RESOURCE); conf.set(FairSchedulerConfiguration.ALLOCATION_FILE, ALLOC_FILE); AllocationFileLoaderService allocLoader = new AllocationFileLoaderService(); @@ -246,6 +253,8 @@ public class TestAllocationFileLoaderService { .fairSharePreemptionTimeout(120) .minSharePreemptionTimeout(50) .fairSharePreemptionThreshold(0.6) + .maxContainerAllocation( + "vcores=16, memory-mb=512, " + A_CUSTOM_RESOURCE + "=10") // Create hierarchical queues G,H, with different min/fair // share preemption timeouts and preemption thresholds. // Also add a child default to make sure it doesn't impact queue H. @@ -253,6 +262,7 @@ public class TestAllocationFileLoaderService { .fairSharePreemptionTimeout(180) .minSharePreemptionTimeout(40) .fairSharePreemptionThreshold(0.7) + .maxContainerAllocation("1024mb,8vcores") .buildSubQueue() .buildQueue() // Set default limit of apps per queue to 15 @@ -286,8 +296,6 @@ public class TestAllocationFileLoaderService { assertEquals(6, queueConf.getConfiguredQueues().get(FSQueueType.LEAF).size()); assertEquals(Resources.createResource(0), queueConf.getMinResources("root." + YarnConfiguration.DEFAULT_QUEUE_NAME)); - assertEquals(Resources.createResource(0), - queueConf.getMinResources("root." + YarnConfiguration.DEFAULT_QUEUE_NAME)); assertEquals(Resources.createResource(2048, 10), queueConf.getMaxResources("root.queueA").getResource()); @@ -358,6 +366,29 @@ public class TestAllocationFileLoaderService { assertEquals(.4f, queueConf.getQueueMaxAMShare("root.queueD"), 0.01); assertEquals(.5f, queueConf.getQueueMaxAMShare("root.queueE"), 0.01); + Resource expectedResourceWithCustomType = Resources.createResource(512, 16); + expectedResourceWithCustomType.setResourceValue(A_CUSTOM_RESOURCE, 10); + + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation( + "root." + YarnConfiguration.DEFAULT_QUEUE_NAME)); + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation("root.queueA")); + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation("root.queueB")); + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation("root.queueC")); + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation("root.queueD")); + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation("root.queueE")); + assertEquals(Resources.unbounded(), + queueConf.getQueueMaxContainerAllocation("root.queueF")); + assertEquals(expectedResourceWithCustomType, + queueConf.getQueueMaxContainerAllocation("root.queueG")); + assertEquals(Resources.createResource(1024, 8), + queueConf.getQueueMaxContainerAllocation("root.queueG.queueH")); + assertEquals(120000, queueConf.getMinSharePreemptionTimeout("root")); assertEquals(-1, queueConf.getMinSharePreemptionTimeout("root." + YarnConfiguration.DEFAULT_QUEUE_NAME)); 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/fair/TestApplicationMasterServiceWithFS.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestApplicationMasterServiceWithFS.java new file mode 100644 index 00000000000..2416d8cf9e5 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestApplicationMasterServiceWithFS.java @@ -0,0 +1,174 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.test.GenericTestUtils; +import org.apache.hadoop.yarn.api.protocolrecords.AllocateResponse; +import org.apache.hadoop.yarn.api.records.Container; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.exceptions.InvalidResourceRequestException; +import org.apache.hadoop.yarn.server.resourcemanager.MockAM; +import org.apache.hadoop.yarn.server.resourcemanager.MockNM; +import org.apache.hadoop.yarn.server.resourcemanager.MockRM; +import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMApp; +import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttempt; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test Application master service using Fair scheduler. + */ +public class TestApplicationMasterServiceWithFS { + + private static final Log LOG = + LogFactory.getLog(TestApplicationMasterServiceWithFS.class); + + private static final int GB = 1024; + private static final int MEMORY_ALLOCATION = 3 * GB; + private static final String TEST_FOLDER = "test-queues"; + private AllocateResponse allocateResponse; + private static YarnConfiguration configuration; + + @BeforeClass + public static void setup() throws IOException { + String allocFile = + GenericTestUtils.getTestDir(TEST_FOLDER).getAbsolutePath(); + + configuration = new YarnConfiguration(); + configuration.setClass(YarnConfiguration.RM_SCHEDULER, FairScheduler.class, + ResourceScheduler.class); + configuration.set(FairSchedulerConfiguration.ALLOCATION_FILE, allocFile); + + PrintWriter out = new PrintWriter(new FileWriter(allocFile)); + out.println(""); + out.println(""); + out.println(" "); + out.println( + " 2048 mb 1 vcores"); + out.println(" "); + out.println(" "); + out.println( + " 3072 mb 1 vcores"); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(""); + out.close(); + } + + @AfterClass + public static void teardown(){ + File allocFile = GenericTestUtils.getTestDir(TEST_FOLDER); + allocFile.delete(); + } + + @Test(timeout = 3000000) + public void testQueueLevelContainerAllocationFail() throws Exception { + MockRM rm = new MockRM(configuration); + rm.start(); + + // Register node1 + MockNM nm1 = rm.registerNode("127.0.0.1:1234", 6 * GB); + + // Submit an application + RMApp app1 = rm.submitApp(2 * GB, "queueA"); + + // kick the scheduling + nm1.nodeHeartbeat(true); + RMAppAttempt attempt1 = app1.getCurrentAppAttempt(); + MockAM am1 = rm.sendAMLaunched(attempt1.getAppAttemptId()); + am1.registerAppAttempt(); + + am1.addRequests(new String[] { "127.0.0.1" }, MEMORY_ALLOCATION, 1, 1); + try { + allocateResponse = am1.schedule(); // send the request + Assert.fail(); + } catch (Exception e) { + Assert.assertTrue(e instanceof InvalidResourceRequestException); + Assert.assertEquals( + InvalidResourceRequestException.InvalidResourceType.GREATER_THEN_MAX_ALLOCATION, + ((InvalidResourceRequestException) e).getInvalidResourceType()); + + } finally { + rm.stop(); + } + } + + @Test(timeout = 3000000) + public void testQueueLevelContainerAllocationSuccess() throws Exception { + testFairSchedulerContainerAllocationSuccess("queueB"); + } + + @Test(timeout = 3000000) + public void testSchedulerLevelContainerAllocationSuccess() throws Exception { + testFairSchedulerContainerAllocationSuccess("queueC"); + } + + private void testFairSchedulerContainerAllocationSuccess(String queueName) + throws Exception { + MockRM rm = new MockRM(configuration); + rm.start(); + + // Register node1 + MockNM nm1 = rm.registerNode("127.0.0.1:1234", 6 * GB); + + // Submit an application + RMApp app1 = rm.submitApp(2 * GB, queueName); + + // kick the scheduling + nm1.nodeHeartbeat(true); + RMAppAttempt attempt1 = app1.getCurrentAppAttempt(); + MockAM am1 = rm.sendAMLaunched(attempt1.getAppAttemptId()); + am1.registerAppAttempt(); + + am1.addRequests(new String[] { "127.0.0.1" }, MEMORY_ALLOCATION, 1, 1); + + allocateResponse = am1.schedule(); // send the request + ((FairScheduler) rm.getResourceScheduler()).update(); + + // kick the scheduler + nm1.nodeHeartbeat(true); + GenericTestUtils.waitFor(() -> { + LOG.info("Waiting for containers to be created for app 1"); + try { + allocateResponse = am1.schedule(); + } catch (Exception e) { + Assert.fail("Allocation should be successful"); + } + return allocateResponse.getAllocatedContainers().size() > 0; + }, 1000, 10000); + + Container allocatedContainer = + allocateResponse.getAllocatedContainers().get(0); + Assert.assertEquals(MEMORY_ALLOCATION, + allocatedContainer.getResource().getMemorySize()); + Assert.assertEquals(1, allocatedContainer.getResource().getVirtualCores()); + rm.stop(); + } +} 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/fair/TestFairScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestFairScheduler.java index 9120d3a6cc1..dbf4b2bf91b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestFairScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/TestFairScheduler.java @@ -18,6 +18,7 @@ package org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair; +import static org.apache.hadoop.yarn.conf.YarnConfiguration.DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -198,8 +199,6 @@ public class TestFairScheduler extends FairSchedulerTestBase { } } - // TESTS - @SuppressWarnings("deprecation") @Test(timeout=2000) public void testLoadConfigurationOnInitialize() throws IOException { @@ -338,6 +337,111 @@ public class TestFairScheduler extends FairSchedulerTestBase { } } + @Test + public void testQueueMaximumCapacityAllocations() throws IOException { + conf.set(FairSchedulerConfiguration.ALLOCATION_FILE, ALLOC_FILE); + + int tooHighQueueAllocation = RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE +1; + + PrintWriter out = new PrintWriter(new FileWriter(ALLOC_FILE)); + out.println(""); + out.println(""); + out.println(" "); + out.println( + " 512 mb 1 vcores"); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(" "); + out.println( + " 2048 mb 3 vcores"); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(" " + tooHighQueueAllocation + + " mb 1 vcores"); + out.println(" "); + out.println(""); + out.close(); + + scheduler.init(conf); + + Assert.assertEquals(1, scheduler.getMaximumResourceCapability("root.queueA") + .getVirtualCores()); + Assert.assertEquals(512, + scheduler.getMaximumResourceCapability("root.queueA").getMemorySize()); + + Assert.assertEquals(DEFAULT_RM_SCHEDULER_MAXIMUM_ALLOCATION_VCORES, + scheduler.getMaximumResourceCapability("root.queueB") + .getVirtualCores()); + Assert.assertEquals(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, + scheduler.getMaximumResourceCapability("root.queueB").getMemorySize()); + + Assert.assertEquals(3, scheduler.getMaximumResourceCapability("root.queueC") + .getVirtualCores()); + Assert.assertEquals(2048, + scheduler.getMaximumResourceCapability("root.queueC").getMemorySize()); + + Assert.assertEquals(3, scheduler + .getMaximumResourceCapability("root.queueC.queueD").getVirtualCores()); + Assert.assertEquals(2048, scheduler + .getMaximumResourceCapability("root.queueC.queueD").getMemorySize()); + + Assert.assertEquals(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, scheduler + .getMaximumResourceCapability("root.queueE").getMemorySize()); + } + + @Test + public void testNormalizationUsingQueueMaximumAllocation() + throws IOException { + + int queueMaxAllocation = 4096; + conf.set(FairSchedulerConfiguration.ALLOCATION_FILE, ALLOC_FILE); + + PrintWriter out = new PrintWriter(new FileWriter(ALLOC_FILE)); + out.println(""); + out.println(""); + out.println(" "); + out.println(" " + queueMaxAllocation + + " mb 1 vcores" + ""); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(""); + out.close(); + + scheduler.init(conf); + scheduler.start(); + scheduler.reinitialize(conf, resourceManager.getRMContext()); + + allocateAppAttempt("root.queueA", 1, queueMaxAllocation + 1024); + allocateAppAttempt("root.queueB", 2, + RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE + 1024); + + scheduler.update(); + FSQueue queueToCheckA = scheduler.getQueueManager().getQueue("root.queueA"); + FSQueue queueToCheckB = scheduler.getQueueManager().getQueue("root.queueB"); + + assertEquals(queueMaxAllocation, queueToCheckA.getDemand().getMemorySize()); + assertEquals(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, + queueToCheckB.getDemand().getMemorySize()); + } + + private void allocateAppAttempt(String queueName, int id, int memorySize) { + ApplicationAttemptId id11 = createAppAttemptId(id, id); + createMockRMApp(id11); + scheduler.addApplication(id11.getApplicationId(), queueName, "user1", + false); + scheduler.addApplicationAttempt(id11, false, false); + List ask1 = new ArrayList(); + ResourceRequest request1 = + createResourceRequest(memorySize, ResourceRequest.ANY, 1, 1, true); + ask1.add(request1); + scheduler.allocate(id11, ask1, null, new ArrayList(), null, + null, NULL_UPDATE_REQUESTS); + } + /** * Test fair shares when max resources are set but are too high to impact * the shares. @@ -1316,8 +1420,9 @@ public class TestFairScheduler extends FairSchedulerTestBase { // New node satisfies resource request scheduler.update(); scheduler.handle(new NodeUpdateSchedulerEvent(node4)); - assertEquals(10240, scheduler.getQueueManager().getQueue("queue1"). - getResourceUsage().getMemorySize()); + assertEquals(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, + scheduler.getQueueManager().getQueue("queue1").getResourceUsage() + .getMemorySize()); scheduler.handle(new NodeUpdateSchedulerEvent(node1)); scheduler.handle(new NodeUpdateSchedulerEvent(node2)); @@ -4099,12 +4204,12 @@ public class TestFairScheduler extends FairSchedulerTestBase { scheduler.start(); scheduler.reinitialize(conf, resourceManager.getRMContext()); - RMNode node1 = - MockNodes.newNodeInfo(1, Resources.createResource(10240, 10), - 1, "127.0.0.1"); - RMNode node2 = - MockNodes.newNodeInfo(1, Resources.createResource(10240, 10), - 2, "127.0.0.2"); + RMNode node1 = MockNodes.newNodeInfo(1, + Resources.createResource(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, 10), + 1, "127.0.0.1"); + RMNode node2 = MockNodes.newNodeInfo(1, + Resources.createResource(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, 10), + 2, "127.0.0.2"); RMNode node3 = MockNodes.newNodeInfo(1, Resources.createResource(5120, 5), 3, "127.0.0.3"); @@ -4122,10 +4227,12 @@ public class TestFairScheduler extends FairSchedulerTestBase { true); Resource amResource1 = Resource.newInstance(1024, 1); Resource amResource2 = Resource.newInstance(1024, 1); - Resource amResource3 = Resource.newInstance(10240, 1); + Resource amResource3 = + Resource.newInstance(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, 1); Resource amResource4 = Resource.newInstance(5120, 1); Resource amResource5 = Resource.newInstance(1024, 1); - Resource amResource6 = Resource.newInstance(10240, 1); + Resource amResource6 = + Resource.newInstance(RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, 1); Resource amResource7 = Resource.newInstance(1024, 1); Resource amResource8 = Resource.newInstance(1024, 1); int amPriority = RMAppAttemptImpl.AM_CONTAINER_PRIORITY.getPriority(); @@ -4159,7 +4266,8 @@ public class TestFairScheduler extends FairSchedulerTestBase { ApplicationAttemptId attId3 = createAppAttemptId(3, 1); createApplicationWithAMResource(attId3, "queue1", "user1", amResource3); - createSchedulingRequestExistingApplication(10240, 1, amPriority, attId3); + createSchedulingRequestExistingApplication( + RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, 1, amPriority, attId3); FSAppAttempt app3 = scheduler.getSchedulerApp(attId3); scheduler.update(); // app3 reserves a container on node1 because node1's available resource @@ -4233,7 +4341,8 @@ public class TestFairScheduler extends FairSchedulerTestBase { ApplicationAttemptId attId6 = createAppAttemptId(6, 1); createApplicationWithAMResource(attId6, "queue1", "user1", amResource6); - createSchedulingRequestExistingApplication(10240, 1, amPriority, attId6); + createSchedulingRequestExistingApplication( + RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, 1, amPriority, attId6); FSAppAttempt app6 = scheduler.getSchedulerApp(attId6); scheduler.update(); // app6 can't reserve a container on node1 because @@ -4322,7 +4431,8 @@ public class TestFairScheduler extends FairSchedulerTestBase { // app6 turns the reservation into an allocation on node2. scheduler.handle(updateE2); assertEquals("Application6's AM requests 10240 MB memory", - 10240, app6.getAMResource().getMemorySize()); + RM_SCHEDULER_MAXIMUM_ALLOCATION_MB_VALUE, + app6.getAMResource().getMemorySize()); assertEquals("Application6's AM should be running", 1, app6.getLiveContainers().size()); assertEquals("Queue1's AM resource usage should be 11264 MB memory", 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/fair/allocationfile/AllocationFileQueue.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueue.java index f1afe6979fc..db816130e0e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueue.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueue.java @@ -60,9 +60,11 @@ class AllocationFileQueue { () -> AllocationFileWriter .createNumberSupplier(properties.getFairSharePreemptionTimeout())); AllocationFileWriter.addIfPresent(pw, "fairSharePreemptionThreshold", + () -> AllocationFileWriter.createNumberSupplier( + properties.getFairSharePreemptionThreshold())); + AllocationFileWriter.addIfPresent(pw, "maxContainerAllocation", () -> AllocationFileWriter - .createNumberSupplier( - properties.getFairSharePreemptionThreshold())); + .createNumberSupplier(properties.getMaxContainerAllocation())); printEndTag(pw); pw.close(); return sw.toString(); 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/fair/allocationfile/AllocationFileQueueBuilder.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueueBuilder.java index a2faf1da318..176024e9b46 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueueBuilder.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueueBuilder.java @@ -94,6 +94,12 @@ public abstract class AllocationFileQueueBuilder { return this; } + public AllocationFileQueueBuilder maxContainerAllocation( + String maxContainerAllocation) { + this.queuePropertiesBuilder.maxContainerAllocation(maxContainerAllocation); + return this; + } + public AllocationFileQueueBuilder subQueue(String queueName) { if (this instanceof AllocationFileSimpleQueueBuilder) { return new AllocationFileSubQueueBuilder( 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/fair/allocationfile/AllocationFileQueueProperties.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueueProperties.java index 2c01144a152..0a0f330b88c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueueProperties.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/allocationfile/AllocationFileQueueProperties.java @@ -33,6 +33,7 @@ public class AllocationFileQueueProperties { private final String maxChildResources; private final Integer fairSharePreemptionTimeout; private final Double fairSharePreemptionThreshold; + private final String maxContainerAllocation; AllocationFileQueueProperties(Builder builder) { this.queueName = builder.queueName; @@ -48,6 +49,7 @@ public class AllocationFileQueueProperties { this.maxChildResources = builder.maxChildResources; this.fairSharePreemptionTimeout = builder.fairSharePreemptionTimeout; this.fairSharePreemptionThreshold = builder.fairSharePreemptionThreshold; + this.maxContainerAllocation = builder.maxContainerAllocation; } public String getQueueName() { @@ -102,6 +104,10 @@ public class AllocationFileQueueProperties { return fairSharePreemptionThreshold; } + public String getMaxContainerAllocation() { + return maxContainerAllocation; + } + /** * Builder class for {@link AllocationFileQueueProperties}. */ @@ -119,6 +125,7 @@ public class AllocationFileQueueProperties { private String maxChildResources; private Integer fairSharePreemptionTimeout; private Double fairSharePreemptionThreshold; + private String maxContainerAllocation; Builder() { } @@ -167,6 +174,11 @@ public class AllocationFileQueueProperties { return this; } + public Builder maxContainerAllocation(String maxContainerAllocation) { + this.maxContainerAllocation = maxContainerAllocation; + return this; + } + public Builder minSharePreemptionTimeout( Integer minSharePreemptionTimeout) { this.minSharePreemptionTimeout = minSharePreemptionTimeout; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/FairScheduler.md b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/FairScheduler.md index b5bcbf5c8e9..5f9e7796636 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/FairScheduler.md +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-site/src/site/markdown/FairScheduler.md @@ -90,6 +90,8 @@ The allocation file must be in XML format. The format contains five types of ele * **maxResources**: maximum resources a queue will allocated, expressed in the form of "X%", "X% cpu, Y% memory", "X mb, Y vcores", or "vcores=X, memory-mb=Y". The last form is required when specifying resources other than memory and CPU. In the last form, X and Y can either be a percentage or an integer resource value without units. In the latter case the units will be inferred from the default units configured for that resource. A queue will not be assigned a container that would put its aggregate usage over this limit. + * **maxContainerAllocation**: maximum resources a queue can allocate for a single container, expressed in the form of "X mb, Y vcores" or "vcores=X, memory-mb=Y". The latter form is required when specifying resources other than memory and CPU. If the property is not set it's value is inherited from a parent queue. It's default value is **yarn.scheduler.maximum-allocation-mb**. Cannot be higher than **maxResources**. This property is invalid for root queue. + * **maxChildResources**: maximum resources an ad hoc child queue will allocated, expressed in the form of "X%", "X% cpu, Y% memory", "X mb, Y vcores", or "vcores=X, memory-mb=Y". The last form is required when specifying resources other than memory and CPU. In the last form, X and Y can either be a percentage or an integer resource value without units. In the latter case the units will be inferred from the default units configured for that resource. An ad hoc child queue will not be assigned a container that would put its aggregate usage over this limit. * **maxRunningApps**: limit the number of apps from the queue to run at once