Skip to content

fix(dal): lock the operator row while updating - #652

Open
LKSNDRTMLKV wants to merge 1 commit into
mainfrom
fix/619-operator-update-lock
Open

LKSNDRTMLKV wants to merge 1 commit into
mainfrom
fix/619-operator-update-lock

Conversation

@LKSNDRTMLKV

@LKSNDRTMLKV LKSNDRTMLKV commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Closes #619.

OperatorService::update read the operator config, patched it in memory and wrote the whole row back through PgOperatorConfigRepo::upsert, with nothing held in between. When two PATCH /vault/api/v1/operator requests overlapped, the one that read first and wrote last restored whatever the other had changed. Here is an example:

  • request A corrects enterpriseSize;
  • request B, which read before A committed, rebrands and writes the old size back.

enterpriseSize decides whether the ESPR Art. 25(1) destruction ban reaches the operator, so a lost correction changes whether a destruction must cite a derogation. The COALESCE in the upsert did not help: it keeps the stored value only when the incoming one is NULL, and a stale value is not NULL.

What changes

  • OperatorConfigRepository::update(operator_id, &UpdateOperatorConfig) runs the whole update in one transaction:

    1. Insert OperatorConfig::empty with ON CONFLICT DO NOTHING, so the row exists. With no row, FOR UPDATE locks nothing and two first updates would both start from "absent". A second first update now waits on the first one's uncommitted insert.
    2. SELECT … FOR UPDATE reads the row and holds it. A concurrent update waits here, then reads what this one wrote.
    3. UpdateOperatorConfig::apply patches the row it read. It is still the only copy of the patch rule.
    4. The write is upsert's own statement, so the COALESCE on registry_verified_at and enterprise_size, and the refusal of a retention_policy_days beyond i32, apply as before. Both statements share one column list and one bind_row.

    Any error drops the transaction, so a refused first update leaves no row behind.

  • OperatorService::update calls it and no longer calls get or upsert.

  • upsert is documented as a whole-row write for seeding. Its remaining callers are test seeding: dpp-node's smoke and registry_outbox, and dpp-vault's test helpers.

  • No migration and no new dependency.

Tests

crates/dpp-dal/tests/pg_operator_config.rs, against PostgreSQL:

  • two_overlapping_updates_to_different_fields_both_survive. The test holds the row FOR UPDATE, starts a size correction and a rebranding, and waits until PostgreSQL shows both queued behind its lock. It then commits and asserts both fields.
  • two_first_updates_with_no_row_yet_both_survive. The test holds an uncommitted insert of the row, so only that insert can make the two updates wait. It uses brandPrimary and customDomain, because the COALESCE keeps a stored enterpriseSize whatever an update read, which would hide the lost update.
  • a_first_update_creates_the_row_from_the_empty_config, an_update_that_carries_no_size_or_date_keeps_the_stored_ones, a_retention_beyond_the_column_is_refused_and_leaves_no_row_behind and a_retention_beyond_the_column_leaves_the_stored_config_unchanged. Nothing tested the COALESCE rule or the i32 refusal before.

Checked

  • The gate: just check green on this branch (1929 tests passed, 3 skipped, audit included), and pg_operator_config passed 6 of 6.
  • The agent's runs:
    • dpp-dal integration tier: 162 tests passed.
    • dpp-vault operator suites: 18 passed.
    • dpp-node smoke: 40 passed.
    • Clippy with the full feature set: clean.
  • Bite tests:
    • FOR UPDATE removed from the locking read. Both race tests fail with a lost field: the size goes back to Small, and the rebranding is None.
    • The guard insert removed, restoring the old read-or-empty behaviour. The no-row test fails because the rebranding is lost.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed overlapping operator configuration updates so changes to separate fields are preserved.
    • Updates now create a configuration when none exists, while invalid updates leave stored settings unchanged.
  • Documentation
    • Clarified the difference between partial updates and whole-configuration replacement.

@LKSNDRTMLKV LKSNDRTMLKV added this to the v0.17.0 milestone Nov 19, 2026
@LKSNDRTMLKV LKSNDRTMLKV added type/defect Something published or encoded here is wrong or unbackable now urgency/backlog Owed, unscheduled surface/persistence Moves retention, archiving or the ESPR Art. 10(4) back-up copy epic/operations Running a node: persistence correctness, monitoring, install, update labels Nov 19, 2026
@LKSNDRTMLKV

Copy link
Copy Markdown
Member Author

@greptileai

@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Operator updates now hold a database row lock while reading, applying changes, and saving, so overlapping requests keep each other's changes.

  • Overlapping operator edits now keep changes to separate fields.

