Merge #955: deploy compose reads under Snapshot isolation (no range locks held across ~22 config tables; reproduced 5 s deadlock-victim stalls vs concurrent edits) + sp_getapplock compose serialisation; phase timing from actor receipt

This commit is contained in:
Joseph Doherty
2026-12-01 20:31:54 -05:00
5 changed files with 304 additions and 15 deletions
+60
View File
@@ -0,0 +1,60 @@
# #955 — the ~10 s StartDeployment stall (#923 gate)
Follow-up to #949 (`88c9766ab`), which made the caller-facing symptom honest (202 OutcomeUnknown) but left the cause open.
The leading hypothesis was lock contention around the compose's SERIALIZABLE transaction.
## What the compose's transaction actually did
`ComposeDeploymentAsync` (added in `47960a6d5`) opened a SERIALIZABLE transaction for the `DataConnectionScopeState` row
lock, and `DraftSnapshotFactory.FromConfigDbAsync` reads **inside an existing transaction** rather than opening its own
SNAPSHOT one. So the "one consistent SNAPSHOT read" described in the factory's remarks had silently become a SERIALIZABLE
read of ~22 config tables, whose key-range S locks were held through validate + compose + gzip + insert. Consequences:
- an uncommitted edit of any config row makes the deploy wait for it;
- a running deploy makes every edit of an already-read table wait for the deploy;
- an edit touching a table the deploy has already read **and** one it has not yet read deadlocks with it.
## Reproduction (central SQL Server, throwaway DB, 300 tags; deploys driven through the real actor)
Exploratory probe, 10 deploys per row (5 for the last), same machine, before and after the fix:
| Concurrent writer (own connection, loop) | SERIALIZABLE (before) | SNAPSHOT + app lock (after) |
|---|---|---|
| none | p50 206 ms, max 482 ms | p50 199 ms, max 258 ms |
| AdminUI-style save (UPDATE Tag + INSERT ConfigEdit) | p50 277 ms, max 468 ms | p50 296 ms, max 679 ms |
| cross-table edit (UPDATE Tag, then UPDATE ClusterNode) | **max 5 092 ms; 9 of 10 deploys failed 1205 (deadlock victim) → "StartDeployment failed on the server"**; first deadlock waited one 5 s lock-monitor cycle | p50 383 ms, max 946 ms, 0 deadlocks |
| deploy bookkeeping (UPDATE Deployment + INSERT ConfigAuditLog) | p50 242 ms, max 350 ms | p50 176 ms, max 580 ms |
| edit holding a row lock 2 s | p50 1 991 ms (deploy = the writer's hold) | p50 210 ms (not held) |
So the mechanism is real: a deploy under SERIALIZABLE stalls for as long as any concurrent edit holds a lock, ~5 s per
deadlock-monitor cycle on a deadlock (two cycles ≈ 10 s), or fails outright.
## Fix
The compose transaction is SNAPSHOT. A snapshot read takes no shared locks: it neither waits on nor blocks an edit, and an
edit that commits after the snapshot is simply in the next deployment (the R03:3.2 contract). Composes are still serialised,
by a transaction-owned `sp_getapplock` (`OtOpcUa/DeployCompose`, 20 s) taken **before** any data access — under SNAPSHOT the
version clock starts at the first data access, so a compose queued behind another reads and advances the scope revision that
compose committed; taking the row lock first would fail the second with update conflict 3960.
The #949 phase timer now starts at the singleton's receipt and adds `dispatch` (thread-pool hand-off) and `in-flight-check`
(both were outside the breakdown), and also logs a slow refused or failed attempt.
## Red / green (`DeployComposeIsolationSqlServerTests`, real SQL Server)
| Test | Red |
|---|---|
| `Deploy_is_not_held_by_an_uncommitted_edit_and_ships_the_committed_state` | isolation reverted to SERIALIZABLE: no reply within 10 s |
| `An_edit_of_an_already_read_table_does_not_wait_for_a_running_deploy` | SERIALIZABLE: the mid-read edit hits its 3 s lock timeout |
| `A_compose_queued_behind_another_advances_the_revision_that_compose_committed` | SNAPSHOT without the app lock: Rejected (3960) |
## What is NOT established
The specific 10.5 s stall in the #923 run is not proven to be this mechanism. In that run the only config writer was the
rig's seeder, which runs (`docker compose run --rm`) and exits **before** the deploy is triggered; the in-process writers on
CONTROL (coordinator, retention, audit, the fused node's `NodeDeploymentState` ack) write only the deploy bookkeeping tables
in sub-second `SaveChanges`; and `system_health` showed no deadlock. The run's logs were not retained. Other places the
10 s could have gone, none of which was timed then: the ask's delivery through the singleton proxy (CONTROL-1 ↔ CONTROL-2),
thread-pool queueing before the compose (now timed as `dispatch`), the in-flight check (now timed), and a stall of the SQL or
CONTROL container itself. A repeat now names the phase from receipt onwards; a long gap between the API's request and the
actor's receipt would show as a slow ask with a fast breakdown.
+1 -1
View File
@@ -1789,7 +1789,7 @@ curl -X POST https://<admin-host>/api/deployments \
Responses: `202 Accepted` (`{ outcome, deploymentId, revisionHash }`) when a deployment was sealed, `200` for `NoChanges`, `409` when another deployment is in flight (the body's `deploymentId` names it when it is a compose still running), `422` when rejected, `401` for a missing/wrong key, `503` when unconfigured.
**Outcome unknown (lmxopcua#949).** The trigger waits 10 s for the admin singleton. A slow compose (one gate run took 10.5 s) can outlast that and still create and apply the deployment, so a timeout is **not** reported as a failure: the id is pre-allocated before the ask and the response is `202 Accepted` with `{ "outcome": "OutcomeUnknown", "deploymentId": "<id>", "message": "…" }` and `Location: /api/deployments/<id>`. Poll that resource: `200` means the deployment exists (and carries exactly that id); `404` for a couple of minutes means it was not created and the trigger can be repeated (a repeat while the original compose is still running answers `409` naming the same id). Scripts must therefore check `outcome`, not just the 2xx status. The AdminUI Deployments page shows the same case as "could not be confirmed … may still be created", with the id. The admin singleton logs a Warning with the per-phase timings (`serializable-begin+scope-lock`, `snapshot-read`, `validate`, `compose`, `save+commit`) for any compose slower than 3 s. Set the secret via `Security:DeployApiKey` (env `Security__DeployApiKey`) on admin nodes only; treat it like any deploy credential (rotate, keep out of source).
**Outcome unknown (lmxopcua#949).** The trigger waits 10 s for the admin singleton. A slow compose (one gate run took 10.5 s) can outlast that and still create and apply the deployment, so a timeout is **not** reported as a failure: the id is pre-allocated before the ask and the response is `202 Accepted` with `{ "outcome": "OutcomeUnknown", "deploymentId": "<id>", "message": "…" }` and `Location: /api/deployments/<id>`. Poll that resource: `200` means the deployment exists (and carries exactly that id); `404` for a couple of minutes means it was not created and the trigger can be repeated (a repeat while the original compose is still running answers `409` naming the same id). Scripts must therefore check `outcome`, not just the 2xx status. The AdminUI Deployments page shows the same case as "could not be confirmed … may still be created", with the id. The admin singleton logs a Warning with the per-phase timings (`dispatch`, `in-flight-check`, `snapshot-begin+compose-lock`, `snapshot-read`, `validate`, `compose`, `save+commit`), timed from the singleton's receipt of the request, for any deploy slower than 3 s. Since lmxopcua#955 the compose reads under SNAPSHOT isolation (serialised against other composes by an app lock), not SERIALIZABLE: a concurrent AdminUI edit neither waits for a running deploy nor holds it, and the two can no longer deadlock (under SERIALIZABLE a cross-table edit made the deploy wait ~5 s per deadlock-monitor cycle or fail with "StartDeployment failed on the server"). An edit that commits after the deploy's snapshot is simply in the next deployment. Set the secret via `Security:DeployApiKey` (env `Security__DeployApiKey`) on admin nodes only; treat it like any deploy credential (rotate, keep out of source).
---