use std::time::Duration;
use libch::inodedb::{Grant, List, Op};
use libch::stock::Exclusive;
use libch::worker::OpWriter;
use libch::writer::{Inserted, Merged};
use libch::{Error, Outcome};
use libch_testkit::ops::{Pause, active, assert_clean, insert, merge, stored, tenant_with_parts, totals};
use libch_testkit::schema::{Batch, batch, daily, events};
use libch_testkit::{Reader, TenantFixture, apply, stack};
use tokio::task::JoinHandle;
const BEFORE_INSERT_COMMIT: &str = "inodedb_insert_pause_before_commit";
const BEFORE_MERGE_COMMIT: &str = "inodedb_merge_pause_before_commit";
async fn held_insert(fixture: &TenantFixture, key: &str, pause: &Pause, batch: &Batch) -> JoinHandle<Result<Outcome<Inserted>, Error>> {
let writer = OpWriter::new(pause.command().await, &fixture.tenant, key).with_settings_xml(pause.settings());
let batch = batch.clone();
let insert = tokio::spawn(async move { insert(&writer, &batch).await });
pause.reached().await;
insert
}
async fn parts(fixture: &TenantFixture) -> (Vec<String>, Vec<String>) {
(active(&stored(fixture, &events()).await), active(&stored(fixture, &daily()).await))
}
#[tokio::test]
async fn an_exclusive_writer_that_starts_mid_insert_fences_it() {
let stack = stack().await;
let (fixture, writer) = tenant_with_parts(1).await;
let before = parts(&fixture).await;
let second = batch(1, 100);
let pause = Pause::at(BEFORE_INSERT_COMMIT).await;
let held = held_insert(&fixture, &fixture.key, &pause, &second).await;
let ddl = [
"CREATE TABLE d.kinds (kind LowCardinality(String), events AggregateFunction(count)) ENGINE = AggregatingMergeTree ORDER BY kind \
SETTINGS non_replicated_deduplication_window = 10000",
"CREATE MATERIALIZED VIEW d.kinds_mv TO d.kinds AS SELECT kind, countState() AS events FROM d.events GROUP BY kind",
];
fixture.exclusively(&ddl).await.unwrap();
pause.resume();
assert_eq!(held.await.unwrap().unwrap(), Outcome::Fenced);
assert_eq!(parts(&fixture).await, before, "the fenced insert committed nothing");
assert!(insert(&writer, &second).await.unwrap().is_committed());
assert_eq!(totals(&fixture).await, (200, 100 * 99 / 2 + second.sum, 100 * 99 / 2 + second.sum));
let reader = Reader::open(stack, &fixture).await.unwrap();
assert_eq!(reader.number("SELECT countMerge(events) FROM d.kinds").await.unwrap(), 100, "the batch reached the view made meanwhile");
reader.close().await.unwrap();
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_key_revoked_mid_insert_fences_it() {
let stack = stack().await;
let (fixture, writer) = tenant_with_parts(1).await;
let before = parts(&fixture).await;
let second = batch(1, 100);
let key = fixture.mint(Grant::Write, Duration::ZERO).await.unwrap();
let pause = Pause::at(BEFORE_INSERT_COMMIT).await;
let held = held_insert(&fixture, &key, &pause, &second).await;
stack.admin().await.unwrap().revoke_key(&key).await.unwrap();
pause.resume();
assert_eq!(held.await.unwrap().unwrap(), Outcome::Fenced);
assert_eq!(parts(&fixture).await, before, "the insert committed nothing");
assert!(insert(&writer, &second).await.unwrap().is_committed());
assert_eq!(totals(&fixture).await.0, 200);
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_key_that_expires_mid_insert_fences_it() {
let (fixture, writer) = tenant_with_parts(1).await;
let before = parts(&fixture).await;
let second = batch(1, 100);
let lifetime = Duration::from_secs(3);
let key = fixture.mint(Grant::Write, lifetime).await.unwrap();
let pause = Pause::at(BEFORE_INSERT_COMMIT).await;
let held = held_insert(&fixture, &key, &pause, &second).await;
tokio::time::sleep(lifetime + Duration::from_secs(1)).await;
pause.resume();
assert_eq!(held.await.unwrap().unwrap(), Outcome::Fenced);
assert_eq!(parts(&fixture).await, before, "the insert committed nothing");
assert!(insert(&writer, &second).await.unwrap().is_committed());
assert_eq!(totals(&fixture).await.0, 200);
assert_clean(&fixture).await;
}
#[tokio::test]
async fn a_definition_rewritten_mid_insert_ends_schema_changed() {
let (fixture, writer) = tenant_with_parts(1).await;
let before = parts(&fixture).await;
let second = batch(1, 100);
let pause = Pause::at(BEFORE_INSERT_COMMIT).await;
let held = held_insert(&fixture, &fixture.key, &pause, &second).await;
let catalog = fixture.catalog().await.unwrap();
let database = catalog.database("d").await.unwrap().unwrap();
let directory = format!("tree/{}", database.path());
let listing = catalog.list_with(&directory, &List { values: true, ..List::default() }).await.unwrap().unwrap();
let definition = listing.entries.iter().find(|entry| entry.name_lossy() == "events.sql").expect("the events table's definition");
let old_version = catalog.table(&events()).await.unwrap().unwrap().definition_version;
apply(catalog.client(), [Op::put(format!("{directory}/events.sql"), definition.value.clone().expect("the definition's bytes")).replace()]).await.unwrap();
assert!(catalog.table(&events()).await.unwrap().unwrap().definition_version > old_version);
pause.resume();
assert_eq!(held.await.unwrap().unwrap(), Outcome::SchemaChanged);
assert_eq!(parts(&fixture).await, before, "the insert committed nothing");
assert!(insert(&writer, &second).await.unwrap().is_committed());
assert_eq!(totals(&fixture).await.0, 200);
assert_clean(&fixture).await;
}
async fn held_merge(fixture: &TenantFixture, key: &str, pause: &Pause) -> JoinHandle<Result<Outcome<Merged>, Error>> {
let writer = OpWriter::new(pause.command().await, &fixture.tenant, key).with_settings_xml(pause.settings());
let merge = tokio::spawn(async move { merge(&writer, &events(), "202611", &["202611_1_1_0", "202611_2_2_0", "202611_3_3_0"]).await });
pause.reached().await;
merge
}
#[tokio::test]
async fn a_key_revoked_mid_merge_fences_it() {
let stack = stack().await;
let (fixture, _writer) = tenant_with_parts(3).await;
let before = totals(&fixture).await;
let key = fixture.mint(Grant::Write, Duration::ZERO).await.unwrap();
let pause = Pause::at(BEFORE_MERGE_COMMIT).await;
let held = held_merge(&fixture, &key, &pause).await;
stack.admin().await.unwrap().revoke_key(&key).await.unwrap();
pause.resume();
assert_eq!(held.await.unwrap().unwrap(), Outcome::Fenced);
assert_eq!(active(&stored(&fixture, &events()).await), ["202611_1_1_0", "202611_2_2_0", "202611_3_3_0"]);
assert_eq!(totals(&fixture).await, before);
assert_clean(&fixture).await;
}
#[tokio::test]
async fn an_exclusive_writer_that_starts_mid_merge_fences_it() {
let stack = stack().await;
let (fixture, _writer) = tenant_with_parts(3).await;
let before = totals(&fixture).await;
let pause = Pause::at(BEFORE_MERGE_COMMIT).await;
let held = held_merge(&fixture, &fixture.key, &pause).await;
Exclusive::run(&stack.stock_env(), &fixture.tenant, &fixture.key, &["ALTER TABLE d.events ADD COLUMN note String DEFAULT ''"]).await.unwrap();
pause.resume();
assert_eq!(held.await.unwrap().unwrap(), Outcome::Fenced);
assert_eq!(totals(&fixture).await, before);
assert_clean(&fixture).await;
}