Avoid unnessary _get_power_state call

the result of _get_power_state can be reused and no need to
call it twice which don't have any state change.

Change-Id: I3c495031d98b35734f37139ac1b1c3a4d25d0a8f
Closes-Bug: 1589960
This commit is contained in:
jichenjc 2016-05-02 10:59:40 +08:00
parent 284d67666d
commit 6211543493
2 changed files with 29 additions and 43 deletions

View File

@ -874,8 +874,9 @@ class ComputeManager(manager.Manager):
self._set_instance_obj_error_state(context, instance)
return
try_reboot, reboot_type = self._retry_reboot(context, instance)
current_power_state = self._get_power_state(context, instance)
try_reboot, reboot_type = self._retry_reboot(context, instance,
current_power_state)
if try_reboot:
LOG.debug("Instance in transitional state (%(task_state)s) at "
@ -1038,8 +1039,7 @@ class ComputeManager(manager.Manager):
LOG.debug('Hypervisor driver does not support '
'firewall rules', instance=instance)
def _retry_reboot(self, context, instance):
current_power_state = self._get_power_state(context, instance)
def _retry_reboot(self, context, instance, current_power_state):
current_task_state = instance.task_state
retry_reboot = False
reboot_type = compute_utils.get_reboot_type(current_task_state,

View File

@ -643,8 +643,6 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
instance).AndReturn(power_state.SHUTDOWN)
self.compute._get_power_state(mox.IgnoreArg(),
instance).AndReturn(power_state.SHUTDOWN)
self.compute._get_power_state(mox.IgnoreArg(),
instance).AndReturn(power_state.SHUTDOWN)
self.compute.driver.plug_vifs(instance, mox.IgnoreArg())
self.compute._get_instance_block_device_info(mox.IgnoreArg(),
instance).AndReturn('fake-bdm')
@ -840,8 +838,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
self.mox.StubOutWithMock(instance, 'save')
self.mox.StubOutWithMock(self.compute, '_retry_reboot')
self.compute._retry_reboot(self.context, instance).AndReturn(
(False, None))
self.compute._retry_reboot(self.context, instance,
power_state.SHUTDOWN).AndReturn((False, None))
compute_utils.get_nw_info_for_instance(instance).AndReturn(
network_model.NetworkInfo())
self.compute.driver.plug_vifs(instance, [])
@ -2213,68 +2211,56 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
instance.uuid = uuids.instance
instance.task_state = task_states.REBOOT_PENDING
instance.vm_state = vm_states.ACTIVE
with mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.RUNNING):
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'SOFT')
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance, power_state.RUNNING)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'SOFT')
def test_retry_reboot_pending_hard(self):
instance = objects.Instance(self.context)
instance.uuid = uuids.instance
instance.task_state = task_states.REBOOT_PENDING_HARD
instance.vm_state = vm_states.ACTIVE
with mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.RUNNING):
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance, power_state.RUNNING)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
def test_retry_reboot_starting_soft_off(self):
instance = objects.Instance(self.context)
instance.uuid = uuids.instance
instance.task_state = task_states.REBOOT_STARTED
with mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.NOSTATE):
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance, power_state.NOSTATE)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
def test_retry_reboot_starting_hard_off(self):
instance = objects.Instance(self.context)
instance.uuid = uuids.instance
instance.task_state = task_states.REBOOT_STARTED_HARD
with mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.NOSTATE):
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance, power_state.NOSTATE)
self.assertTrue(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
def test_retry_reboot_starting_hard_on(self):
instance = objects.Instance(self.context)
instance.uuid = uuids.instance
instance.task_state = task_states.REBOOT_STARTED_HARD
with mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.RUNNING):
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance)
self.assertFalse(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance, power_state.RUNNING)
self.assertFalse(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
def test_retry_reboot_no_reboot(self):
instance = objects.Instance(self.context)
instance.uuid = uuids.instance
instance.task_state = 'bar'
with mock.patch.object(self.compute, '_get_power_state',
return_value=power_state.RUNNING):
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance)
self.assertFalse(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
allow_reboot, reboot_type = self.compute._retry_reboot(
context, instance, power_state.RUNNING)
self.assertFalse(allow_reboot)
self.assertEqual(reboot_type, 'HARD')
@mock.patch('nova.objects.BlockDeviceMapping.get_by_volume_and_instance')
@mock.patch('nova.compute.manager.ComputeManager._driver_detach_volume')