Move the to_dict() method to the Selection object

With the RPC change coming in later patches in the series, it simply
makes more sense to have the method for converting a Selection object to
the older host_state dict format in the object itself.

Blueprint: return-alternate-hosts

Change-Id: I2299c71e034299d63c267bc4cbe6360858445779
This commit is contained in:
Ed Leafe 2017-11-28 16:36:08 +00:00
parent b33cfe8bb8
commit 526934eafd
4 changed files with 73 additions and 74 deletions

View File

@ -58,3 +58,23 @@ class Selection(base.NovaObject, ovo_base.ComparableVersionedObject):
limits=limits,
allocation_request=allocation_request_json,
allocation_request_version=allocation_request_version)
def to_dict(self):
if self.limits is not None:
limits = self.limits.to_dict()
else:
limits = {}
# The NUMATopologyFilter can set 'numa_topology' in the limits dict to
# a NUMATopologyLimits object which we need to convert to a primitive
# before this hits jsonutils.to_primitive(). We only check for that
# known case specifically as we don't care about handling out of tree
# filters or drivers injecting non-serializable things in the limits
# dict.
numa_limit = limits.get("numa_topology")
if numa_limit is not None:
limits['numa_topology'] = numa_limit.obj_to_primitive()
return {
'host': self.service_host,
'nodename': self.nodename,
'limits': limits,
}

View File

@ -45,26 +45,6 @@ CONF = nova.conf.CONF
QUOTAS = quota.QUOTAS
def _selection_obj_to_dict(selection):
if selection.limits is not None:
limits = selection.limits.to_dict()
else:
limits = {}
# The NUMATopologyFilter can set 'numa_topology' in the limits dict
# to a NUMATopologyLimits object which we need to convert to a primitive
# before this hits jsonutils.to_primitive(). We only check for that known
# case specifically as we don't care about handling out of tree filters
# or drivers injecting non-serializable things in the limits dict.
numa_limit = limits.get("numa_topology")
if numa_limit is not None:
limits['numa_topology'] = limits['numa_topology'].obj_to_primitive()
return {
'host': selection.service_host,
'nodename': selection.nodename,
'limits': limits,
}
class SchedulerManager(manager.Manager):
"""Chooses a host to run instances on."""
@ -154,8 +134,7 @@ class SchedulerManager(manager.Manager):
# involves an RPC change. So convert the list of lists of Selection
# objects to a list of host state dicts, which is what the calling
# method expects.
selected = [sel[0] for sel in selections]
selection_dicts = [_selection_obj_to_dict(claim) for claim in selected]
selection_dicts = [sel[0].to_dict() for sel in selections]
return jsonutils.to_primitive(selection_dicts)
def update_aggregates(self, ctxt, aggregates):

View File

