NetworkedApplications: live config generations and durable backup/restore (P-NETWORKED-05)

This commit is contained in:
faumaray committed 2026-12-04 10:13:31 +03:00
1 parent 3010b8a913
commit 03542d0d2f
7 files changed
+256 -4

No files matched your search

@@ -340,6 +340,39 @@ namespace NetworkedKv
m_admin_server = std::move(admin);
}
m_state = ApplicationState::Running;
++m_generation;
return {};
}
Void::Result::Result<void>
NetworkedApplication::reload(NetworkedConfig candidate)
{
if (auto valid = candidate.validate(); !valid) {
return valid;
}
if (m_state != ApplicationState::Running) {
return std::unexpected{state_error(
"NetworkedApplication: reload requires a running application")};
}
// Only policy that can change without rebinding listeners or reopening
// the backend is applied live. Everything else is a restart decision.
if (candidate.listen_address != m_config.listen_address ||
candidate.admin_address != m_config.admin_address ||
candidate.storage_path != m_config.storage_path ||
candidate.max_connections != m_config.max_connections ||
candidate.max_request_bytes != m_config.max_request_bytes ||
candidate.max_scan_page != m_config.max_scan_page) {
return std::unexpected{state_error(
"NetworkedApplication: listener, storage and limit changes "
"require a restart")};
}
// Publish the immutable generation only after every step succeeded.
// The API token swap is the single live mutation; there is no second
// listener to fail after it.
m_api->set_bearer_token(candidate.bearer_token);
m_config = std::move(candidate);
++m_generation;
return {};
}
@@ -401,6 +434,42 @@ namespace NetworkedKv
return m_admin_server ? m_admin_server->bound_port() : 0;
}
Void::Result::Result<void>
NetworkedApplication::backup_storage(const std::string& destination)
{
auto* durable = dynamic_cast<SqliteKvStore*>(m_store.get());
if (durable == nullptr) {
return std::unexpected{state_error(
"NetworkedApplication: backup requires the durable profile")};
}
if (destination.empty()) {
return std::unexpected{
config_error("NetworkedApplication: backup destination is empty")};
}
if (auto backed_up = durable->backup_to(destination); !backed_up) {
return std::unexpected{std::move(backed_up).error()};
}
return {};
}
Void::Result::Result<void>
NetworkedApplication::restore_storage(const std::string& source)
{
auto* durable = dynamic_cast<SqliteKvStore*>(m_store.get());
if (durable == nullptr) {
return std::unexpected{state_error(
"NetworkedApplication: restore requires the durable profile")};
}
if (source.empty()) {
return std::unexpected{
config_error("NetworkedApplication: restore source is empty")};
}
if (auto restored = durable->restore_from(source); !restored) {
return std::unexpected{std::move(restored).error()};
}
return {};
}
void NetworkedApplication::stop_owned_host() noexcept
{
if (m_resources.ownership != ResourceOwnership::Managed ||
@@ -3,6 +3,7 @@
#include <algorithm>
#include <charconv>
#include <format>
#include <mutex>
#include <string>
#include <utility>
@@ -185,10 +186,6 @@ namespace NetworkedKv
bool KvApi::authorized(const HttpRequest& request) const
{
// Fail closed: an empty configured token authorizes nothing.
if (m_config.bearer_token.empty()) {
return false;
}
const auto header = request.header("authorization");
if (!header) {
return false;
@@ -198,6 +195,11 @@ namespace NetworkedKv
!iequals(header->substr(0, scheme.size()), scheme)) {
return false;
}
std::lock_guard lock{m_config_mutex};
// Fail closed: an empty configured token authorizes nothing.
if (m_config.bearer_token.empty()) {
return false;
}
return constant_time_equal(header->substr(scheme.size()),
m_config.bearer_token);
}
@@ -475,3 +475,20 @@ namespace NetworkedKv
return out;
}
} // namespace NetworkedKv
namespace NetworkedKv
{
Void::Result::Result<Void::Sqlite::BackupReport>
SqliteKvStore::backup_to(const std::filesystem::path& destination)
{
std::lock_guard lock{m_mutex};
return m_db.backup_to(destination);
}
Void::Result::Result<Void::Sqlite::BackupReport>
SqliteKvStore::restore_from(const std::filesystem::path& source)
{
std::lock_guard lock{m_mutex};
return m_db.restore_from(source);
}
} // namespace NetworkedKv