2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/AsynchronousMetrics.h>
|
2020-06-10 19:17:30 +00:00
|
|
|
#include <Interpreters/AsynchronousMetricLog.h>
|
2018-09-03 10:14:05 +00:00
|
|
|
#include <Interpreters/ExpressionJIT.h>
|
2020-02-10 13:10:17 +00:00
|
|
|
#include <Interpreters/DatabaseCatalog.h>
|
2020-05-20 20:16:32 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/setThreadName.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/MarkCache.h>
|
|
|
|
#include <Storages/StorageMergeTree.h>
|
|
|
|
#include <Storages/StorageReplicatedMergeTree.h>
|
|
|
|
#include <IO/UncompressedCache.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
2016-10-23 06:12:50 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
2020-04-17 04:09:41 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include "config_core.h"
|
2018-06-19 18:09:09 +00:00
|
|
|
#endif
|
2017-01-20 17:58:07 +00:00
|
|
|
|
2018-08-02 00:20:20 +00:00
|
|
|
#if USE_JEMALLOC
|
2020-04-16 12:31:57 +00:00
|
|
|
# include <jemalloc/jemalloc.h>
|
2018-08-02 00:20:20 +00:00
|
|
|
#endif
|
|
|
|
|
2016-10-23 06:12:50 +00:00
|
|
|
|
2020-04-25 12:36:01 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric MemoryTracking;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 06:12:50 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
AsynchronousMetrics::~AsynchronousMetrics()
|
2016-10-23 06:12:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
2020-06-10 19:17:30 +00:00
|
|
|
std::lock_guard lock{mutex};
|
2017-04-01 07:20:54 +00:00
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_cond.notify_one();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2016-10-23 06:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
AsynchronousMetricValues AsynchronousMetrics::getValues() const
|
2016-10-23 06:12:50 +00:00
|
|
|
{
|
2020-06-10 19:17:30 +00:00
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
return values;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AsynchronousMetrics::run()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
setThreadName("AsyncMetrics");
|
|
|
|
|
|
|
|
/// Next minute + 30 seconds. To be distant with moment of transmission of metrics, see MetricsTransmitter.
|
|
|
|
const auto get_next_minute = []
|
|
|
|
{
|
|
|
|
return std::chrono::time_point_cast<std::chrono::minutes, std::chrono::system_clock>(
|
|
|
|
std::chrono::system_clock::now() + std::chrono::minutes(1)) + std::chrono::seconds(30);
|
|
|
|
};
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2019-12-21 17:48:35 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
std::unique_lock lock{mutex};
|
2019-12-21 17:48:35 +00:00
|
|
|
if (wait_cond.wait_until(lock, get_next_minute(), [this] { return quit; }))
|
|
|
|
break;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-10-23 06:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-23 06:38:53 +00:00
|
|
|
template <typename Max, typename T>
|
|
|
|
static void calculateMax(Max & max, T x)
|
2016-10-23 06:12:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (Max(x) > max)
|
|
|
|
max = x;
|
2016-10-23 06:12:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 06:38:53 +00:00
|
|
|
template <typename Max, typename Sum, typename T>
|
|
|
|
static void calculateMaxAndSum(Max & max, Sum & sum, T x)
|
2016-10-23 06:12:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
sum += x;
|
|
|
|
if (Max(x) > max)
|
|
|
|
max = x;
|
2016-10-23 06:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
void AsynchronousMetrics::update()
|
2016-10-23 06:12:50 +00:00
|
|
|
{
|
2020-06-10 19:17:30 +00:00
|
|
|
AsynchronousMetricValues new_values;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (auto mark_cache = context.getMarkCache())
|
|
|
|
{
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["MarkCacheBytes"] = mark_cache->weight();
|
|
|
|
new_values["MarkCacheFiles"] = mark_cache->count();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if (auto uncompressed_cache = context.getUncompressedCache())
|
|
|
|
{
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["UncompressedCacheBytes"] = uncompressed_cache->weight();
|
|
|
|
new_values["UncompressedCacheCells"] = uncompressed_cache->count();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 10:14:05 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
{
|
2018-09-05 11:37:41 +00:00
|
|
|
if (auto compiled_expression_cache = context.getCompiledExpressionCache())
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["CompiledExpressionCacheCount"] = compiled_expression_cache->count();
|
2018-09-03 10:14:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["Uptime"] = context.getUptimeSeconds();
|
2017-09-07 04:02:29 +00:00
|
|
|
|
2020-04-17 04:09:41 +00:00
|
|
|
/// Process memory usage according to OS
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
{
|
2020-04-19 20:49:13 +00:00
|
|
|
MemoryStatisticsOS::Data data = memory_stat.get();
|
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["MemoryVirtual"] = data.virt;
|
|
|
|
new_values["MemoryResident"] = data.resident;
|
|
|
|
new_values["MemoryShared"] = data.shared;
|
|
|
|
new_values["MemoryCode"] = data.code;
|
|
|
|
new_values["MemoryDataAndStack"] = data.data_and_stack;
|
2020-04-19 21:43:06 +00:00
|
|
|
|
|
|
|
/// We must update the value of total_memory_tracker periodically.
|
|
|
|
/// Otherwise it might be calculated incorrectly - it can include a "drift" of memory amount.
|
|
|
|
/// See https://github.com/ClickHouse/ClickHouse/issues/10293
|
|
|
|
total_memory_tracker.set(data.resident);
|
2020-04-25 12:36:01 +00:00
|
|
|
CurrentMetrics::set(CurrentMetrics::MemoryTracking, data.resident);
|
2020-04-17 04:09:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-02-10 13:10:17 +00:00
|
|
|
auto databases = DatabaseCatalog::instance().getDatabases();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t max_queue_size = 0;
|
|
|
|
size_t max_inserts_in_queue = 0;
|
|
|
|
size_t max_merges_in_queue = 0;
|
|
|
|
|
|
|
|
size_t sum_queue_size = 0;
|
|
|
|
size_t sum_inserts_in_queue = 0;
|
|
|
|
size_t sum_merges_in_queue = 0;
|
|
|
|
|
|
|
|
size_t max_absolute_delay = 0;
|
|
|
|
size_t max_relative_delay = 0;
|
|
|
|
|
|
|
|
size_t max_part_count_for_partition = 0;
|
|
|
|
|
2019-07-17 15:36:28 +00:00
|
|
|
size_t number_of_databases = databases.size();
|
|
|
|
size_t total_number_of_tables = 0;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & db : databases)
|
|
|
|
{
|
2019-10-01 12:44:17 +00:00
|
|
|
/// Lazy database can not contain MergeTree tables
|
|
|
|
if (db.second->getEngineName() == "Lazy")
|
|
|
|
continue;
|
2020-05-28 20:10:45 +00:00
|
|
|
for (auto iterator = db.second->getTablesIterator(context); iterator->isValid(); iterator->next())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-07-17 15:36:28 +00:00
|
|
|
++total_number_of_tables;
|
2020-04-22 06:01:33 +00:00
|
|
|
const auto & table = iterator->table();
|
2020-06-02 02:06:16 +00:00
|
|
|
if (!table)
|
|
|
|
continue;
|
|
|
|
|
2017-11-04 16:46:14 +00:00
|
|
|
StorageMergeTree * table_merge_tree = dynamic_cast<StorageMergeTree *>(table.get());
|
|
|
|
StorageReplicatedMergeTree * table_replicated_merge_tree = dynamic_cast<StorageReplicatedMergeTree *>(table.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (table_replicated_merge_tree)
|
|
|
|
{
|
|
|
|
StorageReplicatedMergeTree::Status status;
|
|
|
|
table_replicated_merge_tree->getStatus(status, false);
|
|
|
|
|
|
|
|
calculateMaxAndSum(max_queue_size, sum_queue_size, status.queue.queue_size);
|
|
|
|
calculateMaxAndSum(max_inserts_in_queue, sum_inserts_in_queue, status.queue.inserts_in_queue);
|
|
|
|
calculateMaxAndSum(max_merges_in_queue, sum_merges_in_queue, status.queue.merges_in_queue);
|
|
|
|
|
2019-08-17 21:18:22 +00:00
|
|
|
if (!status.is_readonly)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-17 21:18:22 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
time_t absolute_delay = 0;
|
|
|
|
time_t relative_delay = 0;
|
|
|
|
table_replicated_merge_tree->getReplicaDelays(absolute_delay, relative_delay);
|
|
|
|
|
|
|
|
calculateMax(max_absolute_delay, absolute_delay);
|
|
|
|
calculateMax(max_relative_delay, relative_delay);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__,
|
|
|
|
"Cannot get replica delay for table: " + backQuoteIfNeed(db.first) + "." + backQuoteIfNeed(iterator->name()));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 02:00:57 +00:00
|
|
|
calculateMax(max_part_count_for_partition, table_replicated_merge_tree->getMaxPartsCountForPartition());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (table_merge_tree)
|
|
|
|
{
|
2019-05-03 02:00:57 +00:00
|
|
|
calculateMax(max_part_count_for_partition, table_merge_tree->getMaxPartsCountForPartition());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["ReplicasMaxQueueSize"] = max_queue_size;
|
|
|
|
new_values["ReplicasMaxInsertsInQueue"] = max_inserts_in_queue;
|
|
|
|
new_values["ReplicasMaxMergesInQueue"] = max_merges_in_queue;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["ReplicasSumQueueSize"] = sum_queue_size;
|
|
|
|
new_values["ReplicasSumInsertsInQueue"] = sum_inserts_in_queue;
|
|
|
|
new_values["ReplicasSumMergesInQueue"] = sum_merges_in_queue;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["ReplicasMaxAbsoluteDelay"] = max_absolute_delay;
|
|
|
|
new_values["ReplicasMaxRelativeDelay"] = max_relative_delay;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["MaxPartCountForPartition"] = max_part_count_for_partition;
|
2019-07-17 15:36:28 +00:00
|
|
|
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["NumberOfDatabases"] = number_of_databases;
|
|
|
|
new_values["NumberOfTables"] = total_number_of_tables;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-10-23 06:12:50 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_JEMALLOC && JEMALLOC_VERSION_MAJOR >= 4
|
2018-08-02 00:20:20 +00:00
|
|
|
{
|
2020-04-16 12:31:57 +00:00
|
|
|
# define FOR_EACH_METRIC(M) \
|
2018-08-02 00:20:20 +00:00
|
|
|
M("allocated", size_t) \
|
|
|
|
M("active", size_t) \
|
|
|
|
M("metadata", size_t) \
|
|
|
|
M("metadata_thp", size_t) \
|
|
|
|
M("resident", size_t) \
|
|
|
|
M("mapped", size_t) \
|
|
|
|
M("retained", size_t) \
|
|
|
|
M("background_thread.num_threads", size_t) \
|
|
|
|
M("background_thread.num_runs", uint64_t) \
|
2020-04-16 12:31:57 +00:00
|
|
|
M("background_thread.run_interval", uint64_t)
|
2018-08-02 00:20:20 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
# define GET_METRIC(NAME, TYPE) \
|
2018-08-02 00:20:20 +00:00
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
TYPE value{}; \
|
|
|
|
size_t size = sizeof(value); \
|
|
|
|
mallctl("stats." NAME, &value, &size, nullptr, 0); \
|
2020-06-10 19:17:30 +00:00
|
|
|
new_values["jemalloc." NAME] = value; \
|
2020-03-08 21:29:00 +00:00
|
|
|
} while (false);
|
2018-08-02 00:20:20 +00:00
|
|
|
|
2019-01-04 14:18:49 +00:00
|
|
|
FOR_EACH_METRIC(GET_METRIC)
|
2018-08-02 00:20:20 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
# undef GET_METRIC
|
|
|
|
# undef FOR_EACH_METRIC
|
2018-08-02 00:20:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Add more metrics as you wish.
|
2020-06-10 19:17:30 +00:00
|
|
|
|
|
|
|
// Log the new metrics.
|
|
|
|
if (auto log = context.getAsynchronousMetricLog())
|
|
|
|
{
|
|
|
|
log->addValues(new_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, update the current metrics.
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
values = new_values;
|
2016-10-23 06:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|