Browse Source

use already loaded BDM in instance.<action> (2)

I8849ae0f54605e003d5b294ca3d66dcef89d7d27 made it possible for
_get_instance_block_device_info to take a BDM parameter instead of
loading the BDM from the db. This allow us to load the BDM a bit
earlier in the call chain and pass that BDM to the notification calls
too.

The remaining calls of the notify_about_instance_action does not have
the BDM loaded already.

Change-Id: Icc3ffe4037a44f4f323bec2f80d99ca226742e22
Related-Bug: #1718226
tags/17.0.0.0b1
Balazs Gibizer 1 year ago
parent
commit
41a0f51341

+ 25
- 14
nova/compute/manager.py View File

@@ -3024,8 +3024,10 @@ class ComputeManager(manager.Manager):
3024 3024
         context = context.elevated()
3025 3025
         LOG.info("Rebooting instance", instance=instance)
3026 3026
 
3027
-        block_device_info = self._get_instance_block_device_info(context,
3028
-                                                                 instance)
3027
+        bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(
3028
+            context, instance.uuid)
3029
+        block_device_info = self._get_instance_block_device_info(
3030
+            context, instance, bdms)
3029 3031
 
3030 3032
         network_info = self.network_api.get_instance_nw_info(context, instance)
3031 3033
 
@@ -3033,7 +3035,8 @@ class ComputeManager(manager.Manager):
3033 3035
         compute_utils.notify_about_instance_action(
3034 3036
             context, instance, self.host,
3035 3037
             action=fields.NotificationAction.REBOOT,
3036
-            phase=fields.NotificationPhase.START
3038
+            phase=fields.NotificationPhase.START,
3039
+            bdms=bdms
3037 3040
         )
3038 3041
 
3039 3042
         instance.power_state = self._get_power_state(context, instance)
@@ -3088,7 +3091,7 @@ class ComputeManager(manager.Manager):
3088 3091
                         context, instance, self.host,
3089 3092
                         action=fields.NotificationAction.REBOOT,
3090 3093
                         phase=fields.NotificationPhase.ERROR,
3091
-                        exception=error
3094
+                        exception=error, bdms=bdms
3092 3095
                     )
3093 3096
                     ctxt.reraise = False
3094 3097
                 else:
@@ -3111,7 +3114,8 @@ class ComputeManager(manager.Manager):
3111 3114
         compute_utils.notify_about_instance_action(
3112 3115
             context, instance, self.host,
3113 3116
             action=fields.NotificationAction.REBOOT,
3114
-            phase=fields.NotificationPhase.END
3117
+            phase=fields.NotificationPhase.END,
3118
+            bdms=bdms
3115 3119
         )
3116 3120
 
3117 3121
     @delete_image_on_error
@@ -4071,7 +4075,7 @@ class ComputeManager(manager.Manager):
4071 4075
         instance.flavor = instance_type
4072 4076
 
4073 4077
     def _finish_resize(self, context, instance, migration, disk_info,
4074
-                       image_meta):
4078
+                       image_meta, bdms):
4075 4079
         resize_instance = False
4076 4080
         old_instance_type_id = migration['old_instance_type_id']
4077 4081
         new_instance_type_id = migration['new_instance_type_id']
@@ -4111,10 +4115,10 @@ class ComputeManager(manager.Manager):
4111 4115
             network_info=network_info)
4112 4116
         compute_utils.notify_about_instance_action(context, instance,
4113 4117
                self.host, action=fields.NotificationAction.RESIZE_FINISH,
4114
-               phase=fields.NotificationPhase.START)
4118
+               phase=fields.NotificationPhase.START, bdms=bdms)
4115 4119
 
4116 4120
         block_device_info = self._get_instance_block_device_info(
4117
-                            context, instance, refresh_conn_info=True)
4121
+            context, instance, refresh_conn_info=True, bdms=bdms)
4118 4122
 
4119 4123
         # NOTE(mriedem): If the original vm_state was STOPPED, we don't
4120 4124
         # automatically power on the instance after it's migrated
@@ -4155,11 +4159,14 @@ class ComputeManager(manager.Manager):
4155 4159
         new host machine.
4156 4160
 
