Fix M5.7 compaction and failure recovery

This commit is contained in:
dingo
2026-09-26 19:40:16 -04:00
parent 56f533c738
commit a2a12fdc69
6 changed files with 187 additions and 29 deletions
+95 -2
View File
@@ -37,6 +37,18 @@ std::unique_ptr<ChunkStore> make_store() {
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";
}
@@ -65,6 +77,10 @@ void checkpoint_and_journal_round_trip() {
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);
@@ -87,18 +103,27 @@ void interruptions_preserve_commit_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 = make_store();
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);
@@ -106,9 +131,75 @@ void interruptions_preserve_commit_point() {
CHECK(commit.has_value());
const auto generation = commit.value_or(FileCommit{}).generation;
CHECK(generation == (point == PublicationInterruption::after_manifest_publish ? 2 : 1));
CHECK((*recovered)->recover());
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);
@@ -164,6 +255,8 @@ void missing_published_payload_fails_closed() {
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&) {