Coverage for nova/scheduler/weights/affinity.py: 88%
29 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
1# Copyright (c) 2015 Ericsson AB
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"""
16Affinity Weighers. Weigh hosts by the number of instances from a given host.
18AffinityWeigher implements the soft-affinity policy for server groups by
19preferring the hosts that has more instances from the given group.
21AntiAffinityWeigher implements the soft-anti-affinity policy for server groups
22by preferring the hosts that has less instances from the given group.
24"""
25from oslo_config import cfg
26from oslo_log import log as logging
28from nova.scheduler import utils
29from nova.scheduler import weights
31CONF = cfg.CONF
33LOG = logging.getLogger(__name__)
36class _SoftAffinityWeigherBase(weights.BaseHostWeigher):
37 policy_name = None
39 def _weigh_object(self, host_state, request_spec):
40 """Higher weights win."""
41 if not request_spec.instance_group: 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true
42 return 0
44 policy = request_spec.instance_group.policy
46 if self.policy_name != policy: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true
47 return 0
49 instances = set(host_state.instances.keys())
50 members = set(request_spec.instance_group.members)
51 member_on_host = instances.intersection(members)
53 return len(member_on_host)
56class ServerGroupSoftAffinityWeigher(_SoftAffinityWeigherBase):
57 policy_name = 'soft-affinity'
59 def weight_multiplier(self, host_state):
60 return utils.get_weight_multiplier(
61 host_state, 'soft_affinity_weight_multiplier',
62 CONF.filter_scheduler.soft_affinity_weight_multiplier)
65class ServerGroupSoftAntiAffinityWeigher(_SoftAffinityWeigherBase):
66 policy_name = 'soft-anti-affinity'
68 def weight_multiplier(self, host_state):
69 return utils.get_weight_multiplier(
70 host_state, 'soft_anti_affinity_weight_multiplier',
71 CONF.filter_scheduler.soft_anti_affinity_weight_multiplier)
73 def _weigh_object(self, host_state, request_spec):
74 weight = super(ServerGroupSoftAntiAffinityWeigher, self)._weigh_object(
75 host_state, request_spec)
76 return -1 * weight