This commit is contained in:
MikhailBurdukov 2024-11-07 10:52:52 +00:00
parent 16ef3d7511
commit afc0251fd6
5 changed files with 17 additions and 17 deletions

View File

@ -1,6 +1,6 @@
#include <Common/logger_useful.h>
#include <Common/Exception.h>
#include <Common/BackgroundShellCommandHolder.h>
#include <Common/ShellCommandsHolder.h>
namespace DB
{
@ -10,25 +10,25 @@ namespace ErrorCodes
extern const int LOGICAL_ERROR;
}
LoggerPtr BackgroundShellCommandHolder::getLogger()
LoggerPtr ShellCommandsHolder::getLogger()
{
return ::getLogger("BackgroundShellCommandHolder");
return ::getLogger("ShellCommandsHolder");
}
void BackgroundShellCommandHolder::removeCommand(pid_t pid)
void ShellCommandsHolder::removeCommand(pid_t pid)
{
std::lock_guard lock(mutex);
bool is_erased = active_shell_commands.erase(pid);
bool is_erased = shell_commands.erase(pid);
LOG_TRACE(getLogger(), "Try to erase command with the pid {}, is_erased: {}", pid, is_erased);
}
void BackgroundShellCommandHolder::addCommand(std::unique_ptr<ShellCommand> command)
void ShellCommandsHolder::addCommand(std::unique_ptr<ShellCommand> command)
{
std::lock_guard lock(mutex);
pid_t command_pid = command->getPid();
auto [iterator, is_inserted] = active_shell_commands.emplace(std::make_pair(command_pid, std::move(command)));
auto [iterator, is_inserted] = shell_commands.emplace(std::make_pair(command_pid, std::move(command)));
if (!is_inserted)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Can't insert process PID {} into active shell commands, because there are running process with same PID", command_pid);

View File

@ -1,9 +1,9 @@
#pragma once
#include <Common/ShellCommand.h>
#include <boost/noncopyable.hpp>
#include <memory>
#include <mutex>
#include <Common/ShellCommand.h>
#include <unordered_map>
@ -12,17 +12,17 @@ namespace DB
/** The holder class for running background shell processes.
*/
class BackgroundShellCommandHolder final
class ShellCommandsHolder final : public boost::noncopyable
{
public:
void removeCommand(pid_t pid);
void addCommand(std::unique_ptr<ShellCommand> command);
private:
using ActiveShellCommandsCollection = std::unordered_map<pid_t, std::unique_ptr<ShellCommand>>;
using ShellCommands = std::unordered_map<pid_t, std::unique_ptr<ShellCommand>>;
std::mutex mutex;
ActiveShellCommandsCollection active_shell_commands TSA_GUARDED_BY(mutex);
ShellCommands shell_commands TSA_GUARDED_BY(mutex);
static LoggerPtr getLogger();
};

View File

@ -314,7 +314,7 @@ void SignalListener::run()
{
pid_t child_pid = 0;
readBinary(child_pid, in);
Context::getGlobalContextInstance()->terminateBackgroundShellCommand(child_pid);
Context::getGlobalContextInstance()->removeBackgroundShellCommand(child_pid);
}
else
{

View File

@ -99,7 +99,7 @@
#include <Common/Config/AbstractConfigurationComparison.h>
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/ShellCommand.h>
#include <Common/BackgroundShellCommandHolder.h>
#include <Common/ShellCommandsHolder.h>
#include <Common/logger_useful.h>
#include <Common/RemoteHostFilter.h>
#include <Common/HTTPHeaderFilter.h>
@ -527,7 +527,7 @@ struct ContextSharedPart : boost::noncopyable
/// Manager of running background shell commands.
/// They will be killed when Context will be destroyed or with SIGCHLD signal.
BackgroundShellCommandHolder background_active_shell_commands;
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;
@ -5023,7 +5023,7 @@ void Context::addBackgroundShellCommand(std::unique_ptr<ShellCommand> cmd) const
shared->background_active_shell_commands.addCommand(std::move(cmd));
}
void Context::terminateBackgroundShellCommand(pid_t pid) const
void Context::removeBackgroundShellCommand(pid_t pid) const
{
shared->background_active_shell_commands.removeCommand(pid);
}

View File

@ -1288,7 +1288,7 @@ public:
void addBackgroundShellCommand(std::unique_ptr<ShellCommand> cmd) const;
/// Terminate background shell command.
void terminateBackgroundShellCommand(pid_t pid) const;
void removeBackgroundShellCommand(pid_t pid) const;
IHostContextPtr & getHostContext();
const IHostContextPtr & getHostContext() const;