Skip to content

Commit

Permalink
Fix volume_glance_metadata deletion (backport)
Browse files Browse the repository at this point in the history
Backport patch https://review.openstack.org/#/c/41768/ and
its dependency: https://review.openstack.org/#/c/41601/
(no snapshot handling exception, for this is not trivial
due to specific havana changes)

If volume_glance_metadata does not exist for a certain volume,
it returns an exception and does not delete volume from db.
Avoid deleting it if it does not exist.

Fixes bug: 1209367

Change-Id: Icb34c90b13aac1e92b4645c3016127a2d4b8af6f
  • Loading branch information
Adalberto Medeiros authored and openstack-gerrit committed Sep 16, 2013
1 parent 25be695 commit bd47a4f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
10 changes: 8 additions & 2 deletions cinder/db/sqlalchemy/api.py
Expand Up @@ -1610,9 +1610,15 @@ def volume_glance_metadata_get(context, volume_id, session=None):
if not session:
session = get_session()

return session.query(models.VolumeGlanceMetadata).\
rows = session.query(models.VolumeGlanceMetadata).\
filter_by(volume_id=volume_id).\
filter_by(deleted=False).all()
filter_by(deleted=False).\
all()

if not rows:
raise exception.GlanceMetadataNotFound(id=volume_id)

return rows


@require_context
Expand Down
4 changes: 4 additions & 0 deletions cinder/exception.py
Expand Up @@ -561,6 +561,10 @@ class GlanceMetadataExists(Invalid):
" exists for volume id %(volume_id)s")


class GlanceMetadataNotFound(NotFound):
message = _("Glance metadata for volume/snapshot %(id)s cannot be found.")


class ImageCopyFailure(Invalid):
message = _("Failed to copy image to volume: %(reason)s")

Expand Down
12 changes: 4 additions & 8 deletions cinder/tests/test_volume_glance_metadata.py
Expand Up @@ -92,11 +92,8 @@ def test_vol_delete_glance_metadata(self):
vol_metadata = db.volume_glance_metadata_create(ctxt, 1, 'key1',
'value1')
db.volume_glance_metadata_delete_by_volume(ctxt, 1)
metadata = db.volume_glance_metadata_get(ctxt, 1)
self.assertEqual(len(metadata), 0)
db.volume_glance_metadata_delete_by_volume(ctxt, 1)
metadata = db.volume_glance_metadata_get(ctxt, 1)
self.assertEqual(len(metadata), 0)
self.assertRaises(exception.GlanceMetadataNotFound,
db.volume_glance_metadata_get, ctxt, 1)

def test_vol_glance_metadata_copy_to_snapshot(self):
ctxt = context.get_admin_context()
Expand All @@ -120,10 +117,9 @@ def test_vol_glance_metadata_copy_to_volume(self):
db.volume_create(ctxt, {'id': 100, 'source_volid': 1})
vol_meta = db.volume_glance_metadata_create(ctxt, 1, 'key1',
'value1')
db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 100, 1)
db.volume_glance_metadata_copy_from_volume_to_volume(ctxt, 1, 100)

expected_meta = {'id': '100',
'key': 'key1',
expected_meta = {'key': 'key1',
'value': 'value1'}

for meta in db.volume_glance_metadata_get(ctxt, 100):
Expand Down
23 changes: 19 additions & 4 deletions cinder/volume/manager.py
Expand Up @@ -442,7 +442,15 @@ def delete_volume(self, context, volume_id):
reservations = None
LOG.exception(_("Failed to update usages deleting volume"))

self.db.volume_glance_metadata_delete_by_volume(context, volume_id)
# Delete glance metadata if it exists
try:
self.db.volume_glance_metadata_delete_by_volume(context, volume_id)
LOG.debug(_("volume %s: glance metadata deleted"),
volume_ref['id'])
except exception.GlanceMetadataNotFound:
LOG.debug(_("no glance metadata found for volume %s"),
volume_ref['id'])

self.db.volume_destroy(context, volume_id)
LOG.info(_("volume %s: deleted successfully"), volume_ref['name'])
self._notify_about_volume_usage(context, volume_ref, "delete.end")
Expand Down Expand Up @@ -476,9 +484,16 @@ def create_snapshot(self, context, volume_id, snapshot_id):
self.db.snapshot_update(context,
snapshot_ref['id'], {'status': 'available',
'progress': '100%'})
self.db.volume_glance_metadata_copy_to_snapshot(context,
snapshot_ref['id'],
volume_id)
# only shows the trace for the exception
try:
self.db.volume_glance_metadata_copy_to_snapshot(
context, snapshot_ref['id'], volume_id)
except exception.CinderException as ex:
LOG.exception(_("Failed updating %(snapshot_id)s"
" metadata using the provided volumes"
" %(volume_id)s metadata") %
{'volume_id': volume_id,
'snapshot_id': snapshot_id})
LOG.info(_("snapshot %s: created successfully"), snapshot_ref['name'])
return snapshot_id

Expand Down

0 comments on commit bd47a4f

Please sign in to comment.