Skip to content

fix(persistence): bound the searches one chained search may issue - #3321

Open
angela-helios wants to merge 2 commits into
mainfrom
fix/3203-chain-fan-out-budget
Open

angela-helios wants to merge 2 commits into
mainfrom
fix/3203-chain-fan-out-budget

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3203

Behaviour change to review

A chained search is now refused with 422 when its reference hops together would need more than 400 backend searches. For a typed one-hop chain such as Observation?subject:Patient.gender=female that is a terminal match of more than 100,000 resources; until now the only limit was the 300,000-id ceiling of #2949. Multi-hop chains and untyped hops with several candidate parent types share the same 400 and reach it sooner. Bulk export _typeFilter resolution goes through the same resolver and is refused the same way.

The number 400 is a judgement, not a measurement: the issue asks for timings of a 300,000-reference hop on SQLite and PostgreSQL first, and those were not taken. Please confirm or change the value.

Summary

The chain resolver searches each hop's references 250 at a time, once per candidate parent type. #2949 bounded the ids a hop may hold, but not the searches it issues: a 300,000-reference hop is 1,200 searches per parent type, run in sequence by one request.

This adds a search budget per chained parameter in crates/persistence/src/search/chain_resolver.rs:

  • MAX_CHAIN_FAN_OUT = 400 searches, shared by every hop and candidate parent type of one chained parameter.
  • Before a hop is searched, its cost (ceil(references / 250) * parent types) is taken from the budget. A hop that does not fit is refused before any of its searches runs, with the existing TooManyResults error (422), carrying the hop's width and the widest hop the remaining budget allowed.
  • The liveness check of a forward chain that ends in _has (existing_ids) chunks the same way and spends from the same budget.

Not in this change: no deadline is checked between chunk searches, a wide hop is still resolved by several OR searches rather than one statement, the budget is per chained parameter rather than per request, and the _has path (#3201) is untouched.

Tests

New unit tests in chain_resolver.rs:

  • fan_out_budget_counts_one_search_per_chunk_and_type: the cost arithmetic, and that a refused hop takes nothing from the budget.
  • fan_out_default_budget_is_below_what_the_id_ceiling_allows: the default admits 100,000 references through one type and refuses 300,000.
  • a_hop_needing_more_searches_than_the_fan_out_ceiling_is_refused: 600 references (three chunks) on SQLite are refused with a ceiling of 2 and resolve with 3.
  • the_fan_out_ceiling_is_shared_by_the_hops_of_a_chain: a two-hop chain needing 2 + 2 searches is refused with a ceiling of 3 and resolves with 4.

Results:

  • cargo test -p helios-persistence --lib: 2087 passed.
  • I did not measure the cost of a chained search on a large store; the limit of 400 chunk searches is a judgement, as the summary says.
  • I did not run clippy locally; CI runs it.

These ran on a branch that also carried thirteen other pending fixes, so the suites were verified in combination, not on this branch alone. That branch also compiled with the postgres, mongodb, s3 and elasticsearch features.

The chain resolver searches a hop's references 250 at a time, once per
candidate parent type, so a hop at the 300,000-id ceiling ran 1,200
searches per type. Each chained parameter now has a budget of 400 such
searches across all its hops and parent types; a hop that does not fit
is refused with 422 before any of its searches runs. The liveness check
of a forward chain ending in _has spends from the same budget.

Closes #3203

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

search: one chained search can issue thousands of backend searches (1,200 per parent type for a 300,000-reference hop)

2 participants