Coverage for nova/weights.py: 96%

60 statements  

« 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. 

15 

16""" 

17Pluggable Weighing support 

18""" 

19 

20import abc 

21 

22from oslo_log import log as logging 

23 

24from nova import loadables 

25 

26 

27LOG = logging.getLogger(__name__) 

28 

29 

30def normalize(weight_list, minval=None, maxval=None): 

31 """Normalize the values in a list between 0 and 1.0. 

32 

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. 

36 

37 If all the values are equal, they are normalized to 0. 

38 """ 

39 

40 if not weight_list: 

41 return () 

42 

43 if maxval is None: 

44 maxval = max(weight_list) 

45 

46 if minval is None: 

47 minval = min(weight_list) 

48 

49 maxval = float(maxval) 

50 minval = float(minval) 

51 

52 if minval == maxval: 

53 return [0] * len(weight_list) 

54 

55 range_ = maxval - minval 

56 return ((i - minval) / range_ for i in weight_list) 

57 

58 

59class WeighedObject(object): 

60 """Object with weight information.""" 

61 

62 def __init__(self, obj, weight): 

63 self.obj = obj 

64 self.weight = weight 

65 

66 def __repr__(self): 

67 return "<WeighedObject '%s': %s>" % (self.obj, self.weight) 

68 

69 

70class BaseWeigher(metaclass=abc.ABCMeta): 

71 """Base class for pluggable weighers. 

72 

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 """ 

78 

79 minval = None 

80 maxval = None 

81 

82 def weight_multiplier(self, host_state): 

83 """How weighted this weigher should be. 

84 

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. 

91 

92 :param host_state: The HostState object. 

93 """ 

94 return 1.0 

95 

96 @abc.abstractmethod 

97 def _weigh_object(self, obj, weight_properties): 

98 """Weigh an specific object.""" 

99 

100 def weigh_objects(self, weighed_obj_list, weight_properties): 

101 """Weigh multiple objects. 

102 

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) 

111 

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) 

117 

118 weights.append(weight) 

119 

120 return weights 

121 

122 

123class BaseWeightHandler(loadables.BaseLoader): 

124 object_class = WeighedObject 

125 

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] 

129 

130 if len(weighed_objs) <= 1: 

131 return weighed_objs 

132 

133 for weigher in weighers: 

134 weights = weigher.weigh_objects(weighed_objs, weighing_properties) 

135 

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 ) 

142 

143 # Normalize the weights 

144 weights = list( 

145 normalize( 

146 weights, minval=weigher.minval, maxval=weigher.maxval)) 

147 

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 ) 

154 

155 log_data = {} 

156 

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 

162 

163 log_data[(obj.obj.host, obj.obj.nodename)] = ( 

164 f"{multiplier} * {weight}") 

165 

166 LOG.debug( 

167 "%s: score (multiplier * weight) %s", 

168 weigher.__class__.__name__, 

169 {name: log for name, log in log_data.items()} 

170 ) 

171 

172 return sorted(weighed_objs, key=lambda x: x.weight, reverse=True)