Skip to content

Commit

Permalink
Add call to retrieve image metadata for volumes in bulk
Browse files Browse the repository at this point in the history
When using the GET volume details REST API call, the image metadata API
contribution is making an individual db call for each of the available
volumes. When the number of volumes is large the details call can take
several minutes.

This patch adds a call to the volume.API to retrieve image metadata in
bulk, very similar to the one used to retrieve individual volume image
metadata.

Change-Id: Ic3aa721016704c72b7564cc5ceff71676806a24a
Partial-Bug: #1197612
(cherry picked from commit bb2daca)
  • Loading branch information
Luis A. Garcia authored and Jay S. Bryant committed Nov 18, 2013
1 parent ad05556 commit 898e8c5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cinder/db/api.py
Expand Up @@ -549,6 +549,11 @@ def volume_glance_metadata_create(context, volume_id, key, value):
value)


def volume_glance_metadata_get_all(context):
"""Return the glance metadata for all volumes."""
return IMPL.volume_glance_metadata_get_all(context)


def volume_glance_metadata_get(context, volume_id):
"""Return the glance metadata for a volume."""
return IMPL.volume_glance_metadata_get(context, volume_id)
Expand Down
19 changes: 19 additions & 0 deletions cinder/db/sqlalchemy/api.py
Expand Up @@ -2350,6 +2350,25 @@ def volume_encryption_metadata_get(context, volume_id, session=None):
####################


@require_context
def _volume_glance_metadata_get_all(context, session=None):
rows = model_query(context,
models.VolumeGlanceMetadata,
project_only=True,
session=session).\
filter_by(deleted=False).\
all()

return rows


@require_context
def volume_glance_metadata_get_all(context):
"""Return the Glance metadata for all volumes."""

return _volume_glance_metadata_get_all(context)


@require_context
@require_volume_exists
def _volume_glance_metadata_get(context, volume_id, session=None):
Expand Down
20 changes: 20 additions & 0 deletions cinder/tests/test_volume_glance_metadata.py
Expand Up @@ -85,6 +85,26 @@ def test_vol_update_glance_metadata(self):
for key, value in expected_metadata_1.items():
self.assertEqual(metadata[0][key], value)

def test_vols_get_glance_metadata(self):
ctxt = context.get_admin_context()
db.volume_create(ctxt, {'id': '1'})
db.volume_create(ctxt, {'id': '2'})
db.volume_create(ctxt, {'id': '3'})
db.volume_glance_metadata_create(ctxt, '1', 'key1', 'value1')
db.volume_glance_metadata_create(ctxt, '2', 'key2', 'value2')
db.volume_glance_metadata_create(ctxt, '2', 'key22', 'value22')

metadata = db.volume_glance_metadata_get_all(ctxt)
self.assertEqual(len(metadata), 3)
self._assert_metadata_equals('1', 'key1', 'value1', metadata[0])
self._assert_metadata_equals('2', 'key2', 'value2', metadata[1])
self._assert_metadata_equals('2', 'key22', 'value22', metadata[2])

def _assert_metadata_equals(self, volume_id, key, value, observed):
self.assertEqual(volume_id, observed.volume_id)
self.assertEqual(key, observed.key)
self.assertEqual(value, observed.value)

def test_vol_delete_glance_metadata(self):
ctxt = context.get_admin_context()
db.volume_create(ctxt, {'id': 1})
Expand Down
10 changes: 10 additions & 0 deletions cinder/volume/api.py
Expand Up @@ -21,6 +21,7 @@
"""


import collections
import functools

from oslo.config import cfg
Expand Down Expand Up @@ -697,6 +698,15 @@ def update_snapshot_metadata(self, context,
def get_snapshot_metadata_value(self, snapshot, key):
pass

def get_volumes_image_metadata(self, context):
check_policy(context, 'get_volumes_image_metadata')
db_data = self.db.volume_glance_metadata_get_all(context)
results = collections.defaultdict(dict)
for meta_entry in db_data:
results[meta_entry['volume_id']].update({meta_entry['key']:
meta_entry['value']})
return results

@wrap_check_policy
def get_volume_image_metadata(self, context, volume):
db_data = self.db.volume_glance_metadata_get(context, volume['id'])
Expand Down

0 comments on commit 898e8c5

Please sign in to comment.