Repository navigation
Commit dfda5fc
feat(search): pick the search backend per relay and shadow-compare OpenSearch (#9863)
## Summary
Follows #9838 and #9862 (indexing, deletes, edits, backfill, workers),
both merged. After those two, OpenSearch holds a full copy of the
message index but nothing reads it. This PR adds the switch that picks a
search backend for the whole relay, and a shadow mode that compares the
two without users seeing it.
**The switch.** An integer feature flag, `search.backend`, holds one
value for the whole relay. It is evaluated for the relay process, not
for the searched community, so a relay never serves some communities
from Postgres and others from OpenSearch. With LaunchDarkly a change
applies within seconds and is the rollback; with the environment
provider it takes a restart.
| Value | Behavior |
| --- | --- |
| 0 (default) | Postgres answers. OpenSearch is not queried. |
| 1 | Postgres answers. The same query also runs on OpenSearch, off the
response path. |
| 2 | OpenSearch answers. |
At value 2 a failure returns an error, and a timeout returns the same
`query timed out` reason and 503 as a Postgres timeout. A search is
bounded at 15 seconds, below Postgres's 20, and the bound is sent to
OpenSearch too. It does not fall back to Postgres, so a broken cluster
is visible and not hidden behind slow searches. In every mode the relay
re-reads hits from Postgres and re-checks access before it returns them.
**Shadow mode.** The shadow search runs alongside the Postgres search,
off the response path: the response never waits for it. It has a 2
second deadline and at most 32 OpenSearch queries run at once; beyond
that they are dropped and counted. Only first pages are compared. The
result is the overlap of the two top-10 lists, recorded in
`buzz_search_shadow_overlap_ratio` and in one log line, "search shadow
comparison". The log line carries no query text.
**Metrics.** `buzz_search_requests_total{backend,role,outcome}` (a
search that ran out of time counts as `timeout`, apart from `error`),
`buzz_search_duration_seconds`,
`buzz_search_opensearch_skipped_total{setting,reason}` and
`buzz_search_backend_flag_invalid_total`. The relay holding the indexer
lease also samples `buzz_search_outbox_depth` and
`buzz_search_index_lag_seconds` every 15 seconds, so the fleet reads the
queue once per interval.
**Only searches the index can answer go to it.** The index holds five
message kinds. At values 1 and 2 a search goes to OpenSearch only when
it names kinds and all of them are indexed. Any other search runs on
Postgres and is not shadowed; `buzz_search_opensearch_skipped_total`
counts it with the reason `unindexed_kinds`. Prefix searches
(`unsupported_mode`) and full-text searches with a `"phrase"`, a `-word`
exclusion or `or` (`unsupported_syntax`) also stay on Postgres: the
index matches plain words only, so it would match a phrase's words
anywhere and require an excluded word.
Worth knowing:
- A search that names no kinds stays on Postgres at every value.
- The desktop app sends full-text searches only for phrases and
exclusions, and prefix searches otherwise, so both stay on Postgres.
Until the index serves them, shadow mode compares mostly CLI, HTTP
bridge and agent searches.
- Postgres search does not see edits; OpenSearch indexes the edited
text. This lowers overlap for edited messages and is a visible change at
value 2.
- A new message is searchable in OpenSearch only after the index
refresh, up to 30 seconds.
- The environment flag provider reads the value at startup, so a change
needs a restart.
- Relevance in this PR is still the plain match from #9838. The next PR
in the stack replaces it.
### Related issue
N/A.
### Testing
A real relay built from this branch, with its own database and an
OpenSearch 2.19.3 container, started with
`BUZZ_FEATURE_FLAGS_PROVIDER=environment
BUZZ_FEATURE_FLAG_SEARCH_BACKEND=<n>`. Each value was checked through a
NIP-50 `REQ` and through the HTTP bridge (`buzz messages search`),
counting queries with OpenSearch's `query_total`:
- **0:** Postgres results; OpenSearch received no query.
- **1:** Postgres results; each search added one OpenSearch query and
one "search shadow comparison" line (overlap 1.0 once the index had
refreshed). No log line held the search word. `/metrics` showed the
duration and overlap histograms, request counts for both backends, and
the outbox gauges.
- **2:** After an edit, a search for a word only the edited text has
found the message, so OpenSearch answered. With OpenSearch stopped,
NIP-50 returned `CLOSED` (`error: search failed`) and the bridge
returned HTTP 500.
- **99:** Postgres results with OpenSearch stopped;
`buzz_search_backend_flag_invalid_total` counted each search.
- **No `BUZZ_OPENSEARCH_URL`:** Postgres results and Postgres request
metrics; no OpenSearch or outbox metrics.
- **Two relays:** only the lease holder reported the outbox gauges.
After it stopped, the other took the lease and began reporting them.
Relay tests also cover: timeouts (a hanging cluster, and `timed_out:
true`) counted as `timeout` at 1 and 2 and closed as `query timed out`
at 2, verified to fail when timeouts count as errors; prefix, phrase,
exclusion and `or` searches served by Postgres at 1 and 2 without
contacting the cluster, verified to fail without the syntax rule; a
removed member, a document of another community, and a deleted message
left in the index, all dropped at 2.
Not tested: a remote flag provider (LaunchDarkly), and production
traffic.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>1 parent 634673b commit dfda5fc
18 files changed
Lines changed: 1781 additions & 53 deletions
File tree
- crates
- buzz-relay/src
- api
- handlers
- buzz-search
- src
- tests
- search_suite
- docs
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
61 | 62 | | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
143 | 149 | | |
144 | 150 | | |
145 | 151 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
70 | 72 | | |
71 | 73 | | |
72 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1887 | 1887 | | |
1888 | 1888 | | |
1889 | 1889 | | |
1890 | | - | |
1891 | | - | |
1892 | | - | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
1893 | 1894 | | |
1894 | | - | |
| 1895 | + | |
1895 | 1896 | | |
1896 | 1897 | | |
1897 | 1898 | | |
| |||
0 commit comments