feat: add Plan 20 Slice 1 CMake native translation, self-test smoke target, VL-009 VOID_TEST check, and warm-cache hardening
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 5s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 5s
This commit is contained in:
1 parent
363fb8200e
commit
d3d2666965
19 files changed
+1466
-65
No files matched your search
+90
-13
@@ -13,6 +13,7 @@
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <format>
|
||||
#include <mutex>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
@@ -143,10 +144,37 @@ void LeaseHandle::release() noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
struct StateStore::CacheState {
|
||||
struct StatFingerprint {
|
||||
std::uint64_t dev{0};
|
||||
std::uint64_t ino{0};
|
||||
std::uint64_t size{0};
|
||||
std::int64_t mtime_sec{0};
|
||||
std::int64_t mtime_nsec{0};
|
||||
bool operator==(const StatFingerprint &) const noexcept = default;
|
||||
};
|
||||
|
||||
static std::optional<StatFingerprint> stat_file(const std::filesystem::path &p) noexcept {
|
||||
struct stat st{};
|
||||
if (::stat(p.c_str(), &st) != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return StatFingerprint{
|
||||
static_cast<std::uint64_t>(st.st_dev), static_cast<std::uint64_t>(st.st_ino),
|
||||
static_cast<std::uint64_t>(st.st_size), static_cast<std::int64_t>(st.st_mtim.tv_sec),
|
||||
static_cast<std::int64_t>(st.st_mtim.tv_nsec)};
|
||||
}
|
||||
|
||||
std::mutex mutex;
|
||||
bool valid{false};
|
||||
StatFingerprint fp{};
|
||||
DatabaseSnapshot db;
|
||||
};
|
||||
|
||||
StateStore::StateStore(std::filesystem::path root)
|
||||
: root_{std::move(root)}, cas_dir_{root_ / "cas" / "blobs"}, staging_dir_{root_ / "staging"},
|
||||
leases_dir_{root_ / "leases"}, db_file_{root_ / "state" / "state.v1.json"},
|
||||
lock_file_{root_ / "state" / "state.lock"} {}
|
||||
lock_file_{root_ / "state" / "state.lock"}, cache_{std::make_shared<CacheState>()} {}
|
||||
|
||||
Void::Result::Result<StateStore, Void::Result::Error>
|
||||
StateStore::open(const std::filesystem::path &vbs_dir) {
|
||||
@@ -193,6 +221,14 @@ std::filesystem::path StateStore::cas_blob_path(std::string_view sha256) const {
|
||||
|
||||
Void::Result::Result<StateStore::DatabaseSnapshot, Void::Result::Error>
|
||||
StateStore::load_db_locked() const {
|
||||
const auto cur_fp = CacheState::stat_file(db_file_);
|
||||
if (cur_fp && cache_) {
|
||||
std::lock_guard lk{cache_->mutex};
|
||||
if (cache_->valid && cache_->fp == *cur_fp) {
|
||||
return cache_->db;
|
||||
}
|
||||
}
|
||||
|
||||
auto content = Void::FileSystem::read_file_bounded(db_file_, {128ULL * 1024ULL * 1024ULL});
|
||||
if (!content) {
|
||||
return std::unexpected{content.error()};
|
||||
@@ -329,6 +365,13 @@ StateStore::load_db_locked() const {
|
||||
}
|
||||
}
|
||||
|
||||
if (cur_fp && cache_) {
|
||||
std::lock_guard lk{cache_->mutex};
|
||||
cache_->db = snap;
|
||||
cache_->fp = *cur_fp;
|
||||
cache_->valid = true;
|
||||
}
|
||||
|
||||
return snap;
|
||||
}
|
||||
|
||||
@@ -354,7 +397,22 @@ StateStore::save_db_locked(const DatabaseSnapshot &db) const {
|
||||
json += serialize_generation(gen);
|
||||
}
|
||||
json += "]}\n";
|
||||
return Platform::atomic_write_file(db_file_, json, true);
|
||||
auto wrote = Platform::atomic_write_file(db_file_, json, true);
|
||||
if (!wrote) {
|
||||
return wrote;
|
||||
}
|
||||
if (cache_) {
|
||||
std::lock_guard lk{cache_->mutex};
|
||||
const auto new_fp = CacheState::stat_file(db_file_);
|
||||
if (new_fp) {
|
||||
cache_->db = db;
|
||||
cache_->fp = *new_fp;
|
||||
cache_->valid = true;
|
||||
} else {
|
||||
cache_->valid = false;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<BlobRef, Void::Result::Error>
|
||||
@@ -456,26 +514,45 @@ StateStore::materialize_blob(const BlobRef &ref, const std::filesystem::path &de
|
||||
|
||||
Void::Result::Result<std::optional<ActionSuccessRecord>, Void::Result::Error>
|
||||
StateStore::lookup_action(std::string_view action_key) const {
|
||||
auto lock = Platform::FileLock::acquire(lock_file_, Platform::LockMode::Shared);
|
||||
if (!lock) {
|
||||
return std::unexpected{lock.error()};
|
||||
std::optional<ActionSuccessRecord> found;
|
||||
{
|
||||
auto lock = Platform::FileLock::acquire(lock_file_, Platform::LockMode::Shared);
|
||||
if (!lock) {
|
||||
return std::unexpected{lock.error()};
|
||||
}
|
||||
const auto cur_fp = CacheState::stat_file(db_file_);
|
||||
bool served_from_cache = false;
|
||||
if (cur_fp && cache_) {
|
||||
std::lock_guard lk{cache_->mutex};
|
||||
if (cache_->valid && cache_->fp == *cur_fp) {
|
||||
if (const auto it = cache_->db.actions.find(action_key);
|
||||
it != cache_->db.actions.end()) {
|
||||
found = it->second;
|
||||
}
|
||||
served_from_cache = true;
|
||||
}
|
||||
}
|
||||
if (!served_from_cache) {
|
||||
auto db = load_db_locked();
|
||||
if (!db) {
|
||||
return std::unexpected{db.error()};
|
||||
}
|
||||
if (const auto it = db->actions.find(action_key); it != db->actions.end()) {
|
||||
found = it->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto db = load_db_locked();
|
||||
if (!db) {
|
||||
return std::unexpected{db.error()};
|
||||
}
|
||||
const auto it = db->actions.find(action_key);
|
||||
if (it == db->actions.end()) {
|
||||
if (!found) {
|
||||
return std::nullopt;
|
||||
}
|
||||
// Verify every referenced CAS blob still exists and is intact.
|
||||
for (const auto &[out_path, ref] : it->second.outputs) {
|
||||
for (const auto &[out_path, ref] : found->outputs) {
|
||||
auto ok = verify_blob(ref.sha256);
|
||||
if (!ok || !*ok) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
return it->second;
|
||||
return found;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
|
||||
Reference in new issue
Block a user