Prepare bounded checkpoint reads and immutable saves off the client thread

This commit is contained in:
faumaray committed 2026-12-02 22:04:58 +03:00
1 parent 8c4c1fe235
commit a063894637
8 files changed
+731 -3

No files matched your search

@@ -16,6 +16,7 @@
namespace settlement {
inline constexpr std::uint32_t checkpoint_version = 1;
inline constexpr std::string_view checkpoint_signature = "STHCHKPT";
inline constexpr std::size_t max_checkpoint_body_bytes = 128 * 1024 * 1024;
inline constexpr std::size_t max_checkpoint_metadata_bytes = 64 * 1024;
inline constexpr std::size_t max_checkpoint_bytes =
@@ -0,0 +1,77 @@
#pragma once
#include "settlement/core/save_service.hpp"
#include "settlement/core/simulation_runtime.hpp"
#include <expected>
#include <functional>
#include <vector>
namespace settlement {
enum class CheckpointFileError {
Io, TooLarge, UnsupportedFormat, InvalidCheckpoint, InvalidLegacy,
IdentityGeneration, ResourceFailure, ReaderFailure,
};
struct CheckpointFileDiagnostic {
CheckpointFileError code;
std::string message;
std::uint32_t native_error = 0;
// Present for a modern codec failure, including the exact incompatible package.
std::optional<CheckpointDiagnostic> checkpoint;
};
using CheckpointLoadResult = std::expected<WorldCheckpoint, CheckpointFileDiagnostic>;
enum class CheckpointLoadAdmission { Accepted, Busy, Closed, InvalidRequest, IdExhausted };
struct CheckpointLoadSubmission {
CheckpointLoadAdmission admission = CheckpointLoadAdmission::InvalidRequest;
std::uint64_t request_id = 0;
std::future<CheckpointLoadResult> completion;
};
enum class CheckpointReadPhase { Opened, BeforeRead };
struct CheckpointFilesOptions {
std::size_t load_capacity = 4; // Includes the running read.
std::size_t save_capacity = 8;
std::size_t max_load_bytes = max_checkpoint_bytes;
// Diagnostic/test seam on the reader. Captures must outlive shutdown.
// Calling shutdown from this observer throws logic_error.
std::function<void(CheckpointReadPhase)> read_observer;
};
// Keep one instance for the window lifetime so accepted saves retain FIFO order
// across world/session replacements. Workers own their paths, catalog and held
// snapshots. The caller owns load-ticket fencing and UI completion handling.
class CheckpointFiles {
public:
explicit CheckpointFiles(CheckpointFilesOptions options = {});
explicit CheckpointFiles(std::vector<RulePackage> installed, CheckpointFilesOptions options = {});
~CheckpointFiles();
CheckpointFiles(const CheckpointFiles&) = delete;
CheckpointFiles& operator=(const CheckpointFiles&) = delete;
// Obtain a healthy immutable view with SimulationRuntime::snapshot_for_save().
// Materialization runs on the existing SaveService writer, bounded to
// max_checkpoint_bytes. Rejected calls do not retain the snapshot.
[[nodiscard]] SaveSubmission try_save(
const std::filesystem::path& destination,
const std::shared_ptr<const PublishedSnapshot>& snapshot);
// Opens once and checks actual size/EOF under the byte limit. Modern failures
// retain their diagnostics. Legacy imports receive a fresh stable world UUID
// and the exact supported default manifest from this instance's catalog.
[[nodiscard]] CheckpointLoadSubmission try_load(
const std::filesystem::path& source, std::size_t workers = min_worker_count,
ExecutionMode mode = ExecutionMode::Parallel);
// Closes admission and drains both services, without holding the admission
// mutex while joining. Destruction requires callers to stop using this object.
void shutdown();
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace settlement