NetworkedApplications: Discovery-backed service directory with partition policy and optional registration (P-NETWORKED-08)

This commit is contained in:
faumaray committed 2026-12-04 10:42:44 +03:00
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;
}