Skip to content

fix(executions): narrow listings to the requested createdAt window before aggregating - #13982

Merged
levivannoort merged 5 commits into
mainfrom
fix/execution-store-createdat-pushdown
Sep 29, 2026
Merged

levivannoort merged 5 commits into
mainfrom
fix/execution-store-createdat-pushdown

Conversation

@levivannoort

@levivannoort levivannoort commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Execution reads resolve the latest version of each execution with argMax(…, version) grouped by (projectId, id). Before this change only the resource filters (resourceInternalId, resourceType) ran ahead of that grouping. A $createdAt range was applied after it, so listing one hour of a busy function still grouped the function's entire execution history, document included, into a hash table, and then threw away everything outside the hour.

In production, a client listing one function's executions hour by hour, in parallel, pushed a regional ClickHouse node to its server-wide memory cap. Each query needed 5–6 GiB. Every other query on the node failed with them, billing aggregation included.

Now a $createdAt range (greaterThan[Equal], lessThan[Equal], between) also narrows the aggregation to the executions that have at least one version inside the window:

… FROM executions AS source
WHERE source.projectId = … AND source.resourceInternalId IN (…) AND source.resourceType IN (…)
  AND source.id IN (SELECT id FROM executions WHERE projectId = … AND resourceInternalId IN (…) AND resourceType IN (…) AND createdAt >= … AND createdAt < …)
GROUP BY source.projectId, source.id

Why not filter source.createdAt directly? createdAt is not constant across an execution's versions. An async execution is written by the API with $createdAt set, and its later versions come from the functions worker, which builds a fresh Document without $createdAt, so Store::snapshot() stamps them with the current time. Filtering versions by the window could drop the latest version, return an older one (a stale waiting status), or bring back a deleted execution. Selecting ids instead is exact: an execution whose latest version is in the window has at least one version in the window. The outer filter still decides on the latest snapshot, as before.

Test Plan

  • E2E: FunctionsCustomServerTest::testListExecutionsWithinCreatedAtWindow lists a sync and an async execution through $createdAt windows via the real API. A window around the async execution's queued $createdAt must return the finished execution or nothing, never the stale queued version.
  • vendor/bin/phpunit tests/unit/Execution/StoreTest.php passes (20 tests). It checks with plain substring assertions that a window adds the id pre-filter and never filters versions by createdAt.
  • pint --test and phpstan are clean.
  • Ran against a local clickhouse/clickhouse-server:26.4.3-alpine, comparing this branch's Store with main's:
    • Correctness: same results and counts across four hourly windows over executions whose versions carry different createdAt values. The cases were: queued at 10:00 and completed with createdAt 12:30, synchronous, deleted by a tombstone with a later createdAt, and straddling a window boundary.

    • Memory: one function with 1M executions of about 2 KB each, listing one hour (100 rows):

      peak memory duration rows returned
      main 3.21 GiB 0.34 s 100
      this PR 27 MiB 0.02 s 100

Related PRs and Issues

  • Closes appwrite-labs/clickhouse#21
  • Not changed here: deleteWhere(createdBefore: …) still filters versions by source.createdAt directly, which can tombstone an execution whose latest version is newer than the cutoff. That's a separate follow-up.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (n/a)

…fore aggregating

Execution reads resolve the latest version of each execution with argMax,
grouped by id. Only the resource filters ran before that grouping, so a
listing for one hour of a busy function still grouped the function's whole
history, document included, before discarding everything outside the hour.
A burst of such listings exhausted a regional ClickHouse's memory and failed
every other query on the node with it.

A $createdAt range now also selects, before aggregation, the executions that
have at least one version inside it. Versions themselves are not filtered by
it: executions queued through the API get a later createdAt on the versions
the functions worker writes, so filtering versions could leave an older one
as the latest and return a stale status or a deleted execution.
@levivannoort levivannoort self-assigned this Sep 29, 2026
@ghost

ghost commented Sep 29, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The generated SQL is correct: parameter keys stay unique, the subquery uses unqualified columns, and the outer filter still decides on the latest snapshot. I found no defects.

