NetworkedApplications: live config generations and durable backup/restore (P-NETWORKED-05)
This commit is contained in:
1 parent
3010b8a913
commit
03542d0d2f
7 files changed
+256
-4
No files matched your search
@@ -95,11 +95,34 @@ namespace NetworkedKv
|
||||
{
|
||||
return m_config;
|
||||
}
|
||||
/// Generation of the live configuration (starts at 1 after the first
|
||||
/// successful start; incremented by each applied reload).
|
||||
[[nodiscard]] std::uint64_t config_generation() const noexcept
|
||||
{
|
||||
return m_generation;
|
||||
}
|
||||
/// Parse/validate a complete candidate and apply the live-reloadable
|
||||
/// parts atomically: the bearer token and readiness gating. Listener
|
||||
/// addresses, backend selection, storage path and limits require a
|
||||
/// restart and are rejected with a conflict error while the
|
||||
/// application is running, leaving the old generation untouched.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
reload(NetworkedConfig candidate);
|
||||
/// 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;
|
||||
|
||||
/// Consistent operator backup of the durable profile. Fails when
|
||||
/// the in-memory profile is active.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
backup_storage(const std::string& destination);
|
||||
|
||||
/// Restore the durable profile from a backup. Quiesce clients first;
|
||||
/// the caller owns re-validation before routing traffic again.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
restore_storage(const std::string& source);
|
||||
|
||||
private:
|
||||
|
||||
NetworkedApplication() = default;
|
||||
@@ -115,5 +138,6 @@ namespace NetworkedKv
|
||||
std::unique_ptr<Void::Net::HttpServer> m_server{};
|
||||
std::unique_ptr<Void::Net::HttpServer> m_admin_server{};
|
||||
ApplicationState m_state{ApplicationState::Created};
|
||||
std::uint64_t m_generation{0};
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -61,6 +62,14 @@ namespace NetworkedKv
|
||||
|
||||
void set_ready(bool ready) noexcept { m_ready = ready; }
|
||||
|
||||
/// Atomically replace the live-reloadable bearer token. In-flight
|
||||
/// requests read either the old or the new token, never a torn mix.
|
||||
void set_bearer_token(std::string token)
|
||||
{
|
||||
std::lock_guard lock{m_config_mutex};
|
||||
m_config.bearer_token = std::move(token);
|
||||
}
|
||||
|
||||
[[nodiscard]] ApiMetrics metrics() const noexcept { return m_metrics; }
|
||||
|
||||
private:
|
||||
@@ -78,6 +87,7 @@ namespace NetworkedKv
|
||||
|
||||
KvStore& m_store;
|
||||
ApiConfig m_config;
|
||||
mutable std::mutex m_config_mutex;
|
||||
bool m_ready{true};
|
||||
ApiMetrics m_metrics;
|
||||
};
|
||||
|
||||
@@ -49,6 +49,17 @@ namespace NetworkedKv
|
||||
return m_limits;
|
||||
}
|
||||
|
||||
/// Consistent operator backup through SQLite's online backup API.
|
||||
/// `destination` must not be the live database file.
|
||||
[[nodiscard]] Void::Result::Result<Void::Sqlite::BackupReport>
|
||||
backup_to(const std::filesystem::path& destination);
|
||||
|
||||
/// Restore this connection from a verified backup file. Callers
|
||||
/// must quiesce application traffic first; run domain checks before
|
||||
/// routing traffic to the restored contents.
|
||||
[[nodiscard]] Void::Result::Result<Void::Sqlite::BackupReport>
|
||||
restore_from(const std::filesystem::path& source);
|
||||
|
||||
private:
|
||||
|
||||
SqliteKvStore(Void::Sqlite::Database database, Limits limits);
|
||||
|
||||
@@ -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
|
||||
@@ -224,6 +224,123 @@ namespace
|
||||
|
||||
std::filesystem::remove_all(directory);
|
||||
}
|
||||
|
||||
void config_reload_publishes_a_new_generation()
|
||||
{
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Managed;
|
||||
NetworkedConfig config;
|
||||
config.listen_address = "127.0.0.1:0";
|
||||
config.bearer_token = "secret";
|
||||
auto app = NetworkedApplication::create(resources, config);
|
||||
assert(app.has_value());
|
||||
|
||||
// Reload before start is rejected and publishes nothing.
|
||||
assert(!app->reload(config).has_value());
|
||||
assert(app->config_generation() == 0);
|
||||
|
||||
assert(app->start().has_value());
|
||||
assert(app->config_generation() == 1);
|
||||
const auto port = app->health().bound_port;
|
||||
const std::string base =
|
||||
"http://127.0.0.1:" + std::to_string(port);
|
||||
Void::Net::HttpClient client;
|
||||
|
||||
// A restart-required change is rejected atomically.
|
||||
NetworkedConfig rebound = app->config();
|
||||
rebound.listen_address = "127.0.0.1:1";
|
||||
auto rejected = app->reload(rebound);
|
||||
assert(!rejected.has_value());
|
||||
assert(app->config_generation() == 1);
|
||||
assert(app->config().listen_address == "127.0.0.1:0");
|
||||
assert(app->health().bound_port == port);
|
||||
|
||||
// An invalid candidate is rejected before anything is applied.
|
||||
NetworkedConfig invalid = app->config();
|
||||
invalid.max_scan_page = 0;
|
||||
assert(!app->reload(invalid).has_value());
|
||||
assert(app->config_generation() == 1);
|
||||
|
||||
// The bearer token rotates live without rebinding the listener.
|
||||
NetworkedConfig rotated = app->config();
|
||||
rotated.bearer_token = "rotated";
|
||||
assert(app->reload(std::move(rotated)).has_value());
|
||||
assert(app->config_generation() == 2);
|
||||
assert(app->health().bound_port == port);
|
||||
|
||||
auto old_token = client.get(
|
||||
base + "/v1/kv/a",
|
||||
{Void::Net::Header{"authorization", "Bearer secret"}});
|
||||
assert(old_token.has_value());
|
||||
assert(old_token->status == 401);
|
||||
auto new_token = client.get(
|
||||
base + "/v1/kv/a",
|
||||
{Void::Net::Header{"authorization", "Bearer rotated"}});
|
||||
assert(new_token.has_value());
|
||||
assert(new_token->status == 404);
|
||||
|
||||
assert(app->stop().has_value());
|
||||
}
|
||||
|
||||
void durable_backup_and_restore_roundtrip()
|
||||
{
|
||||
const auto directory = std::filesystem::temp_directory_path() /
|
||||
("void-netapp-backup-" + std::to_string(::getpid()));
|
||||
std::filesystem::remove_all(directory);
|
||||
std::filesystem::create_directories(directory);
|
||||
const std::string path = (directory / "kv.db").string();
|
||||
const std::string backup = (directory / "kv.backup.db").string();
|
||||
|
||||
{
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Managed;
|
||||
NetworkedConfig config;
|
||||
config.listen_address = "127.0.0.1:0";
|
||||
config.bearer_token = "secret";
|
||||
config.storage_path = path;
|
||||
auto app = NetworkedApplication::create(resources, config);
|
||||
assert(app.has_value());
|
||||
assert(app->start().has_value());
|
||||
|
||||
Void::Net::HttpClient client;
|
||||
const std::string base =
|
||||
"http://127.0.0.1:" + std::to_string(app->health().bound_port);
|
||||
Void::Net::Request put;
|
||||
put.method = Void::Net::Method::Put;
|
||||
put.url = base + "/v1/kv/backed-up";
|
||||
put.body = std::string{"value"};
|
||||
put.headers.push_back(
|
||||
Void::Net::Header{"authorization", "Bearer secret"});
|
||||
put.headers.push_back(Void::Net::Header{"x-request-id", "b1"});
|
||||
put.headers.push_back(Void::Net::Header{"if-none-match", "*"});
|
||||
auto created = client.send(put);
|
||||
assert(created.has_value());
|
||||
assert(created->status == 200);
|
||||
|
||||
assert(app->backup_storage(backup).has_value());
|
||||
assert(std::filesystem::exists(backup));
|
||||
|
||||
// Mutate after the backup so restore is observable.
|
||||
Void::Net::Request overwrite = put;
|
||||
overwrite.body = std::string{"changed"};
|
||||
overwrite.headers[1].value = "b2";
|
||||
overwrite.headers[2] = Void::Net::Header{"if-match", "\"1\""};
|
||||
auto updated = client.send(overwrite);
|
||||
assert(updated.has_value());
|
||||
assert(updated->status == 200);
|
||||
|
||||
assert(app->restore_storage(backup).has_value());
|
||||
auto got = client.get(
|
||||
base + "/v1/kv/backed-up",
|
||||
{Void::Net::Header{"authorization", "Bearer secret"}});
|
||||
assert(got.has_value());
|
||||
assert(got->status == 200);
|
||||
assert(got->body == "value");
|
||||
assert(app->stop().has_value());
|
||||
}
|
||||
|
||||
std::filesystem::remove_all(directory);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
@@ -235,6 +352,8 @@ int main()
|
||||
http_listener_serves_kv();
|
||||
admin_listener_is_separate();
|
||||
durable_profile_survives_restart();
|
||||
config_reload_publishes_a_new_generation();
|
||||
durable_backup_and_restore_roundtrip();
|
||||
std::puts("networked-kv lifecycle ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user