2018-05-29 18:14:31 +00:00
|
|
|
#pragma once
|
2018-09-05 21:01:43 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
|
|
|
#include <Common/ThreadStatus.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/StringRef.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
#include <memory>
|
2018-06-15 13:45:19 +00:00
|
|
|
#include <string>
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-09-06 00:28:15 +00:00
|
|
|
|
2018-05-29 18:14:31 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2018-05-31 15:54:08 +00:00
|
|
|
class Counters;
|
2018-05-29 18:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MemoryTracker;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class QueryStatus;
|
2018-05-31 15:54:08 +00:00
|
|
|
struct Progress;
|
2018-06-15 17:32:35 +00:00
|
|
|
class InternalTextLogsQueue;
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-09-06 00:28:15 +00:00
|
|
|
|
|
|
|
/** Collection of static methods to work with thread-local objects.
|
|
|
|
* Allows to attach and detach query/process (thread group) to a thread
|
|
|
|
* (to calculate query-related metrics and to allow to obtain query-related data from a thread).
|
|
|
|
* Thread will propagate it's metrics to attached query.
|
|
|
|
*/
|
2018-05-29 18:14:31 +00:00
|
|
|
class CurrentThread
|
|
|
|
{
|
|
|
|
public:
|
2020-08-08 00:47:03 +00:00
|
|
|
/// Return true in case of successful initialization
|
2019-08-12 15:16:23 +00:00
|
|
|
static bool isInitialized();
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
/// Handler to current thread
|
2019-01-13 18:51:57 +00:00
|
|
|
static ThreadStatus & get();
|
2018-09-05 21:01:43 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
/// Group to which belongs current thread
|
2018-06-19 20:30:35 +00:00
|
|
|
static ThreadGroupStatusPtr getGroup();
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-06-06 20:57:07 +00:00
|
|
|
/// A logs queue used by TCPHandler to pass logs to a client
|
2019-07-09 10:39:05 +00:00
|
|
|
static void attachInternalTextLogsQueue(const std::shared_ptr<InternalTextLogsQueue> & logs_queue,
|
|
|
|
LogsLevel client_logs_level);
|
2018-06-15 17:32:35 +00:00
|
|
|
static std::shared_ptr<InternalTextLogsQueue> getInternalTextLogsQueue();
|
2021-09-02 14:27:19 +00:00
|
|
|
|
|
|
|
static void attachInternalProfileEventsQueue(const InternalProfileEventsQueuePtr & queue);
|
|
|
|
static InternalProfileEventsQueuePtr getInternalProfileEventsQueue();
|
2018-06-06 20:57:07 +00:00
|
|
|
|
2020-06-20 11:17:15 +00:00
|
|
|
static void setFatalErrorCallback(std::function<void()> callback);
|
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
/// Makes system calls to update ProfileEvents that contain info from rusage and taskstats
|
|
|
|
static void updatePerformanceCounters();
|
|
|
|
|
|
|
|
static ProfileEvents::Counters & getProfileEvents();
|
2019-03-14 18:03:35 +00:00
|
|
|
static MemoryTracker * getMemoryTracker();
|
2019-08-24 22:06:13 +00:00
|
|
|
|
2018-06-20 17:49:52 +00:00
|
|
|
/// Update read and write rows (bytes) statistics (used in system.query_thread_log)
|
|
|
|
static void updateProgressIn(const Progress & value);
|
|
|
|
static void updateProgressOut(const Progress & value);
|
|
|
|
|
|
|
|
/// Query management:
|
|
|
|
|
|
|
|
/// Call from master thread as soon as possible (e.g. when thread accepted connection)
|
|
|
|
static void initializeQuery();
|
|
|
|
|
|
|
|
/// You must call one of these methods when create a query child thread:
|
2018-08-17 18:57:07 +00:00
|
|
|
/// Add current thread to a group associated with the thread group
|
2018-06-19 20:30:35 +00:00
|
|
|
static void attachTo(const ThreadGroupStatusPtr & thread_group);
|
2018-05-29 18:14:31 +00:00
|
|
|
/// Is useful for a ThreadPool tasks
|
2018-06-19 20:30:35 +00:00
|
|
|
static void attachToIfDetached(const ThreadGroupStatusPtr & thread_group);
|
2018-05-29 18:14:31 +00:00
|
|
|
|
2018-06-19 20:30:35 +00:00
|
|
|
/// Update ProfileEvents and dumps info to system.query_thread_log
|
|
|
|
static void finalizePerformanceCounters();
|
2018-06-01 19:39:32 +00:00
|
|
|
|
2018-06-15 13:45:19 +00:00
|
|
|
/// Returns a non-empty string if the thread is attached to a query
|
2019-07-10 20:47:39 +00:00
|
|
|
static StringRef getQueryId()
|
|
|
|
{
|
|
|
|
if (unlikely(!current_thread))
|
|
|
|
return {};
|
|
|
|
return current_thread->getQueryId();
|
|
|
|
}
|
2018-06-15 13:45:19 +00:00
|
|
|
|
2018-05-31 15:54:08 +00:00
|
|
|
/// Non-master threads call this method in destructor automatically
|
2018-05-29 18:14:31 +00:00
|
|
|
static void detachQuery();
|
2018-06-13 19:01:07 +00:00
|
|
|
static void detachQueryIfNotDetached();
|
2018-06-20 15:21:42 +00:00
|
|
|
|
2018-09-26 21:10:43 +00:00
|
|
|
/// Initializes query with current thread as master thread in constructor, and detaches it in destructor
|
2018-06-20 15:21:42 +00:00
|
|
|
struct QueryScope
|
|
|
|
{
|
2021-05-31 14:49:02 +00:00
|
|
|
explicit QueryScope(ContextMutablePtr query_context);
|
2018-06-20 15:21:42 +00:00
|
|
|
~QueryScope();
|
2018-09-26 21:19:49 +00:00
|
|
|
|
|
|
|
void logPeakMemoryUsage();
|
|
|
|
bool log_peak_memory_usage_in_destructor = true;
|
2018-06-20 15:21:42 +00:00
|
|
|
};
|
2018-09-05 21:01:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void defaultThreadDeleter();
|
2021-01-28 13:57:36 +00:00
|
|
|
|
|
|
|
/// Sets query_context for current thread group
|
|
|
|
/// Can by used only through QueryScope
|
2021-04-10 23:33:54 +00:00
|
|
|
static void attachQueryContext(ContextPtr query_context);
|
2018-05-29 18:14:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|