From 2e83d0f61fdc6baa6198efaa5ac167ae825cad97 Mon Sep 17 00:00:00 2001 From: MikhailBurdukov Date: Thu, 14 Nov 2024 09:55:06 +0000 Subject: [PATCH] Remove holder from config --- src/BridgeHelper/LibraryBridgeHelper.cpp | 3 ++- src/BridgeHelper/XDBCBridgeHelper.h | 3 ++- src/Common/ShellCommand.cpp | 15 +++++++++------ src/Common/ShellCommandsHolder.cpp | 6 ++++++ src/Common/ShellCommandsHolder.h | 2 ++ src/Common/SignalHandlers.cpp | 3 ++- src/Interpreters/Context.cpp | 15 --------------- src/Interpreters/Context.h | 5 ----- 8 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/BridgeHelper/LibraryBridgeHelper.cpp b/src/BridgeHelper/LibraryBridgeHelper.cpp index e9fd1078658..cb675da7fa5 100644 --- a/src/BridgeHelper/LibraryBridgeHelper.cpp +++ b/src/BridgeHelper/LibraryBridgeHelper.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace DB @@ -29,7 +30,7 @@ LibraryBridgeHelper::LibraryBridgeHelper(ContextPtr context_) void LibraryBridgeHelper::startBridge(std::unique_ptr cmd) const { - getContext()->addBackgroundShellCommand(std::move(cmd)); + ShellCommandsHolder::instance().addCommand(std::move(cmd)); } diff --git a/src/BridgeHelper/XDBCBridgeHelper.h b/src/BridgeHelper/XDBCBridgeHelper.h index 0851d1289db..cf1720a29ec 100644 --- a/src/BridgeHelper/XDBCBridgeHelper.h +++ b/src/BridgeHelper/XDBCBridgeHelper.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -144,7 +145,7 @@ protected: void startBridge(std::unique_ptr cmd) const override { - getContext()->addBackgroundShellCommand(std::move(cmd)); + ShellCommandsHolder::instance().addCommand(std::move(cmd)); } diff --git a/src/Common/ShellCommand.cpp b/src/Common/ShellCommand.cpp index b540cee9e82..bef7b7a0e9d 100644 --- a/src/Common/ShellCommand.cpp +++ b/src/Common/ShellCommand.cpp @@ -302,7 +302,7 @@ struct ShellCommand::tryWaitResult int ShellCommand::tryWait() { - return tryWaitImpl(false).retcode; + return tryWaitImpl(true).retcode; } ShellCommand::tryWaitResult ShellCommand::tryWaitImpl(bool blocking) @@ -311,15 +311,18 @@ ShellCommand::tryWaitResult ShellCommand::tryWaitImpl(bool blocking) ShellCommand::tryWaitResult result; - int options = ((blocking) ? WNOHANG : 0); + int options = ((!blocking) ? WNOHANG : 0); int status = 0; int waitpid_retcode = -1; while (waitpid_retcode < 0) { waitpid_retcode = waitpid(pid, &status, options); - - if (blocking && !waitpid_retcode) + if (waitpid_retcode > 0) + { + break; + } + if (!blocking && !waitpid_retcode) { result.is_process_terminated = false; return result; @@ -385,7 +388,7 @@ void ShellCommand::handleProcessRetcode(int retcode) const bool ShellCommand::waitIfProccesTerminated() { - auto proc_status = tryWaitImpl(true); + auto proc_status = tryWaitImpl(false); if (proc_status.is_process_terminated) { handleProcessRetcode(proc_status.retcode); @@ -396,7 +399,7 @@ bool ShellCommand::waitIfProccesTerminated() void ShellCommand::wait() { - int retcode = tryWaitImpl(false).retcode; + int retcode = tryWaitImpl(true).retcode; handleProcessRetcode(retcode); } diff --git a/src/Common/ShellCommandsHolder.cpp b/src/Common/ShellCommandsHolder.cpp index eb3f981a6e4..1354cd2e4b1 100644 --- a/src/Common/ShellCommandsHolder.cpp +++ b/src/Common/ShellCommandsHolder.cpp @@ -5,6 +5,12 @@ namespace DB { +ShellCommandsHolder & ShellCommandsHolder::instance() +{ + static ShellCommandsHolder instance; + return instance; +} + LoggerPtr ShellCommandsHolder::getLogger() { return ::getLogger("ShellCommandsHolder"); diff --git a/src/Common/ShellCommandsHolder.h b/src/Common/ShellCommandsHolder.h index 95db7622ebd..70934365768 100644 --- a/src/Common/ShellCommandsHolder.h +++ b/src/Common/ShellCommandsHolder.h @@ -15,6 +15,8 @@ namespace DB class ShellCommandsHolder final : public boost::noncopyable { public: + static ShellCommandsHolder & instance(); + void removeCommand(pid_t pid); void addCommand(std::unique_ptr command); diff --git a/src/Common/SignalHandlers.cpp b/src/Common/SignalHandlers.cpp index 440cfe0b57e..63ac2df272b 100644 --- a/src/Common/SignalHandlers.cpp +++ b/src/Common/SignalHandlers.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -312,7 +313,7 @@ void SignalListener::run() { pid_t child_pid = 0; readBinary(child_pid, in); - Context::getGlobalContextInstance()->removeBackgroundShellCommand(child_pid); + ShellCommandsHolder::instance().removeCommand(child_pid); } else { diff --git a/src/Interpreters/Context.cpp b/src/Interpreters/Context.cpp index 951571dd1db..78d41a336b6 100644 --- a/src/Interpreters/Context.cpp +++ b/src/Interpreters/Context.cpp @@ -100,7 +100,6 @@ #include #include #include -#include #include #include #include @@ -541,10 +540,6 @@ struct ContextSharedPart : boost::noncopyable /// No lock required for application_type modified only during initialization Context::ApplicationType application_type = Context::ApplicationType::SERVER; - /// Manager of running background shell commands. - /// They will be killed when Context will be destroyed or with SIGCHLD signal. - ShellCommandsHolder background_active_shell_commands; - /// No lock required for config_reload_callback, start_servers_callback, stop_servers_callback modified only during initialization Context::ConfigReloadCallback config_reload_callback; Context::StartStopServersCallback start_servers_callback; @@ -5069,16 +5064,6 @@ void Context::addQueryParameters(const NameToNameMap & parameters) query_parameters.insert_or_assign(name, value); } -void Context::addBackgroundShellCommand(std::unique_ptr cmd) const -{ - shared->background_active_shell_commands.addCommand(std::move(cmd)); -} - -void Context::removeBackgroundShellCommand(pid_t pid) const -{ - shared->background_active_shell_commands.removeCommand(pid); -} - IHostContextPtr & Context::getHostContext() { diff --git a/src/Interpreters/Context.h b/src/Interpreters/Context.h index 51f6e299c95..cb8bf9634e2 100644 --- a/src/Interpreters/Context.h +++ b/src/Interpreters/Context.h @@ -1288,11 +1288,6 @@ public: /// Overrides values of existing parameters. void addQueryParameters(const NameToNameMap & parameters); - /// Add background shell command. It will be killed after context destruction or with SIGCHLD. - void addBackgroundShellCommand(std::unique_ptr cmd) const; - - /// Terminate background shell command. - void removeBackgroundShellCommand(pid_t pid) const; IHostContextPtr & getHostContext(); const IHostContextPtr & getHostContext() const;