~mdragon/nova/system-usages

« back to all changes in this revision

Viewing changes to nova/tests/scheduler/test_least_cost_scheduler.py

  • Committer: Monsyne Dragon
  • Date: 2011-06-28 10:23:00 UTC
  • mfrom: (1077.1.141 nova)
  • Revision ID: mdragon@rackspace.com-20110628102300-lnkdr13k8uuyp30i
remergedĀ trunkĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 OpenStack LLC.
 
2
# All Rights Reserved.
 
3
#
 
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
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
#    under the License.
 
15
"""
 
16
Tests For Least Cost Scheduler
 
17
"""
 
18
 
 
19
from nova import flags
 
20
from nova import test
 
21
from nova.scheduler import least_cost
 
22
from nova.tests.scheduler import test_zone_aware_scheduler
 
23
 
 
24
MB = 1024 * 1024
 
25
FLAGS = flags.FLAGS
 
26
 
 
27
 
 
28
class FakeHost(object):
 
29
    def __init__(self, host_id, free_ram, io):
 
30
        self.id = host_id
 
31
        self.free_ram = free_ram
 
32
        self.io = io
 
33
 
 
34
 
 
35
class WeightedSumTestCase(test.TestCase):
 
36
    def test_empty_domain(self):
 
37
        domain = []
 
38
        weighted_fns = []
 
39
        result = least_cost.weighted_sum(domain, weighted_fns)
 
40
        expected = []
 
41
        self.assertEqual(expected, result)
 
42
 
 
43
    def test_basic_costing(self):
 
44
        hosts = [
 
45
            FakeHost(1, 512 * MB, 100),
 
46
            FakeHost(2, 256 * MB, 400),
 
47
            FakeHost(3, 512 * MB, 100),
 
48
        ]
 
49
 
 
50
        weighted_fns = [
 
51
            (1, lambda h: h.free_ram),  # Fill-first, free_ram is a *cost*
 
52
            (2, lambda h: h.io),  # Avoid high I/O
 
53
        ]
 
54
 
 
55
        costs = least_cost.weighted_sum(
 
56
            domain=hosts, weighted_fns=weighted_fns)
 
57
 
 
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)
 
64
 
 
65
 
 
66
class LeastCostSchedulerTestCase(test.TestCase):
 
67
    def setUp(self):
 
68
        super(LeastCostSchedulerTestCase, self).setUp()
 
69
 
 
70
        class FakeZoneManager:
 
71
            pass
 
72
 
 
73
        zone_manager = FakeZoneManager()
 
74
 
 
75
        states = test_zone_aware_scheduler.fake_zone_manager_service_states(
 
76
            num_hosts=10)
 
77
        zone_manager.service_states = states
 
78
 
 
79
        self.sched = least_cost.LeastCostScheduler()
 
80
        self.sched.zone_manager = zone_manager
 
81
 
 
82
    def tearDown(self):
 
83
        super(LeastCostSchedulerTestCase, self).tearDown()
 
84
 
 
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)
 
88
 
 
89
    def test_no_hosts(self):
 
90
        num = 1
 
91
        request_spec = {}
 
92
        hosts = []
 
93
 
 
94
        expected = []
 
95
        self.assertWeights(expected, num, request_spec, hosts)
 
96
 
 
97
    def test_noop_cost_fn(self):
 
98
        FLAGS.least_cost_scheduler_cost_functions = [
 
99
            'nova.scheduler.least_cost.noop_cost_fn',
 
100
        ]
 
101
        FLAGS.noop_cost_fn_weight = 1
 
102
 
 
103
        num = 1
 
104
        request_spec = {}
 
105
        hosts = self.sched.filter_hosts(num, request_spec)
 
106
 
 
107
        expected = [dict(weight=1, hostname=hostname)
 
108
                    for hostname, caps in hosts]
 
109
        self.assertWeights(expected, num, request_spec, hosts)
 
110
 
 
111
    def test_cost_fn_weights(self):
 
112
        FLAGS.least_cost_scheduler_cost_functions = [
 
113
            'nova.scheduler.least_cost.noop_cost_fn',
 
114
        ]
 
115
        FLAGS.noop_cost_fn_weight = 2
 
116
 
 
117
        num = 1
 
118
        request_spec = {}
 
119
        hosts = self.sched.filter_hosts(num, request_spec)
 
120
 
 
121
        expected = [dict(weight=2, hostname=hostname)
 
122
                    for hostname, caps in hosts]
 
123
        self.assertWeights(expected, num, request_spec, hosts)
 
124
 
 
125
    def test_fill_first_cost_fn(self):
 
126
        FLAGS.least_cost_scheduler_cost_functions = [
 
127
            'nova.scheduler.least_cost.fill_first_cost_fn',
 
128
        ]
 
129
        FLAGS.fill_first_cost_fn_weight = 1
 
130
 
 
131
        num = 1
 
132
        request_spec = {}
 
133
        hosts = self.sched.filter_hosts(num, request_spec)
 
134
 
 
135
        expected = []
 
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)
 
143
 
 
144
        self.assertWeights(expected, num, request_spec, hosts)