Skip to content

Commit

Permalink
remove extra quota commit of manage snapshot
Browse files Browse the repository at this point in the history
When manage an existing snapshot of a volume, cinder-api and cinder-vol
both commit quota. This result in an extra commit.This change remove
the extra commit in the cinder-api.

Closes-bug: #1562830
(cherry picked from commit 9809769)

Change-Id: Ic93ceebfcd9703549eb5f6b41e404a36ce24daae
  • Loading branch information
CaoShuFeng authored and Jay Conroy committed Jun 21, 2016
1 parent 37e32bc commit 0520389
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
3 changes: 3 additions & 0 deletions cinder/tests/unit/api/contrib/test_snapshot_manage.py
Expand Up @@ -96,7 +96,10 @@ def test_manage_snapshot_ok(self, mock_db,
# Check the create_snapshot_in_db was called with correct arguments.
self.assertEqual(1, mock_create_snapshot.call_count)
args = mock_create_snapshot.call_args[0]
named_args = mock_create_snapshot.call_args[1]
self.assertEqual('fake_volume_id', args[1].get('id'))
# We should commit quota in cinder-volume layer for this operation.
self.assertFalse(named_args['commit_quota'])

# Check the volume_rpcapi.manage_existing_snapshot was called with
# correct arguments.
Expand Down
92 changes: 49 additions & 43 deletions cinder/volume/api.py
Expand Up @@ -711,7 +711,8 @@ def _create_snapshot(self, context,
def create_snapshot_in_db(self, context,
volume, name, description,
force, metadata,
cgsnapshot_id):
cgsnapshot_id,
commit_quota=True):
check_policy(context, 'create_snapshot', volume)

if volume['status'] == 'maintenance':
Expand All @@ -737,45 +738,47 @@ def create_snapshot_in_db(self, context,
'vol_status': volume['status']}
raise exception.InvalidVolume(reason=msg)

try:
if CONF.no_snapshot_gb_quota:
reserve_opts = {'snapshots': 1}
else:
reserve_opts = {'snapshots': 1, 'gigabytes': volume['size']}
QUOTAS.add_volume_type_opts(context,
reserve_opts,
volume.get('volume_type_id'))
reservations = QUOTAS.reserve(context, **reserve_opts)
except exception.OverQuota as e:
overs = e.kwargs['overs']
usages = e.kwargs['usages']
quotas = e.kwargs['quotas']

def _consumed(name):
return (usages[name]['reserved'] + usages[name]['in_use'])

for over in overs:
if 'gigabytes' in over:
msg = _LW("Quota exceeded for %(s_pid)s, tried to create "
"%(s_size)sG snapshot (%(d_consumed)dG of "
"%(d_quota)dG already consumed).")
LOG.warning(msg, {'s_pid': context.project_id,
's_size': volume['size'],
'd_consumed': _consumed(over),
'd_quota': quotas[over]})
raise exception.VolumeSizeExceedsAvailableQuota(
requested=volume['size'],
consumed=_consumed('gigabytes'),
quota=quotas['gigabytes'])
elif 'snapshots' in over:
msg = _LW("Quota exceeded for %(s_pid)s, tried to create "
"snapshot (%(d_consumed)d snapshots "
"already consumed).")

LOG.warning(msg, {'s_pid': context.project_id,
'd_consumed': _consumed(over)})
raise exception.SnapshotLimitExceeded(
allowed=quotas[over])
if commit_quota:
try:
if CONF.no_snapshot_gb_quota:
reserve_opts = {'snapshots': 1}
else:
reserve_opts = {'snapshots': 1,
'gigabytes': volume['size']}
QUOTAS.add_volume_type_opts(context,
reserve_opts,
volume.get('volume_type_id'))
reservations = QUOTAS.reserve(context, **reserve_opts)
except exception.OverQuota as e:
overs = e.kwargs['overs']
usages = e.kwargs['usages']
quotas = e.kwargs['quotas']

def _consumed(name):
return (usages[name]['reserved'] + usages[name]['in_use'])

for over in overs:
if 'gigabytes' in over:
msg = _LW("Quota exceeded for %(s_pid)s, tried to "
"create %(s_size)sG snapshot (%(d_consumed)d"
"G of %(d_quota)dG already consumed).")
LOG.warning(msg, {'s_pid': context.project_id,
's_size': volume['size'],
'd_consumed': _consumed(over),
'd_quota': quotas[over]})
raise exception.VolumeSizeExceedsAvailableQuota(
requested=volume['size'],
consumed=_consumed('gigabytes'),
quota=quotas['gigabytes'])
elif 'snapshots' in over:
msg = _LW("Quota exceeded for %(s_pid)s, tried to "
"create snapshot (%(d_consumed)d snapshots "
"already consumed).")

LOG.warning(msg, {'s_pid': context.project_id,
'd_consumed': _consumed(over)})
raise exception.SnapshotLimitExceeded(
allowed=quotas[over])

self._check_metadata_properties(metadata)

Expand All @@ -798,14 +801,16 @@ def _consumed(name):
snapshot = objects.Snapshot(context=context, **kwargs)
snapshot.create()

QUOTAS.commit(context, reservations)
if commit_quota:
QUOTAS.commit(context, reservations)
except Exception:
with excutils.save_and_reraise_exception():
try:
if snapshot.obj_attr_is_set('id'):
snapshot.destroy()
finally:
QUOTAS.rollback(context, reservations)
if commit_quota:
QUOTAS.rollback(context, reservations)

return snapshot

Expand Down Expand Up @@ -1592,7 +1597,8 @@ def manage_existing_snapshot(self, context, ref, volume,

snapshot_object = self.create_snapshot_in_db(context, volume, name,
description, False,
metadata, None)
metadata, None,
commit_quota=False)
self.volume_rpcapi.manage_existing_snapshot(context, snapshot_object,
ref, host)
return snapshot_object
Expand Down

0 comments on commit 0520389

Please sign in to comment.