Merge "Clean up allocation when update available resources"
This commit is contained in:
commit
b97662b228
@ -898,6 +898,9 @@ class ResourceTracker(object):
|
||||
for instance in instances:
|
||||
if instance.vm_state not in vm_states.ALLOW_RESOURCE_REMOVAL:
|
||||
self._update_usage_from_instance(context, instance)
|
||||
|
||||
self.scheduler_client.reportclient.remove_deleted_instances(
|
||||
self.compute_node, self.tracked_instances.values())
|
||||
self.compute_node.free_ram_mb = max(0, self.compute_node.free_ram_mb)
|
||||
self.compute_node.free_disk_gb = max(0, self.compute_node.free_disk_gb)
|
||||
|
||||
|
@ -408,17 +408,17 @@ class SchedulerReportClient(object):
|
||||
'text': r.text})
|
||||
|
||||
@safe_connect
|
||||
def _delete_allocation_for_instance(self, instance):
|
||||
url = '/allocations/%s' % instance.uuid
|
||||
def _delete_allocation_for_instance(self, uuid):
|
||||
url = '/allocations/%s' % uuid
|
||||
r = self.delete(url)
|
||||
if r:
|
||||
LOG.info(_LI('Deleted allocation for instance'),
|
||||
instance=instance)
|
||||
LOG.info(_LI('Deleted allocation for instance %s'),
|
||||
uuid)
|
||||
else:
|
||||
LOG.warning(
|
||||
_LW('Unable to delete allocation for instance '
|
||||
'%(uuid)s: (%(code)i %(text)s)'),
|
||||
{'uuid': instance.uuid,
|
||||
{'uuid': uuid,
|
||||
'code': r.status_code,
|
||||
'text': r.text})
|
||||
|
||||
@ -426,4 +426,27 @@ class SchedulerReportClient(object):
|
||||
if sign > 0:
|
||||
self._allocate_for_instance(compute_node, instance)
|
||||
else:
|
||||
self._delete_allocation_for_instance(instance)
|
||||
self._delete_allocation_for_instance(instance.uuid)
|
||||
|
||||
@safe_connect
|
||||
def _get_allocations(self, compute_node):
|
||||
url = '/resource_providers/%s/allocations' % compute_node.uuid
|
||||
resp = self.get(url)
|
||||
if not resp:
|
||||
return {}
|
||||
else:
|
||||
return resp.json()['allocations']
|
||||
|
||||
def remove_deleted_instances(self, compute_node, instance_uuids):
|
||||
allocations = self._get_allocations(compute_node)
|
||||
if allocations is None:
|
||||
allocations = {}
|
||||
|
||||
instance_dict = {instance.uuid: instance
|
||||
for instance in instance_uuids}
|
||||
removed_instances = set(allocations.keys()) - set(instance_dict.keys())
|
||||
|
||||
for uuid in removed_instances:
|
||||
LOG.warning(_LW('Deleting stale allocation for instance %s'),
|
||||
uuid)
|
||||
self._delete_allocation_for_instance(uuid)
|
||||
|
@ -749,3 +749,32 @@ class SchedulerReportClientTestCase(test.NoDBTestCase):
|
||||
mock_delete.return_value.__bool__.return_value = False
|
||||
self.client.update_instance_allocation(cn, inst, -1)
|
||||
self.assertTrue(mock_warn.called)
|
||||
|
||||
@mock.patch('nova.scheduler.client.report.SchedulerReportClient.'
|
||||
'delete')
|
||||
@mock.patch('nova.scheduler.client.report.SchedulerReportClient.'
|
||||
'get')
|
||||
def test_remove_deleted_instances(
|
||||
self, mock_get, mock_delete):
|
||||
cn = objects.ComputeNode(uuid=uuids.cn)
|
||||
inst1 = objects.Instance(uuid=uuids.inst1)
|
||||
inst2 = objects.Instance(uuid=uuids.inst2)
|
||||
fake_allocations = {
|
||||
'MEMORY_MB': 1024,
|
||||
'VCPU': 2,
|
||||
'DISK_GB': 101,
|
||||
}
|
||||
mock_get.return_value.json.return_value = {'allocations': {
|
||||
inst1.uuid: fake_allocations,
|
||||
inst2.uuid: fake_allocations,
|
||||
}
|
||||
}
|
||||
mock_delete.return_value = True
|
||||
with mock.patch.object(self.client, '_allocations'):
|
||||
self.client.remove_deleted_instances(cn, [])
|
||||
mock_get.assert_called_once_with(
|
||||
'/resource_providers/%s/allocations' % cn.uuid)
|
||||
expected_calls = [
|
||||
mock.call('/allocations/%s' % inst1.uuid),
|
||||
mock.call('/allocations/%s' % inst2.uuid)]
|
||||
mock_delete.assert_has_calls(expected_calls, any_order=True)
|
||||
|
Loading…
Reference in New Issue
Block a user