|
| 1 | +use super::scrub_postgres_tests::{options, setup, PauseAfterPlan}; |
| 2 | +use super::*; |
| 3 | +use crate::user_deletion::ExecutionMode; |
| 4 | +use nostr::{EventBuilder, Kind}; |
| 5 | + |
| 6 | +#[tokio::test] |
| 7 | +#[ignore = "requires Postgres"] |
| 8 | +async fn event_batch_deletion_preserves_large_unrelated_delivery_history() { |
| 9 | + let (db, c, _, alice, _) = setup().await; |
| 10 | + for n in 0..100 { |
| 11 | + let event = EventBuilder::new(Kind::Custom(1063), format!("personal metadata {n}")) |
| 12 | + .sign_with_keys(&alice) |
| 13 | + .unwrap(); |
| 14 | + db.insert_event(c, &event, None).await.unwrap(); |
| 15 | + } |
| 16 | + // Event IDs have no delivery-log index. A per-event dependency probe scans |
| 17 | + // this unrelated history 100 times and exceeds the executor's 3s timeout. |
| 18 | + sqlx::query( |
| 19 | + "INSERT INTO delivery_log(community_id,subscription_id,event_id,error_message) |
| 20 | + SELECT $1,'unrelated',decode(md5(g::text)||md5(g::text),'hex'),repeat('x',128) |
| 21 | + FROM generate_series(1,500000) g", |
| 22 | + ) |
| 23 | + .bind(c.as_uuid()) |
| 24 | + .execute(db.pool()) |
| 25 | + .await |
| 26 | + .unwrap(); |
| 27 | + // Many tiny tenants make generic-plan estimates underestimate this tenant. |
| 28 | + // A plain IN semi join can compare every log to every event after ANALYZE. |
| 29 | + sqlx::query( |
| 30 | + "WITH tenants AS ( |
| 31 | + INSERT INTO communities(id,host) |
| 32 | + SELECT gen_random_uuid(),'delivery-'||g||'.example' |
| 33 | + FROM generate_series(1,9999) g RETURNING id |
| 34 | + ) INSERT INTO delivery_log(community_id,event_id) |
| 35 | + SELECT id,decode(repeat('ff',32),'hex') FROM tenants", |
| 36 | + ) |
| 37 | + .execute(db.pool()) |
| 38 | + .await |
| 39 | + .unwrap(); |
| 40 | + sqlx::query("ANALYZE delivery_log") |
| 41 | + .execute(db.pool()) |
| 42 | + .await |
| 43 | + .unwrap(); |
| 44 | + let pool = sqlx::postgres::PgPoolOptions::new() |
| 45 | + .connect_with( |
| 46 | + (*db.pool().connect_options()) |
| 47 | + .clone() |
| 48 | + .options([("plan_cache_mode", "force_generic_plan")]), |
| 49 | + ) |
| 50 | + .await |
| 51 | + .unwrap(); |
| 52 | + let db = crate::Db::from_pool(pool); |
| 53 | + let mut opts = options(c, &alice); |
| 54 | + opts.batch_size = 100; |
| 55 | + for (mode, expected) in [ |
| 56 | + (ExecutionMode::ReadOnly, 100), |
| 57 | + (ExecutionMode::Delete, 100), |
| 58 | + (ExecutionMode::Delete, 0), |
| 59 | + ] { |
| 60 | + let result = db |
| 61 | + .user_deletion_store() |
| 62 | + .run_message_batches(&opts, mode, None, &mut Vec::new()) |
| 63 | + .await |
| 64 | + .unwrap(); |
| 65 | + assert_eq!(result.items, expected); |
| 66 | + } |
| 67 | + let events: i64 = sqlx::query_scalar("SELECT count(*) FROM events WHERE community_id=$1") |
| 68 | + .bind(c.as_uuid()) |
| 69 | + .fetch_one(db.pool()) |
| 70 | + .await |
| 71 | + .unwrap(); |
| 72 | + assert_eq!(events, 0); |
| 73 | + let logs: i64 = sqlx::query_scalar("SELECT count(*) FROM delivery_log WHERE community_id=$1") |
| 74 | + .bind(c.as_uuid()) |
| 75 | + .fetch_one(db.pool()) |
| 76 | + .await |
| 77 | + .unwrap(); |
| 78 | + assert_eq!(logs, 500_000); |
| 79 | +} |
| 80 | + |
| 81 | +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 82 | +#[ignore = "requires Postgres"] |
| 83 | +async fn historical_file_publications_and_late_delivery_copies_follow_source_scope() { |
| 84 | + let (db, c, _, alice, bob) = setup().await; |
| 85 | + let other = db |
| 86 | + .ensure_configured_community(&format!("copies-{}.example", Uuid::new_v4())) |
| 87 | + .await |
| 88 | + .unwrap() |
| 89 | + .id; |
| 90 | + let source = EventBuilder::new(Kind::Custom(1063), "personal file description") |
| 91 | + .sign_with_keys(&alice) |
| 92 | + .unwrap(); |
| 93 | + let survivor = EventBuilder::new(Kind::Custom(1063), "another publisher's description") |
| 94 | + .sign_with_keys(&bob) |
| 95 | + .unwrap(); |
| 96 | + for community in [c, other] { |
| 97 | + db.ensure_user(community, bob.public_key().as_bytes()) |
| 98 | + .await |
| 99 | + .unwrap(); |
| 100 | + db.insert_event(community, &source, None).await.unwrap(); |
| 101 | + db.insert_event(community, &survivor, None).await.unwrap(); |
| 102 | + sqlx::query( |
| 103 | + "INSERT INTO subscriptions(community_id,id,owner_pubkey) VALUES($1,'survivor',$2)", |
| 104 | + ) |
| 105 | + .bind(community.as_uuid()) |
| 106 | + .bind(bob.public_key().as_bytes().as_slice()) |
| 107 | + .execute(db.pool()) |
| 108 | + .await |
| 109 | + .unwrap(); |
| 110 | + } |
| 111 | + let (planned_tx, planned_rx) = tokio::sync::oneshot::channel(); |
| 112 | + let (resume_tx, resume_rx) = std::sync::mpsc::channel(); |
| 113 | + let store = db.user_deletion_store(); |
| 114 | + let opts = options(c, &alice); |
| 115 | + let task = tokio::spawn(async move { |
| 116 | + let mut writer = PauseAfterPlan { |
| 117 | + record: "delete_start", |
| 118 | + line: Vec::new(), |
| 119 | + planned: Some(planned_tx), |
| 120 | + resume: resume_rx, |
| 121 | + }; |
| 122 | + store |
| 123 | + .run_message_batches(&opts, ExecutionMode::Delete, None, &mut writer) |
| 124 | + .await |
| 125 | + }); |
| 126 | + planned_rx.await.unwrap(); |
| 127 | + for community in [c, other] { |
| 128 | + for event in [&source, &survivor] { |
| 129 | + sqlx::query("INSERT INTO delivery_log(community_id,subscription_id,event_id,error_message) VALUES($1,'survivor',$2,'copied payload')") |
| 130 | + .bind(community.as_uuid()).bind(event.id.as_bytes().as_slice()) |
| 131 | + .execute(db.pool()).await.unwrap(); |
| 132 | + } |
| 133 | + } |
| 134 | + resume_tx.send(()).unwrap(); |
| 135 | + let error = task.await.unwrap().unwrap_err(); |
| 136 | + assert!( |
| 137 | + error.to_string().contains("new dependent records"), |
| 138 | + "{error}" |
| 139 | + ); |
| 140 | + let exists: bool = |
| 141 | + sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM events WHERE community_id=$1 AND id=$2)") |
| 142 | + .bind(c.as_uuid()) |
| 143 | + .bind(source.id.as_bytes().as_slice()) |
| 144 | + .fetch_one(db.pool()) |
| 145 | + .await |
| 146 | + .unwrap(); |
| 147 | + assert!( |
| 148 | + exists, |
| 149 | + "source must survive until all dependent logs can be planned" |
| 150 | + ); |
| 151 | + for mode in [ExecutionMode::ReadOnly, ExecutionMode::Delete] { |
| 152 | + let result = db |
| 153 | + .user_deletion_store() |
| 154 | + .run_message_batches(&options(c, &alice), mode, None, &mut Vec::new()) |
| 155 | + .await |
| 156 | + .unwrap(); |
| 157 | + assert_eq!(result.items, 2); |
| 158 | + } |
| 159 | + for (community, expected) in [(c, 1_i64), (other, 2)] { |
| 160 | + for table in ["events", "delivery_log"] { |
| 161 | + let count: i64 = sqlx::query_scalar(sqlx::AssertSqlSafe(format!( |
| 162 | + "SELECT count(*) FROM {table} WHERE community_id=$1" |
| 163 | + ))) |
| 164 | + .bind(community.as_uuid()) |
| 165 | + .fetch_one(db.pool()) |
| 166 | + .await |
| 167 | + .unwrap(); |
| 168 | + assert_eq!(count, expected, "{table}"); |
| 169 | + } |
| 170 | + let subscriptions: i64 = |
| 171 | + sqlx::query_scalar("SELECT count(*) FROM subscriptions WHERE community_id=$1") |
| 172 | + .bind(community.as_uuid()) |
| 173 | + .fetch_one(db.pool()) |
| 174 | + .await |
| 175 | + .unwrap(); |
| 176 | + assert_eq!(subscriptions, 1); |
| 177 | + } |
| 178 | + let preserved: String = |
| 179 | + sqlx::query_scalar("SELECT content FROM events WHERE community_id=$1 AND id=$2") |
| 180 | + .bind(c.as_uuid()) |
| 181 | + .bind(survivor.id.as_bytes().as_slice()) |
| 182 | + .fetch_one(db.pool()) |
| 183 | + .await |
| 184 | + .unwrap(); |
| 185 | + assert_eq!(preserved, survivor.content); |
| 186 | + assert_eq!( |
| 187 | + db.user_deletion_store() |
| 188 | + .run_message_batches( |
| 189 | + &options(c, &alice), |
| 190 | + ExecutionMode::Delete, |
| 191 | + None, |
| 192 | + &mut Vec::new() |
| 193 | + ) |
| 194 | + .await |
| 195 | + .unwrap() |
| 196 | + .items, |
| 197 | + 0 |
| 198 | + ); |
| 199 | +} |
0 commit comments