NetworkedApplications: Discovery-backed service directory with partition policy and optional registration (P-NETWORKED-08)
This commit is contained in:
1 parent
504f259d02
commit
57ebf3130b
7 files changed
+369
-2
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "networked_kv/domain.hpp"
|
||||
#include "networked_kv/http_api.hpp"
|
||||
#include "networked_kv/service_directory.hpp"
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Net/HttpServer.hpp>
|
||||
@@ -30,6 +31,12 @@ namespace NetworkedKv
|
||||
std::size_t max_scan_page{1000};
|
||||
std::chrono::milliseconds read_timeout{std::chrono::seconds{30}};
|
||||
std::chrono::milliseconds write_timeout{std::chrono::seconds{30}};
|
||||
/// Optional Discovery-owned service directory. Registration is an
|
||||
/// observation dependency: its failure never stops the data plane.
|
||||
bool discovery_enabled{false};
|
||||
std::string discovery_service{"networked-kv"};
|
||||
std::string discovery_instance{};
|
||||
std::string discovery_host{"127.0.0.1"};
|
||||
std::chrono::milliseconds request_deadline{
|
||||
std::chrono::seconds{30}};
|
||||
std::chrono::milliseconds shutdown_grace{
|
||||
@@ -64,6 +71,8 @@ namespace NetworkedKv
|
||||
bool managed{false};
|
||||
std::size_t started_workers{0};
|
||||
std::uint16_t bound_port{0};
|
||||
bool discovery_registered{false};
|
||||
std::string discovery_error;
|
||||
};
|
||||
|
||||
/// Composition surface for the networked key-value reference service.
|
||||
@@ -116,6 +125,17 @@ namespace NetworkedKv
|
||||
/// Port the administrative listener bound (0 when not listening).
|
||||
[[nodiscard]] std::uint16_t admin_bound_port() const noexcept;
|
||||
|
||||
/// Discovery directory when enabled (null otherwise). Owned by the
|
||||
/// application; valid until the next reload or stop.
|
||||
[[nodiscard]] ServiceDirectory* directory() noexcept
|
||||
{
|
||||
return m_directory.get();
|
||||
}
|
||||
[[nodiscard]] const ServiceDirectory* directory() const noexcept
|
||||
{
|
||||
return m_directory.get();
|
||||
}
|
||||
|
||||
/// Consistent operator backup of the durable profile. Fails when
|
||||
/// the in-memory profile is active.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
@@ -140,6 +160,8 @@ namespace NetworkedKv
|
||||
std::unique_ptr<KvApi> m_api{};
|
||||
std::unique_ptr<Void::Net::HttpServer> m_server{};
|
||||
std::unique_ptr<Void::Net::HttpServer> m_admin_server{};
|
||||
std::unique_ptr<ServiceDirectory> m_directory{};
|
||||
std::string m_discovery_error{};
|
||||
ApplicationState m_state{ApplicationState::Created};
|
||||
std::uint64_t m_generation{0};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Discovery/Catalog.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
struct ServiceDirectoryConfig {
|
||||
std::string service_name{"networked-kv"};
|
||||
/// Stable instance identity; required when the directory is enabled.
|
||||
std::string instance_id{};
|
||||
std::string advertise_host{"127.0.0.1"};
|
||||
std::string scheme{"http"};
|
||||
std::uint32_t weight{1};
|
||||
};
|
||||
|
||||
/// Thin adapter over the Discovery-owned `Catalog`.
|
||||
///
|
||||
/// The catalog is authoritative for registration, revision and resolution
|
||||
/// policy; this adapter only maps the reference service's endpoint into it.
|
||||
/// Discovery is an observation dependency: a provider that is stale or
|
||||
/// unavailable never stops the data plane and never counts as a quorum
|
||||
/// vote.
|
||||
class ServiceDirectory
|
||||
{
|
||||
public:
|
||||
|
||||
explicit ServiceDirectory(ServiceDirectoryConfig config = {});
|
||||
ServiceDirectory(const ServiceDirectory&) = delete;
|
||||
ServiceDirectory& operator=(const ServiceDirectory&) = delete;
|
||||
ServiceDirectory(ServiceDirectory&&) noexcept = default;
|
||||
ServiceDirectory& operator=(ServiceDirectory&&) noexcept = default;
|
||||
~ServiceDirectory();
|
||||
|
||||
/// Register the live data-plane endpoint. Idempotent while the
|
||||
/// previous registration is active.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
register_endpoint(std::uint16_t port);
|
||||
|
||||
void deregister() noexcept;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<
|
||||
std::vector<Void::Discovery::EndpointRecord>>
|
||||
resolve(std::string_view service,
|
||||
Void::Discovery::ResolvePolicy policy = {}) const;
|
||||
|
||||
/// Adapter hook for an unavailable/stale upstream provider.
|
||||
void set_provider_health(
|
||||
Void::Discovery::Freshness freshness,
|
||||
std::optional<Void::Discovery::Error> provider_error =
|
||||
std::nullopt) noexcept;
|
||||
|
||||
[[nodiscard]] std::size_t active_records() const noexcept;
|
||||
[[nodiscard]] std::uint64_t revision() const noexcept;
|
||||
[[nodiscard]] bool registered() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
ServiceDirectoryConfig m_config;
|
||||
Void::Discovery::Catalog m_catalog;
|
||||
Void::Discovery::Registration m_registration;
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user