#![allow(dead_code)]
use std::io::Cursor;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio_postgres::NoTls;
use crate::factory::StoreFactory;
use crate::postgres_column::harness_ready;
use crate::rows::{committed_file_count, patterned, LatchProtect, SetSource};
use alkblobs::{
Algorithm, Error, FleetSharedMedia, GcAbortCause, Key, LivenessRegistration, PgOpts, Store,
StoreMode,
};
pub(crate) const TUNED_THRESHOLD: u64 = 1024;
pub(crate) const KV_ONLY_THRESHOLD: u64 = 1024;
pub(crate) const WINDOW_DELAY: Duration = Duration::ZERO;
pub(crate) const TEST_TTL: Duration = Duration::from_millis(900);
pub(crate) const ONE_CADENCE: Duration = Duration::from_millis(600);
pub(crate) const EXPIRY_HORIZON: Duration = Duration::from_millis(1300);
fn wall_nanos() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos() as i64)
.unwrap_or(0)
}
fn fresh_schema_name(tag: &str, seq: &AtomicU64) -> String {
let seq = seq.fetch_add(1, Ordering::Relaxed);
format!(
"alkblobs_fleet_{}_{}_{}_{}",
tag,
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0),
seq,
)
}
fn seq_seed() -> u64 {
(std::time::SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.subsec_nanos() as u64)
.unwrap_or(0))
^ ((std::process::id() as u64) << 8)
}
fn opts(schema: String) -> PgOpts {
PgOpts {
schema,
..PgOpts::default()
}
}
fn fresh_root(tag: &str, seq: &AtomicU64) -> PathBuf {
let seq = seq.fetch_add(1, Ordering::Relaxed);
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let base = std::env::temp_dir().join(format!(
"alkblobs-fleet-column-{tag}-{}-{nanos:x}-{seq}",
std::process::id(),
));
std::fs::create_dir_all(&base).expect("temp site created");
base
}
fn remove_dir(base: &Path) -> Result<(), Error> {
match std::fs::remove_dir_all(base) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(Error::Io(e.to_string())),
}
}
#[derive(Clone)]
pub(crate) struct FleetSite {
dsn: String,
schema: String,
roots: Vec<PathBuf>,
}
impl FleetSite {
pub(crate) fn schema(&self) -> &str {
&self.schema
}
pub(crate) fn dsn(&self) -> &str {
&self.dsn
}
pub(crate) fn root(&self, instance: usize) -> &Path {
&self.roots[instance]
}
}
pub(crate) struct FleetDualTierFactory {
dsn: String,
threshold: u64,
schema_seq: AtomicU64,
schemas: Mutex<Vec<String>>,
sites: Mutex<Vec<PathBuf>>,
}
impl FleetDualTierFactory {
pub(crate) fn new(threshold: u64) -> FleetDualTierFactory {
FleetDualTierFactory {
dsn: crate::postgres_column::harness_dsn()
.expect("the harness server is configured for the column"),
threshold,
schema_seq: AtomicU64::new(seq_seed()),
schemas: Mutex::new(Vec::new()),
sites: Mutex::new(Vec::new()),
}
}
pub(crate) fn threshold(&self) -> u64 {
self.threshold
}
pub(crate) async fn open_fleet(&self) -> Result<(Store, Store, FleetSite), Error> {
let schema = fresh_schema_name("dual-tier", &self.schema_seq);
self.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(schema.clone());
let base_a = fresh_root("dual-tier-a", &self.schema_seq);
let base_b = fresh_root("dual-tier-b", &self.schema_seq);
let root_a = base_a.join("local-root");
let root_b = base_b.join("local-root");
let store_a = Store::dual_tier_postgres_local_fleet(
&self.dsn,
opts(schema.clone()),
root_a.clone(),
self.threshold,
WINDOW_DELAY,
TEST_TTL,
FleetSharedMedia::asserted(),
)
.await?;
let store_b = Store::dual_tier_postgres_local_fleet(
&self.dsn,
opts(schema.clone()),
root_b.clone(),
self.threshold,
WINDOW_DELAY,
TEST_TTL,
FleetSharedMedia::asserted(),
)
.await?;
self.sites
.lock()
.unwrap_or_else(|e| e.into_inner())
.extend([base_a, base_b]);
Ok((
store_a,
store_b,
FleetSite {
dsn: self.dsn.clone(),
schema,
roots: vec![root_a, root_b],
},
))
}
pub(crate) fn tracked_schemas(&self) -> Vec<String> {
self.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
pub(crate) fn dsn(&self) -> &str {
&self.dsn
}
pub(crate) async fn teardown(&self) -> Result<(), Error> {
let schemas = self
.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.drain(..)
.collect::<Vec<_>>();
let sites = self
.sites
.lock()
.unwrap_or_else(|e| e.into_inner())
.drain(..)
.collect::<Vec<_>>();
if !schemas.is_empty() {
let client = admin_connect(&self.dsn).await?;
for schema in &schemas {
let sql = format!("DROP SCHEMA IF EXISTS \"{schema}\" CASCADE");
client
.batch_execute(&sql)
.await
.map_err(|e| Error::Io(format!("postgres harness: {e}")))?;
}
}
for base in &sites {
remove_dir(base)?;
}
Ok(())
}
}
pub(crate) struct FleetKvOnlyFactory {
dsn: String,
pub(crate) threshold: u64,
schema_seq: AtomicU64,
schemas: Mutex<Vec<String>>,
}
impl FleetKvOnlyFactory {
pub(crate) fn new(threshold: u64) -> FleetKvOnlyFactory {
FleetKvOnlyFactory {
dsn: crate::postgres_column::harness_dsn()
.expect("the harness server is configured for the column"),
threshold,
schema_seq: AtomicU64::new(seq_seed()),
schemas: Mutex::new(Vec::new()),
}
}
pub(crate) async fn open_fleet(&self) -> Result<(Store, Store, FleetSite), Error> {
let schema = fresh_schema_name("kv-only", &self.schema_seq);
self.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(schema.clone());
let store_a = Store::kv_only_postgres_fleet(
&self.dsn,
opts(schema.clone()),
self.threshold,
WINDOW_DELAY,
TEST_TTL,
)
.await?;
let store_b = Store::kv_only_postgres_fleet(
&self.dsn,
opts(schema.clone()),
self.threshold,
WINDOW_DELAY,
TEST_TTL,
)
.await?;
Ok((
store_a,
store_b,
FleetSite {
dsn: self.dsn.clone(),
schema,
roots: Vec::new(),
},
))
}
pub(crate) fn tracked_schemas(&self) -> Vec<String> {
self.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
pub(crate) fn dsn(&self) -> &str {
&self.dsn
}
pub(crate) async fn teardown(&self) -> Result<(), Error> {
let schemas = self
.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.drain(..)
.collect::<Vec<_>>();
if !schemas.is_empty() {
let client = admin_connect(&self.dsn).await?;
for schema in &schemas {
let sql = format!("DROP SCHEMA IF EXISTS \"{schema}\" CASCADE");
client
.batch_execute(&sql)
.await
.map_err(|e| Error::Io(format!("postgres harness: {e}")))?;
}
}
Ok(())
}
}
impl StoreFactory for FleetKvOnlyFactory {
fn name(&self) -> &'static str {
"fleet kv-only postgres"
}
async fn open(&self) -> Result<Store, Error> {
let schema = fresh_schema_name("kv-only", &self.schema_seq);
self.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(schema.clone());
Store::kv_only_postgres_fleet(
&self.dsn,
opts(schema),
self.threshold,
WINDOW_DELAY,
TEST_TTL,
)
.await
}
async fn teardown(&self) -> Result<(), Error> {
self.teardown_impl().await
}
}
impl FleetKvOnlyFactory {
async fn teardown_impl(&self) -> Result<(), Error> {
let schemas = self
.schemas
.lock()
.unwrap_or_else(|e| e.into_inner())
.drain(..)
.collect::<Vec<_>>();
if !schemas.is_empty() {
let client = admin_connect(&self.dsn).await?;
for schema in &schemas {
let sql = format!("DROP SCHEMA IF EXISTS \"{schema}\" CASCADE");
client
.batch_execute(&sql)
.await
.map_err(|e| Error::Io(format!("postgres harness: {e}")))?;
}
}
Ok(())
}
}
async fn admin_connect(dsn: &str) -> Result<tokio_postgres::Client, Error> {
let (client, connection) = tokio_postgres::connect(dsn, NoTls)
.await
.map_err(|e| Error::Io(format!("postgres harness: {e}")))?;
tokio::spawn(async move {
let _ = connection.await;
});
Ok(client)
}
struct Probe {
client: tokio_postgres::Client,
_conn: tokio::task::JoinHandle<()>,
}
async fn connect_probe(dsn: &str) -> Probe {
let (client, connection) = tokio_postgres::connect(dsn, NoTls)
.await
.expect("the independent connection connects");
Probe {
client,
_conn: tokio::spawn(async move {
let _ = connection.await;
}),
}
}
impl Probe {
async fn scalar(&self, sql: &str) -> i64 {
let row = self
.client
.query_one(sql, &[])
.await
.expect("the oracle query succeeds");
row.get::<_, i64>(0)
}
async fn pool_rows(&self, schema: &str) -> usize {
let sql = format!("SELECT COUNT(*) FROM \"{schema}\".blobs");
self.scalar(&sql).await.max(0) as usize
}
async fn window_rows(&self, schema: &str) -> usize {
let sql = format!("SELECT COUNT(*) FROM \"{schema}\".gc_window");
self.scalar(&sql).await.max(0) as usize
}
async fn pin_rows(&self, schema: &str) -> Vec<(Vec<u8>, Vec<u8>, i64)> {
let sql =
format!("SELECT key, owner, expiry FROM \"{schema}\".gc_pins ORDER BY key, owner");
let rows = self
.client
.query(sql.as_str(), &[])
.await
.expect("the oracle query succeeds");
rows.iter()
.map(|row| {
(
row.get::<_, Vec<u8>>(0),
row.get::<_, Vec<u8>>(1),
row.get::<_, i64>(2),
)
})
.collect()
}
async fn schema_exists(&self, schema: &str) -> bool {
let count: i64 = self
.client
.query_one(
"SELECT count(*) FROM pg_namespace WHERE nspname = $1",
&[&schema],
)
.await
.expect("the oracle query succeeds")
.get(0);
count > 0
}
async fn table_exists(&self, schema: &str, table: &str) -> bool {
let row = self
.client
.query_one(
"SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = $1 AND tablename = $2)",
&[&schema, &table],
)
.await
.expect("the oracle query succeeds");
row.get::<_, bool>(0)
}
async fn create_liveness_table(&self, schema: &str, table: &str) {
let sql = format!("CREATE TABLE \"{schema}\".\"{table}\" (key bytea PRIMARY KEY)");
self.client
.execute(sql.as_str(), &[])
.await
.expect("the embedder DDL succeeds");
}
async fn insert_live_row(&self, schema: &str, table: &str, key: &[u8]) {
let sql = format!("INSERT INTO \"{schema}\".\"{table}\" (key) VALUES ($1)");
self.client
.execute(sql.as_str(), &[&key])
.await
.expect("the live row inserts");
}
async fn expire_pin_rows(&self, schema: &str, key: &[u8]) {
let expired = wall_nanos() - 1_000_000_000;
let sql = format!("UPDATE \"{schema}\".gc_pins SET expiry = $1 WHERE key = $2");
self.client
.execute(sql.as_str(), &[&expired, &key])
.await
.expect("the manual expiry succeeds");
}
async fn pin_expiry(&self, schema: &str, key: &[u8]) -> Option<i64> {
let sql = format!("SELECT expiry FROM \"{schema}\".gc_pins WHERE key = $1");
let rows = self
.client
.query(sql.as_str(), &[&key])
.await
.expect("the oracle query succeeds");
rows.into_iter().next().map(|row| row.get::<_, i64>(0))
}
}
pub async fn every_fleet_valid_configuration_constructs(
kv_only: &FleetKvOnlyFactory,
dual_tier: &FleetDualTierFactory,
) {
let (a, b, site) = kv_only
.open_fleet()
.await
.expect("the kv-only fleet constructs (large = none, the re-routing arm)");
assert_eq!(a.mode(), StoreMode::KvOnly);
assert_eq!(b.mode(), StoreMode::KvOnly);
let content = b"the kv-only fleet client's entry".to_vec();
let (_, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the kv-only fleet put publishes");
drop(pin);
let (_, re_pin) = b
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the second instance's fleet re-put publishes");
drop(re_pin);
let probe = connect_probe(site.dsn()).await;
assert_eq!(
probe.pool_rows(&site.schema).await,
1,
"the fleet re-put dedups — one pool entry across the two instances"
);
let rows = probe.pin_rows(&site.schema).await;
assert_eq!(rows.len(), 2, "two instances, two owner rows on one pool");
assert_ne!(
rows[0].1, rows[1].1,
"the two instances carry distinct store-instance ids — the pin \
rows compose by owner"
);
kv_only
.teardown()
.await
.expect("teardown drops exactly the minted schemas");
assert!(
!probe.schema_exists(&site.schema).await,
"the fleet factory's schema is gone after teardown"
);
let (a, b, site) = dual_tier
.open_fleet()
.await
.expect("the dual-tier fleet constructs (large = local on the declared shared media)");
assert_eq!(a.mode(), StoreMode::DualTier);
assert_eq!(b.mode(), StoreMode::DualTier);
let content = patterned((dual_tier.threshold() - 1) as usize, 191);
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the dual-tier fleet put publishes");
drop(pin);
let (_, re_pin) = b
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the second instance's fleet re-put publishes");
drop(re_pin);
assert!(
b.has(&key.as_bytes()).await.unwrap(),
"the second instance sees the first's entry — one fleet, one pool"
);
let probe = connect_probe(site.dsn()).await;
assert_eq!(probe.pool_rows(&site.schema).await, 1, "one entry row");
let rows = probe.pin_rows(&site.schema).await;
assert_eq!(rows.len(), 2, "two owner rows");
assert_ne!(rows[0].1, rows[1].1, "distinct store-instance ids");
dual_tier
.teardown()
.await
.expect("teardown drops exactly the minted schemas");
}
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
pub async fn the_consolidation_fleet_constructs_over_one_shared_instance(
consolidation: &FleetConsolidationFactory,
) {
let (a, b, site) = consolidation
.open_fleet()
.await
.expect("the consolidation fleet constructs over one shared instance");
assert_eq!(a.mode(), StoreMode::DualTier);
assert_eq!(b.mode(), StoreMode::DualTier);
let content = patterned(2 * TUNED_THRESHOLD as usize + 7, 173);
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the consolidation fleet put publishes");
drop(pin);
let (_, re_pin) = b
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the second instance's fleet re-put publishes");
drop(re_pin);
assert!(
b.has(&key.as_bytes()).await.unwrap(),
"the second instance sees the first's entry — one fleet, one \
shared instance"
);
let probe = connect_probe(site.dsn()).await;
assert_eq!(
probe.schema_scalar(site.kv_schema(), "blobs").await,
0,
"the over-threshold entry rides the large tier — the kv pool \
holds no entry row"
);
let rows = probe.pin_rows(site.kv_schema()).await;
assert_eq!(rows.len(), 2, "two instances' pin rows on the kv host");
assert_ne!(rows[0].1, rows[1].1, "distinct store-instance ids");
consolidation
.teardown()
.await
.expect("teardown reaps the LOs, drops the schemas and the database");
}
pub async fn the_constructor_entry_guards_reject_the_degenerate_tunings() {
let Some(dsn) = crate::postgres_column::harness_dsn() else {
return;
};
let schema = "no_such_schema_zero_ttl";
let err = Store::kv_only_postgres_fleet(
&dsn,
opts(schema.to_owned()),
KV_ONLY_THRESHOLD,
WINDOW_DELAY,
Duration::ZERO,
)
.await
.err()
.expect("a zero pin TTL rejects at the kv-only fleet constructor");
assert!(
err.to_string().contains("must be positive"),
"the zero-TTL rejection is the constructor's, spelled for the \
embedder: {err}"
);
let err = Store::dual_tier_postgres_local_fleet(
&dsn,
opts(schema.to_owned()),
std::env::temp_dir().join("alkblobs-fleet-column-zero-ttl-root"),
TUNED_THRESHOLD,
WINDOW_DELAY,
Duration::ZERO,
FleetSharedMedia::asserted(),
)
.await
.err()
.expect("a zero pin TTL rejects at the dual-tier fleet constructor");
assert!(
err.to_string().contains("must be positive"),
"the zero-TTL rejection is the constructor's: {err}"
);
let err = Store::kv_only_postgres_fleet(
&dsn,
PgOpts {
schema: schema.to_owned(),
max_size: 1,
..PgOpts::default()
},
KV_ONLY_THRESHOLD,
WINDOW_DELAY,
TEST_TTL,
)
.await
.err()
.expect("a single-connection pool rejects at the fleet constructor");
assert!(
err.to_string().contains("at least 2 connections"),
"the pool-sizing guard is the rejection: {err}"
);
}
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
pub async fn the_consolidation_same_instance_probe_errors_closed(dsn: &str) {
let unreachable = "host=no-such-host.invalid port=1 user=x password=x dbname=x";
let err = Store::dual_tier_postgres_pglo_fleet(
dsn,
opts("no_such_schema_probe_closed".to_owned()),
unreachable,
opts("no_such_schema_probe_closed".to_owned()),
TUNED_THRESHOLD,
WINDOW_DELAY,
TEST_TTL,
)
.await
.err()
.expect("the unreachable large instance rejects at the constructor");
assert!(
matches!(err, Error::Io(_)),
"the probe failure surfaces as the operational constructor error \
it is (the probe errors closed): {err}"
);
}
pub async fn the_joint_entry_and_pin_tx_commits_both_or_neither(factory: &FleetDualTierFactory) {
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&a);
let content = patterned((factory.threshold() - 1) as usize, 191);
let canonical = Key::from(Algorithm::GitBlobSha256.derive(&content, content.len() as u64));
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the fleet put publishes");
assert_eq!(key, canonical, "the canonical derivation addresses the put");
let probe = connect_probe(site.dsn()).await;
assert_eq!(
(
probe.pool_rows(&site.schema).await,
probe.pin_rows(&site.schema).await.len()
),
(1, 1),
"the independent connection sees both — the entry row and its \
pin row committed together"
);
assert!(
probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.unwrap()
> wall_nanos(),
"the pin row's expiry is ahead of now — the TTL the store resolved"
);
assert!(
b.has(&key.as_bytes()).await.unwrap(),
"the second instance sees the entry — one fleet, one pool"
);
let err = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64 + 400),
Cursor::new(content.as_slice()),
)
.await
.expect_err("the over-fed mismatch rejects");
assert_eq!(err, Error::Verification, "declared-len mismatch rejects");
assert_eq!(
(
probe.pool_rows(&site.schema).await,
probe.pin_rows(&site.schema).await.len()
),
(1, 1),
"the failed put left neither — the pool and the pin set are \
exactly the successful put's"
);
drop(pin);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn a_dedup_reput_upsert_renews_and_a_fleet_reput_composes_a_second_row(
factory: &FleetDualTierFactory,
) {
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&a);
let content = patterned((factory.threshold() - 1) as usize, 167);
let canonical = Key::from(Algorithm::GitBlobSha256.derive(&content, content.len() as u64));
let (_, pin_one) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the first put publishes");
let probe = connect_probe(site.dsn()).await;
assert_eq!(probe.pool_rows(&site.schema).await, 1, "one entry row");
let first_rows = probe.pin_rows(&site.schema).await;
assert_eq!(first_rows.len(), 1, "instance a's one pin row");
drop(pin_one);
let (key_again, pin_two) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the dedup re-put publishes");
assert_eq!(key_again, canonical, "one derivation, one address");
assert_eq!(
probe.pool_rows(&site.schema).await,
1,
"the dedup re-put committed the pin row alone — no new entry row"
);
let renewed_rows = probe.pin_rows(&site.schema).await;
assert_eq!(
renewed_rows.len(),
1,
"the same instance's row, upsert-renewed"
);
assert!(
renewed_rows[0].2 > first_rows[0].2,
"the row upsert-renews (key, owner): the expiry moved forward"
);
drop(pin_two);
let (key_third, pin_three) = b
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the second instance's re-put publishes");
assert_eq!(key_third, canonical);
assert_eq!(
probe.pool_rows(&site.schema).await,
1,
"still one pool entry across instances"
);
let rows = probe.pin_rows(&site.schema).await;
assert_eq!(rows.len(), 2, "protection composes by row — two owners");
assert_ne!(rows[0].1, rows[1].1, "two store-instance ids, two rows");
drop(pin_three);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn the_pin_guards_ttl_third_renewal_moves_the_rows_expiry(
factory: &FleetDualTierFactory,
) {
let (a, _b, site) = factory.open_fleet().await.expect("open vends the fleet");
let content = patterned((factory.threshold() - 1) as usize, 149);
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the fleet put publishes");
let probe = connect_probe(site.dsn()).await;
let initial = probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the put's pin row");
tokio::time::sleep(ONE_CADENCE).await;
let renewed = probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the row");
assert!(
renewed > initial,
"the guard's TTL/3 duty moved the row's expiry forward \
(initial {initial}, renewed {renewed})"
);
drop(pin);
let at_drop = probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the row");
assert!(
at_drop >= renewed,
"the row's expiry moved forward until the drop and never \
backwards (one TTL/3 tick may land between the read and the \
drop)"
);
tokio::time::sleep(EXPIRY_HORIZON).await;
let settled = probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the row");
assert!(
settled >= at_drop,
"the row's expiry moved forward until the drop and never \
backwards (a renewal write in flight at the drop may land \
once after the `at_drop` read)"
);
tokio::time::sleep(EXPIRY_HORIZON).await;
assert_eq!(
settled,
probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the row"),
"the dropped guard's renewal is stopped — no leaked tasks, no \
renewal past the guard's life (a leaked duty would keep \
moving the row through both settled reads)"
);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn an_expired_row_is_never_renewed_back_to_life(factory: &FleetDualTierFactory) {
let (a, _b, site) = factory.open_fleet().await.expect("open vends the fleet");
let content = patterned((factory.threshold() - 1) as usize, 151);
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the fleet put publishes");
let probe = connect_probe(site.dsn()).await;
probe.expire_pin_rows(&site.schema, &key.as_bytes()).await;
let expired = probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the abandoned row");
assert!(
expired <= wall_nanos(),
"the instrument left the row expired (a live guard holds it)"
);
tokio::time::sleep(ONE_CADENCE).await;
assert_eq!(
expired,
probe
.pin_expiry(&site.schema, &key.as_bytes())
.await
.expect("the row"),
"the expired row was never renewed back to life — the live \
guard's renewal never resurrects"
);
drop(pin);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn a_paused_holder_loses_protection_at_expiry_and_a_reput_recovers(
factory: &FleetDualTierFactory,
) {
let (a, b, _site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&a);
let content = patterned((factory.threshold() - 1) as usize, 137);
let canonical = Key::from(Algorithm::GitBlobSha256.derive(&content, content.len() as u64));
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the fleet put publishes");
drop(pin);
let err = b
.delete(&key.as_bytes())
.await
.expect_err("the un-expired row still protects");
assert_eq!(
err,
Error::GcRefuse,
"the row's once-protection holds while un-expired (a foreign \
instance's read of the pool's pin set)"
);
assert!(a.has(&key.as_bytes()).await.unwrap(), "nothing was deleted");
tokio::time::sleep(EXPIRY_HORIZON).await;
b.delete(&key.as_bytes())
.await
.expect("past expiry, the paused holder's protection is gone");
assert!(!a.has(&key.as_bytes()).await.unwrap());
let (recovered, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the recovering re-put publishes");
assert_eq!(recovered, canonical, "the same canonical address");
let (got_len, blob) = a
.get(&key.as_bytes())
.await
.expect("get succeeds")
.expect("the entry recovered");
assert_eq!(got_len, content.len() as u64);
assert_eq!(
blob.read_at(0, got_len).await.expect("the window reads"),
content.as_slice(),
"the recovered entry is byte-identical"
);
drop(pin);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn two_instance_exact_counts_instance_a_pins_instance_b_sweeps(
factory: &FleetDualTierFactory,
) {
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&b);
let pinned = patterned((factory.threshold() - 1) as usize, 179);
let (key_pinned, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(pinned.len() as u64),
Cursor::new(pinned.as_slice()),
)
.await
.expect("instance a's fleet put publishes — its pin row with it");
let dead = patterned((factory.threshold() - 3) as usize, 173);
let (key_dead, pin_dead) = b
.put_as(
Algorithm::GitBlobSha256,
Some(dead.len() as u64),
Cursor::new(dead.as_slice()),
)
.await
.expect("the twin publishes");
drop(pin_dead);
let dead_too = patterned((factory.threshold() - 5) as usize, 181);
let (key_dead_too, pin_dead_too) = b
.put_as(
Algorithm::GitBlobSha256,
Some(dead_too.len() as u64),
Cursor::new(dead_too.as_slice()),
)
.await
.expect("the other twin publishes");
drop(pin_dead_too);
let probe = connect_probe(site.dsn()).await;
probe
.expire_pin_rows(&site.schema, &key_dead.as_bytes())
.await;
probe
.expire_pin_rows(&site.schema, &key_dead_too.as_bytes())
.await;
assert_eq!(
(
probe.pool_rows(&site.schema).await,
probe.pin_rows(&site.schema).await.len()
),
(3, 3),
"three entries; three pin rows on the one shared pool (one per \
instance's put, a's renewing)"
);
let first = b.sweep().await.expect("sweeps are never errors");
assert_eq!(first.aborted, None);
assert_eq!(
(
first.candidates_staged,
first.deleted,
first.cancelled_by_arbitration
),
(3, 0, 0),
"b's mark staged exactly the three dead-by-b's-seat candidates \
(a's row is not b's enumerable live state) and deleted nothing"
);
assert_eq!(
probe.window_rows(&site.schema).await,
3,
"the staged set waits in the durable window"
);
assert_eq!(
probe.pin_rows(&site.schema).await.len(),
1,
"the maintenance step reaped exactly the twins' expired rows — \
a's renewing row survives"
);
let second = b.sweep().await.expect("sweeps are never errors");
assert_eq!(second.aborted, None);
assert_eq!(
second.candidates_staged, 1,
"the survivor re-staged (a's row is still not enumerable live \
state)"
);
assert_eq!(
(second.deleted, second.cancelled_by_arbitration),
(2, 1),
"the executing sweep deleted exactly the two unpinned twins and \
cancelled exactly the pinned entry — a's un-expired row at the \
SQL re-arbitration"
);
assert!(!b.has(&key_dead.as_bytes()).await.unwrap());
assert!(!b.has(&key_dead_too.as_bytes()).await.unwrap());
assert!(a.has(&key_pinned.as_bytes()).await.unwrap());
assert!(
b.has(&key_pinned.as_bytes()).await.unwrap(),
"the fleet's shared storage"
);
let (got_len, blob) = b
.get(&key_pinned.as_bytes())
.await
.expect("get succeeds")
.expect("the pinned entry survived");
assert_eq!(got_len, pinned.len() as u64);
assert_eq!(
blob.read_at(0, got_len).await.expect("the window reads"),
pinned.as_slice(),
"the surviving pinned entry is byte-identical"
);
drop(pin);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn two_instances_sweep_concurrently_one_leases_the_other_aborts_as_data(
factory: &FleetDualTierFactory,
) {
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&a);
SetSource::register(&b);
let dead = patterned((factory.threshold() - 1) as usize, 191);
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(dead.len() as u64),
Cursor::new(dead.as_slice()),
)
.await
.expect("the dead witness publishes");
drop(pin);
let probe = connect_probe(site.dsn()).await;
probe.expire_pin_rows(&site.schema, &key.as_bytes()).await;
let (entered_tx, entered_rx) = std::sync::mpsc::channel::<()>();
let (release_tx, release_rx) = std::sync::mpsc::channel::<()>();
a.register_protect_callback(Arc::new(LatchProtect {
entered: entered_tx,
release: parking_lot::Mutex::new(Some(release_rx)),
}));
let a = Arc::new(a);
let holder = tokio::spawn({
let a = Arc::clone(&a);
async move { a.sweep().await }
});
while entered_rx.recv_timeout(Duration::from_secs(5)).is_err() {
tokio::task::yield_now().await;
}
let contended = b.sweep().await.expect("sweeps are never errors");
assert_eq!(
contended.aborted,
Some(GcAbortCause::SweeperLock),
"the contended second instance aborts as report data"
);
assert_eq!(
(
contended.candidates_staged,
contended.deleted,
contended.cancelled_by_arbitration
),
(0, 0, 0),
"the contended sweep staged, executed, and deleted nothing"
);
assert!(
b.has(&key.as_bytes()).await.unwrap(),
"the contended sweep deleted nothing"
);
assert_eq!(
probe.window_rows(&site.schema).await,
0,
"the aborted sweep staged nothing into the window"
);
release_tx.send(()).ok();
let held = holder
.await
.expect("the holder task joined")
.expect("sweeps are never errors");
assert_eq!(held.aborted, None, "the lease holder completes");
assert_eq!(
(held.candidates_staged, held.deleted),
(1, 0),
"the holder stages exactly the dead set — it does not execute"
);
assert_eq!(
probe.window_rows(&site.schema).await,
1,
"the holder's staged set waits in the durable window"
);
assert!(
b.has(&key.as_bytes()).await.unwrap(),
"the holder staged, did not execute"
);
let executed = b.sweep().await.expect("sweeps are never errors");
assert_eq!(
executed.deleted, 1,
"the released lease's next sweep executes the staged set"
);
assert!(!b.has(&key.as_bytes()).await.unwrap());
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn a_foreign_owners_unexpired_pin_row_refuses_the_direct_delete_until_expiry(
factory: &FleetDualTierFactory,
) {
let (a, b, _site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&b);
let content = patterned((factory.threshold() - 1) as usize, 163);
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("instance a pins via its fleet put");
let err = b.delete(&key.as_bytes()).await.expect_err(
"the foreign owner's un-expired pin row refuses instance b's \
delete",
);
assert_eq!(err, Error::GcRefuse, "the refusal is the facade's, as data");
assert!(a.has(&key.as_bytes()).await.unwrap(), "nothing was deleted");
assert!(
b.has(&key.as_bytes()).await.unwrap(),
"the shared storage is untouched"
);
drop(pin);
tokio::time::sleep(EXPIRY_HORIZON).await;
b.delete(&key.as_bytes()).await.expect(
"after the row expires (renewal stopped, short TTL), the \
delete proceeds",
);
assert!(!a.has(&key.as_bytes()).await.unwrap());
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn a_put_landing_during_the_fleet_window_survives_the_executing_sweep(
factory: &FleetDualTierFactory,
) {
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&a);
let content = patterned((factory.threshold() - 1) as usize, 191);
let canonical = Key::from(Algorithm::GitBlobSha256.derive(&content, content.len() as u64));
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the twin publishes");
drop(pin);
let probe = connect_probe(site.dsn()).await;
probe.expire_pin_rows(&site.schema, &key.as_bytes()).await;
let staged = a.sweep().await.expect("sweeps are never errors");
assert_eq!(staged.aborted, None);
assert_eq!(
(staged.candidates_staged, staged.deleted),
(1, 0),
"the staging sweep stages exactly the abandoned twin and \
deletes nothing"
);
let (_, foreign_pin) = b
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("instance b's fleet put lands during the window");
let executed = a.sweep().await.expect("sweeps are never errors");
assert_eq!(executed.aborted, None);
assert_eq!(
(executed.deleted, executed.cancelled_by_arbitration),
(0, 1),
"the executing sweep's SQL re-arbitration cancelled the \
candidate — b's un-expired row"
);
assert_eq!(
executed.candidates_staged, 1,
"the survivor re-staged (b's row is not a's enumerable live state)"
);
let (got_len, blob) = a
.get(&key.as_bytes())
.await
.expect("get succeeds")
.expect("the put landing during the window survived");
assert_eq!(got_len, content.len() as u64);
assert_eq!(
blob.read_at(0, got_len).await.expect("the window reads"),
content.as_slice(),
"the survivor is byte-identical"
);
drop(foreign_pin);
tokio::time::sleep(EXPIRY_HORIZON).await;
let reaped = a.sweep().await.expect("sweeps are never errors");
assert_eq!(
(reaped.deleted, reaped.candidates_staged),
(1, 0),
"after the foreign row expired (renewal stopped, short TTL) \
and the maintenance step reaped it, the survivor executed \
exactly once"
);
assert!(
!a.has(&key.as_bytes()).await.unwrap(),
"the address is the canonical one"
);
assert_eq!(canonical.as_bytes(), key.as_bytes());
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn the_maintenance_step_reaps_exactly_the_expired_rows(factory: &FleetDualTierFactory) {
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
SetSource::register(&b);
let doomed = b"whose pin is abandoned".to_vec();
let (key_doomed, pin_one) = a
.put_as(
Algorithm::GitBlobSha256,
Some(doomed.len() as u64),
Cursor::new(doomed.as_slice()),
)
.await
.expect("the doomed entry pins");
let survivor = b"whose pin renews".to_vec();
let (key_survivor, pin_two) = a
.put_as(
Algorithm::GitBlobSha256,
Some(survivor.len() as u64),
Cursor::new(survivor.as_slice()),
)
.await
.expect("the survivor entry pins");
let probe = connect_probe(site.dsn()).await;
probe
.expire_pin_rows(&site.schema, &key_doomed.as_bytes())
.await;
let first = b.sweep().await.expect("sweeps are never errors");
assert_eq!(first.aborted, None);
assert_eq!(
(
first.candidates_staged,
first.deleted,
first.cancelled_by_arbitration
),
(2, 0, 0),
"both entries marked (the in-process live set is b's, empty); \
nothing overdue yet"
);
let rows = probe.pin_rows(&site.schema).await;
assert_eq!(
rows.len(),
1,
"the maintenance step reaped exactly the expired row — one \
row survives"
);
assert_eq!(
rows[0].0,
key_survivor.as_bytes(),
"the un-expired row survives"
);
let second = b.sweep().await.expect("sweeps are never errors");
assert_eq!(second.aborted, None);
assert_eq!(
(second.deleted, second.cancelled_by_arbitration),
(1, 1),
"the reaped row's candidate executed; the survivor's cancelled \
at the SQL re-arbitration"
);
assert_eq!(second.candidates_staged, 1, "the survivor re-staged");
assert!(a.has(&key_survivor.as_bytes()).await.unwrap());
assert!(!a.has(&key_doomed.as_bytes()).await.unwrap());
drop(pin_one);
drop(pin_two);
factory.teardown().await.expect("teardown is idempotent");
}
pub async fn an_embedder_liveness_table_protects_fleet_wide_and_is_never_owned(
factory: &FleetDualTierFactory,
) {
let (a, _b, site) = factory.open_fleet().await.expect("open vends the fleet");
let probe = connect_probe(site.dsn()).await;
probe
.create_liveness_table(&site.schema, "embedder_live")
.await;
let reference = format!("{}.embedder_live", site.schema);
let registration = a
.register_liveness_source(LivenessRegistration::FleetTable(reference.clone()))
.expect("the embedder table reference registers");
let re_registered = a
.register_liveness_source(LivenessRegistration::FleetTable(reference.clone()))
.expect("re-registration is idempotent");
assert_eq!(
registration, re_registered,
"re-registering the same reference returns the original token"
);
let malformed = a
.register_liveness_source(LivenessRegistration::FleetTable(
"bad..reference".to_owned(),
))
.expect_err("a malformed reference errors at registration");
assert!(
malformed
.to_string()
.contains("invalid liveness table reference"),
"the malformed reference surfaces the engine error: {malformed}"
);
assert!(
probe.table_exists(&site.schema, "embedder_live").await
&& !probe.table_exists(&site.schema, "no_such_table").await,
"the schema-ownership rule: the embedder's table is the \
embedder's, and no engine ever created one at registration \
time"
);
let protected = b"live in the embedder's table".to_vec();
let (key_protected, pin_one) = a
.put_as(
Algorithm::GitBlobSha256,
Some(protected.len() as u64),
Cursor::new(protected.as_slice()),
)
.await
.expect("the protected entry pins");
drop(pin_one);
let dead = b"dead beside the table's row".to_vec();
let (key_dead, pin_two) = a
.put_as(
Algorithm::GitBlobSha256,
Some(dead.len() as u64),
Cursor::new(dead.as_slice()),
)
.await
.expect("the dead entry pins");
drop(pin_two);
probe
.expire_pin_rows(&site.schema, &key_protected.as_bytes())
.await;
probe
.expire_pin_rows(&site.schema, &key_dead.as_bytes())
.await;
probe
.insert_live_row(&site.schema, "embedder_live", &key_protected.as_bytes())
.await;
let first = a.sweep().await.expect("sweeps are never errors");
assert_eq!(first.aborted, None);
assert_eq!(
(first.candidates_staged, first.deleted),
(2, 0),
"both marked — the table is never enumerated into the mark set"
);
let second = a.sweep().await.expect("sweeps are never errors");
assert_eq!(
(second.deleted, second.cancelled_by_arbitration),
(1, 1),
"the dead entry executed; the table-protected entry cancelled \
at the execution-time SQL re-arbitration"
);
assert!(a.has(&key_protected.as_bytes()).await.unwrap());
assert!(!a.has(&key_dead.as_bytes()).await.unwrap());
assert_eq!(
a.delete(&key_protected.as_bytes()).await.unwrap_err(),
Error::GcRefuse,
"the liveness table protects the direct delete path too"
);
assert!(
a.unregister_liveness_source(registration),
"the fleet token deregisters once"
);
assert!(
!a.unregister_liveness_source(re_registered),
"the idempotent re-registration consumed one token only"
);
a.delete(&key_protected.as_bytes())
.await
.expect("the correction flow: deregistered, the delete proceeds");
factory.teardown().await.expect("teardown is idempotent");
}
#[tokio::test]
async fn fleet_factory_one_schema_vends_instances_with_distinct_owner_ids() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
{
let (a, b, site) = factory.open_fleet().await.expect("open vends the fleet");
assert_eq!(
factory.tracked_schemas().len(),
1,
"one minted schema per open"
);
assert_ne!(
site.root(0),
site.root(1),
"two instances, two private roots"
);
let one = b"instance a's entry".to_vec();
let two = b"instance b's entry".to_vec();
let (_, pin_a) = a
.put_as(
Algorithm::GitBlobSha256,
Some(one.len() as u64),
Cursor::new(one.as_slice()),
)
.await
.expect("a's put publishes");
let (_, pin_b) = b
.put_as(
Algorithm::GitBlobSha256,
Some(two.len() as u64),
Cursor::new(two.as_slice()),
)
.await
.expect("b's put publishes");
drop(pin_a);
drop(pin_b);
let probe = connect_probe(site.dsn()).await;
let rows = probe.pin_rows(&site.schema).await;
assert_eq!(rows.len(), 2, "two instances, two pin rows");
assert_ne!(
rows[0].1, rows[1].1,
"two store-instance ids — the pin rows compose by owner"
);
assert_eq!(
committed_file_count(site.root(0)),
0,
"the kv-tier entries left no large-tier files"
);
}
factory.teardown().await.expect("teardown drops the schema");
factory.teardown().await.expect("teardown repeats cleanly");
let probe = connect_probe(factory.dsn()).await;
for schema in factory.tracked_schemas() {
assert!(
!probe.schema_exists(&schema).await,
"teardown dropped exactly the minted schema: {schema}"
);
}
let kv_only = FleetKvOnlyFactory::new(KV_ONLY_THRESHOLD);
{
let (a, b, site) = kv_only
.open_fleet()
.await
.expect("open vends the kv-only fleet");
assert_eq!(
kv_only.tracked_schemas().len(),
1,
"one minted schema per open"
);
assert_eq!(a.mode(), StoreMode::KvOnly);
assert_eq!(b.mode(), StoreMode::KvOnly);
let one = b"instance a's kv entry".to_vec();
let two = b"instance b's kv entry".to_vec();
let (_, pin_a) = a
.put_as(
Algorithm::GitBlobSha256,
Some(one.len() as u64),
Cursor::new(one.as_slice()),
)
.await
.expect("a's put publishes");
let (_, pin_b) = b
.put_as(
Algorithm::GitBlobSha256,
Some(two.len() as u64),
Cursor::new(two.as_slice()),
)
.await
.expect("b's put publishes");
drop(pin_a);
drop(pin_b);
let probe = connect_probe(site.dsn()).await;
let rows = probe.pin_rows(&site.schema).await;
assert_eq!(rows.len(), 2, "two instances, two pin rows");
assert_ne!(rows[0].1, rows[1].1, "distinct store-instance ids");
}
kv_only.teardown().await.expect("teardown drops the schema");
let probe = connect_probe(kv_only.dsn()).await;
for schema in kv_only.tracked_schemas() {
assert!(
!probe.schema_exists(&schema).await,
"teardown dropped the kv-only fleet's schema: {schema}"
);
}
}
#[tokio::test]
async fn fleet_factory_teardown_is_safe_against_a_panicking_row() {
if !harness_ready().await {
return;
}
let content = patterned(TUNED_THRESHOLD as usize - 7, 173);
let abandoned = Key::from(Algorithm::GitBlobSha256.derive(&content, content.len() as u64));
let factory = Arc::new(FleetDualTierFactory::new(TUNED_THRESHOLD));
let task = {
let factory = Arc::clone(&factory);
tokio::spawn(async move {
let (store, _, _) = factory.open_fleet().await.expect("open vends the fleet");
let (_, pin) = store
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the put publishes");
drop(pin);
panic!("a failing assertion panics the row (the factory contract's early exit)");
})
};
assert!(
task.await.is_err(),
"the row's panic surfaced as its failure"
);
let schemas = factory.tracked_schemas();
assert!(!schemas.is_empty(), "the panicked row minted a schema");
factory
.teardown()
.await
.expect("teardown makes sense over the abandoned fleet");
factory.teardown().await.expect("teardown is idempotent");
let probe = connect_probe(factory.dsn()).await;
for schema in &schemas {
assert!(
!probe.schema_exists(schema).await,
"teardown dropped the panicked row's schema: {schema}"
);
}
let (store, _, site) = factory
.open_fleet()
.await
.expect("the next open still vends a fleet");
assert!(
!store.has(&abandoned.as_bytes()).await.unwrap(),
"no residue from the panicked run — isolation is factory-owned"
);
assert_eq!(
probe.pool_rows(&site.schema).await,
0,
"the fresh pool is empty"
);
factory
.teardown()
.await
.expect("teardown drops the last schema");
}
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
mod consolidation {
use super::*;
use crate::pg_lo_column::{
admin_dsn, create_db, db_dsn, drop_db, fresh_db_name, fresh_schema_name, reap_lo_schema,
TrackedDb,
};
#[derive(Clone)]
pub(crate) struct ConsolidationSite {
dsn: String,
db: String,
kv_schema: String,
lo_schema: String,
}
impl ConsolidationSite {
pub(crate) fn dsn(&self) -> &str {
&self.dsn
}
pub(crate) fn kv_schema(&self) -> &str {
&self.kv_schema
}
pub(crate) fn lo_schema(&self) -> &str {
&self.lo_schema
}
pub(crate) fn db(&self) -> &str {
&self.db
}
}
pub(crate) struct FleetConsolidationFactory {
dsn: String,
seq: AtomicU64,
dbs: Mutex<Vec<TrackedDb>>,
}
impl FleetConsolidationFactory {
pub(crate) fn new() -> FleetConsolidationFactory {
FleetConsolidationFactory {
dsn: crate::postgres_column::harness_dsn()
.expect("the harness server is configured for the column"),
seq: AtomicU64::new(seq_seed()),
dbs: Mutex::new(Vec::new()),
}
}
pub(crate) async fn open_fleet(&self) -> Result<(Store, Store, ConsolidationSite), Error> {
let maintenance = admin_dsn(&self.dsn);
let db = fresh_db_name("fleet-consolidation", &self.seq);
create_db(&maintenance, &db).await?;
let dsn = db_dsn(&self.dsn, &db);
let kv_schema = fresh_schema_name("kv", &self.seq);
let lo_schema = fresh_schema_name("lo", &self.seq);
self.dbs
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(TrackedDb::new(
db.clone(),
dsn.clone(),
vec![kv_schema.clone(), lo_schema.clone()],
vec![lo_schema.clone()],
&maintenance,
));
let store_a = Store::dual_tier_postgres_pglo_fleet(
&dsn,
opts(kv_schema.clone()),
&dsn,
opts(lo_schema.clone()),
TUNED_THRESHOLD,
WINDOW_DELAY,
TEST_TTL,
)
.await?;
let store_b = Store::dual_tier_postgres_pglo_fleet(
&dsn,
opts(kv_schema.clone()),
&dsn,
opts(lo_schema.clone()),
TUNED_THRESHOLD,
WINDOW_DELAY,
TEST_TTL,
)
.await?;
Ok((
store_a,
store_b,
ConsolidationSite {
dsn,
db,
kv_schema,
lo_schema,
},
))
}
pub(crate) async fn teardown(&self) -> Result<(), Error> {
let mut dbs = self
.dbs
.lock()
.unwrap_or_else(|e| e.into_inner())
.drain(..)
.collect::<Vec<_>>();
let maintenance = admin_dsn(&self.dsn);
for tracked in dbs.iter_mut() {
if let Ok(client) = admin_connect(&tracked.dsn).await {
for schema in &tracked.lo_schemas {
reap_lo_schema(&client, schema).await;
}
for schema in &tracked.schemas {
let sql = format!("DROP SCHEMA IF EXISTS \"{schema}\" CASCADE");
let _ = client.batch_execute(&sql).await;
}
}
drop_db(&maintenance, &tracked.name).await?;
TrackedDb::disarm(tracked);
}
Ok(())
}
}
}
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
use consolidation::FleetConsolidationFactory;
impl Probe {
async fn schema_scalar(&self, schema: &str, table: &str) -> usize {
let sql = format!("SELECT COUNT(*) FROM \"{schema}\".\"{table}\"");
self.scalar(&sql).await.max(0) as usize
}
}
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
pub async fn the_pg_lo_consolidation_round_trip_is_byte_exact_over_the_shared_instance(
factory: &FleetConsolidationFactory,
) {
let (a, b, site) = factory
.open_fleet()
.await
.expect("open vends the consolidation fleet");
SetSource::register(&a);
let content = patterned(2 * 1024 + 7, 167);
let canonical = Key::from(Algorithm::GitBlobSha256.derive(&content, content.len() as u64));
let (key, pin) = a
.put_as(
Algorithm::GitBlobSha256,
Some(content.len() as u64),
Cursor::new(content.as_slice()),
)
.await
.expect("the fleet put routes to the consolidation joint tx");
assert_eq!(key, canonical, "the canonical derivation addresses the put");
let probe = connect_probe(site.dsn()).await;
assert_eq!(
probe.schema_scalar(site.kv_schema(), "blobs").await,
0,
"the over-threshold entry rides the large tier — the kv pool \
holds none"
);
assert_eq!(
probe.schema_scalar(site.lo_schema(), "lo_entries").await,
1,
"the joint tx's companion row is on the LO medium — one row"
);
assert_eq!(
probe.pin_rows(site.kv_schema()).await.len(),
1,
"the joint tx's pin row is on the kv host — both halves of the \
one tx, over the one shared instance"
);
let (got_len, blob) = b
.get(&key.as_bytes())
.await
.expect("get succeeds")
.expect("instance b reads the entry from the shared instance");
assert_eq!(got_len, content.len() as u64);
assert_eq!(
blob.read_at(0, got_len).await.expect("the windows read"),
content.as_slice(),
"instance b's read is byte-exact — the large-entry form \
round-trips over the shared instance"
);
assert_eq!(
b.stat(&key.as_bytes()).await.expect("stat succeeds"),
Some(alkblobs::EntryMeta {
len: content.len() as u64,
algorithm: key.algorithm(),
}),
"stat carries the length plus algorithm on the consolidation \
shape"
);
drop(pin);
factory.teardown().await.expect("teardown is idempotent");
}
fn harness_dsn() -> Option<String> {
crate::postgres_column::harness_dsn()
}
#[tokio::test]
async fn fleet_validity_every_fleet_valid_configuration_constructs() {
if !harness_ready().await {
return;
}
let kv_only = FleetKvOnlyFactory::new(KV_ONLY_THRESHOLD);
let dual_tier = FleetDualTierFactory::new(TUNED_THRESHOLD);
every_fleet_valid_configuration_constructs(&kv_only, &dual_tier).await;
}
#[tokio::test]
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
async fn fleet_validity_the_consolidation_fleet_constructs() {
if !harness_ready().await {
return;
}
let consolidation = FleetConsolidationFactory::new();
the_consolidation_fleet_constructs_over_one_shared_instance(&consolidation).await;
}
#[tokio::test]
async fn fleet_validity_the_constructor_entry_guards_reject_the_degenerate_tunings() {
if !harness_ready().await {
return;
}
the_constructor_entry_guards_reject_the_degenerate_tunings().await;
}
#[tokio::test]
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
async fn fleet_validity_the_consolidation_same_instance_probe_errors_closed() {
if !harness_ready().await {
return;
}
let Some(dsn) = harness_dsn() else {
return;
};
the_consolidation_same_instance_probe_errors_closed(&dsn).await;
}
#[tokio::test]
async fn fleet_put_the_joint_entry_and_pin_tx_commits_both_or_neither() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
the_joint_entry_and_pin_tx_commits_both_or_neither(&factory).await;
}
#[tokio::test]
async fn fleet_put_dedup_reput_upsert_renews_and_a_fleet_reput_composes_a_second_row() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
a_dedup_reput_upsert_renews_and_a_fleet_reput_composes_a_second_row(&factory).await;
}
#[tokio::test]
#[cfg(all(feature = "postgres", feature = "pg-lo"))]
async fn fleet_put_the_consolidation_round_trip_is_byte_exact_over_the_shared_instance() {
if !harness_ready().await {
return;
}
let factory = FleetConsolidationFactory::new();
the_pg_lo_consolidation_round_trip_is_byte_exact_over_the_shared_instance(&factory).await;
}
#[tokio::test]
async fn fleet_ttl_the_pin_guards_ttl_third_renewal_moves_the_rows_expiry() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
the_pin_guards_ttl_third_renewal_moves_the_rows_expiry(&factory).await;
}
#[tokio::test]
async fn fleet_ttl_an_expired_row_is_never_renewed_back_to_life() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
an_expired_row_is_never_renewed_back_to_life(&factory).await;
}
#[tokio::test]
async fn fleet_ttl_a_paused_holder_loses_protection_at_expiry_and_a_reput_recovers() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
a_paused_holder_loses_protection_at_expiry_and_a_reput_recovers(&factory).await;
}
#[tokio::test]
async fn fleet_sweep_two_instance_exact_counts_instance_a_pins_instance_b_sweeps() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
two_instance_exact_counts_instance_a_pins_instance_b_sweeps(&factory).await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn fleet_sweep_two_instances_sweep_concurrently_one_leases_the_other_aborts_as_data() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
two_instances_sweep_concurrently_one_leases_the_other_aborts_as_data(&factory).await;
}
#[tokio::test]
async fn fleet_sweep_a_foreign_owners_unexpired_pin_row_refuses_the_direct_delete_until_expiry() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
a_foreign_owners_unexpired_pin_row_refuses_the_direct_delete_until_expiry(&factory).await;
}
#[tokio::test]
async fn fleet_sweep_a_put_landing_during_the_fleet_window_survives_the_executing_sweep() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
a_put_landing_during_the_fleet_window_survives_the_executing_sweep(&factory).await;
}
#[tokio::test]
async fn fleet_sweep_the_maintenance_step_reaps_exactly_the_expired_rows() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
the_maintenance_step_reaps_exactly_the_expired_rows(&factory).await;
}
#[tokio::test]
async fn fleet_sweep_an_embedder_liveness_table_protects_fleet_wide_and_is_never_owned() {
if !harness_ready().await {
return;
}
let factory = FleetDualTierFactory::new(TUNED_THRESHOLD);
an_embedder_liveness_table_protects_fleet_wide_and_is_never_owned(&factory).await;
}
#[tokio::test]
async fn fleet_kv_only_mode_rejects_over_threshold_puts_at_put_time() {
if !harness_ready().await {
return;
}
let factory = FleetKvOnlyFactory::new(KV_ONLY_THRESHOLD);
crate::rows::kv_only_mode_rejects_over_threshold_puts_at_put_time(&factory).await;
}