summaryrefslogtreecommitdiff
path: root/nova/tests/unit/scheduler/weights/test_weights_affinity.py
blob: 3048e9f06c3498751cee0c5822e361adf1c2fbc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright (c) 2015 Ericsson AB
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from unittest import mock

from nova import objects
from nova.scheduler import weights
from nova.scheduler.weights import affinity
from nova import test
from nova.tests.unit.scheduler import fakes


class SoftWeigherTestBase(test.NoDBTestCase):

    def setUp(self):
        super(SoftWeigherTestBase, self).setUp()
        self.weight_handler = weights.HostWeightHandler()
        self.weighers = []

    def _get_weighed_host(self, hosts, policy, group='default'):
        if group == 'default':
            members = ['member1', 'member2', 'member3', 'member4', 'member5',
                'member6', 'member7']
        else:
            members = ['othermember1', 'othermember2']
        request_spec = objects.RequestSpec(
            instance_group=objects.InstanceGroup(
                policy=policy,
                members=members))
        return self.weight_handler.get_weighed_objects(self.weighers,
                                                       hosts,
                                                       request_spec)[0]

    def _get_all_hosts(self):
        host_values = [
            ('host1', 'node1', {'instances': {
                'member1': mock.sentinel,
                'instance13': mock.sentinel
            }}),
            ('host2', 'node2', {'instances': {
                'member2': mock.sentinel,
                'member3': mock.sentinel,
                'member4': mock.sentinel,
                'member5': mock.sentinel,
                'othermember1': mock.sentinel,
                'othermember2': mock.sentinel,
                'instance14': mock.sentinel
            }}),
            ('host3', 'node3', {'instances': {
                'instance15': mock.sentinel
            }}),
            ('host4', 'node4', {'instances': {
                'member6': mock.sentinel,
                'member7': mock.sentinel,
                'instance16': mock.sentinel
            }})]
        return [fakes.FakeHostState(host, node, values)
                for host, node, values in host_values]

    def _do_test(self, policy, expected_weight, expected_host,
                 group='default'):
        hostinfo_list = self._get_all_hosts()
        weighed_host = self._get_weighed_host(hostinfo_list,
                                              policy, group)
        self.assertEqual(expected_weight, weighed_host.weight)
        if expected_host:
            self.assertEqual(expected_host, weighed_host.obj.host)


class SoftAffinityWeigherTestCase(SoftWeigherTestBase):

    def setUp(self):
        super(SoftAffinityWeigherTestCase, self).setUp()
        self.weighers = [affinity.ServerGroupSoftAffinityWeigher()]
        self.softaffin_weigher = affinity.ServerGroupSoftAffinityWeigher()

    def test_soft_affinity_weight_multiplier_by_default(self):
        self._do_test(policy='soft-affinity',
                      expected_weight=1.0,
                      expected_host='host2')

    def test_soft_affinity_weight_multiplier_zero_value(self):
        # We do not know the host, all have same weight.
        self.flags(soft_affinity_weight_multiplier=0.0,
                   group='filter_scheduler')
        self._do_test(policy='soft-affinity',
                      expected_weight=0.0,
                      expected_host=None)

    def test_soft_affinity_weight_multiplier_positive_value(self):
        self.flags(soft_affinity_weight_multiplier=2.0,
                   group='filter_scheduler')
        self._do_test(policy='soft-affinity',
                      expected_weight=2.0,
                      expected_host='host2')

    def test_soft_affinity_weight_multiplier(self):
        self.flags(soft_affinity_weight_multiplier=0.0,
                   group='filter_scheduler')
        host_attr = {'instances': {'instance1': mock.sentinel}}
        host1 = fakes.FakeHostState('fake-host', 'node', host_attr)
        # By default, return the weight_multiplier configuration directly
        self.assertEqual(0.0, self.softaffin_weigher.weight_multiplier(host1))

        host1.aggregates = [
            objects.Aggregate(
                id=1,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_affinity_weight_multiplier': '2'},
            )]
        # read the weight multiplier from metadata to override the config
        self.assertEqual(2.0, self.softaffin_weigher.weight_multiplier(host1))

        host1.aggregates = [
            objects.Aggregate(
                id=1,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_affinity_weight_multiplier': '2'},
            ),
            objects.Aggregate(
                id=2,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_affinity_weight_multiplier': '1.5'},
            )]
        # If the host is in multiple aggs and there are conflict weight values
        # in the metadata, we will use the min value among them
        self.assertEqual(1.5, self.softaffin_weigher.weight_multiplier(host1))

    def test_host_with_agg(self):
        self.flags(soft_affinity_weight_multiplier=0.0,
                   group='filter_scheduler')
        hostinfo_list = self._get_all_hosts()
        aggs = [
            objects.Aggregate(
                id=1,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_affinity_weight_multiplier': '1.5'},
            )]
        for h in hostinfo_list:
            h.aggregates = aggs

        weighed_host = self._get_weighed_host(hostinfo_list,
                                              'soft-affinity')
        self.assertEqual(1.5, weighed_host.weight)
        self.assertEqual('host2', weighed_host.obj.host)

    def test_running_twice(self):
        """Run the weighing twice for different groups each run

        The first run has a group with more members on the same host than the
        second both. In both cases, most members of their groups are on the
        same host => weight should be maximum (1 with default multiplier).
        """
        self._do_test(policy='soft-affinity',
                      expected_weight=1.0,
                      expected_host='host2')
        self._do_test(policy='soft-affinity',
                      expected_weight=1.0,
                      expected_host='host2',
                      group='other')


