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
+29 -6
View File
@@ -211,14 +211,14 @@ struct LocalWorldFiles::Lock {
LocalWorldFiles::LocalWorldFiles(std::filesystem::path directory, WorldIdentity identity,
std::size_t budget, PublicationInterruption interruption,
std::unique_ptr<Lock> lock)
PublicationFailure failure, std::unique_ptr<Lock> lock)
: directory_{std::move(directory)}, identity_{identity}, budget_{budget},
interruption_{interruption}, lock_{std::move(lock)} {}
interruption_{interruption}, failure_{failure}, lock_{std::move(lock)} {}
LocalWorldFiles::~LocalWorldFiles() = default;
std::expected<std::unique_ptr<LocalWorldFiles>, LocalFileError>
LocalWorldFiles::open(const std::filesystem::path& root, WorldIdentity identity, std::size_t budget,
PublicationInterruption interruption) {
PublicationInterruption interruption, PublicationFailure failure) {
if (!identity.world || !identity.frame || !budget)
return std::unexpected(LocalFileError::invalid_identity);
const auto directory = root / ("world-" + hex(identity.world) + "-" + hex(identity.frame));
@@ -238,7 +238,7 @@ LocalWorldFiles::open(const std::filesystem::path& root, WorldIdentity identity,
return std::unexpected(LocalFileError::busy);
#endif
auto result = std::unique_ptr<LocalWorldFiles>(
new LocalWorldFiles(directory, identity, budget, interruption, std::move(lock)));
new LocalWorldFiles(directory, identity, budget, interruption, failure, std::move(lock)));
for (std::size_t slot = 0; slot < 2; ++slot) {
auto bytes = read_file(directory / ("manifest." + std::to_string(slot)), manifest_size);
if (!bytes) {
@@ -260,13 +260,15 @@ LocalWorldFiles::open(const std::filesystem::path& root, WorldIdentity identity,
}
std::optional<FileCommit> LocalWorldFiles::current() const noexcept {
if (!manifest_)
if (poisoned_ || !manifest_)
return std::nullopt;
return FileCommit{manifest_->generation, manifest_->covered_command,
manifest_->snapshot_checksum, manifest_->has_journal};
}
std::expected<std::unique_ptr<ChunkStore>, LocalFileError> LocalWorldFiles::recover() const {
if (poisoned_)
return std::unexpected(LocalFileError::poisoned);
if (!manifest_)
return std::unexpected(LocalFileError::empty);
const auto snapshot_path =
@@ -314,6 +316,12 @@ LocalWorldFiles::publish(std::span<const std::uint8_t> payload, bool journal, st
const auto temp = directory_ / (std::string{prefix} + ".payload.tmp");
std::filesystem::remove(temp, ec);
ec.clear();
if (failure_ == PublicationFailure::payload_out_of_space) {
failure_ = PublicationFailure::none;
const auto partial_size = std::max<std::size_t>(1, payload.size() / 2);
(void)write_synced(temp, payload.first(partial_size));
return std::unexpected(LocalFileError::io);
}
if (!write_synced(temp, payload))
return std::unexpected(LocalFileError::io);
if (interruption_ == PublicationInterruption::after_payload_sync) {
@@ -349,6 +357,12 @@ LocalWorldFiles::publish(std::span<const std::uint8_t> payload, bool journal, st
const auto encoded = encode_manifest(identity_, candidate);
std::filesystem::remove(temp_manifest, ec);
ec.clear();
if (failure_ == PublicationFailure::manifest_out_of_space) {
failure_ = PublicationFailure::none;
const auto partial_size = std::max<std::size_t>(1, encoded.size() / 2);
(void)write_synced(temp_manifest, std::span{encoded}.first(partial_size));
return std::unexpected(LocalFileError::io);
}
if (!write_synced(temp_manifest, encoded))
return std::unexpected(LocalFileError::io);
if (interruption_ == PublicationInterruption::after_manifest_sync) {
@@ -358,7 +372,8 @@ LocalWorldFiles::publish(std::span<const std::uint8_t> payload, bool journal, st
}
if (!replace_file(temp_manifest, dest_manifest))
return std::unexpected(LocalFileError::io);
if (!sync_directory(directory_)) {
if (failure_ == PublicationFailure::manifest_directory_sync || !sync_directory(directory_)) {
failure_ = PublicationFailure::none;
poisoned_ = true;
return std::unexpected(LocalFileError::io);
}
@@ -389,6 +404,14 @@ std::expected<FileCommit, LocalFileError> LocalWorldFiles::checkpoint(const Chun
: 0;
if (covered < required)
return std::unexpected(LocalFileError::stale_checkpoint);
if (manifest_ && manifest_->has_journal) {
auto committed = recover();
if (!committed)
return std::unexpected(committed.error());
const auto committed_bytes = (*committed)->snapshot(identity_);
if (!committed_bytes || !std::ranges::equal(*bytes, *committed_bytes))
return std::unexpected(LocalFileError::stale_checkpoint);
}
return publish(*bytes, false, covered, hash(*bytes));
}