Skip to content

around: check the query time limit while filtering candidates - #8

Open
cldellow wants to merge 1 commit into
masterfrom
around-health-check
Open

cldellow wants to merge 1 commit into
masterfrom
around-health-check

Conversation

@cldellow

@cldellow cldellow commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #5.

Change

The expensive around filters (filter_nodes_expensive, filter_ways_expensive, filter_relations_expensive in statements/around.cc) ran to completion before any health check. A slow around therefore enforced the query's [timeout:] only late: it kept a CPU busy for the whole computation, then failed with the timeout error at the next check. It never returned a result past its limit; the cost is wasted CPU and server capacity.

This adds a small Periodic_Health_Check that calls rman.health_check(query) periodically. The query now stops with the usual Query timed out error once its time is up, as other long-running statements already do (e.g. filter_by_tags.h).

  • First commit: a check every 256 candidates (or relation members).
  • Second commit: the check counts work instead of candidates. One unit is about one comparison of a candidate's point or segment with a point or segment of the input, and the check runs after every 64k units. is_inside on a way geometry takes the check and ticks it for each point and segment it tests, so a single long way (also one in a candidate relation) can't run unchecked. Counting candidates wasn't enough: a few long ways, such as power lines, tested against a large input took seconds each, so a query could overrun its limit without reaching 256 candidates.

Two files (around.cc, around.h), about 60 lines. No change to the database format or the query API. Queries that finish within their limits behave exactly as before.

Impact

On the Nova Scotia test database (see #2), with [timeout:10]:

rel(id:5429184,7081003,8322255,9270031,10270951,13448043)->.s0;
.s0 <<->.s2;                  // reaches a national-scale boundary relation
way(around.s2:0)[!"height"];
out count;
Build Behaviour
master Enforces the limit only after around finishes, so it was still running when killed at 120 s. Without a [timeout:] (default 180 s) it was still running after 316 s.
This PR Stops after 14 s with runtime error: Query timed out in "query" at line 1 after 14 seconds.

A query that around can finish shows the shape of the old behaviour: with [timeout:5], master runs for the full 23.5 s and then fails with Query timed out … after 23 seconds; this PR fails the same way after 6.4 s. Same database, warm cache:

[timeout:5][out:csv(::count)]; way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100)->.s1; .s1 out count;

