Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

A certified read pins one state without stopping the store (#208)

Fabio Rovai7284539ccd46
diff --git a/Cargo.lock b/Cargo.lock
index a61e8014..6ddfb3bd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2933,8 +2933,7 @@ dependencies = [
[[package]]
name = "oxigraph"
version = "0.5.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6d945cbe78640ab719a73697b9bd250054ea82c60470754efea79c21d5fc1cd1"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"dashmap",
"getrandom 0.3.4",
@@ -2944,6 +2943,7 @@ dependencies = [
"oxrdfio",
"oxrocksdb-sys",
"oxsdatatypes",
+ "rand 0.8.6",
"rand 0.9.4",
"rustc-hash",
"siphasher",
@@ -2971,8 +2971,7 @@ checksum = "54b4ed3a7192fa19f5f48f99871f2755047fabefd7f222f12a1df1773796a102"
[[package]]
name = "oxjsonld"
version = "0.2.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f6e1380a504a8571763f13b8bcad629ff90d0300d47d8a519f3c6c599625840e"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"json-event-parser",
"oxiri",
@@ -2984,14 +2983,13 @@ dependencies = [
[[package]]
name = "oxrdf"
version = "0.3.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0afd5c28e4a399c57ee2bc3accd40c7b671fdc7b6537499f14e95b265af7d7e0"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"hex",
"oxilangtag",
"oxiri",
"oxsdatatypes",
- "rand 0.9.4",
+ "rand 0.8.6",
"sha2",
"thiserror",
]
@@ -2999,8 +2997,7 @@ dependencies = [
[[package]]
name = "oxrdfio"
version = "0.2.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "696223589ecbcab06b1a5df9b527dc25b0c656160cca38752fdb3878a5d3dd03"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"oxjsonld",
"oxrdf",
@@ -3012,8 +3009,7 @@ dependencies = [
[[package]]
name = "oxrdfxml"
version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cd5516ae083d09bc57ec65ed5ee97701481725de6ffaa83d968ab42a96157ba1"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"oxilangtag",
"oxiri",
@@ -3025,8 +3021,7 @@ dependencies = [
[[package]]
name = "oxrocksdb-sys"
version = "0.5.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6977c1d1827fcbf14480e768e4dede0b39c2961f6028497d012f9c04bcda91c6"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"bindgen",
"cc",
@@ -3035,8 +3030,7 @@ dependencies = [
[[package]]
name = "oxsdatatypes"
version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "06fa874d87eae638daae9b4e3198864fe2cce68589f227c0b2cf5b62b1530516"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"thiserror",
]
@@ -3044,8 +3038,7 @@ dependencies = [
[[package]]
name = "oxttl"
version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f03fd471bd54c23d76631c0a2677aa4bb308d905f6e491ee35dcb0732b7c5c6c"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"memchr",
"oxilangtag",
@@ -4301,8 +4294,7 @@ dependencies = [
[[package]]
name = "sparesults"
version = "0.3.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7e13362decdcbeb3fefadff1631238c835c112027221e83d217cc5779f5a81c2"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"json-event-parser",
"memchr",
@@ -4314,8 +4306,7 @@ dependencies = [
[[package]]
name = "spareval"
version = "0.2.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c8eab80b96dc836e4cf8fda72daa67ccc0244dc5303be5d4566e8fd04adbdde5"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"hex",
"json-event-parser",
@@ -4323,7 +4314,7 @@ dependencies = [
"oxiri",
"oxrdf",
"oxsdatatypes",
- "rand 0.9.4",
+ "rand 0.8.6",
"regex",
"rustc-hash",
"sha1",
@@ -4337,25 +4328,23 @@ dependencies = [
[[package]]
name = "spargebra"
version = "0.4.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "46715eb957d1fe960cbbc0b713da8f78e2cb19df315b48fb09c9467a1c84f656"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"oxilangtag",
"oxiri",
"oxrdf",
"peg",
- "rand 0.9.4",
+ "rand 0.8.6",
"thiserror",
]
[[package]]
name = "sparopt"
version = "0.3.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "aed7a854b8ea67618f8ce69aabb0e904d7704226060e9d5a2930eb3136c3fa3b"
+source = "git+https://github.com/fabio-rovai/oxigraph.git?rev=abf36ef22b61f9c4d810b94b647d500c05ee791c#abf36ef22b61f9c4d810b94b647d500c05ee791c"
dependencies = [
"oxrdf",
- "rand 0.9.4",
+ "rand 0.8.6",
"spargebra",
]
diff --git a/Cargo.toml b/Cargo.toml
index 9565e870..fe24f963 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -118,3 +118,19 @@ toml = "0.8"
# derivation. DEV-ONLY on purpose. The shipped binary's dependency surface does
# not grow by a single crate.
proptest = "1"
+
+# A patched Oxigraph, pinned by revision.
+#
+# `Store::snapshot` is not in Oxigraph 0.5.9. It is a read view pinned to one
+# state that takes no lock, and decision 0010 explains at length why neither
+# thing the unpatched crate offers will do: a transaction pins the state but
+# stops every writer for the length of a reasoning run on the memory backend,
+# and copying the graph costs about 221 bytes a triple. The patch is a public
+# wrapper over `Storage::snapshot`, which `Store::len` and
+# `Store::quads_for_pattern` already call internally on every read.
+#
+# Pinned by REVISION and not by branch, so that what CI builds is what was
+# reviewed. Upstream at oxigraph/oxigraph; when it lands this stanza goes and
+# the version bound below is all that remains.
+[patch.crates-io]
+oxigraph = { git = "https://github.com/fabio-rovai/oxigraph.git", rev = "abf36ef22b61f9c4d810b94b647d500c05ee791c" }
diff --git a/src/graph.rs b/src/graph.rs
index 1a7afbee..8581bf72 100644
--- a/src/graph.rs
+++ b/src/graph.rs
@@ -903,7 +903,7 @@ impl GraphStore {
// write land between two of them and produce an asserted set that
// existed at no instant, which is the defect this closes arriving one
// level down.
- self.read_in_one_transaction(|txn| {
+ self.read_one_state(|txn| {
let mut triples = Vec::new();
// The names actually read, in the order read, so a certificate records
// what it was built from rather than what was asked for. The two differ
@@ -1022,18 +1022,24 @@ impl GraphStore {
/// ever had. `serve-http` and `daemon` share one `Arc<GraphStore>` across
/// every session, so the two writers are not hypothetical.
///
- /// The transaction is committed rather than rolled back because it writes
- /// nothing, and committing an empty transaction is how Oxigraph ends one.
- /// A failure to end it is returned rather than swallowed: a reader that
- /// leaked a transaction would be a worse defect than the one this closes.
- fn read_in_one_transaction<T>(
+ /// This used to open a TRANSACTION, which was atomic and cost more than it
+ /// needed to. On the in-memory backend, which is what `GraphStore::new`
+ /// builds and what `serve-http` and `daemon` share, an open read
+ /// transaction blocks every writer until it commits: `Store::insert` opens
+ /// a transaction of its own and `MemoryStorage::start_transaction` waits.
+ /// A selection over a large graph therefore stopped every writer for the
+ /// length of the read. Measured in `tests/transaction_isolation_test.rs`.
+ ///
+ /// It now takes a SNAPSHOT, which pins the same one state and holds no
+ /// lock, so writers proceed while the read runs. `Store::snapshot` is not
+ /// in Oxigraph 0.5.9; it is the patch this repository carries and is
+ /// pinned by revision in `Cargo.toml`. See decision 0010 for why neither
+ /// the transaction nor the copy it was weighed against was good enough.
+ fn read_one_state<T>(
&self,
- f: impl FnOnce(&oxigraph::store::Transaction<'_>) -> anyhow::Result<T>,
+ f: impl FnOnce(&oxigraph::store::StoreSnapshot) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
- let txn = self.store.start_transaction()?;
- let out = f(&txn);
- txn.commit()?;
- out
+ f(&self.store.snapshot())
}
/// Every triple in the store EXCEPT those in the named graphs listed, in
@@ -1056,7 +1062,7 @@ impl GraphStore {
/// graphs it read is checkable against the store, and one that does not is
/// not. `<default>` is the unnamed graph.
pub fn triples_outside(&self, excluded: &[&str]) -> anyhow::Result<AssertedTriples> {
- self.read_in_one_transaction(|txn| {
+ self.read_one_state(|txn| {
let mut triples = Vec::new();
let mut read: BTreeSet<String> = BTreeSet::new();
for quad in txn.iter() {
diff --git a/tests/read_does_not_block_writers_test.rs b/tests/read_does_not_block_writers_test.rs
new file mode 100644
index 00000000..ab25d40a
--- /dev/null
+++ b/tests/read_does_not_block_writers_test.rs
@@ -0,0 +1,95 @@
+//! **A certified read no longer stops the store (#181).**
+//!
+//! `triples_outside` is the selection the certified reasoning paths use, and it
+//! has to see ONE state: a torn read produces an `asserted.tsv` that is
+//! internally consistent and describes a graph nobody ever had, which the Lean
+//! checker cannot detect because it never sees the store. #195 made it atomic by
+//! wrapping it in a transaction, and that was right about the atomicity and
+//! expensive in a way nobody had measured.
+//!
+//! On the in-memory backend, which is what `GraphStore::new` builds and what
+//! `serve-http` and `daemon` share across every session, an open read
+//! transaction blocks EVERY writer until it commits, because `Store::insert`
+//! opens a transaction of its own and `MemoryStorage::start_transaction` waits.
+//! A selection over a large graph therefore stopped the whole store for the
+//! length of the read.
+//!
+//! It now takes a snapshot instead: the same one state, no lock. This file is
+//! the gate on that, and it is written so that it fails if the transaction ever
+//! comes back, rather than merely passing faster.
+
+use open_ontologies::graph::GraphStore;
+use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
+use std::sync::Arc;
+use std::time::{Duration, Instant};
+
+const BIG: usize = 200_000;
+
+fn turtle(from: usize, n: usize) -> String {
+ let mut s = String::with_capacity(n * 60);
+ for i in from..from + n {
+ s.push_str(&format!(
+ "<http://ex.org/{i}> <http://ex.org/p> <http://ex.org/o> .\n"
+ ));
+ }
+ s
+}
+
+#[test]
+fn a_writer_makes_progress_while_a_long_certified_read_runs() {
+ let graph = Arc::new(GraphStore::new());
+ graph.load_turtle(&turtle(0, BIG), None).expect("seed");
+
+ let running = Arc::new(AtomicBool::new(true));
+ let writes = Arc::new(AtomicUsize::new(0));
+ let (w, wr, wc) = (Arc::clone(&graph), Arc::clone(&running), Arc::clone(&writes));
+ let writer = std::thread::spawn(move || {
+ let mut i = 10_000_000usize;
+ while wr.load(Ordering::SeqCst) {
+ w.load_turtle(&turtle(i, 1), None).expect("write");
+ wc.fetch_add(1, Ordering::SeqCst);
+ i += 1;
+ }
+ });
+
+ // Let the writer get going, so a zero below means blocked rather than
+ // not yet started.
+ let warmup_deadline = Instant::now() + Duration::from_secs(5);
+ while writes.load(Ordering::SeqCst) < 5 && Instant::now() < warmup_deadline {
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ let warm = writes.load(Ordering::SeqCst);
+ assert!(warm >= 5, "the writer never started; it managed {warm} writes before the read");
+
+ // ONE read. The whole measurement is what the writer achieves DURING it,
+ // because a transaction is released between calls and a test that made
+ // several reads would hand the writer a gap after each one and measure
+ // nothing. That is exactly what the first version of this test did, and it
+ // passed with the transaction restored.
+ let before = writes.load(Ordering::SeqCst);
+ let t0 = Instant::now();
+ let (triples, _graphs) = graph.triples_outside(&[]).expect("read");
+ let read = t0.elapsed();
+ let during = writes.load(Ordering::SeqCst) - before;
+
+ running.store(false, Ordering::SeqCst);
+ writer.join().expect("writer thread");
+
+ assert!(
+ triples.len() >= BIG,
+ "the selection returned {} triples, fewer than the {BIG} seeded",
+ triples.len()
+ );
+ assert!(
+ read > Duration::from_millis(100),
+ "a single read took {read:?}, too short for this test to mean anything. Raise BIG \
+ rather than deleting the assertion."
+ );
+ assert!(
+ during > 0,
+ "the writer completed NOTHING during a single {read:?} read, having managed {warm} \
+ writes in the moments before it. That is what a read TRANSACTION does on the \
+ in-memory backend: the writer cannot open its own transaction until the reader \
+ commits. The snapshot has been reverted."
+ );
+}