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
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,3 @@
|
|||||||
|
void_add_module(Discovery
|
||||||
|
INTERNAL_DEPS Core Functional Time Async
|
||||||
|
)
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
#include <Void/DiscoveryRpc/EndpointAdapter.hpp>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace Void::DiscoveryRpc
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct OutstandingGuard {
|
||||||
|
std::atomic<std::size_t>& counter;
|
||||||
|
~OutstandingGuard() noexcept
|
||||||
|
{
|
||||||
|
counter.fetch_sub(1, std::memory_order_acq_rel);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
EndpointResolverClient::EndpointResolverClient(
|
||||||
|
const Void::Discovery::Catalog& catalog,
|
||||||
|
EndpointResolverOptions options)
|
||||||
|
: m_catalog(&catalog), m_options(std::move(options))
|
||||||
|
{
|
||||||
|
if (m_options.clock == nullptr) {
|
||||||
|
m_options.clock = &Void::Time::SystemMonotonicClock::instance();
|
||||||
|
}
|
||||||
|
if (m_options.require_tls) {
|
||||||
|
m_options.http_options.verify_tls = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Discovery::Result<SelectedEndpoint>
|
||||||
|
EndpointResolverClient::select_endpoint(
|
||||||
|
std::optional<std::string_view> affinity_key)
|
||||||
|
{
|
||||||
|
if (m_catalog == nullptr) {
|
||||||
|
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||||
|
Void::Discovery::DiscoveryErrorCode::InvalidArgument,
|
||||||
|
"EndpointResolverClient: null catalog"));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto snap =
|
||||||
|
m_catalog->resolve(m_options.service, m_options.resolve_policy);
|
||||||
|
if (!snap.has_value()) {
|
||||||
|
return Void::Result::fail(snap.error());
|
||||||
|
}
|
||||||
|
|
||||||
|
auto selected =
|
||||||
|
affinity_key.has_value()
|
||||||
|
? Void::Discovery::AffinitySelector::select(*snap, *affinity_key)
|
||||||
|
: m_rr.select(*snap);
|
||||||
|
if (!selected.has_value()) {
|
||||||
|
return Void::Result::fail(selected.error());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_options.require_tls && selected->endpoint.scheme != "https") {
|
||||||
|
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||||
|
Void::Discovery::DiscoveryErrorCode::TlsIdentityMismatch,
|
||||||
|
"EndpointResolverClient: refusing non-TLS endpoint when "
|
||||||
|
"require_tls is enabled"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string effective_tls{
|
||||||
|
selected->endpoint.effective_tls_name()};
|
||||||
|
if (!m_options.expected_tls_name.empty() &&
|
||||||
|
effective_tls != m_options.expected_tls_name) {
|
||||||
|
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||||
|
Void::Discovery::DiscoveryErrorCode::TlsIdentityMismatch,
|
||||||
|
"EndpointResolverClient: endpoint TLS identity mismatch "
|
||||||
|
"(expected '" +
|
||||||
|
m_options.expected_tls_name + "', got '" + effective_tls +
|
||||||
|
"')"));
|
||||||
|
}
|
||||||
|
|
||||||
|
SelectedEndpoint out{};
|
||||||
|
out.origin_url = selected->endpoint.to_origin_url();
|
||||||
|
out.effective_tls_name = effective_tls;
|
||||||
|
out.record = std::move(*selected);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<Void::RPC::OwnedBytes>
|
||||||
|
EndpointResolverClient::call(
|
||||||
|
Void::RPC::MethodId method, Void::RPC::OwnedBytes payload,
|
||||||
|
Void::RPC::CallOptions options,
|
||||||
|
std::optional<std::string_view> affinity_key)
|
||||||
|
{
|
||||||
|
// Enforce bounded outstanding operations.
|
||||||
|
std::size_t cur = m_outstanding.load(std::memory_order_acquire);
|
||||||
|
for (;;) {
|
||||||
|
if (cur >= m_options.max_outstanding_ops) {
|
||||||
|
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||||
|
Void::Discovery::DiscoveryErrorCode::LimitExceeded,
|
||||||
|
"EndpointResolverClient: max_outstanding_ops reached"));
|
||||||
|
}
|
||||||
|
if (m_outstanding.compare_exchange_weak(
|
||||||
|
cur, cur + 1, std::memory_order_acq_rel,
|
||||||
|
std::memory_order_acquire)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
OutstandingGuard guard{m_outstanding};
|
||||||
|
|
||||||
|
auto selected = select_endpoint(affinity_key);
|
||||||
|
if (!selected.has_value()) {
|
||||||
|
return Void::Result::fail(selected.error());
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::RPC::ClientOptions copts{};
|
||||||
|
copts.endpoint_base_url = selected->origin_url;
|
||||||
|
copts.http_options = m_options.http_options;
|
||||||
|
if (m_options.require_tls) {
|
||||||
|
copts.http_options.verify_tls = true;
|
||||||
|
}
|
||||||
|
copts.limits = m_options.rpc_limits;
|
||||||
|
copts.clock = m_options.clock;
|
||||||
|
|
||||||
|
if (options.timeout <= std::chrono::milliseconds{0} ||
|
||||||
|
options.timeout > m_options.total_deadline) {
|
||||||
|
options.timeout = m_options.total_deadline;
|
||||||
|
}
|
||||||
|
// Retain peer identity header when address refreshes to an IP while
|
||||||
|
// expected_tls_name preserves the logical authority.
|
||||||
|
if (!selected->record.endpoint.expected_tls_name.empty()) {
|
||||||
|
bool has_authority = false;
|
||||||
|
for (const auto& m : options.metadata) {
|
||||||
|
if (m.key == "void-expected-authority") {
|
||||||
|
has_authority = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!has_authority) {
|
||||||
|
options.metadata.push_back(Void::RPC::MetadataEntry{
|
||||||
|
.key = "void-expected-authority",
|
||||||
|
.value = selected->effective_tls_name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::RPC::Client client{std::move(copts)};
|
||||||
|
return client.call(method, std::move(payload), options);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Void::DiscoveryRpc
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
void_add_module(DiscoveryRpc
|
||||||
|
INTERNAL_DEPS Discovery RPC
|
||||||
|
)
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
cmake_minimum_required(VERSION 4.0)
|
||||||
|
project(VoidDiscoveryOnlyConsumer LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 26)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
find_package(Void REQUIRED CONFIG COMPONENTS Discovery)
|
||||||
|
|
||||||
|
add_executable(discovery_only main.cpp)
|
||||||
|
target_link_libraries(discovery_only PRIVATE Void::Discovery)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
add_test(NAME discovery_only COMMAND discovery_only)
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <Void/Discovery/Catalog.hpp>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Void::Discovery::Catalog catalog{};
|
||||||
|
Void::Discovery::EndpointRecord rec{};
|
||||||
|
rec.service = "svc";
|
||||||
|
rec.instance = "i-1";
|
||||||
|
rec.endpoint.scheme = "http";
|
||||||
|
rec.endpoint.host = "127.0.0.1";
|
||||||
|
rec.endpoint.port = 8080;
|
||||||
|
|
||||||
|
auto rev = catalog.replace_static({rec});
|
||||||
|
if (!rev.has_value() || *rev != 1U) {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
auto snap = catalog.resolve("svc");
|
||||||
|
if (!snap.has_value() || snap->records.size() != 1U) {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -318,6 +318,11 @@ if(_has_rpc)
|
|||||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
has_modules(_has_discovery Discovery Core Functional Time Async)
|
||||||
|
if(_has_discovery)
|
||||||
|
run_consumer(DiscoveryOnly "${install_prefix}" "DiscoveryOnly" ${_no_external_deps})
|
||||||
|
endif()
|
||||||
|
|
||||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||||
if(_has_foundation)
|
if(_has_foundation)
|
||||||
|
|||||||
@@ -0,0 +1,278 @@
|
|||||||
|
#include <Void/Async/Executor.hpp>
|
||||||
|
#include <Void/Discovery/Catalog.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <chrono>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
Void::Discovery::EndpointRecord make_record(std::string svc,
|
||||||
|
std::string inst,
|
||||||
|
std::string host = "127.0.0.1",
|
||||||
|
std::uint16_t port = 8080,
|
||||||
|
std::uint32_t weight = 1)
|
||||||
|
{
|
||||||
|
Void::Discovery::EndpointRecord r{};
|
||||||
|
r.service = std::move(svc);
|
||||||
|
r.instance = std::move(inst);
|
||||||
|
r.endpoint.scheme = "http";
|
||||||
|
r.endpoint.host = std::move(host);
|
||||||
|
r.endpoint.port = port;
|
||||||
|
r.weight = weight;
|
||||||
|
r.ready = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Void::Testing::Test discovery_atomic_static_replacement_and_validation()
|
||||||
|
{
|
||||||
|
using namespace Void::Discovery;
|
||||||
|
|
||||||
|
Catalog catalog{};
|
||||||
|
|
||||||
|
auto good = catalog.replace_static({
|
||||||
|
make_record("orders", "inst-b", "10.0.0.2", 8080, 2),
|
||||||
|
make_record("orders", "inst-a", "10.0.0.1", 8080, 1),
|
||||||
|
});
|
||||||
|
Void::Testing::require(good.has_value());
|
||||||
|
Void::Testing::expect_eq(*good, 1ULL);
|
||||||
|
|
||||||
|
auto snap = catalog.resolve("orders");
|
||||||
|
Void::Testing::require(snap.has_value());
|
||||||
|
Void::Testing::expect_eq(snap->revision, 1ULL);
|
||||||
|
Void::Testing::expect_eq(snap->provider_generation, 1ULL);
|
||||||
|
Void::Testing::require(snap->records.size() == 2U);
|
||||||
|
// Deterministic sorted order by instance identity.
|
||||||
|
Void::Testing::expect_eq(snap->records[0].instance, std::string{"inst-a"});
|
||||||
|
Void::Testing::expect_eq(snap->records[1].instance, std::string{"inst-b"});
|
||||||
|
|
||||||
|
// Duplicate service+instance in candidate must reject atomically and keep
|
||||||
|
// active revision unchanged.
|
||||||
|
auto dup = catalog.replace_static({
|
||||||
|
make_record("orders", "inst-x", "10.0.0.9", 8080),
|
||||||
|
make_record("orders", "inst-x", "10.0.0.10", 8080),
|
||||||
|
});
|
||||||
|
Void::Testing::expect(!dup.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
dup.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::Conflict));
|
||||||
|
Void::Testing::expect_eq(catalog.revision(), 1ULL);
|
||||||
|
|
||||||
|
// Invalid metadata / zero port rejected atomically.
|
||||||
|
auto bad_port = make_record("orders", "inst-c", "10.0.0.3", 0);
|
||||||
|
auto bad_res = catalog.replace_static({bad_port});
|
||||||
|
Void::Testing::expect(!bad_res.has_value());
|
||||||
|
Void::Testing::expect_eq(catalog.revision(), 1ULL);
|
||||||
|
|
||||||
|
auto bad_meta = make_record("orders", "inst-c", "10.0.0.3", 8080);
|
||||||
|
bad_meta.attributes["k"] = std::string(5000, 'x');
|
||||||
|
auto bad_meta_res = catalog.replace_static({bad_meta});
|
||||||
|
Void::Testing::expect(!bad_meta_res.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
bad_meta_res.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::LimitExceeded));
|
||||||
|
Void::Testing::expect_eq(catalog.revision(), 1ULL);
|
||||||
|
|
||||||
|
// Active snapshot completely preserved.
|
||||||
|
auto after = catalog.resolve("orders");
|
||||||
|
Void::Testing::require(after.has_value());
|
||||||
|
Void::Testing::expect_eq(after->records.size(), 2ULL);
|
||||||
|
Void::Testing::expect_eq(after->records[0].instance, std::string{"inst-a"});
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test discovery_local_registration_leases_conflict_and_expiry()
|
||||||
|
{
|
||||||
|
using namespace Void::Discovery;
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
Void::Time::ManualClock clock{};
|
||||||
|
Catalog catalog{CatalogOptions{.clock = &clock}};
|
||||||
|
|
||||||
|
// Zero TTL is invalid, never an undocumented permanent registration.
|
||||||
|
auto zero_ttl = catalog.register_local(
|
||||||
|
make_record("auth", "node-1"), LeasePolicy::with_ttl(0ms));
|
||||||
|
Void::Testing::expect(!zero_ttl.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
zero_ttl.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::InvalidArgument));
|
||||||
|
|
||||||
|
auto reg1 = catalog.register_local(
|
||||||
|
make_record("auth", "node-1", "10.0.1.1", 9000),
|
||||||
|
LeasePolicy::with_ttl(30s, 10s));
|
||||||
|
Void::Testing::require(reg1.has_value());
|
||||||
|
Void::Testing::expect(reg1->active());
|
||||||
|
|
||||||
|
// Duplicate registration conflicts without stealing the first handle.
|
||||||
|
auto dup = catalog.register_local(
|
||||||
|
make_record("auth", "node-1", "10.0.1.2", 9001),
|
||||||
|
LeasePolicy::unleased());
|
||||||
|
Void::Testing::expect(!dup.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
dup.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::Conflict));
|
||||||
|
|
||||||
|
ManagedLeaseRenewer renewer{*reg1, 10s, &clock};
|
||||||
|
Void::Testing::expect(!renewer.due());
|
||||||
|
Void::Testing::require(clock.advance(10s).has_value());
|
||||||
|
Void::Testing::expect(renewer.due());
|
||||||
|
auto ticked = renewer.tick();
|
||||||
|
Void::Testing::require(ticked.has_value());
|
||||||
|
Void::Testing::expect(*ticked);
|
||||||
|
|
||||||
|
// Advance past renewed 30s TTL. Resolve must logically filter expired
|
||||||
|
// record even before reap_expired() runs.
|
||||||
|
Void::Testing::require(clock.advance(30s).has_value());
|
||||||
|
auto snap_expired = catalog.resolve("auth");
|
||||||
|
Void::Testing::require(snap_expired.has_value());
|
||||||
|
Void::Testing::expect(snap_expired->records.empty());
|
||||||
|
|
||||||
|
// Renewal after expiry fails with Expired.
|
||||||
|
auto late_renew = reg1->renew();
|
||||||
|
Void::Testing::expect(!late_renew.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
late_renew.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::Expired));
|
||||||
|
|
||||||
|
Void::Testing::expect_eq(catalog.reap_expired(), 1ULL);
|
||||||
|
|
||||||
|
// Unleased registration stays alive indefinitely until RAII release.
|
||||||
|
{
|
||||||
|
auto unleased = catalog.register_local(
|
||||||
|
make_record("auth", "node-perm", "10.0.1.9", 9009),
|
||||||
|
LeasePolicy::unleased());
|
||||||
|
Void::Testing::require(unleased.has_value());
|
||||||
|
Void::Testing::require(clock.advance(std::chrono::hours{24}).has_value());
|
||||||
|
auto s = catalog.resolve("auth");
|
||||||
|
Void::Testing::require(s.has_value());
|
||||||
|
Void::Testing::require(s->records.size() == 1U);
|
||||||
|
Void::Testing::expect_eq(s->records[0].instance,
|
||||||
|
std::string{"node-perm"});
|
||||||
|
}
|
||||||
|
auto after_release = catalog.resolve("auth");
|
||||||
|
Void::Testing::require(after_release.has_value());
|
||||||
|
Void::Testing::expect(after_release->records.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test discovery_watch_overflow_resync_self_cancel_and_self_join()
|
||||||
|
{
|
||||||
|
using namespace Void::Discovery;
|
||||||
|
|
||||||
|
CatalogLimits limits{};
|
||||||
|
limits.max_events_per_watch = 2;
|
||||||
|
Catalog catalog{CatalogOptions{.limits = limits}};
|
||||||
|
|
||||||
|
std::vector<WatchEvent> events;
|
||||||
|
auto wh = catalog.watch(
|
||||||
|
"billing", 0,
|
||||||
|
[&](WatchEvent ev) { events.push_back(std::move(ev)); },
|
||||||
|
WatchOptions{.dispatch_mode = WatchDispatchMode::Manual});
|
||||||
|
Void::Testing::require(wh.has_value());
|
||||||
|
// Initial snapshot queued (1 event).
|
||||||
|
Void::Testing::expect_eq(wh->pending_events(), 1ULL);
|
||||||
|
|
||||||
|
// Push 3 rapid updates without delivering -> queue cap 2 overflows and
|
||||||
|
// coalesces into an explicit ResyncRequired event carrying latest snapshot.
|
||||||
|
auto r1 = catalog.register_local(make_record("billing", "b-1"));
|
||||||
|
auto r2 = catalog.register_local(make_record("billing", "b-2"));
|
||||||
|
auto r3 = catalog.register_local(make_record("billing", "b-3"));
|
||||||
|
Void::Testing::require(r1.has_value());
|
||||||
|
Void::Testing::require(r2.has_value());
|
||||||
|
Void::Testing::require(r3.has_value());
|
||||||
|
|
||||||
|
Void::Testing::expect_eq(wh->pending_events(), 1ULL);
|
||||||
|
Void::Testing::expect_eq(wh->deliver_all(), 1ULL);
|
||||||
|
Void::Testing::require(events.size() == 1U);
|
||||||
|
Void::Testing::expect(events[0].kind == WatchEventKind::ResyncRequired);
|
||||||
|
Void::Testing::require(events[0].snapshot.has_value());
|
||||||
|
Void::Testing::expect_eq(events[0].snapshot->records.size(), 3ULL);
|
||||||
|
|
||||||
|
// Provider generation reset emits explicit ResyncRequired.
|
||||||
|
auto reset = catalog.replace_static(
|
||||||
|
{make_record("billing", "b-static")}, true);
|
||||||
|
Void::Testing::require(reset.has_value());
|
||||||
|
Void::Testing::expect_eq(catalog.provider_generation(), 2ULL);
|
||||||
|
Void::Testing::expect_eq(wh->deliver_all(), 1ULL);
|
||||||
|
Void::Testing::require(events.size() == 2U);
|
||||||
|
Void::Testing::expect(events[1].kind == WatchEventKind::ResyncRequired);
|
||||||
|
Void::Testing::expect_eq(events[1].provider_generation, 2ULL);
|
||||||
|
|
||||||
|
// Self-cancel and self-join diagnosis inside a callback (on Executor mode).
|
||||||
|
Void::Async::ManualExecutor exec{};
|
||||||
|
WatchHandle* self_ptr = nullptr;
|
||||||
|
bool self_join_diagnosed = false;
|
||||||
|
auto self_watch = catalog.watch(
|
||||||
|
"billing", 0,
|
||||||
|
[&](WatchEvent) {
|
||||||
|
if (self_ptr != nullptr) {
|
||||||
|
auto jr = self_ptr->join();
|
||||||
|
self_join_diagnosed =
|
||||||
|
!jr.has_value() &&
|
||||||
|
jr.error().code().value ==
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::SelfJoin);
|
||||||
|
self_ptr->cancel();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
WatchOptions{
|
||||||
|
.dispatch_mode = WatchDispatchMode::Executor,
|
||||||
|
.executor = &exec,
|
||||||
|
});
|
||||||
|
Void::Testing::require(self_watch.has_value());
|
||||||
|
self_ptr = &(*self_watch);
|
||||||
|
Void::Testing::expect(exec.drain() >= 1U);
|
||||||
|
Void::Testing::expect(self_join_diagnosed);
|
||||||
|
Void::Testing::expect(!self_watch->active());
|
||||||
|
Void::Testing::expect(self_watch->join().has_value());
|
||||||
|
|
||||||
|
Void::Testing::expect(catalog.shutdown_and_join().has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test discovery_freshness_policy_and_selectors()
|
||||||
|
{
|
||||||
|
using namespace Void::Discovery;
|
||||||
|
|
||||||
|
Catalog catalog{};
|
||||||
|
auto pub = catalog.replace_static({
|
||||||
|
make_record("search", "s-1", "10.0.2.1", 8080, 3),
|
||||||
|
make_record("search", "s-2", "10.0.2.2", 8080, 1),
|
||||||
|
});
|
||||||
|
Void::Testing::require(pub.has_value());
|
||||||
|
|
||||||
|
catalog.set_provider_health(
|
||||||
|
Freshness::Stale,
|
||||||
|
make_discovery_error(DiscoveryErrorCode::Stale, "quorum lost"));
|
||||||
|
auto strict = catalog.resolve("search", ResolvePolicy{.allow_stale = false});
|
||||||
|
Void::Testing::expect(!strict.has_value());
|
||||||
|
auto stale_ok =
|
||||||
|
catalog.resolve("search", ResolvePolicy{.allow_stale = true});
|
||||||
|
Void::Testing::require(stale_ok.has_value());
|
||||||
|
Void::Testing::expect(stale_ok->freshness == Freshness::Stale);
|
||||||
|
Void::Testing::expect(stale_ok->provider_error.has_value());
|
||||||
|
|
||||||
|
// Weighted round-robin delivers 3:1 ratio deterministically.
|
||||||
|
WeightedRoundRobinSelector rr{};
|
||||||
|
std::size_t s1_count = 0;
|
||||||
|
std::size_t s2_count = 0;
|
||||||
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
auto picked = rr.select(*stale_ok);
|
||||||
|
Void::Testing::require(picked.has_value());
|
||||||
|
if (picked->instance == "s-1") {
|
||||||
|
++s1_count;
|
||||||
|
} else {
|
||||||
|
++s2_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Void::Testing::expect_eq(s1_count, 6ULL);
|
||||||
|
Void::Testing::expect_eq(s2_count, 2ULL);
|
||||||
|
|
||||||
|
// Affinity selector is deterministic across repeated queries.
|
||||||
|
auto a1 = AffinitySelector::select(*stale_ok, "tenant-42");
|
||||||
|
auto a2 = AffinitySelector::select(*stale_ok, "tenant-42");
|
||||||
|
Void::Testing::require(a1.has_value());
|
||||||
|
Void::Testing::require(a2.has_value());
|
||||||
|
Void::Testing::expect_eq(a1->instance, a2->instance);
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include <Void/DiscoveryRpc/EndpointAdapter.hpp>
|
||||||
|
#include <Void/RPC/Rpc.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
Void::Testing::Test discovery_rpc_identity_preserving_refresh_and_roundtrip()
|
||||||
|
{
|
||||||
|
using namespace Void::Discovery;
|
||||||
|
using namespace Void::DiscoveryRpc;
|
||||||
|
|
||||||
|
Void::RPC::Server server{};
|
||||||
|
static constexpr Void::RPC::MethodId kEchoMethod{100U, 1U};
|
||||||
|
auto reg = server.bind(
|
||||||
|
Void::RPC::MethodDescriptor{
|
||||||
|
.id = kEchoMethod,
|
||||||
|
.service_name = "echo",
|
||||||
|
.method_name = "ping",
|
||||||
|
.schema_version = 1,
|
||||||
|
},
|
||||||
|
[](const Void::RPC::CallContext& ctx, Void::RPC::OwnedBytes req)
|
||||||
|
-> Void::Result::Result<Void::RPC::OwnedBytes> {
|
||||||
|
std::string auth_seen;
|
||||||
|
for (const auto& m : ctx.metadata) {
|
||||||
|
if (m.key == "void-expected-authority") {
|
||||||
|
auth_seen = m.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Void::RPC::OwnedBytes out = std::move(req);
|
||||||
|
for (char c : auth_seen) {
|
||||||
|
out.push_back(static_cast<std::byte>(c));
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
});
|
||||||
|
Void::Testing::require(reg.has_value());
|
||||||
|
Void::Testing::require(server.start().has_value());
|
||||||
|
|
||||||
|
Catalog catalog{};
|
||||||
|
EndpointRecord rec{};
|
||||||
|
rec.service = "echo";
|
||||||
|
rec.instance = "echo-1";
|
||||||
|
rec.endpoint.scheme = "http";
|
||||||
|
rec.endpoint.host = "127.0.0.1";
|
||||||
|
rec.endpoint.port = server.bound_port();
|
||||||
|
rec.endpoint.expected_tls_name = "echo.internal";
|
||||||
|
rec.weight = 1;
|
||||||
|
Void::Testing::require(catalog.replace_static({rec}).has_value());
|
||||||
|
|
||||||
|
EndpointResolverClient client{
|
||||||
|
catalog,
|
||||||
|
EndpointResolverOptions{
|
||||||
|
.service = "echo",
|
||||||
|
.expected_tls_name = "echo.internal",
|
||||||
|
.require_tls = false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Void::RPC::OwnedBytes payload{std::byte{'o'}, std::byte{'k'},
|
||||||
|
std::byte{':'}};
|
||||||
|
auto resp = client.call(kEchoMethod, std::move(payload));
|
||||||
|
Void::Testing::require(resp.has_value());
|
||||||
|
const std::string reply{
|
||||||
|
reinterpret_cast<const char*>(resp->data()), resp->size()};
|
||||||
|
Void::Testing::expect_eq(reply, std::string{"ok:echo.internal"});
|
||||||
|
|
||||||
|
// TLS enforcement rejects non-HTTPS downgrade and mismatched identity.
|
||||||
|
EndpointResolverClient tls_client{
|
||||||
|
catalog,
|
||||||
|
EndpointResolverOptions{
|
||||||
|
.service = "echo",
|
||||||
|
.expected_tls_name = "echo.internal",
|
||||||
|
.require_tls = true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
auto downgrade = tls_client.select_endpoint();
|
||||||
|
Void::Testing::expect(!downgrade.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
downgrade.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::TlsIdentityMismatch));
|
||||||
|
|
||||||
|
// Identity mismatch on HTTPS refresh also fails closed.
|
||||||
|
rec.endpoint.scheme = "https";
|
||||||
|
rec.endpoint.expected_tls_name = "spoofed.evil";
|
||||||
|
Void::Testing::require(catalog.replace_static({rec}).has_value());
|
||||||
|
auto mismatch = tls_client.select_endpoint();
|
||||||
|
Void::Testing::expect(!mismatch.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
mismatch.error().code().value,
|
||||||
|
static_cast<std::int32_t>(DiscoveryErrorCode::TlsIdentityMismatch));
|
||||||
|
|
||||||
|
server.shutdown();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user