Merge relapp-core: reload transaction and quiescence core
This commit is contained in:
commit
6b9ccc5e9a
4 files changed
+494
No files matched your search
@@ -0,0 +1,18 @@
|
|||||||
|
cmake_minimum_required(VERSION 4.0)
|
||||||
|
project(ReloadableHost LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 26)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
find_package(Void CONFIG REQUIRED COMPONENTS Functional)
|
||||||
|
|
||||||
|
add_library(reloadable_host STATIC src/reload_transaction.cpp)
|
||||||
|
target_include_directories(reloadable_host PUBLIC include)
|
||||||
|
target_link_libraries(reloadable_host PUBLIC Void::Functional)
|
||||||
|
|
||||||
|
add_executable(reloadable_host_test tests/reload_transaction_test.cpp)
|
||||||
|
target_link_libraries(reloadable_host_test PRIVATE reloadable_host)
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
add_test(NAME reloadable_host_transaction COMMAND reloadable_host_test)
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Functional/Result.hpp>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace ReloadableHost
|
||||||
|
{
|
||||||
|
using TransactionId = std::uint64_t;
|
||||||
|
|
||||||
|
enum class ReloadPhase : std::uint8_t {
|
||||||
|
Idle,
|
||||||
|
Validating,
|
||||||
|
Preparing,
|
||||||
|
Draining,
|
||||||
|
Publishing,
|
||||||
|
Committing,
|
||||||
|
Completed,
|
||||||
|
RolledBack,
|
||||||
|
Failed,
|
||||||
|
CancelledBeforePublish,
|
||||||
|
UnknownOutcome
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class ReloadOutcome : std::uint8_t {
|
||||||
|
None,
|
||||||
|
Published,
|
||||||
|
RolledBack,
|
||||||
|
Failed,
|
||||||
|
UnknownOutcome
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ReloadReport {
|
||||||
|
TransactionId id{0};
|
||||||
|
ReloadPhase phase{ReloadPhase::Idle};
|
||||||
|
ReloadOutcome outcome{ReloadOutcome::None};
|
||||||
|
std::string candidate;
|
||||||
|
std::string error;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Immutable candidate release identifier and payload location.
|
||||||
|
struct ReleaseCandidate {
|
||||||
|
std::string id;
|
||||||
|
std::string path;
|
||||||
|
std::uint64_t generation{0};
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Host-supplied trust/checksum validation boundary.
|
||||||
|
using Validator =
|
||||||
|
std::function<Void::Result::Result<void>(const ReleaseCandidate&)>;
|
||||||
|
|
||||||
|
struct ReloadLimits {
|
||||||
|
std::chrono::milliseconds drain_deadline{std::chrono::seconds{30}};
|
||||||
|
std::size_t max_pins{1024};
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Tracks in-flight application calls so a reload drains before publishing.
|
||||||
|
class Quiescence
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit Quiescence(ReloadLimits limits = {});
|
||||||
|
|
||||||
|
/// Admit a call. Fails while draining or at the pin cap.
|
||||||
|
[[nodiscard]] Void::Result::Result<void> admit();
|
||||||
|
void release() noexcept;
|
||||||
|
|
||||||
|
/// Stop admissions and wait until pins drain or the deadline
|
||||||
|
/// expires. Returns false on timeout (still draining).
|
||||||
|
[[nodiscard]] bool drain();
|
||||||
|
|
||||||
|
/// Resume admissions after a rollback.
|
||||||
|
void resume() noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t pins() const noexcept;
|
||||||
|
[[nodiscard]] bool draining() const noexcept;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
mutable std::mutex m_mutex;
|
||||||
|
std::condition_variable m_cv;
|
||||||
|
ReloadLimits m_limits;
|
||||||
|
std::size_t m_pins{0};
|
||||||
|
bool m_draining{false};
|
||||||
|
};
|
||||||
|
|
||||||
|
/// One authoritative reload transaction.
|
||||||
|
///
|
||||||
|
/// Exactly one outcome wins. Publication is the point of no return:
|
||||||
|
/// cancellation before it rolls back, after it the outcome stays
|
||||||
|
/// published, and a lost durable acknowledgement is UnknownOutcome rather
|
||||||
|
/// than a false rollback.
|
||||||
|
class ReloadTransaction
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit ReloadTransaction(TransactionId id);
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
prepare(const ReleaseCandidate& candidate,
|
||||||
|
const Validator& validate);
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
drain(Quiescence& quiescence);
|
||||||
|
|
||||||
|
/// Publish the prepared candidate. Only valid after a successful
|
||||||
|
/// drain.
|
||||||
|
[[nodiscard]] Void::Result::Result<void> publish();
|
||||||
|
|
||||||
|
/// Record the durable healthy commit. `durable == false` models a
|
||||||
|
/// lost acknowledgement: the outcome is Unknown.
|
||||||
|
void commit(bool durable) noexcept;
|
||||||
|
|
||||||
|
/// Cancel before publication. A no-op once published.
|
||||||
|
void cancel() noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] ReloadReport report() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
mutable std::mutex m_mutex;
|
||||||
|
TransactionId m_id;
|
||||||
|
ReloadPhase m_phase{ReloadPhase::Idle};
|
||||||
|
ReloadOutcome m_outcome{ReloadOutcome::None};
|
||||||
|
std::optional<ReleaseCandidate> m_candidate;
|
||||||
|
std::string m_error;
|
||||||
|
};
|
||||||
|
} // namespace ReloadableHost
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
#include "reloadable_host/reload_transaction.hpp"
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace ReloadableHost
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
[[nodiscard]] Void::Result::Error state_error(std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||||
|
std::move(message)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Error limit_error(std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{Void::Result::ErrorKind::exhausted,
|
||||||
|
std::move(message)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Error invalid_candidate(
|
||||||
|
std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||||
|
std::move(message)};
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Quiescence::Quiescence(ReloadLimits limits)
|
||||||
|
: m_limits(limits)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> Quiescence::admit()
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (m_draining) {
|
||||||
|
return std::unexpected{
|
||||||
|
state_error("Quiescence: admissions are closed while draining")};
|
||||||
|
}
|
||||||
|
if (m_pins >= m_limits.max_pins) {
|
||||||
|
return std::unexpected{
|
||||||
|
limit_error("Quiescence: pin cap reached")};
|
||||||
|
}
|
||||||
|
++m_pins;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Quiescence::release() noexcept
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (m_pins > 0) {
|
||||||
|
--m_pins;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_cv.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Quiescence::drain()
|
||||||
|
{
|
||||||
|
std::unique_lock lock{m_mutex};
|
||||||
|
m_draining = true;
|
||||||
|
return m_cv.wait_for(lock, m_limits.drain_deadline,
|
||||||
|
[this] { return m_pins == 0; });
|
||||||
|
}
|
||||||
|
|
||||||
|
void Quiescence::resume() noexcept
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
m_draining = false;
|
||||||
|
}
|
||||||
|
m_cv.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t Quiescence::pins() const noexcept
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
return m_pins;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Quiescence::draining() const noexcept
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
return m_draining;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReloadTransaction::ReloadTransaction(TransactionId id)
|
||||||
|
: m_id(id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void>
|
||||||
|
ReloadTransaction::prepare(const ReleaseCandidate& candidate,
|
||||||
|
const Validator& validate)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (m_phase != ReloadPhase::Idle) {
|
||||||
|
return std::unexpected{state_error(
|
||||||
|
"ReloadTransaction: prepare requires an idle transaction")};
|
||||||
|
}
|
||||||
|
m_phase = ReloadPhase::Validating;
|
||||||
|
m_candidate = candidate;
|
||||||
|
m_error.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> validated;
|
||||||
|
try {
|
||||||
|
validated = validate ? validate(candidate) : Void::Result::Result<void>{};
|
||||||
|
} catch (const std::exception& error) {
|
||||||
|
validated = std::unexpected{
|
||||||
|
invalid_candidate(std::string{"validator threw: "} + error.what())};
|
||||||
|
} catch (...) {
|
||||||
|
validated = std::unexpected{
|
||||||
|
invalid_candidate("validator threw an unknown exception")};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (!validated) {
|
||||||
|
m_phase = ReloadPhase::Failed;
|
||||||
|
m_outcome = ReloadOutcome::Failed;
|
||||||
|
m_error = validated.error().message();
|
||||||
|
return std::unexpected{validated.error()};
|
||||||
|
}
|
||||||
|
m_phase = ReloadPhase::Preparing;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> ReloadTransaction::drain(Quiescence& quiescence)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (m_phase != ReloadPhase::Preparing) {
|
||||||
|
return std::unexpected{state_error(
|
||||||
|
"ReloadTransaction: drain requires a prepared transaction")};
|
||||||
|
}
|
||||||
|
m_phase = ReloadPhase::Draining;
|
||||||
|
}
|
||||||
|
|
||||||
|
const bool quiesced = quiescence.drain();
|
||||||
|
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (!quiesced) {
|
||||||
|
m_error = "drain deadline expired with pinned calls";
|
||||||
|
return std::unexpected{
|
||||||
|
Void::Result::Error{Void::Result::ErrorKind::timeout, m_error}};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> ReloadTransaction::publish()
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (m_phase != ReloadPhase::Draining) {
|
||||||
|
return std::unexpected{state_error(
|
||||||
|
"ReloadTransaction: publish requires a drained transaction")};
|
||||||
|
}
|
||||||
|
// Point of no return: from here a cancel() cannot roll back.
|
||||||
|
m_phase = ReloadPhase::Committing;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReloadTransaction::commit(bool durable) noexcept
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
if (m_phase != ReloadPhase::Committing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (durable) {
|
||||||
|
m_phase = ReloadPhase::Completed;
|
||||||
|
m_outcome = ReloadOutcome::Published;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// The publication may be live but the durable acknowledgement was
|
||||||
|
// lost: never report a rollback.
|
||||||
|
m_phase = ReloadPhase::UnknownOutcome;
|
||||||
|
m_outcome = ReloadOutcome::UnknownOutcome;
|
||||||
|
m_error = "durable healthy record not acknowledged";
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReloadTransaction::cancel() noexcept
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
switch (m_phase) {
|
||||||
|
case ReloadPhase::Committing:
|
||||||
|
case ReloadPhase::Completed:
|
||||||
|
case ReloadPhase::UnknownOutcome:
|
||||||
|
return; // published: cannot undo
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
m_phase = ReloadPhase::CancelledBeforePublish;
|
||||||
|
m_outcome = ReloadOutcome::RolledBack;
|
||||||
|
}
|
||||||
|
|
||||||
|
ReloadReport ReloadTransaction::report() const
|
||||||
|
{
|
||||||
|
std::lock_guard lock{m_mutex};
|
||||||
|
ReloadReport report;
|
||||||
|
report.id = m_id;
|
||||||
|
report.phase = m_phase;
|
||||||
|
report.outcome = m_outcome;
|
||||||
|
if (m_candidate) {
|
||||||
|
report.candidate = m_candidate->id;
|
||||||
|
}
|
||||||
|
report.error = m_error;
|
||||||
|
return report;
|
||||||
|
}
|
||||||
|
} // namespace ReloadableHost
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
#include "reloadable_host/reload_transaction.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
using namespace ReloadableHost;
|
||||||
|
|
||||||
|
Void::Result::Result<void> accept_all(const ReleaseCandidate&)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> reject_all(const ReleaseCandidate&)
|
||||||
|
{
|
||||||
|
return std::unexpected{
|
||||||
|
Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||||
|
"release failed validation"}};
|
||||||
|
}
|
||||||
|
|
||||||
|
void happy_path_stays_published()
|
||||||
|
{
|
||||||
|
ReloadTransaction tx{1};
|
||||||
|
const ReleaseCandidate candidate{"rel-a", "/releases/a", 2};
|
||||||
|
assert(tx.prepare(candidate, accept_all).has_value());
|
||||||
|
Quiescence quiescence;
|
||||||
|
assert(tx.drain(quiescence).has_value());
|
||||||
|
assert(tx.publish().has_value());
|
||||||
|
tx.commit(true);
|
||||||
|
|
||||||
|
const auto report = tx.report();
|
||||||
|
assert(report.id == 1);
|
||||||
|
assert(report.phase == ReloadPhase::Completed);
|
||||||
|
assert(report.outcome == ReloadOutcome::Published);
|
||||||
|
assert(report.candidate == "rel-a");
|
||||||
|
quiescence.resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancel_before_publish_rolls_back()
|
||||||
|
{
|
||||||
|
ReloadTransaction tx{2};
|
||||||
|
assert(tx.prepare({"rel-b", "/releases/b", 1}, accept_all)
|
||||||
|
.has_value());
|
||||||
|
tx.cancel();
|
||||||
|
assert(tx.report().outcome == ReloadOutcome::RolledBack);
|
||||||
|
assert(tx.report().phase == ReloadPhase::CancelledBeforePublish);
|
||||||
|
|
||||||
|
Quiescence quiescence;
|
||||||
|
assert(!tx.drain(quiescence).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancel_after_publish_is_a_noop()
|
||||||
|
{
|
||||||
|
ReloadTransaction tx{3};
|
||||||
|
assert(tx.prepare({"rel-c", "/releases/c", 1}, accept_all)
|
||||||
|
.has_value());
|
||||||
|
Quiescence quiescence;
|
||||||
|
assert(tx.drain(quiescence).has_value());
|
||||||
|
assert(tx.publish().has_value());
|
||||||
|
|
||||||
|
tx.cancel();
|
||||||
|
assert(tx.report().phase == ReloadPhase::Committing);
|
||||||
|
|
||||||
|
// A lost durable acknowledgement is Unknown, never RolledBack.
|
||||||
|
tx.commit(false);
|
||||||
|
assert(tx.report().phase == ReloadPhase::UnknownOutcome);
|
||||||
|
assert(tx.report().outcome == ReloadOutcome::UnknownOutcome);
|
||||||
|
quiescence.resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
void invalid_candidate_fails_closed()
|
||||||
|
{
|
||||||
|
ReloadTransaction tx{4};
|
||||||
|
assert(!tx.prepare({"rel-d", "/releases/d", 1}, reject_all)
|
||||||
|
.has_value());
|
||||||
|
assert(tx.report().phase == ReloadPhase::Failed);
|
||||||
|
assert(tx.report().outcome == ReloadOutcome::Failed);
|
||||||
|
}
|
||||||
|
|
||||||
|
void quiescence_blocks_admission_and_drains()
|
||||||
|
{
|
||||||
|
Quiescence quiescence{ReloadLimits{
|
||||||
|
.drain_deadline = std::chrono::milliseconds{10},
|
||||||
|
.max_pins = 2}};
|
||||||
|
|
||||||
|
assert(quiescence.admit().has_value());
|
||||||
|
assert(quiescence.admit().has_value());
|
||||||
|
// Pin cap.
|
||||||
|
assert(!quiescence.admit().has_value());
|
||||||
|
|
||||||
|
// Pinned calls hold the drain until the deadline.
|
||||||
|
assert(!quiescence.drain());
|
||||||
|
assert(quiescence.draining());
|
||||||
|
|
||||||
|
quiescence.release();
|
||||||
|
quiescence.release();
|
||||||
|
assert(quiescence.drain());
|
||||||
|
// Admissions stay closed while draining.
|
||||||
|
assert(!quiescence.admit().has_value());
|
||||||
|
|
||||||
|
quiescence.resume();
|
||||||
|
assert(quiescence.admit().has_value());
|
||||||
|
quiescence.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
void invalid_transitions_are_rejected()
|
||||||
|
{
|
||||||
|
ReloadTransaction tx{5};
|
||||||
|
Quiescence quiescence;
|
||||||
|
assert(!tx.publish().has_value());
|
||||||
|
assert(!tx.drain(quiescence).has_value());
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
happy_path_stays_published();
|
||||||
|
cancel_before_publish_rolls_back();
|
||||||
|
cancel_after_publish_is_a_noop();
|
||||||
|
invalid_candidate_fails_closed();
|
||||||
|
quiescence_blocks_admission_and_drains();
|
||||||
|
invalid_transitions_are_rejected();
|
||||||
|
std::puts("reloadable-host transaction ok");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user