2016-10-24 04:06:27 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
|
|
|
#include <Common/MemoryStatisticsOS.h>
|
2021-05-22 09:57:51 +00:00
|
|
|
#include <Common/MemoryInfoOS.h>
|
|
|
|
#include <Common/ProcessorStatisticsOS.h>
|
|
|
|
#include <Common/DiskStatisticsOS.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2021-07-04 03:03:49 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
#include <condition_variable>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <mutex>
|
2016-10-24 04:06:27 +00:00
|
|
|
#include <string>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <thread>
|
2021-07-04 03:03:49 +00:00
|
|
|
#include <optional>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <unordered_map>
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2021-07-04 03:03:49 +00:00
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-12-17 13:47:03 +00:00
|
|
|
class ProtocolServerAdapter;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
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
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-06-26 00:16:58 +00:00
|
|
|
/** Periodically (by default, each minute, starting at 30 seconds offset)
|
2016-10-24 04:06:27 +00:00
|
|
|
* calculates and updates some metrics,
|
|
|
|
* that are not updated automatically (so, need to be asynchronously calculated).
|
|
|
|
*/
|
2021-04-10 23:33:54 +00:00
|
|
|
class AsynchronousMetrics : WithContext
|
2016-10-24 04:06:27 +00:00
|
|
|
{
|
|
|
|
public:
|
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(
|
2021-04-10 23:33:54 +00:00
|
|
|
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_,
|
2021-07-04 03:03:49 +00:00
|
|
|
std::shared_ptr<std::vector<ProtocolServerAdapter>> servers_);
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~AsynchronousMetrics();
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-12-17 13:47:03 +00:00
|
|
|
/// Separate method allows to initialize the `servers` variable beforehand.
|
2021-07-04 03:03:49 +00:00
|
|
|
void start();
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Returns copy of all values.
|
2020-06-10 19:17:30 +00:00
|
|
|
AsynchronousMetricValues getValues() const;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2021-07-04 03:03:49 +00:00
|
|
|
#if defined(ARCADIA_BUILD)
|
|
|
|
/// This constructor needs only to provide backward compatibility with some other projects (hello, Arcadia).
|
|
|
|
/// Never use this in the ClickHouse codebase.
|
|
|
|
AsynchronousMetrics(
|
|
|
|
ContextPtr global_context_,
|
|
|
|
int update_period_seconds = 60)
|
|
|
|
: WithContext(global_context_)
|
|
|
|
, update_period(update_period_seconds)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
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};
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
mutable std::mutex mutex;
|
2017-04-01 07:20:54 +00:00
|
|
|
std::condition_variable wait_cond;
|
2020-06-10 19:17:30 +00:00
|
|
|
bool quit {false};
|
|
|
|
AsynchronousMetricValues values;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2020-04-21 18:49:19 +00:00
|
|
|
#if defined(OS_LINUX)
|
2020-04-19 20:49:13 +00:00
|
|
|
MemoryStatisticsOS memory_stat;
|
2021-07-04 03:03:49 +00:00
|
|
|
|
|
|
|
std::optional<ReadBufferFromFile> meminfo;
|
|
|
|
std::optional<ReadBufferFromFile> mounts;
|
|
|
|
std::optional<ReadBufferFromFile> loadavg;
|
|
|
|
std::optional<ReadBufferFromFile> proc_stat;
|
|
|
|
std::optional<ReadBufferFromFile> cpuinfo;
|
|
|
|
std::optional<ReadBufferFromFile> schedstat;
|
|
|
|
std::optional<ReadBufferFromFile> sockstat;
|
|
|
|
std::optional<ReadBufferFromFile> netstat;
|
|
|
|
std::optional<ReadBufferFromFile> file_nr;
|
2020-04-21 18:49:19 +00:00
|
|
|
#endif
|
|
|
|
|
2020-12-17 13:47:03 +00:00
|
|
|
std::unique_ptr<ThreadFromGlobalPool> thread;
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void run();
|
|
|
|
void update();
|
2016-10-24 04:06:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|