From 3d5cb0facecf62d9577ead6e129e83a2c4509273 Mon Sep 17 00:00:00 2001 From: zhangtralon Date: Sun, 31 Aug 2014 10:55:39 +0800 Subject: [PATCH] check the block_device_allocate_retries we need to check the value of the configuration item block_device_allocate_retries in order to ensure to retry at least once,done like that configuration item network_allocate_retries Change-Id: Ia5b65bd160cb4e740d21c1b51d234e14e33135e7 Closes-Bug:#1363558 --- nova/compute/manager.py | 18 ++++++++++++++---- nova/tests/compute/test_compute.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index a6098236fdcc..ab28b01ca331 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -1203,18 +1203,28 @@ class ComputeManager(manager.Manager): # TODO(yamahata): creating volume simultaneously # reduces creation time? # TODO(yamahata): eliminate dumb polling - attempts = 0 start = time.time() - while attempts < CONF.block_device_allocate_retries: + retries = CONF.block_device_allocate_retries + if retries < 0: + LOG.warn(_LW("Treating negative config value (%(retries)s) for " + "'block_device_retries' as 0."), + {'retries': retries}) + # (1) treat negative config value as 0 + # (2) the configured value is 0, one attempt should be made + # (3) the configured value is > 0, then the total number attempts + # is (retries + 1) + attempts = 1 + if retries >= 1: + attempts = retries + 1 + for attempt in range(1, attempts + 1): volume = self.volume_api.get(context, vol_id) volume_status = volume['status'] if volume_status not in ['creating', 'downloading']: if volume_status != 'available': LOG.warn(_("Volume id: %s finished being created but was" " not set as 'available'"), vol_id) - return attempts + 1 + return attempt greenthread.sleep(CONF.block_device_allocate_retries_interval) - attempts += 1 # NOTE(harlowja): Should only happen if we ran out of attempts raise exception.VolumeNotCreated(volume_id=vol_id, seconds=int(time.time() - start), diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index 7eeff384ff57..0d146d899f4f 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -522,6 +522,36 @@ class ComputeVolumeTestCase(BaseTestCase): attempts = c._await_block_device_map_created(self.context, '1') self.assertEqual(attempts, 3) + def test_await_block_device_created_retries_negative(self): + c = self.compute + self.flags(block_device_allocate_retries=-1) + self.flags(block_device_allocate_retries_interval=0.1) + + def volume_get(context, vol_id): + return { + 'status': 'available', + 'id': 'blah', + } + + self.stubs.Set(c.volume_api, 'get', volume_get) + attempts = c._await_block_device_map_created(self.context, '1') + self.assertEqual(1, attempts) + + def test_await_block_device_created_retries_zero(self): + c = self.compute + self.flags(block_device_allocate_retries=0) + self.flags(block_device_allocate_retries_interval=0.1) + + def volume_get(context, vol_id): + return { + 'status': 'available', + 'id': 'blah', + } + + self.stubs.Set(c.volume_api, 'get', volume_get) + attempts = c._await_block_device_map_created(self.context, '1') + self.assertEqual(1, attempts) + def test_boot_volume_serial(self): with ( mock.patch.object(objects.BlockDeviceMapping, 'save')