ClickHouse/dbms/Interpreters/MetricLog.cpp

134 lines
4.4 KiB
C++
Raw Normal View History

2019-08-13 14:31:46 +00:00
#include <Interpreters/MetricLog.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
2019-08-18 00:04:58 +00:00
2019-08-13 14:31:46 +00:00
namespace DB
{
Block MetricLogElement::createBlock()
{
ColumnsWithTypeAndName columns_with_type_and_name;
2019-08-18 00:04:58 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeDate>(), "event_date");
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeDateTime>(), "event_time");
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeUInt64>(), "milliseconds");
2019-08-13 14:31:46 +00:00
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
{
std::string name;
name += "ProfileEvent_";
name += ProfileEvents::getName(ProfileEvents::Event(i));
2019-08-14 13:08:07 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeUInt64>(), std::move(name));
2019-08-13 14:31:46 +00:00
}
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
{
std::string name;
name += "CurrentMetric_";
name += CurrentMetrics::getName(ProfileEvents::Event(i));
2019-08-14 13:08:07 +00:00
columns_with_type_and_name.emplace_back(std::make_shared<DataTypeInt64>(), std::move(name));
2019-08-13 14:31:46 +00:00
}
return Block(columns_with_type_and_name);
}
2019-08-18 00:04:58 +00:00
2019-08-13 14:31:46 +00:00
void MetricLogElement::appendToBlock(Block & block) const
{
MutableColumns columns = block.mutateColumns();
2019-08-18 00:04:58 +00:00
size_t column_idx = 0;
2019-08-13 14:31:46 +00:00
2019-08-18 00:04:58 +00:00
columns[column_idx++]->insert(DateLUT::instance().toDayNum(event_time));
columns[column_idx++]->insert(event_time);
columns[column_idx++]->insert(milliseconds);
2019-08-13 14:31:46 +00:00
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
2019-08-18 00:04:58 +00:00
columns[column_idx++]->insert(profile_events[i]);
2019-08-13 14:31:46 +00:00
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
2019-08-18 00:04:58 +00:00
columns[column_idx++]->insert(current_metrics[i]);
2019-08-13 14:31:46 +00:00
}
2019-08-18 00:04:58 +00:00
2019-08-13 16:47:12 +00:00
void MetricLog::startCollectMetric(size_t collect_interval_milliseconds_)
{
collect_interval_milliseconds = collect_interval_milliseconds_;
is_shutdown_metric_thread = false;
metric_flush_thread = ThreadFromGlobalPool([this] { metricThreadFunction(); });
}
2019-08-18 00:04:58 +00:00
2019-08-13 16:47:12 +00:00
void MetricLog::stopCollectMetric()
{
bool old_val = false;
if (!is_shutdown_metric_thread.compare_exchange_strong(old_val, true))
return;
metric_flush_thread.join();
}
2019-08-18 00:04:58 +00:00
2019-08-15 16:09:43 +00:00
inline UInt64 time_in_milliseconds(std::chrono::time_point<std::chrono::system_clock> timepoint)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(timepoint.time_since_epoch()).count();
}
2019-08-18 00:04:58 +00:00
2019-08-15 16:09:43 +00:00
inline UInt64 time_in_seconds(std::chrono::time_point<std::chrono::system_clock> timepoint)
{
return std::chrono::duration_cast<std::chrono::seconds>(timepoint.time_since_epoch()).count();
}
2019-08-18 00:04:58 +00:00
2019-08-13 16:47:12 +00:00
void MetricLog::metricThreadFunction()
{
2019-08-14 12:54:41 +00:00
auto desired_timepoint = std::chrono::system_clock::now();
/// For differentiation of ProfileEvents counters.
std::vector<ProfileEvents::Count> prev_profile_events(ProfileEvents::end());
2019-08-18 00:04:58 +00:00
2019-08-15 23:35:54 +00:00
while (!is_shutdown_metric_thread)
2019-08-13 16:47:12 +00:00
{
try
{
2019-08-15 23:35:54 +00:00
const auto current_time = std::chrono::system_clock::now();
2019-08-18 00:04:58 +00:00
MetricLogElement elem;
2019-08-15 23:35:54 +00:00
elem.event_time = std::chrono::system_clock::to_time_t(current_time);
elem.milliseconds = time_in_milliseconds(current_time) - time_in_seconds(current_time) * 1000;
2019-08-15 16:39:18 +00:00
2019-08-18 00:25:58 +00:00
elem.profile_events.resize(ProfileEvents::end());
2019-08-18 00:04:58 +00:00
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
{
const ProfileEvents::Count new_value = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
2019-09-21 18:30:01 +00:00
auto & old_value = prev_profile_events[i];
2019-08-18 00:04:58 +00:00
elem.profile_events[i] = new_value - old_value;
old_value = new_value;
}
2019-08-18 00:25:58 +00:00
elem.current_metrics.resize(CurrentMetrics::end());
2019-08-18 00:04:58 +00:00
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
{
elem.current_metrics[i] = CurrentMetrics::values[i];
}
2019-08-13 16:47:12 +00:00
this->add(elem);
2019-08-15 23:35:54 +00:00
/// We will record current time into table but align it to regular time intervals to avoid time drift.
/// We may drop some time points if the server is overloaded and recording took too much time.
while (desired_timepoint <= current_time)
2019-08-15 16:09:43 +00:00
desired_timepoint += std::chrono::milliseconds(collect_interval_milliseconds);
2019-08-15 23:35:54 +00:00
2019-08-14 12:54:41 +00:00
std::this_thread::sleep_until(desired_timepoint);
2019-08-13 16:47:12 +00:00
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
}
2019-08-13 14:31:46 +00:00
}