ClickHouse/dbms/src/Common/CurrentThread.cpp

142 lines
3.4 KiB
C++
Raw Normal View History

2018-05-29 18:14:31 +00:00
#include "CurrentThread.h"
#include <common/logger_useful.h>
#include <Common/ThreadStatus.h>
2018-05-29 18:14:31 +00:00
#include <Interpreters/ProcessList.h>
#include <Interpreters/Context.h>
#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;
}
static ThreadStatusPtr getCurrentThreadImpl()
2018-05-29 18:14:31 +00:00
{
#ifndef NDEBUG
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);
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);
#endif
2018-05-29 18:14:31 +00:00
return current_thread;
2018-05-29 18:14:31 +00:00
}
void CurrentThread::initializeQuery()
{
getCurrentThreadImpl()->initializeQuery();
}
void CurrentThread::attachTo(const ThreadGroupStatusPtr & thread_group)
2018-05-29 18:14:31 +00:00
{
getCurrentThreadImpl()->attachQuery(thread_group, true);
2018-05-29 18:14:31 +00:00
}
void CurrentThread::attachToIfDetached(const ThreadGroupStatusPtr & thread_group)
2018-05-29 18:14:31 +00:00
{
getCurrentThreadImpl()->attachQuery(thread_group, false);
2018-05-29 18:14:31 +00:00
}
void CurrentThread::updatePerformanceCounters()
{
getCurrentThreadImpl()->updatePerformanceCounters();
2018-05-29 18:14:31 +00:00
}
ThreadStatusPtr CurrentThread::get()
{
return getCurrentThreadImpl();
2018-05-29 18:14:31 +00:00
}
void CurrentThread::detachQuery()
{
getCurrentThreadImpl()->detachQuery(false);
2018-05-29 18:14:31 +00:00
}
void CurrentThread::detachQueryIfNotDetached()
{
getCurrentThreadImpl()->detachQuery(true);
}
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;
}
void CurrentThread::updateProgressIn(const Progress & value)
{
current_thread->progress_in.incrementPiecewiseAtomically(value);
}
void CurrentThread::updateProgressOut(const Progress & value)
{
current_thread->progress_out.incrementPiecewiseAtomically(value);
}
void CurrentThread::attachInternalTextLogsQueue(const std::shared_ptr<InternalTextLogsQueue> & logs_queue)
{
getCurrentThreadImpl()->attachInternalTextLogsQueue(logs_queue);
}
std::shared_ptr<InternalTextLogsQueue> CurrentThread::getInternalTextLogsQueue()
{
/// NOTE: this method could be called at early server startup stage
/// NOTE: this method could be called in ThreadStatus destructor, therefore we make use_count() check just in case
if (!current_thread || current_thread.use_count() <= 0)
return nullptr;
if (current_thread->getCurrentState() == ThreadStatus::ThreadState::Died)
return nullptr;
return current_thread->getInternalTextLogsQueue();
2018-05-29 18:14:31 +00:00
}
std::string CurrentThread::getCurrentQueryID()
{
if (!current_thread || current_thread.use_count() <= 0)
return {};
return current_thread->getQueryID();
}
void CurrentThread::attachQueryContext(Context & query_context)
{
return getCurrentThreadImpl()->attachQueryContext(query_context);
}
void CurrentThread::finalizePerformanceCounters()
{
getCurrentThreadImpl()->finalizePerformanceCounters();
}
ThreadGroupStatusPtr CurrentThread::getGroup()
{
return getCurrentThreadImpl()->getThreadGroup();
}
CurrentThread::QueryScope::~QueryScope()
{
try
{
CurrentThread::detachQueryIfNotDetached();
}
catch (...)
{
tryLogCurrentException("CurrentThread", __PRETTY_FUNCTION__);
}
}
2018-05-29 18:14:31 +00:00
}