shorter sleeps if metadata changes are detected

This patch updates os-collect-config so that the sleep interval
time is shortened if changes are detected. This should decrease
deployment time when using Heat templates which use depends_on
to step through a sequence of software deployment resources.

The new default sleep is set to 1 and increases
by sleep_time *= 2 until it reaches the default
sleep interval again.

Change-Id: I5cbd0956db2abebec876b15bee72b70ec64d5aef
This commit is contained in:
Dan Prince 2015-06-06 10:46:43 -04:00
parent b10b844fec
commit b63188eb25
2 changed files with 18 additions and 8 deletions

View File

@ -65,8 +65,10 @@ opts = [
' one execution of command. This behavior is implied if no' ' one execution of command. This behavior is implied if no'
' command is specified.'), ' command is specified.'),
cfg.FloatOpt('polling-interval', short='i', default=30, cfg.FloatOpt('polling-interval', short='i', default=30,
help='When running continuously, pause this many seconds' help='When running continuously, pause a maximum of this'
' between collecting data.'), ' many seconds between collecting data. If changes'
' are detected shorter sleeps intervals are gradually'
' increased to this maximum polling interval.'),
cfg.BoolOpt('print-cachedir', cfg.BoolOpt('print-cachedir',
default=False, default=False,
help='Print out the value of cachedir and exit immediately.'), help='Print out the value of cachedir and exit immediately.'),
@ -250,6 +252,7 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
exitval = 0 exitval = 0
config_files = CONF.config_file config_files = CONF.config_file
config_hash = getfilehash(config_files) config_hash = getfilehash(config_files)
sleep_time = 1
while True: while True:
store_and_run = bool(CONF.command and not CONF.print_only) store_and_run = bool(CONF.command and not CONF.print_only)
(changed_keys, content) = collect_all( (changed_keys, content) = collect_all(
@ -258,6 +261,9 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
collector_kwargs_map=collector_kwargs_map) collector_kwargs_map=collector_kwargs_map)
if store_and_run: if store_and_run:
if changed_keys or CONF.force: if changed_keys or CONF.force:
# shorter sleeps while changes are detected allows for faster
# software deployment dependency processing
sleep_time = 1
# ignore HUP now since we will reexec after commit anyway # ignore HUP now since we will reexec after commit anyway
signal.signal(signal.SIGHUP, signal.SIG_IGN) signal.signal(signal.SIGHUP, signal.SIG_IGN)
try: try:
@ -271,9 +277,9 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
if config_hash == new_config_hash: if config_hash == new_config_hash:
logger.warn( logger.warn(
'Sleeping %.2f seconds before re-exec.' % 'Sleeping %.2f seconds before re-exec.' %
CONF.polling_interval sleep_time
) )
time.sleep(CONF.polling_interval) time.sleep(sleep_time)
else: else:
# The command failed but the config file has # The command failed but the config file has
# changed re-exec now as the config file change # changed re-exec now as the config file change
@ -290,8 +296,12 @@ def __main__(args=sys.argv, collector_kwargs_map=None):
if CONF.one_time: if CONF.one_time:
break break
else: else:
logger.info("Sleeping %.2f seconds.", CONF.polling_interval) logger.info("Sleeping %.2f seconds.", sleep_time)
time.sleep(CONF.polling_interval) time.sleep(sleep_time)
sleep_time *= 2
if sleep_time > CONF.polling_interval:
sleep_time = CONF.polling_interval
else: else:
print(json.dumps(content, indent=1)) print(json.dumps(content, indent=1))
break break

View File

@ -309,8 +309,8 @@ class TestCollect(testtools.TestCase):
pass pass
def fake_sleep(sleep_time): def fake_sleep(sleep_time):
self.assertEqual(10, sleep_time) if sleep_time == 10:
raise ExpectedException raise ExpectedException
self.useFixture(fixtures.MonkeyPatch('time.sleep', fake_sleep)) self.useFixture(fixtures.MonkeyPatch('time.sleep', fake_sleep))
try: try: