Update kwapi pollster to use cache

Use the pollster cache to store the list of probes.

blueprint one-meter-per-plugin

Change-Id: I60d9277b2ef1db9bb118cb7fa26ca940b3cc2de8
Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
This commit is contained in:
Doug Hellmann 2013-07-02 17:47:35 -04:00
parent df62b92191
commit 3b20729d6f
2 changed files with 34 additions and 13 deletions

View File

@ -21,6 +21,7 @@ import requests
from ceilometer.central import plugin
from ceilometer import counter
from ceilometer.openstack.common.gettextutils import _
from ceilometer.openstack.common import log
LOG = log.getLogger(__name__)
@ -60,14 +61,21 @@ class _Base(plugin.CentralPollster):
)
return KwapiClient(endpoint, ksclient.auth_token)
def iter_probes(self, ksclient):
CACHE_KEY_PROBE = 'kwapi.probes'
def _iter_probes(self, ksclient, cache):
"""Iterate over all probes."""
if self.CACHE_KEY_PROBE not in cache:
cache[self.CACHE_KEY_PROBE] = self._get_probes(ksclient)
return iter(cache[self.CACHE_KEY_PROBE])
def _get_probes(self, ksclient):
try:
client = self.get_kwapi_client(ksclient)
except exceptions.EndpointNotFound:
LOG.debug(_("Kwapi endpoint not found"))
return []
return client.iter_probes()
return list(client.iter_probes())
class KwapiPollster(_Base):
@ -79,7 +87,7 @@ class KwapiPollster(_Base):
def get_counters(self, manager, cache):
"""Returns all counters."""
for probe in self.iter_probes(manager.keystone):
for probe in self._iter_probes(manager.keystone, cache):
yield counter.Counter(
name='energy',
type=counter.TYPE_CUMULATIVE,

View File

@ -55,7 +55,7 @@ class TestManager(manager.AgentManager):
class TestKwapiPollster(base.TestCase):
@staticmethod
def fake_kwapi_iter_probes(self, ksclient):
def fake_iter_probes(self, ksclient, cache):
probes = PROBE_DICT['probes']
for key, value in probes.iteritems():
probe_dict = value
@ -63,7 +63,7 @@ class TestKwapiPollster(base.TestCase):
yield probe_dict
@staticmethod
def fake_kwapi_get_kwapi_client(self, ksclient):
def fake_get_kwapi_client(self, ksclient):
raise exceptions.EndpointNotFound("fake keystone exception")
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
@ -72,15 +72,16 @@ class TestKwapiPollster(base.TestCase):
self.context = context.get_admin_context()
self.manager = TestManager()
def test_kwapi_endpoint_not_exist(self):
def test_endpoint_not_exist(self):
self.stubs.Set(kwapi._Base, 'get_kwapi_client',
self.fake_kwapi_get_kwapi_client)
self.fake_get_kwapi_client)
counters = list(kwapi.KwapiPollster().get_counters(self.manager, {}))
self.assertEqual(len(counters), 0)
def test_kwapi_counter(self):
self.stubs.Set(kwapi._Base, 'iter_probes', self.fake_kwapi_iter_probes)
def test_counter(self):
self.stubs.Set(kwapi._Base, '_iter_probes',
self.fake_iter_probes)
counters = list(kwapi.KwapiPollster().get_counters(self.manager, {}))
self.assertEqual(len(counters), 6)
@ -101,9 +102,21 @@ class TestKwapiPollster(base.TestCase):
any(map(lambda counter: counter.volume == probe['w'],
power_counters)))
def test_kwapi_counter_list(self):
self.stubs.Set(kwapi._Base, 'iter_probes', self.fake_kwapi_iter_probes)
def test_get_counters_cached(self):
probe = {'id': 'A'}
probe.update(PROBE_DICT['probes']['A'])
cache = {
kwapi.KwapiPollster.CACHE_KEY_PROBE: [probe],
}
self.manager.keystone = mock.Mock()
pollster = kwapi.KwapiPollster()
counters = list(pollster.get_counters(self.manager, cache))
self.assertEqual(len(counters), 2)
counters = list(kwapi.KwapiPollster().get_counters(self.manager, {}))
def test_counter_list(self):
self.stubs.Set(kwapi._Base, '_iter_probes',
self.fake_iter_probes)
pollster = kwapi.KwapiPollster()
counters = list(pollster.get_counters(self.manager, {}))
self.assertEqual(set([c.name for c in counters]),
set(kwapi.KwapiPollster().get_counter_names()))
set(pollster.get_counter_names()))