root / trunk / src / haizea / policies / host_selection.py @ 632
1 |
# -------------------------------------------------------------------------- #
|
---|---|
2 |
# Copyright 2006-2008, University of Chicago #
|
3 |
# Copyright 2008, 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 |
"""This module provides pluggable host selection policies. See the documentation
|
20 |
for haizea.core.schedule.policy.HostSelectionPolicy for more details on
|
21 |
host selection policies.
|
22 |
"""
|
23 |
|
24 |
from haizea.core.scheduler.policy import HostSelectionPolicy |
25 |
|
26 |
class NoPolicy(HostSelectionPolicy): |
27 |
"""A simple host selection policy: all hosts have the same score
|
28 |
"""
|
29 |
def __init__(self, slottable): |
30 |
"""Constructor
|
31 |
|
32 |
Argument
|
33 |
slottable -- A fully constructed SlotTable
|
34 |
"""
|
35 |
HostSelectionPolicy.__init__(self, slottable)
|
36 |
|
37 |
|
38 |
def get_host_score(self, node, time, lease): |
39 |
"""Computes the score of a host
|
40 |
|
41 |
See class documentation for details on what policy is implemented here.
|
42 |
See documentation of HostSelectionPolicy.get_host_score for more details
|
43 |
on this method.
|
44 |
|
45 |
Arguments:
|
46 |
node -- Physical node (the integer identifier used in the slot table)
|
47 |
time -- Time at which the lease might be scheduled
|
48 |
lease -- Lease that is being scheduled.
|
49 |
"""
|
50 |
return 1 |
51 |
|
52 |
|
53 |
|
54 |
class GreedyPolicy(HostSelectionPolicy): |
55 |
"""A greedy host selection policy.
|
56 |
|
57 |
This policy scores hosts such that hosts with fewer leases already
|
58 |
scheduled on them, with the highest capacity, and with fewest leases
|
59 |
scheduled in the future are scored highest.
|
60 |
|
61 |
"""
|
62 |
def __init__(self, slottable): |
63 |
"""Constructor
|
64 |
|
65 |
Argument
|
66 |
slottable -- A fully constructed SlotTable
|
67 |
"""
|
68 |
HostSelectionPolicy.__init__(self, slottable)
|
69 |
|
70 |
def get_host_score(self, node, time, lease): |
71 |
"""Computes the score of a host
|
72 |
|
73 |
See class documentation for details on what policy is implemented here.
|
74 |
See documentation of HostSelectionPolicy.get_host_score for more details
|
75 |
on this method.
|
76 |
|
77 |
Arguments:
|
78 |
node -- Physical node (the integer identifier used in the slot table)
|
79 |
time -- Time at which the lease might be scheduled
|
80 |
lease -- Lease that is being scheduled.
|
81 |
"""
|
82 |
aw = self.slottable.get_availability_window(time)
|
83 |
|
84 |
leases_in_node_horizon = 4
|
85 |
|
86 |
# 1st: We prefer nodes with fewer leases to preempt
|
87 |
leases_in_node = len(aw.get_leases_at(node, time))
|
88 |
if leases_in_node > leases_in_node_horizon:
|
89 |
leases_in_node = leases_in_node_horizon |
90 |
|
91 |
# Nodes with fewer leases already scheduled in them get
|
92 |
# higher scores
|
93 |
leases_in_node = (leases_in_node_horizon - leases_in_node) / float(leases_in_node_horizon)
|
94 |
leases_in_node_score = leases_in_node |
95 |
|
96 |
|
97 |
# 2nd: we prefer nodes with the highest capacity
|
98 |
avail = aw.get_availability_at(node, time) |
99 |
# TODO: normalize into a score
|
100 |
high_capacity_score = 1.0
|
101 |
|
102 |
# 3rd: we prefer nodes where the current capacity
|
103 |
# doesn't change for the longest time.
|
104 |
duration = aw.get_capacity_interval(node, time) |
105 |
if duration == None or duration>=lease.duration.requested: |
106 |
duration_score = 1.0
|
107 |
else:
|
108 |
duration_score = duration.seconds / float(lease.duration.requested.seconds)
|
109 |
|
110 |
return 0.5 * leases_in_node_score + 0.25 * high_capacity_score + 0.25 * duration_score |
111 |
|