v0.3.2-pre.007

This commit is contained in:
2026-08-29 21:40:57 +02:00
parent c4f56d9e85
commit e39cf656b2
19 changed files with 849 additions and 53 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-store-lib/src/error.rs
// version: 3
// version: 4
/// Error code reserved for operations attempted after a Store backend has entered its closed state.
pub const ERROR_CODE_BACKEND_CLOSED: ksp_store_api::ErrorCode = ksp_store_api::ErrorCode::new("store", "backend_closed");
@@ -11,6 +11,8 @@ pub const ERROR_CODE_BACKEND_OPEN_FAILED: ksp_store_api::ErrorCode = ksp_store_a
pub const ERROR_CODE_POSTGRES_CONFIG_INVALID: ksp_store_api::ErrorCode = ksp_store_api::ErrorCode::new("store", "postgres_config_invalid");
/// Error code used when PostgreSQL physical connection establishment fails without exposing remote or credential details.
pub const ERROR_CODE_POSTGRES_CONNECT_FAILED: ksp_store_api::ErrorCode = ksp_store_api::ErrorCode::new("store", "postgres_connect_failed");
/// Error code used when a lightweight PostgreSQL health/readiness probe fails safely.
pub const ERROR_CODE_POSTGRES_HEALTH_FAILED: ksp_store_api::ErrorCode = ksp_store_api::ErrorCode::new("store", "postgres_health_failed");
/// Error code used when PostgreSQL migration/bootstrap execution fails without exposing server text or SQL.
pub const ERROR_CODE_POSTGRES_MIGRATION_FAILED: ksp_store_api::ErrorCode = ksp_store_api::ErrorCode::new("store", "postgres_migration_failed");
/// Error code used when persisted PostgreSQL migration history diverges from the embedded immutable KSP history.

View File