Second commit. Found by running fuzz queries with [timeout:2] and flagging any that overran the limit by more than 5 s (bench/deadline_audit.py, see #2). Against master, 4 of 4,000 queries (2,000 plain, 2,000 history with history,users,complete) overran it, all in around. With the first commit only, 3 of those 4 still overran it by 7–12 s. With both commits, all 4 stop within 1 s of the limit (the limit counts whole seconds). Wall time with [timeout:2]:

Query master first commit both commits
A (plain database) 30.5 s 3.2 s 2.4 s
B (plain database) 21.5 s 7.9 s 2.1 s
C (plain database) 8.6 s 9.1 s 2.9 s
D (history database) 28.7 s 13.9 s 2.1 s

A:

[timeout:2][maxsize:536870912][out:json];
nwr["type"="multipolygon"](46.071015,-60.526860,46.124869,-60.278896)->.s0;
.s0 <<->.s1;
way(around.s0:0)[~"^cro"~"n"]->.s2;
node(way_link.s1:3-)->.s3;
foreach.s0->.it{ .it out ids; (.it; .it >;)->.x; .x out count; }
.s3 out ids;

B:

[timeout:2][maxsize:536870912];
way(45.149100,-61.547864,45.197774,-61.501287)->.s0;
way(around.s0:300)["highway"]->.s1;
node(w.s1)->.s2;
way(around.s1:0)["capacity"!~"^4"]->.s3;
way.s1(45.824970,-60.175020,45.964907,-60.155196)->.s4;
.s0 out count;
.s4 out skel 16;

C:

[timeout:2][maxsize:536870912][out:json];
nwr(poly:'45.022514 -62.084016 45.018789 -62.092687 45.022668 -62.090289 45.018580 -62.117998 45.011347 -62.109236 45.009554 -62.100983')[!"lanes:backward"]->.s0;
way.s0(44.767175,-64.509913,45.015063,-64.090581)->.s1;
way(around.s0:0)["voltage"~"^6"]->.s2;
.s0 out ids 42;
.s1 out ids;
.s2 out count;

D is a fuzz query on the full-history database: a wr(poly:…) selection over a 400-vertex ring near 46.13,-59.87, followed by complete(4) and complete(6) loops, one of which runs way(around:0)["height"]. Its vocabulary comes from licensed data, so its full text isn't given here.

Cost of the extra checks: none measurable. CPU time, 5 alternating runs per build, plain database, output identical: benchmark q06 100 → 100 ms; query C with [timeout:600] 9.55 → 9.24 s; way(around:250,44.679085,-63.511161)->.s0; way(around.s0:100); out count; 25.2 → 24.0 s; rel(45.566265,-62.428420,45.586924,-62.388716)->.s0; node(around.s0:0)["footway"]; out count; 2.27 → 2.30 s. health_check reads the clock and sums cached set sizes.

This matters on CPU-bound production servers: a query that will fail anyway holds a core, and its share of the dispatcher's time budget, until around finishes.

Memory

No new allocations: one counter per around filter call.

Testing

  • Differential fuzzer, seed 2: 3,000 queries against master. 2,996 identical, 4 skipped, 0 differences. The fourth skip is a query that now stops itself with a timeout error instead of running on; the fuzzer counts that as a resource limit, not a difference.
  • Update, re-fuzzed with the corrected fuzzer: this change has since been re-fuzzed as part of the all-changes branch (All changes combined (tracking PR) #12), with the corrected fuzzer and a bigger vocabulary (areas, history, users and complete). That covered 10,000 queries across the plain, area and history databases (6,000 / 2,000 / 2,000), with 0 differences. The per-PR fuzz run above predates a fuzzer fix: about 8% of its queries were invalid on both builds, so it rests on roughly 5,500 queries that ran (see Benchmark queries and differential fuzzer #2).
  • Second commit: 1,000 plain-database fuzz queries against the first commit: 999 identical, 1 skipped, 0 differences. The check doesn't change results, only when a query that runs out of time stops.

Limitations

https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT


Generated by Claude Code

@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 20, 2026 — with Claude
@cldellow cldellow added the human-reviewed Colin has reviewed this. label Nov 21, 2026
@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: High confidence. High benefit (timeouts are enforced).

I think no risk -- I think the pre-existing behaviour was that a query that exceeded its timeout would still ultimately be cancelled before returning results, just not in a timely fashion.

Followup idea: are there other areas in the code that have nested loops that may not enforce deadlines in a timely manner?

@cldellow cldellow added the bug Fixes wrong results (in addition to the kind label) label Nov 21, 2026 — with Claude
@cldellow cldellow added the needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. label Nov 21, 2026

Copy link
Copy Markdown
Owner Author

Follow-up done (audit). I ran 4,000 fuzz queries against master with [timeout:2] (2,000 plain, 2,000 history with history,users,complete) and flagged those that overran the limit by more than 5 s. Only 4 did, and all were in around. With this PR's first commit, 3 of them still overran by 7–12 s: the check counted candidates, and a few long ways against a large input never reached 256. The second commit weights the check by comparisons and ticks inside the per-way loop. All 4 now stop within 1 s of the limit, with no measurable cost and 0 fuzz differences. Details are in the description. The audit tool (bench/deadline_audit.py) goes on bench-tools.


Generated by Claude Code

@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: the followup change looks good.

@cldellow cldellow removed the needs-followup Colin reviewed and wants Claude to iterate further, but didn't have spare agents available. label Nov 21, 2026
cldellow pushed a commit that referenced this pull request Nov 23, 2026
This PR helps attic queries by avoiding unnecessary memory copies.
Passing the end iterator by reference avoids the copy.

On a Nova Scotia full-history database:

`[date:"2018-01-01T00:00:00Z"][out:xml]; rel(42,-67.6,47.5,-58); out
body;`

dropped from 19.0 s to 11.7 s on my computer, with identical output.
@cldellow
cldellow force-pushed the around-health-check branch 2 times, most recently from 7febffa to 8101b72 Compare November 23, 2026 23:15
@cldellow

Copy link
Copy Markdown
Owner Author

Offered upstream in fossgis#12

The expensive around filters (filter_nodes_expensive,
filter_ways_expensive, filter_relations_expensive) ran to completion
before the next health check. A slow around therefore kept a CPU busy
long after the query's [timeout:] had passed, only to fail with the
timeout error once it finished.

A Periodic_Health_Check now calls rman.health_check after every 64k
units of work, so the query stops with the usual timeout error soon
after its time is up. A unit is about one comparison of a candidate's
point or segment with a point or segment of the input: is_inside on a
way geometry now takes the check and ticks it for every point and
segment it tests, also for the ways of candidate relations. Counting
candidates alone isn't enough, because a single long way, such as a
power line, tested against a large input can take seconds.

Queries that finish within their limits return the same results as
before.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
@cldellow
cldellow force-pushed the around-health-check branch from db29404 to bafc862 Compare November 24, 2026 19:57
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
osm-3s_testing/README.regressions describes how to add a test.

The tests:

- regression_ql_nesting_limit (04c045b, "Stop the OverpassQL parser
  looping past the nesting limit"): node(1); inside 1024 levels of
  parentheses. The parser reports that nesting is limited to 1023 levels
  and stops. Without the fix, it reports parse errors without end.

- regression_user_touched_relations (#7): on one relation, way and two
  nodes by uid 1,

    rel(1); nwr._(uid_touched:999999999); out count;
    rel(1); nwr._(uid_touched:1); out count;

  count 0 and 1 relations. Without the fix, the first counts 1 relation.

- regression_cgi_custom_url_line_break (#4): a GET request for
  data=[out:custom];node(1);out; with url=http://example.com/%0d%0aX-Injected:%201
  is rejected with 400 Bad Request. Without the fix, the response is a
  302 redirect with an extra header line "X-Injected: 1".

- regression_cgi_bbox_not_numeric (#4): a GET request for
  data=node(bbox);out; with bbox=7,51,8);node(id:1,52 fails to parse.
  Without the fix, the value is substituted as is, the query becomes
  node(51,7,52,8);node(id:1);out; and returns node 1.

Reverting one fix on this branch makes its tests fail and no others:

  git revert -n 04c045b         # regression_ql_nesting_limit
  git revert -n 4a9c1b5         # regression_user_touched_relations
  git revert -n -m 1 8edf11b    # the two regression_cgi_* tests

Not covered: the parts of #4 that leave the output of these requests
unchanged (reading the POST body in chunks, a "%" at the end of the
input, the scope of the "=" search, the operates_* checks in
pt_diagrams), the cgi-bin shell scripts of #3, and #8 and #11, which
change only performance.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
osm-3s_testing/README.regressions describes how to add a test.

The following commits add tests for the fixes since v0.7.62.11. They
leave out the parts of #4 that leave the output of a request unchanged
(reading the POST body in chunks, a "%" at the end of the input, the
scope of the "=" search, the operates_* checks in pt_diagrams), the
cgi-bin shell scripts of #3, and #8 and #11, which change only
performance.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
osm-3s_testing/README.regressions describes how to add a test.

The following commits add tests for the fixes since v0.7.62.11. They
leave out the parts of #4 that leave the output of a request unchanged
(reading the POST body in chunks, the scope of the "=" search, the
operates_* checks in pt_diagrams), the cgi-bin shell scripts of #3,
and the performance changes of #8 and #11.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
The script passes shellcheck.

The following commits add tests for the fixes since v0.7.62.11. They
leave out the parts of #4 that leave the output of a request unchanged
(reading the POST body in chunks, the scope of the "=" search, the
operates_* checks in pt_diagrams), the cgi-bin shell scripts of #3,
and the performance changes of #8 and #11.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
The script passes shellcheck.

The following commits add tests for the fixes since v0.7.62.11. They
leave out the parts of #4 that leave the output of a request unchanged
(reading the POST body in chunks, the scope of the "=" search, the
operates_* checks in pt_diagrams), the cgi-bin shell scripts of #3,
and the performance changes of #8 and #11.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
cldellow added a commit that referenced this pull request Nov 27, 2026
Add run_testsuite_regressions.sh, which runs every test in
osm-3s_testing/input/regression_*/ and compares its output with
expected/regression_*/, and call it from run_testsuite.sh. Each test is
one directory, so a fix can add its test without changing a script:

  stdin.log    the query (for a CGI test, the request body, if any)
  init.osm     optional: data imported into a database of the test's own
  import.args  optional: arguments for update_database (default --meta)
  env          optional: run cgi-bin/interpreter with these environment
               variables instead of osm3s_query, with a dispatcher over
               the test's database

Each test runs for at most 60 seconds and writes at most 10 MB per file,
so a test of a hang fails rather than blocks the suite. The database is
created with mktemp, because the dispatcher's socket path in a directory
under osm-3s_testing/run can exceed the length limit for socket paths.
The script passes shellcheck.

The following commits add tests for the fixes since v0.7.62.11. They
leave out the parts of #4 that leave the output of a request unchanged
(reading the POST body in chunks, the scope of the "=" search, the
operates_* checks in pt_diagrams), the cgi-bin shell scripts of #3,
and the performance changes of #8 and #11.

Co-Authored-By: Colin Dellow <cldellow@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes wrong results (in addition to the kind label) human-reviewed Colin has reviewed this. query-perf Query performance change; collected on all-changes upstreamed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

around enforces the query timeout only after it finishes

2 participants