No actionable issues found.

Reviews (1) · Last reviewed commit: "fix(dal): lock the operator row while up..." · Reviewed by Greptile

@LKSNDRTMLKV

LKSNDRTMLKV commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@LKSNDRTMLKV What would you like me to do on this pull request?

You are interacting with an AI system.

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@LKSNDRTMLKV LKSNDRTMLKV added the review-ready Opt this PR into a CodeRabbit review label Nov 20, 2026
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Operator configuration patches now use a repository update operation. The PostgreSQL implementation applies each patch in a transaction that locks the row and creates it if absent. The service delegates updates to the repository, and integration tests cover concurrent patches and validation behavior.

Changes: Operator configuration updates

Layer / File(s) Summary
Repository update contract
crates/dpp-types/src/operator.rs
The repository contract distinguishes whole-row upsert seeding from atomic partial update operations. It specifies row creation, concurrent patch behavior, and error handling.
Transactional PostgreSQL update
crates/dpp-dal/src/pg/repo_operator_config.rs, CHANGELOG.md
The repository creates a missing row, locks the configuration with FOR UPDATE, applies the patch, writes the result, and commits. Shared SQL and binding helpers serve both write paths.
Service delegation and integration tests
crates/dpp-vault/src/domain/operator_service.rs, crates/dpp-dal/tests/pg_operator_config.rs
The service delegates patches to the repository. PostgreSQL tests cover overlapping updates, first-update row creation, retained fields, and invalid retention values.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant OperatorService
  participant PgOperatorConfigRepo
  participant PostgreSQL
  OperatorService->>PgOperatorConfigRepo: update(operator_id, patch)
  PgOperatorConfigRepo->>PostgreSQL: Begin transaction and insert empty row if absent
  PgOperatorConfigRepo->>PostgreSQL: Select configuration FOR UPDATE
  PgOperatorConfigRepo->>PgOperatorConfigRepo: Apply patch
  PgOperatorConfigRepo->>PostgreSQL: Persist configuration and commit
  PgOperatorConfigRepo-->>OperatorService: Return updated configuration
Loading

Suggested labels: bug

Suggested reviewers: odal-node-maintainer

Merge Risk: Minimal · up to 22124

Overlapping operator-config updates now serialize through a row lock, so changes to different fields no longer overwrite each other. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 6

