ClickHouse/dbms/src/Common/CurrentThread.h

113 lines
3.2 KiB
C++
Raw Normal View History

2018-05-29 18:14:31 +00:00
#pragma once
2018-05-29 18:14:31 +00:00
#include <memory>
#include <string>
2018-05-29 18:14:31 +00:00
#include <Common/ThreadStatus.h>
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
{
class Counters;
2018-05-29 18:14:31 +00:00
}
class MemoryTracker;
namespace DB
{
class Context;
2018-05-29 18:14:31 +00:00
class QueryStatus;
struct Progress;
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:
/// Handler to current thread
2018-05-29 18:14:31 +00:00
static ThreadStatusPtr get();
/// Group to which belongs current thread
static ThreadGroupStatusPtr getGroup();
2018-05-29 18:14:31 +00:00
/// A logs queue used by TCPHandler to pass logs to a client
static void attachInternalTextLogsQueue(const std::shared_ptr<InternalTextLogsQueue> & logs_queue);
static std::shared_ptr<InternalTextLogsQueue> getInternalTextLogsQueue();
/// Makes system calls to update ProfileEvents that contain info from rusage and taskstats
static void updatePerformanceCounters();
static ProfileEvents::Counters & getProfileEvents();
static MemoryTracker & getMemoryTracker();
/// 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();
/// Sets query_context for current thread group
static void attachQueryContext(Context & query_context);
/// 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
static void attachTo(const ThreadGroupStatusPtr & thread_group);
2018-05-29 18:14:31 +00:00
/// Is useful for a ThreadPool tasks
static void attachToIfDetached(const ThreadGroupStatusPtr & thread_group);
2018-05-29 18:14:31 +00:00
/// Update ProfileEvents and dumps info to system.query_thread_log
static void finalizePerformanceCounters();
/// Returns a non-empty string if the thread is attached to a query
static std::string getCurrentQueryID();
/// Non-master threads call this method in destructor automatically
2018-05-29 18:14:31 +00:00
static void detachQuery();
static void detachQueryIfNotDetached();
/// Initializes query with current thread as master thread in constructor, and detaches it in destructor
struct QueryScope
{
explicit QueryScope(Context & query_context);
~QueryScope();
void logPeakMemoryUsage();
bool log_peak_memory_usage_in_destructor = true;
};
/// Implicitly finalizes current thread in the destructor
class ThreadScope
{
public:
void (*deleter)() = nullptr;
ThreadScope() = default;
~ThreadScope()
{
if (deleter)
deleter();
/// std::terminate on exception: this is Ok.
}
};
using ThreadScopePtr = std::shared_ptr<ThreadScope>;
static ThreadScopePtr getScope();
private:
static void defaultThreadDeleter();
2018-05-29 18:14:31 +00:00
};
}