2019-01-14 19:22:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
#include <Common/Exception.h>
|
2020-07-16 23:12:47 +00:00
|
|
|
#include <Common/getNumberOfPhysicalCPUCores.h>
|
2019-01-14 19:22:09 +00:00
|
|
|
|
2020-06-22 19:04:12 +00:00
|
|
|
#include <cassert>
|
2019-01-14 19:22:09 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2020-06-14 06:43:01 +00:00
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
|
|
|
|
2019-01-14 19:22:09 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_SCHEDULE_TASK;
|
2020-09-15 10:29:47 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-01-14 19:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-14 10:59:58 +00:00
|
|
|
|
2019-08-01 20:09:38 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric GlobalThread;
|
|
|
|
extern const Metric GlobalThreadActive;
|
|
|
|
extern const Metric LocalThread;
|
|
|
|
extern const Metric LocalThreadActive;
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
|
2020-07-16 23:12:47 +00:00
|
|
|
template <typename Thread>
|
|
|
|
ThreadPoolImpl<Thread>::ThreadPoolImpl()
|
|
|
|
: ThreadPoolImpl(getNumberOfPhysicalCPUCores())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
template <typename Thread>
|
2019-08-03 11:02:40 +00:00
|
|
|
ThreadPoolImpl<Thread>::ThreadPoolImpl(size_t max_threads_)
|
|
|
|
: ThreadPoolImpl(max_threads_, max_threads_, max_threads_)
|
2019-01-14 10:59:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
Do not shutdown global thread pool on exception
Otherwise GlobalThreadPool can be terminated (for example due to an
exception from the ParallelInputsHandler::onFinish/onFinishThread, from
ParallelAggregatingBlockInputStream::Handler::onFinish/onFinishThread,
since writeToTemporaryFile() can definitelly throw) and the server will
not accept new connections (or/and execute queries) anymore.
Here is possible stacktrace (it is a bit inaccurate, due to
optimizations I guess, and it had been obtained with the
DB::tryLogCurrentException() in the catch block of the
ThreadPoolImpl::worker()):
2020.02.16 22:30:40.415246 [ 45909 ] {} <Error> ThreadPool: Unhandled exception in the ThreadPool(10000,1000,10000) the loop will be shutted down: Code: 241, e.displayText() = DB::Exception: Memory limit (total) exceeded: would use 279.40 GiB (attempt to allocate chunk of 4205536 bytes), maximum: 279.40 GiB, Stack trace (when copying this message, always include the lines below):
1. Common/Exception.cpp:35: DB::Exception::Exception(...)
...
6. Common/Allocator.h:102: void DB::PODArrayBase<8ul, 4096ul, Allocator<false, false>, 15ul, 16ul>::reserve<>(unsigned long) (.part.0)
7. Interpreters/Aggregator.cpp:1040: void DB::Aggregator::writeToTemporaryFileImpl<...>(...)
8. Interpreters/Aggregator.cpp:719: DB::Aggregator::writeToTemporaryFile(...)
9. include/memory:4206: DB::Aggregator::writeToTemporaryFile(...)
10. DataStreams/ParallelInputsProcessor.h:223: DB::ParallelInputsProcessor<DB::ParallelAggregatingBlockInputStream::Handler>::thread(...)
Refs: https://github.com/ClickHouse/ClickHouse/issues/6833#issuecomment-579221732
(Reference to particular comment, since I'm not sure about the initial issue)
2020-02-17 20:15:29 +00:00
|
|
|
ThreadPoolImpl<Thread>::ThreadPoolImpl(size_t max_threads_, size_t max_free_threads_, size_t queue_size_, bool shutdown_on_exception_)
|
|
|
|
: max_threads(max_threads_)
|
|
|
|
, max_free_threads(max_free_threads_)
|
|
|
|
, queue_size(queue_size_)
|
|
|
|
, shutdown_on_exception(shutdown_on_exception_)
|
2019-01-14 10:59:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-02 17:14:04 +00:00
|
|
|
template <typename Thread>
|
|
|
|
void ThreadPoolImpl<Thread>::setMaxThreads(size_t value)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
max_threads = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
void ThreadPoolImpl<Thread>::setMaxFreeThreads(size_t value)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
max_free_threads = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
void ThreadPoolImpl<Thread>::setQueueSize(size_t value)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
queue_size = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
template <typename Thread>
|
2019-01-14 19:22:09 +00:00
|
|
|
template <typename ReturnType>
|
|
|
|
ReturnType ThreadPoolImpl<Thread>::scheduleImpl(Job job, int priority, std::optional<uint64_t> wait_microseconds)
|
2019-01-14 10:59:58 +00:00
|
|
|
{
|
2019-06-29 22:37:46 +00:00
|
|
|
auto on_error = [&]
|
2019-01-14 19:22:09 +00:00
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<ReturnType, void>)
|
2019-06-29 22:37:46 +00:00
|
|
|
{
|
|
|
|
if (first_exception)
|
|
|
|
{
|
|
|
|
std::exception_ptr exception;
|
|
|
|
std::swap(exception, first_exception);
|
|
|
|
std::rethrow_exception(exception);
|
|
|
|
}
|
2019-01-14 19:22:09 +00:00
|
|
|
throw DB::Exception("Cannot schedule a task", DB::ErrorCodes::CANNOT_SCHEDULE_TASK);
|
2019-06-29 22:37:46 +00:00
|
|
|
}
|
2019-01-14 19:22:09 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
|
|
|
|
auto pred = [this] { return !queue_size || scheduled_jobs < queue_size || shutdown; };
|
|
|
|
|
2019-08-02 17:14:04 +00:00
|
|
|
if (wait_microseconds) /// Check for optional. Condition is true if the optional is set and the value is zero.
|
2019-01-14 19:22:09 +00:00
|
|
|
{
|
|
|
|
if (!job_finished.wait_for(lock, std::chrono::microseconds(*wait_microseconds), pred))
|
|
|
|
return on_error();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
job_finished.wait(lock, pred);
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
if (shutdown)
|
2019-01-14 19:22:09 +00:00
|
|
|
return on_error();
|
2019-01-14 10:59:58 +00:00
|
|
|
|
|
|
|
jobs.emplace(std::move(job), priority);
|
2019-01-14 19:22:09 +00:00
|
|
|
++scheduled_jobs;
|
2019-01-14 10:59:58 +00:00
|
|
|
|
2019-01-14 19:22:09 +00:00
|
|
|
if (threads.size() < std::min(max_threads, scheduled_jobs))
|
|
|
|
{
|
|
|
|
threads.emplace_front();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
threads.front() = Thread([this, it = threads.begin()] { worker(it); });
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
threads.pop_front();
|
2019-08-02 17:14:04 +00:00
|
|
|
|
|
|
|
/// Remove the job and return error to caller.
|
|
|
|
/// Note that if we have allocated at least one thread, we may continue
|
|
|
|
/// (one thread is enough to process all jobs).
|
|
|
|
/// But this condition indicate an error nevertheless and better to refuse.
|
|
|
|
|
|
|
|
jobs.pop();
|
|
|
|
--scheduled_jobs;
|
|
|
|
return on_error();
|
2019-01-14 19:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-14 10:59:58 +00:00
|
|
|
}
|
|
|
|
new_job_or_shutdown.notify_one();
|
2019-01-14 19:22:09 +00:00
|
|
|
return ReturnType(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
2019-10-17 14:41:27 +00:00
|
|
|
void ThreadPoolImpl<Thread>::scheduleOrThrowOnError(Job job, int priority)
|
2019-01-14 19:22:09 +00:00
|
|
|
{
|
|
|
|
scheduleImpl<void>(std::move(job), priority, std::nullopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
2019-10-17 14:41:27 +00:00
|
|
|
bool ThreadPoolImpl<Thread>::trySchedule(Job job, int priority, uint64_t wait_microseconds) noexcept
|
2019-01-14 19:22:09 +00:00
|
|
|
{
|
|
|
|
return scheduleImpl<bool>(std::move(job), priority, wait_microseconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
void ThreadPoolImpl<Thread>::scheduleOrThrow(Job job, int priority, uint64_t wait_microseconds)
|
|
|
|
{
|
|
|
|
scheduleImpl<void>(std::move(job), priority, wait_microseconds);
|
2019-01-14 10:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
void ThreadPoolImpl<Thread>::wait()
|
|
|
|
{
|
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
job_finished.wait(lock, [this] { return scheduled_jobs == 0; });
|
2019-01-14 10:59:58 +00:00
|
|
|
|
|
|
|
if (first_exception)
|
|
|
|
{
|
|
|
|
std::exception_ptr exception;
|
|
|
|
std::swap(exception, first_exception);
|
|
|
|
std::rethrow_exception(exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
ThreadPoolImpl<Thread>::~ThreadPoolImpl()
|
|
|
|
{
|
|
|
|
finalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
void ThreadPoolImpl<Thread>::finalize()
|
|
|
|
{
|
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
2019-01-14 10:59:58 +00:00
|
|
|
shutdown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_job_or_shutdown.notify_all();
|
|
|
|
|
|
|
|
for (auto & thread : threads)
|
|
|
|
thread.join();
|
|
|
|
|
|
|
|
threads.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
|
|
|
size_t ThreadPoolImpl<Thread>::active() const
|
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
return scheduled_jobs;
|
2019-01-14 10:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Thread>
|
2019-01-14 19:22:09 +00:00
|
|
|
void ThreadPoolImpl<Thread>::worker(typename std::list<Thread>::iterator thread_it)
|
2019-01-14 10:59:58 +00:00
|
|
|
{
|
2019-08-01 20:09:38 +00:00
|
|
|
CurrentMetrics::Increment metric_all_threads(
|
|
|
|
std::is_same_v<Thread, std::thread> ? CurrentMetrics::GlobalThread : CurrentMetrics::LocalThread);
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Job job;
|
|
|
|
bool need_shutdown = false;
|
|
|
|
|
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
2019-01-14 10:59:58 +00:00
|
|
|
new_job_or_shutdown.wait(lock, [this] { return shutdown || !jobs.empty(); });
|
|
|
|
need_shutdown = shutdown;
|
|
|
|
|
|
|
|
if (!jobs.empty())
|
|
|
|
{
|
2020-11-06 15:53:33 +00:00
|
|
|
job = std::move(jobs.top().job);
|
2019-01-14 10:59:58 +00:00
|
|
|
jobs.pop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-05 20:49:21 +00:00
|
|
|
/// shutdown is true, simply finish the thread.
|
2019-01-14 10:59:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!need_shutdown)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-08-01 20:09:38 +00:00
|
|
|
CurrentMetrics::Increment metric_active_threads(
|
|
|
|
std::is_same_v<Thread, std::thread> ? CurrentMetrics::GlobalThreadActive : CurrentMetrics::LocalThreadActive);
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
job();
|
2020-10-27 11:04:03 +00:00
|
|
|
/// job should be reset before decrementing scheduled_jobs to
|
2020-09-22 09:14:37 +00:00
|
|
|
/// ensure that the Job destroyed before wait() returns.
|
2020-09-21 17:49:15 +00:00
|
|
|
job = {};
|
2019-01-14 10:59:58 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-10-27 11:04:03 +00:00
|
|
|
/// job should be reset before decrementing scheduled_jobs to
|
2020-09-22 09:14:37 +00:00
|
|
|
/// ensure that the Job destroyed before wait() returns.
|
|
|
|
job = {};
|
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
2019-01-14 10:59:58 +00:00
|
|
|
if (!first_exception)
|
2020-03-18 02:02:24 +00:00
|
|
|
first_exception = std::current_exception(); // NOLINT
|
Do not shutdown global thread pool on exception
Otherwise GlobalThreadPool can be terminated (for example due to an
exception from the ParallelInputsHandler::onFinish/onFinishThread, from
ParallelAggregatingBlockInputStream::Handler::onFinish/onFinishThread,
since writeToTemporaryFile() can definitelly throw) and the server will
not accept new connections (or/and execute queries) anymore.
Here is possible stacktrace (it is a bit inaccurate, due to
optimizations I guess, and it had been obtained with the
DB::tryLogCurrentException() in the catch block of the
ThreadPoolImpl::worker()):
2020.02.16 22:30:40.415246 [ 45909 ] {} <Error> ThreadPool: Unhandled exception in the ThreadPool(10000,1000,10000) the loop will be shutted down: Code: 241, e.displayText() = DB::Exception: Memory limit (total) exceeded: would use 279.40 GiB (attempt to allocate chunk of 4205536 bytes), maximum: 279.40 GiB, Stack trace (when copying this message, always include the lines below):
1. Common/Exception.cpp:35: DB::Exception::Exception(...)
...
6. Common/Allocator.h:102: void DB::PODArrayBase<8ul, 4096ul, Allocator<false, false>, 15ul, 16ul>::reserve<>(unsigned long) (.part.0)
7. Interpreters/Aggregator.cpp:1040: void DB::Aggregator::writeToTemporaryFileImpl<...>(...)
8. Interpreters/Aggregator.cpp:719: DB::Aggregator::writeToTemporaryFile(...)
9. include/memory:4206: DB::Aggregator::writeToTemporaryFile(...)
10. DataStreams/ParallelInputsProcessor.h:223: DB::ParallelInputsProcessor<DB::ParallelAggregatingBlockInputStream::Handler>::thread(...)
Refs: https://github.com/ClickHouse/ClickHouse/issues/6833#issuecomment-579221732
(Reference to particular comment, since I'm not sure about the initial issue)
2020-02-17 20:15:29 +00:00
|
|
|
if (shutdown_on_exception)
|
|
|
|
shutdown = true;
|
2019-01-14 19:22:09 +00:00
|
|
|
--scheduled_jobs;
|
2019-01-14 10:59:58 +00:00
|
|
|
}
|
Do not shutdown global thread pool on exception
Otherwise GlobalThreadPool can be terminated (for example due to an
exception from the ParallelInputsHandler::onFinish/onFinishThread, from
ParallelAggregatingBlockInputStream::Handler::onFinish/onFinishThread,
since writeToTemporaryFile() can definitelly throw) and the server will
not accept new connections (or/and execute queries) anymore.
Here is possible stacktrace (it is a bit inaccurate, due to
optimizations I guess, and it had been obtained with the
DB::tryLogCurrentException() in the catch block of the
ThreadPoolImpl::worker()):
2020.02.16 22:30:40.415246 [ 45909 ] {} <Error> ThreadPool: Unhandled exception in the ThreadPool(10000,1000,10000) the loop will be shutted down: Code: 241, e.displayText() = DB::Exception: Memory limit (total) exceeded: would use 279.40 GiB (attempt to allocate chunk of 4205536 bytes), maximum: 279.40 GiB, Stack trace (when copying this message, always include the lines below):
1. Common/Exception.cpp:35: DB::Exception::Exception(...)
...
6. Common/Allocator.h:102: void DB::PODArrayBase<8ul, 4096ul, Allocator<false, false>, 15ul, 16ul>::reserve<>(unsigned long) (.part.0)
7. Interpreters/Aggregator.cpp:1040: void DB::Aggregator::writeToTemporaryFileImpl<...>(...)
8. Interpreters/Aggregator.cpp:719: DB::Aggregator::writeToTemporaryFile(...)
9. include/memory:4206: DB::Aggregator::writeToTemporaryFile(...)
10. DataStreams/ParallelInputsProcessor.h:223: DB::ParallelInputsProcessor<DB::ParallelAggregatingBlockInputStream::Handler>::thread(...)
Refs: https://github.com/ClickHouse/ClickHouse/issues/6833#issuecomment-579221732
(Reference to particular comment, since I'm not sure about the initial issue)
2020-02-17 20:15:29 +00:00
|
|
|
|
2019-01-14 10:59:58 +00:00
|
|
|
job_finished.notify_all();
|
|
|
|
new_job_or_shutdown.notify_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-01-14 19:22:09 +00:00
|
|
|
std::unique_lock lock(mutex);
|
|
|
|
--scheduled_jobs;
|
|
|
|
|
|
|
|
if (threads.size() > scheduled_jobs + max_free_threads)
|
|
|
|
{
|
2019-02-25 15:45:07 +00:00
|
|
|
thread_it->detach();
|
2019-01-14 19:22:09 +00:00
|
|
|
threads.erase(thread_it);
|
|
|
|
job_finished.notify_all();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 10:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
job_finished.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template class ThreadPoolImpl<std::thread>;
|
|
|
|
template class ThreadPoolImpl<ThreadFromGlobalPool>;
|
|
|
|
|
2020-06-22 19:04:12 +00:00
|
|
|
std::unique_ptr<GlobalThreadPool> GlobalThreadPool::the_instance;
|
2019-01-14 10:59:58 +00:00
|
|
|
|
2020-06-22 19:04:12 +00:00
|
|
|
void GlobalThreadPool::initialize(size_t max_threads)
|
2019-08-22 03:24:05 +00:00
|
|
|
{
|
2020-09-15 10:29:47 +00:00
|
|
|
if (the_instance)
|
|
|
|
{
|
2020-09-16 08:59:58 +00:00
|
|
|
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR,
|
2020-09-15 10:29:47 +00:00
|
|
|
"The global thread pool is initialized twice");
|
|
|
|
}
|
2020-06-14 06:43:01 +00:00
|
|
|
|
2020-06-22 19:04:12 +00:00
|
|
|
the_instance.reset(new GlobalThreadPool(max_threads,
|
|
|
|
1000 /*max_free_threads*/, 10000 /*max_queue_size*/,
|
|
|
|
false /*shutdown_on_exception*/));
|
|
|
|
}
|
2020-06-14 06:43:01 +00:00
|
|
|
|
2020-06-22 19:04:12 +00:00
|
|
|
GlobalThreadPool & GlobalThreadPool::instance()
|
|
|
|
{
|
|
|
|
if (!the_instance)
|
|
|
|
{
|
2020-06-23 17:31:46 +00:00
|
|
|
// Allow implicit initialization. This is needed for old code that is
|
|
|
|
// impractical to redo now, especially Arcadia users and unit tests.
|
|
|
|
initialize();
|
2020-06-22 19:04:12 +00:00
|
|
|
}
|
2020-06-14 06:43:01 +00:00
|
|
|
|
2020-06-22 19:04:12 +00:00
|
|
|
return *the_instance;
|
2019-08-22 03:24:05 +00:00
|
|
|
}
|