4157 4161
         """
4162
+        bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(
4163
+            context, instance.uuid)
4164
+
4158 4165
         with self._error_out_instance_on_exception(context, instance), \
4159 4166
              errors_out_migration_ctxt(migration):
4160 4167
             image_meta = objects.ImageMeta.from_dict(image)
4161 4168
             network_info = self._finish_resize(context, instance, migration,
4162
-                                               disk_info, image_meta)
4169
+                                               disk_info, image_meta, bdms)
4163 4170
 
4164 4171
         self._update_scheduler_instance_info(context, instance)
4165 4172
         self._notify_about_instance_usage(
@@ -4167,7 +4174,7 @@ class ComputeManager(manager.Manager):
4167 4174
             network_info=network_info)
4168 4175
         compute_utils.notify_about_instance_action(context, instance,
4169 4176
                self.host, action=fields.NotificationAction.RESIZE_FINISH,
4170
-               phase=fields.NotificationPhase.END)
4177
+               phase=fields.NotificationPhase.END, bdms=bdms)
4171 4178
 
4172 4179
     @wrap_exception()
4173 4180
     @wrap_instance_fault
@@ -4349,13 +4356,17 @@ class ComputeManager(manager.Manager):
4349 4356
         LOG.info('Resuming', instance=instance)
4350 4357
 
4351 4358
         self._notify_about_instance_usage(context, instance, 'resume.start')
4359
+
4360
+        bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(
4361
+            context, instance.uuid)
4362
+        block_device_info = self._get_instance_block_device_info(
4363
+            context, instance, bdms)
4364
+
4352 4365
         compute_utils.notify_about_instance_action(context, instance,
4353 4366
             self.host, action=fields.NotificationAction.RESUME,
4354
-            phase=fields.NotificationPhase.START)
4367
+            phase=fields.NotificationPhase.START, bdms=bdms)
4355 4368
 
4356 4369
         network_info = self.network_api.get_instance_nw_info(context, instance)
4357
-        block_device_info = self._get_instance_block_device_info(
4358
-                            context, instance)
4359 4370
 
4360 4371
         with self._error_out_instance_on_exception(context, instance,
4361 4372
              instance_state=instance.vm_state):
@@ -4373,7 +4384,7 @@ class ComputeManager(manager.Manager):
4373 4384
         self._notify_about_instance_usage(context, instance, 'resume.end')
4374 4385
         compute_utils.notify_about_instance_action(context, instance,
4375 4386
             self.host, action=fields.NotificationAction.RESUME,
4376
-            phase=fields.NotificationPhase.END)
4387
+            phase=fields.NotificationPhase.END, bdms=bdms)
4377 4388
 
4378 4389
     @wrap_exception()
4379 4390
     @reverts_task_state

+ 28
- 13
nova/tests/unit/compute/test_compute.py View File

@@ -2658,13 +2658,17 @@ class ComputeTestCase(BaseTestCase,
2658 2658
 
2659 2659
         self.compute.terminate_instance(self.context, instance, [], [])
2660 2660
 
2661
+    @mock.patch.object(objects.BlockDeviceMappingList, 'get_by_instance_uuid')
2661 2662
     @mock.patch.object(nova.compute.utils, 'notify_about_instance_action')
2662 2663
     @mock.patch('nova.context.RequestContext.elevated')
2663
-    def test_resume_notifications(self, mock_context, mock_notify):
2664
+    def test_resume_notifications(self, mock_context, mock_notify,
2665
+                                  mock_get_bdms):
2664 2666
         # ensure instance can be suspended and resumed.
2665 2667
         context = self.context
2666 2668
         mock_context.return_value = context
2667 2669
         instance = self._create_fake_instance_obj()
2670
+        bdms = block_device_obj.block_device_make_list(self.context, [])
2671
+        mock_get_bdms.return_value = bdms
2668 2672
         self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
2669 2673
                                             block_device_mapping=[])
2670 2674
         instance.task_state = task_states.SUSPENDING
@@ -2682,9 +2686,9 @@ class ComputeTestCase(BaseTestCase,
2682 2686
                          'compute.instance.resume.end')
2683 2687
         mock_notify.assert_has_calls([
2684 2688
             mock.call(context, instance, 'fake-mini',
2685
-                      action='resume', phase='start'),
2689
+                      action='resume', phase='start', bdms=bdms),
2686 2690
             mock.call(context, instance, 'fake-mini',
2687
-                      action='resume', phase='end')])
2691
+                      action='resume', phase='end', bdms=bdms)])
2688 2692
         self.compute.terminate_instance(self.context, instance, [], [])
2689 2693
 
2690 2694
     def test_resume_no_old_state(self):
@@ -2899,6 +2903,7 @@ class ComputeTestCase(BaseTestCase,
2899 2903
                                       on_shared_storage=False)
2900 2904
         self.compute.terminate_instance(self.context, instance, [], [])
2901 2905
 
2906
+    @mock.patch.object(objects.BlockDeviceMappingList, 'get_by_instance_uuid')
2902 2907
     @mock.patch.object(compute_manager.ComputeManager,
2903 2908
                            '_get_instance_block_device_info')
2904 2909
     @mock.patch.object(compute_manager.ComputeManager,
@@ -2909,7 +2914,7 @@ class ComputeTestCase(BaseTestCase,
2909 2914
     @mock.patch('nova.compute.utils.notify_about_instance_action')
2910 2915
     def _test_reboot(self, soft, mock_notify_action, mock_get_power,
2911 2916
                      mock_get_orig, mock_update, mock_notify_usage,
2912
-                     mock_get_blk, test_delete=False,
2917
+                     mock_get_blk, mock_get_bdms, test_delete=False,
2913 2918
                      test_unrescue=False, fail_reboot=False,
2914 2919
                      fail_running=False):
2915 2920
         reboot_type = soft and 'SOFT' or 'HARD'
@@ -2959,6 +2964,9 @@ class ComputeTestCase(BaseTestCase,
2959 2964
                    task_state=expected_task,
2960 2965
                    launched_at=timeutils.utcnow()))
2961 2966
 
2967
+        bdms = block_device_obj.block_device_make_list(self.context, [])
2968
+        mock_get_bdms.return_value = bdms
2969
+
2962 2970
         if test_unrescue:
2963 2971
             instance.vm_state = vm_states.RESCUED
2964 2972
         instance.obj_reset_changes()
@@ -2985,7 +2993,7 @@ class ComputeTestCase(BaseTestCase,
2985 2993
         notify_call_list = [mock.call(econtext, instance, 'reboot.start')]
2986 2994
         notify_action_call_list = [
2987 2995
             mock.call(econtext, instance, 'fake-mini', action='reboot',
2988
-                      phase='start')]
2996
+                      phase='start', bdms=bdms)]
2989 2997
 
2990 2998
         ps_call_list = [mock.call(econtext, instance)]
2991 2999
         db_call_list = [mock.call(econtext, instance['uuid'],
@@ -3052,7 +3060,7 @@ class ComputeTestCase(BaseTestCase,
3052 3060
                                               'reboot.end'))
3053 3061
             notify_action_call_list.append(
3054 3062
                 mock.call(econtext, instance, 'fake-mini',
3055
-                          action='reboot', phase='end'))
3063
+                          action='reboot', phase='end', bdms=bdms))
3056 3064
         elif fail_reboot and not fail_running:
3057 3065
             mock_get_orig.side_effect = chain(mock_get_orig.side_effect,
3058 3066
                                               [fault])
@@ -3074,12 +3082,13 @@ class ComputeTestCase(BaseTestCase,
3074 3082
                                                   'reboot.error', fault=fault))
3075 3083
                 notify_action_call_list.append(
3076 3084
                     mock.call(econtext, instance, 'fake-mini',
3077
-                              action='reboot', phase='error', exception=fault))
3085
+                              action='reboot', phase='error', exception=fault,
3086
+                              bdms=bdms))
3078 3087
             notify_call_list.append(mock.call(econtext, instance,
3079 3088
                                               'reboot.end'))
3080 3089
             notify_action_call_list.append(
3081 3090
                 mock.call(econtext, instance, 'fake-mini',
3082
-                          action='reboot', phase='end'))
3091
+                          action='reboot', phase='end', bdms=bdms))
3083 3092
 
3084 3093
         if not fail_reboot or fail_running:
3085 3094
             self.compute.reboot_instance(self.context, instance=instance,
@@ -3093,7 +3102,7 @@ class ComputeTestCase(BaseTestCase,
3093 3102
                                   reboot_type=reboot_type)
3094 3103
 
3095 3104
         self.assertEqual(expected_call_info, reboot_call_info)
3096
-        mock_get_blk.assert_called_once_with(econtext, instance)
3105
+        mock_get_blk.assert_called_once_with(econtext, instance, bdms)
3097 3106
         mock_get_nw.assert_called_once_with(econtext, instance)
3098 3107
         mock_notify_usage.assert_has_calls(notify_call_list)
3099 3108
         mock_notify_action.assert_has_calls(notify_action_call_list)
@@ -4589,6 +4598,9 @@ class ComputeTestCase(BaseTestCase,
4589 4598
         network_api = self.compute.network_api
4590 4599
 
4591 4600
         with test.nested(
4601
+            mock.patch.object(objects.BlockDeviceMappingList,
4602
+                              'get_by_instance_uuid',
4603
+                              return_value='fake_bdms'),
4592 4604
             mock.patch.object(network_api, 'setup_networks_on_host'),
4593 4605
             mock.patch.object(network_api, 'migrate_instance_finish'),
4594 4606
             mock.patch.object(self.compute.network_api,
@@ -4599,7 +4611,7 @@ class ComputeTestCase(BaseTestCase,
4599 4611
             mock.patch.object(self.compute, '_get_instance_block_device_info'),
4600 4612
             mock.patch.object(migration, 'save'),
4601 4613
             mock.patch.object(instance, 'save'),
4602
-        ) as (mock_setup, mock_net_mig, mock_get_nw, mock_notify,
4614
+        ) as (mock_get_bdm, mock_setup, mock_net_mig, mock_get_nw, mock_notify,
4603 4615
               mock_notify_action, mock_virt_mig, mock_get_blk, mock_mig_save,
4604 4616
               mock_inst_save):
4605 4617
             def _mig_save():
@@ -4662,9 +4674,11 @@ class ComputeTestCase(BaseTestCase,
4662 4674
                           network_info='fake-nwinfo1')])
4663 4675
             mock_notify_action.assert_has_calls([
4664 4676
                 mock.call(self.context, instance, 'fake-mini',
4665
-                          action='resize_finish', phase='start'),
4677
+                          action='resize_finish', phase='start',
4678
+                          bdms='fake_bdms'),
4666 4679
                 mock.call(self.context, instance, 'fake-mini',
4667
-                          action='resize_finish', phase='end')])
4680
+                          action='resize_finish', phase='end',
4681
+                          bdms='fake_bdms')])
4668 4682
             # nova.conf sets the default flavor to m1.small and the test
4669 4683
             # sets the default flavor to m1.tiny so they should be different
4670 4684
             # which makes this a resize
@@ -4673,7 +4687,8 @@ class ComputeTestCase(BaseTestCase,
4673 4687
                 test.MatchType(objects.ImageMeta), resize_instance,
4674 4688
                 'fake-bdminfo', power_on)
4675 4689
             mock_get_blk.assert_called_once_with(self.context, instance,
4676
-                                                 refresh_conn_info=True)
4690
+                                                 refresh_conn_info=True,
4691
+                                                 bdms='fake_bdms')
4677 4692
             mock_inst_save.assert_has_calls(inst_call_list)
4678 4693
             mock_mig_save.assert_called_once_with()
4679 4694
 

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

@@ -5497,7 +5497,10 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
5497 5497
             mock.patch.object(db, 'instance_fault_create'),
5498 5498
             mock.patch.object(self.compute, '_update_resource_tracker'),
5499 5499
             mock.patch.object(self.instance, 'save'),
5500
-        ) as (_finish_resize, fault_create, instance_update, instance_save):
5500
+            mock.patch.object(objects.BlockDeviceMappingList,
5501
+                              'get_by_instance_uuid')
5502
+        ) as (_finish_resize, fault_create, instance_update, instance_save,
5503
+              get_bdm):
5501 5504
             fault_create.return_value = (
5502 5505
                 test_instance_fault.fake_faults['fake-uuid'][0])
5503 5506
             yield _finish_resize

Loading…
Cancel
Save