Update glance pollster to use cache

Save the images being scanned by the glance
pollster to the cache.

blueprint one-meter-per-plugin

Change-Id: I7fc808e7b6f0c0ca93a10319574cbb865a69a424
Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
This commit is contained in:
Doug Hellmann 2013-07-02 17:12:35 -04:00
parent a10773e413
commit 4437fb5f0b
2 changed files with 19 additions and 5 deletions

View File

@ -40,8 +40,7 @@ class _Base(plugin.PollsterBase):
return glanceclient.Client('1', endpoint,
token=ksclient.auth_token)
def iter_images(self, ksclient):
"""Iterate over all images."""
def _get_images(self, ksclient):
client = self.get_glance_client(ksclient)
#TODO(eglynn): use pagination to protect against unbounded
# memory usage
@ -70,6 +69,12 @@ class _Base(plugin.PollsterBase):
imageIdSet -= set([image.id])
yield image
def _iter_images(self, ksclient, cache):
"""Iterate over all images."""
if 'images' not in cache:
cache['images'] = list(self._get_images(ksclient))
return iter(cache['images'])
@staticmethod
def extract_image_metadata(image):
return dict((k, getattr(image, k))
@ -100,7 +105,7 @@ class ImagePollster(_Base):
return ['image', 'image.size']
def get_counters(self, manager, cache):
for image in self.iter_images(manager.keystone):
for image in self._iter_images(manager.keystone, cache):
yield counter.Counter(
name='image',
type=counter.TYPE_GAUGE,

View File

@ -133,12 +133,21 @@ class TestImagePollster(base.TestCase):
self.stubs.Set(glance._Base, 'get_glance_client',
self.fake_get_glance_client)
# Tests whether the iter_images method returns an unique image list.
def test_iter_images(self):
# Tests whether the iter_images method returns an unique image
# list when there is nothing in the cache
images = list(glance.ImagePollster().
iter_images(self.manager.keystone))
_iter_images(self.manager.keystone, {}))
self.assertEqual(len(images), len(set(image.id for image in images)))
def test_iter_images_cached(self):
# Tests whether the iter_images method returns the values from
# the cache
cache = {'images': []}
images = list(glance.ImagePollster().
_iter_images(self.manager.keystone, cache))
self.assertEqual(images, [])
def test_glance_image_counter(self):
counters = list(glance.ImagePollster().get_counters(self.manager, {}))
self.assertEqual(len(counters), 6)