1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
Tests For Least Cost Scheduler
19
from nova import flags
21
from nova.scheduler import least_cost
22
from nova.tests.scheduler import test_zone_aware_scheduler
28
class FakeHost(object):
29
def __init__(self, host_id, free_ram, io):
31
self.free_ram = free_ram
35
class WeightedSumTestCase(test.TestCase):
36
def test_empty_domain(self):
39
result = least_cost.weighted_sum(domain, weighted_fns)
41
self.assertEqual(expected, result)
43
def test_basic_costing(self):
45
FakeHost(1, 512 * MB, 100),
46
FakeHost(2, 256 * MB, 400),
47
FakeHost(3, 512 * MB, 100),
51
(1, lambda h: h.free_ram), # Fill-first, free_ram is a *cost*
52
(2, lambda h: h.io), # Avoid high I/O
55
costs = least_cost.weighted_sum(
56
domain=hosts, weighted_fns=weighted_fns)
58
# Each 256 MB unit of free-ram contributes 0.5 points by way of:
59
# cost = weight * (score/max_score) = 1 * (256/512) = 0.5
60
# Each 100 iops of IO adds 0.5 points by way of:
61
# cost = 2 * (100/400) = 2 * 0.25 = 0.5
62
expected = [1.5, 2.5, 1.5]
63
self.assertEqual(expected, costs)
66
class LeastCostSchedulerTestCase(test.TestCase):
68
super(LeastCostSchedulerTestCase, self).setUp()
70
class FakeZoneManager:
73
zone_manager = FakeZoneManager()
75
states = test_zone_aware_scheduler.fake_zone_manager_service_states(
77
zone_manager.service_states = states
79
self.sched = least_cost.LeastCostScheduler()
80
self.sched.zone_manager = zone_manager
83
super(LeastCostSchedulerTestCase, self).tearDown()
85
def assertWeights(self, expected, num, request_spec, hosts):
86
weighted = self.sched.weigh_hosts(num, request_spec, hosts)
87
self.assertDictListMatch(weighted, expected, approx_equal=True)
89
def test_no_hosts(self):
95
self.assertWeights(expected, num, request_spec, hosts)
97
def test_noop_cost_fn(self):
98
FLAGS.least_cost_scheduler_cost_functions = [
99
'nova.scheduler.least_cost.noop_cost_fn',
101
FLAGS.noop_cost_fn_weight = 1
105
hosts = self.sched.filter_hosts(num, request_spec)
107
expected = [dict(weight=1, hostname=hostname)
108
for hostname, caps in hosts]
109
self.assertWeights(expected, num, request_spec, hosts)
111
def test_cost_fn_weights(self):
112
FLAGS.least_cost_scheduler_cost_functions = [
113
'nova.scheduler.least_cost.noop_cost_fn',
115
FLAGS.noop_cost_fn_weight = 2
119
hosts = self.sched.filter_hosts(num, request_spec)
121
expected = [dict(weight=2, hostname=hostname)
122
for hostname, caps in hosts]
123
self.assertWeights(expected, num, request_spec, hosts)
125
def test_fill_first_cost_fn(self):
126
FLAGS.least_cost_scheduler_cost_functions = [
127
'nova.scheduler.least_cost.fill_first_cost_fn',
129
FLAGS.fill_first_cost_fn_weight = 1
133
hosts = self.sched.filter_hosts(num, request_spec)
136
for idx, (hostname, caps) in enumerate(hosts):
137
# Costs are normalized so over 10 hosts, each host with increasing
138
# free ram will cost 1/N more. Since the lowest cost host has some
139
# free ram, we add in the 1/N for the base_cost
140
weight = 0.1 + (0.1 * idx)
141
weight_dict = dict(weight=weight, hostname=hostname)
142
expected.append(weight_dict)
144
self.assertWeights(expected, num, request_spec, hosts)