Merge "Affiniy/anti-affinity costs for soft affinity placement in solver scheduler" into stable/icehouse

This commit is contained in:
Jenkins
2015-06-24 12:34:43 +00:00
committed by Gerrit Code Review
2 changed files with 532 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
# Copyright (c) 2015 Cisco Systems, Inc.
# 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.
import six
from oslo.config import cfg
from nova.compute import api as compute
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova_solverscheduler.scheduler.solvers import costs as solver_costs
affinity_cost_opts = [
cfg.FloatOpt('affinity_cost_multiplier',
default=1.0,
help='Multiplier used for affinity cost. Must be a '
'positive number.'),
cfg.FloatOpt('anti_affinity_cost_multiplier',
default=1.0,
help='Multiplier used for anti-affinity cost. Must be '
'a positive number.'),
]
CONF = cfg.CONF
CONF.register_opts(affinity_cost_opts, group='solver_scheduler')
LOG = logging.getLogger(__name__)
class AffinityCost(solver_costs.BaseLinearCost):
def __init__(self):
super(AffinityCost, self).__init__()
self.compute_api = compute.API()
def cost_multiplier(self):
return CONF.solver_scheduler.affinity_cost_multiplier
def get_extended_cost_matrix(self, hosts, filter_properties):
num_hosts = len(hosts)
num_instances = filter_properties.get('num_instances')
multiplier = self.cost_multiplier()
if multiplier == 0:
extended_cost_matrix = [[0 for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
return extended_cost_matrix
context = filter_properties['context']
scheduler_hints = filter_properties.get('scheduler_hints') or {}
affinity_uuids = scheduler_hints.get('soft_same_host', [])
if affinity_uuids == '':
extended_cost_matrix = [[float(-j) / multiplier
for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
LOG.debug(_('No instance specified for AffinityCost.'))
return extended_cost_matrix
if isinstance(affinity_uuids, six.string_types):
affinity_uuids = [affinity_uuids]
if affinity_uuids:
extended_cost_matrix = [[1 - (float(j) / multiplier)
for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
for i in xrange(num_hosts):
if self.compute_api.get_all(context, {'host': hosts[i].host,
'uuid': affinity_uuids,
'deleted': False}):
extended_cost_matrix[i] = [float(-j) / multiplier for j in
xrange(num_instances + 1)]
else:
extended_cost_matrix = [[0 for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
return extended_cost_matrix
class AntiAffinityCost(solver_costs.BaseLinearCost):
def __init__(self):
super(AntiAffinityCost, self).__init__()
self.compute_api = compute.API()
def cost_multiplier(self):
return CONF.solver_scheduler.anti_affinity_cost_multiplier
def get_extended_cost_matrix(self, hosts, filter_properties):
num_hosts = len(hosts)
num_instances = filter_properties.get('num_instances')
multiplier = self.cost_multiplier()
if multiplier == 0:
extended_cost_matrix = [[0 for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
return extended_cost_matrix
context = filter_properties['context']
scheduler_hints = filter_properties.get('scheduler_hints') or {}
affinity_uuids = scheduler_hints.get('soft_different_host', [])
if affinity_uuids == '':
extended_cost_matrix = [[float(j) / multiplier
for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
LOG.debug(_('No instance specified for AntiAffinityCost.'))
return extended_cost_matrix
if isinstance(affinity_uuids, six.string_types):
affinity_uuids = [affinity_uuids]
if affinity_uuids:
extended_cost_matrix = [[float(j) / multiplier
for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
for i in xrange(num_hosts):
if self.compute_api.get_all(context, {'host': hosts[i].host,
'uuid': affinity_uuids,
'deleted': False}):
extended_cost_matrix[i] = [1 + (float(j) / multiplier)
for j in xrange(num_instances + 1)]
else:
extended_cost_matrix = [[0 for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
return extended_cost_matrix

View File

@@ -0,0 +1,393 @@
# Copyright (c) 2014 Cisco Systems, Inc.
# 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.
"""Test case for solver scheduler affinity-cost."""
from nova import context
from nova import test
from nova_solverscheduler.scheduler.solvers import costs
from nova_solverscheduler.scheduler.solvers.costs import affinity_cost
from nova_solverscheduler.tests.scheduler import solver_scheduler_fakes \
as fakes
class TestAffinityCost(test.NoDBTestCase):
USES_DB = True
def setUp(self):
super(TestAffinityCost, self).setUp()
self.context = context.RequestContext('fake_usr', 'fake_proj')
self.host_manager = fakes.FakeSolverSchedulerHostManager()
self.cost_handler = costs.CostHandler()
self.cost_classes = self.cost_handler.get_matching_classes(
['nova_solverscheduler.scheduler.solvers.costs.'
'affinity_cost.AffinityCost'])
def _get_fake_hosts(self):
host1 = fakes.FakeSolverSchedulerHostState('host1', 'node1', {})
host2 = fakes.FakeSolverSchedulerHostState('host2', 'node2', {})
host3 = fakes.FakeSolverSchedulerHostState('host3', 'node3', {})
host4 = fakes.FakeSolverSchedulerHostState('host4', 'node4', {})
return [host1, host2, host3, host4]
def test_affinity_cost_multiplier(self):
self.flags(affinity_cost_multiplier=0.5, group='solver_scheduler')
self.assertEqual(0.5, affinity_cost.AffinityCost().cost_multiplier())
def test_get_extended_cost_matrix_one_inst(self):
fake_hosts = self._get_fake_hosts()
instance = fakes.FakeInstance(context=self.context,
params={'host': 'host2'})
instance_uuid = instance.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_same_host': instance_uuid}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[1, 0, -1, -2],
[0, -1, -2, -3],
[1, 0, -1, -2],
[1, 0, -1, -2]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_multi_inst(self):
fake_hosts = self._get_fake_hosts()
instance1 = fakes.FakeInstance(context=self.context,
params={'host': 'host1'})
instance2 = fakes.FakeInstance(context=self.context,
params={'host': 'host3'})
instance1_uuid = instance1.uuid
instance2_uuid = instance2.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_same_host':
[instance1_uuid, instance2_uuid]}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, -1, -2, -3],
[1, 0, -1, -2],
[0, -1, -2, -3],
[1, 0, -1, -2]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_change_multiplier(self):
self.flags(affinity_cost_multiplier=0.5, group='solver_scheduler')
fake_hosts = self._get_fake_hosts()
instance1 = fakes.FakeInstance(context=self.context,
params={'host': 'host1'})
instance2 = fakes.FakeInstance(context=self.context,
params={'host': 'host3'})
instance1_uuid = instance1.uuid
instance2_uuid = instance2.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_same_host':
[instance1_uuid, instance2_uuid]}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, -2, -4, -6],
[1, -1, -3, -5],
[0, -2, -4, -6],
[1, -1, -3, -5]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_zero_multiplier(self):
self.flags(affinity_cost_multiplier=0, group='solver_scheduler')
fake_hosts = self._get_fake_hosts()
instance = fakes.FakeInstance(context=self.context,
params={'host': 'host2'})
instance_uuid = instance.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_same_host': instance_uuid}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_no_instance_list(self):
fake_hosts = self._get_fake_hosts()
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_same_host': ''}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[-0, -1, -2, -3],
[-0, -1, -2, -3],
[-0, -1, -2, -3],
[-0, -1, -2, -3]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_no_hint(self):
fake_hosts = self._get_fake_hosts()
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
class TestAntiAffinityCost(test.NoDBTestCase):
USES_DB = True
def setUp(self):
super(TestAntiAffinityCost, self).setUp()
self.context = context.RequestContext('fake_usr', 'fake_proj')
self.host_manager = fakes.FakeSolverSchedulerHostManager()
self.cost_handler = costs.CostHandler()
self.cost_classes = self.cost_handler.get_matching_classes(
['nova_solverscheduler.scheduler.solvers.costs.'
'affinity_cost.AntiAffinityCost'])
def _get_fake_hosts(self):
host1 = fakes.FakeSolverSchedulerHostState('host1', 'node1', {})
host2 = fakes.FakeSolverSchedulerHostState('host2', 'node2', {})
host3 = fakes.FakeSolverSchedulerHostState('host3', 'node3', {})
host4 = fakes.FakeSolverSchedulerHostState('host4', 'node4', {})
return [host1, host2, host3, host4]
def test_anti_affinity_cost_multiplier(self):
self.flags(anti_affinity_cost_multiplier=2, group='solver_scheduler')
self.assertEqual(2,
affinity_cost.AntiAffinityCost().cost_multiplier())
def test_get_extended_cost_matrix_one_inst(self):
fake_hosts = self._get_fake_hosts()
instance = fakes.FakeInstance(context=self.context,
params={'host': 'host2'})
instance_uuid = instance.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_different_host': instance_uuid}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, 1, 2, 3],
[1, 2, 3, 4],
[0, 1, 2, 3],
[0, 1, 2, 3]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_multi_inst(self):
fake_hosts = self._get_fake_hosts()
instance1 = fakes.FakeInstance(context=self.context,
params={'host': 'host1'})
instance2 = fakes.FakeInstance(context=self.context,
params={'host': 'host3'})
instance1_uuid = instance1.uuid
instance2_uuid = instance2.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_different_host':
[instance1_uuid, instance2_uuid]}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[1, 2, 3, 4],
[0, 1, 2, 3],
[1, 2, 3, 4],
[0, 1, 2, 3]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_change_multiplier(self):
self.flags(anti_affinity_cost_multiplier=0.5,
group='solver_scheduler')
fake_hosts = self._get_fake_hosts()
instance1 = fakes.FakeInstance(context=self.context,
params={'host': 'host1'})
instance2 = fakes.FakeInstance(context=self.context,
params={'host': 'host3'})
instance1_uuid = instance1.uuid
instance2_uuid = instance2.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_different_host':
[instance1_uuid, instance2_uuid]}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[1, 3, 5, 7],
[0, 2, 4, 6],
[1, 3, 5, 7],
[0, 2, 4, 6]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_zero_multiplier(self):
self.flags(anti_affinity_cost_multiplier=0, group='solver_scheduler')
fake_hosts = self._get_fake_hosts()
instance = fakes.FakeInstance(context=self.context,
params={'host': 'host2'})
instance_uuid = instance.uuid
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_different_host': instance_uuid}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_no_instance_list(self):
fake_hosts = self._get_fake_hosts()
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {'soft_different_host': ''}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
def test_get_extended_cost_matrix_no_hint(self):
fake_hosts = self._get_fake_hosts()
fake_filter_properties = {
'context': self.context.elevated(),
'num_instances': 3,
'instance_uuids': ['fake_uuid_%s' % x for x in range(3)],
'scheduler_hints': {}
}
fake_cost = self.cost_classes[0]()
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)