commit 1360e42615f4dee7411fd73b7b390e3a7a10e02e Author: Wangda Tan Date: Sat Oct 1 13:05:13 2016 -0700 Added new Resource object avoid creating too many PBImpls 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/FinalResource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/FinalResource.java new file mode 100644 index 0000000..469dc7e --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/FinalResource.java @@ -0,0 +1,72 @@ +/** + * 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; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; + +public class FinalResource extends Resource { + private final long mem; + private final int virtualCores; + + public FinalResource(long mem, int virtualCores) { + this.mem = mem; + this.virtualCores = virtualCores; + } + + @Override + @Deprecated + public int getMemory() { + return (int) getMemorySize(); + } + + @Override + @Deprecated + public void setMemory(int memory) { + setMemorySize(memory); + } + + @Override + public long getMemorySize() { + return mem; + } + + @Override + public void setMemorySize(long memory) { + throw new YarnRuntimeException( + "Cannot set fields of " + FinalResource.class.getName()); + } + + @Override + public int getVirtualCores() { + return virtualCores; + } + + @Override + public void setVirtualCores(int vCores) { + throw new YarnRuntimeException( + "Cannot set fields of " + FinalResource.class.getName()); + } + + @Override + public int compareTo(Resource o) { + return 0; + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java index 5f326e0..fa45f6e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerApplicationAttempt.java @@ -1063,8 +1063,8 @@ public boolean hasPendingResourceRequest(ResourceCalculator rc, // To avoid too many allocation-proposals rejected for non-default // partition allocation if (StringUtils.equals(nodePartition, RMNodeLabelsManager.NO_LABEL)) { - pending = Resources.subtract(pending, Resources - .createResource(unconfirmedAllocatedMem.get(), + pending = SchedulerResourcesUtils.subtract(pending, + SchedulerResourcesUtils.createResource(unconfirmedAllocatedMem.get(), unconfirmedAllocatedVcores.get())); } 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/SchedulerResourcesUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerResourcesUtils.java new file mode 100644 index 0000000..7aa2d1b --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/SchedulerResourcesUtils.java @@ -0,0 +1,46 @@ +/** + * 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; + +import org.apache.hadoop.yarn.api.records.Resource; + +/** + * Similar to {@link org.apache.hadoop.yarn.util.resource.Resources}, but this + * is to avoid extra overhead introduced by -PBImpl + */ +public class SchedulerResourcesUtils { + public static Resource add(Resource lhs, Resource rhs) { + return createResource(lhs.getMemorySize() + rhs.getMemorySize(), + lhs.getVirtualCores() + rhs.getVirtualCores()); + } + + public static Resource clone(Resource res) { + return createResource(res.getMemorySize(), res.getVirtualCores()); + } + + public static Resource subtract(Resource lhs, Resource rhs) { + return createResource(lhs.getMemorySize() - rhs.getMemorySize(), + lhs.getVirtualCores() - rhs.getVirtualCores()); + } + + public static Resource createResource(long memory, int cores) { + return new FinalResource(memory, cores); + } +} 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/AbstractCSQueue.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/AbstractCSQueue.java index 5f6c168..7514f5e 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/AbstractCSQueue.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/AbstractCSQueue.java @@ -50,6 +50,7 @@ import org.apache.hadoop.yarn.security.YarnAuthorizationProvider; 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.SchedulerResourcesUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.activities.ActivitiesManager; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceLimits; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceUsage; @@ -788,7 +789,8 @@ public boolean accept(Resource cluster, schedulerContainer.getNodePartition(), cluster); } if (!Resources.fitsIn(resourceCalculator, cluster, - Resources.add(queueUsage.getUsed(partition), netAllocated), + SchedulerResourcesUtils + .add(queueUsage.getUsed(partition), netAllocated), maxResourceLimit)) { if (LOG.isDebugEnabled()) { LOG.debug("Used resource=" + queueUsage.getUsed(partition) 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 b9b44cd..864a609 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 @@ -110,6 +110,7 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerDynamicEditException; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerHealth; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerNode; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerResourcesUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.activities.ActivityDiagnosticConstant; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.activities.ActivitiesLogger; @@ -1476,7 +1477,7 @@ CSAssignment allocateContainersToNode(PlacementSet ps, // Try to schedule more if there are no reservations to fulfill if (node.getReservedContainer() == null) { - if (calculator.computeAvailableContainers(Resources + if (calculator.computeAvailableContainers(SchedulerResourcesUtils .add(node.getUnallocatedResource(), node.getTotalKillableResources()), minimumAllocation) > 0) { 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/LeafQueue.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/LeafQueue.java index c5e9361..722439f 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/LeafQueue.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/LeafQueue.java @@ -732,7 +732,7 @@ private void activateApplications() { amLimit = calculateAndGetAMResourceLimitPerPartition(partitionName); } // Check am resource limit. - Resource amIfStarted = Resources.add( + Resource amIfStarted = SchedulerResourcesUtils.add( application.getAMResource(partitionName), queueUsage.getAMUsed(partitionName)); @@ -774,7 +774,7 @@ private void activateApplications() { userAmPartitionLimit.put(partitionName, userAMLimit); } - Resource userAmIfStarted = Resources.add( + Resource userAmIfStarted = SchedulerResourcesUtils.add( application.getAMResource(partitionName), user.getConsumedAMResources(partitionName)); @@ -992,7 +992,7 @@ public CSAssignment assignContainers(Resource clusterResource, boolean canAssignWithoutReservation = super.canAssignToThisQueue( clusterResource, ps.getPartition(), currentResourceLimits, Resources.none(), schedulingMode); - Resource queueHeadroom = Resources.clone( + Resource queueHeadroom = SchedulerResourcesUtils.clone( currentResourceLimits.getHeadroom()); // Make sure we only calculate once for each user under each queue @@ -1424,9 +1424,10 @@ private Resource computeUserLimit(FiCaSchedulerApp application, Resource consumed = Resources.multiplyAndNormalizeUp(resourceCalculator, partitionResource, qUsageRatios.getUsageRatio(nodePartition), minimumAllocation); - Resource currentCapacity = - Resources.lessThan(resourceCalculator, partitionResource, consumed, - queueCapacity) ? queueCapacity : Resources.add(consumed, required); + Resource currentCapacity = Resources.lessThan(resourceCalculator, + partitionResource, consumed, queueCapacity) ? + queueCapacity : + SchedulerResourcesUtils.add(consumed, required); // Never allow a single user to take more than the // queue's configured capacity * user-limit-factor. // Also, the queue's configured capacity should be higher than 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/ParentQueue.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/ParentQueue.java index 5bdb793..736a5fc 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/ParentQueue.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/ParentQueue.java @@ -613,7 +613,7 @@ private boolean canAssign(Resource clusterResource, FiCaSchedulerNode node) { // 1) Node doesn't have reserved container // 2) Node's available-resource + killable-resource should > 0 return node.getReservedContainer() == null && Resources.greaterThanOrEqual( - resourceCalculator, clusterResource, Resources + resourceCalculator, clusterResource, SchedulerResourcesUtils .add(node.getUnallocatedResource(), node.getTotalKillableResources()), minimumAllocation); } @@ -632,7 +632,7 @@ private ResourceLimits getResourceLimitsOfChild(CSQueue child, getTotalKillableResource(nodePartition)); // Child's limit = parent-available-resource + child-used - Resource childLimit = Resources.add(parentMaxAvailableResource, + Resource childLimit = SchedulerResourcesUtils.add(parentMaxAvailableResource, child.getQueueResourceUsage().getUsed(nodePartition)); // Get child's max resource 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/allocator/RegularContainerAllocator.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/allocator/RegularContainerAllocator.java index b276356..2b33655 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/allocator/RegularContainerAllocator.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/allocator/RegularContainerAllocator.java @@ -40,6 +40,7 @@ import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceLimits; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerAppUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerRequestKey; +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerResourcesUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerUtils; import org.apache.hadoop.yarn.server.resourcemanager.scheduler.activities.ActivityDiagnosticConstant; @@ -87,9 +88,9 @@ private boolean checkHeadroom(Resource clusterResource, // allocation. resourceCouldBeUnReserved = Resources.none(); } - return Resources.greaterThanOrEqual(rc, clusterResource, Resources.add( - currentResourceLimits.getHeadroom(), resourceCouldBeUnReserved), - required); + return Resources.greaterThanOrEqual(rc, clusterResource, + SchedulerResourcesUtils.add(currentResourceLimits.getHeadroom(), + resourceCouldBeUnReserved), required); } private ContainerAllocation preCheckForPlacementSet(Resource clusterResource, @@ -536,7 +537,8 @@ private ContainerAllocation assignContainer(Resource clusterResource, // Check if we need to kill some containers to allocate this one List toKillContainers = null; - if (availableContainers == 0 && currentResoureLimits.isAllowPreemption()) { + if (availableContainers == 0 && currentResoureLimits.isAllowPreemption() + && !node.getKillableContainers().isEmpty()) { Resource availableAndKillable = Resources.clone(available); for (RMContainer killableContainer : node .getKillableContainers().values()) {