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
@@ -136,6 +136,11 @@ namespace NetworkedKv
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: shutdown_grace must not be negative")};
|
||||
}
|
||||
if (discovery_enabled && discovery_instance.empty()) {
|
||||
return std::unexpected{config_error(
|
||||
"NetworkedConfig: discovery_instance is required when "
|
||||
"discovery is enabled")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -148,6 +153,8 @@ namespace NetworkedKv
|
||||
, m_api(std::move(other.m_api))
|
||||
, m_server(std::move(other.m_server))
|
||||
, m_admin_server(std::move(other.m_admin_server))
|
||||
, m_directory(std::move(other.m_directory))
|
||||
, m_discovery_error(std::move(other.m_discovery_error))
|
||||
, m_state(other.m_state)
|
||||
{
|
||||
other.m_host = nullptr;
|
||||
@@ -167,6 +174,8 @@ namespace NetworkedKv
|
||||
m_api = std::move(other.m_api);
|
||||
m_server = std::move(other.m_server);
|
||||
m_admin_server = std::move(other.m_admin_server);
|
||||
m_directory = std::move(other.m_directory);
|
||||
m_discovery_error = std::move(other.m_discovery_error);
|
||||
m_state = other.m_state;
|
||||
other.m_host = nullptr;
|
||||
other.m_state = ApplicationState::Stopped;
|
||||
@@ -218,6 +227,13 @@ namespace NetworkedKv
|
||||
ApiConfig{.bearer_token = app.m_config.bearer_token,
|
||||
.max_body_bytes = app.m_config.max_request_bytes,
|
||||
.max_scan_limit = app.m_config.max_scan_page});
|
||||
if (app.m_config.discovery_enabled) {
|
||||
app.m_directory = std::make_unique<ServiceDirectory>(
|
||||
ServiceDirectoryConfig{
|
||||
.service_name = app.m_config.discovery_service,
|
||||
.instance_id = app.m_config.discovery_instance,
|
||||
.advertise_host = app.m_config.discovery_host});
|
||||
}
|
||||
return app;
|
||||
}
|
||||
|
||||
@@ -304,6 +320,17 @@ namespace NetworkedKv
|
||||
return started;
|
||||
}
|
||||
m_server = std::move(server);
|
||||
if (m_directory) {
|
||||
// Discovery is an observation dependency: a failed registration
|
||||
// is reported, never fatal for the data plane.
|
||||
if (auto registered =
|
||||
m_directory->register_endpoint(m_server->bound_port());
|
||||
!registered) {
|
||||
m_discovery_error = registered.error().message();
|
||||
} else {
|
||||
m_discovery_error.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The administrative listener is separate from the data plane and
|
||||
@@ -402,6 +429,9 @@ namespace NetworkedKv
|
||||
return Void::Runtime::ShutdownReport{};
|
||||
}
|
||||
m_state = ApplicationState::Stopping;
|
||||
if (m_directory) {
|
||||
m_directory->deregister();
|
||||
}
|
||||
if (m_server) {
|
||||
m_server->shutdown(m_config.shutdown_grace);
|
||||
m_server.reset();
|
||||
@@ -436,6 +466,9 @@ namespace NetworkedKv
|
||||
snapshot.started_workers = m_host->started_worker_count();
|
||||
}
|
||||
snapshot.bound_port = bound_port();
|
||||
snapshot.discovery_registered =
|
||||
m_directory != nullptr && m_directory->registered();
|
||||
snapshot.discovery_error = m_discovery_error;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#include "networked_kv/service_directory.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
ServiceDirectory::ServiceDirectory(ServiceDirectoryConfig config)
|
||||
: m_config(std::move(config))
|
||||
{
|
||||
}
|
||||
|
||||
ServiceDirectory::~ServiceDirectory()
|
||||
{
|
||||
deregister();
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
ServiceDirectory::register_endpoint(std::uint16_t port)
|
||||
{
|
||||
if (m_registration.active()) {
|
||||
return {};
|
||||
}
|
||||
if (m_config.instance_id.empty()) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::invalid_argument,
|
||||
"ServiceDirectory: instance_id is required"}};
|
||||
}
|
||||
if (port == 0) {
|
||||
return std::unexpected{Void::Result::Error{
|
||||
Void::Result::ErrorKind::invalid_argument,
|
||||
"ServiceDirectory: a bound port is required"}};
|
||||
}
|
||||
|
||||
Void::Discovery::EndpointRecord record;
|
||||
record.instance = m_config.instance_id;
|
||||
record.service = m_config.service_name;
|
||||
record.endpoint.scheme = m_config.scheme;
|
||||
record.endpoint.host = m_config.advertise_host;
|
||||
record.endpoint.port = port;
|
||||
record.weight = m_config.weight;
|
||||
record.ready = true;
|
||||
|
||||
auto registered = m_catalog.register_local(std::move(record));
|
||||
if (!registered) {
|
||||
return std::unexpected{std::move(registered).error()};
|
||||
}
|
||||
m_registration = std::move(*registered);
|
||||
return {};
|
||||
}
|
||||
|
||||
void ServiceDirectory::deregister() noexcept
|
||||
{
|
||||
if (m_registration.active()) {
|
||||
m_registration.release();
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<Void::Discovery::EndpointRecord>>
|
||||
ServiceDirectory::resolve(std::string_view service,
|
||||
Void::Discovery::ResolvePolicy policy) const
|
||||
{
|
||||
auto snapshot = m_catalog.resolve(service, policy);
|
||||
if (!snapshot) {
|
||||
return std::unexpected{std::move(snapshot).error()};
|
||||
}
|
||||
return std::move(snapshot->records);
|
||||
}
|
||||
|
||||
void ServiceDirectory::set_provider_health(
|
||||
Void::Discovery::Freshness freshness,
|
||||
std::optional<Void::Discovery::Error> provider_error) noexcept
|
||||
{
|
||||
m_catalog.set_provider_health(freshness, std::move(provider_error));
|
||||
}
|
||||
|
||||
std::size_t ServiceDirectory::active_records() const noexcept
|
||||
{
|
||||
return m_catalog.active_record_count();
|
||||
}
|
||||
|
||||
std::uint64_t ServiceDirectory::revision() const noexcept
|
||||
{
|
||||
return m_catalog.revision();
|
||||
}
|
||||
|
||||
bool ServiceDirectory::registered() const noexcept
|
||||
{
|
||||
return m_registration.active();
|
||||
}
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user