mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
feat(search): keep an OpenSearch message index in sync through a Postgres outbox (#9838)
## Summary
This PR keeps a second copy of the message index in OpenSearch. Searches
still run on Postgres. Each searchable message writes a small "index me"
row to a new Postgres table, `search_outbox`, in the same transaction
that stores it. One relay drains that queue into OpenSearch. Writes
never wait on OpenSearch, and if it is down, rows wait in the queue.
```mermaid
sequenceDiagram
participant Client
participant Relay as Relay (admission tx)
participant PG as Postgres
participant Idx as Indexer (1 leader)
participant OS as OpenSearch
Client->>Relay: kind 9 "flamingo"
Relay->>PG: INSERT events + INSERT search_outbox (same tx)
loop every 5s, backing off up to 60s on failure
Idx->>PG: claim ≤500 rows of one community (SKIP LOCKED)
Idx->>PG: re-read those events (+ channel, thread, mentions)
Idx->>OS: one bulk request: upsert docs, delete missing ones
Idx->>PG: DELETE the claimed rows, COMMIT
end
```
- **Turn it on** with `BUZZ_OPENSEARCH_URL` plus
`BUZZ_OPENSEARCH_PREVIEW=true` (optional basic auth: set both or
neither). Without the opt-in, a relay with the URL refuses to start. The
relay refuses to start if migration `0106` has not run. Without the
setting, each insert pays one flag check.
- **Queue** (`migrations/0106`, `search_outbox.rs`): one row per stored
message of kind 9, 40002, 40008, 45001 or 45003. A rolled-back write
takes its row with it. The indexer re-reads each event, so duplicate
rows are harmless.
- **Indexer** (`indexer.rs`): one leader, chosen with a Postgres session
advisory lock. A batch holds its community's write transaction until the
bulk request ends, so deletion can't fence the community mid-batch. HTTP
400/413 skips that community for the pass. Any other failure ends the
pass and backs off. Batches have no byte cap yet, so a cluster with a 10
MiB request limit can reject a full batch; #9862 caps requests at 8 MiB.
- **Index** (`opensearch.rs`): alias `buzz-messages` →
`buzz-messages-v1`, strict mapping, message text indexed but not stored.
Writes require the alias, so they never create an index. Searches filter
on community first and fail rather than return partial hits.
- **Deletion:** community deletion fences and purges the community's
queued `search_outbox` rows with its other rows. It doesn't touch
documents already in the index.
- **Infra:** an opt-in `opensearch` service in docker-compose
(`--profile search`). The CI Postgres job starts OpenSearch 2.19.3. The
−1,895 lines move the Postgres search tests into `tests/search_suite/`,
which now runs against both backends.
### Worth knowing
- **A rejected batch blocks its community.** Rows are claimed oldest
first, so the same batch comes back every pass. Other communities still
drain, but each rejection counts as a failure, so passes slow to 60 s
apart.
- **Text isn't stored in the index,** so a new mapping version needs a
full backfill from Postgres. No backfill exists yet, so messages stored
before the setting aren't indexed.
- **Not for relays with real data yet.** Deletes, edits and account
scrubs don't reach the index until #9862, and nothing purges a deleted
community's documents. Searches never return them, since every hit is
re-read from Postgres, but their words stay in the index. So the relay
requires the `BUZZ_OPENSEARCH_PREVIEW` opt-in, which #9862 removes.
- **Coverage gaps:** each relay queues only what it stores, so set the
URL on every relay.
- **Delay:** up to ~5 s of polling plus a 30 s refresh before a message
is searchable.
### Testing
CI's Postgres job now runs the shared search suite and the outbox sync
tests against a real OpenSearch. For a local run, see
`docs/opensearch-search-deployment.md`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
21
.github/workflows/_ci-relay.yml
vendored
21
.github/workflows/_ci-relay.yml
vendored
@@ -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.
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user