Coverage for nova/scheduler/weights/metrics.py: 97%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright (c) 2011 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""" 

17Metrics Weigher. Weigh hosts by their metrics. 

18 

19This weigher can compute the weight based on the compute node host's various 

20metrics. The to-be weighed metrics and their weighing ratio are specified 

21in the configuration file as follows: 

22 

23 [metrics] 

24 weight_setting = name1=1.0, name2=-1.0 

25 

26 The final weight would be name1.value * 1.0 + name2.value * -1.0. 

27""" 

28 

29import nova.conf 

30from nova import exception 

31from nova.scheduler import utils 

32from nova.scheduler import weights 

33 

34 

35CONF = nova.conf.CONF 

36 

37 

38class MetricsWeigher(weights.BaseHostWeigher): 

39 def __init__(self): 

40 self._parse_setting() 

41 

42 def _parse_setting(self): 

43 self.setting = utils.parse_options(CONF.metrics.weight_setting, 

44 sep='=', 

45 converter=float, 

46 name="metrics.weight_setting") 

47 

48 def weight_multiplier(self, host_state): 

49 """Override the weight multiplier.""" 

50 return utils.get_weight_multiplier( 

51 host_state, 'metrics_weight_multiplier', 

52 CONF.metrics.weight_multiplier) 

53 

54 def _weigh_object(self, host_state, weight_properties): 

55 value = 0.0 

56 

57 # NOTE(sbauza): Keying a dict of Metrics per metric name given that we 

58 # have a MonitorMetricList object 

59 metrics_dict = {m.name: m for m in host_state.metrics or []} 

60 for (name, ratio) in self.setting: 

61 try: 

62 value += metrics_dict[name].value * ratio 

63 except KeyError: 

64 if CONF.metrics.required: 

65 raise exception.ComputeHostMetricNotFound( 

66 host=host_state.host, 

67 node=host_state.nodename, 

68 name=name) 

69 else: 

70 # We treat the unavailable metric as the most negative 

71 # factor, i.e. set the value to make this obj would be 

72 # at the end of the ordered weighed obj list 

73 # Do nothing if ratio or weight_multiplier is 0. 

74 if ratio * self.weight_multiplier(host_state) != 0: 74 ↛ 60line 74 didn't jump to line 60 because the condition on line 74 was always true

75 return CONF.metrics.weight_of_unavailable 

76 

77 return value