2018-09-05 21:01:43 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
#include "CurrentThread.h"
|
|
|
|
#include <common/logger_useful.h>
|
2018-05-31 15:54:08 +00:00
|
|
|
#include <Common/ThreadStatus.h>
|
2018-05-29 18:14:31 +00:00
|
|
|
#include <Interpreters/ProcessList.h>
|
2018-06-15 17:32:35 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2018-05-31 15:54:08 +00:00
|
|
|
#include <Poco/Ext/ThreadNumber.h>
|
|
|
|
#include <Poco/Logger.h>
|
2018-05-29 18:14:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-09-05 21:01:43 +00:00
|
|
|
/// Order of current_thread and current_thread_scope matters
|
|
|
|
thread_local ThreadStatusPtr current_thread = ThreadStatus::create();
|
|
|
|
thread_local CurrentThread::ThreadScopePtr current_thread_scope = std::make_shared<CurrentThread::ThreadScope>();
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
void CurrentThread::updatePerformanceCounters()
|
|
|
|
{
|
|
|
|
get()->updatePerformanceCounters();
|
|
|
|
}
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
ThreadStatusPtr CurrentThread::get()
|
2018-05-29 18:14:31 +00:00
|
|
|
{
|
2018-06-14 14:29:42 +00:00
|
|
|
#ifndef NDEBUG
|
2018-06-06 20:57:07 +00:00
|
|
|
if (!current_thread || current_thread.use_count() <= 0)
|
2018-05-29 18:14:31 +00:00
|
|
|
throw Exception("Thread #" + std::to_string(Poco::ThreadNumber::get()) + " status was not initialized", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2018-06-06 20:57:07 +00:00
|
|
|
if (Poco::ThreadNumber::get() != current_thread->thread_number)
|
2018-05-29 18:14:31 +00:00
|
|
|
throw Exception("Current thread has different thread number", ErrorCodes::LOGICAL_ERROR);
|
2018-06-14 14:29:42 +00:00
|
|
|
#endif
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-06-06 20:57:07 +00:00
|
|
|
return current_thread;
|
2018-05-29 18:14:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 21:01:43 +00:00
|
|
|
CurrentThread::ThreadScopePtr CurrentThread::getScope()
|
|
|
|
{
|
|
|
|
return current_thread_scope;
|
|
|
|
}
|
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
ProfileEvents::Counters & CurrentThread::getProfileEvents()
|
|
|
|
{
|
|
|
|
return current_thread->performance_counters;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryTracker & CurrentThread::getMemoryTracker()
|
|
|
|
{
|
|
|
|
return current_thread->memory_tracker;
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
void CurrentThread::updateProgressIn(const Progress & value)
|
|
|
|
{
|
|
|
|
current_thread->progress_in.incrementPiecewiseAtomically(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentThread::updateProgressOut(const Progress & value)
|
|
|
|
{
|
|
|
|
current_thread->progress_out.incrementPiecewiseAtomically(value);
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:30:35 +00:00
|
|
|
void CurrentThread::attachInternalTextLogsQueue(const std::shared_ptr<InternalTextLogsQueue> & logs_queue)
|
2018-06-06 20:57:07 +00:00
|
|
|
{
|
2018-06-20 17:49:52 +00:00
|
|
|
get()->attachInternalTextLogsQueue(logs_queue);
|
2018-06-06 20:57:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 17:32:35 +00:00
|
|
|
std::shared_ptr<InternalTextLogsQueue> CurrentThread::getInternalTextLogsQueue()
|
2018-06-06 20:57:07 +00:00
|
|
|
{
|
|
|
|
/// NOTE: this method could be called at early server startup stage
|
2018-06-14 14:29:42 +00:00
|
|
|
/// NOTE: this method could be called in ThreadStatus destructor, therefore we make use_count() check just in case
|
2018-06-06 20:57:07 +00:00
|
|
|
|
|
|
|
if (!current_thread || current_thread.use_count() <= 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (current_thread->getCurrentState() == ThreadStatus::ThreadState::Died)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-06-15 17:32:35 +00:00
|
|
|
return current_thread->getInternalTextLogsQueue();
|
2018-05-29 18:14:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:21:42 +00:00
|
|
|
ThreadGroupStatusPtr CurrentThread::getGroup()
|
|
|
|
{
|
2018-06-20 17:49:52 +00:00
|
|
|
return get()->getThreadGroup();
|
2018-06-20 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
}
|