From 749b220ef668eb331a63bdb2a8ab061f1bca29b6 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Thu, 26 Oct 2017 18:40:12 -0400 Subject: [PATCH] Cleanup build_request_spec The RequestContext parameter was unused so it's removed. Change-Id: I2391aa8e61c5e450d123cf3df1294bc119d361dc --- nova/cells/scheduler.py | 3 +-- nova/conductor/manager.py | 7 +++---- nova/scheduler/utils.py | 17 ++++++++++++++++- nova/tests/unit/cells/test_cells_scheduler.py | 12 ++++++------ nova/tests/unit/conductor/test_conductor.py | 9 ++++----- .../unit/scheduler/test_scheduler_utils.py | 6 ++---- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/nova/cells/scheduler.py b/nova/cells/scheduler.py index 91d70850ff..752d7e4d89 100644 --- a/nova/cells/scheduler.py +++ b/nova/cells/scheduler.py @@ -198,8 +198,7 @@ class CellsScheduler(base.Base): instance_uuids = [inst['uuid'] for inst in build_inst_kwargs['instances']] instances = build_inst_kwargs['instances'] - request_spec = scheduler_utils.build_request_spec(message.ctxt, - image, instances) + request_spec = scheduler_utils.build_request_spec(image, instances) filter_properties = copy.copy(build_inst_kwargs['filter_properties']) filter_properties.update({'context': message.ctxt, 'scheduler': self, diff --git a/nova/conductor/manager.py b/nova/conductor/manager.py index 75bbf34264..f7361345ec 100644 --- a/nova/conductor/manager.py +++ b/nova/conductor/manager.py @@ -539,8 +539,7 @@ class ComputeTaskManager(base.Base): # have a single instance. # TODO(sbauza): Provide directly the RequestSpec object # when populate_retry() accepts it - request_spec = scheduler_utils.build_request_spec( - context, image, instances) + request_spec = scheduler_utils.build_request_spec(image, instances) scheduler_utils.populate_retry( filter_properties, instances[0].uuid) instance_uuids = [instance.uuid for instance in instances] @@ -668,7 +667,7 @@ class ComputeTaskManager(base.Base): # old. We need to mock that the old way filter_properties = {} request_spec = scheduler_utils.build_request_spec( - context, image, [instance]) + image, [instance]) else: # NOTE(sbauza): Force_hosts/nodes needs to be reset # if we want to make sure that the next destination @@ -831,7 +830,7 @@ class ComputeTaskManager(base.Base): image_meta = nova_object.obj_to_primitive( instance.image_meta) request_spec = scheduler_utils.build_request_spec( - context, image_meta, [instance]) + image_meta, [instance]) request_spec = objects.RequestSpec.from_primitives( context, request_spec, filter_properties) else: diff --git a/nova/scheduler/utils.py b/nova/scheduler/utils.py index 07e01df787..7b9b9658af 100644 --- a/nova/scheduler/utils.py +++ b/nova/scheduler/utils.py @@ -180,11 +180,23 @@ class ResourceRequest(object): self._clean_empties() -def build_request_spec(ctxt, image, instances, instance_type=None): +def build_request_spec(image, instances, instance_type=None): """Build a request_spec for the scheduler. The request_spec assumes that all instances to be scheduled are the same type. + + :param image: optional primitive image meta dict + :param instances: list of instances; objects will be converted to + primitives + :param instance_type: optional flavor; objects will be converted to + primitives + :return: dict with the following keys:: + + 'image': the image dict passed in or {} + 'instance_properties': primitive version of the first instance passed + 'instance_type': primitive version of the instance_type or None + 'num_instances': the number of instances passed in """ instance = instances[0] if instance_type is None: @@ -218,6 +230,9 @@ def build_request_spec(ctxt, image, instances, instance_type=None): 'instance_properties': instance, 'instance_type': instance_type, 'num_instances': len(instances)} + # NOTE(mriedem): obj_to_primitive above does not serialize everything + # in an object, like datetime fields, so we need to still call to_primitive + # to recursively serialize the items in the request_spec dict. return jsonutils.to_primitive(request_spec) diff --git a/nova/tests/unit/cells/test_cells_scheduler.py b/nova/tests/unit/cells/test_cells_scheduler.py index 0f2996c1f1..320bd83e57 100644 --- a/nova/tests/unit/cells/test_cells_scheduler.py +++ b/nova/tests/unit/cells/test_cells_scheduler.py @@ -209,7 +209,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info['target_cell'] = target_cell call_info['build_inst_kwargs'] = build_inst_kwargs - def fake_build_request_spec(ctxt, image, instances): + def fake_build_request_spec(image, instances): request_spec = { 'num_instances': len(instances), 'image': image} @@ -254,7 +254,7 @@ class CellsSchedulerTestCase(test.TestCase): def fake_rpc_build_instances(self, ctxt, **build_inst_kwargs): call_info['build_inst_kwargs'] = build_inst_kwargs - def fake_build_request_spec(ctxt, image, instances): + def fake_build_request_spec(image, instances): request_spec = { 'num_instances': len(instances), 'image': image} @@ -304,7 +304,7 @@ class CellsSchedulerTestCase(test.TestCase): self.assertEqual(vm_states.ERROR, inst.vm_state) call_info['errored_uuids'].append(inst.uuid) - def fake_build_request_spec(ctxt, image, instances): + def fake_build_request_spec(image, instances): request_spec = { 'num_instances': len(instances), 'image': image} @@ -350,7 +350,7 @@ class CellsSchedulerTestCase(test.TestCase): self.assertEqual(vm_states.ERROR, instance['vm_state']) call_info['errored_uuids2'].append(instance['uuid']) - def fake_build_request_spec(ctxt, image, instances): + def fake_build_request_spec(image, instances): request_spec = { 'num_instances': len(instances), 'image': image} @@ -415,7 +415,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info['filt_props'] = filt_properties return cells - def fake_build_request_spec(ctxt, image, instances): + def fake_build_request_spec(image, instances): request_spec = { 'num_instances': len(instances), 'instance_properties': instances[0], @@ -534,7 +534,7 @@ class CellsSchedulerTestCase(test.TestCase): call_info['weight_props'] = filt_properties return [weights.WeightedCell(cells[0], 0.0)] - def fake_build_request_spec(ctxt, image, instances): + def fake_build_request_spec(image, instances): request_spec = { 'num_instances': len(instances), 'instance_properties': instances[0], diff --git a/nova/tests/unit/conductor/test_conductor.py b/nova/tests/unit/conductor/test_conductor.py index 1c1c8a6fdd..481f5aa259 100644 --- a/nova/tests/unit/conductor/test_conductor.py +++ b/nova/tests/unit/conductor/test_conductor.py @@ -1045,7 +1045,7 @@ class _BaseTaskTestCase(object): self.conductor_manager.image_api.get(self.context, 'fake_image_id', show_deleted=False).AndReturn('fake_image') - scheduler_utils.build_request_spec(self.context, 'fake_image', + scheduler_utils.build_request_spec('fake_image', mox.IgnoreArg()).AndReturn('req_spec') self.conductor_manager._schedule_instances(self.context, fake_spec, [instance.uuid]).AndReturn( @@ -1145,7 +1145,7 @@ class _BaseTaskTestCase(object): self.mox.StubOutWithMock(self.conductor_manager.compute_rpcapi, 'unshelve_instance') - scheduler_utils.build_request_spec(self.context, None, + scheduler_utils.build_request_spec(None, mox.IgnoreArg()).AndReturn('req_spec') self.conductor_manager._schedule_instances(self.context, fake_spec, [instance.uuid]).AndReturn( @@ -1216,8 +1216,7 @@ class _BaseTaskTestCase(object): instance=inst_obj, **rebuild_args) bs_mock.assert_called_once_with( - self.context, obj_base.obj_to_primitive(inst_obj.image_meta), - [inst_obj]) + obj_base.obj_to_primitive(inst_obj.image_meta), [inst_obj]) fp_mock.assert_called_once_with(self.context, request_spec, filter_properties) select_dest_mock.assert_called_once_with(self.context, fake_spec, @@ -2418,7 +2417,7 @@ class ConductorTaskTestCase(_BaseTaskTestCase, test_compute.BaseTestCase): self.mox.StubOutWithMock(self.conductor_manager.compute_rpcapi, 'build_and_run_instance') - scheduler_utils.build_request_spec(self.context, image, + scheduler_utils.build_request_spec(image, mox.IgnoreArg()).AndReturn(spec) filter_properties = {'retry': {'num_attempts': 1, 'hosts': []}} inst_uuids = [inst.uuid for inst in instances] diff --git a/nova/tests/unit/scheduler/test_scheduler_utils.py b/nova/tests/unit/scheduler/test_scheduler_utils.py index 9b5fb5219f..cb73150d6b 100644 --- a/nova/tests/unit/scheduler/test_scheduler_utils.py +++ b/nova/tests/unit/scheduler/test_scheduler_utils.py @@ -42,8 +42,7 @@ class SchedulerUtilsTestCase(test.NoDBTestCase): with mock.patch.object(flavors, 'extract_flavor') as mock_extract: mock_extract.return_value = instance_type - request_spec = scheduler_utils.build_request_spec(self.context, - None, + request_spec = scheduler_utils.build_request_spec(None, [instance]) mock_extract.assert_called_once_with({'uuid': uuids.instance}) self.assertEqual({}, request_spec['image']) @@ -54,8 +53,7 @@ class SchedulerUtilsTestCase(test.NoDBTestCase): with mock.patch.object(instance, 'get_flavor') as mock_get: mock_get.return_value = instance_type - request_spec = scheduler_utils.build_request_spec(self.context, - None, + request_spec = scheduler_utils.build_request_spec(None, [instance]) mock_get.assert_called_once_with() self.assertIsInstance(request_spec['instance_properties'], dict)