Repository navigation
feat: let admins change a service's ID - #262
Conversation
Add `POST /services/{serviceId}/rename` (admins only), which moves a
service, with its verification and `createdAt`, its roles and the names
of its users to a new identifier. The service document moves in a single
write, so it is never registered under both identifiers; the roles move
right after it. Renames lock both identifiers (collection
`service_renames`), as MongoDB transactions need a replica set; renames
interrupted after the service moved are finished, and those before it
rolled back, by the next rename after a minute or on startup.
New conflict codes: `service-id-taken`, `rename-in-progress`.
Closes #243
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changes of a service (PUT, DELETE, granting and revoking roles) could interleave with a rename: a revoked role moved to the new identifier, a deletion left the renamed service without roles, and an admin's PUT of the old identifier took the roles of the renamed service. Renames now lock both identifiers exclusively, and changes hold shared locks (collection `service_locks`); each side waits up to about a second for the other (`409`, code `service-busy`). Interrupted renames are also resumed before requests (at most every 10 s per worker), not only on startup, and failures to resume are logged instead of failing requests. Review findings (adversarial and quality review). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The old and new identifiers were checked only before they were locked, and the roles of the new identifier were then removed unconditionally: a repeated rename, a rename of another service to the same identifier, or a registration in between left the service with the new identifier without roles. Both identifiers are now checked again once locked. Also from the second review round: the Conflict response names service-busy for deletions and roles; more tests for changes that wait for renames, fail, or meet interrupted renames. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis PR adds an admin-only service ID rename workflow implemented without MongoDB transactions: both identifiers are exclusively locked, existing service mutations use shared locks, the service document is moved before roles and user names are migrated, and stale or interrupted operations are recovered on startup and periodically before requests. The API, conflict codes, documentation, configuration, and extensive race/failure tests are updated accordingly. Sequence diagram for atomic service ID renamesequenceDiagram
actor Admin
participant API
participant Locks as service_locks
participant Services as services
participant Roles as roles_and_user_names
Admin->>API: renameService(serviceId, rename)
API->>API: _check_id(new_id)
API->>Locks: _lock(path, source, target)
Locks-->>API: exclusive tokens for both IDs
API->>Services: service_exists(source) and service_exists(target)
API->>Services: update source ID to target
API->>Roles: _move_roles(path, source, target)
Roles-->>API: roles and names copied, old entries removed
API->>Locks: _unlock(source, target, tokens)
API-->>Admin: 200 new identifier
Sequence diagram for service mutation synchronizationsequenceDiagram
participant Client
participant API
participant Locks as service_locks
participant Service
Client->>API: PUT or DELETE service, or role change
API->>Locks: changing(serviceId)
alt rename lock exists
Locks-->>API: wait up to about one second
Locks-->>API: ServiceBusy
API-->>Client: 409 service-busy
else lock acquired
API->>Service: perform mutation
API->>Locks: release shared lock
API-->>Client: mutation response
end
State diagram for interrupted service rename recoverystateDiagram-v2
[*] --> Locked: _lock
Locked --> Moved: _start
Moved --> Completed: _move_roles
Completed --> [*]: _unlock
Moved --> Interrupted: worker failure
Interrupted --> Resumed: resume_renames
Resumed --> Completed: _finish
Resumed --> RolledBack: _finish
RolledBack --> [*]: unlock
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #262 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 18 20 +2
Lines 1778 1975 +197
==========================================
+ Hits 1778 1975 +197 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="cloud_registry/ga4gh/registry/locks.py" line_range="45" />
<code_context>
+# Age after which locks are stale; renames and changes take a few writes, so
+# they are done by then unless they failed. Keep in sync with "a minute" in
+# `docs/api.md` (Renaming services)
+LOCK_TIMEOUT = timedelta(minutes=1)
+# How long and how often renames and changes wait for each other (in total
+# about a second)
</code_context>
<issue_to_address>
**Live operations lose their locks**
When a legitimate rename or service change runs longer than `LOCK_TIMEOUT`, `lock_exclusive()` and `remove_stale_changes()` treat old shared locks as stale, while `resume_renames()` can claim an active rename using the same unrefreshed timestamp. The original operation then continues without protection and overlaps with another operation, leaving roles, names, or service changes inconsistent.
Refresh lock timestamps while operations are active, or otherwise ensure recovery and lock acquisition cannot treat a live operation as stale.
Also at `cloud_registry/ga4gh/registry/rename.py:145`, `cloud_registry/ga4gh/registry/rename.py:314-328`, `cloud_registry/ga4gh/registry/locks.py:80-83`, `cloud_registry/ga4gh/registry/locks.py:113-121`, `cloud_registry/ga4gh/registry/locks.py:139-140`.
</issue_to_address>
### Comment 2
<location path="cloud_registry/ga4gh/registry/rename.py" line_range="245" />
<code_context>
+ return outcome
+ # As when a service is registered (cf. `_register_service()`); the
+ # new identifier is locked, and isn't registered
+ remove_permissions(target)
+ result = get_collection("services").update_one(
+ {"id": source}, {"$set": {"id": target, UPDATED: format_time(now())}}
</code_context>
<issue_to_address>
**New service loses its owner role**
When a generated-ID registration picks the rename target during the interval between the target check and permission cleanup, `_start()` removes target permissions after checking that the target is unused. If `POST /services` generates that target ID and grants its owner role in between, `remove_permissions(target)` deletes the new service's role, leaving it without an owner.
Make generated-ID registrations coordinate with rename locks or ensure target permissions cannot be removed after a concurrent registration.
</issue_to_address>
### Comment 3
<location path="cloud_registry/ga4gh/registry/rename.py" line_range="136" />
<code_context>
+ if service_exists(new_id):
+ return problem(ServiceIdTaken())
+ path = services_path()
+ tokens = _lock(path, serviceId, new_id)
+ if tokens is None:
+ return problem(ServiceBusy())
</code_context>
<issue_to_address>
**Verification changes bypass rename locks**
When an admin verifies or removes verification while the service is being renamed, `putServiceVerification()` and `deleteServiceVerification()` do not use `changing()`, so a verification request racing the identifier update can still address the old ID and return `404` after the move instead of applying to the renamed service. Callers see a failed verification change despite the service continuing under its new ID.
Wrap verification writes in `changing(serviceId)` so they wait for the rename or receive `service-busy`.
Also at `cloud_registry/ga4gh/registry/server.py:202-207`.
</issue_to_address>
### Comment 4
<location path="tests/test_rename.py" line_range="265" />
<code_context>
+ after = stored_service(app, NEW_ID)
+ assert after is not None
+ assert after["createdAt"] == before["createdAt"]
+ assert after["updatedAt"] >= before["updatedAt"]
+ assert after[VERIFIED] == before[VERIFIED]
+ assert get(client, NEW_ID).get_json()["id"] == NEW_ID
</code_context>
<issue_to_address>
**Unchanged update timestamp passes**
When the rename leaves `updatedAt` at its existing value, `test_rename` compares timestamps with `>=`, so leaving `updatedAt` unchanged still satisfies the assertion and the test passes.
Make the test prove the timestamp was renewed, for example by setting the stored timestamp to a known earlier value before renaming.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 4 findings to address first, and renaming changes the stored service ID and migrates its roles and user names; if the move is wrong, reverting the code will not restore the old ID or any roles already removed. In particular, roles that fail validation are skipped before the old resource is deleted, so restoring lost access may require manual reconstruction.
Blocking findings: cloud_registry/ga4gh/registry/locks.py:45, cloud_registry/ga4gh/registry/rename.py:245, cloud_registry/ga4gh/registry/rename.py:136, tests/test_rename.py:265
The test accepted an unchanged timestamp; it now starts from an earlier one (Sourcery review). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Admins can change a service's ID with
POST /services/{serviceId}/rename(body{"id": "<new ID>"}). The rename is atomic: the old ID is deleted, and the old and new IDs never exist at the same time. The service keeps its roles, its stored user names, its verification andcreatedAt;updatedAtis set.400: the new ID can't be used in URLs or roles.404: the service doesn't exist.409service-id-taken: the new ID is registered.409service-busy: either ID is being renamed or changed.service_locks):PUT/DELETE /services/{serviceId}and granting or revoking roles hold shared locks.409service-busy.Closes #243
Verification
coverage run --source cloud_registry,scripts -m pytest: 1387 passed, 230 skipped; coverage 100 % (rename.py and locks.py checked again after the last test was added).MONGODB_TEST_URI):tests/ga4ghandtests/test_rename.py, 363 passed.pre-commit runon the changed files: passed.test_target_taken_before_lockandtest_rename_deleted_before_lock);Declined review findings
ServiceIdTakenstays inrename.py: it is only returned (not raised), and only by renames._claim,_finish, lock waits) to set up races deterministically; kept.POST /servicesthat generates exactly the new ID while it is being renamed could lose its owner role. Generated IDs are random, so this is not handled. (Sourcery raised the same point.)id, so a request racing a move returns an accurate404instead of a409.Open questions
None.
🤖 Generated with Claude Code