Repository navigation
fix(databases): chunk batch duplicate lookup by max query values - #15017
Conversation
A batch write over 500 IDs that hit a duplicate asked findOne for every ID at once, so the query validator refused it and the caller got a 500 instead of the already-exists error naming the stored ID.
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
The change makes the bulk-write duplicate lookup split ID lists into chunks sized by the database adapter query-value limit instead of issuing one oversized IN query. The e2e error-message suite gains a case covering a batch past the query-values limit, and .env raises the batch limit so the new test can exercise the code path.
Reviewed commit: 4485cc4 |
🟢 Tier S · Ready to mergeThe duplicate-ID lookup now splits submitted IDs into chunks sized to the database query-value limit and returns the first stored duplicate found. The E2E suite adds coverage for an existing ID beyond the first chunk, and the local test environment raises its batch limit to allow that request size.
📂 Walkthrough · 3
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
There was a problem hiding this comment.
This PR chunks the duplicate-ID lookup for bulk document writes to stay under per-adapter query value limits, with a matching e2e error-message test.
Key findings
- 🟡 E2E limit test needs batch env above 501 — ErrorMessages.php:191
| } | ||
|
|
||
| public function testBulkCreatePastTheQueryValuesLimitNamesTheExistingId(): void | ||
| { |
There was a problem hiding this comment.
🟡 test · medium
E2E limit test needs batch env above 501
testBulkCreatePastTheQueryValuesLimitNamesTheExistingId sends APP_DATABASE_QUERY_MAX_VALUES + 1 (501) records, which only reaches the duplicate check when _APP_LIMIT_DATABASE_BATCH >= 501; with the code default of 100 in app/init/constants.php the request fails ArrayList validation with 400 before the code under test runs.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
What
A batch create (or bulk upsert) that hits a duplicate ID looks up which ID is already stored so the error can name it. That lookup put every ID in the batch into one
Query::equal('$id', ...), and when a batch had more than 500 IDs the query validator refused it. The caller got a 500 (Invalid query: Query on attribute has greater than 500 values: $id) instead of the 409.getBatchDuplicateException()now looks the IDs up in chunks of$dbForDatabases->getMaxQueryValues()and stops at the first chunk that finds a stored ID, so the caller getsrow_already_exists/document_already_existsnaming that ID.Test
testBulkCreatePastTheQueryValuesLimitNamesTheExistingIdbatch-createsAPP_DATABASE_QUERY_MAX_VALUES + 1rows with an existing ID last, so it sits in the second chunk, and expects a 409 naming that ID. It runs for every API that uses theErrorMessagestrait.The test stack's
.envsets_APP_LIMIT_DATABASE_BATCH=1000because the self-hosted default of 100 rejects the batch before it reaches the database. Cloud plans already allow larger batches throughdatabasesBatchSize.Fixes CLOUD-3RBE
Related: appwrite-labs/cloud#5938