Files
aevolution/tests/world_local_files_tests.cpp
T

265 lines
11 KiB
C++

#include "evo/world/local_files.hpp"
#include <array>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::world;
constexpr WorldIdentity identity{71, 91};
constexpr std::size_t budget = 2'000'000;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("local world files line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
struct TempRoot {
std::filesystem::path path =
std::filesystem::temp_directory_path() /
("evo-local-files-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
TempRoot() {
std::filesystem::create_directories(path);
}
~TempRoot() {
std::error_code ignored;
std::filesystem::remove_all(path, ignored);
}
};
std::unique_ptr<ChunkStore> make_store() {
auto result =
ChunkStore::create({}, 4, budget, {{VolumeUnits{1'000'000}, VolumeUnits{1'000'000}}});
CHECK(result);
CHECK((*result)->load({0, 0, -2}));
return std::move(*result);
}
std::unique_ptr<ChunkStore> edited_store(std::uint32_t amount) {
auto result = make_store();
const auto view = result->view({0, 0, -2});
CHECK(view);
const std::array<CellCut, 1> cuts{{{{0, 0, -2}, {0, 0, 0}, view->revision(), amount}}};
ExcavationRequest request{1, 1, cuts, 0};
const auto digest = excavation_digest(request);
CHECK(digest);
request.digest = *digest;
CHECK(result->excavate(request));
return result;
}
std::filesystem::path world_dir(const std::filesystem::path& root) {
return root / "world-0000000000000047-000000000000005b";
}
void checkpoint_and_journal_round_trip() {
TempRoot root;
auto files = LocalWorldFiles::open(root.path, identity, budget);
CHECK(files && !(*files)->recover() && (*files)->recover().error() == LocalFileError::empty);
auto original = make_store();
auto checkpoint = original->snapshot(identity);
CHECK(checkpoint);
auto committed = (*files)->checkpoint(*original);
CHECK(committed && committed->generation == 1 && !committed->has_journal);
const auto view = original->view({0, 0, -2});
CHECK(view);
const std::array<CellCut, 1> cuts{{{{0, 0, -2}, {0, 0, 0}, view->revision(), 2}}};
ExcavationRequest request{1, 1, cuts, 0};
const auto digest = excavation_digest(request);
CHECK(digest);
request.digest = *digest;
const std::array<ExcavationRequest, 1> requests{request};
auto journal = make_edit_journal(*checkpoint, identity, requests, budget);
CHECK(journal);
auto journal_commit = (*files)->publish_journal(*checkpoint, *journal);
CHECK(journal_commit && journal_commit->generation == 2 && journal_commit->has_journal);
CHECK(!(*files)->publish_journal(*checkpoint, *journal));
const auto stale_checkpoint = (*files)->checkpoint(*original);
CHECK(!stale_checkpoint && stale_checkpoint.error() == LocalFileError::stale_checkpoint);
auto divergent = edited_store(3);
const auto divergent_checkpoint = (*files)->checkpoint(*divergent);
CHECK(!divergent_checkpoint &&
divergent_checkpoint.error() == LocalFileError::stale_checkpoint);
auto recovered = (*files)->recover();
CHECK(recovered);
auto expected = replay_edit_journal(*checkpoint, *journal, identity, budget);
CHECK(expected);
CHECK((*recovered)->snapshot(identity) == (**expected).snapshot(identity));
(*files).reset();
auto reopened = LocalWorldFiles::open(root.path, identity, budget);
CHECK(reopened);
const auto reopened_commit = (*reopened)->current();
CHECK(reopened_commit.has_value());
CHECK(reopened_commit.value_or(FileCommit{}).generation == 2);
CHECK((*reopened)->recover());
auto compacted = (*reopened)->checkpoint(**recovered);
CHECK(compacted && compacted->generation == 3 && !compacted->has_journal);
CHECK((*reopened)->recover());
}
void interruptions_preserve_commit_point() {
for (const auto point :
{PublicationInterruption::after_payload_sync, PublicationInterruption::after_manifest_sync,
PublicationInterruption::after_manifest_publish}) {
TempRoot root;
WorldBytes old_snapshot;
{
auto files = LocalWorldFiles::open(root.path, identity, budget);
CHECK(files);
auto initial = make_store();
auto snapshot = initial->snapshot(identity);
CHECK(snapshot);
old_snapshot = *snapshot;
CHECK((*files)->checkpoint(*initial));
}
auto interrupted = LocalWorldFiles::open(root.path, identity, budget, point);
CHECK(interrupted);
auto changed = edited_store(2);
auto new_snapshot = changed->snapshot(identity);
CHECK(new_snapshot);
const auto result = (*interrupted)->checkpoint(*changed);
CHECK(!result && result.error() == LocalFileError::interrupted);
CHECK(!(*interrupted)->checkpoint(*changed));
const auto poisoned_recovery = (*interrupted)->recover();
CHECK(!poisoned_recovery && poisoned_recovery.error() == LocalFileError::poisoned);
CHECK(!(*interrupted)->current());
(*interrupted).reset();
auto recovered = LocalWorldFiles::open(root.path, identity, budget);
CHECK(recovered);
const auto commit = (*recovered)->current();
CHECK(commit.has_value());
const auto generation = commit.value_or(FileCommit{}).generation;
CHECK(generation == (point == PublicationInterruption::after_manifest_publish ? 2 : 1));
auto recovered_state = (*recovered)->recover();
CHECK(recovered_state);
const auto& expected =
point == PublicationInterruption::after_manifest_publish ? *new_snapshot : old_snapshot;
CHECK((**recovered_state).snapshot(identity) == expected);
const auto expected_debris =
point == PublicationInterruption::after_manifest_publish ? 2U : 0U;
const auto debris = (**recovered_state).debris({1});
CHECK(debris && *debris == VolumeUnits{expected_debris});
}
}
void partial_write_failures_preserve_and_retry() {
for (const auto failure :
{PublicationFailure::payload_out_of_space, PublicationFailure::manifest_out_of_space}) {
TempRoot root;
WorldBytes old_snapshot;
{
auto files = LocalWorldFiles::open(root.path, identity, budget);
CHECK(files);
auto initial = make_store();
auto snapshot = initial->snapshot(identity);
CHECK(snapshot);
old_snapshot = *snapshot;
CHECK((*files)->checkpoint(*initial));
}
auto files = LocalWorldFiles::open(root.path, identity, budget,
PublicationInterruption::none, failure);
CHECK(files);
auto changed = edited_store(2);
auto new_snapshot = changed->snapshot(identity);
CHECK(new_snapshot);
const auto failed = (*files)->checkpoint(*changed);
CHECK(!failed && failed.error() == LocalFileError::io);
auto before_retry = (*files)->recover();
CHECK(before_retry);
CHECK((**before_retry).snapshot(identity) == old_snapshot);
auto retried = (*files)->checkpoint(*changed);
CHECK(retried);
auto after_retry = (*files)->recover();
CHECK(after_retry);
CHECK((**after_retry).snapshot(identity) == *new_snapshot);
}
}
void manifest_directory_sync_failure_requires_reopen() {
TempRoot root;
{
auto files = LocalWorldFiles::open(root.path, identity, budget);
CHECK(files);
auto initial = make_store();
CHECK((*files)->checkpoint(*initial));
}
auto files = LocalWorldFiles::open(root.path, identity, budget, PublicationInterruption::none,
PublicationFailure::manifest_directory_sync);
CHECK(files);
auto changed = edited_store(2);
auto expected = changed->snapshot(identity);
CHECK(expected);
const auto failed = (*files)->checkpoint(*changed);
CHECK(!failed && failed.error() == LocalFileError::io);
const auto poisoned = (*files)->recover();
CHECK(!poisoned && poisoned.error() == LocalFileError::poisoned);
CHECK(!(*files)->current());
(*files).reset();
auto reopened = LocalWorldFiles::open(root.path, identity, budget);
CHECK(reopened);
auto recovered = (*reopened)->recover();
CHECK(recovered);
CHECK((**recovered).snapshot(identity) == *expected);
}
void lock_and_corruption_fail_closed() {
TempRoot root;
auto first = LocalWorldFiles::open(root.path, identity, budget);
CHECK(first);
auto busy = LocalWorldFiles::open(root.path, identity, budget);
CHECK(!busy && busy.error() == LocalFileError::busy);
auto state = make_store();
CHECK((*first)->checkpoint(*state));
(*first).reset();
auto reopened = LocalWorldFiles::open(root.path, identity, budget);
CHECK(reopened && (*reopened)->recover());
const auto manifest = world_dir(root.path) / "manifest.0";
{
std::fstream file(manifest, std::ios::binary | std::ios::in | std::ios::out);
CHECK(file);
file.seekp(40);
const char corrupt = '\x7f';
file.write(&corrupt, 1);
}
(*reopened).reset();
auto corrupt = LocalWorldFiles::open(root.path, identity, budget);
CHECK(!corrupt && corrupt.error() == LocalFileError::corrupt_manifest);
}
void missing_published_payload_fails_closed() {
TempRoot root;
std::uint64_t checksum{};
{
auto files = LocalWorldFiles::open(root.path, identity, budget);
CHECK(files);
auto state = make_store();
auto published = (*files)->checkpoint(*state);
CHECK(published);
checksum = published->snapshot_checksum;
}
const auto payload = world_dir(root.path) / ("snapshot-" + [&] {
constexpr char digits[] = "0123456789abcdef";
std::string value(16, '0');
for (int i = 15; i >= 0; --i) {
value[static_cast<std::size_t>(i)] = digits[checksum & 15U];
checksum >>= 4U;
}
return value;
}() + ".bin");
std::error_code ec;
CHECK(std::filesystem::remove(payload, ec) && !ec);
auto files = LocalWorldFiles::open(root.path, identity, budget);
CHECK(files);
const auto recovered = (*files)->recover();
CHECK(!recovered && recovered.error() == LocalFileError::corrupt_payload);
}
} // namespace
int main() try {
checkpoint_and_journal_round_trip();
interruptions_preserve_commit_point();
partial_write_failures_preserve_and_retry();
manifest_directory_sync_failure_requires_reopen();
lock_and_corruption_fail_closed();
missing_published_payload_fails_closed();
} catch (const std::exception&) {
return 1;
}