ReloadableApplications: reload transaction and quiescence core
This commit is contained in:
1 parent
1f137f101d
commit
f9eeb34369
4 files changed
+494
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user