feat(state,registry): back StateStore with Void::Sqlite when available and filter audit queries
This commit is contained in:
1 parent
9a6f209f5b
commit
e990f44037
8 files changed
+387
-34
No files matched your search
+276
-22
@@ -174,7 +174,8 @@ struct StateStore::CacheState {
|
||||
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"}, cache_{std::make_shared<CacheState>()} {}
|
||||
sqlite_file_{root_ / "state" / "state.v2.sqlite"}, 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) {
|
||||
@@ -197,6 +198,43 @@ StateStore::open(const std::filesystem::path &vbs_dir) {
|
||||
return std::unexpected{lock.error()};
|
||||
}
|
||||
|
||||
#if defined(VBS_HAS_VOID_SQLITE)
|
||||
// Durable transactional action cache backed by Void::Sqlite (Track E4). The legacy JSON
|
||||
// database is imported once when it exists and the SQLite database is still empty.
|
||||
auto sqlite = Void::Sqlite::Database::open(store.sqlite_file_);
|
||||
if (!sqlite) {
|
||||
return std::unexpected{sqlite.error()};
|
||||
}
|
||||
store.sqlite_ = std::move(*sqlite);
|
||||
if (auto schema = store.sqlite_->exec(
|
||||
"CREATE TABLE IF NOT EXISTS meta(key TEXT PRIMARY KEY, value TEXT NOT NULL);"
|
||||
"CREATE TABLE IF NOT EXISTS actions(action_key TEXT PRIMARY KEY, payload TEXT NOT "
|
||||
"NULL);"
|
||||
"CREATE TABLE IF NOT EXISTS generations(sequence INTEGER PRIMARY KEY, payload TEXT "
|
||||
"NOT NULL);");
|
||||
!schema) {
|
||||
return std::unexpected{schema.error()};
|
||||
}
|
||||
auto action_count = store.sqlite_->prepare("SELECT COUNT(*) FROM actions");
|
||||
if (!action_count) {
|
||||
return std::unexpected{action_count.error()};
|
||||
}
|
||||
auto has_row = action_count->step();
|
||||
if (!has_row) {
|
||||
return std::unexpected{has_row.error()};
|
||||
}
|
||||
const auto counted = *has_row ? action_count->column_int(0) : 0;
|
||||
if (counted == 0 && std::filesystem::exists(store.db_file_, ec)) {
|
||||
auto legacy = store.load_json_db_locked();
|
||||
if (!legacy) {
|
||||
return std::unexpected{legacy.error()};
|
||||
}
|
||||
if (auto imported = store.save_sqlite_db_locked(*legacy); !imported) {
|
||||
return std::unexpected{imported.error()};
|
||||
}
|
||||
}
|
||||
return store;
|
||||
#else
|
||||
if (std::filesystem::exists(store.db_file_, ec)) {
|
||||
auto loaded = store.load_db_locked();
|
||||
if (!loaded) {
|
||||
@@ -210,6 +248,7 @@ StateStore::open(const std::filesystem::path &vbs_dir) {
|
||||
}
|
||||
}
|
||||
return store;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::filesystem::path StateStore::cas_blob_path(std::string_view sha256) const {
|
||||
@@ -219,32 +258,140 @@ std::filesystem::path StateStore::cas_blob_path(std::string_view sha256) const {
|
||||
return cas_dir_ / std::string{sha256.substr(0, 2)} / std::string{sha256};
|
||||
}
|
||||
|
||||
#if defined(VBS_HAS_VOID_SQLITE)
|
||||
namespace {
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error>
|
||||
read_column_text(const Void::Sqlite::RowView &row, int column) {
|
||||
auto text = row.get_text(column);
|
||||
if (!text) {
|
||||
return std::unexpected{text.error()};
|
||||
}
|
||||
return std::move(*text);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
#endif
|
||||
|
||||
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;
|
||||
#if defined(VBS_HAS_VOID_SQLITE)
|
||||
if (sqlite_) {
|
||||
DatabaseSnapshot snap;
|
||||
auto meta = sqlite_->prepare("SELECT key, value FROM meta");
|
||||
if (!meta) {
|
||||
return std::unexpected{meta.error()};
|
||||
}
|
||||
while (true) {
|
||||
auto has_row = meta->step();
|
||||
if (!has_row) {
|
||||
return std::unexpected{has_row.error()};
|
||||
}
|
||||
if (!*has_row) {
|
||||
break;
|
||||
}
|
||||
const auto row = meta->current_row();
|
||||
if (!row) {
|
||||
return std::unexpected{row.error()};
|
||||
}
|
||||
auto key = read_column_text(*row, 0);
|
||||
auto value = read_column_text(*row, 1);
|
||||
if (!key || !value) {
|
||||
return std::unexpected{key ? value.error() : key.error()};
|
||||
}
|
||||
if (*key == "schema_version") {
|
||||
snap.schema_version = std::stoi(*value);
|
||||
} else if (*key == "next_sequence") {
|
||||
snap.next_sequence = std::stoull(*value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto content = Void::FileSystem::read_file_bounded(db_file_, {128ULL * 1024ULL * 1024ULL});
|
||||
if (!content) {
|
||||
return std::unexpected{content.error()};
|
||||
auto actions = sqlite_->prepare("SELECT payload FROM actions");
|
||||
if (!actions) {
|
||||
return std::unexpected{actions.error()};
|
||||
}
|
||||
while (true) {
|
||||
auto has_row = actions->step();
|
||||
if (!has_row) {
|
||||
return std::unexpected{has_row.error()};
|
||||
}
|
||||
if (!*has_row) {
|
||||
break;
|
||||
}
|
||||
const auto action_row = actions->current_row();
|
||||
if (!action_row) {
|
||||
return std::unexpected{action_row.error()};
|
||||
}
|
||||
auto payload = read_column_text(*action_row, 0);
|
||||
if (!payload) {
|
||||
return std::unexpected{payload.error()};
|
||||
}
|
||||
auto records = parse_database_snapshot(
|
||||
"{\"schema_version\":1,\"actions\":[" + *payload + "]}", "sqlite actions");
|
||||
if (!records) {
|
||||
return std::unexpected{records.error()};
|
||||
}
|
||||
if (records->actions.empty()) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"StateStore: empty SQLite action payload")};
|
||||
}
|
||||
auto record = records->actions.begin();
|
||||
snap.actions[record->first] = std::move(record->second);
|
||||
}
|
||||
|
||||
auto generations = sqlite_->prepare("SELECT payload FROM generations ORDER BY sequence");
|
||||
if (!generations) {
|
||||
return std::unexpected{generations.error()};
|
||||
}
|
||||
while (true) {
|
||||
auto has_row = generations->step();
|
||||
if (!has_row) {
|
||||
return std::unexpected{has_row.error()};
|
||||
}
|
||||
if (!*has_row) {
|
||||
break;
|
||||
}
|
||||
const auto generation_row = generations->current_row();
|
||||
if (!generation_row) {
|
||||
return std::unexpected{generation_row.error()};
|
||||
}
|
||||
auto payload = read_column_text(*generation_row, 0);
|
||||
if (!payload) {
|
||||
return std::unexpected{payload.error()};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*payload);
|
||||
if (!parsed) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"StateStore: corrupt SQLite generation payload")};
|
||||
}
|
||||
const auto *obj = std::get_if<JsonVal::Object>(&parsed->value);
|
||||
if (!obj) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"StateStore: generation payload must be object")};
|
||||
}
|
||||
GraphGenerationRecord gen;
|
||||
gen.generation_id = get_str(*obj, "generation_id").value_or("");
|
||||
gen.profile_name = get_str(*obj, "profile_name").value_or("");
|
||||
gen.profile_identity = get_str(*obj, "profile_identity").value_or("");
|
||||
gen.snapshot_id = get_str(*obj, "snapshot_id").value_or("");
|
||||
gen.status = get_str(*obj, "status").value_or("invalid");
|
||||
gen.diagnostic = get_str(*obj, "diagnostic").value_or("");
|
||||
gen.sequence = static_cast<std::uint64_t>(get_i64(*obj, "sequence").value_or(0));
|
||||
snap.generations.push_back(std::move(gen));
|
||||
}
|
||||
return snap;
|
||||
}
|
||||
if (!Util::is_valid_utf8(*content)) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error,
|
||||
std::format("StateStore: corrupt state database '{}' (invalid UTF-8)",
|
||||
db_file_.string()))};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*content);
|
||||
#endif
|
||||
return load_json_db_locked();
|
||||
}
|
||||
|
||||
Void::Result::Result<StateStore::DatabaseSnapshot, Void::Result::Error>
|
||||
StateStore::parse_database_snapshot(std::string_view content, std::string_view origin) {
|
||||
auto parsed = Void::Json::Strict::parse(content);
|
||||
if (!parsed) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error,
|
||||
std::format("StateStore: corrupt state database '{}' (JSON parse failure)",
|
||||
db_file_.string()))};
|
||||
return std::unexpected{make_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("StateStore: corrupt state database '{}' (JSON parse failure)", origin))};
|
||||
}
|
||||
const auto *root_obj = std::get_if<JsonVal::Object>(&parsed->value);
|
||||
if (!root_obj) {
|
||||
@@ -364,10 +511,37 @@ StateStore::load_db_locked() const {
|
||||
snap.generations.push_back(std::move(gen));
|
||||
}
|
||||
}
|
||||
return snap;
|
||||
}
|
||||
|
||||
Void::Result::Result<StateStore::DatabaseSnapshot, Void::Result::Error>
|
||||
StateStore::load_json_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()};
|
||||
}
|
||||
if (!Util::is_valid_utf8(*content)) {
|
||||
return std::unexpected{
|
||||
make_error(Void::System::SystemErrorCode::io_error,
|
||||
std::format("StateStore: corrupt state database '{}' (invalid UTF-8)",
|
||||
db_file_.string()))};
|
||||
}
|
||||
auto snap = parse_database_snapshot(*content, db_file_.string());
|
||||
if (!snap) {
|
||||
return std::unexpected{snap.error()};
|
||||
}
|
||||
|
||||
if (cur_fp && cache_) {
|
||||
std::lock_guard lk{cache_->mutex};
|
||||
cache_->db = snap;
|
||||
cache_->db = *snap;
|
||||
cache_->fp = *cur_fp;
|
||||
cache_->valid = true;
|
||||
}
|
||||
@@ -375,8 +549,88 @@ StateStore::load_db_locked() const {
|
||||
return snap;
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
StateStore::save_sqlite_db_locked(const DatabaseSnapshot &db) const {
|
||||
#if defined(VBS_HAS_VOID_SQLITE)
|
||||
if (!sqlite_) {
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
"StateStore: SQLite database is not open")};
|
||||
}
|
||||
auto transaction = sqlite_->begin();
|
||||
if (!transaction) {
|
||||
return std::unexpected{transaction.error()};
|
||||
}
|
||||
if (auto cleared = sqlite_->exec("DELETE FROM actions; DELETE FROM generations;"); !cleared) {
|
||||
(void)transaction->rollback_checked();
|
||||
return std::unexpected{cleared.error()};
|
||||
}
|
||||
|
||||
auto upsert_meta = sqlite_->prepare("INSERT INTO meta(key, value) VALUES(?1, ?2) "
|
||||
"ON CONFLICT(key) DO UPDATE SET value = excluded.value");
|
||||
auto insert_action =
|
||||
sqlite_->prepare("INSERT INTO actions(action_key, payload) VALUES(?1, ?2)");
|
||||
auto insert_generation =
|
||||
sqlite_->prepare("INSERT INTO generations(sequence, payload) VALUES(?1, ?2)");
|
||||
if (!upsert_meta || !insert_action || !insert_generation) {
|
||||
(void)transaction->rollback_checked();
|
||||
return std::unexpected{!(upsert_meta) ? upsert_meta.error()
|
||||
: !(insert_action) ? insert_action.error()
|
||||
: insert_generation.error()};
|
||||
}
|
||||
|
||||
const auto bind_meta = [&](std::string_view key, const std::string &value) -> bool {
|
||||
upsert_meta->bind(1, key).bind(2, value);
|
||||
const auto ran = upsert_meta->step();
|
||||
upsert_meta->reset();
|
||||
// step() reports whether a row was produced; SQLITE_DONE (no row) is success here.
|
||||
return ran.has_value();
|
||||
};
|
||||
if (!bind_meta("schema_version", std::to_string(db.schema_version)) ||
|
||||
!bind_meta("next_sequence", std::to_string(db.next_sequence))) {
|
||||
(void)transaction->rollback_checked();
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
|
||||
"StateStore: failed writing SQLite metadata")};
|
||||
}
|
||||
|
||||
for (const auto &[key, record] : db.actions) {
|
||||
insert_action->bind(1, key).bind(2, serialize_action(record));
|
||||
auto ran = insert_action->step();
|
||||
insert_action->reset();
|
||||
if (!ran) {
|
||||
(void)transaction->rollback_checked();
|
||||
return std::unexpected{ran.error()};
|
||||
}
|
||||
}
|
||||
for (const auto &generation : db.generations) {
|
||||
insert_generation->bind(1, static_cast<std::int64_t>(generation.sequence))
|
||||
.bind(2, serialize_generation(generation));
|
||||
auto ran = insert_generation->step();
|
||||
insert_generation->reset();
|
||||
if (!ran) {
|
||||
(void)transaction->rollback_checked();
|
||||
return std::unexpected{ran.error()};
|
||||
}
|
||||
}
|
||||
|
||||
auto committed = transaction->commit();
|
||||
if (!committed) {
|
||||
return std::unexpected{committed.error()};
|
||||
}
|
||||
return {};
|
||||
#else
|
||||
static_cast<void>(db);
|
||||
return std::unexpected{make_error(Void::System::SystemErrorCode::unsupported,
|
||||
"StateStore: built without Void::Sqlite")};
|
||||
#endif
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
StateStore::save_db_locked(const DatabaseSnapshot &db) const {
|
||||
#if defined(VBS_HAS_VOID_SQLITE)
|
||||
if (sqlite_) {
|
||||
return save_sqlite_db_locked(db);
|
||||
}
|
||||
#endif
|
||||
std::string json = "{\"schema_version\":" + std::to_string(db.schema_version) +
|
||||
",\"next_sequence\":" + std::to_string(db.next_sequence) + ",\"actions\":[";
|
||||
bool first = true;
|
||||
|
||||
Reference in new issue
Block a user