diff --git 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 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 10f1e287f41..bcb917eeee6 100644 --- 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 +++ 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 @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -65,8 +66,10 @@ private Resource demand = Resources.createResource(0); - // Variables used for preemption - private long lastTimeAtMinShare; + // Last time this queue was at min share. + // Using {@link AtomicLong} to handle the race between updates and the read + // in {@link #dumpStateInternal} + private final AtomicLong lastTimeAtMinShare; // Track the AM resource usage for this queue private Resource amResourceUsage; @@ -78,7 +81,8 @@ public FSLeafQueue(String name, FairScheduler scheduler, super(name, scheduler, parent); this.scheduler = scheduler; this.context = scheduler.getContext(); - this.lastTimeAtMinShare = scheduler.getClock().getTime(); + this.lastTimeAtMinShare = + new AtomicLong(scheduler.getClock().getTime()); activeUsersManager = new ActiveUsersManager(getMetrics()); amResourceUsage = Resource.newInstance(0, 0); getMetrics().setAMResourceUsage(amResourceUsage); @@ -198,13 +202,10 @@ public void collectSchedulerApplications( } @Override - public void updateInternal(boolean checkStarvation) { + public void updateInternal() { readLock.lock(); try { policy.computeShares(runnableApps, getFairShare()); - if (checkStarvation) { - updateStarvedApps(); - } } finally { readLock.unlock(); } @@ -283,8 +284,10 @@ private void updateStarvedAppsMinshare( * If this queue is starving due to fairshare, there must be at least * one application that is starved. And, even if the queue is not * starved due to fairshare, there might still be starved applications. + * + * Caller does not need read/write lock on the leaf queue. */ - private void updateStarvedApps() { + void updateStarvedApps() { // Fetch apps with pending demand TreeSet appsWithDemand = fetchAppsWithDemand(false); @@ -431,7 +434,7 @@ public Resource assignContainer(FSSchedulerNode node) { } private void setLastTimeAtMinShare(long lastTimeAtMinShare) { - this.lastTimeAtMinShare = lastTimeAtMinShare; + this.lastTimeAtMinShare.set(lastTimeAtMinShare); } @Override @@ -578,7 +581,7 @@ private Resource minShareStarvation() { setLastTimeAtMinShare(now); } - if (now - lastTimeAtMinShare < getMinSharePreemptionTimeout()) { + if (now - lastTimeAtMinShare.get() < getMinSharePreemptionTimeout()) { // the queue is not starved for the preemption timeout starvation = Resources.clone(Resources.none()); } @@ -635,7 +638,7 @@ protected void dumpStateInternal(StringBuilder sb) { ", MaxAMShare: " + maxAMShare + ", MaxAMResource: " + computeMaxAMResource() + ", AMResourceUsage: " + getAmResourceUsage() + - ", LastTimeAtMinShare: " + lastTimeAtMinShare + + ", LastTimeAtMinShare: " + lastTimeAtMinShare.get() + "}"); } } diff --git 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 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 b062c586d01..e25f65515cd 100644 --- 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 +++ 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 @@ -79,13 +79,13 @@ void removeChildQueue(FSQueue child) { } @Override - public void updateInternal(boolean checkStarvation) { + public void updateInternal() { readLock.lock(); try { policy.computeShares(childQueues, getFairShare()); for (FSQueue childQueue : childQueues) { childQueue.getMetrics().setFairShare(childQueue.getFairShare()); - childQueue.updateInternal(checkStarvation); + childQueue.updateInternal(); } } finally { readLock.unlock(); diff --git 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 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 e1311407022..12b1b83f1a1 100644 --- 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 +++ 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 @@ -326,16 +326,23 @@ public boolean isPreemptable() { /** * Recomputes the shares for all child queues and applications based on this - * queue's current share, and checks for starvation. + * queue's current share. * - * @param checkStarvation whether to check for fairshare or minshare - * starvation on update + * To be called holding the scheduler writelock. */ - abstract void updateInternal(boolean checkStarvation); + abstract void updateInternal(); - public void update(Resource fairShare, boolean checkStarvation) { + /** + * Set the queue's fairshare and update the demand/fairshare of child + * queues/applications. + * + * To be called holding the scheduler writelock. + * + * @param fairShare + */ + public void update(Resource fairShare) { setFairShare(fairShare); - updateInternal(checkStarvation); + updateInternal(); } /** diff --git 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 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 d1a237ada97..f2e3279875a 100644 --- 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 +++ 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 @@ -366,28 +366,38 @@ private void dumpSchedulerState() { */ @VisibleForTesting public void update() { - try { - writeLock.lock(); - - FSQueue rootQueue = queueMgr.getRootQueue(); + FSQueue rootQueue = queueMgr.getRootQueue(); + // Update demands and fairshares + writeLock.lock(); + try { // Recursively update demands for all queues rootQueue.updateDemand(); - - Resource clusterResource = getClusterResource(); - rootQueue.update(clusterResource, shouldAttemptPreemption()); + rootQueue.update(getClusterResource()); // Update metrics updateRootQueueMetrics(); + } finally { + writeLock.unlock(); + } - if (LOG.isDebugEnabled()) { - if (--updatesToSkipForDebug < 0) { - updatesToSkipForDebug = UPDATE_DEBUG_FREQUENCY; - dumpSchedulerState(); + // Update starvation stats and identify starved applications + readLock.lock(); + try { + if (shouldAttemptPreemption()) { + for (FSLeafQueue queue : queueMgr.getLeafQueues()) { + queue.updateStarvedApps(); } } } finally { - writeLock.unlock(); + readLock.unlock(); + } + + if (LOG.isDebugEnabled()) { + if (--updatesToSkipForDebug < 0) { + updatesToSkipForDebug = UPDATE_DEBUG_FREQUENCY; + dumpSchedulerState(); + } } }