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-31 15:54:08 +00:00
|
|
|
class QueryThreadLog;
|
2018-06-04 14:16:27 +00:00
|
|
|
struct TasksStatsCounters;
|
|
|
|
struct RusageCounters;
|
|
|
|
class TaskStatsInfoGetter;
|
2018-06-06 20:57:07 +00:00
|
|
|
class SystemLogsQueue;
|
|
|
|
using SystemLogsQueuePtr = std::shared_ptr<SystemLogsQueue>;
|
|
|
|
using SystemLogsQueueWeakPtr = std::weak_ptr<SystemLogsQueue>;
|
2018-02-01 17:55:08 +00:00
|
|
|
|
|
|
|
|
2018-06-06 20:57:07 +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-06-01 19:39:32 +00:00
|
|
|
/// Poco's thread number (the same number is used in logs)
|
|
|
|
UInt32 thread_number = 0;
|
2018-06-04 14:16:27 +00:00
|
|
|
/// Linux's PID (or TGID) (the same id is shown by ps util)
|
|
|
|
Int32 os_thread_id = -1;
|
|
|
|
|
2018-06-09 15:29:08 +00:00
|
|
|
/// TODO: merge them into common entity
|
|
|
|
ProfileEvents::Counters performance_counters{VariableContext::Thread};
|
|
|
|
MemoryTracker memory_tracker{VariableContext::Thread};
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-06-01 19:39:32 +00:00
|
|
|
/// Statistics of read and write rows/bytes
|
2018-05-31 15:54:08 +00:00
|
|
|
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-06-01 19:39:32 +00:00
|
|
|
void clean();
|
2018-02-01 17:55:08 +00:00
|
|
|
|
2018-06-01 19:39:32 +00:00
|
|
|
enum ThreadState
|
2018-05-31 15:54:08 +00:00
|
|
|
{
|
2018-06-01 19:39:32 +00:00
|
|
|
DetachedFromQuery = 0, /// We just created thread or it is background thread
|
|
|
|
QueryInitializing, /// We accepted a connection, but haven't enqueued a query to ProcessList
|
|
|
|
AttachedToQuery, /// Thread executes enqueued query
|
|
|
|
Died, /// Thread does not exist
|
|
|
|
};
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-06-01 19:39:32 +00:00
|
|
|
int getCurrentState() const
|
2018-05-31 15:54:08 +00:00
|
|
|
{
|
2018-06-01 19:39:32 +00:00
|
|
|
return thread_state.load(std::memory_order_relaxed);
|
2018-05-31 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 20:57:07 +00:00
|
|
|
SystemLogsQueuePtr getSystemLogsQueue() const
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
return thread_state == Died ? nullptr : logs_queue_ptr.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void attachSystemLogsQueue(const SystemLogsQueuePtr & logs_queue)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
logs_queue_ptr = logs_queue;
|
|
|
|
}
|
|
|
|
|
2018-06-09 15:29:08 +00:00
|
|
|
Context * getGlobalContext()
|
|
|
|
{
|
|
|
|
return global_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-06-01 19:39:32 +00:00
|
|
|
void initializeQuery();
|
|
|
|
|
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-06-01 19:39:32 +00:00
|
|
|
MemoryTracker * parent_memory_tracker,
|
2018-06-06 20:57:07 +00:00
|
|
|
const SystemLogsQueueWeakPtr & logs_queue_ptr_,
|
2018-05-29 18:14:31 +00:00
|
|
|
bool check_detached = true);
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-06-13 19:01:07 +00:00
|
|
|
void detachQuery(bool exit_if_already_detached = false, bool thread_exits = false);
|
2018-05-31 15:54:08 +00:00
|
|
|
|
|
|
|
void logToQueryThreadLog(QueryThreadLog & thread_log);
|
|
|
|
|
2018-06-15 13:45:19 +00:00
|
|
|
void updatePerformanceCountersImpl();
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-06-01 19:39:32 +00:00
|
|
|
std::atomic<int> thread_state{ThreadState::DetachedFromQuery};
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-06-06 20:57:07 +00:00
|
|
|
mutable std::mutex mutex;
|
2018-06-01 19:39:32 +00:00
|
|
|
QueryStatus * parent_query = nullptr;
|
2018-05-31 15:54:08 +00:00
|
|
|
|
|
|
|
/// Is set once
|
|
|
|
std::atomic<Context *> global_context{nullptr};
|
2018-06-01 19:39:32 +00:00
|
|
|
/// Use it only from current thread
|
|
|
|
Context * query_context = nullptr;
|
|
|
|
|
2018-06-06 20:57:07 +00:00
|
|
|
/// A logs queue used by TCPHandler to pass logs to a client
|
|
|
|
SystemLogsQueueWeakPtr logs_queue_ptr;
|
|
|
|
|
2018-06-01 19:39:32 +00:00
|
|
|
UInt64 query_start_time_nanoseconds = 0;
|
|
|
|
time_t query_start_time = 0;
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-06-01 11:58:17 +00:00
|
|
|
bool log_to_query_thread_log = true;
|
|
|
|
bool log_profile_events = true;
|
2018-06-04 14:16:27 +00:00
|
|
|
size_t queries_started = 0;
|
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-06-06 20:57:07 +00:00
|
|
|
/// Use ptr not to add extra dependencies in the header
|
2018-06-04 14:16:27 +00:00
|
|
|
std::unique_ptr<RusageCounters> last_rusage;
|
|
|
|
std::unique_ptr<TasksStatsCounters> last_taskstats;
|
|
|
|
std::unique_ptr<TaskStatsInfoGetter> taskstats_getter;
|
2018-06-14 14:29:42 +00:00
|
|
|
bool has_permissions_for_taskstats = false;
|
2018-05-31 15:54:08 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
class CurrentThreadScope;
|
2018-02-01 17:55:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|