latestWhere in the execution Store now turns $createdAt range queries (<, <=, >, >=, between) into an id IN (SELECT id …) pre-filter, so the argMax grouping only covers executions that have a version in the window. Versions are not filtered directly, so the latest snapshot is still chosen correctly. The PR adds a unit test for the generated SQL and an e2e test for $createdAt windows.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Execution/Store.php latestWhere collects $createdAt range filters into an id IN (SELECT id …) subquery, scoped by the same project and resource filters, ahead of the argMax grouping.
tests/unit/Execution/StoreTest.php Adds a unit test that a window adds the id pre-filter and never filters source.createdAt. It also asserts that a query without a window has no subquery.
tests/e2e/Services/Functions/FunctionsCustomServerTest.php Adds an e2e test that lists sync and async executions through $createdAt windows and checks that a stale queued version is never returned.

Reviewed cebf10c · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment thread tests/unit/Execution/StoreTest.php Resolved
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Narrows execution listing queries to a time window.

The PR appears safe to merge based on this review; no new actionable issue or outstanding previous finding was established.

Summary

The PR narrows execution aggregation to IDs with a version in the requested $createdAt window while retaining latest-version filtering. The latest revision normalizes timestamps in the end-to-end test and removes an unnecessary diagnostic API request.

Reviews (5) · Last reviewed commit: "test(executions): compare createdAt valu..."

Comment thread tests/unit/Execution/StoreTest.php Resolved
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/execution-store-createdat-pushdown (after).

Metric Before After Change
🚀 Requests/sec 188.98 181.91 ⚪ -3.7%
⏱️ Latency P50 90.22 ms 94.82 ms 🔴 +5.1%
⏱️ Latency P95 217.85 ms 225.56 ms ⚪ +3.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 94.82 225.56 11,400 181.91 +7.71
Account 189.15 348.81 600 10.04 +10.15
TablesDB 92.13 175.09 6,200 100.25 +8.08
Storage 86.1 186.43 3,000 50.65 -2.56
Functions 136.82 275.45 1,600 27.88 -2.3

Top API waits (after)

API request Max wait (ms)
account.name.update 550.24
functions.create 535.3
account.prefs.update 490.04
tablesdb.rows.update 475.33
functions.delete 468.19

The unit test matched the whole generated SQL with regular expressions, which
AGENTS.md rules out and which could not tell whether the returned executions
are right. Keep it to plain checks that the window adds the id pre-filter and
never filters versions by createdAt, and add an e2e test that lists a sync
and an async execution through windows around their createdAt values,
including the async execution's queued createdAt, which must never return
the stale queued version.
…g windows

Sync executions also reach the store through the executions queue. The test read the sync execution's createdAt without checking it was stored yet, so under parallel CI load the covering window could be built from a missing value and see only the async execution.
The async create response returns $createdAt in the database format
(2026-09-29 12:46:25.848) while reads return ISO 8601. Taking min() of the two
as strings sorts the space before the T, so the window started at the later
queued time and left out the sync execution. Normalise every timestamp to ISO
first, and drop the diagnostic message that found this.
@levivannoort
levivannoort merged commit d3cd0db into main Sep 29, 2026
52 checks passed
abnegate added a commit that referenced this pull request Sep 29, 2026
#11649 conflicted with main again after main took 14
more commits (to d3cd0db): #13982's createdAt window pushdown for
execution listings and #13979's S3 retry fixes in packages/storage.
Main absorbed no further packages, and composer.json and composer.lock
merge unchanged.

Only src/Appwrite/Execution/Store.php conflicted: main added the
createdAt window to latestWhere() beside the equality filter the train
had moved to the query library's Method enum. The resolution keeps
main's window logic with the train's QueryMethod::Equal, and main's new
WINDOW_METHODS list names the same five methods as Method cases, since
database 8 has no Query::TYPE_* constants and getMethod() returns the
enum. filterSql() already renders those cases, and main's new
StoreTest cases pass against the merged file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant