From dec48426ae0244bc880c6274e2ed36f32917a4fd Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 19:58:20 +0300 Subject: [PATCH] RPC: bounded observation sink for hosts (S-RPC-05) --- include/Void/RPC/Observations.hpp | 72 +++++++++++++++++++++ src/RPC/Observations.cpp | 68 ++++++++++++++++++++ tests/standalone/RPC/Observations.cpp | 92 +++++++++++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 include/Void/RPC/Observations.hpp create mode 100644 src/RPC/Observations.cpp create mode 100644 tests/standalone/RPC/Observations.cpp diff --git a/include/Void/RPC/Observations.hpp b/include/Void/RPC/Observations.hpp new file mode 100644 index 0000000..230c0bf --- /dev/null +++ b/include/Void/RPC/Observations.hpp @@ -0,0 +1,72 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +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 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 m_events; + std::size_t m_dropped{0}; + std::uint64_t m_recorded{0}; + std::uint64_t m_bytes{0}; + }; +} // namespace Void::RPC diff --git a/src/RPC/Observations.cpp b/src/RPC/Observations.cpp new file mode 100644 index 0000000..93f1c92 --- /dev/null +++ b/src/RPC/Observations.cpp @@ -0,0 +1,68 @@ +#include + +#include + +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 RpcObservations::drain() noexcept + { + std::lock_guard lock{m_mutex}; + std::vector 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 diff --git a/tests/standalone/RPC/Observations.cpp b/tests/standalone/RPC/Observations.cpp new file mode 100644 index 0000000..7019479 --- /dev/null +++ b/tests/standalone/RPC/Observations.cpp @@ -0,0 +1,92 @@ +#include +#include +#include + +#include +#include +#include + +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 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(kThreads) * + static_cast(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(total)); + const std::size_t retained = observations.size(); + Void::Testing::expect_eq(observations.drain().size(), retained); +}