From a9fe9b4964a6a3c4de7f4a04d5c6dd9bca2059b8 Mon Sep 17 00:00:00 2001 From: David Durieux Date: Tue, 4 Apr 2017 21:49:24 +0200 Subject: [PATCH] Fix clean satellites when receive new conf and use it. closes #768 --- alignak/daemons/brokerdaemon.py | 19 ++-------- alignak/daemons/receiverdaemon.py | 1 + alignak/daemons/schedulerdaemon.py | 11 ++++++ alignak/satellite.py | 1 + test/test_setup_new_conf.py | 61 ++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 15 deletions(-) diff --git a/alignak/daemons/brokerdaemon.py b/alignak/daemons/brokerdaemon.py index f98a6a675..87fa0f684 100644 --- a/alignak/daemons/brokerdaemon.py +++ b/alignak/daemons/brokerdaemon.py @@ -455,6 +455,7 @@ def setup_new_conf(self): # pylint: disable=R0915,R0912 """ with self.conf_lock: + self.clean_previous_run() conf = unserialize(self.new_conf, True) self.new_conf = None self.cur_conf = conf @@ -712,7 +713,9 @@ def clean_previous_run(self): self.schedulers.clear() self.pollers.clear() self.reactionners.clear() + self.receivers.clear() self.broks = self.broks[:] + self.arbiters.clear() self.broks_internal_raised = self.broks_internal_raised[:] with self.arbiter_broks_lock: self.arbiter_broks = self.arbiter_broks[:] @@ -760,21 +763,7 @@ def do_loop_turn(self): # Begin to clean modules self.check_and_del_zombie_modules() - # Maybe the arbiter ask us to wait for a new conf - # If true, we must restart all... - if self.cur_conf is None: - # Clean previous run from useless objects - # and close modules - self.clean_previous_run() - - self.wait_for_initial_conf() - # we may have been interrupted or so; then - # just return from this loop turn - if not self.new_conf: - return - self.setup_new_conf() - - # Now we check if arbiter speak to us. + # Now we check if arbiter speak to me. # If so, we listen for it # When it pushes conf to us, we reinit connections self.watch_for_new_conf(0.0) diff --git a/alignak/daemons/receiverdaemon.py b/alignak/daemons/receiverdaemon.py index 0a5b8b9eb..a4902f862 100644 --- a/alignak/daemons/receiverdaemon.py +++ b/alignak/daemons/receiverdaemon.py @@ -202,6 +202,7 @@ def setup_new_conf(self): :return: None """ with self.conf_lock: + self.clean_previous_run() conf = unserialize(self.new_conf, True) self.new_conf = None self.cur_conf = conf diff --git a/alignak/daemons/schedulerdaemon.py b/alignak/daemons/schedulerdaemon.py index 2b0fa5d46..022d7fca0 100644 --- a/alignak/daemons/schedulerdaemon.py +++ b/alignak/daemons/schedulerdaemon.py @@ -229,6 +229,7 @@ def setup_new_conf(self): :return: None """ with self.conf_lock: + self.clean_previous_run() new_c = self.new_conf logger.warning("Sending us a configuration %s", new_c['push_flavor']) conf_raw = new_c['conf'] @@ -368,6 +369,16 @@ def what_i_managed(self): else: return {} + def clean_previous_run(self): + """Clean variables from previous configuration + + :return: None + """ + # Clean all lists + self.pollers.clear() + self.reactionners.clear() + self.brokers.clear() + def main(self): """Main function for Scheduler, launch after the init:: diff --git a/alignak/satellite.py b/alignak/satellite.py index b0b64352f..e59c817b0 100644 --- a/alignak/satellite.py +++ b/alignak/satellite.py @@ -883,6 +883,7 @@ def setup_new_conf(self): # pylint: disable=R0915,R0912 :return: None """ with self.conf_lock: + self.clean_previous_run() conf = self.new_conf self.new_conf = None self.cur_conf = conf diff --git a/test/test_setup_new_conf.py b/test/test_setup_new_conf.py index 9c923914a..6b03880a5 100644 --- a/test/test_setup_new_conf.py +++ b/test/test_setup_new_conf.py @@ -57,6 +57,19 @@ def test_conf_scheduler(self): assert sched.modules[0].module_alias == 'Example' assert sched.modules[0].option_3 == 'foobar' assert 2 == len(sched.conf.hosts) + assert len(sched.pollers) == 1 + assert len(sched.reactionners) == 1 + assert len(sched.brokers) == 1 + + # send new conf, so it's the second time. This test the cleanup + self.setup_with_file('cfg/cfg_default.cfg') + for scheduler in self.arbiter.dispatcher.schedulers: + sched.new_conf = scheduler.conf_package + sched.setup_new_conf() + assert len(sched.pollers) == 1 + assert len(sched.reactionners) == 1 + assert len(sched.brokers) == 1 + # Stop launched modules sched.modules_manager.stop_all() @@ -84,6 +97,16 @@ def test_conf_receiver(self): assert receiv.modules[0].option_3 == 'foobar' # check get hosts assert len(receiv.host_assoc) == 2 + assert len(receiv.schedulers) == 1 + + # send new conf, so it's the second time. This test the cleanup + self.setup_with_file('cfg/cfg_default.cfg') + for satellite in self.arbiter.dispatcher.satellites: + if satellite.get_my_type() == 'receiver': + receiv.new_conf = satellite.cfg + receiv.setup_new_conf() + assert len(receiv.schedulers) == 1 + # Stop launched modules receiv.modules_manager.stop_all() @@ -109,6 +132,16 @@ def test_conf_poller(self): assert 1 == len(poller.new_modules_conf) assert poller.new_modules_conf[0].module_alias == 'Example' assert poller.new_modules_conf[0].option_3 == 'foobar' + assert len(poller.schedulers) == 1 + + # send new conf, so it's the second time. This test the cleanup + self.setup_with_file('cfg/cfg_default.cfg') + for satellite in self.arbiter.dispatcher.satellites: + if satellite.get_my_type() == 'poller': + poller.new_conf = satellite.cfg + poller.setup_new_conf() + assert len(poller.schedulers) == 1 + # Stop launched modules poller.modules_manager.stop_all() @@ -134,6 +167,24 @@ def test_conf_broker(self): assert 1 == len(broker.modules) assert broker.modules[0].module_alias == 'Example' assert broker.modules[0].option_3 == 'foobar' + assert len(broker.schedulers) == 1 + assert len(broker.arbiters) == 1 + assert len(broker.pollers) == 1 + assert len(broker.reactionners) == 1 + assert len(broker.receivers) == 1 + + # send new conf, so it's the second time. This test the cleanup + self.setup_with_file('cfg/cfg_default.cfg') + for satellite in self.arbiter.dispatcher.satellites: + if satellite.get_my_type() == 'broker': + broker.new_conf = satellite.cfg + broker.setup_new_conf() + assert len(broker.schedulers) == 1 + assert len(broker.arbiters) == 1 + assert len(broker.pollers) == 1 + assert len(broker.reactionners) == 1 + assert len(broker.receivers) == 1 + # Stop launched modules broker.modules_manager.stop_all() @@ -159,5 +210,15 @@ def test_conf_reactionner(self): assert 1 == len(reac.new_modules_conf) assert reac.new_modules_conf[0].module_alias == 'Example' assert reac.new_modules_conf[0].option_3 == 'foobar' + assert len(reac.schedulers) == 1 + + # send new conf, so it's the second time. This test the cleanup + self.setup_with_file('cfg/cfg_default.cfg') + for satellite in self.arbiter.dispatcher.satellites: + if satellite.get_my_type() == 'reactionner': + reac.new_conf = satellite.cfg + reac.setup_new_conf() + assert len(reac.schedulers) == 1 + # Stop launched modules reac.modules_manager.stop_all()