Browse Source

use already loaded BDM in instance.create

In I18e7483ec9a484a660e1d306fdc0986e1d5f952b BDM was added to the instance
notifications. In general to add BDM to the payload an exta DB query is
needed. However the BDM is already locaded before the
notify_about_instance_create is called to send the notification. In this cases
loading the BDM again is unnecessary as the already loaded BDM can be reused.

This patch makes sure that notify_about_instance_create is called with the
already loaded BDM.

The remaining instance related versioned notification calls does not have
BDM already loaded.

Change-Id: Ic25de45c18348206f0309da6d4997f4bf336acb2
Closes-Bug: #1718226
tags/17.0.0.0b1
Balazs Gibizer 1 year ago
parent
commit
ad33ae5771

+ 20
- 10
nova/compute/manager.py View File

@@ -1933,7 +1933,8 @@ class ComputeManager(manager.Manager):
1933 1933
                 extra_usage_info={'image_name': image_name})
1934 1934
         compute_utils.notify_about_instance_create(
1935 1935
             context, instance, self.host,
1936
-            phase=fields.NotificationPhase.START)
1936
+            phase=fields.NotificationPhase.START,
1937
+            bdms=block_device_mapping)
1937 1938
 
1938 1939
         # NOTE(mikal): cache the keystone roles associated with the instance
1939 1940
         # at boot time for later reference
@@ -1980,14 +1981,16 @@ class ComputeManager(manager.Manager):
1980 1981
                     'create.error', fault=e)
1981 1982
                 compute_utils.notify_about_instance_create(
1982 1983
                     context, instance, self.host,
1983
-                    phase=fields.NotificationPhase.ERROR, exception=e)
1984
+                    phase=fields.NotificationPhase.ERROR, exception=e,
1985
+                    bdms=block_device_mapping)
1984 1986
         except exception.ComputeResourcesUnavailable as e:
1985 1987
             LOG.debug(e.format_message(), instance=instance)
1986 1988
             self._notify_about_instance_usage(context, instance,
1987 1989
                     'create.error', fault=e)
1988 1990
             compute_utils.notify_about_instance_create(
1989 1991
                     context, instance, self.host,
1990
-                    phase=fields.NotificationPhase.ERROR, exception=e)
1992
+                    phase=fields.NotificationPhase.ERROR, exception=e,
1993
+                    bdms=block_device_mapping)
1991 1994
             raise exception.RescheduledException(
1992 1995
                     instance_uuid=instance.uuid, reason=e.format_message())
1993 1996
         except exception.BuildAbortException as e:
@@ -1997,7 +2000,8 @@ class ComputeManager(manager.Manager):
1997 2000
                     'create.error', fault=e)
1998 2001
                 compute_utils.notify_about_instance_create(
1999 2002
                     context, instance, self.host,
2000
-                    phase=fields.NotificationPhase.ERROR, exception=e)
2003
+                    phase=fields.NotificationPhase.ERROR, exception=e,
2004
+                    bdms=block_device_mapping)
2001 2005
         except (exception.FixedIpLimitExceeded,
2002 2006
                 exception.NoMoreNetworks, exception.NoMoreFixedIps) as e:
2003 2007
             LOG.warning('No more network or fixed IP to be allocated',
@@ -2006,7 +2010,8 @@ class ComputeManager(manager.Manager):
2006 2010
                     'create.error', fault=e)
2007 2011
             compute_utils.notify_about_instance_create(
2008 2012
                     context, instance, self.host,
2009
-                    phase=fields.NotificationPhase.ERROR, exception=e)
2013
+                    phase=fields.NotificationPhase.ERROR, exception=e,
2014
+                    bdms=block_device_mapping)
2010 2015
             msg = _('Failed to allocate the network(s) with error %s, '
2011 2016
                     'not rescheduling.') % e.format_message()
2012 2017
             raise exception.BuildAbortException(instance_uuid=instance.uuid,
@@ -2021,7 +2026,8 @@ class ComputeManager(manager.Manager):
2021 2026
                     'create.error', fault=e)
2022 2027
             compute_utils.notify_about_instance_create(
2023 2028
                     context, instance, self.host,
2024
-                    phase=fields.NotificationPhase.ERROR, exception=e)
2029
+                    phase=fields.NotificationPhase.ERROR, exception=e,
2030
+                    bdms=block_device_mapping)
2025 2031
             msg = _('Failed to allocate the network(s), not rescheduling.')
2026 2032
             raise exception.BuildAbortException(instance_uuid=instance.uuid,
2027 2033
                     reason=msg)
@@ -2038,7 +2044,8 @@ class ComputeManager(manager.Manager):
2038 2044
                     'create.error', fault=e)
2039 2045
             compute_utils.notify_about_instance_create(
2040 2046
                     context, instance, self.host,
2041
-                    phase=fields.NotificationPhase.ERROR, exception=e)
2047
+                    phase=fields.NotificationPhase.ERROR, exception=e,
2048
+                    bdms=block_device_mapping)
2042 2049
             raise exception.BuildAbortException(instance_uuid=instance.uuid,
2043 2050
                     reason=e.format_message())
2044 2051
         except Exception as e:
@@ -2046,7 +2053,8 @@ class ComputeManager(manager.Manager):
2046 2053
                     'create.error', fault=e)
2047 2054
             compute_utils.notify_about_instance_create(
2048 2055
                     context, instance, self.host,
2049
-                    phase=fields.NotificationPhase.ERROR, exception=e)
2056
+                    phase=fields.NotificationPhase.ERROR, exception=e,
2057
+                    bdms=block_device_mapping)
2050 2058
             raise exception.RescheduledException(
2051 2059
                     instance_uuid=instance.uuid, reason=six.text_type(e))
2052 2060
 
@@ -2080,14 +2088,16 @@ class ComputeManager(manager.Manager):
2080 2088
                     'create.error', fault=e)
2081 2089
                 compute_utils.notify_about_instance_create(
2082 2090
                     context, instance, self.host,
2083
-                    phase=fields.NotificationPhase.ERROR, exception=e)
2091
+                    phase=fields.NotificationPhase.ERROR, exception=e,
2092
+                    bdms=block_device_mapping)
2084 2093
 
2085 2094
         self._update_scheduler_instance_info(context, instance)
2086 2095
         self._notify_about_instance_usage(context, instance, 'create.end',
2087 2096
                 extra_usage_info={'message': _('Success')},
2088 2097
                 network_info=network_info)
2089 2098
         compute_utils.notify_about_instance_create(context, instance,
2090
-                self.host, phase=fields.NotificationPhase.END)
2099
+                self.host, phase=fields.NotificationPhase.END,
2100
+                bdms=block_device_mapping)
2091 2101
 
2092 2102
     @contextlib.contextmanager
2093 2103
     def _build_resources(self, context, instance, requested_networks,

+ 5
- 2
nova/compute/utils.py View File

@@ -384,7 +384,7 @@ def notify_about_instance_action(context, instance, host, action, phase=None,
384 384
 @rpc.if_notifications_enabled
385 385
 def notify_about_instance_create(context, instance, host, phase=None,
386 386
                                  source=fields.NotificationSource.COMPUTE,
387
-                                 exception=None):
387
+                                 exception=None, bdms=None):
388 388
     """Send versioned notification about instance creation
389 389
 
390 390
     :param context: the request context
@@ -393,11 +393,14 @@ def notify_about_instance_create(context, instance, host, phase=None,
393 393
     :param phase: the phase of the creation
394 394
     :param source: the source of the notification
395 395
     :param exception: the thrown exception (used in error notifications)
396
+    :param bdms: BlockDeviceMappingList object for the instance. If it is not
397
+                provided then we will load it from the db if so configured
396 398
     """
397 399
     fault, priority = _get_fault_and_priority_from_exc(exception)
398 400
     payload = instance_notification.InstanceCreatePayload(
399 401
         instance=instance,
400
-        fault=fault)
402
+        fault=fault,
403
+        bdms=bdms)
401 404
     notification = instance_notification.InstanceCreateNotification(
402 405
         context=context,
403 406
         priority=priority,

+ 4
- 3
nova/notifications/objects/instance.py View File

@@ -204,10 +204,11 @@ class InstanceCreatePayload(InstanceActionPayload):
204 204
         'tags': fields.ListOfStringsField(),
205 205
     }
206 206
 
207
-    def __init__(self, instance, fault):
207
+    def __init__(self, instance, fault, bdms):
208 208
         super(InstanceCreatePayload, self).__init__(
209
-                instance=instance,
210
-                fault=fault)
209
+            instance=instance,
210
+            fault=fault,
211
+            bdms=bdms)
211 212
         self.keypairs = [keypair_payload.KeypairPayload(keypair=keypair)
212 213
                          for keypair in instance.keypairs]
213 214
         self.tags = [instance_tag.tag

+ 4
- 4
nova/tests/unit/compute/test_compute_mgr.py View File

@@ -4881,9 +4881,9 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
4881 4881
 
4882 4882
             mock_notify.assert_has_calls([
4883 4883
                 mock.call(self.context, self.instance, 'fake-mini',
4884
-                          phase='start'),
4884
+                          phase='start', bdms=[]),
4885 4885
                 mock.call(self.context, self.instance, 'fake-mini',
4886
-                          phase='error', exception=exc)])
4886
+                          phase='error', exception=exc, bdms=[])])
4887 4887
 
4888 4888
             save.assert_has_calls([
4889 4889
                 mock.call(),
@@ -5348,9 +5348,9 @@ class ComputeManagerBuildInstanceTestCase(test.NoDBTestCase):
5348 5348
 
5349 5349
             mock_notify_instance_create.assert_has_calls([
5350 5350
                 mock.call(self.context, self.instance, 'fake-mini',
5351
-                          phase='start'),
5351
+                          phase='start', bdms=[]),
5352 5352
                 mock.call(self.context, self.instance, 'fake-mini',
5353
-                          phase='end')])
5353
+                          phase='end', bdms=[])])
5354 5354
 
5355 5355
     def test_access_ip_set_when_instance_set_to_active(self):
5356 5356
 

Loading…
Cancel
Save