sample-platform icon indicating copy to clipboard operation
sample-platform copied to clipboard

GET /samples/{id}/history times out (504) on production data

Open gaurav02081 opened this issue 3 months ago • 1 comments

GET /samples/{id}/history times out (504) on production data

Summary

The endpoint applies limit/offset in Python after loading and processing every result for the sample, so the page size has no effect on how much work the request does. On production this takes long enough to hit the gateway timeout.

Reproduction

Against sampleplatform.ccextractor.org, with a runs:read token:

curl -H "Authorization: Bearer $TOKEN" \
  "https://sampleplatform.ccextractor.org/api/v1/samples/3/history?limit=5"
# HTTP 504 after 78.7s

curl -H "Authorization: Bearer $TOKEN" \
  "https://sampleplatform.ccextractor.org/api/v1/samples/7/history?limit=5"
# HTTP 504 after 61.4s

Both asked for 5 entries. Neither returned.

This does not reproduce on a small development database — with ~50 runs the request is instant, which is presumably why it got through review.

Cause

mod_api/routes/samples.py, get_sample_history (line 538):

results = query.order_by(Test.id.desc()).all()          # L585 — every result, all runs
...
all_files = TestResultFile.query.options(               # L589 — files for every one of them
    joinedload(TestResultFile.regression_test_output)
    .joinedload(RegressionTestOutput.multiple_files)
).filter(TestResultFile.test_id.in_(test_ids)).all()
...
unique_tests = Test.query.filter(Test.id.in_(test_ids)).all()   # L602
_, timestamps_map = batch_get_run_data(unique_tests)            # L607
entries = _process_history_entries(...)

total = len(entries)                                    # L617
paged = entries[offset:offset + limit]                  # L618 — the slice happens here

For a sample exercised in most runs, test_ids is in the thousands, and the TestResultFile query fans out further through the two nested joinedloads. All of that runs regardless of limit.

The batching that is there (batch_get_run_data, _preload_expected_outputs) is doing its job — it avoids N+1 — but it batches over the whole history rather than over the page being returned.

Suggested fix

Push the pagination into SQL, so the expensive follow-up queries only ever see one page:

total = query.count()
results = query.order_by(Test.id.desc()).offset(offset).limit(limit).all()

and drop the Python slice. Everything downstream (test_ids, all_files, unique_tests, timestamps_map) then scales with the page, not with history.

One complication: status is currently filtered inside _process_history_entries, after the entries are built, so total would no longer be correct when ?status= is supplied. Options, roughly in order of preference:

  1. Derive status in SQL so it can join the main query — best, but the biggest change, since status depends on result files and expected outputs.
  2. Keep the load-everything path only when status is present, and use the paginated path otherwise. The unfiltered case is the common one and it is the one that currently 504s.
  3. Cap how far back history is read (e.g. most recent N runs) and document it.

Impact

Any API consumer that reads sample history. Concretely, it makes sp investigate --with-history unusable against production: that command does one history lookup per failing sample, so a run with 45 failures cannot complete.

Environment

  • upstream/master @ fd0d5b3
  • Observed 2026-08-05 against the production deployment

gaurav02081 avatar Aug 05 '26 19:08 gaurav02081

One more consequence of paginating after the fact, in case it shapes the fix.

Because the slice happens once entries for all of the sample's regression tests have been built, limit does not mean "N runs" to a caller interested in one test. A page of N covers roughly N / (number of regression tests on that sample) runs of any single test. For a sample with 10 tests, ?limit=20 yields about 2 runs of the test you asked about.

That is not just a performance concern — it changes answers. A test that failed the last two runs but passed eight runs earlier comes back looking as though it has never passed, because the pass is outside the window the caller could not tell was short. sp investigate --with-history was reporting NEVER_PASSED where STILL_FAILING was correct. I have worked around it client-side by always requesting the maximum page and filtering locally, and by lowering the confidence of that verdict when the page comes back saturated, but the window is still whatever a full page happens to cover.

So if pagination does move into SQL, it would help a lot if the filtering moved with it — either by accepting an optional regression_test_id filter, or by paginating per regression test rather than across all of them. Then limit would mean the same thing to the caller as it does to the query.

gaurav02081 avatar Aug 05 '26 20:08 gaurav02081