#pragma once #include #include #include #include #include namespace Poco { class Logger; } namespace DB { class Context; class QueryStatus; class ThreadStatus; class QueryThreadLog; using ThreadStatusPtr = std::shared_ptr; extern thread_local ThreadStatusPtr current_thread; class ThreadStatus : public std::enable_shared_from_this { public: /// Poco's thread number (the same number is used in logs) UInt32 thread_number = 0; ProfileEvents::Counters performance_counters; MemoryTracker memory_tracker; Int32 os_thread_id = -1; /// Statistics of read and write rows/bytes Progress progress_in; Progress progress_out; public: static ThreadStatusPtr create(); /// Called by master thread when the query finishes void clean(); enum ThreadState { 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 }; int getCurrentState() const { return thread_state.load(std::memory_order_relaxed); } Context * getGlobalContext() { return global_context.load(std::memory_order_relaxed); } ~ThreadStatus(); protected: ThreadStatus(); void initializeQuery(); void attachQuery( QueryStatus * parent_query_, ProfileEvents::Counters * parent_counters, MemoryTracker * parent_memory_tracker, bool check_detached = true); void detachQuery(bool thread_exits = false); void logToQueryThreadLog(QueryThreadLog & thread_log); void updatePerfomanceCountersImpl(); std::atomic thread_state{ThreadState::DetachedFromQuery}; std::mutex mutex; QueryStatus * parent_query = nullptr; /// Is set once std::atomic global_context{nullptr}; /// Use it only from current thread Context * query_context = nullptr; UInt64 query_start_time_nanoseconds = 0; time_t query_start_time = 0; bool log_to_query_thread_log = true; bool log_profile_events = true; Poco::Logger * log = nullptr; friend class CurrentThread; friend struct TasksStatsCounters; struct Impl; std::unique_ptr impl; public: class CurrentThreadScope; }; }