2018-02-01 17:55:08 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/MemoryTracker.h>
|
2018-05-31 15:54:08 +00:00
|
|
|
#include <IO/Progress.h>
|
2018-02-01 17:55:08 +00:00
|
|
|
#include <memory>
|
2018-03-01 16:52:24 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2018-02-01 17:55:08 +00:00
|
|
|
|
|
|
|
namespace Poco
|
|
|
|
{
|
|
|
|
class Logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
class Context;
|
2018-05-17 16:01:41 +00:00
|
|
|
class QueryStatus;
|
|
|
|
class ThreadStatus;
|
2018-05-29 18:14:31 +00:00
|
|
|
class ScopeCurrentThread;
|
2018-05-31 15:54:08 +00:00
|
|
|
class QueryThreadLog;
|
2018-02-01 17:55:08 +00:00
|
|
|
using ThreadStatusPtr = std::shared_ptr<ThreadStatus>;
|
|
|
|
|
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
extern thread_local ThreadStatusPtr current_thread;
|
|
|
|
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
class ThreadStatus : public std::enable_shared_from_this<ThreadStatus>
|
2018-02-01 17:55:08 +00:00
|
|
|
{
|
2018-05-17 16:01:41 +00:00
|
|
|
public:
|
|
|
|
|
2018-02-01 17:55:08 +00:00
|
|
|
UInt32 poco_thread_number = 0;
|
|
|
|
ProfileEvents::Counters performance_counters;
|
|
|
|
MemoryTracker memory_tracker;
|
2018-05-31 15:54:08 +00:00
|
|
|
Int32 os_thread_id = -1;
|
|
|
|
|
|
|
|
Progress progress_in;
|
|
|
|
Progress progress_out;
|
2018-02-01 17:55:08 +00:00
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
static ThreadStatusPtr create();
|
2018-02-01 17:55:08 +00:00
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
/// Called by master thread when the query finishes
|
2018-02-01 17:55:08 +00:00
|
|
|
void reset();
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
QueryStatus * getParentQuery()
|
|
|
|
{
|
|
|
|
return parent_query.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
Context * getGlobalContext()
|
|
|
|
{
|
|
|
|
return global_context.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
Context * getQueryContext()
|
|
|
|
{
|
|
|
|
return query_context.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:55:08 +00:00
|
|
|
~ThreadStatus();
|
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
protected:
|
2018-03-01 16:52:24 +00:00
|
|
|
|
2018-02-01 17:55:08 +00:00
|
|
|
ThreadStatus();
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
void attachQuery(
|
2018-05-31 15:54:08 +00:00
|
|
|
QueryStatus * parent_query_,
|
|
|
|
ProfileEvents::Counters * parent_counters,
|
2018-05-29 18:14:31 +00:00
|
|
|
MemoryTracker *parent_memory_tracker,
|
|
|
|
bool check_detached = true);
|
2018-05-31 15:54:08 +00:00
|
|
|
|
|
|
|
void detachQuery(bool thread_exits = false);
|
|
|
|
|
|
|
|
void logToQueryThreadLog(QueryThreadLog & thread_log);
|
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
void updatePerfomanceCountersImpl();
|
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
std::atomic<bool> is_active_query{false};
|
|
|
|
bool is_active_thread{false};
|
|
|
|
bool is_first_query_of_the_thread{true};
|
|
|
|
|
|
|
|
UInt64 query_start_time_nanoseconds{0};
|
|
|
|
time_t query_start_time{0};
|
|
|
|
|
|
|
|
std::atomic<QueryStatus *> parent_query{nullptr};
|
|
|
|
/// Use it only from current thread
|
|
|
|
std::atomic<Context *> query_context{nullptr};
|
|
|
|
/// Is set once
|
|
|
|
std::atomic<Context *> global_context{nullptr};
|
|
|
|
|
2018-06-01 11:58:17 +00:00
|
|
|
bool log_to_query_thread_log = true;
|
|
|
|
bool log_profile_events = true;
|
2018-05-31 15:54:08 +00:00
|
|
|
|
|
|
|
Poco::Logger * log = nullptr;
|
2018-02-01 17:55:08 +00:00
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
friend class CurrentThread;
|
|
|
|
friend struct TasksStatsCounters;
|
|
|
|
|
2018-03-01 16:52:24 +00:00
|
|
|
struct Impl;
|
2018-05-29 18:14:31 +00:00
|
|
|
std::unique_ptr<Impl> impl;
|
2018-05-31 15:54:08 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
class CurrentThreadScope;
|
2018-02-01 17:55:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|