@@ -0,0 +1,132 @@
// file: crates/ksp-store-lib/src/health.rs
// version: 1
/// Portable Store health state independent from the selected physical backend.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum StoreHealthState {
/// The selected Store backend answered the bounded readiness probe and its migration foundation is current.
Ready,
/// The Store instance exists but its latest bounded readiness probe could not prove readiness.
NotReady,
}
/// Safe synchronous Store runtime snapshot without performing backend I/O.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StoreRuntimeSnapshot {
backend_kind: crate::StoreBackendKind,
network: ksp_store_api::RawNetworkId,
pool_available: u32,
pool_capacity: u32,
pool_size: u32,
pool_waiting: u32,
}
impl StoreRuntimeSnapshot {
/// Creates one portable runtime projection from backend-owned safe counters.
#[must_use]
pub(crate) fn new(
backend_kind: crate::StoreBackendKind,
network: ksp_store_api::RawNetworkId,
pool_capacity: u32,
pool_size: u32,
pool_available: u32,
pool_waiting: u32,
) -> Self {
return Self { backend_kind, network, pool_available, pool_capacity, pool_size, pool_waiting };
}
/// Returns the selected backend identity.
#[must_use]
pub const fn backend_kind(&self) -> crate::StoreBackendKind {
return self.backend_kind;
}
/// Returns the one logical network bound to this Store instance.
#[must_use]
pub const fn network(&self) -> &ksp_store_api::RawNetworkId {
return &self.network;
}
/// Returns the number of currently available pooled backend objects.
#[must_use]
pub const fn pool_available(&self) -> u32 {
return self.pool_available;
}
/// Returns the configured maximum pooled backend object count.
#[must_use]
pub const fn pool_capacity(&self) -> u32 {
return self.pool_capacity;
}
/// Returns the current pooled backend object count.
#[must_use]
pub const fn pool_size(&self) -> u32 {
return self.pool_size;
}
/// Returns the number of tasks currently waiting for a pooled backend object.
#[must_use]
pub const fn pool_waiting(&self) -> u32 {
return self.pool_waiting;
}
}
/// Portable Store readiness projection containing only safe runtime and migration diagnostics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StoreHealthSnapshot {
last_error_code: std::option::Option<ksp_store_api::ErrorCode>,
migration_version: std::option::Option<u64>,
pending_migration_count: u32,
runtime: StoreRuntimeSnapshot,
state: StoreHealthState,
}
impl StoreHealthSnapshot {
/// Creates one safe health projection from already classified backend diagnostics.
#[must_use]
pub(crate) fn new(
state: StoreHealthState,
runtime: StoreRuntimeSnapshot,
migration_version: std::option::Option<u64>,
pending_migration_count: u32,
last_error_code: std::option::Option<ksp_store_api::ErrorCode>,
) -> Self {
return Self { last_error_code, migration_version, pending_migration_count, runtime, state };
}
/// Returns the latest safe error code when readiness could not be proven.
#[must_use]
pub const fn last_error_code(&self) -> std::option::Option<ksp_store_api::ErrorCode> {
return self.last_error_code;
}
/// Returns the migration version observed by the readiness probe when available.
#[must_use]
pub const fn migration_version(&self) -> std::option::Option<u64> {
return self.migration_version;
}
/// Returns the number of embedded migrations newer than the observed applied version.
#[must_use]
pub const fn pending_migration_count(&self) -> u32 {
return self.pending_migration_count;
}
/// Returns the safe synchronous runtime projection captured for this health probe.
#[must_use]
pub const fn runtime(&self) -> &StoreRuntimeSnapshot {
return &self.runtime;
}
/// Returns whether this probe proved the Store ready.
#[must_use]
pub const fn state(&self) -> StoreHealthState {
return self.state;
}
}
#[cfg(test)]
#[path = "../unit_tests/health.rs"]
mod tests;

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-store-lib/src/lib.rs
// version: 5
// version: 6
#![warn(missing_docs)]
#![deny(unreachable_pub)]
@@ -7,9 +7,10 @@
//! Common backend-neutral Store runtime facade for KSP.
//!
//! `0.3.2-pre.006` materializes the physical PostgreSQL runtime plus its private
//! migration/bootstrap foundation: bounded pool, explicit TLS, versioned SHA-256
//! history and no business persistence schema.
//! `0.3.2-pre.007` closes the physical PostgreSQL runtime composition with a
//! portable safe runtime snapshot and lightweight health/readiness projection,
//! while retaining the private migration/bootstrap foundation and no business
//! persistence schema.
//!
//! The default `postgres` feature compiles the official PostgreSQL backend as
//! an optional implementation dependency. No backend implementation type is
@@ -17,6 +18,7 @@
mod constants;
mod error;
mod health;
mod settings;
mod store;
@@ -30,6 +32,8 @@ pub use self::error::ERROR_CODE_BACKEND_OPEN_FAILED;
pub use self::error::ERROR_CODE_POSTGRES_CONFIG_INVALID;
/// Error code used when PostgreSQL physical connection establishment fails.
pub use self::error::ERROR_CODE_POSTGRES_CONNECT_FAILED;
/// Error code used when a lightweight PostgreSQL health/readiness probe fails safely.
pub use self::error::ERROR_CODE_POSTGRES_HEALTH_FAILED;
/// Error code used when PostgreSQL migration/bootstrap execution fails safely.
pub use self::error::ERROR_CODE_POSTGRES_MIGRATION_FAILED;
/// Error code used when PostgreSQL migration history diverges from the embedded immutable KSP history.
@@ -44,6 +48,12 @@ pub use self::error::ERROR_CODE_POSTGRES_TLS_FAILED;
pub use self::error::ERROR_CODE_SETTINGS_INVALID;
/// Error code used when explicit Store shutdown exceeds its configured deadline.
pub use self::error::ERROR_CODE_SHUTDOWN_TIMEOUT;
/// Portable Store health/readiness projection containing only safe diagnostics.
pub use self::health::StoreHealthSnapshot;
/// Portable Store health state independent from physical backend types.
pub use self::health::StoreHealthState;
/// Safe synchronous Store runtime snapshot containing backend-neutral pool counters.
pub use self::health::StoreRuntimeSnapshot;
/// Bounded PostgreSQL bootstrap and migration settings owned by the Store facade.
pub use self::settings::PostgresBootstrapSettings;
/// Bounded PostgreSQL connection-pool settings owned by the Store facade.

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-store-lib/src/store.rs
// version: 3
// version: 4
/// Opaque common Store runtime facade.
///
@@ -37,6 +37,44 @@ impl Store {
};
}
/// Returns a safe synchronous runtime snapshot without performing backend I/O.
#[must_use]
pub fn runtime_snapshot(&self) -> crate::StoreRuntimeSnapshot {
#[cfg(feature = "postgres")]
{
return match &self.runtime {
StoreRuntime::Postgres(backend) => map_postgres_runtime_snapshot(backend.runtime_snapshot(), self.backend_kind, self.network.clone()),
};
}
#[cfg(not(feature = "postgres"))]
{
return crate::StoreRuntimeSnapshot::new(self.backend_kind, self.network.clone(), 0, 0, 0, 0);
}
}
/// Runs the selected backend's lightweight bounded readiness probe and returns only portable redacted diagnostics.
pub async fn health(&self) -> crate::StoreHealthSnapshot {
#[cfg(feature = "postgres")]
{
return match &self.runtime {
StoreRuntime::Postgres(backend) => {
let snapshot = backend.health().await;
map_postgres_health_snapshot(snapshot, self.backend_kind, self.network.clone())
},
};
}
#[cfg(not(feature = "postgres"))]
{
return crate::StoreHealthSnapshot::new(
crate::StoreHealthState::NotReady,
self.runtime_snapshot(),
std::option::Option::None,
0,
std::option::Option::Some(crate::ERROR_CODE_BACKEND_NOT_COMPILED),
);
}
}
/// Explicitly closes the Store runtime, consumes its facade handle and applies the configured bounded shutdown deadline.
pub async fn close(self) -> ksp_store_api::Result<()> {
let backend_kind = self.backend_kind;
@@ -136,9 +174,47 @@ async fn open_postgres(
#[cfg(feature = "postgres")]
fn map_postgres_error(error: ksp_store_postgres_lib::PostgresBackendError, backend_kind: crate::StoreBackendKind, network: &str) -> ksp_store_api::Error {
let code = match error.kind() {
let code = postgres_error_code(error.kind());
return ksp_store_api::Error::new(code, "PostgreSQL Store backend lifecycle operation failed")
.with_context("backend", backend_kind.code())
.with_context("network", network)
.with_context("phase", error.phase());
}
#[cfg(feature = "postgres")]
fn map_postgres_runtime_snapshot(
snapshot: ksp_store_postgres_lib::PostgresBackendRuntimeSnapshot,
backend_kind: crate::StoreBackendKind,
network: ksp_store_api::RawNetworkId,
) -> crate::StoreRuntimeSnapshot {
return crate::StoreRuntimeSnapshot::new(
backend_kind,
network,
snapshot.pool_capacity(),
snapshot.pool_size(),
snapshot.pool_available(),
snapshot.pool_waiting(),
);
}
#[cfg(feature = "postgres")]
fn map_postgres_health_snapshot(
snapshot: ksp_store_postgres_lib::PostgresBackendHealthSnapshot,
backend_kind: crate::StoreBackendKind,
network: ksp_store_api::RawNetworkId,
) -> crate::StoreHealthSnapshot {
let state = if snapshot.is_ready() { crate::StoreHealthState::Ready } else { crate::StoreHealthState::NotReady };
let error_code = snapshot.error_kind().map(|kind| return postgres_error_code(kind));
let runtime = map_postgres_runtime_snapshot(snapshot.runtime().clone(), backend_kind, network);
return crate::StoreHealthSnapshot::new(state, runtime, snapshot.migration_version(), snapshot.pending_migration_count(), error_code);
}
#[cfg(feature = "postgres")]
fn postgres_error_code(kind: ksp_store_postgres_lib::PostgresBackendErrorKind) -> ksp_store_api::ErrorCode {
return match kind {
ksp_store_postgres_lib::PostgresBackendErrorKind::ConfigInvalid => crate::ERROR_CODE_POSTGRES_CONFIG_INVALID,
ksp_store_postgres_lib::PostgresBackendErrorKind::ConnectFailed => crate::ERROR_CODE_POSTGRES_CONNECT_FAILED,
ksp_store_postgres_lib::PostgresBackendErrorKind::HealthFailed => crate::ERROR_CODE_POSTGRES_HEALTH_FAILED,
ksp_store_postgres_lib::PostgresBackendErrorKind::PoolTimeout => crate::ERROR_CODE_POSTGRES_POOL_TIMEOUT,
ksp_store_postgres_lib::PostgresBackendErrorKind::MigrationFailed => crate::ERROR_CODE_POSTGRES_MIGRATION_FAILED,
ksp_store_postgres_lib::PostgresBackendErrorKind::MigrationMismatch => crate::ERROR_CODE_POSTGRES_MIGRATION_MISMATCH,
@@ -147,10 +223,6 @@ fn map_postgres_error(error: ksp_store_postgres_lib::PostgresBackendError, backe
ksp_store_postgres_lib::PostgresBackendErrorKind::TlsFailed => crate::ERROR_CODE_POSTGRES_TLS_FAILED,
_ => crate::ERROR_CODE_BACKEND_OPEN_FAILED,
};
return ksp_store_api::Error::new(code, "PostgreSQL Store backend lifecycle operation failed")
.with_context("backend", backend_kind.code())
.with_context("network", network)
.with_context("phase", error.phase());
}
#[cfg(not(feature = "postgres"))]