@ -87,6 +87,58 @@ class _TestSelectionObject(object):
self.assertEqual(expected_alloc, dest.allocation_request)
self.assertIsNone(dest.allocation_request_version)
def test_selection_obj_to_dict(self):
"""Tests that to_dict() method properly converts a Selection object to
the corresponding dict.
"""
fake_numa_limit = objects.numa.NUMATopologyLimits(
cpu_allocation_ratio=1.0, ram_allocation_ratio=1.0)
fake_limit = {"memory_mb": 1024, "disk_gb": 100, "vcpus": 2,
"numa_topology": fake_numa_limit}
fake_limit_obj = objects.SchedulerLimits.from_dict(fake_limit)
sel_obj = objects.Selection(service_host="fakehost",
nodename="fakenode", compute_node_uuid=uuids.host,
cell_uuid=uuids.cell, limits=fake_limit_obj,
allocation_request="fake", allocation_request_version="99.9")
expected = {
'host': 'fakehost',
'nodename': 'fakenode',
'limits': {
'disk_gb': 100,
'memory_mb': 1024,
'numa_topology': {
'nova_object.changes': [
'cpu_allocation_ratio',
'ram_allocation_ratio'],
'nova_object.data': {
'cpu_allocation_ratio': 1.0,
'ram_allocation_ratio': 1.0},
'nova_object.name': 'NUMATopologyLimits',
'nova_object.namespace': 'nova',
'nova_object.version': '1.0'}}}
result = sel_obj.to_dict()
self.assertDictEqual(expected, result)
def test_selection_obj_to_dict_no_numa(self):
"""Tests that to_dict() method properly converts a
Selection object to the corresponding dict when the numa_topology field
is None.
"""
fake_limit = {"memory_mb": 1024, "disk_gb": 100, "vcpus": 2,
"numa_topology": None}
fake_limit_obj = objects.SchedulerLimits.from_dict(fake_limit)
sel_obj = objects.Selection(service_host="fakehost",
nodename="fakenode", compute_node_uuid=uuids.host,
cell_uuid=uuids.cell, limits=fake_limit_obj,
allocation_request="fake", allocation_request_version="99.9")
expected = {"host": "fakehost",
"nodename": "fakenode",
"limits": {
"disk_gb": 100,
"memory_mb": 1024}}
result = sel_obj.to_dict()
self.assertDictEqual(expected, result)
class TestSelectionObject(test_objects._LocalTest,
_TestSelectionObject):

View File

@ -267,58 +267,6 @@ class SchedulerManagerTestCase(test.NoDBTestCase):
cell_mapping=cm2)]
self.manager._discover_hosts_in_cells(mock.sentinel.context)
def test_selection_obj_to_dict(self):
"""Tests that _selection_obj_to_dict() method properly converts a
Selection object to the corresponding dict.
"""
fake_numa_limit = objects.numa.NUMATopologyLimits(
cpu_allocation_ratio=1.0, ram_allocation_ratio=1.0)
fake_limit = {"memory_mb": 1024, "disk_gb": 100, "vcpus": 2,
"numa_topology": fake_numa_limit}
fake_limit_obj = objects.SchedulerLimits.from_dict(fake_limit)
sel_obj = objects.Selection(service_host="fakehost",
nodename="fakenode", compute_node_uuid=uuids.host,
cell_uuid=uuids.cell, limits=fake_limit_obj,
allocation_request="fake", allocation_request_version="99.9")
expected = {
'host': 'fakehost',
'nodename': 'fakenode',
'limits': {
'disk_gb': 100,
'memory_mb': 1024,
'numa_topology': {
'nova_object.changes': [
'cpu_allocation_ratio',
'ram_allocation_ratio'],
'nova_object.data': {
'cpu_allocation_ratio': 1.0,
'ram_allocation_ratio': 1.0},
'nova_object.name': 'NUMATopologyLimits',
'nova_object.namespace': 'nova',
'nova_object.version': '1.0'}}}
result = manager._selection_obj_to_dict(sel_obj)
self.assertDictEqual(expected, result)
def test_selection_obj_to_dict_no_numa(self):
"""Tests that _selection_obj_to_dict() method properly converts a
Selection object to the corresponding dict when the numa_topology field
is None.
"""
fake_limit = {"memory_mb": 1024, "disk_gb": 100, "vcpus": 2,
"numa_topology": None}
fake_limit_obj = objects.SchedulerLimits.from_dict(fake_limit)
sel_obj = objects.Selection(service_host="fakehost",
nodename="fakenode", compute_node_uuid=uuids.host,
cell_uuid=uuids.cell, limits=fake_limit_obj,
allocation_request="fake", allocation_request_version="99.9")
expected = {"host": "fakehost",
"nodename": "fakenode",
"limits": {
"disk_gb": 100,
"memory_mb": 1024}}
result = manager._selection_obj_to_dict(sel_obj)
self.assertDictEqual(expected, result)
class SchedulerInitTestCase(test.NoDBTestCase):
"""Test case for base scheduler driver initiation."""