feat(discovery): implement P3 S-DISCOVERY-01..04 Discovery catalog and optional DiscoveryRpc adapter
This commit is contained in:
1 parent
0cf187d664
commit
ec8c0bd40a
12 files changed
+2686
No files matched your search
@@ -0,0 +1,216 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Discovery/Types.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Discovery
|
||||
{
|
||||
class Catalog;
|
||||
|
||||
/// Move-only RAII handle for a local endpoint registration.
|
||||
/// Destruction releases the registration from future snapshots.
|
||||
class Registration final
|
||||
{
|
||||
public:
|
||||
|
||||
Registration() noexcept = default;
|
||||
~Registration() noexcept;
|
||||
|
||||
Registration(Registration&& other) noexcept;
|
||||
Registration& operator=(Registration&& other) noexcept;
|
||||
Registration(const Registration&) = delete;
|
||||
Registration& operator=(const Registration&) = delete;
|
||||
|
||||
[[nodiscard]] bool active() const noexcept;
|
||||
[[nodiscard]] const std::string& service() const noexcept
|
||||
{
|
||||
return m_service;
|
||||
}
|
||||
[[nodiscard]] const std::string& instance() const noexcept
|
||||
{
|
||||
return m_instance;
|
||||
}
|
||||
[[nodiscard]] std::optional<Void::Time::MonotonicClock::time_point>
|
||||
expires_at() const noexcept;
|
||||
|
||||
[[nodiscard]] Result<Void::Time::MonotonicClock::time_point> renew();
|
||||
void release() noexcept;
|
||||
|
||||
private:
|
||||
|
||||
friend class Catalog;
|
||||
struct State;
|
||||
std::shared_ptr<void> m_catalog_state{};
|
||||
std::string m_service{};
|
||||
std::string m_instance{};
|
||||
std::uint64_t m_token{0};
|
||||
bool m_leased{false};
|
||||
std::chrono::milliseconds m_ttl{0};
|
||||
bool m_active{false};
|
||||
};
|
||||
|
||||
/// Move-only handle for a bounded service watch.
|
||||
/// `cancel()` stops new admission and is safe from inside the callback.
|
||||
/// `join()` waits for already-admitted callbacks and returns `SelfJoin`
|
||||
/// instead of deadlocking when called synchronously from the callback itself.
|
||||
class WatchHandle final
|
||||
{
|
||||
public:
|
||||
|
||||
WatchHandle() noexcept = default;
|
||||
~WatchHandle() noexcept;
|
||||
|
||||
WatchHandle(WatchHandle&& other) noexcept;
|
||||
WatchHandle& operator=(WatchHandle&& other) noexcept;
|
||||
WatchHandle(const WatchHandle&) = delete;
|
||||
WatchHandle& operator=(const WatchHandle&) = delete;
|
||||
|
||||
[[nodiscard]] bool active() const noexcept;
|
||||
[[nodiscard]] std::size_t pending_events() const noexcept;
|
||||
|
||||
void cancel() noexcept;
|
||||
[[nodiscard]] Result<void> join() noexcept;
|
||||
|
||||
/// Explicit deterministic delivery step for `Manual` watch mode.
|
||||
/// Returns true if an event was delivered.
|
||||
bool deliver_one();
|
||||
std::size_t deliver_all();
|
||||
|
||||
private:
|
||||
|
||||
friend class Catalog;
|
||||
std::shared_ptr<void> m_catalog_state{};
|
||||
std::shared_ptr<void> m_watch_node{};
|
||||
std::uint64_t m_watch_id{0};
|
||||
};
|
||||
|
||||
/// Pure, thread-free service endpoint catalog supporting atomic static
|
||||
/// snapshots, local RAII leased/unleased registrations, and bounded watches.
|
||||
class Catalog final
|
||||
{
|
||||
public:
|
||||
|
||||
explicit Catalog(CatalogOptions options = {});
|
||||
~Catalog() noexcept;
|
||||
|
||||
Catalog(Catalog&&) noexcept;
|
||||
Catalog& operator=(Catalog&&) noexcept;
|
||||
Catalog(const Catalog&) = delete;
|
||||
Catalog& operator=(const Catalog&) = delete;
|
||||
|
||||
/// Validate candidate static snapshot completely before atomic
|
||||
/// publication. On any validation/conflict/cap error, preserves the
|
||||
/// active revision unchanged.
|
||||
[[nodiscard]] Result<std::uint64_t>
|
||||
replace_static(std::vector<EndpointRecord> candidate,
|
||||
bool bump_provider_generation = false);
|
||||
|
||||
/// Explicitly set provider health (e.g. for stale/unavailable
|
||||
/// resolution policy testing or upper replicated directory adapters).
|
||||
void set_provider_health(Freshness freshness,
|
||||
std::optional<Error> provider_error =
|
||||
std::nullopt);
|
||||
|
||||
[[nodiscard]] Result<Snapshot>
|
||||
resolve(std::string_view service,
|
||||
ResolvePolicy policy = {}) const;
|
||||
|
||||
[[nodiscard]] Result<Registration>
|
||||
register_local(EndpointRecord record,
|
||||
LeasePolicy lease = LeasePolicy::unleased());
|
||||
|
||||
[[nodiscard]] Result<WatchHandle>
|
||||
watch(std::string service, std::uint64_t after_revision,
|
||||
WatchCallback callback, WatchOptions options = {});
|
||||
|
||||
/// Process at most `limits.max_expirations_per_turn` logically
|
||||
/// expired local leases, advancing revision and notifying watches.
|
||||
std::size_t reap_expired();
|
||||
|
||||
/// Remove local entries, stop watch admission, and join admitted
|
||||
/// callbacks. Destruction alone only requests stop.
|
||||
[[nodiscard]] Result<void> shutdown_and_join() noexcept;
|
||||
|
||||
[[nodiscard]] std::uint64_t revision() const noexcept;
|
||||
[[nodiscard]] std::uint64_t provider_generation() const noexcept;
|
||||
[[nodiscard]] std::size_t active_record_count() const noexcept;
|
||||
[[nodiscard]] std::size_t active_watch_count() const noexcept;
|
||||
[[nodiscard]] std::size_t shared_watch_event_bytes() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
friend class Registration;
|
||||
friend class WatchHandle;
|
||||
struct State;
|
||||
std::shared_ptr<State> m_state;
|
||||
};
|
||||
|
||||
/// Explicit, thread-free lease renewal helper driven by injected steady time.
|
||||
class ManagedLeaseRenewer final
|
||||
{
|
||||
public:
|
||||
|
||||
ManagedLeaseRenewer(
|
||||
Registration& registration,
|
||||
std::chrono::milliseconds renew_interval =
|
||||
std::chrono::seconds{10},
|
||||
const Void::Time::MonotonicClock* clock =
|
||||
&Void::Time::SystemMonotonicClock::instance()) noexcept;
|
||||
|
||||
[[nodiscard]] bool due() const noexcept;
|
||||
[[nodiscard]] Result<bool> tick();
|
||||
[[nodiscard]] std::size_t consecutive_failures() const noexcept
|
||||
{
|
||||
return m_consecutive_failures;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Registration* m_registration{nullptr};
|
||||
std::chrono::milliseconds m_interval{std::chrono::seconds{10}};
|
||||
const Void::Time::MonotonicClock* m_clock{
|
||||
&Void::Time::SystemMonotonicClock::instance()};
|
||||
Void::Time::MonotonicClock::time_point m_next_renew{};
|
||||
std::size_t m_consecutive_failures{0};
|
||||
};
|
||||
|
||||
/// Deterministic client-side smooth weighted round-robin selector over an
|
||||
/// immutable Snapshot.
|
||||
class WeightedRoundRobinSelector final
|
||||
{
|
||||
public:
|
||||
|
||||
WeightedRoundRobinSelector() = default;
|
||||
|
||||
[[nodiscard]] Result<EndpointRecord>
|
||||
select(const Snapshot& snapshot);
|
||||
|
||||
void reset() noexcept;
|
||||
|
||||
private:
|
||||
|
||||
mutable std::mutex m_mu{};
|
||||
std::map<std::string, std::int64_t, std::less<>> m_current_weights{};
|
||||
};
|
||||
|
||||
/// Deterministic client-side affinity selector using weighted Rendezvous
|
||||
/// (HRW) hashing over `Void::Hash::fnv1a64`.
|
||||
class AffinitySelector final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static Result<EndpointRecord>
|
||||
select(const Snapshot& snapshot, std::string_view affinity_key);
|
||||
};
|
||||
|
||||
} // namespace Void::Discovery
|
||||
@@ -0,0 +1,245 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Detail/Types.hpp>
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Discovery
|
||||
{
|
||||
using Void::Result::Error;
|
||||
template <typename T> using Result = Void::Result::Result<T, Error>;
|
||||
|
||||
enum class DiscoveryErrorCode : std::int32_t {
|
||||
None = 0,
|
||||
InvalidArgument = 1,
|
||||
NotFound = 2,
|
||||
Conflict = 3,
|
||||
LimitExceeded = 4,
|
||||
Expired = 5,
|
||||
Unavailable = 6,
|
||||
Stale = 7,
|
||||
Cancelled = 8,
|
||||
SelfJoin = 9,
|
||||
Closed = 10,
|
||||
TlsIdentityMismatch = 11,
|
||||
};
|
||||
|
||||
[[nodiscard]] inline Error make_discovery_error(DiscoveryErrorCode code,
|
||||
std::string message)
|
||||
{
|
||||
using Void::Result::ErrorCode;
|
||||
using Void::Result::ErrorKind;
|
||||
|
||||
ErrorKind kind = ErrorKind::internal;
|
||||
switch (code) {
|
||||
case DiscoveryErrorCode::None:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
case DiscoveryErrorCode::InvalidArgument:
|
||||
case DiscoveryErrorCode::TlsIdentityMismatch:
|
||||
kind = ErrorKind::invalid_argument;
|
||||
break;
|
||||
case DiscoveryErrorCode::NotFound:
|
||||
kind = ErrorKind::not_found;
|
||||
break;
|
||||
case DiscoveryErrorCode::Conflict:
|
||||
case DiscoveryErrorCode::Expired:
|
||||
case DiscoveryErrorCode::SelfJoin:
|
||||
kind = ErrorKind::conflict;
|
||||
break;
|
||||
case DiscoveryErrorCode::LimitExceeded:
|
||||
kind = ErrorKind::exhausted;
|
||||
break;
|
||||
case DiscoveryErrorCode::Unavailable:
|
||||
case DiscoveryErrorCode::Stale:
|
||||
kind = ErrorKind::overloaded;
|
||||
break;
|
||||
case DiscoveryErrorCode::Cancelled:
|
||||
case DiscoveryErrorCode::Closed:
|
||||
kind = ErrorKind::cancelled;
|
||||
break;
|
||||
}
|
||||
return Error{kind, std::move(message),
|
||||
ErrorCode{Void::Result::kDiscoveryErrorDomain,
|
||||
static_cast<std::int32_t>(code)}};
|
||||
}
|
||||
|
||||
enum class Freshness : std::uint8_t {
|
||||
Fresh = 0,
|
||||
Stale = 1,
|
||||
Unavailable = 2,
|
||||
};
|
||||
|
||||
struct EndpointAddress {
|
||||
std::string scheme{"http"};
|
||||
std::string host{};
|
||||
std::uint16_t port{0};
|
||||
/// Configured peer identity retained across IP/address refreshes
|
||||
/// so DNS/address updates cannot downgrade TLS expected-name checks.
|
||||
std::string expected_tls_name{};
|
||||
|
||||
[[nodiscard]] std::string_view effective_tls_name() const noexcept
|
||||
{
|
||||
return expected_tls_name.empty()
|
||||
? std::string_view{host}
|
||||
: std::string_view{expected_tls_name};
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string to_origin_url() const
|
||||
{
|
||||
return scheme + "://" + host + ":" + std::to_string(port);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto
|
||||
operator<=>(const EndpointAddress&) const noexcept = default;
|
||||
};
|
||||
|
||||
struct EndpointRecord {
|
||||
std::string instance{};
|
||||
std::string service{};
|
||||
EndpointAddress endpoint{};
|
||||
std::uint32_t weight{1};
|
||||
bool ready{true};
|
||||
std::map<std::string, std::string, std::less<>> attributes{};
|
||||
|
||||
[[nodiscard]] bool
|
||||
operator==(const EndpointRecord&) const noexcept = default;
|
||||
};
|
||||
|
||||
struct Snapshot {
|
||||
std::uint64_t provider_generation{1};
|
||||
std::uint64_t revision{0};
|
||||
std::vector<EndpointRecord> records{};
|
||||
Freshness freshness{Freshness::Fresh};
|
||||
std::optional<Error> provider_error{};
|
||||
};
|
||||
|
||||
struct ResolvePolicy {
|
||||
bool allow_stale{false};
|
||||
bool include_unready{false};
|
||||
};
|
||||
|
||||
struct LeasePolicy {
|
||||
bool leased{false};
|
||||
std::chrono::milliseconds ttl{std::chrono::seconds{30}};
|
||||
std::chrono::milliseconds renew_interval{std::chrono::seconds{10}};
|
||||
|
||||
[[nodiscard]] static constexpr LeasePolicy unleased() noexcept
|
||||
{
|
||||
return LeasePolicy{
|
||||
.leased = false,
|
||||
.ttl = std::chrono::seconds{30},
|
||||
.renew_interval = std::chrono::seconds{10},
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr LeasePolicy with_ttl(
|
||||
std::chrono::milliseconds lease_ttl,
|
||||
std::chrono::milliseconds renewal =
|
||||
std::chrono::seconds{10}) noexcept
|
||||
{
|
||||
return LeasePolicy{
|
||||
.leased = true,
|
||||
.ttl = lease_ttl,
|
||||
.renew_interval = renewal,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
struct CatalogLimits {
|
||||
std::size_t max_services{1024};
|
||||
std::size_t max_endpoints_per_service{256};
|
||||
std::size_t max_total_records{10000};
|
||||
std::size_t max_attributes_per_record{16};
|
||||
std::size_t max_metadata_bytes_per_record{4096};
|
||||
std::size_t max_watches{1024};
|
||||
std::size_t max_events_per_watch{64};
|
||||
std::size_t max_shared_event_bytes{16ULL * 1024 * 1024};
|
||||
std::size_t max_expirations_per_turn{256};
|
||||
};
|
||||
|
||||
enum class WatchEventKind : std::uint8_t {
|
||||
InitialSnapshot = 0,
|
||||
Delta = 1,
|
||||
ResyncRequired = 2,
|
||||
};
|
||||
|
||||
struct WatchEvent {
|
||||
WatchEventKind kind{WatchEventKind::InitialSnapshot};
|
||||
std::string service{};
|
||||
std::uint64_t provider_generation{1};
|
||||
std::uint64_t revision{0};
|
||||
std::optional<Snapshot> snapshot{};
|
||||
std::vector<EndpointRecord> added{};
|
||||
std::vector<EndpointRecord> updated{};
|
||||
std::vector<std::string> removed_instances{};
|
||||
};
|
||||
|
||||
using WatchCallback = std::function<void(WatchEvent)>;
|
||||
|
||||
enum class WatchDispatchMode : std::uint8_t {
|
||||
/// Deliver outside catalog locks immediately on admission/mutation.
|
||||
Inline = 0,
|
||||
/// Queue events for explicit `WatchHandle::deliver_one()`/`deliver_all()`.
|
||||
Manual = 1,
|
||||
/// Serialize delivery on an injected `Void::Async::Executor`.
|
||||
Executor = 2,
|
||||
};
|
||||
|
||||
struct WatchOptions {
|
||||
WatchDispatchMode dispatch_mode{WatchDispatchMode::Inline};
|
||||
Void::Async::Executor* executor{nullptr};
|
||||
std::size_t max_queued_events{0}; // 0 => CatalogLimits::max_events_per_watch
|
||||
};
|
||||
|
||||
enum class ObservationKind : std::uint8_t {
|
||||
Resolve = 0,
|
||||
Register = 1,
|
||||
Renew = 2,
|
||||
Release = 3,
|
||||
Expire = 4,
|
||||
ReplaceStatic = 5,
|
||||
WatchResync = 6,
|
||||
};
|
||||
|
||||
/// Bounded low-cardinality observation event (never includes arbitrary
|
||||
/// per-instance metric labels).
|
||||
struct ObservationEvent {
|
||||
ObservationKind kind{ObservationKind::Resolve};
|
||||
bool ok{true};
|
||||
std::size_t count{0};
|
||||
};
|
||||
|
||||
using ObservationHook = std::function<void(const ObservationEvent&)>;
|
||||
|
||||
struct CatalogOptions {
|
||||
const Void::Time::MonotonicClock* clock{
|
||||
&Void::Time::SystemMonotonicClock::instance()};
|
||||
CatalogLimits limits{};
|
||||
ObservationHook observer{};
|
||||
};
|
||||
|
||||
[[nodiscard]] Result<void>
|
||||
validate_endpoint_record(const EndpointRecord& record,
|
||||
const CatalogLimits& limits = {});
|
||||
|
||||
[[nodiscard]] std::size_t
|
||||
estimate_record_bytes(const EndpointRecord& record) noexcept;
|
||||
|
||||
[[nodiscard]] std::size_t
|
||||
estimate_watch_event_bytes(const WatchEvent& event) noexcept;
|
||||
|
||||
} // namespace Void::Discovery
|
||||
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Discovery/Catalog.hpp>
|
||||
#include <Void/RPC/Rpc.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::DiscoveryRpc
|
||||
{
|
||||
struct EndpointResolverOptions {
|
||||
std::string service{};
|
||||
/// Optional pinned TLS peer identity. When set, any refreshed
|
||||
/// endpoint whose effective TLS name differs or attempts to
|
||||
/// downgrade from `https` is rejected with `TlsIdentityMismatch`.
|
||||
std::string expected_tls_name{};
|
||||
bool require_tls{false};
|
||||
Void::Discovery::ResolvePolicy resolve_policy{};
|
||||
Void::Net::HttpClientOptions http_options{
|
||||
.verify_tls = true,
|
||||
};
|
||||
Void::RPC::RpcLimits rpc_limits{};
|
||||
const Void::Time::MonotonicClock* clock{
|
||||
&Void::Time::SystemMonotonicClock::instance()};
|
||||
std::size_t max_outstanding_ops{128};
|
||||
std::chrono::milliseconds total_deadline{std::chrono::seconds{5}};
|
||||
};
|
||||
|
||||
struct SelectedEndpoint {
|
||||
Void::Discovery::EndpointRecord record{};
|
||||
std::string origin_url{};
|
||||
std::string effective_tls_name{};
|
||||
};
|
||||
|
||||
/// Optional RPC endpoint adapter living strictly above `Discovery` and
|
||||
/// `RPC` (preserving `RPC`'s independence from `Discovery`).
|
||||
class EndpointResolverClient final
|
||||
{
|
||||
public:
|
||||
|
||||
EndpointResolverClient(const Void::Discovery::Catalog& catalog,
|
||||
EndpointResolverOptions options);
|
||||
|
||||
[[nodiscard]] Void::Discovery::Result<SelectedEndpoint>
|
||||
select_endpoint(
|
||||
std::optional<std::string_view> affinity_key = std::nullopt);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Void::RPC::OwnedBytes>
|
||||
call(Void::RPC::MethodId method,
|
||||
Void::RPC::OwnedBytes payload,
|
||||
Void::RPC::CallOptions options = {},
|
||||
std::optional<std::string_view> affinity_key =
|
||||
std::nullopt);
|
||||
|
||||
[[nodiscard]] std::size_t outstanding_ops() const noexcept
|
||||
{
|
||||
return m_outstanding.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
const Void::Discovery::Catalog* m_catalog{nullptr};
|
||||
EndpointResolverOptions m_options{};
|
||||
Void::Discovery::WeightedRoundRobinSelector m_rr{};
|
||||
std::atomic<std::size_t> m_outstanding{0};
|
||||
};
|
||||
|
||||
} // namespace Void::DiscoveryRpc
|
||||
Reference in new issue
Block a user