RPC: bounded observation sink for hosts (S-RPC-05)
This commit is contained in:
1 parent
3f71bfdd4a
commit
dec48426ae
3 files changed
+232
No files matched your search
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/RPC/Types.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::RPC
|
||||
{
|
||||
/// Capacity bounds for one observation sink.
|
||||
struct RpcObservationLimits {
|
||||
/// Fixed ring size. Memory never exceeds this many events, no
|
||||
/// matter how fast the producers are or how slow the consumer is.
|
||||
std::size_t max_events{1024};
|
||||
};
|
||||
|
||||
enum class RpcObservationKind : std::uint8_t {
|
||||
CallStarted,
|
||||
CallFinished,
|
||||
StreamOpened,
|
||||
StreamClosed,
|
||||
BytesReceived,
|
||||
BytesSent,
|
||||
Error
|
||||
};
|
||||
|
||||
struct RpcObservation {
|
||||
RpcObservationKind kind{RpcObservationKind::CallStarted};
|
||||
RpcStatusCode status{RpcStatusCode::Ok};
|
||||
std::size_t bytes{0};
|
||||
std::chrono::milliseconds duration{0};
|
||||
};
|
||||
|
||||
/// Bounded, thread-safe observation sink for RPC hosts.
|
||||
///
|
||||
/// Producers never block: when the fixed ring is full the event is
|
||||
/// dropped and counted, so a stalled or absent collector cannot grow
|
||||
/// memory. `drain()` hands the retained events to a consumer.
|
||||
class RpcObservations
|
||||
{
|
||||
public:
|
||||
|
||||
explicit RpcObservations(RpcObservationLimits limits = {});
|
||||
|
||||
void record(const RpcObservation& event) noexcept;
|
||||
|
||||
/// Remove and return all retained events in arrival order.
|
||||
[[nodiscard]] std::vector<RpcObservation> drain() noexcept;
|
||||
|
||||
[[nodiscard]] std::size_t capacity() const noexcept;
|
||||
[[nodiscard]] std::size_t size() const noexcept;
|
||||
/// Events discarded because the ring was full (cumulative).
|
||||
[[nodiscard]] std::size_t dropped() const noexcept;
|
||||
/// Every event ever recorded, including dropped ones.
|
||||
[[nodiscard]] std::uint64_t total_recorded() const noexcept;
|
||||
/// Sum of `bytes` across all byte observations (cumulative).
|
||||
[[nodiscard]] std::uint64_t total_bytes() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
RpcObservationLimits m_limits;
|
||||
std::deque<RpcObservation> m_events;
|
||||
std::size_t m_dropped{0};
|
||||
std::uint64_t m_recorded{0};
|
||||
std::uint64_t m_bytes{0};
|
||||
};
|
||||
} // namespace Void::RPC
|
||||
@@ -0,0 +1,68 @@
|
||||
#include <Void/RPC/Observations.hpp>
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace Void::RPC
|
||||
{
|
||||
RpcObservations::RpcObservations(RpcObservationLimits limits)
|
||||
: m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
void RpcObservations::record(const RpcObservation& event) noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
++m_recorded;
|
||||
if (event.kind == RpcObservationKind::BytesReceived ||
|
||||
event.kind == RpcObservationKind::BytesSent) {
|
||||
m_bytes += event.bytes;
|
||||
}
|
||||
if (m_events.size() >= m_limits.max_events) {
|
||||
++m_dropped;
|
||||
return;
|
||||
}
|
||||
m_events.push_back(event);
|
||||
}
|
||||
|
||||
std::vector<RpcObservation> RpcObservations::drain() noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
std::vector<RpcObservation> out;
|
||||
out.reserve(m_events.size());
|
||||
for (const auto& event : m_events) {
|
||||
out.push_back(event);
|
||||
}
|
||||
m_events.clear();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::size_t RpcObservations::capacity() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_limits.max_events;
|
||||
}
|
||||
|
||||
std::size_t RpcObservations::size() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_events.size();
|
||||
}
|
||||
|
||||
std::size_t RpcObservations::dropped() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_dropped;
|
||||
}
|
||||
|
||||
std::uint64_t RpcObservations::total_recorded() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_recorded;
|
||||
}
|
||||
|
||||
std::uint64_t RpcObservations::total_bytes() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_bytes;
|
||||
}
|
||||
} // namespace Void::RPC
|
||||
@@ -0,0 +1,92 @@
|
||||
#include <Void/RPC/Observations.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::RPC;
|
||||
|
||||
RpcObservation event(RpcObservationKind kind, std::size_t bytes = 0)
|
||||
{
|
||||
return RpcObservation{.kind = kind, .bytes = bytes};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test rpc_observations_ring_is_bounded_and_counted()
|
||||
{
|
||||
RpcObservations observations{RpcObservationLimits{.max_events = 4}};
|
||||
Void::Testing::expect_eq(observations.capacity(), std::size_t{4});
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
observations.record(event(RpcObservationKind::CallStarted));
|
||||
}
|
||||
Void::Testing::expect_eq(observations.size(), std::size_t{4});
|
||||
Void::Testing::expect_eq(observations.dropped(), std::size_t{2});
|
||||
Void::Testing::expect_eq(observations.total_recorded(), std::uint64_t{6});
|
||||
|
||||
// Drain preserves arrival order and clears the ring.
|
||||
const auto drained = observations.drain();
|
||||
Void::Testing::expect_eq(drained.size(), std::size_t{4});
|
||||
for (const auto& entry : drained) {
|
||||
Void::Testing::expect(entry.kind == RpcObservationKind::CallStarted);
|
||||
}
|
||||
Void::Testing::expect_eq(observations.size(), std::size_t{0});
|
||||
Void::Testing::expect(observations.drain().empty());
|
||||
|
||||
// Dropped is cumulative across drains.
|
||||
Void::Testing::expect_eq(observations.dropped(), std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_observations_zero_capacity_drops_everything()
|
||||
{
|
||||
RpcObservations observations{RpcObservationLimits{.max_events = 0}};
|
||||
observations.record(event(RpcObservationKind::StreamOpened));
|
||||
Void::Testing::expect_eq(observations.size(), std::size_t{0});
|
||||
Void::Testing::expect_eq(observations.dropped(), std::size_t{1});
|
||||
Void::Testing::expect_eq(observations.total_recorded(), std::uint64_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_observations_aggregates_byte_totals()
|
||||
{
|
||||
RpcObservations observations;
|
||||
observations.record(event(RpcObservationKind::BytesSent, 10));
|
||||
observations.record(event(RpcObservationKind::BytesReceived, 5));
|
||||
observations.record(event(RpcObservationKind::CallStarted));
|
||||
// Non-byte observations do not contribute even if they carry a size.
|
||||
observations.record(event(RpcObservationKind::Error, 100));
|
||||
Void::Testing::expect_eq(observations.total_bytes(), std::uint64_t{15});
|
||||
Void::Testing::expect_eq(observations.total_recorded(), std::uint64_t{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_observations_concurrent_producers_stay_bounded()
|
||||
{
|
||||
constexpr std::size_t kCapacity = 64;
|
||||
constexpr int kThreads = 4;
|
||||
constexpr int kPerThread = 500;
|
||||
RpcObservations observations{RpcObservationLimits{.max_events = kCapacity}};
|
||||
|
||||
std::vector<std::thread> producers;
|
||||
for (int t = 0; t < kThreads; ++t) {
|
||||
producers.emplace_back([&observations] {
|
||||
for (int i = 0; i < kPerThread; ++i) {
|
||||
observations.record(event(RpcObservationKind::CallFinished));
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& producer : producers) {
|
||||
producer.join();
|
||||
}
|
||||
|
||||
const std::uint64_t total = static_cast<std::uint64_t>(kThreads) *
|
||||
static_cast<std::uint64_t>(kPerThread);
|
||||
Void::Testing::expect_eq(observations.total_recorded(), total);
|
||||
Void::Testing::expect(observations.size() <= kCapacity);
|
||||
Void::Testing::expect_eq(observations.size() + observations.dropped(),
|
||||
static_cast<std::size_t>(total));
|
||||
const std::size_t retained = observations.size();
|
||||
Void::Testing::expect_eq(observations.drain().size(), retained);
|
||||
}
|
||||
Reference in new issue
Block a user