Separate database connections from file-backed host information and document handoff
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m25s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m25s
This commit is contained in:
+8
-7
@@ -1,14 +1,15 @@
|
||||
# Cargo 工作区
|
||||
|
||||
本目录包含四个 crate。Cargo 包名与目录一致,均不带项目前缀;路径依赖定义在根 Cargo.toml。
|
||||
包名与目录一致,路径依赖定义在根 Cargo.toml。
|
||||
|
||||
| 目录 | 包名/产物 | 职责 |
|
||||
| --- | --- | --- |
|
||||
| [core](core/README.md) | core | 零外部依赖的对象运行时、堆与字节码 VM |
|
||||
| [storage](storage/README.md) | storage | 宿主元数据与 SQLite/PostgreSQL 适配,不依赖 core |
|
||||
| [cli](cli/README.md) | cli,二进制 cli | 引导演示和 metadata 命令 |
|
||||
| [server](server/README.md) | server | 最小常驻 HTTP 宿主 |
|
||||
| [core](core/README.md) | core,库 endor_core | 零外部依赖的对象运行时、堆与字节码 VM |
|
||||
| [host](host/README.md) | host | UTF-8 JSON 文件基础信息,与数据库分离 |
|
||||
| [storage](storage/README.md) | storage | 为数据库特殊类保留的显式连接适配,不提供基础信息存储 |
|
||||
| [cli](cli/README.md) | cli,二进制 cli | 引导演示与文件信息命令,不使用数据库 |
|
||||
| [server](server/README.md) | server,二进制 server | HTTP 宿主与文件版本信息,不使用数据库 |
|
||||
|
||||
core 的 Rust 库目标明确命名为 endor_core,避免与 Rust 标准库 core 重名;storage 的引用名为 storage,两个二进制分别为 cli、server。Cargo 通过 [lib] 自定义库名,详见 [Cargo 目标说明](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field)。
|
||||
CLI 和 Server 依赖 Core 与 Host,不依赖 Storage;Core 与 Storage 也互不依赖。未来特殊类的宿主实现按需依赖 Storage,见 [数据库对象](../docs/runtime/database-objects.md)。
|
||||
|
||||
CLI/Server 依赖 Core 和 Storage,Core 与 Storage 互不依赖。网络、数据库、浏览器、进程配置和信号留在宿主。全局说明见 [架构](../docs/architecture/README.md),查功能位置从 [文档索引](../docs/README.md) 进入。返回 [项目入口](../README.md)。
|
||||
core 的 Rust 库名为 endor_core,避免与标准库 core 重名,见 [Cargo 目标说明](https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field)。网络、数据库、进程配置和信号不进入 Core。全局说明见 [架构](../docs/architecture/README.md),返回 [项目入口](../README.md)。
|
||||
|
||||
@@ -11,8 +11,7 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
core.workspace = true
|
||||
storage.workspace = true
|
||||
tokio.workspace = true
|
||||
host.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# cli
|
||||
|
||||
命令行宿主。bootstrap 演示两个运行时对象并释放;metadata put/get 使用独立 Storage,不让数据库配置进入 Core。
|
||||
同步命令行宿主,提供 bootstrap、help 和文件 metadata 命令。bootstrap 演示两个运行时对象并释放,不读取数据库配置、不连接或写入数据库。
|
||||
|
||||
| 内容 | 职责 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | CLI 包、cli 二进制名称、core/storage/Tokio 依赖 |
|
||||
| [src](src/README.md) | 参数路由、命令执行和退出码 |
|
||||
| Cargo.toml | cli 二进制与 core/host 依赖;无 storage 或 Tokio 依赖 |
|
||||
| [src](src/README.md) | 参数路由、引导演示和退出码 |
|
||||
|
||||
用法见 [运行指南](../../docs/operations/README.md),文本参数与环境边界见 [配置](../../docs/operations/configuration.md)。此 CLI 当前不是 Endor 源码解释命令。返回 [工作区](../README.md)。
|
||||
运行方式见 [运行指南](../../docs/operations/README.md),输入边界见 [配置](../../docs/operations/configuration.md)。当前不是 Endor 源码解释命令,metadata put/get 和只导入缺失项的 import 均使用文件,见 [宿主信息](../../docs/operations/host-information.md)。返回 [工作区](../README.md)。
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# CLI 入口
|
||||
|
||||
[main.rs](main.rs) 是唯一入口:main 将 run 的结果转成进程退出码;run 解析 bootstrap、metadata put/get 和 help,错误打印到 stderr 并返回失败。
|
||||
[main.rs](main.rs) 是唯一入口:同步 main 将 run 的结果转成进程退出码;run 处理 bootstrap、help、文件 metadata put/get/import,错误打印到 stderr 并返回失败。
|
||||
|
||||
bootstrap 创建并显式释放两个对象,不打开数据库。metadata 每次读取 StorageConfig、连接、操作并关闭池。参数目前由 std::env::args 读取,非 Unicode 参数可能 panic,现状及后续边界见 [配置](../../../docs/operations/configuration.md)。
|
||||
bootstrap 创建并显式释放两个对象,不读取数据库环境、不打开数据库。未知命令报错;metadata 通过 host::MetadataStore 读写文件,import 从标准输入接收旧记录且不覆盖已有文件。参数目前由 std::env::args 读取,非 Unicode 参数可能 panic,边界见 [配置](../../../docs/operations/configuration.md)。
|
||||
|
||||
新增命令时更新用法、[运行指南](../../../docs/operations/README.md) 和 [smoke.py 的导航](../../../scripts/README.md)。返回 [cli](../README.md)。
|
||||
|
||||
+16
-14
@@ -2,13 +2,12 @@ use std::error::Error;
|
||||
use std::process::ExitCode;
|
||||
|
||||
use endor_core::{Value, Vm};
|
||||
use storage::{Storage, StorageConfig};
|
||||
use host::MetadataStore;
|
||||
|
||||
const USAGE: &str = "Usage: cli [bootstrap | metadata put <key> <value> | metadata get <key>]";
|
||||
const USAGE: &str = "Usage: cli [bootstrap | metadata put <key> <value> | metadata get <key> | metadata import | help]";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> ExitCode {
|
||||
match run().await {
|
||||
fn main() -> ExitCode {
|
||||
match run() {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(error) => {
|
||||
eprintln!("cli: {error}");
|
||||
@@ -17,7 +16,7 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Box<dyn Error>> {
|
||||
fn run() -> Result<(), Box<dyn Error>> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
match args
|
||||
.iter()
|
||||
@@ -43,22 +42,25 @@ async fn run() -> Result<(), Box<dyn Error>> {
|
||||
vm.release(first)?;
|
||||
vm.release(second)?;
|
||||
println!("released heap_objects={}", vm.object_count());
|
||||
println!("bootstrap is independent of host storage");
|
||||
println!("bootstrap uses no database");
|
||||
}
|
||||
["help"] | ["--help"] | ["-h"] => println!("{USAGE}"),
|
||||
["metadata", "put", key, value] => {
|
||||
let store = Storage::connect(&StorageConfig::from_env()?).await?;
|
||||
store.put(key, value).await?;
|
||||
println!("stored ({})", store.backend_name());
|
||||
store.close().await;
|
||||
MetadataStore::from_env()?.put(key, value)?;
|
||||
println!("stored (file)");
|
||||
}
|
||||
["metadata", "get", key] => {
|
||||
let store = Storage::connect(&StorageConfig::from_env()?).await?;
|
||||
println!(
|
||||
"{}",
|
||||
store.get(key).await?.as_deref().unwrap_or("(missing)")
|
||||
MetadataStore::from_env()?
|
||||
.get(key)?
|
||||
.as_deref()
|
||||
.unwrap_or("(missing)")
|
||||
);
|
||||
store.close().await;
|
||||
}
|
||||
["metadata", "import"] => {
|
||||
let count = MetadataStore::from_env()?.import_missing(std::io::stdin().lock())?;
|
||||
println!("imported {count} missing entries (file)");
|
||||
}
|
||||
_ => return Err(USAGE.into()),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "host"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -0,0 +1,11 @@
|
||||
# host
|
||||
|
||||
宿主基础信息的文件层,与 Core 和数据库 Storage 分开。CLI/Server 使用它保存 host.version、deployment.release 和其他命名信息;不打开数据库,不保存 VM 堆。
|
||||
|
||||
| 内容 | 职责 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | host 包、Serde/JSON 依赖及远端检查依赖 |
|
||||
| [src](src/README.md) | MetadataStore、UTF-8 JSON 文件与迁移导入 |
|
||||
| [tests](tests/README.md) | 持久读取、替换、并发、坏记录、路径边界和幂等导入 |
|
||||
|
||||
ASTER_STATE_DIR 指定信息目录;默认 data/host,容器使用 /data/host。协议、生命周期及旧数据库一次性迁移见 [宿主信息](../../docs/operations/host-information.md)。返回 [工作区](../README.md)。
|
||||
@@ -0,0 +1,8 @@
|
||||
# Host 源码导航
|
||||
|
||||
| 文件 | 职责 |
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | 导出 MetadataStore,明确文件层不依赖 VM 或 SQL |
|
||||
| [metadata.rs](metadata.rs) | 目录配置、键校验、逐键 JSON 读取/同步写入、原子发布、仅导入缺失项 |
|
||||
|
||||
文件数据以 UTF-8 JSON 保存,操作系统路径用 PathBuf;坏记录不自动覆盖。每个键独立发布,不提供多键事务。旧库导入不覆盖新文件,规则见 [宿主信息](../../../docs/operations/host-information.md)。返回 [host](../README.md)。
|
||||
@@ -0,0 +1,4 @@
|
||||
//! Host information stored in UTF-8 JSON files, independently of the VM and SQL.
|
||||
mod metadata;
|
||||
|
||||
pub use metadata::MetadataStore;
|
||||
@@ -0,0 +1,172 @@
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::{self, Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
static NEXT_TEMP: AtomicU64 = AtomicU64::new(0);
|
||||
const FORMAT_VERSION: u32 = 1;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LegacyRecord {
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Record {
|
||||
format_version: u32,
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
/// One independently replaceable JSON file per ASCII metadata key. Clones
|
||||
/// share a directory, not a cached map; reads always observe the file system.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MetadataStore {
|
||||
directory: PathBuf,
|
||||
}
|
||||
|
||||
impl MetadataStore {
|
||||
/// Does not create the directory until the first write.
|
||||
pub fn new(directory: impl Into<PathBuf>) -> io::Result<Self> {
|
||||
let directory = directory.into();
|
||||
if directory.as_os_str().is_empty() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"metadata directory is empty",
|
||||
));
|
||||
}
|
||||
Ok(Self { directory })
|
||||
}
|
||||
|
||||
/// Preserve OS paths without requiring the environment value to be UTF-8.
|
||||
pub fn from_env() -> io::Result<Self> {
|
||||
Self::new(
|
||||
std::env::var_os("ASTER_STATE_DIR")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| PathBuf::from("data/host")),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn directory(&self) -> &Path {
|
||||
&self.directory
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &str) -> io::Result<Option<String>> {
|
||||
let path = self.path(key)?;
|
||||
let bytes = match fs::read(path) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let record: Record = serde_json::from_slice(&bytes)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
if record.format_version != FORMAT_VERSION || record.key != key {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"metadata key or format version mismatch",
|
||||
));
|
||||
}
|
||||
Ok(Some(record.value))
|
||||
}
|
||||
|
||||
/// Writes a new sibling file, syncs it, then publishes it with rename.
|
||||
/// Different keys do not share a read/modify/write map. For the same key,
|
||||
/// the last successful rename wins. This is not a multi-key transaction.
|
||||
pub fn put(&self, key: &str, value: &str) -> io::Result<()> {
|
||||
self.store(key, value, true).map(|_| ())
|
||||
}
|
||||
|
||||
/// One-time migration input: a JSON array of key/value string records.
|
||||
/// Validate all keys first, and never overwrite an existing file record.
|
||||
pub fn import_missing(&self, reader: impl Read) -> io::Result<usize> {
|
||||
let records: Vec<LegacyRecord> = serde_json::from_reader(reader)
|
||||
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
|
||||
for record in &records {
|
||||
self.path(&record.key)?;
|
||||
}
|
||||
let mut imported = 0;
|
||||
for record in records {
|
||||
if self.store(&record.key, &record.value, false)? {
|
||||
imported += 1;
|
||||
} else if self.get(&record.key)?.is_none() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
"existing metadata disappeared during import",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(imported)
|
||||
}
|
||||
|
||||
fn store(&self, key: &str, value: &str, replace: bool) -> io::Result<bool> {
|
||||
let destination = self.path(key)?;
|
||||
let mut contents = serde_json::to_vec_pretty(&Record {
|
||||
format_version: FORMAT_VERSION,
|
||||
key: key.into(),
|
||||
value: value.into(),
|
||||
})
|
||||
.map_err(io::Error::other)?;
|
||||
contents.push(b'\n');
|
||||
fs::create_dir_all(&self.directory)?;
|
||||
for _ in 0..32 {
|
||||
let sequence = NEXT_TEMP.fetch_add(1, Ordering::Relaxed);
|
||||
let temporary = self
|
||||
.directory
|
||||
.join(format!(".{key}.{}.{sequence}.tmp", std::process::id()));
|
||||
let mut options = OpenOptions::new();
|
||||
options.write(true).create_new(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
options.mode(0o600);
|
||||
}
|
||||
let mut file = match options.open(&temporary) {
|
||||
Ok(file) => file,
|
||||
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
let result = (|| {
|
||||
file.write_all(&contents)?;
|
||||
file.sync_all()?;
|
||||
drop(file);
|
||||
if replace {
|
||||
fs::rename(&temporary, &destination)?;
|
||||
Ok(true)
|
||||
} else {
|
||||
// Atomic create-if-absent: a concurrent writer's record
|
||||
// cannot be replaced by the legacy import.
|
||||
match fs::hard_link(&temporary, &destination) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => Ok(false),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
})();
|
||||
let _ = fs::remove_file(&temporary);
|
||||
return result;
|
||||
}
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::AlreadyExists,
|
||||
"cannot reserve a metadata temporary file",
|
||||
))
|
||||
}
|
||||
|
||||
fn path(&self, key: &str) -> io::Result<PathBuf> {
|
||||
if key.is_empty()
|
||||
|| key.len() > 128
|
||||
|| !key.as_bytes()[0].is_ascii_alphanumeric()
|
||||
|| !key
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
|
||||
{
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"metadata keys must start with an ASCII letter/digit and contain at most 128 ASCII letters, digits, dots, underscores or hyphens",
|
||||
));
|
||||
}
|
||||
Ok(self.directory.join(format!("{key}.json")))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Host 文件信息检查
|
||||
|
||||
[metadata.rs](metadata.rs) 检查缺失读取不建目录、日文/中文/emoji 值和日文路径、重开与同键替换、非法键不越界、坏 JSON/版本/键不匹配不改原文件、并发替换可见完整记录,以及旧记录幂等导入和保留更新值。
|
||||
|
||||
检查使用临时目录,仅在指定远端环境运行。本地不执行文件行为测试;当前是否通过应以对应提交的 CI 为准。命令见 [验证](../../../docs/operations/validation.md),返回 [host](../README.md)。
|
||||
@@ -0,0 +1,131 @@
|
||||
use host::MetadataStore;
|
||||
|
||||
#[test]
|
||||
fn missing_reads_do_not_create_state() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("state");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
assert_eq!(store.get("missing").unwrap(), None);
|
||||
assert!(!directory.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utf8_values_survive_reopening_and_same_key_replacement() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("日本語");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
store.put("host.version", "0.1.0").unwrap();
|
||||
store.put("project.note", "日本語・中文・😀").unwrap();
|
||||
let reopened = MetadataStore::new(&directory).unwrap();
|
||||
assert_eq!(
|
||||
reopened.get("project.note").unwrap().as_deref(),
|
||||
Some("日本語・中文・😀")
|
||||
);
|
||||
reopened.put("host.version", "0.2.0").unwrap();
|
||||
assert_eq!(store.get("host.version").unwrap().as_deref(), Some("0.2.0"));
|
||||
assert_eq!(
|
||||
store.get("project.note").unwrap().as_deref(),
|
||||
Some("日本語・中文・😀")
|
||||
);
|
||||
let record: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(directory.join("project.note.json")).unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(record["format_version"], 1);
|
||||
assert_eq!(record["key"], "project.note");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys_cannot_escape_the_state_directory() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("state");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
for key in [
|
||||
"",
|
||||
"../outside",
|
||||
"/outside",
|
||||
"nested/key",
|
||||
"nested\\key",
|
||||
".hidden",
|
||||
"日本語",
|
||||
] {
|
||||
assert!(store.put(key, "invalid").is_err(), "{key}");
|
||||
assert!(store.get(key).is_err(), "{key}");
|
||||
}
|
||||
assert!(store.put(&"x".repeat(129), "invalid").is_err());
|
||||
assert!(!directory.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_or_mismatched_records_fail_without_being_rewritten() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let store = MetadataStore::new(temporary.path()).unwrap();
|
||||
let path = temporary.path().join("host.version.json");
|
||||
for bytes in [
|
||||
b"not-json".as_slice(),
|
||||
b"\xff".as_slice(),
|
||||
br#"{"format_version":2,"key":"host.version","value":"old"}"#.as_slice(),
|
||||
br#"{"format_version":1,"key":"wrong","value":"old"}"#.as_slice(),
|
||||
] {
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
assert!(store.get("host.version").is_err());
|
||||
assert_eq!(std::fs::read(&path).unwrap(), bytes);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_replacements_expose_complete_records_and_preserve_other_keys() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let store = MetadataStore::new(temporary.path()).unwrap();
|
||||
store.put("independent", "keep").unwrap();
|
||||
let a = "a".repeat(8192);
|
||||
let b = "b".repeat(8192);
|
||||
store.put("shared", &a).unwrap();
|
||||
std::thread::scope(|scope| {
|
||||
for value in [&a, &b] {
|
||||
let store = &store;
|
||||
scope.spawn(move || {
|
||||
for _ in 0..8 {
|
||||
store.put("shared", value).unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
for _ in 0..64 {
|
||||
let value = store.get("shared").unwrap().unwrap();
|
||||
assert!(value == a || value == b);
|
||||
}
|
||||
});
|
||||
assert_eq!(store.get("independent").unwrap().as_deref(), Some("keep"));
|
||||
assert!(std::fs::read_dir(temporary.path()).unwrap().all(|entry| {
|
||||
!entry
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.to_string_lossy()
|
||||
.ends_with(".tmp")
|
||||
}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_import_is_repeatable_and_preserves_newer_file_values() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let store = MetadataStore::new(temporary.path()).unwrap();
|
||||
store.put("host.version", "new").unwrap();
|
||||
let input =
|
||||
br#"[{"key":"host.version","value":"old"},{"key":"project.note","value":"legacy"}]"#;
|
||||
assert_eq!(store.import_missing(input.as_slice()).unwrap(), 1);
|
||||
assert_eq!(store.import_missing(input.as_slice()).unwrap(), 0);
|
||||
assert_eq!(store.get("host.version").unwrap().as_deref(), Some("new"));
|
||||
assert_eq!(
|
||||
store.get("project.note").unwrap().as_deref(),
|
||||
Some("legacy")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_import_keys_are_rejected_before_any_record_is_written() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let directory = temporary.path().join("state");
|
||||
let store = MetadataStore::new(&directory).unwrap();
|
||||
let input = br#"[{"key":"valid","value":"keep"},{"key":"../outside","value":"invalid"}]"#;
|
||||
assert!(store.import_missing(input.as_slice()).is_err());
|
||||
assert!(!directory.exists());
|
||||
}
|
||||
@@ -7,7 +7,7 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
core.workspace = true
|
||||
storage.workspace = true
|
||||
host.workspace = true
|
||||
axum.workspace = true
|
||||
serde.workspace = true
|
||||
tokio.workspace = true
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# server
|
||||
|
||||
最小常驻 HTTP 宿主,提供数据库健康检查和只读 VM 引导信息。它通过 Arc 共享 Vm,没有语言执行接口或可变 VM 请求上下文。
|
||||
最小常驻 HTTP 宿主,提供存活检查和只读 VM 信息。通过 Arc 共享 Vm,启动不读取数据库配置、不连接或写入数据库;通过 host 包把 host.version 写入文件。特殊类的数据库能力尚未接入 HTTP 宿主。
|
||||
|
||||
| 内容 | 职责 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | core/storage、Axum、Tokio、Serde 依赖 |
|
||||
| [src](src/README.md) | 启动顺序、AppState、路由、信号与退出 |
|
||||
| Cargo.toml | core/host、Axum、Tokio、Serde 依赖;无 storage 依赖 |
|
||||
| [src](src/README.md) | AppState、启动、路由、信号与退出 |
|
||||
|
||||
HTTP 返回内容和运行方式见 [运行指南](../../docs/operations/README.md),变量见 [配置](../../docs/operations/configuration.md),生产端口/数据库见 [部署](../../docs/operations/deployment.md)。返回 [工作区](../README.md)。
|
||||
HTTP 返回内容见 [运行指南](../../docs/operations/README.md),监听变量见 [配置](../../docs/operations/configuration.md),预置 PostgreSQL 资源见 [部署](../../docs/operations/deployment.md)。没有语言执行接口或可变 VM 请求上下文。返回 [工作区](../README.md)。
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
# Server 入口
|
||||
|
||||
[main.rs](main.rs) 包含当前所有宿主启动代码:
|
||||
[main.rs](main.rs) 包含所有宿主启动代码:
|
||||
|
||||
| 部分 | 职责 |
|
||||
| --- | --- |
|
||||
| AppState | 共享只读 Arc<Vm> 与可克隆的 Storage 连接池 |
|
||||
| main/run | 创建 VM、连接数据库、写 host.version、绑定地址、组装路由、服务结束后关闭存储 |
|
||||
| health | ping 数据库,返回 200/503 及后端标签 |
|
||||
| AppState | 共享只读 Arc<Vm> |
|
||||
| main/run | 创建空 VM、绑定地址、设置退出信号、写文件 host.version、组装路由;不创建数据库连接 |
|
||||
| health | 返回 200 和 status=ok,仅检查 HTTP 宿主可响应 |
|
||||
| bootstrap | 返回对象模型、type_system=false 和堆对象数量,不分配对象 |
|
||||
| shutdown_signal | Ctrl-C;Unix 上同时接受 SIGTERM,触发 Axum 优雅退出 |
|
||||
| tests | 关闭数据库池后健康状态变为 unavailable |
|
||||
|
||||
改变端点时同步 [运行指南](../../../docs/operations/README.md) 与 [smoke.py](../../../scripts/README.md);改变启动变量时同步 [配置](../../../docs/operations/configuration.md)。网络、信号和数据库逻辑不进入 Core。返回 [server](../README.md)。
|
||||
/health 不表示 PostgreSQL、特殊类查询或执行脚本正常;数据库检查应属于实际消费连接的对象。宿主独立于数据库的行为由远端 smoke.py 验证,见 [验证](../../../docs/operations/validation.md)。改变端点同步 [运行指南](../../../docs/operations/README.md) 和 [scripts](../../../scripts/README.md)。返回 [server](../README.md)。
|
||||
|
||||
@@ -3,21 +3,19 @@ use std::future::Future;
|
||||
use std::process::ExitCode;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{Json, Router, extract::State, http::StatusCode, routing::get};
|
||||
use axum::{Json, Router, extract::State, routing::get};
|
||||
use endor_core::Vm;
|
||||
use host::MetadataStore;
|
||||
use serde::Serialize;
|
||||
use storage::{Storage, StorageConfig};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
vm: Arc<Vm>,
|
||||
storage: Storage,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Health {
|
||||
status: &'static str,
|
||||
storage: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -40,44 +38,23 @@ async fn main() -> ExitCode {
|
||||
|
||||
async fn run() -> Result<(), Box<dyn Error>> {
|
||||
let vm = Arc::new(Vm::new()?);
|
||||
let storage = Storage::connect(&StorageConfig::from_env()?).await?;
|
||||
storage
|
||||
.put("host.version", env!("CARGO_PKG_VERSION"))
|
||||
.await?;
|
||||
let address = std::env::var("ASTER_LISTEN").unwrap_or_else(|_| "127.0.0.1:8080".into());
|
||||
let listener = tokio::net::TcpListener::bind(&address).await?;
|
||||
let shutdown = shutdown_signal()?;
|
||||
MetadataStore::from_env()?.put("host.version", env!("CARGO_PKG_VERSION"))?;
|
||||
let app = Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/bootstrap", get(bootstrap))
|
||||
.with_state(AppState {
|
||||
vm,
|
||||
storage: storage.clone(),
|
||||
});
|
||||
println!(
|
||||
"Endor host listening on {} (storage={})",
|
||||
listener.local_addr()?,
|
||||
storage.backend_name()
|
||||
);
|
||||
.with_state(AppState { vm });
|
||||
println!("Endor host listening on {}", listener.local_addr()?);
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown)
|
||||
.await?;
|
||||
storage.close().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn health(State(state): State<AppState>) -> (StatusCode, Json<Health>) {
|
||||
let (code, status) = match state.storage.ping().await {
|
||||
Ok(()) => (StatusCode::OK, "ok"),
|
||||
Err(_) => (StatusCode::SERVICE_UNAVAILABLE, "storage_unavailable"),
|
||||
};
|
||||
(
|
||||
code,
|
||||
Json(Health {
|
||||
status,
|
||||
storage: state.storage.backend_name(),
|
||||
}),
|
||||
)
|
||||
async fn health() -> Json<Health> {
|
||||
Json(Health { status: "ok" })
|
||||
}
|
||||
|
||||
async fn bootstrap(State(state): State<AppState>) -> Json<BootstrapInfo> {
|
||||
@@ -98,25 +75,3 @@ fn shutdown_signal() -> std::io::Result<impl Future<Output = ()>> {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_reports_unavailable_after_storage_closes() {
|
||||
let storage = Storage::connect(&StorageConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
let state = AppState {
|
||||
vm: Arc::new(Vm::new().unwrap()),
|
||||
storage,
|
||||
};
|
||||
let (status, _) = health(State(state.clone())).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
state.storage.close().await;
|
||||
let (status, Json(body)) = health(State(state)).await;
|
||||
assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE);
|
||||
assert_eq!(body.status, "storage_unavailable");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
# storage
|
||||
|
||||
宿主元数据存储,依赖 SQLx/Tokio,不依赖 VM 内核。一个 Storage 选择 SQLite 或 PostgreSQL;明确配置 PostgreSQL 后失败不回退。
|
||||
按需数据库连接适配层,供将来需要数据库的特殊类或宿主适配器使用。依赖 SQLx/Tokio,不依赖 VM 内核;普通对象、CLI 和 Server 不连接这层。
|
||||
|
||||
| 内容 | 入口 |
|
||||
| --- | --- |
|
||||
| Cargo.toml | SQLx、Tokio 和检查所需依赖 |
|
||||
| [src](src/README.md) | 配置解析、连接池、元数据表和操作 |
|
||||
| [tests](tests/README.md) | 配置、持久化、隔离与专用 PostgreSQL 检查 |
|
||||
| Cargo.toml | SQLx、Tokio 与远端检查依赖 |
|
||||
| [src](src/README.md) | 显式 DatabaseConfig、连接池与关闭 |
|
||||
| [tests](tests/README.md) | 连接不建表、调用方 SQL、隔离/持久化、真实 PostgreSQL |
|
||||
|
||||
Storage 仅保存 key/value 元数据,不保存 EndorObject 或执行帧。运行与连接规则见 [运行指南](../../docs/operations/README.md),环境优先级与错误规则见 [配置](../../docs/operations/configuration.md),生产数据与迁移边界见 [部署](../../docs/operations/deployment.md)。返回 [工作区](../README.md)。
|
||||
没有默认数据库、自动环境变量读取、基础信息表或通用元数据 put/get。特殊类的名称、布局与查询接口尚未定义;当前只保留连接能力。设计边界见 [数据库对象](../../docs/runtime/database-objects.md),参数见 [配置](../../docs/operations/configuration.md)。返回 [工作区](../README.md)。
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
# Storage 源码导航
|
||||
# 数据库连接源码导航
|
||||
|
||||
| 文件 | 职责 |
|
||||
| --- | --- |
|
||||
| [lib.rs](lib.rs) | StorageConfig::from_values/from_env、StorageError、SQLite/PostgreSQL 连接池、schema 初始化、ping/put/get/close |
|
||||
| [schema.sql](schema.sql) | 当前 aster_metadata 文本 key/value 表,幂等建表 |
|
||||
[lib.rs](lib.rs) 是唯一实现文件:DatabaseConfig 显式选择 SQLite 文件/内存或 PostgreSQL;DatabaseConnection 提供 connect、sqlite_pool/postgres_pool、backend_name、ping 和 close;DatabaseError 对公开错误脱敏。
|
||||
|
||||
配置改动同步 [配置表](../../../docs/operations/configuration.md) 与 storage 测试。schema 改动须考虑已有数据库,当前没有版本化迁移;不得将 CREATE IF NOT EXISTS 当作已有结构升级。Storage 克隆共享连接池,close 影响所有克隆。
|
||||
connect 只建立连接池,不执行应用 DDL,不保存基础信息。没有 schema.sql、初始化表、put/get 或 from_env。业务表和查询由未来的数据库特殊类决定;本层不替它定义数据模型。
|
||||
|
||||
连接与持久化契约见 [运行指南](../../../docs/operations/README.md),测试定位见 [tests](../tests/README.md)。返回 [storage](../README.md)。
|
||||
克隆共享连接池,close 会关闭所有克隆;具体生命周期及限制见 [数据库对象](../../../docs/runtime/database-objects.md)。配置变动同步 [配置](../../../docs/operations/configuration.md) 和 [tests](../tests/README.md)。返回 [storage](../README.md)。
|
||||
|
||||
+41
-109
@@ -1,4 +1,5 @@
|
||||
//! Host metadata storage; deliberately has no dependency on core.
|
||||
//! Explicit database connections for database-backed objects.
|
||||
//! Connecting never creates application tables or stores VM/host metadata.
|
||||
use std::fmt;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
@@ -8,17 +9,15 @@ use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
|
||||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
|
||||
use sqlx::{PgPool, SqlitePool};
|
||||
|
||||
const SCHEMA: &str = include_str!("schema.sql");
|
||||
|
||||
/// An explicit PostgreSQL configuration never falls back to SQLite.
|
||||
#[derive(Clone)]
|
||||
pub enum StorageConfig {
|
||||
pub enum DatabaseConfig {
|
||||
SqliteFile(PathBuf),
|
||||
SqliteMemory,
|
||||
Postgres(String),
|
||||
}
|
||||
|
||||
impl fmt::Debug for StorageConfig {
|
||||
impl fmt::Debug for DatabaseConfig {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::SqliteFile(path) => f.debug_tuple("SqliteFile").field(path).finish(),
|
||||
@@ -28,57 +27,14 @@ impl fmt::Debug for StorageConfig {
|
||||
}
|
||||
}
|
||||
|
||||
impl StorageConfig {
|
||||
/// DATABASE_URL accepts PostgreSQL only; SQLITE_PATH selects file or :memory:.
|
||||
/// Empty/invalid explicit values are errors rather than default selection.
|
||||
pub fn from_values(
|
||||
database_url: Option<&str>,
|
||||
sqlite_path: Option<&str>,
|
||||
) -> Result<Self, StorageError> {
|
||||
if let Some(url) = database_url {
|
||||
if !(url.starts_with("postgres://") || url.starts_with("postgresql://")) {
|
||||
return Err(StorageError::Config(
|
||||
"DATABASE_URL must be a PostgreSQL URL",
|
||||
));
|
||||
}
|
||||
return Ok(Self::Postgres(url.into()));
|
||||
}
|
||||
match sqlite_path.unwrap_or("data/aster.db") {
|
||||
"" => Err(StorageError::Config("SQLITE_PATH cannot be empty")),
|
||||
":memory:" => Ok(Self::SqliteMemory),
|
||||
path => Ok(Self::SqliteFile(path.into())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_env() -> Result<Self, StorageError> {
|
||||
let database_url = read_env("DATABASE_URL")?;
|
||||
// Irrelevant SQLite settings must not prevent PostgreSQL configuration.
|
||||
if database_url.is_some() {
|
||||
return Self::from_values(database_url.as_deref(), None);
|
||||
}
|
||||
let sqlite_path = read_env("SQLITE_PATH")?;
|
||||
Self::from_values(None, sqlite_path.as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
fn read_env(name: &str) -> Result<Option<String>, StorageError> {
|
||||
match std::env::var(name) {
|
||||
Ok(value) => Ok(Some(value)),
|
||||
Err(std::env::VarError::NotPresent) => Ok(None),
|
||||
Err(std::env::VarError::NotUnicode(_)) => Err(StorageError::Config(
|
||||
"storage environment must be valid Unicode",
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum StorageError {
|
||||
pub enum DatabaseError {
|
||||
Config(&'static str),
|
||||
Directory(std::io::Error),
|
||||
Database(sqlx::Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for StorageError {
|
||||
impl fmt::Display for DatabaseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Config(message) => f.write_str(message),
|
||||
@@ -91,7 +47,7 @@ impl fmt::Display for StorageError {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for StorageError {
|
||||
impl std::error::Error for DatabaseError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Config(_) => None,
|
||||
@@ -101,7 +57,7 @@ impl std::error::Error for StorageError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for StorageError {
|
||||
impl From<sqlx::Error> for DatabaseError {
|
||||
fn from(error: sqlx::Error) -> Self {
|
||||
Self::Database(error)
|
||||
}
|
||||
@@ -113,20 +69,26 @@ enum Backend {
|
||||
Postgres(PgPool),
|
||||
}
|
||||
|
||||
/// Clones share a pool. A memory SQLite store uses exactly one connection, so
|
||||
/// initialization and every operation see the same database for its lifetime.
|
||||
/// Created only on explicit request by an object or host adapter. Clones share
|
||||
/// a pool; close() closes it for all users. There is no automatic schema or data.
|
||||
/// In-memory SQLite uses one connection to preserve its database lifetime.
|
||||
#[derive(Clone)]
|
||||
pub struct Storage {
|
||||
pub struct DatabaseConnection {
|
||||
backend: Backend,
|
||||
}
|
||||
|
||||
impl Storage {
|
||||
/// Connects eagerly and initializes the v1 metadata schema before returning.
|
||||
pub async fn connect(config: &StorageConfig) -> Result<Self, StorageError> {
|
||||
impl DatabaseConnection {
|
||||
/// Connects eagerly to the explicit backend without executing application DDL.
|
||||
pub async fn connect(config: &DatabaseConfig) -> Result<Self, DatabaseError> {
|
||||
let backend = match config {
|
||||
StorageConfig::Postgres(url) => {
|
||||
DatabaseConfig::Postgres(url) => {
|
||||
if !(url.starts_with("postgres://") || url.starts_with("postgresql://")) {
|
||||
return Err(DatabaseError::Config(
|
||||
"expected a PostgreSQL connection URL",
|
||||
));
|
||||
}
|
||||
let options = PgConnectOptions::from_str(url)
|
||||
.map_err(|_| StorageError::Config("invalid PostgreSQL connection URL"))?;
|
||||
.map_err(|_| DatabaseError::Config("invalid PostgreSQL connection URL"))?;
|
||||
Backend::Postgres(
|
||||
PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
@@ -135,16 +97,16 @@ impl Storage {
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
StorageConfig::SqliteFile(path) => {
|
||||
DatabaseConfig::SqliteFile(path) => {
|
||||
if path.as_os_str().is_empty() || path == std::path::Path::new(":memory:") {
|
||||
return Err(StorageError::Config(
|
||||
"use SqliteMemory for an in-memory database",
|
||||
return Err(DatabaseError::Config(
|
||||
"expected a nonempty file path; use SqliteMemory for :memory:",
|
||||
));
|
||||
}
|
||||
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
|
||||
tokio::fs::create_dir_all(parent)
|
||||
.await
|
||||
.map_err(StorageError::Directory)?;
|
||||
.map_err(DatabaseError::Directory)?;
|
||||
}
|
||||
let options = SqliteConnectOptions::new()
|
||||
.filename(path)
|
||||
@@ -159,7 +121,7 @@ impl Storage {
|
||||
.await?,
|
||||
)
|
||||
}
|
||||
StorageConfig::SqliteMemory => {
|
||||
DatabaseConfig::SqliteMemory => {
|
||||
let options = SqliteConnectOptions::new().in_memory(true);
|
||||
Backend::Sqlite(
|
||||
SqlitePoolOptions::new()
|
||||
@@ -172,21 +134,22 @@ impl Storage {
|
||||
)
|
||||
}
|
||||
};
|
||||
let storage = Self { backend };
|
||||
storage.initialize().await?;
|
||||
Ok(storage)
|
||||
Ok(Self { backend })
|
||||
}
|
||||
|
||||
async fn initialize(&self) -> Result<(), StorageError> {
|
||||
/// Backend-specific access for the database-backed object implementation.
|
||||
pub fn sqlite_pool(&self) -> Option<&SqlitePool> {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => {
|
||||
sqlx::query(SCHEMA).execute(pool).await?;
|
||||
}
|
||||
Backend::Postgres(pool) => {
|
||||
sqlx::query(SCHEMA).execute(pool).await?;
|
||||
}
|
||||
Backend::Sqlite(pool) => Some(pool),
|
||||
Backend::Postgres(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn postgres_pool(&self) -> Option<&PgPool> {
|
||||
match &self.backend {
|
||||
Backend::Postgres(pool) => Some(pool),
|
||||
Backend::Sqlite(_) => None,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn backend_name(&self) -> &'static str {
|
||||
@@ -196,7 +159,7 @@ impl Storage {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn ping(&self) -> Result<(), StorageError> {
|
||||
pub async fn ping(&self) -> Result<(), DatabaseError> {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => {
|
||||
sqlx::query("SELECT 1").execute(pool).await?;
|
||||
@@ -208,38 +171,7 @@ impl Storage {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn put(&self, key: &str, value: &str) -> Result<(), StorageError> {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => {
|
||||
sqlx::query("INSERT INTO aster_metadata (key, value) VALUES (?1, ?2) ON CONFLICT (key) DO UPDATE SET value = excluded.value")
|
||||
.bind(key).bind(value).execute(pool).await?;
|
||||
}
|
||||
Backend::Postgres(pool) => {
|
||||
sqlx::query("INSERT INTO aster_metadata (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = excluded.value")
|
||||
.bind(key).bind(value).execute(pool).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get(&self, key: &str) -> Result<Option<String>, StorageError> {
|
||||
Ok(match &self.backend {
|
||||
Backend::Sqlite(pool) => {
|
||||
sqlx::query_scalar("SELECT value FROM aster_metadata WHERE key = ?1")
|
||||
.bind(key)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
}
|
||||
Backend::Postgres(pool) => {
|
||||
sqlx::query_scalar("SELECT value FROM aster_metadata WHERE key = $1")
|
||||
.bind(key)
|
||||
.fetch_optional(pool)
|
||||
.await?
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Explicitly closes the shared pool, including all Storage clones.
|
||||
/// Explicitly closes the shared pool, including all DatabaseConnection clones.
|
||||
pub async fn close(&self) {
|
||||
match &self.backend {
|
||||
Backend::Sqlite(pool) => pool.close().await,
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
CREATE TABLE IF NOT EXISTS aster_metadata (
|
||||
key TEXT PRIMARY KEY NOT NULL,
|
||||
value TEXT NOT NULL
|
||||
)
|
||||
@@ -1,8 +1,8 @@
|
||||
# Storage 行为检查
|
||||
# 数据库连接检查
|
||||
|
||||
| 文件 | 覆盖与前提 |
|
||||
| --- | --- |
|
||||
| [storage.rs](storage.rs) | 配置默认值/优先级/错误、内存共享与实例隔离、临时文件重连、目录错误;不处理 PostgreSQL 协议的 socket 仅检查连接超时和不降级 |
|
||||
| [postgres.rs](postgres.rs) | 真实 PostgreSQL 写入/覆盖/缺失键/重连;默认 ignore,显式运行需 ASTER_TEST_POSTGRES_URL 指向专用测试库 |
|
||||
| [storage.rs](storage.rs) | 显式错误配置、凭据脱敏、SQLite 连接后无应用表、克隆共享与独立内存隔离、调用方自建数据跨文件重连、目录错误、PostgreSQL 失败不降级 |
|
||||
| [postgres.rs](postgres.rs) | 真实连接后 public 无表;测试在事务中创建临时 class_rows、参数绑定和读取,提交自动删除临时表,再连接;默认 ignore,需全新专用 ASTER_TEST_POSTGRES_URL |
|
||||
|
||||
SQLite 文件检查使用临时目录;PostgreSQL 成功路径不使用生产数据库。ces-control 脚本提供一次性的测试数据库,见 [验证](../../../docs/operations/validation.md) 和 [scripts](../../../scripts/README.md)。不在本地运行或为测试下载 SQLite 依赖。返回 [storage](../README.md)。
|
||||
所有业务表都由测试自己创建,连接层不得自动初始化基础信息表。临时 SQLite 文件和 PostgreSQL 测试库由远端 CI 隔离,不使用生产数据。检查命令见 [验证](../../../docs/operations/validation.md),原生连接能力边界见 [数据库对象](../../../docs/runtime/database-objects.md)。本地不运行测试或下载依赖。返回 [storage](../README.md)。
|
||||
|
||||
@@ -1,35 +1,53 @@
|
||||
use storage::{Storage, StorageConfig};
|
||||
use storage::{DatabaseConfig, DatabaseConnection};
|
||||
|
||||
/// Run explicitly against a disposable database; never silently skip a missing URL.
|
||||
/// Run against a fresh disposable database, never a production or shared schema.
|
||||
#[tokio::test]
|
||||
#[ignore = "requires ASTER_TEST_POSTGRES_URL pointing to a test PostgreSQL database"]
|
||||
async fn postgres_roundtrip_and_reconnect() {
|
||||
#[ignore = "requires ASTER_TEST_POSTGRES_URL pointing to a fresh disposable PostgreSQL database"]
|
||||
async fn postgres_connects_without_schema_and_executes_caller_owned_sql() {
|
||||
let url = std::env::var("ASTER_TEST_POSTGRES_URL")
|
||||
.expect("set ASTER_TEST_POSTGRES_URL to a disposable PostgreSQL database");
|
||||
let config = StorageConfig::from_values(Some(&url), None).unwrap();
|
||||
let store = Storage::connect(&config).await.unwrap();
|
||||
assert_eq!(store.backend_name(), "postgresql");
|
||||
store.ping().await.unwrap();
|
||||
let key = format!(
|
||||
"test.{}.{}",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
);
|
||||
assert_eq!(store.get(&key).await.unwrap(), None);
|
||||
store.put(&key, "你好 ' ; $1").await.unwrap();
|
||||
assert_eq!(
|
||||
store.get(&key).await.unwrap().as_deref(),
|
||||
Some("你好 ' ; $1")
|
||||
);
|
||||
store.put(&key, "updated").await.unwrap();
|
||||
store.close().await;
|
||||
let reopened = Storage::connect(&config).await.unwrap();
|
||||
assert_eq!(
|
||||
reopened.get(&key).await.unwrap().as_deref(),
|
||||
Some("updated")
|
||||
);
|
||||
.expect("set ASTER_TEST_POSTGRES_URL to a fresh disposable PostgreSQL database");
|
||||
let config = DatabaseConfig::Postgres(url);
|
||||
let database = DatabaseConnection::connect(&config).await.unwrap();
|
||||
assert_eq!(database.backend_name(), "postgresql");
|
||||
assert!(database.sqlite_pool().is_none());
|
||||
database.ping().await.unwrap();
|
||||
let pool = database.postgres_pool().unwrap();
|
||||
let tables: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'",
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0, "connecting must not create application tables");
|
||||
let mut transaction = pool.begin().await.unwrap();
|
||||
sqlx::query(
|
||||
"CREATE TEMP TABLE class_rows (id INTEGER PRIMARY KEY, value TEXT NOT NULL) ON COMMIT DROP",
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO class_rows (id, value) VALUES ($1, $2)")
|
||||
.bind(1_i64)
|
||||
.bind("你好 ' ; $1")
|
||||
.execute(&mut *transaction)
|
||||
.await
|
||||
.unwrap();
|
||||
let value: String = sqlx::query_scalar("SELECT value FROM class_rows WHERE id = $1")
|
||||
.bind(1_i64)
|
||||
.fetch_one(&mut *transaction)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(value, "你好 ' ; $1");
|
||||
transaction.commit().await.unwrap();
|
||||
database.close().await;
|
||||
let reopened = DatabaseConnection::connect(&config).await.unwrap();
|
||||
reopened.ping().await.unwrap();
|
||||
let tables: i64 = sqlx::query_scalar(
|
||||
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'",
|
||||
)
|
||||
.fetch_one(reopened.postgres_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0);
|
||||
reopened.close().await;
|
||||
}
|
||||
|
||||
@@ -1,69 +1,104 @@
|
||||
use storage::{Storage, StorageConfig};
|
||||
use storage::{DatabaseConfig, DatabaseConnection};
|
||||
|
||||
#[test]
|
||||
fn configuration_defaults_precedence_and_errors() {
|
||||
assert!(
|
||||
matches!(StorageConfig::from_values(None, None).unwrap(), StorageConfig::SqliteFile(p) if p.to_str() == Some("data/aster.db"))
|
||||
);
|
||||
assert!(matches!(
|
||||
StorageConfig::from_values(None, Some(":memory:")).unwrap(),
|
||||
StorageConfig::SqliteMemory
|
||||
));
|
||||
assert!(matches!(
|
||||
StorageConfig::from_values(Some("postgresql://localhost/aster"), Some("")).unwrap(),
|
||||
StorageConfig::Postgres(_)
|
||||
));
|
||||
assert!(StorageConfig::from_values(Some(""), None).is_err());
|
||||
assert!(StorageConfig::from_values(Some("mysql://localhost/aster"), None).is_err());
|
||||
assert!(StorageConfig::from_values(None, Some("")).is_err());
|
||||
let config = StorageConfig::Postgres("postgres://user:secret@localhost/db".into());
|
||||
fn postgres_configuration_debug_redacts_credentials() {
|
||||
let config = DatabaseConfig::Postgres("postgres://user:secret@localhost/db".into());
|
||||
assert!(!format!("{config:?}").contains("secret"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn memory_store_is_shared_by_clones_but_isolated_between_stores() {
|
||||
let store = Storage::connect(&StorageConfig::SqliteMemory)
|
||||
async fn explicit_invalid_backends_fail_without_fallback() {
|
||||
for config in [
|
||||
DatabaseConfig::Postgres(String::new()),
|
||||
DatabaseConfig::Postgres("mysql://localhost/db".into()),
|
||||
DatabaseConfig::SqliteFile("".into()),
|
||||
DatabaseConfig::SqliteFile(":memory:".into()),
|
||||
] {
|
||||
assert!(DatabaseConnection::connect(&config).await.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn memory_connections_start_without_tables_and_clones_share_the_pool() {
|
||||
let database = DatabaseConnection::connect(&DatabaseConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
let other = Storage::connect(&StorageConfig::SqliteMemory)
|
||||
let other = DatabaseConnection::connect(&DatabaseConfig::SqliteMemory)
|
||||
.await
|
||||
.unwrap();
|
||||
store.ping().await.unwrap();
|
||||
assert_eq!(store.get("missing").await.unwrap(), None);
|
||||
store.put("message", "你好 ' ? $1 ;").await.unwrap();
|
||||
assert_eq!(
|
||||
store.clone().get("message").await.unwrap().as_deref(),
|
||||
Some("你好 ' ? $1 ;")
|
||||
);
|
||||
assert_eq!(other.get("message").await.unwrap(), None);
|
||||
store.put("message", "updated").await.unwrap();
|
||||
assert_eq!(
|
||||
store.get("message").await.unwrap().as_deref(),
|
||||
Some("updated")
|
||||
);
|
||||
store.close().await;
|
||||
assert!(database.postgres_pool().is_none());
|
||||
let pool = database.sqlite_pool().unwrap();
|
||||
let tables: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0, "connecting must not create application tables");
|
||||
// This schema belongs to the test's simulated database-backed object.
|
||||
sqlx::query("CREATE TABLE class_rows (id INTEGER PRIMARY KEY, value TEXT NOT NULL)")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO class_rows (id, value) VALUES (?, ?)")
|
||||
.bind(1_i64)
|
||||
.bind("你好 ' ? $1 ;")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
let cloned = database.clone();
|
||||
let value: String = sqlx::query_scalar("SELECT value FROM class_rows WHERE id = ?")
|
||||
.bind(1_i64)
|
||||
.fetch_one(cloned.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(value, "你好 ' ? $1 ;");
|
||||
let other_tables: i64 =
|
||||
sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_one(other.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(other_tables, 0);
|
||||
database.close().await;
|
||||
assert!(cloned.ping().await.is_err());
|
||||
other.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sqlite_file_creates_parents_and_survives_reopen() {
|
||||
async fn explicit_sqlite_file_preserves_only_caller_created_data_across_reconnects() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("nested/data/metadata.db");
|
||||
let config = StorageConfig::SqliteFile(path.clone());
|
||||
let store = Storage::connect(&config).await.unwrap();
|
||||
store.put("project", "Aster VM").await.unwrap();
|
||||
store.close().await;
|
||||
let path = dir.path().join("nested/data/class.db");
|
||||
let config = DatabaseConfig::SqliteFile(path.clone());
|
||||
let database = DatabaseConnection::connect(&config).await.unwrap();
|
||||
let pool = database.sqlite_pool().unwrap();
|
||||
let tables: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, 0);
|
||||
sqlx::query("CREATE TABLE class_rows (id INTEGER PRIMARY KEY, value TEXT NOT NULL)")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO class_rows (id, value) VALUES (?, ?)")
|
||||
.bind(1_i64)
|
||||
.bind("caller-owned data")
|
||||
.execute(pool)
|
||||
.await
|
||||
.unwrap();
|
||||
database.close().await;
|
||||
assert!(path.is_file());
|
||||
let reopened = Storage::connect(&config).await.unwrap();
|
||||
assert_eq!(
|
||||
reopened.get("project").await.unwrap().as_deref(),
|
||||
Some("Aster VM")
|
||||
);
|
||||
reopened.put("project", "v2").await.unwrap();
|
||||
assert_eq!(
|
||||
reopened.get("project").await.unwrap().as_deref(),
|
||||
Some("v2")
|
||||
);
|
||||
let reopened = DatabaseConnection::connect(&config).await.unwrap();
|
||||
let value: String = sqlx::query_scalar("SELECT value FROM class_rows WHERE id = ?")
|
||||
.bind(1_i64)
|
||||
.fetch_one(reopened.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(value, "caller-owned data");
|
||||
let tables: Vec<String> =
|
||||
sqlx::query_scalar("SELECT name FROM sqlite_master WHERE type = 'table'")
|
||||
.fetch_all(reopened.sqlite_pool().unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(tables, vec!["class_rows"]);
|
||||
reopened.close().await;
|
||||
}
|
||||
|
||||
@@ -73,7 +108,7 @@ async fn invalid_sqlite_directory_reports_error() {
|
||||
let file = dir.path().join("file");
|
||||
std::fs::write(&file, "occupied").unwrap();
|
||||
assert!(
|
||||
Storage::connect(&StorageConfig::SqliteFile(file.join("aster.db")))
|
||||
DatabaseConnection::connect(&DatabaseConfig::SqliteFile(file.join("class.db")))
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
@@ -81,14 +116,12 @@ async fn invalid_sqlite_directory_reports_error() {
|
||||
|
||||
#[tokio::test]
|
||||
async fn postgres_connection_failure_is_an_error() {
|
||||
// Hold a TCP port without serving PostgreSQL: deterministic local failure.
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!(
|
||||
"postgres://aster:secret@{}/aster?sslmode=disable",
|
||||
listener.local_addr().unwrap()
|
||||
);
|
||||
let result = Storage::connect(&StorageConfig::Postgres(url)).await;
|
||||
let error = match result {
|
||||
let error = match DatabaseConnection::connect(&DatabaseConfig::Postgres(url)).await {
|
||||
Ok(_) => panic!("unreachable PostgreSQL must fail"),
|
||||
Err(error) => error,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user