Repository navigation
Conversation
Walkthrough
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/client/src/runtime/core/engines/client/query-plan-cache.ts`:
- Around line 66-73: Update the existing-entry branch in the batch cache
insertion logic to remove the old entry and then reuse the common
insertion/eviction path, rather than returning after updating
`#batchCacheTotalQueries`. Ensure replacement batches trigger the same capacity
eviction loop as new entries, and add a regression case covering a replacement
that exceeds the plan limit.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: e3eb1f52-a7f1-481c-9953-6471f8762e20
📒 Files selected for processing (2)
packages/client/src/runtime/core/engines/client/query-plan-cache.test.tspackages/client/src/runtime/core/engines/client/query-plan-cache.ts
| if (this.#batchCache.has(key)) { | ||
| // Update existing entry (also moves to end for LRU) | ||
| const oldResponse = this.#batchCache.get(key)! | ||
| this.#batchCacheTotalQueries -= oldResponse.type === 'multi' ? oldResponse.plans.length : 1 | ||
| this.#batchCache.delete(key) | ||
| this.#batchCache.set(key, response) | ||
| this.#batchCacheTotalQueries += queryCount | ||
| return |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Evict after replacing an existing batch.
A larger replacement updates #batchCacheTotalQueries then returns at Line 73 without running the eviction loop. For example, a cache of 4 plans containing batches of 2 and 2 can replace one batch with 4 plans and retain 6 plans. Reuse the common insertion/eviction path after removing the old entry, and add a replacement regression case.
Proposed fix
if (this.#batchCache.has(key)) {
- // Update existing entry (also moves to end for LRU)
const oldResponse = this.#batchCache.get(key)!
this.#batchCacheTotalQueries -= oldResponse.type === 'multi' ? oldResponse.plans.length : 1
this.#batchCache.delete(key)
- this.#batchCache.set(key, response)
- this.#batchCacheTotalQueries += queryCount
- return
}
this.#batchCache.set(key, response)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (this.#batchCache.has(key)) { | |
| // Update existing entry (also moves to end for LRU) | |
| const oldResponse = this.#batchCache.get(key)! | |
| this.#batchCacheTotalQueries -= oldResponse.type === 'multi' ? oldResponse.plans.length : 1 | |
| this.#batchCache.delete(key) | |
| this.#batchCache.set(key, response) | |
| this.#batchCacheTotalQueries += queryCount | |
| return | |
| if (this.#batchCache.has(key)) { | |
| const oldResponse = this.#batchCache.get(key)! | |
| this.#batchCacheTotalQueries -= oldResponse.type === 'multi' ? oldResponse.plans.length : 1 | |
| this.#batchCache.delete(key) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/client/src/runtime/core/engines/client/query-plan-cache.ts` around
lines 66 - 73, Update the existing-entry branch in the batch cache insertion
logic to remove the old entry and then reuse the common insertion/eviction path,
rather than returning after updating `#batchCacheTotalQueries`. Ensure replacement
batches trigger the same capacity eviction loop as new entries, and add a
regression case covering a replacement that exceeds the plan limit.
What
Modifies QueryPlanCache in the client package to limit memory usage based on the total number of cached query plans rather than the number of cached batches.
Updates the QueryPlanCache test suite with additional test cases verifying the new eviction logic.
Why
Unbounded Memory Bloat
The previous implementation of QueryPlanCache stored up to 1000 batch entries in the batch cache. However, each batch was treated as a single entry regardless of how many individual query plans it contained. When executing dynamic transactions with highly variable batch sizes (such as mappings over an array of variable length), the cache keys were unique for each batch size. This resulted in caching hundreds of massive batch responses, accumulating hundreds of thousands of individual query plans. This caused Node heap memory to bloat up to 550MB in long running applications like web crawlers before hitting the 1000 entry limit.
Changes
packages/client
[MODIFY] src/runtime/core/engines/client/queryPlanCache.ts
[MODIFY] src/runtime/core/engines/client/queryPlanCache.test.ts
Testing
All unit tests in the query plan cache test suite pass successfully (15 tests total).
This pr resolves issue : #29774