from oslo_config import cfg
from cinder.scheduler import weights
+from cinder import utils
capacity_weight_opts = [
def _weigh_object(self, host_state, weight_properties):
"""Higher weights win. We want spreading to be the default."""
- reserved = float(host_state.reserved_percentage) / 100
free_space = host_state.free_capacity_gb
total_space = host_state.total_capacity_gb
if (free_space == 'infinite' or free_space == 'unknown' or
# capacity anymore.
free = -1 if CONF.capacity_weight_multiplier > 0 else float('inf')
else:
- total = float(total_space)
- if host_state.thin_provisioning_support:
- # Calculate virtual free capacity for thin provisioning.
- free = (total * host_state.max_over_subscription_ratio
- - host_state.provisioned_capacity_gb -
- math.floor(total * reserved))
- else:
- # Calculate how much free space is left after taking into
- # account the reserved space.
- free = free_space - math.floor(total * reserved)
+ free = utils.calculate_virtual_free_capacity(
+ total_space,
+ free_space,
+ host_state.provisioned_capacity_gb,
+ host_state.thin_provisioning_support,
+ host_state.max_over_subscription_ratio,
+ host_state.reserved_percentage)
+
return free
self.assertEqual('OK', result)
self.assertEqual(2, mock_log.debug.call_count)
+
+ def test_utils_calculate_virtual_free_capacity_with_thick(self):
+ host_stat = {'total_capacity_gb': 30.01,
+ 'free_capacity_gb': 28.01,
+ 'provisioned_capacity_gb': 2.0,
+ 'max_over_subscription_ratio': 1.0,
+ 'thin_provisioning_support': False,
+ 'thick_provisioning_support': True,
+ 'reserved_percentage': 5}
+
+ free = utils.calculate_virtual_free_capacity(
+ host_stat['total_capacity_gb'],
+ host_stat['free_capacity_gb'],
+ host_stat['provisioned_capacity_gb'],
+ host_stat['thin_provisioning_support'],
+ host_stat['max_over_subscription_ratio'],
+ host_stat['reserved_percentage'])
+
+ self.assertEqual(27.01, free)
+
+ def test_utils_calculate_virtual_free_capacity_with_thin(self):
+ host_stat = {'total_capacity_gb': 20.01,
+ 'free_capacity_gb': 18.01,
+ 'provisioned_capacity_gb': 2.0,
+ 'max_over_subscription_ratio': 2.0,
+ 'thin_provisioning_support': True,
+ 'thick_provisioning_support': False,
+ 'reserved_percentage': 5}
+
+ free = utils.calculate_virtual_free_capacity(
+ host_stat['total_capacity_gb'],
+ host_stat['free_capacity_gb'],
+ host_stat['provisioned_capacity_gb'],
+ host_stat['thin_provisioning_support'],
+ host_stat['max_over_subscription_ratio'],
+ host_stat['reserved_percentage'])
+
+ self.assertEqual(37.02, free)
import functools
import inspect
import logging as py_logging
+import math
import os
import pyclbr
import random
if str_format:
return str_format % elements
return elements
+
+
+def calculate_virtual_free_capacity(total_capacity,
+ free_capacity,
+ provisioned_capacity,
+ thin_provisioning_support,
+ max_over_subscription_ratio,
+ reserved_percentage):
+ """Calculate the virtual free capacity based on thin provisioning support.
+
+ :param total_capacity: total_capacity_gb of a host_state or pool.
+ :param free_capacity: free_capacity_gb of a host_state or pool.
+ :param provisioned_capacity: provisioned_capacity_gb of a host_state
+ or pool.
+ :param thin_provisioning_support: thin_provisioning_support of
+ a host_state or a pool.
+ :param max_over_subscription_ratio: max_over_subscription_ratio of
+ a host_state or a pool
+ :param reserved_percentage: reserved_percentage of a host_state or
+ a pool.
+ :returns: the calculated virtual free capacity.
+ """
+
+ total = float(total_capacity)
+ reserved = float(reserved_percentage) / 100
+
+ if thin_provisioning_support:
+ free = (total * max_over_subscription_ratio
+ - provisioned_capacity
+ - math.floor(total * reserved))
+ else:
+ # Calculate how much free space is left after taking into
+ # account the reserved space.
+ free = free_capacity - math.floor(total * reserved)
+ return free