Use InstanceList.get_count_by_hosts when deleting a compute service

This resolves the TODO in the delete service API code by using the
InstanceList.get_count_by_hosts method which is a simpler COUNT
query function rather than get_uuids_by_host.

Change-Id: I7de2526b72c2c080f314419792e4c6766907872d
This commit is contained in:
Matt Riedemann 2019-04-10 16:51:52 -04:00
parent d42a007425
commit 8e59a7d8be
2 changed files with 7 additions and 9 deletions

View File

@ -249,11 +249,9 @@ class ServiceController(wsgi.Controller):
# related compute_nodes record) delete since it will impact
# resource accounting in Placement and orphan the compute node
# resource provider.
# TODO(mriedem): Use a COUNT SQL query-based function instead
# of InstanceList.get_uuids_by_host for performance.
instance_uuids = objects.InstanceList.get_uuids_by_host(
context, service['host'])
if instance_uuids:
num_instances = objects.InstanceList.get_count_by_hosts(
context, [service['host']])
if num_instances:
raise webob.exc.HTTPConflict(
explanation=_('Unable to delete compute service that '
'is hosting instances. Migrate or '

View File

@ -703,13 +703,13 @@ class ServicesTestV21(test.TestCase):
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.delete, self.req, 1234)
@mock.patch('nova.objects.InstanceList.get_uuids_by_host',
return_value=objects.InstanceList())
@mock.patch('nova.objects.InstanceList.get_count_by_hosts',
return_value=0)
@mock.patch('nova.objects.HostMapping.get_by_host',
side_effect=exception.HostMappingNotFound(name='host1'))
@mock.patch('nova.objects.Service.destroy')
def test_compute_service_delete_host_mapping_not_found(
self, service_delete, get_instances, get_hm):
self, service_delete, get_hm, get_count_by_hosts):
"""Tests that we are still able to successfully delete a nova-compute
service even if the HostMapping is not found.
"""
@ -727,7 +727,7 @@ class ServicesTestV21(test.TestCase):
self.controller.delete(self.req, 2)
ctxt = self.req.environ['nova.context']
service_get_by_id.assert_called_once_with(ctxt, 2)
get_instances.assert_called_once_with(ctxt, 'host1')
get_count_by_hosts.assert_called_once_with(ctxt, ['host1'])
get_aggregates_by_host.assert_called_once_with(ctxt, 'host1')
delete_resource_provider.assert_called_once_with(
ctxt, service_get_by_id.return_value.compute_node,