Files
buzz/docker-compose.yml
Salman Mohammed e68cd09794 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>
2026-11-24 15:09:57 -05:00

228 lines
5.6 KiB
YAML

name: buzz
services:
postgres:
image: postgres:17-alpine
container_name: buzz-postgres
environment:
POSTGRES_USER: buzz
POSTGRES_PASSWORD: buzz_dev
POSTGRES_DB: buzz
PGDATA: /var/lib/postgresql/data
ports:
- "127.0.0.1:5432:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
networks:
- buzz-net
healthcheck:
test: ["CMD-SHELL", "pg_isready -U buzz"]
interval: 5s
timeout: 5s
retries: 10
start_period: 10s
deploy:
resources:
limits:
memory: 512m
labels:
com.buzz.service: "postgres"
com.buzz.env: "dev"
restart: unless-stopped
redis:
image: redis:7-alpine
container_name: buzz-redis
ports:
- "127.0.0.1:6379:6379"
networks:
- buzz-net
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s
deploy:
resources:
limits:
memory: 128m
labels:
com.buzz.service: "redis"
com.buzz.env: "dev"
restart: unless-stopped
adminer:
image: adminer:latest
container_name: buzz-adminer
ports:
- "127.0.0.1:8082:8080"
networks:
- buzz-net
depends_on:
postgres:
condition: service_healthy
environment:
ADMINER_DEFAULT_SERVER: postgres
deploy:
resources:
limits:
memory: 64m
labels:
com.buzz.service: "adminer"
com.buzz.env: "dev"
restart: unless-stopped
keycloak:
image: quay.io/keycloak/keycloak:26.0
container_name: buzz-keycloak
command: start-dev --http-port=8080
environment:
KC_DB: dev-mem
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
ports:
- "127.0.0.1:8180:8080"
networks:
- buzz-net
healthcheck:
test: ["CMD-SHELL", "exec 3<>/dev/tcp/localhost/8080 && echo -e 'GET /health/ready HTTP/1.1\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n' >&3 && cat <&3 | grep -q '200 OK'"]
interval: 10s
timeout: 5s
retries: 15
start_period: 30s
deploy:
resources:
limits:
memory: 512m
labels:
com.buzz.service: "keycloak"
com.buzz.env: "dev"
restart: unless-stopped
minio:
# Silo preserves the MinIO configuration and health endpoints.
image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z@sha256:635197cb9f36d01bee221d34d1c7d7960f6a95c48b0b6c01d99cd13bdae51a46
container_name: buzz-minio
command: server /data --console-address ":9001"
environment:
MINIO_ROOT_USER: buzz_dev
MINIO_ROOT_PASSWORD: buzz_dev_secret
ports:
- "127.0.0.1:9000:9000"
- "127.0.0.1:9001:9001"
volumes:
- minio-data:/data
networks:
- buzz-net
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 5s
timeout: 5s
retries: 10
start_period: 10s
deploy:
resources:
limits:
memory: 256m
labels:
com.buzz.service: "minio"
com.buzz.env: "dev"
restart: unless-stopped
minio-init:
image: pgsty/mc:RELEASE.2026-09-16T00-00-00Z@sha256:cfc83108c3abb371f8fb84d99c1fdc88f8c237e022409b0081fb7c0a3be634dd
container_name: buzz-minio-init
depends_on:
minio:
condition: service_healthy
networks:
- buzz-net
entrypoint: >
/bin/sh -c "
mc alias set local http://minio:9000 buzz_dev buzz_dev_secret &&
mc mb --ignore-existing local/buzz-media &&
mc anonymous set none local/buzz-media
"
labels:
com.buzz.service: "minio-init"
com.buzz.env: "dev"
restart: "no"
# Opt-in message index; see docs/opensearch-search-deployment.md.
opensearch:
image: opensearchproject/opensearch:2.19.3@sha256:e96cc6ae1500a073d973c0906f30f7cf4d9c461f32f855f9242a2da933660cdd
container_name: buzz-opensearch
profiles: ["search"]
environment:
discovery.type: single-node
DISABLE_SECURITY_PLUGIN: "true"
DISABLE_INSTALL_DEMO_CONFIG: "true"
OPENSEARCH_JAVA_OPTS: "-Xms512m -Xmx512m"
ports:
- "127.0.0.1:9200:9200"
volumes:
- opensearch-data:/usr/share/opensearch/data
networks:
- buzz-net
healthcheck:
test: ["CMD-SHELL", "curl -fsS http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=1s"]
interval: 5s
timeout: 5s
retries: 24
start_period: 20s
deploy:
resources:
limits:
memory: 1g
labels:
com.buzz.service: "opensearch"
com.buzz.env: "dev"
restart: unless-stopped
prometheus:
image: prom/prometheus:latest
container_name: buzz-prometheus
ports:
- "127.0.0.1:9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prometheus-data:/prometheus
networks:
- buzz-net
extra_hosts:
- "host.docker.internal:host-gateway"
deploy:
resources:
limits:
memory: 128m
labels:
com.buzz.service: "prometheus"
com.buzz.env: "dev"
restart: unless-stopped
volumes:
postgres-data:
name: buzz-postgres-data
labels:
com.buzz.volume: "postgres"
minio-data:
name: buzz-minio-data
labels:
com.buzz.volume: "minio"
prometheus-data:
name: buzz-prometheus-data
labels:
com.buzz.volume: "prometheus"
opensearch-data:
name: buzz-opensearch-data
labels:
com.buzz.volume: "opensearch"
networks:
buzz-net:
name: buzz-net
driver: bridge
labels:
com.buzz.network: "dev"