Check name Status Explanation
Title check Passed The title "fix(dal): lock the operator row while updating" matches the main change: the new transactional update in PgOperatorConfigRepo takes a FOR UPDATE row lock, which prevents overlapping PATCH r…
Description check Passed The description follows the template well. It has a Summary explaining the lost-update bug and fix, a Related issue section (Closes #619), a Changes section with bullets, and a Checklist. Tests are de…
Linked Issues check Passed Issue #619 requires that two overlapping operator updates to different fields both survive, proved by a PostgreSQL interleaving test. Head implements this.

Evidence at head:
- `PgOperatorConfigRepo::…
Out of Scope Changes check Passed The changes in this PR map to #619. Reviewed files and their roles:
- crates/dpp-types/src/operator.rs: adds OperatorConfigRepository::update and documents upsert as seeding. Required by the fix.
…
Publication Boundary Passed The Publication boundary check passes. I reviewed the full pull-request range (5 changed files: CHANGELOG.md, repo_operator_config.rs, pg_operator_config.rs, operator.rs, operator_service.rs) and the …
New Dependency Is Justified Passed The check applies only when the diff adds a new direct dependency to a Cargo.toml. The changed-file inventory for refs/coderabbit/pre-merge-check/base..head lists five files: CHANGELOG.md, crates/dpp-…
Full details: Title check

Explanation

The title "fix(dal): lock the operator row while updating" matches the main change: the new transactional update in PgOperatorConfigRepo takes a FOR UPDATE row lock, which prevents overlapping PATCH requests from overwriting each other. It is specific, short, and names the affected layer (dal). It does not mention the transaction or the lost-update problem, but it covers the primary change.

Full details: Description check

Explanation

The description follows the template well. It has a Summary explaining the lost-update bug and fix, a Related issue section (Closes #619), a Changes section with bullets, and a Checklist. Tests are described in detail, and the gate results are reported. The checklist items are not ticked and the CHANGELOG entry is not confirmed in the description, though the raw summary says CHANGELOG.md was updated. The 'No changelog' line is not needed in that case. Minor gaps: the checklist boxes remain unchecked, and the description does not confirm that docs for public API changes were updated, though the trait doc comments were changed.

Resolution

Optional: tick the checklist items that apply (CHANGELOG entry, tests, just check, debug-check, secrets, docs), and note whether the docs check and migrations check were run, since the template asks for them when relevant.

Full details: Linked Issues check

Explanation

Issue #619 requires that two overlapping operator updates to different fields both survive, proved by a PostgreSQL interleaving test. Head implements this.

Evidence at head:

  • PgOperatorConfigRepo::update (crates/dpp-dal/src/pg/repo_operator_config.rs) runs in one transaction. It runs INSERT ... ON CONFLICT DO NOTHING for OperatorConfig::empty, then SELECT ... FOR UPDATE, then UpdateOperatorConfig::apply, then the UPSERT_SQL write, then commit. Any error drops the transaction.
  • OperatorService::update (crates/dpp-vault/src/domain/operator_service.rs) delegates to repo.update and no longer calls get or upsert. This removes the unlocked read-modify-write named in #619.
  • The COALESCE rules and the i32 refusal are kept, through the shared bind_row helper.
  • The upsert doc comment now states it is a whole-row write for seeding.

Tests: the PR describes two_overlapping_updates_to_different_fields_both_survive in crates/dpp-dal/tests/pg_operator_config.rs. It holds a row lock, queues two updates, and asserts both fields after commit. I did not read the test file directly. This conclusion relies on the PR description and the attested summary of that file.

Stack scope: no stack evidence was supplied. The objective is fully covered by this PR alone.

Full details: Out of Scope Changes check

Explanation

The changes in this PR map to #619. Reviewed files and their roles:

  • crates/dpp-types/src/operator.rs: adds OperatorConfigRepository::update and documents upsert as seeding. Required by the fix.
  • crates/dpp-dal/src/pg/repo_operator_config.rs: implements update. It also moves the SQL and binding into shared helpers (insert_row!, UPSERT_SQL, INSERT_IF_ABSENT_SQL, bind_row). This refactor supports the single-write path that update uses, so it is connected to the objective.
  • crates/dpp-vault/src/domain/operator_service.rs: delegates to the repository. Required by the fix.
  • crates/dpp-dal/tests/pg_operator_config.rs: regression and COALESCE/i32 tests. Supporting tests for the objective.
  • CHANGELOG.md: documents the fix.

No unrelated behaviour change was found in the inspected code. The PR adds no migration and no dependency, as stated.

Full details: Publication Boundary

Explanation

The Publication boundary check passes. I reviewed the full pull-request range (5 changed files: CHANGELOG.md, repo_operator_config.rs, pg_operator_config.rs, operator.rs, operator_service.rs) and the PR description. - No ADR reference by number, title, or section. The grep for "ADR", "Architecture", and "§" found nothing in added lines. - No non-public repository name or filesystem path. The only paths are local crate paths and a link to docs/governance/VERSIONING.md, which is an existing unchanged line. - No pricing, quotes, contract terms, rates, vendor lead times, or negotiation status. Hits for "rate" and similar words are code identifiers, such as STANDALONE_OPERATOR_ID and "generated". - No real company or individual in a non-public arrangement. The only company-like name is "Acme GmbH", a generic placeholder in a test fixture (pg_operator_config.rs, legal_name). It does not name a real entity. - The public references are the ESPR Art. 25(1) legal text and GitHub issue and PR numbers (#619, #652). These are public and allowed. The PR description and commit message contain no prohibited content.

Full details: New Dependency Is Justified

Explanation

The check applies only when the diff adds a new direct dependency to a Cargo.toml. The changed-file inventory for refs/coderabbit/pre-merge-check/base..head lists five files: CHANGELOG.md, crates/dpp-dal/src/pg/repo_operator_config.rs, crates/dpp-dal/tests/pg_operator_config.rs (added), crates/dpp-types/src/operator.rs, and crates/dpp-vault/src/domain/operator_service.rs. No Cargo.toml or Cargo.lock file is changed. The PR description also states "No migration and no new dependency," which matches the diff. The check therefore does not apply, and it passes trivially.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between d41f31d and 2212466.

Files selected for processing: 5

  • CHANGELOG.md
  • crates/dpp-dal/src/pg/repo_operator_config.rs
  • crates/dpp-dal/tests/pg_operator_config.rs
  • crates/dpp-types/src/operator.rs
  • crates/dpp-vault/src/domain/operator_service.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

epic/operations Running a node: persistence correctness, monitoring, install, update review-ready Opt this PR into a CodeRabbit review surface/persistence Moves retention, archiving or the ESPR Art. 10(4) back-up copy type/defect Something published or encoded here is wrong or unbackable now urgency/backlog Owed, unscheduled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Two overlapping operator updates can restore a stale field

1 participant