Skip to content

Commit bf56d89

Browse files
authored
feat(search): remove a deleted community's messages from OpenSearch (#9999)
## Summary Deleting a community now removes its messages from the OpenSearch index. Before this PR they stayed there, and that was the main reason real data was kept off OpenSearch behind `BUZZ_OPENSEARCH_PREVIEW`. The relay indexes chat messages into OpenSearch beside Postgres. Community deletion (`buzz-admin deletions run`) walks a request through fixed stages: it fences the community, purges Postgres, purges Redis, then verifies that nothing is left. It never touched OpenSearch, so a deleted community's text stayed in the index for good. Search never showed it, because every hit is re-read from Postgres, but the words stayed on disk. This PR adds OpenSearch to the purge and to the final verification: - **Before fencing (`Approved`):** the executor checks that it can reach OpenSearch. If Postgres shows an index is maintained but `BUZZ_OPENSEARCH_URL` is unset, the deletion stops and retries rather than skipping the index. - **Purge (`PostgresPurged`):** the executor pages through the community's documents with `_search` and `search_after`, 500 at a time. It deletes each page with a versioned `_bulk` request, taking versions from the same `search_index_version` sequence the indexer uses. It does not use `_delete_by_query`, which OpenSearch Serverless does not offer. - **Verification (`CachePurged`):** the executor waits a lease-heartbeated 90-second settle window (covering `BULK_TIMEOUT` = 60 s plus margin and Serverless write-to-searchable lag), refreshes on clusters that support `_refresh`, and runs a second purge pass before counting. If that pass deletes any stragglers, the stage fails transiently and retries so zero only counts after a full quiet window. Other changes: - The relay and the deletion executor share `OpenSearchConfig::parse_parts` and `OpenSearchConfig::with_aws_sigv4` (with `OpenSearchConfig::from_env` used by `buzz-deletion`). - The Helm `operatorJobs.deletionDrain` CronJob wires the five optional `BUZZ_OPENSEARCH_*` Secret keys (`BUZZ_OPENSEARCH_URL`, `BUZZ_OPENSEARCH_USERNAME`, `BUZZ_OPENSEARCH_PASSWORD`, `BUZZ_OPENSEARCH_AWS_SIGV4_SERVICE`, `BUZZ_OPENSEARCH_AWS_REGION`) via `optional: true` `secretKeyRef`. - The deployment and deletion docs cover passing OpenSearch settings to `buzz-admin deletions`, pausing indexing vs. permanently decommissioning OpenSearch, and the residual late-write retention gap. - `BUZZ_OPENSEARCH_PREVIEW` stays required. Its error text and doc now name the gaps that remain: late writes and Postgres failover. **Risk:** OpenSearch is not deployed anywhere yet and stays behind the preview flag, so this affects no live search. A deployment that never ran the indexer skips the OpenSearch steps. Before turning OpenSearch on anywhere: the deletion executor needs the same OpenSearch settings (and IAM identity when using SigV4) and mapping version as the relays, and each deletion takes at least 90 s longer (plus 30 s + 90 s per straggler retry, well inside the CronJob's 3600 s deadline). Rollback is the older executor, which leaves docs in the index as before. ### Related issue N/A. No existing issue or PR found. ### Testing Ran locally on `5cba5c7d02` against Postgres, Redis, and OpenSearch 2.19: - `./scripts/postgres-test-run.sh -p buzz-deletion -j 4 -E 'test(community_purge_deletes) | test(execute_stage_wires_opensearch)'`: 2 of 2 passed. - `community_purge_deletes_and_verifies_opensearch_documents`: verifies fail-closed when `BUZZ_OPENSEARCH_URL` is unset, purges target while keeping bystander intact, plants a late straggler and verifies retryable error + cleanup + clean second pass. - `execute_stage_wires_opensearch_preflight_purge_and_verification`: exercises `execute_stage` end-to-end across `Approved` (unreachable OpenSearch fails closed and leaves community unfenced), `PostgresPurged` (purges target docs and records `deleted_search_documents: 2`), and `CachePurged` (verifies absence and records `"search_index": true`). - `cargo clippy -p buzz-deletion --all-targets -- -D warnings` and `cargo fmt --check`: passed. - Helm `templates/deletion-drain-cronjob.yaml` render check and CI `lint + unittest + render matrix`: passed. Not tested: - Running against a real Amazon OpenSearch Serverless (`aoss`) collection. --------- Signed-off-by: Salman Mohammed <smohammed@squareup.com>
1 parent 0f51a74 commit bf56d89

12 files changed

Lines changed: 1078 additions & 112 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/buzz-db/src/store/deletion.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,18 @@ impl DeletionStore {
923923
.unwrap_or(false)
924924
}
925925

926+
/// Whether an indexing relay has ever maintained a search index against
927+
/// this database.
928+
pub async fn search_index_maintained(&self) -> Result<bool> {
929+
crate::search_outbox::is_index_maintained(&self.pool).await
930+
}
931+
932+
/// Take the next monotonic external version for OpenSearch document
933+
/// deletes during community purge.
934+
pub async fn next_search_index_version(&self) -> Result<i64> {
935+
crate::search_outbox::next_index_version(&self.pool).await
936+
}
937+
926938
/// Persist a request. Only active non-tombstone communities may be submitted.
927939
///
928940
/// `requested_by` is recorded on a new row and is also the convergence key

‎crates/buzz-db/src/store/search_outbox.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,26 @@ pub(crate) async fn index_maintained(conn: &mut sqlx::PgConnection) -> Result<bo
141141
.await?)
142142
}
143143

144+
/// Whether an indexing relay has ever run against this database.
145+
///
146+
/// Used by out-of-band processes such as community deletion to fail closed
147+
/// when a search index is maintained but no OpenSearch address was configured.
148+
pub async fn is_index_maintained(pool: &PgPool) -> Result<bool> {
149+
let mut conn = pool.acquire().await?;
150+
index_maintained(&mut conn).await
151+
}
152+
153+
/// Take the next monotonic external version from `search_index_version`.
154+
///
155+
/// Used when writing or deleting documents outside a normal outbox batch
156+
/// (such as whole-community purge) so late requests with older versions cannot
157+
/// overwrite newer deletes.
158+
pub async fn next_index_version(pool: &PgPool) -> Result<i64> {
159+
Ok(sqlx::query_scalar("SELECT nextval('search_index_version')")
160+
.fetch_one(pool)
161+
.await?)
162+
}
163+
144164
/// Queue the index change for an event this admitted transaction inserted,
145165
/// soft-deleted or otherwise changed: the event itself when it is
146166
/// searchable, or the message it edits when it is a message edit.

‎crates/buzz-deletion/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ buzz-core = { workspace = true }
1414
buzz-db = { workspace = true }
1515
buzz-media = { workspace = true }
1616
buzz-pubsub = { workspace = true }
17+
buzz-search = { workspace = true }
1718
chrono = { workspace = true }
1819
clap = { version = "4", features = ["derive"] }
1920
hex = { workspace = true }
@@ -24,6 +25,7 @@ serde_json = { workspace = true }
2425
sqlx = { workspace = true }
2526
tokio = { workspace = true }
2627
tokio-util = { workspace = true }
28+
tracing = { workspace = true }
2729
uuid = { workspace = true }
2830

2931
fs2 = "0.4"

0 commit comments

Comments
 (0)