pci: pass in instance PCI requests to claim

Removes the calls to InstancePCIRequests.get_XXX() from within the
claims.Claim and claims.MoveClaim constructors and instead has the
resource tracker construct the PCI requests and pass them into the
constructor.

This allows us to remove the needlessly duplicative _test_pci() method
in claims.MoveClaim and will allow the next patch in the series to
remove the call in nova.pci.manager.PciDevTracker.claim_instance() that
re-fetches PCI requests for the supplied instance.

Change-Id: Ib2cc7c985839fbf88b5e6e437c4b395ab484b1b6
This commit is contained in:
Jay Pipes 2016-04-01 16:03:47 -07:00 committed by Moshe Levi
parent 8d8f44657a
commit 74fbff8863
5 changed files with 31 additions and 32 deletions

View File

@ -75,13 +75,14 @@ class Claim(NopClaim):
correct decisions with respect to host selection.
"""
def __init__(self, context, instance, tracker, resources, overhead=None,
limits=None):
def __init__(self, context, instance, tracker, resources, pci_requests,
overhead=None, limits=None):
super(Claim, self).__init__()
# Stash a copy of the instance at the current point of time
self.instance = instance.obj_clone()
self._numa_topology_loaded = False
self.tracker = tracker
self._pci_requests = pci_requests
if not overhead:
overhead = {'memory_mb': 0}
@ -185,9 +186,7 @@ class Claim(NopClaim):
return self._test(type_, unit, total, used, requested, limit)
def _test_pci(self):
pci_requests = objects.InstancePCIRequests.get_by_instance_uuid(
self.context, self.instance.uuid)
pci_requests = self._pci_requests
if pci_requests.requests:
stats = self.tracker.pci_tracker.stats
if not stats.support_requests(pci_requests.requests):
@ -261,15 +260,15 @@ class MoveClaim(Claim):
Move can be either a migrate/resize, live-migrate or an evacuate operation.
"""
def __init__(self, context, instance, instance_type, image_meta, tracker,
resources, overhead=None, limits=None):
resources, pci_requests, overhead=None, limits=None):
self.context = context
self.instance_type = instance_type
if isinstance(image_meta, dict):
image_meta = objects.ImageMeta.from_dict(image_meta)
self.image_meta = image_meta
super(MoveClaim, self).__init__(context, instance, tracker,
resources, overhead=overhead,
limits=limits)
resources, pci_requests,
overhead=overhead, limits=limits)
self.migration = None
@property
@ -290,16 +289,6 @@ class MoveClaim(Claim):
return hardware.numa_get_constraints(self.instance_type,
self.image_meta)
def _test_pci(self):
pci_requests = objects.InstancePCIRequests.\
get_by_instance_uuid_and_newness(
self.context, self.instance.uuid, True)
if pci_requests.requests:
claim = self.tracker.pci_tracker.stats.support_requests(
pci_requests.requests)
if not claim:
return _('Claim pci failed.')
def abort(self):
"""Compute operation requiring claimed resources has failed or
been aborted.

View File

@ -128,8 +128,10 @@ class ResourceTracker(object):
"MB", {'flavor': instance_ref.memory_mb,
'overhead': overhead['memory_mb']})
pci_requests = objects.InstancePCIRequests.get_by_instance_uuid(
context, instance_ref.uuid)
claim = claims.Claim(context, instance_ref, self, self.compute_node,
overhead=overhead, limits=limits)
pci_requests, overhead=overhead, limits=limits)
if self.pci_tracker:
# NOTE(jaypipes): ComputeNode.pci_device_pools is set below
@ -207,9 +209,13 @@ class ResourceTracker(object):
"MB", {'flavor': new_instance_type.memory_mb,
'overhead': overhead['memory_mb']})
pci_requests = objects.InstancePCIRequests.\
get_by_instance_uuid_and_newness(
context, instance.uuid, True)
claim = claims.MoveClaim(context, instance, new_instance_type,
image_meta, self, self.compute_node,
overhead=overhead, limits=limits)
pci_requests, overhead=overhead,
limits=limits)
claim.migration = migration
instance.migration_context = claim.create_migration_context()
instance.save()

View File

@ -84,13 +84,13 @@ class ClaimTestCase(test.NoDBTestCase):
if overhead is None:
overhead = {'memory_mb': 0}
@mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid',
return_value=requests or self.empty_requests)
requests = requests or self.empty_requests
@mock.patch('nova.db.instance_extra_get_by_instance_uuid',
return_value=db_numa_topology)
def get_claim(mock_extra_get, mock_pci_get):
def get_claim(mock_extra_get):
return claims.Claim(self.context, instance, self.tracker,
self.resources, overhead=overhead,
self.resources, requests, overhead=overhead,
limits=limits)
return get_claim()
@ -378,17 +378,17 @@ class MoveClaimTestCase(ClaimTestCase):
if overhead is None:
overhead = {'memory_mb': 0}
@mock.patch('nova.objects.InstancePCIRequests.'
'get_by_instance_uuid_and_newness',
return_value=requests or self.empty_requests)
requests = requests or self.empty_requests
@mock.patch('nova.virt.hardware.numa_get_constraints',
return_value=numa_topology)
@mock.patch('nova.db.instance_extra_get_by_instance_uuid',
return_value=self.db_numa_topology)
def get_claim(mock_extra_get, mock_numa_get, mock_pci_get):
def get_claim(mock_extra_get, mock_numa_get):
return claims.MoveClaim(self.context, self.instance, instance_type,
image_meta, self.tracker, self.resources,
overhead=overhead, limits=limits)
requests, overhead=overhead,
limits=limits)
return get_claim()
def test_abort(self):

View File

@ -1078,7 +1078,8 @@ class InstanceClaimTestCase(BaseTrackerTestCase):
inst.obj_what_changed())
mock_save.side_effect = fake_save
inst = objects.Instance(host=None, node=None, memory_mb=1024)
inst = objects.Instance(host=None, node=None, memory_mb=1024,
uuid=uuidsentinel.instance1)
inst.obj_reset_changes()
numa = objects.InstanceNUMATopology()
claim = mock.MagicMock()

View File

@ -212,8 +212,10 @@ class ShelveComputeManagerTestCase(test_compute.BaseTestCase):
def fake_claim(context, instance, limits):
instance.host = self.compute.host
requests = objects.InstancePCIRequests(requests=[])
return claims.Claim(context, instance,
self.rt, _fake_resources())
self.rt, _fake_resources(),
requests)
tracking = {
'last_state': instance.task_state,
@ -319,7 +321,8 @@ class ShelveComputeManagerTestCase(test_compute.BaseTestCase):
self.context, instance, self.compute.host)
self.rt.instance_claim(self.context, instance, limits).AndReturn(
claims.Claim(self.context, instance, self.rt,
_fake_resources()))
_fake_resources(),
objects.InstancePCIRequests(requests=[])))
self.compute.driver.spawn(self.context, instance,
mox.IsA(objects.ImageMeta),
injected_files=[], admin_password=None,