[Stable Only] Remove soft-deleted instances from quota_usages

The resources in quota_usages table will minus 1 while
soft deleting an instance. But the soft-deleted instances
are counted when synchronizing quota_usages using command
"nova-manage project quota_usage_refresh". So we filter
out soft-deleted instances when synchronizing quota_usage.

We didn't need to do this on master as the quota_usage_refresh
command has been removed in 17.0.0 (Queens). And the
_instance_data_get_for_user function is already removed
in commit c881e77819.
https://review.openstack.org/#/c/570865/

Change-Id: I8dcbe1e5f3f70ddd7067fed1125ef3379044ab0f
Closes-bug: #1773542
(cherry picked from commit 58538ace35)
(cherry picked from commit ab03100bcb)
This commit is contained in:
huanhongda 2018-05-26 22:40:27 +08:00 committed by Matt Riedemann
parent fe0c1033d9
commit b465901793
2 changed files with 23 additions and 1 deletions

View File

@ -1824,11 +1824,15 @@ def instance_create(context, values):
def _instance_data_get_for_user(context, project_id, user_id):
not_soft_deleted = or_(
models.Instance.vm_state != vm_states.SOFT_DELETED,
models.Instance.vm_state == null()
)
result = model_query(context, models.Instance, (
func.count(models.Instance.id),
func.sum(models.Instance.vcpus),
func.sum(models.Instance.memory_mb))).\
filter_by(project_id=project_id)
filter_by(project_id=project_id).filter(not_soft_deleted)
if user_id:
result = result.filter_by(user_id=user_id).first()
else:

View File

@ -1346,6 +1346,24 @@ class SqlAlchemyDbApiTestCase(DbTestCase):
filters={},
sort_keys=keys)
def test_instance_data_get_for_user(self):
ctxt = context.get_admin_context()
instance_1 = self.create_instance_with_args(project_id='project-HHD')
self.create_instance_with_args(project_id='project-HHD')
@sqlalchemy_api.pick_context_manager_reader
def test(context):
return sqlalchemy_api._instance_data_get_for_user(
context, 'project-HHD', None)
inst_num, _, _ = test(ctxt)
self.assertEqual(2, inst_num)
db.instance_update(ctxt, instance_1['uuid'],
{"vm_state": vm_states.SOFT_DELETED})
inst_num_2, _, _ = test(ctxt)
self.assertEqual(1, inst_num_2)
class ProcessSortParamTestCase(test.TestCase):