Pass service ref to init_host(), if exists

This just adds the service ref to init_host() so that we can pass it
(if it exists) at startup. On the first run, this will be None, so
we know that we don't have an existing service record and thus do not
need to do any migration.

Related to blueprint stable-compute-uuid

Change-Id: I1491c9b234ef0c262b5ed01d4c138eba8dedff77
This commit is contained in:
Dan Smith 2022-11-02 09:34:35 -07:00
parent 3b33b0938e
commit cf111d1001
5 changed files with 22 additions and 18 deletions

View File

@ -1492,7 +1492,7 @@ class ComputeManager(manager.Manager):
"then you can ignore this warning.", node_name)
return nodes_by_uuid
def init_host(self):
def init_host(self, service_ref):
"""Initialization for a standalone compute service."""
if CONF.pci.device_spec:

View File

@ -103,12 +103,15 @@ class Manager(PeriodicTasks, metaclass=ManagerMeta):
"""Tasks to be run at a periodic interval."""
return self.run_periodic_tasks(context, raise_on_error=raise_on_error)
def init_host(self):
def init_host(self, service_ref):
"""Hook to do additional manager initialization when one requests
the service be started. This is called before any service record
is created.
is created, but if one already exists for this service, it is
provided.
Child classes should override this method.
:param service_ref: An objects.Service if one exists, else None.
"""
pass

View File

@ -156,11 +156,11 @@ class Service(service.Service):
LOG.info('Starting %(topic)s node (version %(version)s)',
{'topic': self.topic, 'version': verstr})
self.basic_config_check()
self.manager.init_host()
self.model_disconnected = False
ctxt = context.get_admin_context()
self.service_ref = objects.Service.get_by_host_and_binary(
ctxt, self.host, self.binary)
self.manager.init_host(self.service_ref)
self.model_disconnected = False
if self.service_ref:
_update_service_ref(self.service_ref)

View File

@ -933,7 +933,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
hypervisor_hostname='fake-node')
mock_get_nodes.return_value = {uuids.our_node_uuid: our_node}
self.compute.init_host()
self.compute.init_host(None)
mock_validate_pinning.assert_called_once_with(inst_list)
mock_validate_vtpm.assert_called_once_with(inst_list)
@ -978,7 +978,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
mock_get_nodes.return_value = {
uuids.cn_uuid1: objects.ComputeNode(
uuid=uuids.cn_uuid1, hypervisor_hostname='node1')}
self.compute.init_host()
self.compute.init_host(None)
mock_error_interrupted.assert_called_once_with(
test.MatchType(nova.context.RequestContext), set(),
@ -997,7 +997,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
mock_instance_list.get_by_host.return_value = []
with mock.patch.object(self.compute, 'driver') as mock_driver:
self.compute.init_host()
self.compute.init_host(None)
mock_driver.init_host.assert_called_once_with(host='fake-mini')
self.compute.cleanup_host()
@ -1086,7 +1086,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
'remove_provider_tree_from_instance_allocation')
) as (mock_get_net, mock_remove_allocation):
self.compute.init_host()
self.compute.init_host(None)
mock_remove_allocation.assert_called_once_with(
self.context, deleted_instance.uuid, uuids.our_node_uuid)
@ -1143,7 +1143,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
hypervisor_hostname='fake-node')
mock_get_nodes.return_value = {uuids.our_node_uuid: our_node}
self.compute.init_host()
self.compute.init_host(None)
mock_init_instance.assert_called_once_with(
self.context, active_instance)
@ -1216,7 +1216,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
def test_init_host_disk_devices_configuration_failure(self):
self.flags(max_disk_devices_to_attach=0, group='compute')
self.assertRaises(exception.InvalidConfiguration,
self.compute.init_host)
self.compute.init_host, None)
@mock.patch.object(objects.InstanceList, 'get_by_host',
new=mock.Mock())
@ -1230,7 +1230,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
mock_validate_pinning.side_effect = exception.InvalidConfiguration
self.assertRaises(exception.InvalidConfiguration,
self.compute.init_host)
self.compute.init_host, None)
@mock.patch.object(objects.InstanceList, 'get_by_host',
new=mock.Mock())
@ -1247,7 +1247,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
mock_validate_vtpm.side_effect = exception.InvalidConfiguration
self.assertRaises(exception.InvalidConfiguration,
self.compute.init_host)
self.compute.init_host, None)
@mock.patch.object(objects.Instance, 'save')
@mock.patch.object(objects.InstanceList, 'get_by_filters')
@ -5145,7 +5145,7 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase,
group='pci'
)
self.assertRaises(exception.PciDeviceInvalidDeviceName,
self.compute.init_host)
self.compute.init_host, None)
@mock.patch('nova.compute.manager.ComputeManager._instance_update')
def test_error_out_instance_on_exception_not_implemented_err(self,

View File

@ -128,7 +128,7 @@ class ServiceTestCase(test.NoDBTestCase):
serv.manager.additional_endpoints = []
serv.start()
# init_host is called before any service record is created
serv.manager.init_host.assert_called_once_with()
serv.manager.init_host.assert_called_once_with(None)
mock_get_by_host_and_binary.assert_called_once_with(mock.ANY,
self.host, self.binary)
mock_create.assert_called_once_with()
@ -186,7 +186,7 @@ class ServiceTestCase(test.NoDBTestCase):
mock_create.side_effect = ex
serv.manager = mock_manager
self.assertRaises(test.TestingException, serv.start)
serv.manager.init_host.assert_called_with()
serv.manager.init_host.assert_called_with(None)
mock_get_by_host_and_binary.assert_has_calls([
mock.call(mock.ANY, self.host, self.binary),
mock.call(mock.ANY, self.host, self.binary)])
@ -216,7 +216,7 @@ class ServiceTestCase(test.NoDBTestCase):
serv.manager.service_name = self.topic
serv.manager.additional_endpoints = []
serv.start()
serv.manager.init_host.assert_called_once_with()
serv.manager.init_host.assert_called_once_with(None)
mock_get_by_host_and_binary.assert_called_once_with(mock.ANY,
self.host,
self.binary)
@ -241,7 +241,8 @@ class ServiceTestCase(test.NoDBTestCase):
serv.manager.additional_endpoints = []
serv.start()
serv.manager.init_host.assert_called_with()
serv.manager.init_host.assert_called_with(
mock_svc_get_by_host_and_binary.return_value)
serv.stop()
serv.manager.cleanup_host.assert_called_with()