Coverage for nova/weights.py: 96%
60 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-24 11:16 +0000
1# Copyright (c) 2011-2012 OpenStack Foundation
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.
16"""
17Pluggable Weighing support
18"""
20import abc
22from oslo_log import log as logging
24from nova import loadables
27LOG = logging.getLogger(__name__)
30def normalize(weight_list, minval=None, maxval=None):
31 """Normalize the values in a list between 0 and 1.0.
33 The normalization is made regarding the lower and upper values present in
34 weight_list. If the minval and/or maxval parameters are set, these values
35 will be used instead of the minimum and maximum from the list.
37 If all the values are equal, they are normalized to 0.
38 """
40 if not weight_list:
41 return ()
43 if maxval is None:
44 maxval = max(weight_list)
46 if minval is None:
47 minval = min(weight_list)
49 maxval = float(maxval)
50 minval = float(minval)
52 if minval == maxval:
53 return [0] * len(weight_list)
55 range_ = maxval - minval
56 return ((i - minval) / range_ for i in weight_list)
59class WeighedObject(object):
60 """Object with weight information."""
62 def __init__(self, obj, weight):
63 self.obj = obj
64 self.weight = weight
66 def __repr__(self):
67 return "<WeighedObject '%s': %s>" % (self.obj, self.weight)
70class BaseWeigher(metaclass=abc.ABCMeta):
71 """Base class for pluggable weighers.
73 The attributes maxval and minval can be specified to set up the maximum
74 and minimum values for the weighed objects. These values will then be
75 taken into account in the normalization step, instead of taking the values
76 from the calculated weights.
77 """
79 minval = None
80 maxval = None
82 def weight_multiplier(self, host_state):
83 """How weighted this weigher should be.
85 Override this method in a subclass, so that the returned value is
86 read from a configuration option to permit operators specify a
87 multiplier for the weigher. If the host is in an aggregate, this
88 method of subclass can read the ``weight_multiplier`` from aggregate
89 metadata of ``host_state``, and use it to overwrite multiplier
90 configuration.
92 :param host_state: The HostState object.
93 """
94 return 1.0
96 @abc.abstractmethod
97 def _weigh_object(self, obj, weight_properties):
98 """Weigh an specific object."""
100 def weigh_objects(self, weighed_obj_list, weight_properties):
101 """Weigh multiple objects.
103 Override in a subclass if you need access to all objects in order
104 to calculate weights. Do not modify the weight of an object here,
105 just return a list of weights.
106 """
107 # Calculate the weights
108 weights = []
109 for obj in weighed_obj_list:
110 weight = self._weigh_object(obj.obj, weight_properties)
112 # don't let the weight go beyond the defined max/min
113 if self.minval is not None:
114 weight = max(weight, self.minval)
115 if self.maxval is not None: 115 ↛ 116line 115 didn't jump to line 116 because the condition on line 115 was never true
116 weight = min(weight, self.maxval)
118 weights.append(weight)
120 return weights
123class BaseWeightHandler(loadables.BaseLoader):
124 object_class = WeighedObject
126 def get_weighed_objects(self, weighers, obj_list, weighing_properties):
127 """Return a sorted (descending), normalized list of WeighedObjects."""
128 weighed_objs = [self.object_class(obj, 0.0) for obj in obj_list]
130 if len(weighed_objs) <= 1:
131 return weighed_objs
133 for weigher in weighers:
134 weights = weigher.weigh_objects(weighed_objs, weighing_properties)
136 LOG.debug(
137 "%s: raw weights %s",
138 weigher.__class__.__name__,
139 {(obj.obj.host, obj.obj.nodename): weight
140 for obj, weight in zip(weighed_objs, weights)}
141 )
143 # Normalize the weights
144 weights = list(
145 normalize(
146 weights, minval=weigher.minval, maxval=weigher.maxval))
148 LOG.debug(
149 "%s: normalized weights %s",
150 weigher.__class__.__name__,
151 {(obj.obj.host, obj.obj.nodename): weight
152 for obj, weight in zip(weighed_objs, weights)}
153 )
155 log_data = {}
157 for i, weight in enumerate(weights):
158 obj = weighed_objs[i]
159 multiplier = weigher.weight_multiplier(obj.obj)
160 weigher_score = multiplier * weight
161 obj.weight += weigher_score
163 log_data[(obj.obj.host, obj.obj.nodename)] = (
164 f"{multiplier} * {weight}")
166 LOG.debug(
167 "%s: score (multiplier * weight) %s",
168 weigher.__class__.__name__,
169 {name: log for name, log in log_data.items()}
170 )
172 return sorted(weighed_objs, key=lambda x: x.weight, reverse=True)