ClickHouse/src/Interpreters/AsynchronousMetrics.h

92 lines
2.6 KiB
C++
Raw Normal View History

#pragma once
#include <Interpreters/Context_fwd.h>
#include <Common/MemoryStatisticsOS.h>
#include <Common/ThreadPool.h>
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
namespace DB
{
2020-12-17 13:47:03 +00:00
class ProtocolServerAdapter;
2020-12-17 13:47:03 +00:00
using AsynchronousMetricValue = double;
using AsynchronousMetricValues = std::unordered_map<std::string, AsynchronousMetricValue>;
2020-06-10 19:17:30 +00:00
2020-06-26 00:16:58 +00:00
/** Periodically (by default, each minute, starting at 30 seconds offset)
* calculates and updates some metrics,
* that are not updated automatically (so, need to be asynchronously calculated).
*/
class AsynchronousMetrics : WithContext
{
public:
2020-12-22 06:24:14 +00:00
#if defined(ARCADIA_BUILD)
/// This constructor needs only to provide backward compatibility with some other projects (hello, Arcadia).
2020-12-21 23:03:08 +00:00
/// Never use this in the ClickHouse codebase.
2020-12-21 21:47:10 +00:00
AsynchronousMetrics(
ContextPtr global_context_,
2020-12-21 21:47:10 +00:00
int update_period_seconds = 60)
: WithContext(global_context_)
2020-12-21 21:47:10 +00:00
, update_period(update_period_seconds)
{
}
2020-12-22 06:24:14 +00:00
#endif
2020-12-21 23:03:08 +00:00
/// The default value of update_period_seconds is for ClickHouse-over-YT
/// in Arcadia -- it uses its own server implementation that also uses these
/// metrics.
2020-12-17 13:47:03 +00:00
AsynchronousMetrics(
ContextPtr global_context_,
2020-12-17 13:47:03 +00:00
int update_period_seconds,
2020-12-21 21:47:10 +00:00
std::shared_ptr<std::vector<ProtocolServerAdapter>> servers_to_start_before_tables_,
std::shared_ptr<std::vector<ProtocolServerAdapter>> servers_)
: WithContext(global_context_)
2020-12-17 13:47:03 +00:00
, update_period(update_period_seconds)
, servers_to_start_before_tables(servers_to_start_before_tables_)
, servers(servers_)
{
}
~AsynchronousMetrics();
2020-12-17 13:47:03 +00:00
/// Separate method allows to initialize the `servers` variable beforehand.
void start()
{
/// Update once right now, to make metrics available just after server start
/// (without waiting for asynchronous_metrics_update_period_s).
update();
2020-12-17 13:47:03 +00:00
thread = std::make_unique<ThreadFromGlobalPool>([this] { run(); });
}
/// Returns copy of all values.
2020-06-10 19:17:30 +00:00
AsynchronousMetricValues getValues() const;
private:
2020-06-26 00:16:58 +00:00
const std::chrono::seconds update_period;
2020-12-21 21:47:10 +00:00
std::shared_ptr<std::vector<ProtocolServerAdapter>> servers_to_start_before_tables{nullptr};
std::shared_ptr<std::vector<ProtocolServerAdapter>> servers{nullptr};
2020-06-10 19:17:30 +00:00
mutable std::mutex mutex;
std::condition_variable wait_cond;
2020-06-10 19:17:30 +00:00
bool quit {false};
AsynchronousMetricValues values;
2020-04-21 18:49:19 +00:00
#if defined(OS_LINUX)
2020-04-19 20:49:13 +00:00
MemoryStatisticsOS memory_stat;
2020-04-21 18:49:19 +00:00
#endif
2020-12-17 13:47:03 +00:00
std::unique_ptr<ThreadFromGlobalPool> thread;
void run();
void update();
};
}