1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.yarn.server.resourcemanager.reservation;
20 
21 import java.util.Collection;
22 import java.util.List;
23 
24 import org.apache.hadoop.yarn.api.records.ReservationId;
25 import org.apache.hadoop.yarn.api.records.Resource;
26 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.Queue;
27 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler;
28 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FSLeafQueue;
29 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FSParentQueue;
30 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FSQueue;
31 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler;
32 import org.apache.hadoop.yarn.util.Clock;
33 import org.apache.hadoop.yarn.util.resource.Resources;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 
37 public class FairSchedulerPlanFollower extends AbstractSchedulerPlanFollower {
38   private static final Logger LOG = LoggerFactory
39       .getLogger(FairSchedulerPlanFollower.class);
40 
41   private FairScheduler fs;
42 
43   @Override
init(Clock clock, ResourceScheduler sched, Collection<Plan> plans)44   public void init(Clock clock, ResourceScheduler sched,
45       Collection<Plan> plans) {
46     super.init(clock, sched, plans);
47     fs = (FairScheduler)sched;
48     LOG.info("Initializing Plan Follower Policy:"
49         + this.getClass().getCanonicalName());
50   }
51 
52   @Override
getPlanQueue(String planQueueName)53   protected Queue getPlanQueue(String planQueueName) {
54     Queue planQueue = fs.getQueueManager().getParentQueue(planQueueName, false);
55     if (planQueue == null) {
56       LOG.error("The queue " + planQueueName + " cannot be found or is not a " +
57           "ParentQueue");
58     }
59     return planQueue;
60   }
61 
62   @Override
calculateReservationToPlanRatio(Resource clusterResources, Resource planResources, Resource capToAssign)63   protected float calculateReservationToPlanRatio(Resource clusterResources,
64       Resource planResources, Resource capToAssign) {
65     return Resources.divide(fs.getResourceCalculator(),
66         clusterResources, capToAssign, planResources);
67   }
68 
69   @Override
arePlanResourcesLessThanReservations(Resource clusterResources, Resource planResources, Resource reservedResources)70   protected boolean arePlanResourcesLessThanReservations(Resource
71       clusterResources, Resource planResources, Resource reservedResources) {
72     return Resources.greaterThan(fs.getResourceCalculator(),
73         clusterResources, reservedResources, planResources);
74   }
75 
76   @Override
getChildReservationQueues(Queue queue)77   protected List<? extends Queue> getChildReservationQueues(Queue queue) {
78     FSQueue planQueue = (FSQueue)queue;
79     List<FSQueue> childQueues = planQueue.getChildQueues();
80     return childQueues;
81   }
82 
83 
84   @Override
addReservationQueue(String planQueueName, Queue queue, String currResId)85   protected void addReservationQueue(String planQueueName, Queue queue,
86       String currResId) {
87     String leafQueueName = getReservationQueueName(planQueueName, currResId);
88     fs.getQueueManager().getLeafQueue(leafQueueName, true);
89   }
90 
91   @Override
createDefaultReservationQueue(String planQueueName, Queue queue, String defReservationId)92   protected void createDefaultReservationQueue(String planQueueName,
93       Queue queue, String defReservationId) {
94     String defReservationQueueName = getReservationQueueName(planQueueName,
95         defReservationId);
96     if (!fs.getQueueManager().exists(defReservationQueueName)) {
97       fs.getQueueManager().getLeafQueue(defReservationQueueName, true);
98     }
99   }
100 
101   @Override
getPlanResources(Plan plan, Queue queue, Resource clusterResources)102   protected Resource getPlanResources(Plan plan, Queue queue,
103       Resource clusterResources) {
104     FSParentQueue planQueue = (FSParentQueue)queue;
105     Resource planResources = planQueue.getSteadyFairShare();
106     return planResources;
107   }
108 
109   @Override
getReservationQueueResourceIfExists(Plan plan, ReservationId reservationId)110   protected Resource getReservationQueueResourceIfExists(Plan plan,
111       ReservationId reservationId) {
112     String reservationQueueName = getReservationQueueName(plan.getQueueName(),
113         reservationId.toString());
114     FSLeafQueue reservationQueue =
115         fs.getQueueManager().getLeafQueue(reservationQueueName, false);
116     Resource reservationResource = null;
117     if (reservationQueue != null) {
118       reservationResource = reservationQueue.getSteadyFairShare();
119     }
120     return reservationResource;
121   }
122 
123   @Override
getReservationQueueName(String planQueueName, String reservationQueueName)124   protected String getReservationQueueName(String planQueueName,
125       String reservationQueueName) {
126     String planQueueNameFullPath = fs.getQueueManager().getQueue
127         (planQueueName).getName();
128 
129     if (!reservationQueueName.startsWith(planQueueNameFullPath)) {
130       // If name is not a path we need full path for FairScheduler. See
131       // YARN-2773 for the root cause
132       return planQueueNameFullPath + "." + reservationQueueName;
133     }
134     return reservationQueueName;
135   }
136 
137   @Override
getReservationIdFromQueueName(String resQueueName)138   protected String getReservationIdFromQueueName(String resQueueName) {
139     return resQueueName.substring(resQueueName.lastIndexOf(".") + 1);
140   }
141 }
142