Integrate background save publication into M01
This commit is contained in:
commit
1fa37d8e87
6 files changed
+771
No files matched your search
@@ -0,0 +1,103 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Core/Uuid.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace settlement {
|
||||
|
||||
enum class SavePhase {
|
||||
Materialize, CreateStage, WriteStage, FlushStage, CloseStage, Publish, Published,
|
||||
};
|
||||
|
||||
enum class SaveError {
|
||||
None, ProducerFailed, TooLarge, Io, Internal, Injected,
|
||||
};
|
||||
|
||||
struct SaveRequest {
|
||||
// Absolute filesystem path. The parent must exist. Both creation and
|
||||
// replacement are supported; acceptance does not imply the path is writable.
|
||||
// Open readers may make Windows reject replacement, even with delete
|
||||
// sharing (observed ERROR_ACCESS_DENIED on Wine 11). Such failures preserve
|
||||
// the old slot; they never trigger a delete/truncate publication fallback.
|
||||
std::filesystem::path destination;
|
||||
std::uint64_t tick{};
|
||||
Void::Uuid::Uuid session{};
|
||||
// Capture an immutable snapshot/shared byte buffer by value. Invoked exactly
|
||||
// once, on the writer. Never capture live mutable World/UI state by reference.
|
||||
// Copies must be cheap; materialization and its memory budget belong here.
|
||||
std::function<std::string()> produce;
|
||||
};
|
||||
|
||||
struct SaveResult {
|
||||
std::uint64_t request_id{};
|
||||
std::uint64_t tick{};
|
||||
Void::Uuid::Uuid session{};
|
||||
SaveError error{SaveError::None};
|
||||
SavePhase phase{SavePhase::Materialize};
|
||||
std::uint32_t native_error{};
|
||||
bool visible_on_disk{false};
|
||||
// True for an error after publication: the new file is visible and must not
|
||||
// be reported as rolled back. Success promises flushed bytes and atomic
|
||||
// visibility, not resilience to power loss on every filesystem/device.
|
||||
bool durability_unknown{false};
|
||||
std::uint32_t cleanup_error{};
|
||||
// Set only if removal of this request's staging file failed.
|
||||
std::filesystem::path retained_staging_file;
|
||||
|
||||
[[nodiscard]] bool succeeded() const noexcept { return error == SaveError::None; }
|
||||
};
|
||||
|
||||
enum class SaveAdmission { Accepted, Busy, Closed, InvalidRequest, IdExhausted };
|
||||
|
||||
struct SaveSubmission {
|
||||
SaveAdmission admission{SaveAdmission::InvalidRequest};
|
||||
// Zero and an invalid future on rejection. Accepted IDs start at one and
|
||||
// increase in FIFO publication order, including for concurrent submitters.
|
||||
std::uint64_t request_id{};
|
||||
std::future<SaveResult> completion;
|
||||
};
|
||||
|
||||
struct SaveServiceOptions {
|
||||
std::size_t capacity{8}; // Includes the running request.
|
||||
std::size_t max_bytes{64 * 1024 * 1024}; // Checked after materialization.
|
||||
// Test seam, executed on the writer before each phase (each write chunk).
|
||||
// Nonzero injects a native error. Published runs after the rename.
|
||||
std::function<std::uint32_t(SavePhase)> fault_hook;
|
||||
};
|
||||
|
||||
// One bounded Void::Async FIFO writer per service. Use one service for all
|
||||
// requests to a slot; separate services/processes are independent writers.
|
||||
// Windows adapter tracked by https://git.faumaray.com/Void/Library/issues/3.
|
||||
// Replace it when Void::FileSystem::replace_file supports Windows and passes
|
||||
// the save-slot create/replace, reader, phase-failure and sibling-preservation
|
||||
// tests with the same visibility and cleanup reporting.
|
||||
class SaveService {
|
||||
public:
|
||||
explicit SaveService(SaveServiceOptions options = {});
|
||||
~SaveService(); // Drains accepted work; never destroy on its writer thread.
|
||||
SaveService(const SaveService&) = delete;
|
||||
SaveService& operator=(const SaveService&) = delete;
|
||||
|
||||
// Does no file IO or producer invocation and never waits for a slot, disk,
|
||||
// or another submitter. Contention is Busy. Rejection does not copy/move the
|
||||
// producer. Allocation/copy failures throw before accepting the request.
|
||||
[[nodiscard]] SaveSubmission try_submit(const SaveRequest& request);
|
||||
|
||||
// Closes admission and drains every accepted request. Safe concurrently
|
||||
// with submissions and other shutdown calls, but not object destruction.
|
||||
// Throws logic_error if called from this service's producer/fault hook.
|
||||
void shutdown();
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
|
||||
} // namespace settlement
|
||||
Reference in new issue
Block a user