Repository navigation
fix(functions): recalculate cpu when memory changes without explicit cpu - #11526
anjalimishra-0007 wants to merge 1 commit into
Conversation
When a 2nd gen function has its memory increased (e.g. from 512MiB to 8GiB) without an explicit CPU configured, inferDetailsFromExisting was unconditionally copying haveE.cpu to wantE.cpu. This prevented resolveCpuAndConcurrency from recalculating the minimum required CPU for the updated memory limit (such as 2 CPUs for >=4GiB), leading to Cloud Run deployment errors. This change ensures haveE.cpu is only preserved when wantE.availableMemoryMb has not changed or when CPU was explicitly provided. Fixes firebase#11492 Signed-off-by: Anjali Mishra <246823347+anjalimishra-0007@users.noreply.github.com>
There was a problem hiding this comment.
Code Review
This pull request updates the Firebase Functions deployment preparation logic to ensure that CPU requirements are recalculated when a function's memory configuration changes without an explicitly specified CPU. It introduces a memoryChanged check in src/deploy/functions/prepare.ts to prevent copying the existing CPU configuration if the memory has changed, and adds corresponding unit tests in src/deploy/functions/prepare.spec.ts to verify this behavior. There are no review comments, so no additional feedback is provided.
IzaakGough
left a comment
There was a problem hiding this comment.
@anjalimishra-0007 Thanks for picking this up! The change looks right to me.
Two small requests:
- Could you add a
CHANGELOG.mdentry? Something like:
- Fixed CPU not being recalculated when an existing 2nd gen function's memory changes without an explicit `cpu`, which caused deploys above 4GiB to fail. (#11492)
- It would also be good to have tests for two more cases:
- Memory not set (copied from the existing function), with a CPU that differs from the memory default, for example 512MiB and 4 CPU set in the console. CPU should stay at 4. Most users hit this path, and the current test only covers memory set explicitly to the same value.
- 512MiB raised to 4GiB, which should give 2 CPU. Here the deploy succeeds, but the function silently gets the wrong CPU.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #11526 +/- ##
=======================================
Coverage ? 60.76%
=======================================
Files ? 864
Lines ? 50647
Branches ? 10183
=======================================
Hits ? 30776
Misses ? 18777
Partials ? 1094 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Description
Fixes #11492.
When an existing 2nd gen function is deployed with an increased memory allocation (e.g. from 512MiB to 8GiB) without an explicit
cpuparameter configured,inferDetailsFromExistingwas unconditionally copyinghaveE.cputowantE.cpu. BecausewantE.cpuwas already populated,resolveCpuAndConcurrencyskipped calculating the default CPU requirement for the updated memory limit (e.g. 2 CPU for >=4GiB), causing Cloud Run deployments to fail with:This change checks if
wantE.availableMemoryMbwas defined and differs fromhaveE.availableMemoryMb(memoryChanged). Only when memory has not changed (or when CPU is explicitly specified) ishaveE.cpuretained. Otherwise,wantE.cpuis resolved based on the new memory amount.Scenarios Tested
src/deploy/functions/prepare.spec.ts:src/deploy/functions/prepare.spec.tspass.