Merge netapp-admin-listener: separate administrative listener

This commit is contained in:
faumaray committed 2026-12-03 20:35:22 +03:00
commit 61582ea461
3 files changed
+105

No files matched your search

@@ -95,6 +95,8 @@ namespace NetworkedKv
}
/// Port the HTTP listener actually bound (0 when not listening).
[[nodiscard]] std::uint16_t bound_port() const noexcept;
/// Port the administrative listener bound (0 when not listening).
[[nodiscard]] std::uint16_t admin_bound_port() const noexcept;
private:
@@ -109,6 +111,7 @@ namespace NetworkedKv
std::unique_ptr<MemoryKvStore> m_store{};
std::unique_ptr<KvApi> m_api{};
std::unique_ptr<Void::Net::HttpServer> m_server{};
std::unique_ptr<Void::Net::HttpServer> m_admin_server{};
ApplicationState m_state{ApplicationState::Created};
};
} // namespace NetworkedKv
@@ -137,6 +137,7 @@ namespace NetworkedKv
, m_store(std::move(other.m_store))
, m_api(std::move(other.m_api))
, m_server(std::move(other.m_server))
, m_admin_server(std::move(other.m_admin_server))
, m_state(other.m_state)
{
other.m_host = nullptr;
@@ -155,6 +156,7 @@ namespace NetworkedKv
m_store = std::move(other.m_store);
m_api = std::move(other.m_api);
m_server = std::move(other.m_server);
m_admin_server = std::move(other.m_admin_server);
m_state = other.m_state;
other.m_host = nullptr;
other.m_state = ApplicationState::Stopped;
@@ -168,6 +170,10 @@ namespace NetworkedKv
m_server->shutdown(m_config.shutdown_grace);
m_server.reset();
}
if (m_admin_server) {
m_admin_server->shutdown(m_config.shutdown_grace);
m_admin_server.reset();
}
stop_owned_host();
}
@@ -277,6 +283,52 @@ namespace NetworkedKv
}
m_server = std::move(server);
}
// The administrative listener is separate from the data plane and
// only exposes bounded lifecycle and telemetry views.
if (!m_admin_server && !m_config.admin_address.empty()) {
std::string host;
std::uint16_t port = 0;
if (!parse_address(m_config.admin_address, host, port)) {
m_state = ApplicationState::Failed;
return std::unexpected{config_error(
"NetworkedApplication: admin_address must be host:port")};
}
Void::Net::HttpServerOptions options;
options.bind_host = std::move(host);
options.port = port;
options.max_concurrent_connections = 8;
auto admin = std::make_unique<Void::Net::HttpServer>(options);
auto add_admin = [&](const char* pattern) {
return admin->add_route(
Void::Net::Method::Get, pattern,
[this](const Void::Net::ServerRequest& request)
-> Void::Net::RouteResult {
return to_server_response(
m_api->admin(to_http_request(request)));
});
};
Void::Result::Error admin_error{};
bool admin_ok = true;
for (const char* pattern : {"/live", "/ready", "/metrics"}) {
if (!admin_ok) {
break;
}
if (auto added = add_admin(pattern); !added) {
admin_ok = false;
admin_error = std::move(added).error();
}
}
if (!admin_ok) {
m_state = ApplicationState::Failed;
return std::unexpected{std::move(admin_error)};
}
if (auto started = admin->start(); !started) {
m_state = ApplicationState::Failed;
return started;
}
m_admin_server = std::move(admin);
}
m_state = ApplicationState::Running;
return {};
}
@@ -296,6 +348,10 @@ namespace NetworkedKv
m_server->shutdown(m_config.shutdown_grace);
m_server.reset();
}
if (m_admin_server) {
m_admin_server->shutdown(m_config.shutdown_grace);
m_admin_server.reset();
}
if (m_resources.ownership == ResourceOwnership::Managed && m_host) {
auto report =
m_host->shutdown_blocking(deadline_after(m_config.shutdown_grace));
@@ -330,6 +386,11 @@ namespace NetworkedKv
return m_server ? m_server->bound_port() : 0;
}
std::uint16_t NetworkedApplication::admin_bound_port() const noexcept
{
return m_admin_server ? m_admin_server->bound_port() : 0;
}
void NetworkedApplication::stop_owned_host() noexcept
{
if (m_resources.ownership != ResourceOwnership::Managed ||
@@ -120,6 +120,46 @@ namespace
assert(app->stop().has_value());
assert(app->health().bound_port == 0);
}
void admin_listener_is_separate()
{
ApplicationResources resources;
resources.ownership = ResourceOwnership::Managed;
NetworkedConfig config;
config.listen_address = "127.0.0.1:0";
config.admin_address = "127.0.0.1:0";
config.bearer_token = "secret";
auto app = NetworkedApplication::create(resources, config);
assert(app.has_value());
assert(app->start().has_value());
const auto data_port = app->health().bound_port;
const auto admin_port = app->admin_bound_port();
assert(data_port != 0);
assert(admin_port != 0);
assert(admin_port != data_port);
Void::Net::HttpClient client;
const std::string admin_base =
"http://127.0.0.1:" + std::to_string(admin_port);
auto live = client.get(admin_base + "/live");
assert(live.has_value() && live->status == 200);
auto ready = client.get(admin_base + "/ready");
assert(ready.has_value() && ready->status == 200);
auto metrics = client.get(admin_base + "/metrics");
assert(metrics.has_value() && metrics->status == 200);
assert(metrics->body.find("networked_kv_requests_total") !=
std::string::npos);
// Administrative routes are not exposed on the data plane.
const std::string data_base =
"http://127.0.0.1:" + std::to_string(data_port);
auto not_admin = client.get(data_base + "/live");
assert(not_admin.has_value() && not_admin->status == 404);
assert(app->stop().has_value());
assert(app->admin_bound_port() == 0);
}
} // namespace
int main()
@@ -129,6 +169,7 @@ int main()
embedded_mode_borrows_the_host();
embedded_requires_a_host();
http_listener_serves_kv();
admin_listener_is_separate();
std::puts("networked-kv lifecycle ok");
return 0;
}