Commit 00b85271 authored by Jenkins's avatar Jenkins Committed by Gerrit Code Review

Merge "Add an update interval to compute discovery"

parents 7942f0bd 45ef4699
......@@ -23,7 +23,18 @@ OPTS = [
help='Enable work-load partitioning, allowing multiple '
'compute agents to be run simultaneously.')
'compute agents to be run simultaneously.'),
help="New instances will be discovered periodically based"
" on this option (in seconds). By default, "
"the agent discovers instances according to pipeline "
"polling interval. If option is greater than 0, "
"the instance list to poll will be updated based "
"on this option's interval. Measurements relating "
"to the instances will match intervals "
"defined in pipeline.")
cfg.CONF.register_opts(OPTS, group='compute')
......@@ -34,17 +45,30 @@ class InstanceDiscovery(plugin_base.DiscoveryBase):
self.nova_cli = nova_client.Client()
self.last_run = None
self.instances = {}
self.expiration_time = cfg.CONF.compute.resource_update_interval
def discover(self, manager, param=None):
"""Discover resources to monitor."""
instances = self.nova_cli.instance_get_all_by_host(, self.last_run)
except Exception:
# NOTE(zqfan): instance_get_all_by_host is wrapped and will log
# exception when there is any error. It is no need to raise it
# again and print one more time.
return []
secs_from_last_update = 0
if self.last_run:
secs_from_last_update = timeutils.delta_seconds(
self.last_run, timeutils.utcnow(True))
instances = []
# NOTE(ityaptin) we update make a nova request only if
# it's a first discovery or resources expired
if not self.last_run or secs_from_last_update >= self.expiration_time:
utc_now = timeutils.utcnow(True)
since = self.last_run.isoformat() if self.last_run else None
instances = self.nova_cli.instance_get_all_by_host(, since)
self.last_run = utc_now
except Exception:
# NOTE(zqfan): instance_get_all_by_host is wrapped and will log
# exception when there is any error. It is no need to raise it
# again and print one more time.
return []
for instance in instances:
if getattr(instance, 'OS-EXT-STS:vm_state', None) in ['deleted',
......@@ -52,7 +76,7 @@ class InstanceDiscovery(plugin_base.DiscoveryBase):
self.instances.pop(, None)
self.instances[] = instance
self.last_run = timeutils.utcnow(True).isoformat()
return self.instances.values()
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import iso8601
import mock
from oslo_config import fixture as fixture_config
from oslotest import mockpatch
from ceilometer.compute import discovery
import ceilometer.tests.base as base
class TestDiscovery(base.BaseTestCase):
def setUp(self):
super(TestDiscovery, self).setUp()
self.instance = mock.MagicMock() = 'instance-00000001'
setattr(self.instance, 'OS-EXT-SRV-ATTR:instance_name',
setattr(self.instance, 'OS-EXT-STS:vm_state',
'active') = 1
self.instance.flavor = {'name': 'm1.small', 'id': 2, 'vcpus': 1,
'ram': 512, 'disk': 20, 'ephemeral': 0}
self.instance.status = 'active'
self.instance.metadata = {
'fqdn': 'vm_fqdn',
'metering.stack': '2cadc4b4-8789-123c-b4eg-edd2f0a9c128',
'project_cos': 'dev'}
# as we're having lazy hypervisor inspector singleton object in the
# base compute pollster class, that leads to the fact that we
# need to mock all this class property to avoid context sharing between
# the tests
self.client = mock.MagicMock()
self.client.instance_get_all_by_host.return_value = [self.instance]
patch_client = mockpatch.Patch('ceilometer.nova_client.Client',
self.utc_now = mock.MagicMock(
return_value=datetime.datetime(2016, 1, 1,
patch_timeutils = mockpatch.Patch('oslo_utils.timeutils.utcnow',
self.CONF = self.useFixture(fixture_config.Config()).conf
self.CONF.set_override('host', 'test')
def test_normal_discovery(self):
dsc = discovery.InstanceDiscovery()
resources =
self.assertEqual(1, len(resources))
self.assertEqual(1, list(resources)[0].id)
'test', None)
resources =
self.assertEqual(1, len(resources))
self.assertEqual(1, list(resources)[0].id)
self.client.instance_get_all_by_host.assert_called_with(, "2016-01-01T00:00:00+00:00")
def test_discovery_with_resource_update_interval(self):
self.CONF.set_override("resource_update_interval", 600,
dsc = discovery.InstanceDiscovery()
dsc.last_run = datetime.datetime(2016, 1, 1,
self.utc_now.return_value = datetime.datetime(
2016, 1, 1, minute=5, tzinfo=iso8601.iso8601.UTC)
resources =
self.assertEqual(0, len(resources))
self.utc_now.return_value = datetime.datetime(
2016, 1, 1, minute=20, tzinfo=iso8601.iso8601.UTC)
resources =
self.assertEqual(1, len(resources))
self.assertEqual(1, list(resources)[0].id)
self.client.instance_get_all_by_host.assert_called_once_with(, "2016-01-01T00:00:00+00:00")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment