Skip to content

fix(functions): recalculate cpu when memory changes without explicit cpu - #11526

Open
anjalimishra-0007 wants to merge 1 commit into
firebase:mainfrom
anjalimishra-0007:fix/functions-v2-cpu-memory-recalc
Open

anjalimishra-0007 wants to merge 1 commit into
firebase:mainfrom
anjalimishra-0007:fix/functions-v2-cpu-memory-recalc

Conversation

@anjalimishra-0007

Copy link
Copy Markdown

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 cpu parameter configured, inferDetailsFromExisting was unconditionally copying haveE.cpu to wantE.cpu. Because wantE.cpu was already populated, resolveCpuAndConcurrency skipped calculating the default CPU requirement for the updated memory limit (e.g. 2 CPU for >=4GiB), causing Cloud Run deployments to fail with:

HTTP Error: 400, Could not update Cloud Run service ... spec.template.spec.containers[0].resources.limits.memory: Invalid value specified for container memory. For 1.0 CPU, memory must be between 128Mi and 4Gi inclusive.

This change checks if wantE.availableMemoryMb was defined and differs from haveE.availableMemoryMb (memoryChanged). Only when memory has not changed (or when CPU is explicitly specified) is haveE.cpu retained. Otherwise, wantE.cpu is resolved based on the new memory amount.

Scenarios Tested

  • Unit tested in src/deploy/functions/prepare.spec.ts:
    • Verified that changing memory from 512MiB to 8GiB on an existing function with 1 CPU recalculates CPU to 2 CPUs.
    • Verified that preserving memory preserves existing CPU.
    • Verified that explicitly setting CPU (e.g. 4 CPUs) when changing memory preserves the explicit CPU setting.
    • All 100 tests in src/deploy/functions/prepare.spec.ts pass.
    • Ran ESLint and Prettier across modified files (0 errors).

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>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 IzaakGough left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@anjalimishra-0007 Thanks for picking this up! The change looks right to me.

Two small requests:

  1. Could you add a CHANGELOG.md entry? 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)
  1. 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-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@d08f41e). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

Raising memory on an existing 2nd gen function keeps the old CPU, so deploys above 4GiB fail

3 participants