mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
## 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>
14 lines
669 B
SQL
14 lines
669 B
SQL
-- The search indexer's queue; see schema/tables/public/search_outbox.sql.
|
|
-- Its writers use community admission (docs/database.md). A new, empty table.
|
|
SET LOCAL lock_timeout = '5s';
|
|
CREATE TABLE search_outbox (
|
|
community_id UUID NOT NULL,
|
|
id UUID NOT NULL DEFAULT gen_random_uuid(),
|
|
event_id BYTEA NOT NULL CHECK (length(event_id) = 32),
|
|
event_created_at TIMESTAMPTZ NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
PRIMARY KEY (community_id, id)
|
|
);
|
|
CREATE INDEX search_outbox_queue ON search_outbox (created_at);
|
|
CREATE INDEX search_outbox_community_queue ON search_outbox (community_id, created_at);
|