class SoftAntiAffinityWeigherTestCase(SoftWeigherTestBase):

    def setUp(self):
        super(SoftAntiAffinityWeigherTestCase, self).setUp()
        self.weighers = [affinity.ServerGroupSoftAntiAffinityWeigher()]
        self.antiaffin_weigher = affinity.ServerGroupSoftAntiAffinityWeigher()

    def test_soft_anti_affinity_weight_multiplier_by_default(self):
        self._do_test(policy='soft-anti-affinity',
                      expected_weight=1.0,
                      expected_host='host3')

    def test_soft_anti_affinity_weight_multiplier_zero_value(self):
        # We do not know the host, all have same weight.
        self.flags(soft_anti_affinity_weight_multiplier=0.0,
                   group='filter_scheduler')
        self._do_test(policy='soft-anti-affinity',
                      expected_weight=0.0,
                      expected_host=None)

    def test_soft_anti_affinity_weight_multiplier_positive_value(self):
        self.flags(soft_anti_affinity_weight_multiplier=2.0,
                   group='filter_scheduler')
        self._do_test(policy='soft-anti-affinity',
                      expected_weight=2.0,
                      expected_host='host3')

    def test_soft_anti_affinity_weight_multiplier(self):
        self.flags(soft_anti_affinity_weight_multiplier=0.0,
                   group='filter_scheduler')
        host_attr = {'instances': {'instance1': mock.sentinel}}
        host1 = fakes.FakeHostState('fake-host', 'node', host_attr)
        # By default, return the weight_multiplier configuration directly
        self.assertEqual(0.0, self.antiaffin_weigher.weight_multiplier(host1))

        host1.aggregates = [
            objects.Aggregate(
                id=1,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_anti_affinity_weight_multiplier': '2'},
            )]
        # read the weight multiplier from metadata to override the config
        self.assertEqual(2.0, self.antiaffin_weigher.weight_multiplier(host1))

        host1.aggregates = [
            objects.Aggregate(
                id=1,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_anti_affinity_weight_multiplier': '2'},
            ),
            objects.Aggregate(
                id=2,
                name='foo',
                hosts=['fake-host'],
                metadata={'soft_anti_affinity_weight_multiplier': '1.5'},
            )]
        # If the host is in multiple aggs and there are conflict weight values
        # in the metadata, we will use the min value among them
        self.assertEqual(1.5, self.antiaffin_weigher.weight_multiplier(host1))

    def test_host_with_agg(self):
        self.flags(soft_anti_affinity_weight_multiplier=0.0,
                   group='filter_scheduler')
        hostinfo_list = self._get_all_hosts()
        aggs = [
            objects.Aggregate(
                id=1,
                name='foo',
                hosts=['host1', 'host2', 'host3', 'host4'],
                metadata={'soft_anti_affinity_weight_multiplier': '1.5'},
            )]
        for h in hostinfo_list:
            h.aggregates = aggs

        weighed_host = self._get_weighed_host(hostinfo_list,
                                              'soft-anti-affinity')
        self.assertEqual(1.5, weighed_host.weight)
        self.assertEqual('host3', weighed_host.obj.host)