136 lines
3.2 KiB
C++
136 lines
3.2 KiB
C++
#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
|