Merge "Add rack affinity costs for network optimization" into stable/juno

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

View File

@@ -0,0 +1,211 @@
# 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 import db
from nova.objects import instance as instance_obj
from nova.openstack.common.gettextutils import _
from nova.openstack.common import log as logging
from nova_solverscheduler.scheduler.solvers import costs as solver_costs
from nova_solverscheduler.scheduler.solvers import utils as solver_utils
affinity_cost_opts = [
cfg.FloatOpt('tenant_rack_affinity_cost_multiplier',
default=1.0,
help='Multiplier used for tenant rack affinity cost. '
'Must be a positive number.'),
cfg.FloatOpt('rack_network_affinity_cost_multiplier',
default=1.0,
help='Multiplier used for rack network affinity cost. '
'Must be a positive number.'),
]
CONF = cfg.CONF
CONF.register_opts(affinity_cost_opts, group='solver_scheduler')
LOG = logging.getLogger(__name__)
class TenantRackAffinityCost(solver_costs.BaseLinearCost):
"""Tenant Rack Affinity Cost tends to let scheduler place instances in
the racks that contain existing instances of the tenant.
If a rack has existing instances of the same tenant as that making request,
then the hosts in the rack will have a lower cost value.
"""
def __init__(self):
super(TenantRackAffinityCost, self).__init__()
def cost_multiplier(self):
return CONF.solver_scheduler.tenant_rack_affinity_cost_multiplier
def get_extended_cost_matrix(self, hosts, filter_properties):
num_hosts = len(hosts)
num_instances = filter_properties.get('num_instances')
extended_cost_matrix = [[0 for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
project_id = filter_properties['project_id']
context = filter_properties['context']
elevated = context.elevated()
host_racks_map = db.aggregate_host_get_by_metadata_key(elevated,
key='rack')
if not host_racks_map:
# try load from external source
host_racks_map = solver_utils.get_host_racks_config()
affinity_racks = set([])
affinity_hosts = set([])
# get affinity racks/hosts
for i in xrange(num_hosts):
host_name = hosts[i].host
host_racks = host_racks_map.get(host_name, set([]))
# if tenant not in host state then tenant network does not exist
# there, hence no need for further check
if project_id in hosts[i].projects:
affinity_hosts.add(host_name)
affinity_racks = affinity_racks.union(host_racks)
# check each hosts for affinity
for i in xrange(num_hosts):
host_name = hosts[i].host
host_racks = host_racks_map.get(host_name, set([]))
if (not any([rack in affinity_racks for rack in host_racks])) and (
host_name not in affinity_hosts):
extended_cost_matrix[i] = [1 for j
in xrange(num_instances + 1)]
else:
LOG.debug(_("%(host)s is in tenant affinity rack."),
{'host': host_name})
return extended_cost_matrix
class RackNetworkAffinityCost(solver_costs.BaseLinearCost):
"""Rack Network Affinity Cost tends to let scheduler place instances in
the racks that contain existing instances which are connected to the same
tenant networks as requested instances.
If a rack has existing instances that are connected to the same networks as
those of requested instances, then the hosts in the rack will have a lower
cost value.
"""
def __init__(self):
super(RackNetworkAffinityCost, self).__init__()
def cost_multiplier(self):
return CONF.solver_scheduler.rack_network_affinity_cost_multiplier
def get_extended_cost_matrix(self, hosts, filter_properties):
num_hosts = len(hosts)
num_instances = filter_properties.get('num_instances')
extended_cost_matrix = [[0 for j in xrange(num_instances + 1)]
for i in xrange(num_hosts)]
project_id = filter_properties['project_id']
context = filter_properties['context']
elevated = context.elevated()
scheduler_hints = filter_properties.get('scheduler_hints') or {}
affinity_networks = scheduler_hints.get('affinity_networks', [])
request_spec = filter_properties.get('request_spec', {})
instance_properties = request_spec.get('instance_properties', {})
requested_networks = request_spec.get('requested_networks', [])
requested_net_ids = set([])
# if a booting request, network ids are in request_spec
# NOTE (Xinyuan): need to backport the following change,
# Change-Id: I696a7ac1fe95e410d05e5fae1cccdbec39cba7ca
for req_network in requested_networks:
network_id = req_network.get('network_id', None)
if network_id:
requested_net_ids.add(network_id)
# if the instance already exists, get network info from its info_cache
instance_info_cache = instance_properties.get('info_cache', {})
instance_network_info = instance_info_cache.get('network_info', [])
for vif in instance_network_info:
instance_network = vif.get('network', {})
instance_network_id = instance_network.get('id', None)
if instance_network_id:
requested_net_ids.add(instance_network_id)
# additional network ids from scheduler hint
if isinstance(affinity_networks, six.string_types):
affinity_networks = [affinity_networks]
for affinity_network_id in affinity_networks:
requested_net_ids.add(affinity_network_id)
if not requested_net_ids:
return extended_cost_matrix
host_racks_map = db.aggregate_host_get_by_metadata_key(elevated,
key='rack')
if not host_racks_map:
# try load from external source
host_racks_map = solver_utils.get_host_racks_config()
affinity_racks = set([])
affinity_hosts = set([])
# get affinity racks/hosts
for i in xrange(num_hosts):
host_name = hosts[i].host
host_racks = host_racks_map.get(host_name, set([]))
# if tenant not in host state then tenant network does not exist
# there, hence no need for further check
if project_id not in hosts[i].projects:
continue
# if a host's racks already counted then no need to check the host
if host_racks and host_racks.issubset(affinity_racks):
continue
instances = instance_obj.InstanceList.get_by_host(elevated,
host_name, expected_attrs=['info_cache'])
if not instances:
continue
for inst in instances:
info_cache = inst.info_cache
if info_cache is None:
continue
network_info = info_cache.network_info or []
instance_networks = [vif['network']['id']
for vif in network_info]
if requested_net_ids.intersection(instance_networks):
if not host_racks:
affinity_hosts.add(host_name)
else:
affinity_racks = affinity_racks.union(host_racks)
# check each hosts for affinity
for i in xrange(num_hosts):
host_name = hosts[i].host
host_racks = host_racks_map.get(host_name, set([]))
if (not any([rack in affinity_racks for rack in host_racks])) and (
host_name not in affinity_hosts):
extended_cost_matrix[i] = [1 for j
in xrange(num_instances + 1)]
else:
LOG.debug(_("%(host)s is in network affinity rack."),
{'host': host_name})
return extended_cost_matrix

View File

@@ -0,0 +1,367 @@
# 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 rack affinity cost."""
import mock
from nova import context
from nova import db
from nova.network import model as network_model
from nova import test
from nova_solverscheduler.scheduler.solvers import costs
from nova_solverscheduler.scheduler.solvers.costs import rack_affinity_cost
from nova_solverscheduler.tests.scheduler import solver_scheduler_fakes \
as fakes
@mock.patch('nova.db.aggregate_host_get_by_metadata_key')
class TestTenantRackAffinityCost(test.NoDBTestCase):
USES_DB = True
def setUp(self):
super(TestTenantRackAffinityCost, 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.'
'rack_affinity_cost.TenantRackAffinityCost'])
def _get_fake_hosts(self):
host1 = fakes.FakeSolverSchedulerHostState('host1', 'node1',
{'projects': [self.context.project_id]})
host2 = fakes.FakeSolverSchedulerHostState('host2', 'node2',
{'Projects': []})
host3 = fakes.FakeSolverSchedulerHostState('host3', 'node3',
{'projects': [self.context.project_id]})
host4 = fakes.FakeSolverSchedulerHostState('host4', 'node4',
{'projects': []})
host5 = fakes.FakeSolverSchedulerHostState('host5', 'node5',
{'projects': []})
host6 = fakes.FakeSolverSchedulerHostState('host6', 'node6',
{'projects': []})
return [host1, host2, host3, host4, host5, host6]
def test_cost_multiplier(self, agg_mock):
self.flags(tenant_rack_affinity_cost_multiplier=0.5,
group='solver_scheduler')
self.assertEqual(0.5,
rack_affinity_cost.TenantRackAffinityCost().cost_multiplier())
def test_get_extended_cost_matrix_normal(self, agg_mock):
fake_hosts = self._get_fake_hosts()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1]]
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_cross_rack_host_1(self, agg_mock):
fake_hosts = self._get_fake_hosts()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3', 'rack1']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1]]
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_cross_rack_host_2(self, agg_mock):
fake_hosts = self._get_fake_hosts()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3
}
agg_mock.return_value = {
'host1': set(['rack1', 'rack3']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[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_incomplete_rack_config(self, agg_mock):
fake_hosts = self._get_fake_hosts()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host4': set(['rack2']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)
@mock.patch('nova.db.aggregate_host_get_by_metadata_key')
class TestRackNetworkAffinityCost(test.NoDBTestCase):
USES_DB = True
def setUp(self):
super(TestRackNetworkAffinityCost, 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.'
'rack_affinity_cost.RackNetworkAffinityCost'])
def _get_fake_hosts(self):
host1 = fakes.FakeSolverSchedulerHostState('host1', 'node1',
{'projects': [self.context.project_id]})
host2 = fakes.FakeSolverSchedulerHostState('host2', 'node2',
{'Projects': []})
host3 = fakes.FakeSolverSchedulerHostState('host3', 'node3',
{'projects': [self.context.project_id]})
host4 = fakes.FakeSolverSchedulerHostState('host4', 'node4',
{'projects': []})
host5 = fakes.FakeSolverSchedulerHostState('host5', 'node5',
{'projects': []})
host6 = fakes.FakeSolverSchedulerHostState('host6', 'node6',
{'projects': []})
return [host1, host2, host3, host4, host5, host6]
def _setup_instances(self):
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
nwinfo1 = network_model.NetworkInfo.hydrate(
[{'network': {'id': 'net1'}}])
nwinfo2 = network_model.NetworkInfo.hydrate(
[{'network': {'id': 'net2'}}])
db.instance_info_cache_update(self.context, instance1_uuid,
{'network_info': nwinfo1.json()})
db.instance_info_cache_update(self.context, instance2_uuid,
{'network_info': nwinfo2.json()})
def test_cost_multiplier(self, agg_mock):
self.flags(rack_network_affinity_cost_multiplier=0.5,
group='solver_scheduler')
self.assertEqual(0.5,
rack_affinity_cost.RackNetworkAffinityCost().cost_multiplier())
def test_get_extended_cost_matrix_one_requested_network(self, agg_mock):
fake_hosts = self._get_fake_hosts()
self._setup_instances()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3,
'request_spec': {'requested_networks': [{'network_id': 'net1'}]}
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
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_requested_networks(self, agg_mock):
fake_hosts = self._get_fake_hosts()
self._setup_instances()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3,
'request_spec': {'requested_networks': [{'network_id': 'net1'},
{'network_id': 'net2'}]}
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1]]
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_requested_network(self, agg_mock):
fake_hosts = self._get_fake_hosts()
self._setup_instances()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3,
'request_spec': {'requested_networks': []}
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[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_incomplete_rack_config(self, agg_mock):
fake_hosts = self._get_fake_hosts()
self._setup_instances()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3,
'request_spec': {'requested_networks': [{'network_id': 'net1'},
{'network_id': 'net2'}]}
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host4': set(['rack2']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
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_with_hint_and_inst_prop(self, agg_mock):
fake_hosts = self._get_fake_hosts()
self._setup_instances()
fake_cost = self.cost_classes[0]()
fake_filter_properties = {
'context': self.context,
'project_id': self.context.project_id,
'num_instances': 3,
'scheduler_hints': {'affinity_networks': ['net1']},
'request_spec': {'requested_networks': [],
'instance_properties': {'info_cache':
{'network_info': [{'network': {'id': 'net2'}}]}}}
}
agg_mock.return_value = {
'host1': set(['rack1']),
'host2': set(['rack1']),
'host3': set(['rack2']),
'host4': set(['rack2']),
'host5': set(['rack3']),
'host6': set(['rack3'])
}
expected_x_cost_mat = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[1, 1, 1, 1],
[1, 1, 1, 1]]
x_cost_mat = fake_cost.get_extended_cost_matrix(fake_hosts,
fake_filter_properties)
self.assertEqual(expected_x_cost_mat, x_cost_mat)