feat(storage): implement original V1 durable Storage engine, tools, and async adapter
This commit is contained in:
1 parent
427cc9a843
commit
fdf0da5ca8
21 files changed
+6966
No files matched your search
@@ -0,0 +1,327 @@
|
||||
#include <Void/Core/Uuid.hpp>
|
||||
#include <Void/Storage/Engine.hpp>
|
||||
#include <Void/Storage/Format.hpp>
|
||||
#include <Void/Storage/Tools.hpp>
|
||||
#include "TableInternal.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Storage
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Result<OwnedBytes> read_entire_file(
|
||||
FilePort& port,
|
||||
const std::filesystem::path& path,
|
||||
std::size_t max_bytes)
|
||||
{
|
||||
auto sz_r = port.file_size(path);
|
||||
if (!sz_r) return Void::Result::fail(std::move(sz_r).error());
|
||||
if (*sz_r > max_bytes) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::corrupt,
|
||||
std::format("Storage tool: '{}' size {} exceeds bound {}",
|
||||
path.string(), *sz_r, max_bytes)));
|
||||
}
|
||||
OwnedBytes out(*sz_r);
|
||||
auto f = port.open_file(path, FileOpenMode::ReadOnly);
|
||||
if (!f) return Void::Result::fail(std::move(f).error());
|
||||
if (*sz_r > 0) {
|
||||
if (auto r = (*f)->pread_exact(0, out); !r) {
|
||||
return Void::Result::fail(std::move(r).error());
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Result<VerifyReport> verify_directory(
|
||||
const std::filesystem::path& directory,
|
||||
const Limits& limits,
|
||||
std::shared_ptr<FilePort> port)
|
||||
{
|
||||
if (directory.empty()) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument, "verify_directory: empty directory"));
|
||||
}
|
||||
if (auto vl = validate_limits(limits); !vl) {
|
||||
return Void::Result::fail(std::move(vl).error());
|
||||
}
|
||||
if (!port) port = make_posix_file_port();
|
||||
|
||||
auto cur_bytes = read_entire_file(*port, directory / "CURRENT", 512);
|
||||
if (!cur_bytes) return Void::Result::fail(std::move(cur_bytes).error());
|
||||
auto cur = decode_current(*cur_bytes);
|
||||
if (!cur) return Void::Result::fail(std::move(cur).error());
|
||||
|
||||
auto man_bytes = read_entire_file(
|
||||
*port, directory / cur->manifest_filename, limits.max_manifest_bytes);
|
||||
if (!man_bytes) return Void::Result::fail(std::move(man_bytes).error());
|
||||
auto man = decode_manifest(
|
||||
*man_bytes, limits.max_manifest_bytes, limits.max_manifest_tables, limits.max_key_bytes);
|
||||
if (!man) return Void::Result::fail(std::move(man).error());
|
||||
|
||||
if (man->database_uuid != cur->database_uuid || man->generation != cur->generation) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::corrupt, "verify_directory: manifest mismatches CURRENT"));
|
||||
}
|
||||
|
||||
VerifyReport report;
|
||||
report.manifest_generation = man->generation;
|
||||
report.checkpoint_sequence = man->checkpoint_sequence;
|
||||
|
||||
for (const auto& tm : man->tables) {
|
||||
auto tr = detail::TableReader::open(
|
||||
port,
|
||||
directory / make_table_filename(tm.table_id),
|
||||
man->database_uuid,
|
||||
tm,
|
||||
limits);
|
||||
if (!tr) return Void::Result::fail(std::move(tr).error());
|
||||
auto blks = (*tr)->verify_all_blocks_and_file_crc();
|
||||
if (!blks) return Void::Result::fail(std::move(blks).error());
|
||||
++report.tables_verified;
|
||||
report.blocks_verified += *blks;
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < man->wal_segments.size(); ++i) {
|
||||
const auto seg_id = man->wal_segments[i];
|
||||
const bool is_final = (i + 1 == man->wal_segments.size());
|
||||
const std::optional<std::uint64_t> exp_prev =
|
||||
(i > 0) ? std::optional<std::uint64_t>{man->wal_segments[i - 1]}
|
||||
: (seg_id == 1 ? std::optional<std::uint64_t>{0} : std::nullopt);
|
||||
auto scan = detail::scan_wal_segment(
|
||||
*port,
|
||||
directory / make_wal_filename(seg_id),
|
||||
man->database_uuid,
|
||||
seg_id,
|
||||
exp_prev,
|
||||
is_final,
|
||||
limits,
|
||||
true);
|
||||
if (!scan) return Void::Result::fail(std::move(scan).error());
|
||||
++report.wal_segments_verified;
|
||||
report.wal_batches_verified += scan->batches.size();
|
||||
}
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
Result<void> restore_backup(
|
||||
const std::filesystem::path& backup_dir,
|
||||
const std::filesystem::path& destination,
|
||||
const Limits& limits,
|
||||
std::shared_ptr<FilePort> port)
|
||||
{
|
||||
if (backup_dir.empty() || destination.empty()) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument, "restore_backup: empty path"));
|
||||
}
|
||||
if (!port) port = make_posix_file_port();
|
||||
|
||||
std::error_code ec;
|
||||
if (std::filesystem::exists(destination, ec) &&
|
||||
std::filesystem::equivalent(backup_dir, destination, ec)) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"restore_backup: source and destination cannot be identical"));
|
||||
}
|
||||
|
||||
auto vr = verify_directory(backup_dir, limits, port);
|
||||
if (!vr) return Void::Result::fail(std::move(vr).error());
|
||||
|
||||
if (auto ed = port->ensure_dir(destination); !ed) return ed;
|
||||
auto lock = port->lock_directory(destination);
|
||||
if (!lock) return Void::Result::fail(std::move(lock).error());
|
||||
|
||||
auto cur_bytes = read_entire_file(*port, backup_dir / "CURRENT", 512);
|
||||
if (!cur_bytes) return Void::Result::fail(std::move(cur_bytes).error());
|
||||
auto cur = decode_current(*cur_bytes);
|
||||
if (!cur) return Void::Result::fail(std::move(cur).error());
|
||||
|
||||
auto man_bytes = read_entire_file(
|
||||
*port, backup_dir / cur->manifest_filename, limits.max_manifest_bytes);
|
||||
if (!man_bytes) return Void::Result::fail(std::move(man_bytes).error());
|
||||
auto man = decode_manifest(
|
||||
*man_bytes, limits.max_manifest_bytes, limits.max_manifest_tables, limits.max_key_bytes);
|
||||
if (!man) return Void::Result::fail(std::move(man).error());
|
||||
|
||||
auto copy_file = [&](const std::string& name) -> Result<void> {
|
||||
const auto src = backup_dir / name;
|
||||
const auto dst = destination / name;
|
||||
const auto dst_tmp = destination / (name + ".tmp");
|
||||
auto sz = port->file_size(src);
|
||||
if (!sz) return Void::Result::fail(std::move(sz).error());
|
||||
OwnedBytes buf(*sz);
|
||||
auto sf = port->open_file(src, FileOpenMode::ReadOnly);
|
||||
if (!sf) return Void::Result::fail(std::move(sf).error());
|
||||
if (*sz > 0) {
|
||||
if (auto r = (*sf)->pread_exact(0, buf); !r) return r;
|
||||
}
|
||||
(void)port->unlink_file(dst_tmp);
|
||||
auto df = port->open_file(dst_tmp, FileOpenMode::CreateNew);
|
||||
if (!df) return Void::Result::fail(std::move(df).error());
|
||||
if (!buf.empty()) {
|
||||
if (auto w = (*df)->append_all(buf); !w) return Void::Result::fail(std::move(w).error());
|
||||
}
|
||||
if (auto s = (*df)->sync(); !s) return s;
|
||||
if (auto c = (*df)->close(); !c) return c;
|
||||
return port->rename_atomic(dst_tmp, dst);
|
||||
};
|
||||
|
||||
for (const auto& tm : man->tables) {
|
||||
if (auto r = copy_file(make_table_filename(tm.table_id)); !r) return r;
|
||||
}
|
||||
for (const auto seg : man->wal_segments) {
|
||||
if (auto r = copy_file(make_wal_filename(seg)); !r) return r;
|
||||
}
|
||||
if (auto r = copy_file(cur->manifest_filename); !r) return r;
|
||||
if (auto r = copy_file("CURRENT"); !r) return r;
|
||||
return port->sync_dir(destination);
|
||||
}
|
||||
|
||||
Result<SalvageReport> salvage_directory(
|
||||
const std::filesystem::path& source_dir,
|
||||
const std::filesystem::path& destination_dir,
|
||||
const Limits& limits,
|
||||
std::shared_ptr<FilePort> port)
|
||||
{
|
||||
if (source_dir.empty() || destination_dir.empty()) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument, "salvage_directory: empty path"));
|
||||
}
|
||||
if (auto vl = validate_limits(limits); !vl) {
|
||||
return Void::Result::fail(std::move(vl).error());
|
||||
}
|
||||
if (!port) port = make_posix_file_port();
|
||||
|
||||
std::error_code ec;
|
||||
if (std::filesystem::exists(destination_dir, ec) &&
|
||||
std::filesystem::equivalent(source_dir, destination_dir, ec)) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"salvage_directory: offline salvage must write into a distinct destination directory"));
|
||||
}
|
||||
|
||||
// Collect latest committed/recoverable key states without mutating source_dir!
|
||||
struct Val {
|
||||
Sequence seq{0};
|
||||
bool erased{false};
|
||||
OwnedBytes bytes{};
|
||||
};
|
||||
std::map<OwnedBytes, Val, ByteLess> kv;
|
||||
SalvageReport rep;
|
||||
|
||||
std::optional<Void::Uuid::Uuid> db_uuid;
|
||||
std::vector<std::string> files;
|
||||
if (auto fl = port->list_dir(source_dir); fl) {
|
||||
files = std::move(*fl);
|
||||
} else {
|
||||
return Void::Result::fail(std::move(fl).error());
|
||||
}
|
||||
|
||||
// Try CURRENT -> Manifest if intact, so valid checkpoint tables are recovered too.
|
||||
if (auto ce = port->exists(source_dir / "CURRENT"); ce && *ce) {
|
||||
auto cb = read_entire_file(*port, source_dir / "CURRENT", 512);
|
||||
if (cb) {
|
||||
if (auto cp = decode_current(*cb); cp) {
|
||||
db_uuid = cp->database_uuid;
|
||||
if (auto mb = read_entire_file(*port, source_dir / cp->manifest_filename, limits.max_manifest_bytes); mb) {
|
||||
if (auto md = decode_manifest(*mb, limits.max_manifest_bytes, limits.max_manifest_tables, limits.max_key_bytes); md) {
|
||||
for (const auto& tm : md->tables) {
|
||||
auto tr = detail::TableReader::open(
|
||||
port, source_dir / make_table_filename(tm.table_id),
|
||||
md->database_uuid, tm, limits);
|
||||
if (!tr) continue;
|
||||
for (std::size_t bi = 0; bi < (*tr)->index().size(); ++bi) {
|
||||
auto blk = (*tr)->read_block(bi);
|
||||
if (!blk) continue;
|
||||
for (const auto& e : *blk) {
|
||||
auto it = kv.find(e.key);
|
||||
if (it == kv.end() || e.sequence > it->second.seq) {
|
||||
kv[e.key] = Val{e.sequence, e.erased, e.value};
|
||||
}
|
||||
rep.highest_recovered_sequence =
|
||||
std::max(rep.highest_recovered_sequence, e.sequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Scan every WAL-*.vwal in lexicographic name order stopping at first damaged record.
|
||||
for (const auto& fname : files) {
|
||||
if (!fname.starts_with("WAL-") || !fname.ends_with(".vwal")) continue;
|
||||
const auto wpath = source_dir / fname;
|
||||
// Peek segment header for UUID if CURRENT/Manifest wasn't usable.
|
||||
if (!db_uuid.has_value()) {
|
||||
OwnedBytes hdr_b(kWalSegmentHeaderSize);
|
||||
auto wf = port->open_file(wpath, FileOpenMode::ReadOnly);
|
||||
if (wf && (*wf)->pread_exact(0, hdr_b).has_value()) {
|
||||
if (auto dec = decode_wal_segment_header(hdr_b); dec) {
|
||||
db_uuid = dec->database_uuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!db_uuid.has_value()) continue;
|
||||
auto scan = detail::salvage_wal_segment(*port, wpath, *db_uuid, limits);
|
||||
if (!scan) continue;
|
||||
if (scan->truncate_offset.has_value()) {
|
||||
rep.truncated_or_skipped_bytes += (scan->file_size - *scan->truncate_offset);
|
||||
}
|
||||
for (const auto& b : scan->batches) {
|
||||
++rep.recovered_batches;
|
||||
rep.highest_recovered_sequence =
|
||||
std::max(rep.highest_recovered_sequence, b.sequence);
|
||||
for (const auto& e : b.entries) {
|
||||
auto it = kv.find(e.key);
|
||||
if (it == kv.end() || e.sequence > it->second.seq) {
|
||||
kv[e.key] = Val{e.sequence, e.erased, e.value};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Materialize salvaged data into fresh destination directory preserving versions!
|
||||
OpenOptions out_opts;
|
||||
out_opts.directory = destination_dir;
|
||||
out_opts.limits = limits;
|
||||
out_opts.file_port = port;
|
||||
out_opts.initial_sequence = rep.highest_recovered_sequence;
|
||||
auto dest_eng = Engine::open(std::move(out_opts));
|
||||
if (!dest_eng) return Void::Result::fail(std::move(dest_eng).error());
|
||||
|
||||
Batch batch;
|
||||
std::size_t approx = 0;
|
||||
for (const auto& [k, v] : kv) {
|
||||
if (v.erased) {
|
||||
++rep.tombstones_preserved;
|
||||
continue;
|
||||
}
|
||||
++rep.keys_recovered;
|
||||
if (batch.mutations().size() + 1 > limits.max_batch_mutations ||
|
||||
approx + k.size() + v.bytes.size() + 128 > limits.max_batch_encoded_bytes) {
|
||||
auto wr = dest_eng->write(std::move(batch), Durability::Sync);
|
||||
if (!wr.has_value()) return Void::Result::fail(wr.error().cause.error);
|
||||
batch = Batch{};
|
||||
approx = 0;
|
||||
}
|
||||
batch.put(k, v.bytes);
|
||||
approx += k.size() + v.bytes.size() + 64;
|
||||
}
|
||||
if (!batch.empty()) {
|
||||
auto wr = dest_eng->write(std::move(batch), Durability::Sync);
|
||||
if (!wr.has_value()) return Void::Result::fail(wr.error().cause.error);
|
||||
}
|
||||
if (auto cl = dest_eng->close(); !cl) return Void::Result::fail(std::move(cl).error());
|
||||
return rep;
|
||||
}
|
||||
|
||||
} // namespace Void::Storage
|
||||
Reference in new issue
Block a user