Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ BUZZ_PUSH_GATEWAY_DELIVERY_URL=https://push.buzz.xyz/v1/deliveries/apns
# measured_at as already recorded.
# BUZZ_STORAGE_USAGE_URL=https://kgoose.example/cash-app/goose/v1/buzz/storage-usage/record

# Optional OpenSearch message index; see docs/opensearch-search-deployment.md.
# BUZZ_OPENSEARCH_URL=http://localhost:9200
# BUZZ_OPENSEARCH_USERNAME=
# BUZZ_OPENSEARCH_PASSWORD=
# BUZZ_OPENSEARCH_PREVIEW=true

# Optional relay-owned KLIPY key. When set, NIP-11 advertises GIF search and
# authenticated desktop clients use this relay as the metadata/search proxy.
# Keep the real value in your deployment's secret manager; never commit it.
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/_ci-relay.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,18 @@ jobs:
PGSCHEMA_PLAN_PORT: "5432"
PGSCHEMA_PLAN_USER: buzz
PGSCHEMA_PLAN_PASSWORD: buzz_dev
BUZZ_TEST_OPENSEARCH_URL: http://localhost:9200
steps:
# Started without waiting, so its boot overlaps the setup steps below.
# Not a service container: those block the job until they are healthy.
- name: Start OpenSearch
run: |
docker run --detach --name opensearch --publish 9200:9200 \
--env discovery.type=single-node \
--env DISABLE_SECURITY_PLUGIN=true \
--env DISABLE_INSTALL_DEMO_CONFIG=true \
--env OPENSEARCH_JAVA_OPTS="-Xms512m -Xmx512m" \
opensearchproject/opensearch:2.19.3@sha256:e96cc6ae1500a073d973c0906f30f7cf4d9c461f32f855f9242a2da933660cdd
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
- uses: cashapp/activate-hermit@cea9af7913204a965fd488637a8d1811bba2e616 # v1
# pgschema has no --version; --help exits 0 once Hermit has fetched it.
Expand Down Expand Up @@ -239,6 +250,16 @@ jobs:
path: target/ci
- name: Restore relay executable permission
run: chmod +x ./target/ci/buzz-relay
- name: Wait for OpenSearch
run: |
for _ in $(seq 1 60); do
if curl -fsS "$BUZZ_TEST_OPENSEARCH_URL/_cluster/health?wait_for_status=yellow&timeout=1s" >/dev/null; then
exit 0
fi
sleep 2
done
docker logs opensearch | tail -50
exit 1
- name: PostgreSQL-backed tests
env:
BUZZ_POSTGRES_ADMIN_URL: postgres://buzz:${{ env.BUZZ_TEST_POSTGRES_PASSWORD }}@localhost:5432/postgres
Expand Down
44 changes: 38 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jsonwebtoken = { version = "10.4.0", default-features = false, features = ["aws_

# HTTP client (webhook delivery)
reqwest = { version = "0.13", features = ["json", "rustls", "stream"], default-features = false }
opensearch = { version = "2.4", default-features = false, features = ["rustls-tls"] }

# Cryptography
sha2 = "0.11"
Expand Down
2 changes: 1 addition & 1 deletion crates/buzz-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub use store::{
git_repo, leader_lock, media, membership_snapshot, moderation, nip_fi_binding,
operator_listener, partition, personal_read, pins, product_feedback, push, reaction,
read_state, relay_admin_actions, relay_invite, relay_members, relay_operators, reminder,
replaceable, storage_accounting, thread, thread_window, user, user_deletion,
replaceable, search_outbox, storage_accounting, thread, thread_window, user, user_deletion,
user_deletion_fences, workflow,
};

Expand Down
54 changes: 49 additions & 5 deletions crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ pub(crate) async fn run_migrations_through(pool: &PgPool, target: i64) -> Result
.await
}

/// Migrations that only add a tenant table, applied after the fence
/// retirement (0090). Append each new one here.
#[cfg(test)]
const TENANT_TABLE_MIGRATIONS_AFTER_FENCE_RETIREMENT: [i64; 1] = [106];

/// Create the tenant tables added since the fence retirement, without
/// recording their migrations. For tests that stop before the retirement but
/// exercise the whole deletion catalog, and never migrate further.
#[cfg(test)]
pub(crate) async fn create_tenant_tables_added_after_fence_retirement(pool: &PgPool) -> Result<()> {
for version in TENANT_TABLE_MIGRATIONS_AFTER_FENCE_RETIREMENT {
let migration = MIGRATOR
.iter()
.find(|migration| migration.version == version)
.ok_or_else(|| crate::DbError::InvalidData(format!("no migration {version}")))?;
sqlx::raw_sql(migration.sql.as_str()).execute(pool).await?;
}
Ok(())
}

async fn run_migrations_locked(conn: &mut PgConnection) -> Result<()> {
reject_legacy_nip_rs_cardinality_ambiguity(conn).await?;
for (version, doc, indexes) in INLINE_INDEX_BUILDS {
Expand Down Expand Up @@ -1036,7 +1056,11 @@ mod postgres_tests {
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
migrations.sort_by_key(|migration| migration.version);

assert_eq!(migrations.len(), 105);
assert_eq!(migrations.len(), 106);
assert_eq!(migrations[105].version, 106);
assert!(
strip_sql_comments(migrations[105].sql.as_str()).contains("CREATE TABLE search_outbox")
);
assert_eq!(migrations[104].version, 105);
assert!(strip_sql_comments(migrations[104].sql.as_str())
.contains("ADD COLUMN finished BOOLEAN NOT NULL DEFAULT true;"));
Expand Down Expand Up @@ -1709,6 +1733,7 @@ mod postgres_tests {
"nip_fi_profile_heads",
"media_uploaders",
"media_publications",
"search_outbox",
];
for table in crate::deletion::EXPECTED_SCOPED_TABLES
.iter()
Expand Down Expand Up @@ -3501,6 +3526,7 @@ mod postgres_tests {
"user_deletion_fences",
"workflow_runs",
"push_gateway_installations",
"search_outbox",
] {
assert_eq!(
columns(&desired, table).await,
Expand Down Expand Up @@ -4469,11 +4495,22 @@ mod postgres_tests {
.validate_serving_catalog()
.await
.expect("new runtime must accept pre-retirement catalog");
store
// Deletion needs every tenant table this binary purges, and tables
// added after the retirement are not there yet.
let drift = store
.validate_catalog()
.await
.expect("deletion must accept pre-retirement catalog");
.expect_err("deletion refuses a catalog missing later tenant tables")
.to_string();
assert!(
drift.contains("(missing=search_outbox, unknown=)"),
"deletion must accept the pre-retirement fences: {drift}"
);
run_migrations(&pool).await.expect("apply all migrations");
store
.validate_catalog()
.await
.expect("deletion accepts the migrated catalog");
let fence_triggers: i64 = sqlx::query_scalar(
"SELECT count(*) FROM pg_trigger WHERE NOT tgisinternal AND tgname LIKE 'community_write_fence_%'",
)
Expand Down Expand Up @@ -4545,10 +4582,17 @@ mod postgres_tests {
.validate_serving_catalog()
.await
.expect("new code must boot before the drop");
store
// Deletion needs every tenant table this binary purges, and tables
// added after the retirement are not there yet.
let drift = store
.validate_catalog()
.await
.expect("deletion must accept the catalog before the drop");
.expect_err("deletion refuses a catalog missing later tenant tables")
.to_string();
assert!(
drift.contains("(missing=search_outbox, unknown=)"),
"deletion must accept the catalog before the drop: {drift}"
);
run_migrations(&pool).await.expect("apply all migrations");
assert_eq!(present(&pool).await, (false, false));
store
Expand Down
72 changes: 71 additions & 1 deletion crates/buzz-db/src/store/deletion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub const EXPECTED_SCOPED_TABLES: &[&str] = &[
"relay_invites",
"relay_members",
"scheduled_workflow_fires",
"search_outbox",
"thread_metadata",
"users",
"workflow_approvals",
Expand Down Expand Up @@ -161,7 +162,6 @@ pub const PENDING_SCOPED_TABLES: &[&str] = &[
"identity_retirement_holds",
"identity_retirement_workflows",
"search_backfill",
"search_outbox",
];

/// Tenant tables an expand migration may add between a deletion's approval
Expand Down Expand Up @@ -205,6 +205,7 @@ pub const PURGE_SCOPED_TABLES: &[&str] = &[
"scheduled_workflow_fires",
"workflow_runs",
"push_wake_outbox",
"search_outbox",
Comment thread
salman1993 marked this conversation as resolved.
Resolved
"join_policy_acceptances",
"media_uploaders",
"media_publications",
Expand Down Expand Up @@ -4867,6 +4868,9 @@ pub(crate) mod postgres_tests {
crate::migration::run_migrations_through(&db.pool, 89)
.await
.expect("migrate with fence objects present");
crate::migration::create_tenant_tables_added_after_fence_retirement(&db.pool)
.await
.expect("tenant tables added since");
let store = db.deletion_store();
let community = db
.ensure_configured_community("optional-fences.example")
Expand Down Expand Up @@ -5012,6 +5016,9 @@ DROP FUNCTION community_deletion_lock_key(UUID);
crate::migration::run_migrations_through(&db.pool, 86)
.await
.expect("phase-one schema");
crate::migration::create_tenant_tables_added_after_fence_retirement(&db.pool)
.await
.expect("tenant tables added since");
let community = db
.ensure_configured_community("optional-guards.example")
.await
Expand Down Expand Up @@ -9708,6 +9715,69 @@ DROP FUNCTION protect_community_deletion_manifest_keys();
(db, store, scratch)
}

/// Once a community is fenced for deletion, admission refuses its writes
Comment thread
wpfleger96 marked this conversation as resolved.
Resolved
/// (and so its outbox rows) and claims skip it; the purge removes its
/// outbox rows and keeps another community's.
#[tokio::test]
#[ignore = "requires Postgres"]
async fn search_outbox_is_fenced_and_purged() {
let (db, store) = store().await;
crate::search_outbox::enable(&db.pool)
.await
.expect("enable outbox");
let (request, inventory) = inventoried_request(&db, &store).await;
let community = request.community_id;
let bystander = db
.ensure_configured_community(&format!("bystander-{}.example", Uuid::new_v4().simple()))
.await
.expect("bystander community")
.id;
let keys = nostr::Keys::generate();
let message = |content: &str| {
nostr::EventBuilder::new(nostr::Kind::Custom(9), content)
.sign_with_keys(&keys)
.expect("sign")
};
for owner in [community, bystander] {
db.insert_event(owner, &message("queued"), None)
.await
.expect("insert");
}
let queued = |owner: CommunityId| {
let pool = db.pool.clone();
async move {
sqlx::query_scalar::<_, i64>(
"SELECT count(*) FROM search_outbox WHERE community_id = $1",
)
.bind(owner.as_uuid())
.fetch_one(&pool)
.await
.expect("count outbox rows")
}
};

let token = fence_and_drain(&store, &request, &inventory).await;
db.insert_event(community, &message("after fence"), None)
.await
.expect_err("a fenced community admits no writes");
assert_eq!(queued(community).await, 1);
let batch = crate::search_outbox::claim_batch(&db.pool, 10, &[])
.await
.expect("claim")
.expect("the bystander's batch");
assert_eq!(
batch.community(),
bystander,
"claims skip a fenced community"
);
drop(batch);

let deleted = store.purge_postgres(&token).await.expect("purge postgres");
assert_eq!(deleted.get("search_outbox"), Some(&1));
assert_eq!(queued(community).await, 0);
assert_eq!(queued(bystander).await, 1);
}

/// Approve, claim, fence, drain and remove bindings, leaving the request
/// ready for `purge_postgres`.
async fn fence_and_drain(
Expand Down
Loading
Loading