Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
fix(core): a live delete wins over a running update, and a pre-ready failure keeps its error (#2779)
LJ π₯₯π΄48a103ce7768
diff --git a/python/tests/core/test_live_component.py b/python/tests/core/test_live_component.pyindex 584c3b4e..cb2c1f1e 100644--- a/python/tests/core/test_live_component.py+++ b/python/tests/core/test_live_component.py@@ -718,6 +718,51 @@ def test_live_component_incremental_delete_direct() -> None:assert "b" not in GlobalDictTarget.store.data+class _CreateThenDeleteAtOnceLiveComponent:+ """Issues an update and a delete of the same new child without awaiting+ the update first, for many children."""++ async def process(self) -> None:+ pass++ async def process_live(self, operator: coco.LiveComponentOperator) -> None:+ await operator.update_full()+ handles = []+ for i in range(50):+ key = f"tmp{i}"+ handles.append(+ await operator.update(+ coco.component_subpath(key), _declare_item, key, i+ )+ )+ handles.append(await operator.delete(coco.component_subpath(key)))+ for handle in handles:+ await handle.ready()+ await operator.mark_ready()+++def test_live_component_delete_right_after_update_leaves_nothing() -> None:+ """A delete issued while the same child's update is already running wins:+ the update's target state does not survive it."""+ GlobalDictTarget.store.clear()++ async def _main() -> None:+ await coco.mount(+ coco.component_subpath("live"), _CreateThenDeleteAtOnceLiveComponent+ )++ app = coco.App(+ coco.AppConfig(+ name="test_live_delete_right_after_update", environment=coco_env+ ),+ _main,+ )+ app.update_blocking(live=True)++ survivors = [k for k in GlobalDictTarget.store.data if str(k).startswith("tmp")]+ assert survivors == []++class _DeleteOnRequestLiveComponent:"""Mounts a child per source key; deletes child "b" throughoperator.delete() when `delete_b` is set."""diff --git a/rust/core/src/engine/live_component.rs b/rust/core/src/engine/live_component.rsindex 7799c125..0accedef 100644--- a/rust/core/src/engine/live_component.rs+++ b/rust/core/src/engine/live_component.rs@@ -395,13 +395,16 @@ impl<Prof: EngineProfile> LiveComponentState<Prof> {}}- /// Resolve readiness with error. Drops the guard without calling- /// resolve(); the guard's Drop reports failure to the parent.- fn resolve_ready_with_error(&self, _err: Error) {+ /// Resolve readiness with `err`: the parent's readiness fails with this+ /// error, so the caller that waits on it sees why `process_live` failed+ /// rather than a generic "build cancelled".+ fn resolve_ready_with_error(&self, err: Error) {let mut state = self.readiness.lock().unwrap();if !state.ready {state.ready = true;- state.guard.take();+ if let Some(guard) = state.guard.take() {+ guard.resolve_result(Err(SharedError::from(err)));+ }self.ready_notify.notify_waiters();}}@@ -697,35 +700,10 @@ impl<Prof: EngineProfile> LiveComponentController<Prof> {on_error: Option<OnError>,) -> Result<ComponentExecutionHandle> {// Synchronously remove the existence entry and write a tombstone,- // matching the prior implementation's contract.- if let Some((parent_ref, child_key)) = subpath.as_ref().split_parent() {- let app_store = self.app_store.clone();- let parent_path: StablePath = parent_ref.into();- let child_key = child_key.clone();- let component_path = self.component.stable_path().clone();- let relative_child = subpath.as_ref().strip_parent(component_path.as_ref())?;- let relative_child: StablePath = relative_child.into();- self.app_store- .run_txn(move |wtxn| {- let app_store = app_store.clone();- let parent_path = parent_path.clone();- let child_key = child_key.clone();- let component_path = component_path.clone();- let relative_child = relative_child.clone();- Box::pin(async move {- app_store- .remove_child_with_tombstone(- wtxn,- &parent_path,- &child_key,- &component_path,- &relative_child,- )- .await- })- })- .await?;- }+ // matching the prior implementation's contract. The drain task does+ // it again when the op runs (see `run_op`).+ remove_existence_with_tombstone(&self.app_store, self.component.stable_path(), &subpath)+ .await?;self.dispatch(subpath, Op::Delete { on_error }).await}@@ -1214,6 +1192,47 @@ async fn drain_task_body<Prof: EngineProfile>(}}+/// Remove `subpath`'s existence entry under its parent and write its+/// tombstone under `component_path` (the live component), in one txn. Both+/// writes are idempotent.+async fn remove_existence_with_tombstone(+ app_store: &AppStore,+ component_path: &StablePath,+ subpath: &StablePath,+) -> Result<()> {+ let Some((parent_ref, child_key)) = subpath.as_ref().split_parent() else {+ return Ok(());+ };+ let parent_path: StablePath = parent_ref.into();+ let child_key = child_key.clone();+ let relative_child: StablePath = subpath+ .as_ref()+ .strip_parent(component_path.as_ref())?+ .into();+ let component_path = component_path.clone();+ let store = app_store.clone();+ app_store+ .run_txn(move |wtxn| {+ let store = store.clone();+ let parent_path = parent_path.clone();+ let child_key = child_key.clone();+ let component_path = component_path.clone();+ let relative_child = relative_child.clone();+ Box::pin(async move {+ store+ .remove_child_with_tombstone(+ wtxn,+ &parent_path,+ &child_key,+ &component_path,+ &relative_child,+ )+ .await+ })+ })+ .await+}+/// Execute a single op. Returns `Result<()>` from the underlying child/// `run_in_background` / `delete` flow.#[allow(clippy::too_many_arguments)]@@ -1268,6 +1287,15 @@ async fn run_op<Prof: EngineProfile>(if app_store.write_scope() != crate::state_store::WriteScope::Persisted {return Ok(());}+ // Remove the existence entry again, now that this subpath's+ // earlier ops have finished. An update that was already running+ // when the delete was dispatched wrote it back (its build's eager+ // existence upsert), and the delete run below takes an existing+ // `Component` entry for a re-mount and deletes nothing, which+ // would leave the update's target states behind. Ops of one+ // subpath run one at a time, and none runs during `update_full`,+ // so nothing writes the entry between here and the delete run.+ remove_existence_with_tombstone(app_store, component.stable_path(), subpath).await?;let context = ComponentProcessorContext::new_borrowing_from(child.clone(),Some(lender.clone()),diff --git a/rust/sdk/cocoindex/tests/live_battle.rs b/rust/sdk/cocoindex/tests/live_battle.rsnew file mode 100644index 00000000..41e79d4d--- /dev/null+++ b/rust/sdk/cocoindex/tests/live_battle.rs@@ -0,0 +1,969 @@+//! Adversarial live-mode tests driven straight through a+//! [`LiveComponentOperator`]: update storms on one key and on many, rapid+//! create-delete-create, incremental work racing a full pass, a child that+//! fails and recovers, readiness and `mark_ready` semantics, `auto_refresh`+//! pacing, and shutdown while work is in flight.+//!+//! The test body drives the operator itself: the component's `process_live`+//! hands it out through a channel and then waits, so each test can issue+//! exactly the sequence of operations it wants to probe.+//!+//! cargo test -p cocoindex --test live_battle++use std::collections::BTreeMap;+use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};+use std::sync::{Arc, Mutex};+use std::time::{Duration, Instant};++use cocoindex::{+ App, Ctx, Error, LiveComponent, LiveComponentOperator, MountKind, Result, StableKey,+ TargetAction, TargetActionSink, TargetHandler, TargetReconcileOutput, TargetStateProvider,+ UpdateOptions, async_trait, declare_target_state, register_root_target_states_provider,+};+use tokio::sync::{Notify, mpsc};++// ---------------------------------------------------------------------------+// A target that keeps the rows it was told to write+// ---------------------------------------------------------------------------++/// The rows the sink holds, and how many actions of each kind it applied.+#[derive(Default, Debug)]+struct Table {+ rows: BTreeMap<String, String>,+ creates: usize,+ updates: usize,+ deletes: usize,+}++type SharedTable = Arc<Mutex<Table>>;++/// What the exception handler saw: kind, background, failing path, message.+type Reports = Arc<Mutex<Vec<(MountKind, bool, String, String)>>>;++fn key_str(key: &StableKey) -> String {+ match key {+ StableKey::Str(s) | StableKey::Symbol(s) => s.to_string(),+ other => format!("{other:?}"),+ }+}++#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]+struct Row {+ key: String,+ value: Option<String>,+}++#[derive(Clone)]+struct RowHandler {+ sink: TargetActionSink<Row>,+}++fn row_handler(table: SharedTable) -> RowHandler {+ RowHandler {+ sink: TargetActionSink::from_async_fn(move |actions: Vec<TargetAction<Row>>| {+ let table = table.clone();+ async move {+ let mut table = table.lock().unwrap();+ for action in actions {+ match action {+ TargetAction::Create(row) => {+ table.creates += 1;+ table.rows.insert(row.key, row.value.unwrap_or_default());+ }+ TargetAction::Update(row) => {+ table.updates += 1;+ table.rows.insert(row.key, row.value.unwrap_or_default());+ }+ TargetAction::Delete(row) => {+ table.deletes += 1;+ table.rows.remove(&row.key);+ }+ }+ }+ Ok(())+ }+ }),+ }+}++impl TargetHandler<String> for RowHandler {+ type TrackingRecord = String;+ type Action = Row;++ fn reconcile(+ &self,+ key: StableKey,+ desired: Option<String>,+ prev: Vec<String>,+ prev_may_be_missing: bool,+ ) -> Result<Option<TargetReconcileOutput<Row, String>>> {+ let key = key_str(&key);+ match desired {+ Some(value) => {+ if !prev_may_be_missing && !prev.is_empty() && prev.iter().all(|p| *p == value) {+ return Ok(None);+ }+ let row = Row {+ key,+ value: Some(value.clone()),+ };+ Ok(Some(TargetReconcileOutput {+ action: if prev.is_empty() {+ TargetAction::Create(row)+ } else {+ TargetAction::Update(row)+ },+ sink: self.sink.clone(),+ tracking_record: Some(value),+ child_invalidation: None,+ }))+ }+ None => {+ if prev.is_empty() && !prev_may_be_missing {+ return Ok(None);+ }+ Ok(Some(TargetReconcileOutput {+ action: TargetAction::Delete(Row { key, value: None }),+ sink: self.sink.clone(),+ tracking_record: None,+ child_invalidation: None,+ }))+ }+ }+ }+}++// ---------------------------------------------------------------------------+// A live component the test drives from outside+// ---------------------------------------------------------------------------++/// The source a full pass declares: one row per entry.+type Source = Arc<Mutex<BTreeMap<String, String>>>;++/// A hook the full pass awaits before declaring, so a test can hold a pass+/// open while it races incremental work against it.+#[derive(Default)]+struct Gate {+ entered: Notify,+ release: Notify,+ armed: AtomicBool,+ /// When set, the next full pass fails (after declaring nothing).+ fail: AtomicBool,+}++/// A live component whose full pass declares `source` and whose+/// `process_live` hands its operator to the test, then waits to be cancelled.+/// It marks itself ready only when `ready_on_start` is set; otherwise the test+/// decides when.+struct Driven {+ provider: TargetStateProvider<String>,+ source: Source,+ gate: Arc<Gate>,+ full_passes: Arc<AtomicUsize>,+ operator_tx: mpsc::UnboundedSender<Arc<LiveComponentOperator>>,+ ready_on_start: bool,+}++#[async_trait]+impl LiveComponent for Driven {+ async fn process(&self, ctx: Ctx) -> Result<()> {+ self.full_passes.fetch_add(1, Ordering::SeqCst);+ if self.gate.armed.load(Ordering::SeqCst) {+ self.gate.entered.notify_one();+ self.gate.release.notified().await;+ }+ if self.gate.fail.swap(false, Ordering::SeqCst) {+ return Err(Error::engine("full pass failed"));+ }+ let entries: Vec<(String, String)> = self+ .source+ .lock()+ .unwrap()+ .iter()+ .map(|(k, v)| (k.clone(), v.clone()))+ .collect();+ let provider = self.provider.clone();+ ctx.mount_each(+ entries,+ |(k, _): &(String, String)| k.clone(),+ move |child, (k, v)| {+ let provider = provider.clone();+ async move { declare_target_state(&child, provider.target_state(k, v)) }+ },+ )+ .await?;+ Ok(())+ }++ async fn process_live(&self, operator: LiveComponentOperator) -> Result<()> {+ operator.update_full().await?;+ if self.ready_on_start {+ operator.mark_ready().await;+ }+ let operator = Arc::new(operator);+ let _ = self.operator_tx.send(operator.clone());+ std::future::pending::<()>().await;+ Ok(())+ }+}++/// What a running driven app exposes to its test.+struct Harness {+ app: App,+ _dir: tempfile::TempDir,+ table: SharedTable,+ source: Source,+ gate: Arc<Gate>,+ full_passes: Arc<AtomicUsize>,+ operator: Arc<LiveComponentOperator>,+ provider: TargetStateProvider<String>,+ handle: Option<cocoindex::UpdateHandle<()>>,+ reports: Reports,+}++impl Harness {+ /// Start a live update with one driven component under `comp` and wait for+ /// its operator. Background failures go to a handler that records and+ /// swallows them.+ async fn start(name: &str, initial: &[(&str, &str)]) -> Self {+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder(name)+ .db_path(dir.path().join(".cocoindex_db"))+ .build()+ .await+ .unwrap();+ let table: SharedTable = Arc::default();+ let source: Source = Arc::new(Mutex::new(+ initial+ .iter()+ .map(|(k, v)| (k.to_string(), v.to_string()))+ .collect(),+ ));+ let gate = Arc::new(Gate::default());+ let full_passes = Arc::new(AtomicUsize::new(0));+ let reports: Reports = Arc::default();+ let (operator_tx, mut operator_rx) = mpsc::unbounded_channel();+ let (provider_tx, mut provider_rx) = mpsc::unbounded_channel();++ let handle = {+ let table = table.clone();+ let source = source.clone();+ let gate = gate.clone();+ let full_passes = full_passes.clone();+ let reports = reports.clone();+ app.start_update_with_options(+ UpdateOptions {+ live: true,+ ..UpdateOptions::default()+ },+ move |ctx| async move {+ let provider = register_root_target_states_provider(+ &ctx,+ "battle/rows",+ row_handler(table),+ )?;+ let _ = provider_tx.send(provider.clone());+ ctx.mount_live_with_handler(+ &"comp",+ Driven {+ provider,+ source,+ gate,+ full_passes,+ operator_tx,+ ready_on_start: true,+ },+ move |err, exc| {+ reports.lock().unwrap().push((+ exc.mount_kind,+ exc.is_background,+ exc.stable_path.clone(),+ err.to_string(),+ ));+ Ok(())+ },+ )+ .await+ },+ )+ .unwrap()+ };+ let operator = tokio::time::timeout(Duration::from_secs(10), operator_rx.recv())+ .await+ .expect("live component did not start")+ .unwrap();+ let provider = provider_rx.recv().await.unwrap();+ Self {+ app,+ _dir: dir,+ table,+ source,+ gate,+ full_passes,+ operator,+ provider,+ handle: Some(handle),+ reports,+ }+ }++ fn rows(&self) -> BTreeMap<String, String> {+ self.table.lock().unwrap().rows.clone()+ }++ /// `update` the child under `key` to declare `key = value`.+ async fn start_put(&self, key: &str, value: &str) -> cocoindex::live_component::ReadyHandle {+ let provider = self.provider.clone();+ let (k, v) = (key.to_string(), value.to_string());+ self.operator+ .start_update(key, move |ctx| async move {+ declare_target_state(&ctx, provider.target_state(k, v))+ })+ .await+ .unwrap()+ }++ async fn put(&self, key: &str, value: &str) {+ self.start_put(key, value).await.ready().await.unwrap();+ }++ async fn stop(mut self) {+ let _ = tokio::time::timeout(Duration::from_secs(20), self.app.drop_state()).await;+ if let Some(handle) = self.handle.take() {+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+ }+ }+}++// ---------------------------------------------------------------------------+// Storms+// ---------------------------------------------------------------------------++/// A thousand updates of one key issued without waiting: every handle+/// resolves, the target ends at the last value, and queued updates coalesce+/// (the child runs far fewer times than it was asked to).+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn same_key_storm_coalesces_and_last_value_wins() {+ let h = Harness::start("battle_same_key_storm", &[]).await;+ let runs = Arc::new(AtomicUsize::new(0));+ let mut handles = Vec::new();+ for i in 0..1000 {+ let provider = h.provider.clone();+ let runs = runs.clone();+ let value = format!("v{i}");+ handles.push(+ h.operator+ .start_update("hot", move |ctx| async move {+ runs.fetch_add(1, Ordering::SeqCst);+ declare_target_state(&ctx, provider.target_state("hot".to_string(), value))+ })+ .await+ .unwrap(),+ );+ }+ for handle in handles {+ handle.ready().await.unwrap();+ }+ assert_eq!(h.rows().get("hot").map(String::as_str), Some("v999"));+ let ran = runs.load(Ordering::SeqCst);+ assert!(+ ran < 1000,+ "1000 queued updates of one key should coalesce; the child ran {ran} times"+ );+ h.stop().await;+}++/// Two thousand distinct keys issued without waiting all land, each with one+/// create, and a later full pass that declares the same rows changes nothing.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn distinct_key_storm_lands_every_key_once() {+ let h = Harness::start("battle_distinct_storm", &[]).await;+ let n = 2000;+ let mut handles = Vec::with_capacity(n);+ for i in 0..n {+ handles.push(h.start_put(&format!("k{i:05}"), &format!("v{i}")).await);+ }+ for handle in handles {+ handle.ready().await.unwrap();+ }+ {+ let table = h.table.lock().unwrap();+ assert_eq!(table.rows.len(), n);+ assert_eq!(table.creates, n);+ assert_eq!(table.updates, 0);+ assert_eq!(table.deletes, 0);+ }+ h.stop().await;+}++/// Create, delete and create again the same key, fifty times over and+/// without waiting: the key ends present with its last value; a sequence that+/// ends in a delete leaves it absent.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn rapid_create_delete_create_ends_in_the_last_operation() {+ let h = Harness::start("battle_create_delete", &[]).await;+ let mut handles = Vec::new();+ for i in 0..50 {+ handles.push(h.start_put("flip", &format!("v{i}")).await);+ handles.push(h.operator.start_delete("flip").await.unwrap());+ }+ handles.push(h.start_put("flip", "final").await);+ for handle in handles {+ handle.ready().await.unwrap();+ }+ assert_eq!(h.rows().get("flip").map(String::as_str), Some("final"));++ let mut handles = Vec::new();+ for i in 0..50 {+ handles.push(h.operator.start_delete("flip").await.unwrap());+ handles.push(h.start_put("flip", &format!("w{i}")).await);+ }+ handles.push(h.operator.start_delete("flip").await.unwrap());+ for handle in handles {+ handle.ready().await.unwrap();+ }+ assert_eq!(h.rows().get("flip"), None);++ // A full pass after the churn neither resurrects the deleted key nor+ // drops a declared one.+ h.source.lock().unwrap().insert("steady".into(), "s".into());+ h.operator.update_full().await.unwrap();+ assert_eq!(+ h.rows(),+ BTreeMap::from([("steady".to_string(), "s".to_string())])+ );+ h.stop().await;+}++// ---------------------------------------------------------------------------+// Incremental work racing a full pass+// ---------------------------------------------------------------------------++/// An incremental update issued while a full pass runs waits for the pass,+/// then applies on top of it: the end state has both the pass's rows and the+/// update's.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn update_issued_during_a_full_pass_applies_after_it() {+ let h = Harness::start("battle_update_during_full", &[("a", "1")]).await;+ h.gate.armed.store(true, Ordering::SeqCst);+ h.source.lock().unwrap().insert("b".into(), "2".into());++ let operator = h.operator.clone();+ let full = tokio::spawn(async move { operator.update_full().await });+ tokio::time::timeout(Duration::from_secs(5), h.gate.entered.notified())+ .await+ .expect("full pass did not start");++ let handle = h.start_put("c", "3").await;+ // The pass is still held open: the update must not have landed yet.+ tokio::time::sleep(Duration::from_millis(200)).await;+ assert_eq!(h.rows().get("c"), None, "update ran during the full pass");++ h.gate.armed.store(false, Ordering::SeqCst);+ h.gate.release.notify_one();+ full.await.unwrap().unwrap();+ handle.ready().await.unwrap();++ // `c` was mounted by `update` only. The pass that ran before it does not+ // know it; it stays until the next full pass, which garbage-collects it,+ // as in the Python SDK.+ assert_eq!(+ h.rows(),+ BTreeMap::from([+ ("a".to_string(), "1".to_string()),+ ("b".to_string(), "2".to_string()),+ ("c".to_string(), "3".to_string()),+ ])+ );+ h.operator.update_full().await.unwrap();+ assert_eq!(+ h.rows(),+ BTreeMap::from([+ ("a".to_string(), "1".to_string()),+ ("b".to_string(), "2".to_string()),+ ])+ );+ h.stop().await;+}++/// Concurrent full passes serialize: none overlaps another.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn concurrent_full_passes_serialize() {+ let h = Harness::start("battle_full_serialize", &[("a", "1")]).await;+ let before = h.full_passes.load(Ordering::SeqCst);+ let passes: Vec<_> = (0..8)+ .map(|_| {+ let operator = h.operator.clone();+ tokio::spawn(async move { operator.update_full().await })+ })+ .collect();+ for pass in passes {+ pass.await.unwrap().unwrap();+ }+ assert_eq!(h.full_passes.load(Ordering::SeqCst) - before, 8);+ assert_eq!(+ h.rows(),+ BTreeMap::from([("a".to_string(), "1".to_string())])+ );+ h.stop().await;+}++// ---------------------------------------------------------------------------+// Failure and recovery+// ---------------------------------------------------------------------------++/// A child that fails in live mode keeps the target state its last good run+/// wrote, its failure reaches the handler as background `Update` work, and the+/// next good run replaces the state.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn failing_child_keeps_its_last_good_state_and_recovers() {+ let h = Harness::start("battle_fail_recover", &[]).await;+ h.put("k", "good-1").await;+ assert_eq!(h.rows().get("k").map(String::as_str), Some("good-1"));++ // The failing run declares a new value before failing: nothing of it may+ // land.+ let provider = h.provider.clone();+ h.operator+ .update("k", move |ctx| async move {+ declare_target_state(&ctx, provider.target_state("k".to_string(), "bad".into()))?;+ Err(Error::engine("source unreadable"))+ })+ .await+ .expect("the handler swallows the failure");+ assert_eq!(h.rows().get("k").map(String::as_str), Some("good-1"));+ {+ let reports = h.reports.lock().unwrap();+ assert!(+ reports.iter().any(|(kind, background, path, err)| {+ *kind == MountKind::Update+ && *background+ && path.contains('k')+ && err.contains("source unreadable")+ }),+ "{reports:?}"+ );+ }+ // A failing first run of a new key leaves nothing behind either.+ h.operator+ .update("fresh", |_ctx| async move { Err(Error::engine("boom")) })+ .await+ .unwrap();+ assert_eq!(h.rows().get("fresh"), None);++ h.put("k", "good-2").await;+ h.put("fresh", "ok").await;+ assert_eq!(h.rows().get("k").map(String::as_str), Some("good-2"));+ assert_eq!(h.rows().get("fresh").map(String::as_str), Some("ok"));++ // A full pass after the failures keeps declared rows and drops the+ // update-only ones, as usual.+ h.source.lock().unwrap().insert("k".into(), "good-2".into());+ h.operator.update_full().await.unwrap();+ assert_eq!(+ h.rows(),+ BTreeMap::from([("k".to_string(), "good-2".to_string())])+ );+ h.stop().await;+}++/// A full pass that fails keeps every row the previous pass declared, its+/// failure reaches the handler as `UpdateFull`, and the next pass that+/// succeeds reconciles normally.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn failing_full_pass_keeps_previous_rows() {+ let h = Harness::start("battle_full_fail", &[("a", "1"), ("b", "2")]).await;+ assert_eq!(h.rows().len(), 2);+ h.source.lock().unwrap().remove("b");+ h.gate.fail.store(true, Ordering::SeqCst);+ h.operator+ .update_full()+ .await+ .expect("the handler swallows it");+ assert_eq!(+ h.rows().get("b").map(String::as_str),+ Some("2"),+ "a failed full pass must not delete the previous pass's rows"+ );+ assert!(+ h.reports+ .lock()+ .unwrap()+ .iter()+ .any(|(kind, _, _, err)| *kind == MountKind::UpdateFull+ && err.contains("full pass failed"))+ );+ h.operator.update_full().await.unwrap();+ assert_eq!(+ h.rows(),+ BTreeMap::from([("a".to_string(), "1".to_string())])+ );+ h.stop().await;+}++// ---------------------------------------------------------------------------+// Readiness+// ---------------------------------------------------------------------------++/// A component that marks ready only when told to, and records when its mount+/// returned relative to that.+struct LateReady {+ release: Arc<Notify>,+ marked: Arc<AtomicBool>,+ calls_to_mark: usize,+}++#[async_trait]+impl LiveComponent for LateReady {+ async fn process(&self, _ctx: Ctx) -> Result<()> {+ Ok(())+ }++ async fn process_live(&self, operator: LiveComponentOperator) -> Result<()> {+ operator.update_full().await?;+ self.release.notified().await;+ self.marked.store(true, Ordering::SeqCst);+ for _ in 0..self.calls_to_mark {+ operator.mark_ready().await;+ }+ std::future::pending::<()>().await;+ Ok(())+ }+}++/// `mount_live` returns only once the component marks itself ready, the+/// update's readiness waits for it too, and a repeated `mark_ready` is a+/// no-op.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn mount_live_returns_only_after_mark_ready() {+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder("battle_late_ready")+ .db_path(dir.path().join(".cocoindex_db"))+ .build()+ .await+ .unwrap();+ let release = Arc::new(Notify::new());+ let marked = Arc::new(AtomicBool::new(false));+ let mounted = Arc::new(AtomicBool::new(false));+ let mut handle = {+ let release = release.clone();+ let marked = marked.clone();+ let mounted = mounted.clone();+ app.start_update_with_options(+ UpdateOptions {+ live: true,+ ..UpdateOptions::default()+ },+ move |ctx| async move {+ ctx.mount_live(+ &"late",+ LateReady {+ release,+ marked: marked.clone(),+ calls_to_mark: 3,+ },+ )+ .await?;+ assert!(+ marked.load(Ordering::SeqCst),+ "mount_live returned before mark_ready"+ );+ mounted.store(true, Ordering::SeqCst);+ Ok(())+ },+ )+ .unwrap()+ };+ tokio::time::sleep(Duration::from_millis(300)).await;+ assert!(!mounted.load(Ordering::SeqCst));+ assert!(+ handle.detailed_stats_snapshot().status != cocoindex::UpdateStatus::Ready,+ "the update reported ready before its live component"+ );+ release.notify_one();+ let deadline = Instant::now() + Duration::from_secs(10);+ while handle.detailed_stats_snapshot().status != cocoindex::UpdateStatus::Ready {+ assert!(Instant::now() < deadline, "update never became ready");+ let _ = tokio::time::timeout(Duration::from_millis(100), handle.changed()).await;+ }+ assert!(mounted.load(Ordering::SeqCst));+ let _ = app.drop_state().await;+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+}++/// A `process_live` that returns without calling `mark_ready` is ready when+/// it returns (as in Python, where returning marks the component ready).+struct ReturnsWithoutReady;++#[async_trait]+impl LiveComponent for ReturnsWithoutReady {+ async fn process(&self, _ctx: Ctx) -> Result<()> {+ Ok(())+ }++ async fn process_live(&self, operator: LiveComponentOperator) -> Result<()> {+ operator.update_full().await+ }+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn process_live_returning_without_mark_ready_is_ready() {+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder("battle_return_ready")+ .db_path(dir.path().join(".cocoindex_db"))+ .build()+ .await+ .unwrap();+ for live in [false, true] {+ tokio::time::timeout(+ Duration::from_secs(10),+ app.update_with_options(+ UpdateOptions {+ live,+ ..UpdateOptions::default()+ },+ |ctx| async move { ctx.mount_live(&"r", ReturnsWithoutReady).await },+ ),+ )+ .await+ .expect("mount_live hung without mark_ready")+ .unwrap();+ }+}++/// A `process_live` that fails before it is ready: the failure reaches the+/// caller of `mount_live` (the parent cannot have its live child half-up), in+/// catch-up and in live mode.+struct FailsBeforeReady;++#[async_trait]+impl LiveComponent for FailsBeforeReady {+ async fn process(&self, _ctx: Ctx) -> Result<()> {+ Ok(())+ }++ async fn process_live(&self, _operator: LiveComponentOperator) -> Result<()> {+ Err(Error::engine("cannot connect to the source"))+ }+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn process_live_failing_before_ready_fails_the_mount() {+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder("battle_fail_before_ready")+ .db_path(dir.path().join(".cocoindex_db"))+ .build()+ .await+ .unwrap();+ for live in [false, true] {+ let result = tokio::time::timeout(+ Duration::from_secs(10),+ app.update_with_options(+ UpdateOptions {+ live,+ ..UpdateOptions::default()+ },+ |ctx| async move { ctx.mount_live(&"f", FailsBeforeReady).await },+ ),+ )+ .await+ .expect("update hung");+ let err = result.expect_err("a process_live that failed before ready must fail the update");+ assert!(+ format!("{err:?}").contains("cannot connect to the source"),+ "live={live}: {err:?}"+ );+ }+}++// ---------------------------------------------------------------------------+// auto_refresh+// ---------------------------------------------------------------------------++/// Cycles slower than the interval never overlap, and the loop keeps going.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn auto_refresh_cycles_never_overlap() {+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder("battle_auto_refresh_overlap")+ .db_path(dir.path().join(".cocoindex_db"))+ .build()+ .await+ .unwrap();+ let running = Arc::new(AtomicUsize::new(0));+ let max_running = Arc::new(AtomicUsize::new(0));+ let cycles = Arc::new(AtomicUsize::new(0));+ let handle = {+ let (running, max_running, cycles) = (running.clone(), max_running.clone(), cycles.clone());+ app.start_update_with_options(+ UpdateOptions {+ live: true,+ ..UpdateOptions::default()+ },+ move |ctx| async move {+ ctx.auto_refresh("poll", Duration::from_millis(10), move |_ctx| {+ let (running, max_running, cycles) =+ (running.clone(), max_running.clone(), cycles.clone());+ async move {+ let now = running.fetch_add(1, Ordering::SeqCst) + 1;+ max_running.fetch_max(now, Ordering::SeqCst);+ tokio::time::sleep(Duration::from_millis(50)).await;+ running.fetch_sub(1, Ordering::SeqCst);+ cycles.fetch_add(1, Ordering::SeqCst);+ Ok(())+ }+ })+ .await+ },+ )+ .unwrap()+ };+ let deadline = Instant::now() + Duration::from_secs(10);+ while cycles.load(Ordering::SeqCst) < 6 {+ assert!(Instant::now() < deadline, "auto_refresh stalled");+ tokio::time::sleep(Duration::from_millis(20)).await;+ }+ assert_eq!(max_running.load(Ordering::SeqCst), 1, "cycles overlapped");+ let _ = app.drop_state().await;+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+ // Once the app is dropped, no further cycle starts.+ let after_drop = cycles.load(Ordering::SeqCst);+ tokio::time::sleep(Duration::from_millis(300)).await;+ assert!(cycles.load(Ordering::SeqCst) <= after_drop + 1);+}++/// The interval is the pause between the end of one cycle and the start of+/// the next: fast cycles at a 200ms interval run about five times a second,+/// not hundreds.+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn auto_refresh_respects_its_interval() {+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder("battle_auto_refresh_interval")+ .db_path(dir.path().join(".cocoindex_db"))+ .build()+ .await+ .unwrap();+ let cycles = Arc::new(AtomicUsize::new(0));+ let handle = {+ let cycles = cycles.clone();+ app.start_update_with_options(+ UpdateOptions {+ live: true,+ ..UpdateOptions::default()+ },+ move |ctx| async move {+ ctx.auto_refresh("tick", Duration::from_millis(200), move |_ctx| {+ let cycles = cycles.clone();+ async move {+ cycles.fetch_add(1, Ordering::SeqCst);+ Ok(())+ }+ })+ .await+ },+ )+ .unwrap()+ };+ tokio::time::sleep(Duration::from_millis(1100)).await;+ let n = cycles.load(Ordering::SeqCst);+ assert!(+ (3..=8).contains(&n),+ "expected ~6 cycles in 1.1s at a 200ms interval, got {n}"+ );+ let _ = app.drop_state().await;+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+}++// ---------------------------------------------------------------------------+// Shutdown while work is in flight+// ---------------------------------------------------------------------------++/// Dropping the app while slow updates are queued and running finishes+/// promptly, resolves every handle, and leaves no row behind: no update lands+/// after the drop has reverted the target.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn drop_while_updates_are_in_flight_resolves_every_handle() {+ let h = Harness::start("battle_drop_in_flight", &[("seed", "s")]).await;+ let started = Arc::new(AtomicUsize::new(0));+ let mut handles = Vec::new();+ for i in 0..200 {+ let provider = h.provider.clone();+ let started = started.clone();+ let key = format!("slow{i:03}");+ handles.push(+ h.operator+ .start_update(key.clone(), move |ctx| async move {+ started.fetch_add(1, Ordering::SeqCst);+ tokio::time::sleep(Duration::from_millis(300)).await;+ declare_target_state(&ctx, provider.target_state(key, "v".into()))+ })+ .await+ .unwrap(),+ );+ }+ let deadline = Instant::now() + Duration::from_secs(10);+ while started.load(Ordering::SeqCst) == 0 {+ assert!(Instant::now() < deadline);+ tokio::time::sleep(Duration::from_millis(5)).await;+ }+ let table = h.table.clone();+ let begun = Instant::now();+ let app = h.app.clone();+ tokio::time::timeout(Duration::from_secs(20), app.drop_state())+ .await+ .expect("drop_state hung with updates in flight")+ .unwrap();+ assert!(+ begun.elapsed() < Duration::from_secs(10),+ "drop took {:?}",+ begun.elapsed()+ );+ for handle in handles {+ // Each handle resolves: done, or cancelled by the drop.+ tokio::time::timeout(Duration::from_secs(5), handle.ready())+ .await+ .expect("a handle never resolved after the drop")+ .ok();+ }+ // Nothing reaches the target once the drop has returned. (A provider+ // registered with `register_root_target_states_provider` is not reverted+ // by `drop_state`, so the rows written before the drop stay.)+ let writes_at_drop = {+ let t = table.lock().unwrap();+ t.creates + t.updates+ };+ tokio::time::sleep(Duration::from_millis(500)).await;+ let writes_after = {+ let t = table.lock().unwrap();+ t.creates + t.updates+ };+ assert_eq!(+ writes_after, writes_at_drop,+ "a cancelled update wrote after drop_state returned"+ );+ h.stop().await;+}++/// A new key created and deleted at once, without waiting for the create:+/// the delete wins and no row is left behind.+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+async fn new_key_created_then_deleted_at_once_leaves_nothing() {+ let h = Harness::start("battle_new_key_delete", &[]).await;+ let mut handles = Vec::new();+ for i in 0..200 {+ let key = format!("tmp{i:03}");+ handles.push(h.start_put(&key, "x").await);+ handles.push(h.operator.start_delete(key).await.unwrap());+ }+ for handle in handles {+ handle.ready().await.unwrap();+ }+ tokio::time::sleep(Duration::from_millis(200)).await;+ let rows = h.rows();+ assert!(+ rows.is_empty(),+ "{} rows survived their delete: {:?}",+ rows.len(),+ rows.keys().take(5).collect::<Vec<_>>()+ );+ h.stop().await;+}diff --git a/rust/sdk/cocoindex/tests/live_soak.rs b/rust/sdk/cocoindex/tests/live_soak.rsnew file mode 100644index 00000000..e7ea145d--- /dev/null+++ b/rust/sdk/cocoindex/tests/live_soak.rs@@ -0,0 +1,493 @@+//! Long live-mode runs, behind `#[ignore]` (minutes, and they measure the+//! process's memory, so run them one at a time in release mode):+//!+//! cargo test -p cocoindex --features fs_live --release --test live_soak \+//! -- --ignored --test-threads=1 --nocapture+//!+//! * `soak_100k_events_keep_memory_and_queue_bounded`: 100k change events+//! through a live feed β rewrites of a hot key set, and distinct keys+//! created then deleted (the churn of component paths a log directory or a+//! topic of unique keys produces) β with resident memory and the number of+//! unresolved handles sampled as it runs.+//! * `soak_fs_100k_writes`: 100k file writes over 1000 files through the live+//! LocalFS watcher (more than the inotify queue holds, so overflow rescans+//! happen too), then the output tree checked against the source tree.+//! * `fs_event_to_target_latency`: the time from a file write to its output+//! file, over sequential single-file edits.+#![cfg(feature = "fs_live")]++use std::collections::BTreeMap;+use std::path::Path;+use std::sync::atomic::{AtomicUsize, Ordering};+use std::sync::{Arc, Mutex};+use std::time::{Duration, Instant};++use cocoindex::live_component::{LiveMapFeed, LiveMapSubscriber, LiveMapView};+use cocoindex::resources::fs::{self, FileEntry};+use cocoindex::{+ App, Result, StableKey, TargetAction, TargetActionSink, TargetHandler, TargetReconcileOutput,+ UpdateOptions, async_trait, declare_target_state, register_root_target_states_provider,+};+use futures::StreamExt;+use tokio::sync::mpsc;++/// Anonymous resident memory of this process (the heap: what a leak grows),+/// in MiB. File-backed pages β the LMDB map, which grows with the state db β+/// are left out.+fn rss_mib() -> f64 {+ let status = std::fs::read_to_string("/proc/self/status").unwrap_or_default();+ let kib: f64 = status+ .lines()+ .find_map(|l| l.strip_prefix("RssAnon:"))+ .and_then(|v| v.split_whitespace().next()?.parse().ok())+ .unwrap_or(0.0);+ kib / 1024.0+}++// ---------------------------------------------------------------------------+// 100k events through a channel-driven feed+// ---------------------------------------------------------------------------++enum Event {+ Put(String, String),+ Delete(String),+}++/// A watch-only feed fed from a channel. It queues each event without waiting+/// and keeps the unresolved handles, recording the most it ever held.+struct ChannelFeed {+ rx: tokio::sync::Mutex<mpsc::UnboundedReceiver<Event>>,+ in_flight_max: Arc<AtomicUsize>,+ in_flight_now: Arc<AtomicUsize>,+}++#[async_trait]+impl LiveMapFeed<String, String> for ChannelFeed {+ async fn watch(&self, subscriber: LiveMapSubscriber<String, String>) -> Result<()> {+ let mut rx = self.rx.lock().await;+ let mut in_flight = futures::stream::FuturesUnordered::new();+ loop {+ tokio::select! {+ event = rx.recv() => {+ let Some(event) = event else { break };+ let handle = match event {+ Event::Put(k, v) => subscriber.start_update(k, v).await?,+ Event::Delete(k) => subscriber.start_delete(k).await?,+ };+ in_flight.push(handle.ready());+ self.in_flight_max.fetch_max(in_flight.len(), Ordering::Relaxed);+ }+ Some(result) = in_flight.next(), if !in_flight.is_empty() => result?,+ }+ self.in_flight_now.store(in_flight.len(), Ordering::Relaxed);+ }+ while let Some(result) = in_flight.next().await {+ result?;+ }+ Ok(())+ }+}++#[async_trait]+impl LiveMapView<String, String> for ChannelFeed {+ async fn scan(&self) -> Result<Vec<(String, String)>> {+ Ok(Vec::new())+ }+}++#[derive(Clone, serde::Serialize, serde::Deserialize)]+struct Row(String, Option<String>);++/// Keeps the rows in a map and counts applied actions.+#[derive(Clone)]+struct MapHandler {+ rows: Arc<Mutex<BTreeMap<String, String>>>,+ applied: Arc<AtomicUsize>,+ sink: TargetActionSink<Row>,+}++impl MapHandler {+ fn new() -> Self {+ let rows: Arc<Mutex<BTreeMap<String, String>>> = Arc::default();+ let applied: Arc<AtomicUsize> = Arc::default();+ let sink = {+ let (rows, applied) = (rows.clone(), applied.clone());+ TargetActionSink::from_async_fn(move |actions: Vec<TargetAction<Row>>| {+ let (rows, applied) = (rows.clone(), applied.clone());+ async move {+ let mut rows = rows.lock().unwrap();+ for action in actions {+ applied.fetch_add(1, Ordering::Relaxed);+ match action {+ TargetAction::Create(Row(k, v)) | TargetAction::Update(Row(k, v)) => {+ rows.insert(k, v.unwrap_or_default());+ }+ TargetAction::Delete(Row(k, _)) => {+ rows.remove(&k);+ }+ }+ }+ Ok(())+ }+ })+ };+ Self {+ rows,+ applied,+ sink,+ }+ }+}++impl TargetHandler<String> for MapHandler {+ type TrackingRecord = String;+ type Action = Row;++ fn reconcile(+ &self,+ key: StableKey,+ desired: Option<String>,+ prev: Vec<String>,+ prev_may_be_missing: bool,+ ) -> Result<Option<TargetReconcileOutput<Row, String>>> {+ let key = match key {+ StableKey::Str(s) => s.to_string(),+ other => format!("{other:?}"),+ };+ if desired.is_none() && prev.is_empty() && !prev_may_be_missing {+ return Ok(None);+ }+ let sink = self.sink.clone();+ Ok(Some(TargetReconcileOutput {+ action: match &desired {+ Some(v) if prev.is_empty() => TargetAction::Create(Row(key, Some(v.clone()))),+ Some(v) => TargetAction::Update(Row(key, Some(v.clone()))),+ None => TargetAction::Delete(Row(key, None)),+ },+ sink,+ tracking_record: desired,+ child_invalidation: None,+ }))+ }+}++#[tokio::test(flavor = "multi_thread", worker_threads = 8)]+#[ignore = "soak test: run with --release --ignored --test-threads=1"]+async fn soak_100k_events_keep_memory_and_queue_bounded() {+ let events: usize = std::env::var("SOAK_EVENTS")+ .ok()+ .and_then(|v| v.parse().ok())+ .unwrap_or(100_000);+ const HOT_KEYS: usize = 1000;+ let dir = tempfile::tempdir().unwrap();+ let app = App::builder("LiveSoakEvents")+ .db_path(dir.path().join("db"))+ .build()+ .await+ .unwrap();+ let (tx, rx) = mpsc::unbounded_channel();+ let in_flight_max = Arc::new(AtomicUsize::new(0));+ let in_flight_now = Arc::new(AtomicUsize::new(0));+ let handler = MapHandler::new();+ let runs = Arc::new(AtomicUsize::new(0));+ let handle = {+ let feed = ChannelFeed {+ rx: tokio::sync::Mutex::new(rx),+ in_flight_max: in_flight_max.clone(),+ in_flight_now: in_flight_now.clone(),+ };+ let (handler, runs) = (handler.clone(), runs.clone());+ app.start_update_with_options(+ UpdateOptions {+ live: true,+ ..UpdateOptions::default()+ },+ move |ctx| async move {+ let provider = register_root_target_states_provider(&ctx, "soak/rows", handler)?;+ ctx.mount_each_live(&"events", feed, move |ctx, value: String| {+ let provider = provider.clone();+ let runs = runs.clone();+ async move {+ runs.fetch_add(1, Ordering::Relaxed);+ let key = value.split_once('=').unwrap().0.to_string();+ declare_target_state(&ctx, provider.target_state(key, value))+ }+ })+ .await+ },+ )+ .unwrap()+ };+ tokio::time::sleep(Duration::from_millis(500)).await;++ let started = Instant::now();+ let mut samples = Vec::new();+ let mut expected: BTreeMap<String, String> = BTreeMap::new();+ let mut churn = 0usize;+ // `SOAK_MODE=hot` sends only hot-key rewrites, `churn` only create/delete+ // pairs; the default mixes both.+ let mode = std::env::var("SOAK_MODE").unwrap_or_default();+ for i in 0..events {+ let hot = match mode.as_str() {+ "hot" => true,+ "churn" => false,+ _ => i % 2 == 0,+ };+ let create = if mode == "churn" {+ i % 2 == 0+ } else {+ i % 4 == 1+ };+ if hot {+ let key = format!("hot{:04}", (i / 2) % HOT_KEYS);+ let value = format!("{key}={i}");+ expected.insert(key.clone(), value.clone());+ tx.send(Event::Put(key, value)).unwrap();+ } else if create {+ let key = format!("churn{churn:06}");+ tx.send(Event::Put(key.clone(), format!("{key}=x")))+ .unwrap();+ } else {+ let key = format!("churn{churn:06}");+ tx.send(Event::Delete(key)).unwrap();+ churn += 1;+ }+ if i % (events / 10).max(1) == (events / 10).max(1) - 1 {+ // Pace the producer to the consumer so the samples measure the+ // engine rather than the channel backlog.+ while in_flight_now.load(Ordering::Relaxed) > 5_000 {+ tokio::time::sleep(Duration::from_millis(5)).await;+ }+ samples.push((i + 1, rss_mib(), in_flight_now.load(Ordering::Relaxed)));+ }+ }+ let deadline = Instant::now() + Duration::from_secs(600);+ let mut last_report = Instant::now();+ loop {+ let rows = handler.rows.lock().unwrap().clone();+ if rows == expected {+ break;+ }+ if last_report.elapsed() > Duration::from_secs(10) || Instant::now() > deadline {+ last_report = Instant::now();+ let wrong: Vec<_> = expected+ .iter()+ .filter(|(k, v)| rows.get(*k) != Some(*v))+ .take(5)+ .map(|(k, v)| (k.clone(), v.clone(), rows.get(k).cloned()))+ .collect();+ let extra = rows.keys().filter(|k| !expected.contains_key(*k)).count();+ eprintln!(+ "waiting: {} rows ({} expected, {extra} extra), unresolved handles {}, runs {}; first wrong: {wrong:?}",+ rows.len(),+ expected.len(),+ in_flight_now.load(Ordering::Relaxed),+ runs.load(Ordering::Relaxed),+ );+ }+ assert!(Instant::now() < deadline, "the soak never converged");+ tokio::time::sleep(Duration::from_millis(100)).await;+ }+ let took = started.elapsed();+ tokio::time::sleep(Duration::from_secs(2)).await;+ let settled_rss = rss_mib();+ for (events, rss, pending) in &samples {+ eprintln!("after {events:>6} events: rss {rss:7.1} MiB, unresolved handles {pending}");+ }+ eprintln!(+ "{events} events converged in {took:?} ({:.0} events/s); {} component runs, {} target actions; \+ most unresolved handles {}; rss settled at {settled_rss:.1} MiB",+ events as f64 / took.as_secs_f64(),+ runs.load(Ordering::Relaxed),+ handler.applied.load(Ordering::Relaxed),+ in_flight_max.load(Ordering::Relaxed),+ );+ // Memory: the second half of the run must not grow like the first. The+ // churn creates and deletes 25k distinct component paths; a per-path leak+ // would show here.+ let at_half = samples[samples.len() / 2 - 1].1;+ let at_end = samples.last().unwrap().1;+ assert!(+ at_end - at_half < 16.0,+ "rss grew {:.1} MiB over the second half of the events",+ at_end - at_half+ );+ assert_eq!(+ in_flight_now.load(Ordering::Relaxed),+ 0,+ "handles left unresolved"+ );++ let _ = tokio::time::timeout(Duration::from_secs(60), app.drop_state()).await;+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+}++// ---------------------------------------------------------------------------+// LocalFS+// ---------------------------------------------------------------------------++fn tree(dir: &Path) -> BTreeMap<String, String> {+ std::fs::read_dir(dir)+ .map(|entries| {+ entries+ .filter_map(|e| {+ let e = e.ok()?;+ Some((+ e.file_name().to_string_lossy().into_owned(),+ std::fs::read_to_string(e.path()).ok()?,+ ))+ })+ .collect()+ })+ .unwrap_or_default()+}++async fn start_fs(+ app: &App,+ src: &Path,+ out: &Path,+ runs: Arc<AtomicUsize>,+) -> cocoindex::UpdateHandle<()> {+ let (src, out) = (src.to_path_buf(), out.to_path_buf());+ app.start_update_with_options(+ UpdateOptions {+ live: true,+ ..UpdateOptions::default()+ },+ move |ctx| async move {+ let target = fs::mount_dir_target(&ctx, out)?;+ let feed = fs::walk_dir(src).live().rescan_interval(None);+ ctx.mount_each_live(&"files", feed, move |ctx, file: FileEntry| {+ let (target, runs) = (target.clone(), runs.clone());+ async move {+ runs.fetch_add(1, Ordering::Relaxed);+ let content = file.content()?;+ target.declare_file(&ctx, &file.key(), &content)+ }+ })+ .await+ },+ )+ .unwrap()+}++#[tokio::test(flavor = "multi_thread", worker_threads = 8)]+#[ignore = "soak test: run with --release --ignored --test-threads=1"]+async fn soak_fs_100k_writes() {+ const FILES: usize = 1000;+ let rounds: usize = std::env::var("SOAK_ROUNDS")+ .ok()+ .and_then(|v| v.parse().ok())+ .unwrap_or(100);+ let dir = tempfile::tempdir().unwrap();+ let root = dir.path().canonicalize().unwrap();+ let (src, out) = (root.join("src"), root.join("out"));+ std::fs::create_dir_all(&src).unwrap();+ let app = App::builder("LiveSoakFs")+ .db_path(root.join("db"))+ .build()+ .await+ .unwrap();+ let runs = Arc::new(AtomicUsize::new(0));+ let handle = start_fs(&app, &src, &out, runs.clone()).await;+ tokio::time::sleep(Duration::from_millis(500)).await;++ let started = Instant::now();+ let mut samples = Vec::new();+ for round in 0..rounds {+ for i in 0..FILES {+ std::fs::write(src.join(format!("f{i:04}.txt")), format!("{i}-{round}")).unwrap();+ }+ if round % (rounds / 10).max(1) == (rounds / 10).max(1) - 1 {+ samples.push(((round + 1) * FILES, rss_mib(), runs.load(Ordering::Relaxed)));+ // Let the watcher keep up a little, as a real writer would.+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ }+ let written = started.elapsed();+ let expected = tree(&src);+ let deadline = Instant::now() + Duration::from_secs(600);+ while tree(&out) != expected {+ assert!(Instant::now() < deadline, "the fs soak never converged");+ tokio::time::sleep(Duration::from_millis(200)).await;+ }+ let took = started.elapsed();+ for (writes, rss, runs) in &samples {+ eprintln!("after {writes:>6} writes: rss {rss:7.1} MiB, component runs so far {runs}");+ }+ eprintln!(+ "{} writes over {FILES} files written in {written:?}, converged in {took:?}; {} component runs; rss {:.1} MiB",+ FILES * rounds,+ runs.load(Ordering::Relaxed),+ rss_mib()+ );+ let at_half = samples[samples.len() / 2 - 1].1;+ let at_end = samples.last().unwrap().1;+ assert!(+ at_end - at_half < 64.0,+ "rss grew {:.1} MiB over the second half",+ at_end - at_half+ );+ let _ = tokio::time::timeout(Duration::from_secs(60), app.drop_state()).await;+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+}++#[tokio::test(flavor = "multi_thread", worker_threads = 4)]+#[ignore = "measurement: run with --release --ignored --nocapture"]+async fn fs_event_to_target_latency() {+ let dir = tempfile::tempdir().unwrap();+ let root = dir.path().canonicalize().unwrap();+ let (src, out) = (root.join("src"), root.join("out"));+ std::fs::create_dir_all(&src).unwrap();+ for i in 0..100 {+ std::fs::write(src.join(format!("bg{i:03}.txt")), b"bg").unwrap();+ }+ let app = App::builder("LiveLatency")+ .db_path(root.join("db"))+ .build()+ .await+ .unwrap();+ let handle = start_fs(&app, &src, &out, Arc::default()).await;+ let deadline = Instant::now() + Duration::from_secs(60);+ while tree(&out).len() < 100 {+ assert!(Instant::now() < deadline);+ tokio::time::sleep(Duration::from_millis(20)).await;+ }+ tokio::time::sleep(Duration::from_millis(500)).await;++ let mut latencies = Vec::new();+ for i in 0..50 {+ let name = if i % 2 == 0 {+ "edit.txt".to_string()+ } else {+ format!("new{i:02}.txt")+ };+ let content = format!("v{i}");+ let written = Instant::now();+ std::fs::write(src.join(&name), &content).unwrap();+ loop {+ if std::fs::read_to_string(out.join(&name)).ok().as_deref() == Some(content.as_str()) {+ break;+ }+ assert!(+ written.elapsed() < Duration::from_secs(10),+ "edit {i} never landed"+ );+ tokio::time::sleep(Duration::from_millis(1)).await;+ }+ latencies.push(written.elapsed());+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ latencies.sort();+ let pick = |q: f64| latencies[((latencies.len() - 1) as f64 * q).round() as usize];+ eprintln!(+ "event-to-target latency over {} edits: min {:?}, p50 {:?}, p95 {:?}, max {:?}",+ latencies.len(),+ latencies[0],+ pick(0.5),+ pick(0.95),+ latencies.last().unwrap()+ );+ let _ = tokio::time::timeout(Duration::from_secs(20), app.drop_state()).await;+ let _ = tokio::time::timeout(Duration::from_secs(10), handle.result()).await;+}