ReloadableApplications: retained-resource ledger and failure-injection matrix; reject publish after failed drain (P-RELOADABLE-05)
This commit is contained in:
1 parent
2489098478
commit
2da3971760
5 files changed
+382
-1
No files matched your search
@@ -0,0 +1,87 @@
|
||||
#include "reloadable_host/resources.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace ReloadableHost
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error exhausted(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::exhausted,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Error conflict(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::conflict,
|
||||
std::move(message)};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
ResourceLedger::ResourceLedger(ResourceLimits limits)
|
||||
: m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
ResourceLedger::reserve(std::uint64_t generation, std::size_t bytes)
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (m_reserved.contains(generation)) {
|
||||
return std::unexpected{
|
||||
conflict("ResourceLedger: generation is already reserved")};
|
||||
}
|
||||
if (bytes > m_limits.max_generation_bytes) {
|
||||
return std::unexpected{exhausted(
|
||||
"ResourceLedger: generation exceeds max_generation_bytes")};
|
||||
}
|
||||
if (m_reserved.size() >= m_limits.max_generations) {
|
||||
return std::unexpected{exhausted(
|
||||
"ResourceLedger: retained generation cap reached; release "
|
||||
"pinned generations before reloading")};
|
||||
}
|
||||
if (bytes > m_limits.max_total_bytes - m_total_bytes) {
|
||||
return std::unexpected{exhausted(
|
||||
"ResourceLedger: aggregate retained budget reached")};
|
||||
}
|
||||
m_reserved.emplace(generation, bytes);
|
||||
m_total_bytes += bytes;
|
||||
m_peak.total_bytes = std::max(m_peak.total_bytes, m_total_bytes);
|
||||
m_peak.generations =
|
||||
std::max(m_peak.generations, m_reserved.size());
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ResourceLedger::release(std::uint64_t generation) noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_reserved.find(generation);
|
||||
if (found == m_reserved.end()) {
|
||||
return false;
|
||||
}
|
||||
m_total_bytes -= found->second;
|
||||
m_reserved.erase(found);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourceLedger::holds(std::uint64_t generation) const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_reserved.contains(generation);
|
||||
}
|
||||
|
||||
ResourceUsage ResourceLedger::usage() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return ResourceUsage{m_total_bytes, m_reserved.size()};
|
||||
}
|
||||
|
||||
ResourceUsage ResourceLedger::peak_usage() const noexcept
|
||||
{
|
||||
std::lock_guard lock{m_mutex};
|
||||
return m_peak;
|
||||
}
|
||||
} // namespace ReloadableHost
|
||||
Reference in new issue
Block a user