Package haizea :: Package core :: Package scheduler :: Module policy
[hide private]
[frames] | no frames]

Source Code for Module haizea.core.scheduler.policy

  1  # -------------------------------------------------------------------------- # 
  2  # Copyright 2006-2009, University of Chicago                                 # 
  3  # Copyright 2008-2009, Distributed Systems Architecture Group, Universidad   # 
  4  # Complutense de Madrid (dsa-research.org)                                   # 
  5  #                                                                            # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); you may    # 
  7  # not use this file except in compliance with the License. You may obtain    # 
  8  # 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  """Haizea uses a policy manager that allows certain scheduling decisions to 
 20  be delegated to pluggable policies. This is done so scheduling policies 
 21  can be (1) modified without having to modify core components of Haizea, and 
 22  (2) implemented by writing a single Python class that implements a given 
 23  interface for pluggable policies. 
 24   
 25  Three policies are currently pluggable: lease preemptability ("Can lease X 
 26  preempt lease Y?"), host selection ("I want to deploy a VM, what host should 
 27  I select for this?") and lease admission ("Should I accept/reject this lease 
 28  request?"). Haizea provides several simple policy modules in the 
 29  haizea.policies package. The policy to use is selected in the configuration 
 30  file. See the Haizea Documentation for more details on how this is done. 
 31   
 32  This module provides Haizea's policy manager and the base classes for 
 33  pluggable policies.   
 34  """ 
 35   
 36   
 37  from haizea.common.utils import abstract 
 38  from mx.DateTime import DateTimeDelta 
 39  import operator 
 40   
41 -class PolicyManager(object):
42 """The Policy Manager 43 44 This class manages the policy modules and provides methods to 45 access these modules. 46 47 """
48 - def __init__(self, admission, preemption, host_selection):
49 """Constructor 50 51 Expects fully-constructed policies (these are currently 52 loaded in the Manager class, based on the config file). 53 54 Arguments: 55 admission -- A child of LeaseAdmissionPolicy 56 preemption -- A child of PreemptabilityPolicy 57 host_selection -- A child of HostSelectionPolicy 58 59 """ 60 self.admission = admission 61 self.preemption = preemption 62 self.host_selection = host_selection
63
64 - def sort_leases(self, preemptor, preemptees, time):
65 """Sorts a list of leases by their preemptability 66 67 Takes a list of leases (the "preemptees"), determines their preemptability 68 by another lease (the "preemptor"), and returns a list with the 69 leases sorted by decreasing preemptability score (most preemptable 70 leases first) 71 72 See documentation of PreemptabilityPolicy.get_lease_preemptability_score 73 for more details on the preemptability score. 74 75 Argument 76 preemptor -- Preemptor lease 77 preemptees -- List of preemptee leases 78 time -- Time at which preemption would take place 79 """ 80 leases_score = [(preemptee, self.get_lease_preemptability_score(preemptor,preemptee, time)) for preemptee in preemptees] 81 leases_score = [(preemptee,score) for preemptee,score in leases_score if score != -1] 82 leases_score.sort(key=operator.itemgetter(1), reverse=True) 83 return [preemptee for preemptee,score in leases_score]
84 85
86 - def sort_hosts(self, nodes, time, lease):
87 """Sorts a list of hosts by their score 88 89 Takes a list of hosts, determines their score, and sorts them in 90 order of decreasing score (most desireable hosts first) 91 92 See documentation of HostSelectionPolicy.get_host_score for more details. 93 94 Arguments: 95 nodes -- List of physical node (the integer identifier used in the slot table) 96 time -- Time at which the lease might be scheduled 97 lease -- Lease that is being scheduled. 98 """ 99 nodes_score = [(node, self.get_host_score(node, time, lease)) for node in nodes] 100 nodes_score.sort(key=operator.itemgetter(1), reverse=True) 101 return [node for node,score in nodes_score]
102 103
104 - def accept_lease(self, lease):
105 """Lease admission function 106 107 Returns True if the lease can be accepted, False if it should be rejected. 108 109 Argument 110 lease -- Lease request 111 """ 112 return self.admission.accept_lease(lease)
113 114
115 - def get_lease_preemptability_score(self, preemptor, preemptee, time):
116 """Computes the lease preemptability score 117 118 See documentation of PreemptabilityPolicy.get_lease_preemptability_score 119 for more details. 120 121 Arguments: 122 preemptor -- Preemptor lease 123 preemptee -- Preemptee lease 124 time -- Time at which preemption would take place 125 """ 126 return self.preemption.get_lease_preemptability_score(preemptor, preemptee, time)
127 128
129 - def get_host_score(self, node, time, lease):
130 """Computes the score of a host 131 132 See documentation of HostSelectionPolicy.get_host_score for more details. 133 134 Arguments: 135 node -- Physical node (the integer identifier used in the slot table) 136 time -- Time at which the lease might be scheduled 137 lease -- Lease that is being scheduled. 138 """ 139 return self.host_selection.get_host_score(node, time, lease)
140 141 142
143 -class LeaseAdmissionPolicy(object):
144 """Lease Admission policy 145 146 This is the parent class of lease admission policies. A lease admission 147 policy determines whether a given lease request should be accepted or not 148 by Haizea. Note that this is distinct from whether the lease can be 149 scheduled or not (although this could certainly be a part of the 150 policy); the policy simply decides whether the lease can be considered for 151 scheduling or not. For example, a user could submit an AR lease that must 152 start in 5 hours, but the policy could dictate that all ARs must be notified 153 at least 24 hours in advance (and the lease would be rejected, regardless of 154 whether there was resources available for it in 5 hours). Similarly, an 155 AR lease could be requested 48 hours in advance, be accepted by the lease 156 admission policy, but then be rejected by the scheduler if there are no 157 resources available. 158 159 """
160 - def __init__(self, slottable):
161 """Constructor 162 163 Argument 164 slottable -- A fully constructed SlotTable 165 """ 166 self.slottable = slottable
167 168
169 - def accept_lease(self, lease):
170 """Lease admission function 171 172 Returns True if the lease can be accepted, False if it should be rejected. 173 174 Argument 175 lease -- Lease request 176 """ 177 abstract()
178 179 180
181 -class PreemptabilityPolicy(object):
182 """Lease Preemptability policy 183 184 This is the parent class of lease preemptability policies. This type of 185 policy is used to determine whether a lease can be preempted by another 186 lease at a given time. However, the policy doesn't return True or False but, 187 rather, a "preemptability score" (see get_lease_preemptability_score for 188 more details) 189 190 """
191 - def __init__(self, slottable):
192 """Constructor 193 194 Argument 195 slottable -- A fully constructed SlotTable 196 """ 197 self.slottable = slottable
198 199
200 - def get_lease_preemptability_score(self, preemptor, preemptee, time):
201 """Computes the lease preemptability score 202 203 Given a lease that needs to preempt resources (the "preemptor"), 204 another lease (the "preemptee") that may be preempted by it, and a time, 205 this method determines the preemptability score of the preemptee or 206 "how preemptable is the preemptee by the preemptor at the given time". 207 The score can be the following: 208 209 -1 : Cannot be preempted under any circumstances 210 0.0 <= x <= 1.0: Lease can be preempted. The higher the score, 211 the "more preemptable" it is (this is a relative measure; the score 212 should be used to determine which of several leases is a better 213 candidate for preemption) 214 215 Arguments: 216 preemptor -- Preemptor lease 217 preemptee -- Preemptee lease 218 time -- Time at which preemption would take place 219 """ 220 abstract()
221 222
223 - def _get_aging_factor(self, lease, time):
224 """Returns an aging factor for the preemptability score 225 226 This is a convenience function that can be used to "age" a 227 preemptability score (allowing leases that have been submitted 228 long ago to avoid preemption). The method returns a factor 229 between 0 and 1 that can be multiplied by the score, reducing 230 the score based on the lease's "age". 231 232 Currently, this method uses a hard-coded horizon of 31 days 233 (any lease older than 7 days cannot be preempted, and leases 234 less than 7 days are assigned a factor proportional to their age) 235 236 Arguments: 237 lease -- Lease that is going to be preempted 238 time -- Time at which preemption would take place 239 """ 240 # TODO: Make horizon configurable 241 horizon = time - DateTimeDelta(7) 242 if lease.submit_time <= horizon: 243 return -1 244 else: 245 seconds = (time - lease.submit_time).seconds 246 horizon_seconds = DateTimeDelta(31).seconds 247 return float(horizon_seconds - seconds) / horizon_seconds
248 249
250 -class HostSelectionPolicy(object):
251 """Host Selection policy 252 253 This is the parent class of host selection policies. When mapping VMs 254 to physical hosts, this policy determines what hosts are more desireable. 255 For example, an energy-saving policy might value hosts that already have 256 VMs running (to leave as many empty machines as possible, which could then 257 be turned off), whereas another policy might prefer empty hosts to make 258 sure that VMs are spread out across nodes. 259 260 To do this, the policy will assign a score to each host. See the documentation 261 for get_host_score for more details. 262 263 """
264 - def __init__(self, slottable):
265 """Constructor 266 267 Argument 268 slottable -- A fully constructed SlotTable 269 """ 270 self.slottable = slottable
271 272
273 - def get_host_score(self, node, time, lease):
274 """Computes the score of a host 275 276 Given a physical host, a time, and a lease we would like to 277 schedule at that time, this method returns a score indicating 278 how desireable that host is for that lease at that time. 279 The score can be between 0.0 and 1.0. The higher the score, 280 the "more desireable" the physical host is (this is a relative measure; 281 the score should be used to determine which of several physical hosts 282 is more desireable for this lease). 283 284 Arguments: 285 node -- Physical node (the integer identifier used in the slot table) 286 time -- Time at which the lease might be scheduled 287 lease -- Lease that is being scheduled. 288 """ 289 abstract()
290