AMBARI-18922 Agent Auto Restart Doesn't Release Ping Port (dsen)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/d46461a3 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/d46461a3 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/d46461a3 Branch: refs/heads/branch-2.5 Commit: d46461a308762f29c789d044873b685643fc63bc Parents: f8ccb47 Author: Dmytro Sen <d...@apache.org> Authored: Mon Nov 21 12:01:31 2016 +0200 Committer: Toader, Sebastian <stoa...@hortonworks.com> Committed: Wed Jan 18 09:41:55 2017 +0100 ---------------------------------------------------------------------- .../src/main/python/ambari_agent/AmbariAgent.py | 19 +++++++++++++------ .../ambari_agent/StatusCommandsExecutor.py | 4 +++- .../test/python/ambari_agent/TestAmbariAgent.py | 7 ++++++- 3 files changed, 22 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/d46461a3/ambari-agent/src/main/python/ambari_agent/AmbariAgent.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/ambari_agent/AmbariAgent.py b/ambari-agent/src/main/python/ambari_agent/AmbariAgent.py index d701e49..28b9528 100644 --- a/ambari-agent/src/main/python/ambari_agent/AmbariAgent.py +++ b/ambari-agent/src/main/python/ambari_agent/AmbariAgent.py @@ -21,6 +21,7 @@ limitations under the License. import os import sys import subprocess +import signal from Controller import AGENT_AUTO_RESTART_EXIT_CODE if os.environ.has_key("PYTHON_BIN"): @@ -48,12 +49,18 @@ def main(): mergedArgs = [PYTHON, AGENT_SCRIPT] + args - while status == AGENT_AUTO_RESTART_EXIT_CODE: - mainProcess = subprocess.Popen(mergedArgs) - mainProcess.communicate() - status = mainProcess.returncode - if os.path.isfile(AGENT_PID_FILE) and status == AGENT_AUTO_RESTART_EXIT_CODE: - os.remove(AGENT_PID_FILE) + # Become a parent for all subprocesses + os.setpgrp() + + try: + while status == AGENT_AUTO_RESTART_EXIT_CODE: + mainProcess = subprocess.Popen(mergedArgs) + mainProcess.communicate() + status = mainProcess.returncode + if os.path.isfile(AGENT_PID_FILE) and status == AGENT_AUTO_RESTART_EXIT_CODE: + os.remove(AGENT_PID_FILE) + finally: + os.killpg(0, signal.SIGKILL) if __name__ == "__main__": main() \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/d46461a3/ambari-agent/src/main/python/ambari_agent/StatusCommandsExecutor.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/ambari_agent/StatusCommandsExecutor.py b/ambari-agent/src/main/python/ambari_agent/StatusCommandsExecutor.py index 5d3607a..fbb29f4 100644 --- a/ambari-agent/src/main/python/ambari_agent/StatusCommandsExecutor.py +++ b/ambari-agent/src/main/python/ambari_agent/StatusCommandsExecutor.py @@ -24,6 +24,7 @@ import logging import multiprocessing from ambari_agent.PythonReflectiveExecutor import PythonReflectiveExecutor from ambari_agent.RemoteDebugUtils import bind_debug_signal_handlers +from ambari_agent.ExitHelper import ExitHelper logger = logging.getLogger(__name__) @@ -41,6 +42,7 @@ class StatusCommandsExecutor(multiprocessing.Process): self.status_command_timeout = int(self.config.get('agent', 'status_command_timeout', 5)) # in seconds self.hasTimeoutedEvent = multiprocessing.Event() + ExitHelper().register(self.kill) def run(self): try: @@ -88,4 +90,4 @@ class StatusCommandsExecutor(multiprocessing.Process): # prevent queue from ending up with non-freed semaphores, locks during put. Which would result in dead-lock in process executing get. self.actionQueue.statusCommandResultQueue.close() self.actionQueue.statusCommandResultQueue.join_thread() - self.actionQueue.statusCommandResultQueue = multiprocessing.Queue() \ No newline at end of file + self.actionQueue.statusCommandResultQueue = multiprocessing.Queue() http://git-wip-us.apache.org/repos/asf/ambari/blob/d46461a3/ambari-agent/src/test/python/ambari_agent/TestAmbariAgent.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/test/python/ambari_agent/TestAmbariAgent.py b/ambari-agent/src/test/python/ambari_agent/TestAmbariAgent.py index 1be487c..8ff192a 100644 --- a/ambari-agent/src/test/python/ambari_agent/TestAmbariAgent.py +++ b/ambari-agent/src/test/python/ambari_agent/TestAmbariAgent.py @@ -34,7 +34,10 @@ class TestAmbariAgent(unittest.TestCase): @patch.object(subprocess, "Popen") @patch("os.path.isfile") @patch("os.remove") - def test_main(self, os_remove_mock, os_path_isfile_mock, subprocess_popen_mock): + @patch("os.killpg") + @patch("os.setpgrp") + def test_main(self, os_setpgrp_mock, os_killpg_mock, os_remove_mock, + os_path_isfile_mock, subprocess_popen_mock): facter1 = MagicMock() facter2 = MagicMock() subprocess_popen_mock.side_effect = [facter1, facter2] @@ -46,6 +49,7 @@ class TestAmbariAgent(unittest.TestCase): sys.argv[0] = "test data" AmbariAgent.main() + self.assertTrue(os_setpgrp_mock.called) self.assertTrue(subprocess_popen_mock.called) self.assertTrue(subprocess_popen_mock.call_count == 2) self.assertTrue(facter1.communicate.called) @@ -53,6 +57,7 @@ class TestAmbariAgent(unittest.TestCase): self.assertTrue(os_path_isfile_mock.called) self.assertTrue(os_path_isfile_mock.call_count == 2) self.assertTrue(os_remove_mock.called) + self.assertTrue(os_killpg_mock.called) # # Test AmbariConfig.getLogFile() for ambari-agent