Skip to content

♻️ director-v2: node resources and pricing links become read-only (PR 3/3) - #10310

Draft
sanderegg wants to merge 8 commits into
ITISFoundation:masterfrom
sanderegg:agents/remove-locks-director-v2-tests-fixes
Draft

sanderegg wants to merge 8 commits into
ITISFoundation:masterfrom
sanderegg:agents/remove-locks-director-v2-tests-fixes

Conversation

@sanderegg

@sanderegg sanderegg commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Goal

director-v2 stops writing node resources and pricing links and reads them instead. Third and last PR of the pricing/resources ownership rework (after #9881 and #9950, discussion in #9851).

What do these changes do?

  • upsert_tasks_from_project reads projects_nodes.required_resources and the projects_node_to_pricing_unit links in one outer join on one connection, before it opens its transaction, and the transaction then only takes the projects row lock and writes comp_tasks. The catalog and resource-usage-tracker calls now run outside the transaction, so neither the row lock nor a pooled connection is held across them ([Maintenance]: Follow-up: director-v2 holds project lock/connection while calling other services when updating comp_tasks #9819). The tasks carry the resources and the pricing unit stored when that read ran; a node sized or relinked in between is picked up by the next upsert of the project.
  • director-v2 no longer writes projects_nodes.required_resources nor projects_node_to_pricing_unit. The web-server writes both since ✨ web-server: link pricing units and size nodes at creation/copy (PR 2/3) #9950.
  • Only COMPUTATIONAL nodes are priced, and only from the unit linked to them. Dynamic and frontend nodes no longer ask the resource-usage-tracker for a pricing unit. A computational node of a billed product without a link answers 404 (PricingPlanUnitNotFoundError) where director-v2 previously linked the product's default unit itself.
  • EC2 sizing leaves the upsert: _update_project_node_resources_from_hardware_info and its helpers go, along with EC2InstanceTypeNotFoundError (404) and MultiContainerOverrideNotSupportedError (422) and their handlers in POST .../computations. The clusters-keeper is no longer called on this path. The sizing RPC of ✨ director-v2: expose node-resources sizing RPC (PR 1/3) #9881 is unchanged and stays the web-server's way of sizing a node.
  • Catalog service resources are asked only for nodes whose required_resources are empty and cached per service for the duration of one call. Pricing units are cached per plan/unit, so nodes sharing a unit ask the resource-usage-tracker once.

Bonus

test_comp_tasks_upsert_concurrency.py is renamed test_comp_tasks_upsert_project_lock.py. test_upsert_skips_writing_unchanged_required_resources becomes test_upsert_writes_no_projects_nodes_rows, and two tests pin the new lock scope: the projects row is not locked, and no pooled connection is checked out, while the catalog and resource-usage-tracker are called. test_comp_tasks_node_resources_sizing.py (4 tests) goes with the removed sizing helper.

Lock-hold time before and after: not measured.

Context

The characterization tests added in #9851 pinned the old contract. The ones marked flips_in="PR3" flip here: a billed scenario now creates the pricing links up front, and the invalid-EC2-type case becomes a missing-link case. They were the last @pytest.mark.epic uses in the repository.

Related issue/s

completes the pricing/resources ownership rework: PR 1 #9881, PR 2 #9950, epic discussion #9851.
Removes the backend calls from the comp_tasks upsert transaction: #9819.

How to test

cd services/director-v2
pytest tests/unit/with_dbs/test_comp_tasks_upsert_project_lock.py \
       tests/unit/with_dbs/comp_scheduler/test_api_route_computations.py -q

Both files need the postgres, redis and registry services that packages/pytest-simcore starts. They were collected but not run: no Docker daemon available here.

Follow-up

  • The epic marker registered in services/director-v2/setup.cfg and services/web/server/setup.cfg has no users left. Remove it, or keep it for the next epic.

Dev-ops

No ENV or configuration changes, no schema changes. This PR assumes #9950 is deployed: a billed product whose web-server does not link nodes answers 404 on a computation start instead of director-v2 linking the default unit.

AI assistance

  • 🤖 This PR was AI generated

Assisted-by: GitHub Copilot:Qwen3.8-Flash Next

sanderegg and others added 2 commits December 3, 2026 10:42
…writing them

`upsert_tasks_from_project` now fetches `required_resources` and the pricing links
before it opens the transaction. The transaction then only takes `FOR KEY SHARE` on
the `projects` row and writes `comp_tasks`, so neither a row lock nor a pooled
connection is held while the catalog and resource-usage-tracker calls run.

- director-v2 no longer writes `projects_nodes.required_resources` nor
  `projects_node_to_pricing_unit`; the web server owns both since PR 2 of the
  pricing/resources ownership rework (sanderegg/9851)
- pricing comes from the linked unit only; a billed computational node without a link
  raises `PricingPlanUnitNotFoundError`, which is a 404
- catalog service resources and RUT pricing units are cached per upsert call
- dropped the EC2 sizing of nodes from the upsert:
  `_update_project_node_resources_from_hardware_info`, `EC2InstanceTypeNotFoundError`,
  `MultiContainerOverrideNotSupportedError` and their handlers, and the
  clusters-keeper calls
- tests: the PR 3 characterization tests now pin the new contract (billed scenarios
  create the pricing links up front), `test_comp_tasks_node_resources_sizing.py` goes
  with the removed helper, and `test_comp_tasks_upsert_project_lock.py` pins the
  duration and scope of the `projects` row lock

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`list_required_resources` and `list_pricing_unit_ids` become a single
`list_required_resources_and_pricing_unit_ids`: one outer join, one connection and one
pool checkout instead of two of each.

- `_get_pricing_and_hardware_infos` → `_get_computational_pricing_and_hardware_infos`,
  which is what the early return already restricted it to
- the projects-row comment now names the mode the statement actually renders
  (`with_for_update(key_share=True)` is `FOR NO KEY UPDATE`, not `FOR KEY SHARE`, and
  that is the lock `lock_project_for_write` takes)
- dropped the comments on the backend-first reads, on the frontend/dynamic early return,
  on the missing pricing link, and the history paragraph of the upsert docstring

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the a:director-v2 issue related with the director-v2 service label Dec 3, 2026
@github-actions github-actions Bot added this to the Run Rudolph run milestone Dec 3, 2026
@sanderegg sanderegg self-assigned this Dec 3, 2026
@sanderegg
sanderegg requested a balanced review from Copilot December 3, 2026 13:07

Copilot AI 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.

🟡 Changes recommended

Critical issues remain: billed project creation through /view can fail with a 404 after insertion, and concurrent node deletion can leave an orphan computational task.

2 open issues
What changed in this PR

Completes the pricing/resources ownership rework: web-server owns node resources and pricing links, while director-v2 reads them when preparing computational tasks.

Changes:

  • Moves backend calls outside the task-write transaction.
  • Prices only computational nodes from existing links and removes computation-start sizing.
  • Updates ownership, missing-link, caching, and lock-scope tests.
File Description
services/​director-v2/​tests/​unit/​with_dbs/​test_comp_tasks_upsert_project_lock.py Tests read-only ownership and shorter lock scope.
services/​director-v2/​tests/​unit/​with_dbs/​conftest.py Removes sizing fixtures and types wallet IDs.
services/​director-v2/​tests/​unit/​with_dbs/​comp_scheduler/​test_api_route_computations.py Updates billed computation and missing-link expectations.
services/​director-v2/​tests/​unit/​test_comp_tasks_node_resources_sizing.py Removes obsolete sizing tests.
services/​director-v2/​src/​simcore_service_director_v2/​modules/​db/​tables.py Exports the pricing-link table.
services/​director-v2/​src/​simcore_service_director_v2/​modules/​db/​repositories/​projects_nodes.py Reads resources and pricing links together.
services/​director-v2/​src/​simcore_service_director_v2/​modules/​db/​repositories/​comp_tasks/​_utils.py Reads linked pricing and removes resource sizing.
services/​director-v2/​src/​simcore_service_director_v2/​modules/​db/​repositories/​comp_tasks/​_core.py Prepares tasks before opening the write transaction.
services/​director-v2/​src/​simcore_service_director_v2/​core/​errors.py Removes obsolete sizing errors.
services/​director-v2/​src/​simcore_service_director_v2/​api/​routes/​computations.py Removes sizing-related dependencies and error handlers.

Review effort: Balanced

Comment on lines +164 to +168
list_of_comp_tasks_in_project, insufficient_credits = await _utils.generate_tasks_list_from_project(
project=project,
project_nodes=project_nodes,
catalog_client=catalog_client,
published_nodes=published_nodes,
Comment on lines +197 to +201
try:
unit_ids = node_pricing_unit_ids[node_id]
except KeyError as exc:
msg = f"node {node_id} of project {project_id} has no pricing unit linked, cannot price the service {node_key}"
raise PricingPlanUnitNotFoundError(msg=msg) from exc
director-v2's upsert no longer queries `projects_nodes` and
`projects_node_to_pricing_unit` itself. It goes through
`simcore_postgres_database.utils_projects_nodes.ProjectNodesRepo`: `list()` for
`required_resources`, and a new `list_project_node_pricing_unit_ids()` next to the
per-node getter for the links. Both run on one connection, so director-v2's
`tables.py` no longer needs to import `projects_node_to_pricing_unit`.

- the read validates `required_resources` into a `ServiceResourcesDict`, so
  `_generate_task_image` takes the proper type instead of a raw JSON dict
- `_link_non_frontend_nodes` becomes `_link_computational_nodes`: those are the
  nodes director-v2 prices, with the `to_node_class` check the module already uses
- dropped the comment above the catalog `limited_gather`

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added the a:database associated to postgres service and postgres-database package label Dec 3, 2026
sanderegg and others added 5 commits December 3, 2026 15:04
Nothing outside director-v2 reads the pricing links of a whole project, so
`packages/postgres-database` does not gain a method for it. The read goes back to
`ProjectsNodesRepository.list_required_resources_and_pricing_unit_ids` as one
outer join on one connection, and `required_resources` is validated into a
`ServiceResourcesDict` there so `_generate_task_image` still takes the proper type.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`node_resources = node_required_resources` only existed to hold the validated
value. The parameter takes the `ServiceResourcesDict` directly now.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reading `required_resources` and the pricing links before the catalog and resource-usage-tracker
calls left a window in which a web-server write lands after that read: the tasks would then carry a
pricing unit or resources the project no longer has, and the `projects` row lock does not cover
`projects_node_to_pricing_unit`, so holding it does not prevent it. The transaction now reads the
same two mappings again once it holds the lock and the generation is repeated when they moved, at
most three times.

`test_upsert_prices_from_the_link_that_is_stored_when_it_writes` relinks a node while the catalog
call is stalled and asserts the written task carries the new unit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`generate_tasks_list_from_project` already needs the class of the node to decide whether a published
node becomes PUBLISHED, so it passes it to the pricing helper instead of the helper running
`to_node_class` on the key a second time, and reuses it for the task's own `node_class`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The links decide which pricing unit the resource-usage-tracker is asked about, so that read has to
precede the backend calls. Since ITISFoundation#9819 asks the calls to run outside the transaction, and a lock
lives as long as its transaction, the read leaves the lock together with them. Re-checking the state
under the lock and generating again only approximates the guarantee the lock used to give, so it goes
as well: the tasks carry the resources and the pricing unit that were stored when the read ran, and
the next upsert of the project takes the values a node PATCH wrote in between.

`test_upsert_prices_from_the_link_that_is_stored_when_it_writes` pinned the removed guarantee.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

Copilot AI 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.

🟡 Changes recommended

The pricing check rejects ordinary graph synchronization for billed projects with missing pricing links, returning 404 after node changes have already been saved.

3 open issues

Review effort: Balanced

Comment on lines +196 to +200
try:
unit_ids = node_pricing_unit_ids[node_id]
except KeyError as exc:
msg = f"node {node_id} of project {project_id} has no pricing unit linked, cannot price the service {node_key}"
raise PricingPlanUnitNotFoundError(msg=msg) from exc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a:database associated to postgres service and postgres-database package a:director-v2 issue related with the director-v2 service

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants