Convert migrate reqeusts to use joins

The modifications to add joins for fixed_ip_requests allows us to
condense three network calls into one for both start and finish
migration.

Change-Id: I61ca03661f722c6406d39021b79a648f4374dfcd
Partial-bug: #1365606
This commit is contained in:
Vishvananda Ishaya 2014-09-05 00:32:32 -07:00
parent 31b0961c85
commit 2a38de9408
2 changed files with 35 additions and 40 deletions

View File

@ -503,20 +503,17 @@ class API(base_api.NetworkAPI):
self.network_rpcapi.setup_networks_on_host(context, **args)
def _is_multi_host(self, context, instance):
def _get_multi_addresses(self, context, instance):
try:
fixed_ips = objects.FixedIPList.get_by_instance_uuid(
context, instance['uuid'])
except exception.FixedIpNotFoundForInstance:
return False
network = objects.Network.get_by_id(context,
fixed_ips[0].network_id,
project_only='allow_none')
return network.multi_host
def _get_floating_ip_addresses(self, context, instance):
return objects.FloatingIP.get_addresses_by_instance(
context, instance)
return False, []
addresses = []
for fixed in fixed_ips:
for floating in fixed.floating_ips:
addresses.append(floating.address)
return fixed_ips[0].network.multi_host, addresses
@wrap_check_policy
def migrate_instance_start(self, context, instance, migration):
@ -531,9 +528,9 @@ class API(base_api.NetworkAPI):
floating_addresses=None,
)
if self._is_multi_host(context, instance):
args['floating_addresses'] = \
self._get_floating_ip_addresses(context, instance)
multi_host, addresses = self._get_multi_addresses(context, instance)
if multi_host:
args['floating_addresses'] = addresses
args['host'] = migration['source_compute']
self.network_rpcapi.migrate_instance_start(context, **args)
@ -551,9 +548,9 @@ class API(base_api.NetworkAPI):
floating_addresses=None,
)
if self._is_multi_host(context, instance):
args['floating_addresses'] = \
self._get_floating_ip_addresses(context, instance)
multi_host, addresses = self._get_multi_addresses(context, instance)
if multi_host:
args['floating_addresses'] = addresses
args['host'] = migration['dest_compute']
self.network_rpcapi.migrate_instance_finish(context, **args)

View File

@ -297,18 +297,13 @@ class ApiTestCase(test.TestCase):
def fake_mig_inst_method(*args, **kwargs):
info['kwargs'] = kwargs
def fake_is_multi_host(*args, **kwargs):
return multi_host
def fake_get_floaters(*args, **kwargs):
return ['fake_float1', 'fake_float2']
def fake_get_multi_addresses(*args, **kwargs):
return multi_host, ['fake_float1', 'fake_float2']
self.stubs.Set(network_rpcapi.NetworkAPI, method,
fake_mig_inst_method)
self.stubs.Set(self.network_api, '_is_multi_host',
fake_is_multi_host)
self.stubs.Set(self.network_api, '_get_floating_ip_addresses',
fake_get_floaters)
self.stubs.Set(self.network_api, '_get_multi_addresses',
fake_get_multi_addresses)
expected = {'instance_uuid': 'fake_uuid',
'source_compute': 'fake_compute_source',
@ -356,20 +351,22 @@ class ApiTestCase(test.TestCase):
self.stubs.Set(self.network_api.db, 'fixed_ip_get_by_instance',
fake_fixed_ip_get_by_instance)
instance = {'uuid': FAKE_UUID}
self.assertFalse(self.network_api._is_multi_host(self.context,
instance))
result, floats = self.network_api._get_multi_addresses(self.context,
instance)
self.assertFalse(result)
@mock.patch('nova.objects.fixed_ip.FixedIPList.get_by_instance_uuid')
@mock.patch('nova.objects.network.Network.get_by_id')
def _test_is_multi_host_network_has_no_project_id(self, is_multi_host,
net_get, fip_get):
net_get.return_value = objects.Network(id=123,
project_id=None,
multi_host=is_multi_host)
fip_get.return_value = [objects.FixedIP(
network_id=123, instance_uuid=FAKE_UUID)]
fip_get):
network = objects.Network(
id=123, project_id=None,
multi_host=is_multi_host)
fip_get.return_value = [
objects.FixedIP(instance_uuid=FAKE_UUID, network=network,
floating_ips=objects.FloatingIPList())]
instance = {'uuid': FAKE_UUID}
result = self.network_api._is_multi_host(self.context, instance)
result, floats = self.network_api._get_multi_addresses(self.context,
instance)
self.assertEqual(is_multi_host, result)
def test_is_multi_host_network_has_no_project_id_multi(self):
@ -379,16 +376,17 @@ class ApiTestCase(test.TestCase):
self._test_is_multi_host_network_has_no_project_id(False)
@mock.patch('nova.objects.fixed_ip.FixedIPList.get_by_instance_uuid')
@mock.patch('nova.objects.network.Network.get_by_id')
def _test_is_multi_host_network_has_project_id(self, is_multi_host,
net_get, fip_get):
net_get.return_value = objects.Network(
fip_get):
network = objects.Network(
id=123, project_id=self.context.project_id,
multi_host=is_multi_host)
fip_get.return_value = [
objects.FixedIP(network_id=123, instance_uuid=FAKE_UUID)]
objects.FixedIP(instance_uuid=FAKE_UUID, network=network,
floating_ips=objects.FloatingIPList())]
instance = {'uuid': FAKE_UUID}
result = self.network_api._is_multi_host(self.context, instance)
result, floats = self.network_api._get_multi_addresses(self.context,
instance)
self.assertEqual(is_multi_host, result)
def test_is_multi_host_network_has_project_id_multi(self):