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-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-06-01 19:39:32 +00:00
|
|
|
/// Poco's thread number (the same number is used in logs)
|
|
|
|
UInt32 thread_number = 0;
|
2018-02-01 17:55:08 +00:00
|
|
|
ProfileEvents::Counters performance_counters;
|
|
|
|
MemoryTracker memory_tracker;
|
2018-05-31 15:54:08 +00:00
|
|
|
Int32 os_thread_id = -1;
|
|
|
|
|
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-01 19:39:32 +00:00
|
|
|
Context * getGlobalContext()
|
2018-05-31 15:54:08 +00:00
|
|
|
{
|
2018-06-01 19:39:32 +00:00
|
|
|
return global_context.load(std::memory_order_relaxed);
|
2018-05-31 15:54:08 +00:00
|
|
|
}
|
|
|
|
|
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-05-29 18:14:31 +00:00
|
|
|
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-06-01 19:39:32 +00:00
|
|
|
std::atomic<int> thread_state{ThreadState::DetachedFromQuery};
|
2018-05-31 15:54:08 +00:00
|
|
|
|
2018-06-01 19:39:32 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-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
|
|
|
};
|
|
|
|
|
|
|
|
}
|