|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +//! End-to-end cluster tests for the create-only |
| 3 | +//! `CatalogEntry::PutCollectionIfAbsent` primitive. |
| 4 | +//! |
| 5 | +//! `PutCollectionIfAbsent` materializes a collection through the |
| 6 | +//! metadata raft group ONLY when no collection of the same |
| 7 | +//! `(database_id, tenant_id, name)` already exists — it never |
| 8 | +//! clobbers an existing schema. This is the durable primitive CRDT |
| 9 | +//! sync will use to announce collections without racing a |
| 10 | +//! locally-authored definition. |
| 11 | +//! |
| 12 | +//! No SQL DDL emits this variant yet, so the tests propose the entry |
| 13 | +//! directly through `metadata_proposer::propose_catalog_entry` (which |
| 14 | +//! forwards to the metadata-group leader) and assert idempotency + |
| 15 | +//! no-clobber by reading the replicated record on every node. |
| 16 | +
|
| 17 | +mod common; |
| 18 | + |
| 19 | +use std::time::Duration; |
| 20 | + |
| 21 | +use common::cluster_harness::{TestCluster, wait_for}; |
| 22 | + |
| 23 | +use nodedb::control::catalog_entry::CatalogEntry; |
| 24 | +use nodedb::control::metadata_proposer::propose_catalog_entry; |
| 25 | +use nodedb::control::security::catalog::StoredCollection; |
| 26 | +use nodedb_types::DatabaseId; |
| 27 | + |
| 28 | +const TENANT: u64 = 1; |
| 29 | +const COLL: &str = "if_absent_coll"; |
| 30 | + |
| 31 | +/// Read the distinguishing fields `(bitemporal, declared_primary_key)` |
| 32 | +/// of a collection from a node's local `SystemCatalog` redb — the same |
| 33 | +/// record every node's applier writes. |
| 34 | +fn coll_fields( |
| 35 | + node: &common::cluster_harness::TestClusterNode, |
| 36 | + name: &str, |
| 37 | +) -> Option<(bool, Option<String>)> { |
| 38 | + node.shared |
| 39 | + .credentials |
| 40 | + .catalog() |
| 41 | + .as_ref() |
| 42 | + .and_then(|c| { |
| 43 | + c.get_collection(DatabaseId::DEFAULT, TENANT, name) |
| 44 | + .ok() |
| 45 | + .flatten() |
| 46 | + }) |
| 47 | + .map(|c| (c.bitemporal, c.declared_primary_key)) |
| 48 | +} |
| 49 | + |
| 50 | +/// Propose a `PutCollectionIfAbsent` for `coll`, trying each node |
| 51 | +/// until one accepts (the proposer forwards to the metadata leader, |
| 52 | +/// so any node works — the loop mirrors `exec_ddl_on_any_leader`). |
| 53 | +fn propose_if_absent(cluster: &TestCluster, coll: StoredCollection) -> Result<(), String> { |
| 54 | + let entry = CatalogEntry::PutCollectionIfAbsent(Box::new(coll)); |
| 55 | + let mut last_err = String::new(); |
| 56 | + for node in &cluster.nodes { |
| 57 | + match propose_catalog_entry(&node.shared, &entry) { |
| 58 | + Ok(_) => return Ok(()), |
| 59 | + Err(e) => last_err = e.to_string(), |
| 60 | + } |
| 61 | + } |
| 62 | + Err(format!( |
| 63 | + "no node accepted PutCollectionIfAbsent: {last_err}" |
| 64 | + )) |
| 65 | +} |
| 66 | + |
| 67 | +#[tokio::test(flavor = "multi_thread", worker_threads = 6)] |
| 68 | +async fn put_if_absent_creates_then_no_clobbers_then_idempotent() { |
| 69 | + let cluster = TestCluster::spawn_three().await.expect("3-node cluster"); |
| 70 | + |
| 71 | + // A: the winning definition — bitemporal + a distinct PRIMARY KEY. |
| 72 | + let mut a = StoredCollection::new(TENANT, COLL, "tester"); |
| 73 | + a.bitemporal = true; |
| 74 | + a.declared_primary_key = Some("a_key".to_string()); |
| 75 | + |
| 76 | + // 1. Create via PutCollectionIfAbsent (collection is absent). |
| 77 | + propose_if_absent(&cluster, a.clone()).expect("propose A"); |
| 78 | + |
| 79 | + // Assert A materialized on all three nodes with A's fields. |
| 80 | + wait_for( |
| 81 | + "all 3 nodes see collection with A's distinguishing fields", |
| 82 | + Duration::from_secs(10), |
| 83 | + Duration::from_millis(50), |
| 84 | + || { |
| 85 | + cluster |
| 86 | + .nodes |
| 87 | + .iter() |
| 88 | + .all(|n| coll_fields(n, COLL) == Some((true, Some("a_key".to_string())))) |
| 89 | + }, |
| 90 | + ) |
| 91 | + .await; |
| 92 | + |
| 93 | + // 2. Propose a DIFFERENT definition B under the same tenant+name. |
| 94 | + // Because the collection already exists, this must be a no-op. |
| 95 | + let mut b = StoredCollection::new(TENANT, COLL, "tester"); |
| 96 | + b.bitemporal = false; |
| 97 | + b.declared_primary_key = Some("b_key".to_string()); |
| 98 | + propose_if_absent(&cluster, b).expect("propose B"); |
| 99 | + |
| 100 | + // Wait for B's proposal to have applied cluster-wide, then assert |
| 101 | + // every node STILL shows A's fields — B was skipped, no clobber. |
| 102 | + wait_for( |
| 103 | + "no-clobber: every node still shows A after B proposal", |
| 104 | + Duration::from_secs(10), |
| 105 | + Duration::from_millis(50), |
| 106 | + || { |
| 107 | + cluster |
| 108 | + .nodes |
| 109 | + .iter() |
| 110 | + .all(|n| coll_fields(n, COLL) == Some((true, Some("a_key".to_string())))) |
| 111 | + }, |
| 112 | + ) |
| 113 | + .await; |
| 114 | + for node in &cluster.nodes { |
| 115 | + assert_eq!( |
| 116 | + coll_fields(node, COLL), |
| 117 | + Some((true, Some("a_key".to_string()))), |
| 118 | + "B must not clobber A on node {}", |
| 119 | + node.node_id |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + // 3. Re-propose A verbatim — idempotent no-op. Still exactly one |
| 124 | + // collection, unchanged. |
| 125 | + propose_if_absent(&cluster, a).expect("re-propose A"); |
| 126 | + wait_for( |
| 127 | + "idempotent: still exactly one collection with A's fields", |
| 128 | + Duration::from_secs(10), |
| 129 | + Duration::from_millis(50), |
| 130 | + || { |
| 131 | + cluster.nodes.iter().all(|n| { |
| 132 | + n.cached_collection_count() == 1 |
| 133 | + && coll_fields(n, COLL) == Some((true, Some("a_key".to_string()))) |
| 134 | + }) |
| 135 | + }, |
| 136 | + ) |
| 137 | + .await; |
| 138 | + for node in &cluster.nodes { |
| 139 | + assert_eq!( |
| 140 | + node.cached_collection_count(), |
| 141 | + 1, |
| 142 | + "exactly one collection on node {}", |
| 143 | + node.node_id |
| 144 | + ); |
| 145 | + assert_eq!( |
| 146 | + coll_fields(node, COLL), |
| 147 | + Some((true, Some("a_key".to_string()))), |
| 148 | + "A unchanged on node {}", |
| 149 | + node.node_id |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + cluster.shutdown().await; |
| 154 | +} |
0 commit comments