Merge relapp-checkpoint: versioned checkpoint migration

This commit is contained in:
faumaray committed 2026-12-04 00:51:30 +03:00
commit 1bd729147f
4 files changed
+305

No files matched your search

+5
View File
@@ -8,6 +8,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Void CONFIG REQUIRED COMPONENTS Functional)
add_library(reloadable_host STATIC
src/checkpoint.cpp
src/control.cpp
src/release_registry.cpp
src/reload_transaction.cpp)
@@ -23,7 +24,11 @@ target_link_libraries(reloadable_host_release_test PRIVATE reloadable_host)
add_executable(reloadable_host_control_test tests/control_test.cpp)
target_link_libraries(reloadable_host_control_test PRIVATE reloadable_host)
add_executable(reloadable_host_checkpoint_test tests/checkpoint_test.cpp)
target_link_libraries(reloadable_host_checkpoint_test PRIVATE reloadable_host)
enable_testing()
add_test(NAME reloadable_host_transaction COMMAND reloadable_host_test)
add_test(NAME reloadable_host_release COMMAND reloadable_host_release_test)
add_test(NAME reloadable_host_control COMMAND reloadable_host_control_test)
add_test(NAME reloadable_host_checkpoint COMMAND reloadable_host_checkpoint_test)
@@ -0,0 +1,40 @@
#pragma once
#include <Void/Functional/Result.hpp>
#include <cstddef>
#include <cstdint>
#include <span>
#include <string>
#include <vector>
namespace ReloadableHost
{
inline constexpr std::uint32_t kCurrentCheckpointVersion{3};
/// Durable workflow state carried across releases.
struct CheckpointState {
std::uint64_t counter{0};
std::string workflow;
std::uint64_t generation{0};
};
struct CheckpointLimits {
std::size_t max_bytes{64ULL * 1024ULL};
std::size_t max_workflow_bytes{4ULL * 1024ULL};
};
/// Encode the current checkpoint schema (bounded).
[[nodiscard]] Void::Result::Result<std::vector<std::byte>>
encode_checkpoint(const CheckpointState& state,
CheckpointLimits limits = {});
/// Decode a supported checkpoint and migrate it to the current schema.
///
/// v1 (counter) and v2 (counter + workflow) are migrated forward with
/// defaults for fields they did not have. Corrupt magic, truncation,
/// oversized payloads and unknown newer versions are rejected.
[[nodiscard]] Void::Result::Result<CheckpointState>
decode_checkpoint(std::span<const std::byte> bytes,
CheckpointLimits limits = {});
} // namespace ReloadableHost
+143
View File
@@ -0,0 +1,143 @@
#include "reloadable_host/checkpoint.hpp"
#include <algorithm>
#include <array>
#include <string>
#include <utility>
namespace ReloadableHost
{
namespace
{
constexpr std::array<std::byte, 4> kMagic{
std::byte{'V'}, std::byte{'C'}, std::byte{'H'}, std::byte{'K'}};
[[nodiscard]] Void::Result::Error invalid(std::string message)
{
return Void::Result::Error{Void::Result::ErrorKind::corrupt,
std::move(message)};
}
void append_u32(std::vector<std::byte>& out, std::uint32_t value)
{
for (int shift = 24; shift >= 0; shift -= 8) {
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
}
}
void append_u64(std::vector<std::byte>& out, std::uint64_t value)
{
for (int shift = 56; shift >= 0; shift -= 8) {
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
}
}
[[nodiscard]] bool read_u32(std::span<const std::byte> in,
std::size_t& pos,
std::uint32_t& value) noexcept
{
if (pos + 4 > in.size()) {
return false;
}
value = 0;
for (int i = 0; i < 4; ++i) {
value = (value << 8) |
std::to_integer<std::uint32_t>(in[pos + i]);
}
pos += 4;
return true;
}
[[nodiscard]] bool read_u64(std::span<const std::byte> in,
std::size_t& pos,
std::uint64_t& value) noexcept
{
if (pos + 8 > in.size()) {
return false;
}
value = 0;
for (int i = 0; i < 8; ++i) {
value = (value << 8) |
std::to_integer<std::uint64_t>(in[pos + i]);
}
pos += 8;
return true;
}
} // namespace
Void::Result::Result<std::vector<std::byte>>
encode_checkpoint(const CheckpointState& state, CheckpointLimits limits)
{
if (state.workflow.size() > limits.max_workflow_bytes ||
state.workflow.size() > 0xFFFFFFFFULL) {
return std::unexpected{
invalid("Checkpoint: workflow exceeds the size limit")};
}
std::vector<std::byte> out;
out.reserve(4 + 4 + 8 + 4 + state.workflow.size() + 8);
out.insert(out.end(), kMagic.begin(), kMagic.end());
append_u32(out, kCurrentCheckpointVersion);
append_u64(out, state.counter);
append_u32(out, static_cast<std::uint32_t>(state.workflow.size()));
for (const char ch : state.workflow) {
out.push_back(static_cast<std::byte>(ch));
}
append_u64(out, state.generation);
if (out.size() > limits.max_bytes) {
return std::unexpected{
invalid("Checkpoint: encoded state exceeds the size limit")};
}
return out;
}
Void::Result::Result<CheckpointState>
decode_checkpoint(std::span<const std::byte> bytes,
CheckpointLimits limits)
{
if (bytes.size() > limits.max_bytes ||
bytes.size() < kMagic.size() + 4 + 8) {
return std::unexpected{
invalid("Checkpoint: truncated or oversized payload")};
}
if (!std::equal(kMagic.begin(), kMagic.end(), bytes.begin())) {
return std::unexpected{invalid("Checkpoint: bad magic")};
}
std::size_t pos = kMagic.size();
std::uint32_t version = 0;
if (!read_u32(bytes, pos, version)) {
return std::unexpected{invalid("Checkpoint: truncated version")};
}
if (version == 0 || version > kCurrentCheckpointVersion) {
return std::unexpected{invalid(
"Checkpoint: unsupported schema version")};
}
CheckpointState state;
if (!read_u64(bytes, pos, state.counter)) {
return std::unexpected{invalid("Checkpoint: truncated counter")};
}
if (version >= 2) {
std::uint32_t workflow_size = 0;
if (!read_u32(bytes, pos, workflow_size) ||
workflow_size > limits.max_workflow_bytes ||
pos + workflow_size > bytes.size()) {
return std::unexpected{
invalid("Checkpoint: invalid workflow payload")};
}
state.workflow.assign(
reinterpret_cast<const char*>(bytes.data() + pos),
workflow_size);
pos += workflow_size;
}
if (version >= 3 && !read_u64(bytes, pos, state.generation)) {
return std::unexpected{
invalid("Checkpoint: truncated generation")};
}
if (pos != bytes.size()) {
return std::unexpected{
invalid("Checkpoint: trailing bytes after payload")};
}
return state;
}
} // namespace ReloadableHost
@@ -0,0 +1,117 @@
#include "reloadable_host/checkpoint.hpp"
#include <cassert>
#include <cstdio>
#include <cstdint>
#include <string>
#include <string_view>
#include <vector>
namespace
{
using namespace ReloadableHost;
void append_u32(std::vector<std::byte>& out, std::uint32_t value)
{
for (int shift = 24; shift >= 0; shift -= 8) {
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
}
}
void append_u64(std::vector<std::byte>& out, std::uint64_t value)
{
for (int shift = 56; shift >= 0; shift -= 8) {
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
}
}
std::vector<std::byte> legacy(std::uint32_t version, std::uint64_t counter,
std::string_view workflow = {})
{
std::vector<std::byte> out{std::byte{'V'}, std::byte{'C'},
std::byte{'H'}, std::byte{'K'}};
append_u32(out, version);
append_u64(out, counter);
if (version >= 2) {
append_u32(out, static_cast<std::uint32_t>(workflow.size()));
for (const char ch : workflow) {
out.push_back(static_cast<std::byte>(ch));
}
}
return out;
}
void current_schema_round_trips()
{
const CheckpointState state{7, "workflow-a", 3};
auto encoded = encode_checkpoint(state);
assert(encoded.has_value());
auto decoded = decode_checkpoint(*encoded);
assert(decoded.has_value());
assert(decoded->counter == 7);
assert(decoded->workflow == "workflow-a");
assert(decoded->generation == 3);
}
void older_schemas_migrate_forward()
{
auto v1 = decode_checkpoint(legacy(1, 42));
assert(v1.has_value());
assert(v1->counter == 42);
assert(v1->workflow.empty());
assert(v1->generation == 0);
auto v2 = decode_checkpoint(legacy(2, 9, "wf"));
assert(v2.has_value());
assert(v2->counter == 9);
assert(v2->workflow == "wf");
assert(v2->generation == 0);
}
void malformed_payloads_are_rejected()
{
const CheckpointState state{1, "wf", 2};
auto encoded = encode_checkpoint(state);
assert(encoded.has_value());
// Unknown newer schema.
auto future = legacy(1, 5);
future[7] = std::byte{4}; // version field is bytes 4..7 (big-endian)
assert(!decode_checkpoint(future).has_value());
// Bad magic.
auto bad_magic = *encoded;
bad_magic[0] = std::byte{'X'};
assert(!decode_checkpoint(bad_magic).has_value());
// Truncated and trailing bytes.
auto truncated = *encoded;
truncated.pop_back();
assert(!decode_checkpoint(truncated).has_value());
auto trailing = *encoded;
trailing.push_back(std::byte{0});
assert(!decode_checkpoint(trailing).has_value());
}
void limits_are_enforced()
{
auto big = encode_checkpoint(CheckpointState{0, std::string(10, 'x'), 0});
assert(big.has_value());
assert(!decode_checkpoint(*big, CheckpointLimits{.max_workflow_bytes = 4})
.has_value());
assert(!encode_checkpoint(
CheckpointState{0, std::string(100, 'x'), 0},
CheckpointLimits{.max_workflow_bytes = 4})
.has_value());
}
} // namespace
int main()
{
current_schema_round_trips();
older_schemas_migrate_forward();
malformed_payloads_are_rejected();
limits_are_enforced();
std::puts("reloadable-host checkpoint ok");
return 0;
}