Skip to content

Commit

Permalink
Add volume quota in volume/api.py and olume/manager.py
Browse files Browse the repository at this point in the history
Fixes bug #1048158

volume.api.API.create() call QUOTAS.reserve(), then
volume.manager.VolumeManager.create_volume call QUOTAS.commit().
volume.manager.VolumeManager.delete_volume call QUOTAS.reserve() and
QUOTAS.commit().

It also fixes bug #1049459.

Change-Id: I764aeec83cce93595fbe9aa00205a7d8c9e13c35
  • Loading branch information
zhurongze committed Sep 12, 2012
1 parent 24b3cd5 commit a955730
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 8 additions & 4 deletions cinder/volume/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ def _consumed(name):
}

volume = self.db.volume_create(context, options)
self._cast_create_volume(context, volume['id'], snapshot_id, image_id)
self._cast_create_volume(context, volume['id'], snapshot_id,
image_id, reservations)
return volume

def _cast_create_volume(self, context, volume_id, snapshot_id, image_id):
def _cast_create_volume(self, context, volume_id, snapshot_id,
image_id, reservations):

# NOTE(Rongze Zhu): It is a simple solution for bug 1008866
# If snapshot_id is set, make the call create volume directly to
Expand All @@ -186,15 +188,17 @@ def _cast_create_volume(self, context, volume_id, snapshot_id, image_id):
{"method": "create_volume",
"args": {"volume_id": volume_id,
"snapshot_id": snapshot_id,
"image_id": image_id}})
"image_id": image_id,
"reservations": reservations}})
else:
rpc.cast(context,
FLAGS.scheduler_topic,
{"method": "create_volume",
"args": {"topic": FLAGS.volume_topic,
"volume_id": volume_id,
"snapshot_id": snapshot_id,
"image_id": image_id}})
"image_id": image_id,
"reservations": reservations}})

@wrap_check_policy
def delete(self, context, volume, force=False):
Expand Down
24 changes: 23 additions & 1 deletion cinder/volume/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@
from cinder.openstack.common import excutils
from cinder.openstack.common import importutils
from cinder.openstack.common import timeutils
from cinder import quota
from cinder import utils
from cinder.volume import utils as volume_utils


LOG = logging.getLogger(__name__)

QUOTAS = quota.QUOTAS

volume_manager_opts = [
cfg.StrOpt('volume_driver',
default='cinder.volume.driver.ISCSIDriver',
Expand Down Expand Up @@ -100,7 +103,7 @@ def init_host(self):
LOG.info(_("volume %s: skipping export"), volume['name'])

def create_volume(self, context, volume_id, snapshot_id=None,
image_id=None):
image_id=None, reservations=None):
"""Creates and exports the volume."""
context = context.elevated()
volume_ref = self.db.volume_get(context, volume_id)
Expand Down Expand Up @@ -147,8 +150,14 @@ def create_volume(self, context, volume_id, snapshot_id=None,
model_update = self.driver.create_export(context, volume_ref)
if model_update:
self.db.volume_update(context, volume_ref['id'], model_update)

# Commit the reservation
if reservations:
QUOTAS.commit(context, reservations)
except Exception:
with excutils.save_and_reraise_exception():
if reservations:
QUOTAS.rollback(context, reservations)
self.db.volume_update(context,
volume_ref['id'], {'status': 'error'})

Expand Down Expand Up @@ -195,9 +204,22 @@ def delete_volume(self, context, volume_id):
volume_ref['id'],
{'status': 'error_deleting'})

# Get reservations
try:
reservations = QUOTAS.reserve(context, volumes=-1,
gigabytes=-volume_ref['size'])
except Exception:
reservations = None
LOG.exception(_("Failed to update usages deleting volume"))

self.db.volume_destroy(context, volume_id)
LOG.debug(_("volume %s: deleted successfully"), volume_ref['name'])
self._notify_about_volume_usage(context, volume_ref, "delete.end")

# Commit the reservations
if reservations:
QUOTAS.commit(context, reservations)

return True

def create_snapshot(self, context, volume_id, snapshot_id):
Expand Down

0 comments on commit a955730

Please sign in to comment.