Repository navigation
fix(executions): narrow listings to the requested createdAt window before aggregating - #13982
Conversation
…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.
🟢 Tier S · Ready to merge
📂 Walkthrough · 3
Reviewed |
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
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.
#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]>
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$createdAtrange was applied after it, so listing one hour of a busy function still grouped the function's entire execution history,documentincluded, 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
$createdAtrange (greaterThan[Equal],lessThan[Equal],between) also narrows the aggregation to the executions that have at least one version inside the window:Why not filter
source.createdAtdirectly?createdAtis not constant across an execution's versions. An async execution is written by the API with$createdAtset, and its later versions come from the functions worker, which builds a freshDocumentwithout$createdAt, soStore::snapshot()stamps them with the current time. Filtering versions by the window could drop the latest version, return an older one (a stalewaitingstatus), 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
FunctionsCustomServerTest::testListExecutionsWithinCreatedAtWindowlists a sync and an async execution through$createdAtwindows via the real API. A window around the async execution's queued$createdAtmust return the finished execution or nothing, never the stale queued version.vendor/bin/phpunit tests/unit/Execution/StoreTest.phppasses (20 tests). It checks with plain substring assertions that a window adds the id pre-filter and never filters versions bycreatedAt.pint --testandphpstanare clean.clickhouse/clickhouse-server:26.4.3-alpine, comparing this branch'sStorewithmain's:Correctness: same results and counts across four hourly windows over executions whose versions carry different
createdAtvalues. 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):
mainRelated PRs and Issues
deleteWhere(createdBefore: …)still filters versions bysource.createdAtdirectly, which can tombstone an execution whose latest version is newer than the cutoff. That's a separate follow-up.Checklist