ClickHouse/src/Coordination/KeeperConnectionStats.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.8 KiB
C++
Raw Normal View History

2022-03-03 06:30:22 +00:00
#include <atomic>
2021-11-18 20:17:22 +00:00
#include <Coordination/KeeperConnectionStats.h>
2022-06-10 07:49:46 +00:00
#include <Common/ProfileEvents.h>
namespace ProfileEvents
{
extern const Event KeeperPacketsSent;
extern const Event KeeperPacketsReceived;
extern const Event KeeperRequestTotal;
extern const Event KeeperLatency;
}
2021-11-18 20:17:22 +00:00
namespace DB
{
uint64_t KeeperConnectionStats::getMinLatency() const
{
2022-03-03 06:30:22 +00:00
return min_latency.load(std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
uint64_t KeeperConnectionStats::getMaxLatency() const
{
2022-03-03 06:30:22 +00:00
return max_latency.load(std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
uint64_t KeeperConnectionStats::getAvgLatency() const
{
2022-03-03 06:30:22 +00:00
auto cnt = count.load(std::memory_order_relaxed);
if (cnt)
return total_latency.load(std::memory_order_relaxed) / cnt;
2021-11-18 20:17:22 +00:00
return 0;
}
uint64_t KeeperConnectionStats::getLastLatency() const
{
2022-03-03 06:30:22 +00:00
return last_latency.load(std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
uint64_t KeeperConnectionStats::getPacketsReceived() const
{
2022-03-03 06:30:22 +00:00
return packets_received.load(std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
uint64_t KeeperConnectionStats::getPacketsSent() const
{
2022-03-03 06:30:22 +00:00
return packets_sent.load(std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
void KeeperConnectionStats::incrementPacketsReceived()
{
2022-03-03 06:30:22 +00:00
packets_received.fetch_add(1, std::memory_order_relaxed);
2022-06-10 07:49:46 +00:00
ProfileEvents::increment(ProfileEvents::KeeperPacketsReceived, 1);
2021-11-18 20:17:22 +00:00
}
void KeeperConnectionStats::incrementPacketsSent()
{
2022-03-03 06:30:22 +00:00
packets_sent.fetch_add(1, std::memory_order_relaxed);
2022-06-10 07:49:46 +00:00
ProfileEvents::increment(ProfileEvents::KeeperPacketsSent, 1);
2021-11-18 20:17:22 +00:00
}
void KeeperConnectionStats::updateLatency(uint64_t latency_ms)
{
2022-03-03 06:30:22 +00:00
last_latency.store(latency_ms, std::memory_order_relaxed);
total_latency.fetch_add(latency_ms, std::memory_order_relaxed);
2022-06-10 07:49:46 +00:00
ProfileEvents::increment(ProfileEvents::KeeperLatency, latency_ms);
2022-03-03 06:30:22 +00:00
count.fetch_add(1, std::memory_order_relaxed);
2022-06-10 07:49:46 +00:00
ProfileEvents::increment(ProfileEvents::KeeperRequestTotal, 1);
2022-03-03 06:30:22 +00:00
2022-03-07 03:11:14 +00:00
uint64_t prev_val = min_latency.load(std::memory_order_relaxed);
while (prev_val > latency_ms && !min_latency.compare_exchange_weak(prev_val, latency_ms, std::memory_order_relaxed)) {}
2022-03-03 06:30:22 +00:00
2022-03-07 03:11:14 +00:00
prev_val = max_latency.load(std::memory_order_relaxed);
while (prev_val < latency_ms && !max_latency.compare_exchange_weak(prev_val, latency_ms, std::memory_order_relaxed)) {}
2021-11-18 20:17:22 +00:00
}
void KeeperConnectionStats::reset()
{
resetLatency();
resetRequestCounters();
}
void KeeperConnectionStats::resetLatency()
{
2022-03-03 06:30:22 +00:00
total_latency.store(0, std::memory_order_relaxed);
count.store(0, std::memory_order_relaxed);
max_latency.store(0, std::memory_order_relaxed);
min_latency.store(0, std::memory_order_relaxed);
last_latency.store(0, std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
void KeeperConnectionStats::resetRequestCounters()
{
2022-03-03 06:30:22 +00:00
packets_received.store(0, std::memory_order_relaxed);
packets_sent.store(0, std::memory_order_relaxed);
2021-11-18 20:17:22 +00:00
}
}