Refactor scheduling weights.
This makes scheduling weights more plugin friendly and creates shared code that can be used by the host scheduler as well as the future cells scheduler. Weighing classes can now be specified much like you can specify scheduling host filters. The new weights code reverses the old behavior where lower weights win. Higher weights are now the winners. The least_cost module and configs have been deprecated, but are still supported for backwards compatibility. The code has moved to nova.scheduler.weights.least_cost and been modified to work with the new loadable-class code. If any of the least_cost related config options are specified, this least_cost weigher will be used. For those not overriding the default least_cost config values, the new RamWeigher class will be used. The default behavior of the RamWeigher class is the same default behavior as the old least_cost module. The new weights code introduces a new config option 'scheduler_weight_classes' which is used to specify which weigher classes to use. The default is 'all classes', but modified if least_cost deprecated config options are used, as mentioned above. The RamWeigher class introduces a new config option 'ram_weight_multiplier'. The default of 1.0 causes weights equal to the free memory in MB to be returned, thus hosts with more free memory are preferred (causes equal spreading). Changing this value to a negative number such as -1.0 will cause reverse behavior (fill first). DocImpact Change-Id: I1e5e5039c299db02f7287f2d33299ebf0b9732cechanges/04/15704/11
parent
0d9ce8319d
commit
2c6ab62ae2
@ -1,118 +0,0 @@
|
||||
# Copyright (c) 2011 OpenStack, LLC.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""
|
||||
Least Cost is an algorithm for choosing which host machines to
|
||||
provision a set of resources to. The input is a WeightedHost object which
|
||||
is decided upon by a set of objective-functions, called the 'cost-functions'.
|
||||
The WeightedHost contains a combined weight for each cost-function.
|
||||
|
||||
The cost-function and weights are tabulated, and the host with the least cost
|
||||
is then selected for provisioning.
|
||||
"""
|
||||
|
||||
from nova import config
|
||||
from nova import flags
|
||||
from nova.openstack.common import cfg
|
||||
from nova.openstack.common import log as logging
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
least_cost_opts = [
|
||||
cfg.ListOpt('least_cost_functions',
|
||||
default=[
|
||||
'nova.scheduler.least_cost.compute_fill_first_cost_fn'
|
||||
],
|
||||
help='Which cost functions the LeastCostScheduler should use'),
|
||||
cfg.FloatOpt('noop_cost_fn_weight',
|
||||
default=1.0,
|
||||
help='How much weight to give the noop cost function'),
|
||||
cfg.FloatOpt('compute_fill_first_cost_fn_weight',
|
||||
default=-1.0,
|
||||
help='How much weight to give the fill-first cost function. '
|
||||
'A negative value will reverse behavior: '
|
||||
'e.g. spread-first'),
|
||||
]
|
||||
|
||||
CONF = config.CONF
|
||||
CONF.register_opts(least_cost_opts)
|
||||
|
||||
# TODO(sirp): Once we have enough of these rules, we can break them out into a
|
||||
# cost_functions.py file (perhaps in a least_cost_scheduler directory)
|
||||
|
||||
|
||||
class WeightedHost(object):
|
||||
"""Reduced set of information about a host that has been weighed.
|
||||
This is an attempt to remove some of the ad-hoc dict structures
|
||||
previously used."""
|
||||
|
||||
def __init__(self, weight, host_state=None):
|
||||
self.weight = weight
|
||||
self.host_state = host_state
|
||||
|
||||
def to_dict(self):
|
||||
x = dict(weight=self.weight)
|
||||
if self.host_state:
|
||||
x['host'] = self.host_state.host
|
||||
return x
|
||||
|
||||
def __repr__(self):
|
||||
if self.host_state:
|
||||
return "WeightedHost host: %s" % self.host_state.host
|
||||
return "WeightedHost with no host_state"
|
||||
|
||||
|
||||
def noop_cost_fn(host_state, weighing_properties):
|
||||
"""Return a pre-weight cost of 1 for each host"""
|
||||
return 1
|
||||
|
||||
|
||||
def compute_fill_first_cost_fn(host_state, weighing_properties):
|
||||
"""More free ram = higher weight. So servers with less free
|
||||
ram will be preferred.
|
||||
|
||||
Note: the weight for this function in default configuration
|
||||
is -1.0. With a -1.0 this function runs in reverse, so systems
|
||||
with the most free memory will be preferred.
|
||||
"""
|
||||
return host_state.free_ram_mb
|
||||
|
||||
|
||||
def weighted_sum(weighted_fns, host_states, weighing_properties):
|
||||
"""Use the weighted-sum method to compute a score for an array of objects.
|
||||
|
||||
Normalize the results of the objective-functions so that the weights are
|
||||
meaningful regardless of objective-function's range.
|
||||
|
||||
:param host_list: ``[(host, HostInfo()), ...]``
|
||||
:param weighted_fns: list of weights and functions like::
|
||||
|
||||
[(weight, objective-functions), ...]
|
||||
|
||||
:param weighing_properties: an arbitrary dict of values that can
|
||||
influence weights.
|
||||
|
||||
:returns: a single WeightedHost object which represents the best
|
||||
candidate.
|
||||
"""
|
||||
|
||||
min_score, best_host = None, None
|
||||
for host_state in host_states:
|
||||
score = sum(weight * fn(host_state, weighing_properties)
|
||||
for weight, fn in weighted_fns)
|
||||
if min_score is None or score < min_score:
|
||||
min_score, best_host = score, host_state
|
||||
|
||||
return WeightedHost(min_score, host_state=best_host)
|
@ -0,0 +1,61 @@
|
||||
# Copyright (c) 2011 OpenStack, LLC.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Scheduler host weights
|
||||
"""
|
||||
|
||||
|
||||
from nova import config
|
||||
from nova.openstack.common import log as logging
|
||||
from nova.scheduler.weights import least_cost
|
||||
from nova import weights
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
CONF = config.CONF
|
||||
|
||||
|
||||
class WeighedHost(weights.WeighedObject):
|
||||
def to_dict(self):
|
||||
x = dict(weight=self.weight)
|
||||
x['host'] = self.obj.host
|
||||
return x
|
||||
|
||||
def __repr__(self):
|
||||
return "WeighedHost [host: %s, weight: %s]" % (
|
||||
self.obj.host, self.weight)
|
||||
|
||||
|
||||
class BaseHostWeigher(weights.BaseWeigher):
|
||||
"""Base class for host weights."""
|
||||
pass
|
||||
|
||||
|
||||
class HostWeightHandler(weights.BaseWeightHandler):
|
||||
object_class = WeighedHost
|
||||
|
||||
def __init__(self):
|
||||
super(HostWeightHandler, self).__init__(BaseHostWeigher)
|
||||
|
||||
|
||||
def all_weighers():
|
||||
"""Return a list of weight plugin classes found in this directory."""
|
||||
|
||||
if (CONF.least_cost_functions is not None or
|
||||
CONF.compute_fill_first_cost_fn_weight is not None):
|
||||
LOG.deprecated(_('least_cost has been deprecated in favor of '
|
||||
'the RAM Weigher.'))
|
||||
return least_cost.get_least_cost_weighers()
|
||||
return HostWeightHandler().get_all_classes()
|
@ -0,0 +1,126 @@
|
||||
# Copyright (c) 2011-2012 OpenStack, LLC.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""
|
||||
Least Cost is an algorithm for choosing which host machines to
|
||||
provision a set of resources to. The input is a WeightedHost object which
|
||||
is decided upon by a set of objective-functions, called the 'cost-functions'.
|
||||
The WeightedHost contains a combined weight for each cost-function.
|
||||
|
||||
The cost-function and weights are tabulated, and the host with the least cost
|
||||
is then selected for provisioning.
|
||||
|
||||
NOTE(comstud): This is deprecated. One should use the RAMWeigher and/or
|
||||
create other weight modules.
|
||||
"""
|
||||
|
||||
from nova import config
|
||||
from nova import exception
|
||||
from nova.openstack.common import cfg
|
||||
from nova.openstack.common import importutils
|
||||
from nova.openstack.common import log as logging
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
least_cost_opts = [
|
||||
cfg.ListOpt('least_cost_functions',
|
||||
default=None,
|
||||
help='Which cost functions the LeastCostScheduler should use'),
|
||||
cfg.FloatOpt('noop_cost_fn_weight',
|
||||
default=1.0,
|
||||
help='How much weight to give the noop cost function'),
|
||||
cfg.FloatOpt('compute_fill_first_cost_fn_weight',
|
||||
default=None,
|
||||
help='How much weight to give the fill-first cost function. '
|
||||
'A negative value will reverse behavior: '
|
||||
'e.g. spread-first'),
|
||||
]
|
||||
|
||||
CONF = config.CONF
|
||||
CONF.register_opts(least_cost_opts)
|
||||
|
||||
|
||||
def noop_cost_fn(host_state, weight_properties):
|
||||
"""Return a pre-weight cost of 1 for each host"""
|
||||
return 1
|
||||
|
||||
|
||||
def compute_fill_first_cost_fn(host_state, weight_properties):
|
||||
"""Higher weights win, so we should return a lower weight
|
||||
when there's more free ram available.
|
||||
|
||||
Note: the weight modifier for this function in default configuration
|
||||
is -1.0. With -1.0 this function runs in reverse, so systems
|
||||
with the most free memory will be preferred.
|
||||
"""
|
||||
return -host_state.free_ram_mb
|
||||
|
||||
|
||||
def _get_cost_functions():
|
||||
"""Returns a list of tuples containing weights and cost functions to
|
||||
use for weighing hosts
|
||||
"""
|
||||
cost_fns_conf = CONF.least_cost_functions
|
||||
if cost_fns_conf is None:
|
||||
# The old default. This will get fixed up below.
|
||||
fn_str = 'nova.scheduler.least_cost.compute_fill_first_cost_fn'
|
||||
cost_fns_conf = [fn_str]
|
||||
cost_fns = []
|
||||
for cost_fn_str in cost_fns_conf:
|
||||
short_name = cost_fn_str.split('.')[-1]
|
||||
if not (short_name.startswith('compute_') or
|
||||
short_name.startswith('noop')):
|
||||
continue
|
||||
# Fix up any old paths to the new paths
|
||||
if cost_fn_str.startswith('nova.scheduler.least_cost.'):
|
||||
cost_fn_str = ('nova.scheduler.weights.least_cost' +
|
||||
cost_fn_str[25:])
|
||||
try:
|
||||
# NOTE: import_class is somewhat misnamed since
|
||||
# the weighing function can be any non-class callable
|
||||
# (i.e., no 'self')
|
||||
cost_fn = importutils.import_class(cost_fn_str)
|
||||
except ImportError:
|
||||
raise exception.SchedulerCostFunctionNotFound(
|
||||
cost_fn_str=cost_fn_str)
|
||||
|
||||
try:
|
||||
flag_name = "%s_weight" % cost_fn.__name__
|
||||
weight = getattr(CONF, flag_name)
|
||||
except AttributeError:
|
||||
raise exception.SchedulerWeightFlagNotFound(
|
||||
flag_name=flag_name)
|
||||
# Set the original default.
|
||||
if (flag_name == 'compute_fill_first_cost_fn_weight' and
|
||||
weight is None):
|
||||
weight = -1.0
|
||||
cost_fns.append((weight, cost_fn))
|
||||
return cost_fns
|
||||
|
||||
|
||||
def get_least_cost_weighers():
|
||||
cost_functions = _get_cost_functions()
|
||||
|
||||
# Unfortunately we need to import this late so we don't have an
|
||||
# import loop.
|
||||
from nova.scheduler import weights
|
||||
|
||||
class _LeastCostWeigher(weights.BaseHostWeigher):
|
||||
def weigh_objects(self, weighted_hosts, weight_properties):
|
||||
for host in weighted_hosts:
|
||||
host.weight = sum(weight * fn(host.obj, weight_properties)
|
||||
for weight, fn in cost_functions)
|
||||
|
||||
return [_LeastCostWeigher]
|
@ -0,0 +1,46 @@
|
||||
# Copyright (c) 2011 OpenStack, LLC.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""
|
||||
RAM Weigher. Weigh hosts by their RAM usage.
|
||||
|
||||
The default is to spread instances across all hosts evenly. If you prefer
|
||||
stacking, you can set the 'ram_weight_multiplier' option to a negative
|
||||
number and the weighing has the opposite effect of the default.
|
||||
"""
|
||||
|
||||
from nova import config
|
||||
from nova.openstack.common import cfg
|
||||
from nova.scheduler import weights
|
||||
|
||||
|
||||
ram_weight_opts = [
|
||||
cfg.FloatOpt('ram_weight_multiplier',
|
||||
default=1.0,
|
||||
help='Multiplier used for weighing ram. Negative '
|
||||
'numbers mean to stack vs spread.'),
|
||||
]
|
||||
|
||||
CONF = config.CONF
|
||||
CONF.register_opts(ram_weight_opts)
|
||||
|
||||
|
||||
class RAMWeigher(weights.BaseHostWeigher):
|
||||
def _weight_multiplier(self):
|
||||
"""Override the weight multiplier."""
|
||||
return CONF.ram_weight_multiplier
|
||||
|
||||
def _weigh_object(self, host_state, weight_properties):
|
||||
"""Higher weights win. We want spreading to be the default."""
|
||||
return host_state.free_ram_mb
|
@ -0,0 +1,117 @@
|
||||
# Copyright 2011-2012 OpenStack LLC.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
"""
|
||||
Tests For Scheduler weights.
|
||||
"""
|
||||
|
||||
from nova import context
|
||||
from nova.scheduler import weights
|
||||
from nova import test
|
||||
from nova.tests import matchers
|
||||
from nova.tests.scheduler import fakes
|
||||
|
||||
|
||||
class TestWeighedHost(test.TestCase):
|
||||
def test_dict_conversion(self):
|
||||
host_state = fakes.FakeHostState('somehost', None, {})
|
||||
host = weights.WeighedHost(host_state, 'someweight')
|
||||
expected = {'weight': 'someweight',
|
||||
'host': 'somehost'}
|
||||
self.assertThat(host.to_dict(), matchers.DictMatches(expected))
|
||||
|
||||
def test_all_weighers(self):
|
||||
classes = weights.all_weighers()
|
||||
class_names = [cls.__name__ for cls in classes]
|
||||
self.assertEqual(len(classes), 1)
|
||||
self.assertIn('RAMWeigher', class_names)
|
||||
|
||||
def test_all_weighers_with_deprecated_config1(self):
|
||||
self.flags(compute_fill_first_cost_fn_weight=-1.0)
|
||||
classes = weights.all_weighers()
|
||||
class_names = [cls.__name__ for cls in classes]
|
||||
self.assertEqual(len(classes), 1)
|
||||
self.assertIn('_LeastCostWeigher', class_names)
|
||||
|
||||
def test_all_weighers_with_deprecated_config2(self):
|
||||
self.flags(least_cost_functions=['something'])
|
||||
classes = weights.all_weighers()
|
||||
class_names = [cls.__name__ for cls in classes]
|
||||
self.assertEqual(len(classes), 1)
|
||||
self.assertIn('_LeastCostWeigher', class_names)
|
||||
|
||||
|
||||
class RamWeigherTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(RamWeigherTestCase, self).setUp()
|
||||
self.host_manager = fakes.FakeHostManager()
|
||||
self.weight_handler = weights.HostWeightHandler()
|
||||
self.weight_classes = self.weight_handler.get_matching_classes(
|
||||
['nova.scheduler.weights.ram.RAMWeigher'])
|
||||
|
||||
def _get_weighed_host(self, hosts, weight_properties=None):
|
||||
if weight_properties is None:
|
||||
weight_properties = {}
|
||||
return self.weight_handler.get_weighed_objects(self.weight_classes,
|
||||
hosts, weight_properties)[0]
|
||||
|
||||
def _get_all_hosts(self):
|
||||
ctxt = context.get_admin_context()
|
||||
fakes.mox_host_manager_db_calls(self.mox, ctxt)
|
||||
self.mox.ReplayAll()
|
||||
host_states = self.host_manager.get_all_host_states(ctxt)
|
||||
self.mox.VerifyAll()
|
||||
self.mox.ResetAll()
|
||||
return host_states
|
||||
|
||||
def test_default_of_spreading_first(self):
|
||||
hostinfo_list = self._get_all_hosts()
|
||||
|
||||
# host1: free_ram_mb=512
|
||||
# host2: free_ram_mb=1024
|
||||
# host3: free_ram_mb=3072
|
||||
# host4: free_ram_mb=8192
|
||||
|
||||
# so, host4 should win:
|
||||
weighed_host = self._get_weighed_host(hostinfo_list)
|
||||
self.assertEqual(weighed_host.weight, 8192)
|
||||
self.assertEqual(weighed_host.obj.host, 'host4')
|
||||
|
||||
def test_ram_filter_multiplier1(self):
|
||||
self.flags(ram_weight_multiplier=-1.0)
|
||||
hostinfo_list = self._get_all_hosts()
|
||||
|
||||
# host1: free_ram_mb=-512
|
||||
# host2: free_ram_mb=-1024
|
||||
# host3: free_ram_mb=-3072
|
||||
# host4: free_ram_mb=-8192
|
||||
|
||||
# so, host1 should win:
|
||||
weighed_host = self._get_weighed_host(hostinfo_list)
|
||||
self.assertEqual(weighed_host.weight, -512)
|
||||
self.assertEqual(weighed_host.obj.host, 'host1')
|
||||
|
||||
def test_ram_filter_multiplier2(self):
|
||||
self.flags(ram_weight_multiplier=2.0)
|
||||
hostinfo_list = self._get_all_hosts()
|
||||
|
||||
# host1: free_ram_mb=512 * 2
|
||||
# host2: free_ram_mb=1024 * 2
|
||||
# host3: free_ram_mb=3072 * 2
|
||||
# host4: free_ram_mb=8192 * 2
|
||||
|
||||
# so, host4 should win:
|
||||
weighed_host = self._get_weighed_host(hostinfo_list)
|
||||
self.assertEqual(weighed_host.weight, 8192 * 2)
|
||||
self.assertEqual(weighed_host.obj.host, 'host4')
|
@ -0,0 +1,71 @@
|
||||
# Copyright (c) 2011-2012 OpenStack, LLC.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Pluggable Weighing support
|
||||
"""
|
||||
|
||||
from nova import loadables
|
||||
|
||||
|
||||
class WeighedObject(object):
|
||||
"""Object with weight information."""
|
||||
def __init__(self, obj, weight):
|
||||
self.obj = obj
|
||||
self.weight = weight
|
||||
|
||||
def __repr__(self):
|
||||
return "<WeighedObject '%s': %s>" % (self.obj, self.weight)
|
||||
|
||||
|
||||
class BaseWeigher(object):
|
||||
"""Base class for pluggable weighers."""
|
||||
def _weight_multiplier(self):
|
||||
"""How weighted this weigher should be. Normally this would
|
||||
be overriden in a subclass based on a config value.
|
||||
"""
|
||||
return 1.0
|
||||
|
||||
def _weigh_object(self, obj, weight_properties):
|
||||
"""Override in a subclass to specify a weight for a specific
|
||||
object.
|
||||
"""
|
||||
return 0.0
|
||||
|
||||
def weigh_objects(self, weighed_obj_list, weight_properties):
|
||||
"""Weigh multiple objects. Override in a subclass if you need
|
||||
need access to all objects in order to manipulate weights.
|
||||
"""
|
||||
for obj in weighed_obj_list:
|
||||
obj.weight += (self._weight_multiplier() *
|
||||
self._weigh_object(obj.obj, weight_properties))
|
||||
|
||||
|
||||
class BaseWeightHandler(loadables.BaseLoader):
|
||||
object_class = WeighedObject
|
||||
|
||||
def get_weighed_objects(self, weigher_classes, obj_list,
|
||||
weighing_properties):
|
||||
"""Return a sorted (highest score first) list of WeighedObjects."""
|
||||
|
||||
if not obj_list:
|
||||
return []
|
||||
|
||||
weighed_objs = [self.object_class(obj, 0.0) for obj in obj_list]
|
||||
for weigher_cls in weigher_classes:
|
||||
weigher = weigher_cls()
|
||||
weigher.weigh_objects(weighed_objs, weighing_properties)
|
||||
|
||||
return sorted(weighed_objs, key=lambda x: x.weight, reverse=True)
|
Loading…
Reference in New Issue