fix(deploy): #955 compose under SNAPSHOT + compose app lock, not SERIALIZABLE; time the deploy from receipt
The deploy compose's SERIALIZABLE transaction range-locked every config table for the whole validate+compose+gzip: a concurrent edit held the deploy (as long as its lock), was held by it, or deadlocked with it (reproduced on SQL Server: ~5 s per deadlock-monitor cycle, the deploy the victim 9 of 10 times). SNAPSHOT reads take no shared locks; composes stay serialised by a transaction-owned sp_getapplock taken before any data access (row-lock-first would fail a queued compose with 3960). The #949 phase timer now starts at receipt (dispatch, in-flight-check). The #923 gate's specific 10.5 s stall is not proven to be this mechanism (see evidence).
This commit is contained in:
@@ -30,7 +30,10 @@ namespace ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
/// validator never saw" unrepresentable. SQL Server requires <c>ALLOW_SNAPSHOT_ISOLATION ON</c>
|
||||
/// (migration <c>EnableSnapshotIsolation</c>); without it the first query fails with error 3952 and
|
||||
/// the deploy is rejected rather than silently read unisolated. A non-relational provider (the
|
||||
/// InMemory test provider) and a context already inside a transaction read without one.
|
||||
/// InMemory test provider) and a context already inside a transaction read without one — the
|
||||
/// deploy compose opens its own SNAPSHOT transaction (it also writes the Deployment row in it). Do not
|
||||
/// call this inside a SERIALIZABLE transaction: the read would range-lock every config table until
|
||||
/// commit and deadlock with concurrent edits (lmxopcua#955).
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class DraftSnapshotFactory
|
||||
|
||||
@@ -262,10 +262,28 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
// Captured here: Context/Sender are not available on the thread-pool thread.
|
||||
var sourceNode = Akka.Cluster.Cluster.Get(Context.System).SelfAddress.Host ?? "unknown";
|
||||
var self = Self;
|
||||
// #955: timed from receipt, not from the compose's first line — the thread-pool hand-off and the
|
||||
// in-flight check were outside the #949 breakdown, so a stall there would have read as "fast compose".
|
||||
var phases = new ComposePhaseTimer();
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var refusal = await CheckInFlightAsync(msg).ConfigureAwait(false);
|
||||
return refusal ?? await ComposeDeploymentAsync(msg, sourceNode).ConfigureAwait(false);
|
||||
phases.Mark("dispatch");
|
||||
try
|
||||
{
|
||||
var refusal = await CheckInFlightAsync(msg).ConfigureAwait(false);
|
||||
phases.Mark("in-flight-check");
|
||||
if (refusal is not null)
|
||||
{
|
||||
phases.LogIfSlow(_log, msg, outcome: "refused-in-flight");
|
||||
return refusal;
|
||||
}
|
||||
return await ComposeDeploymentAsync(msg, sourceNode, phases).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
phases.LogIfSlow(_log, msg, outcome: "failed");
|
||||
throw;
|
||||
}
|
||||
})
|
||||
.PipeTo(self,
|
||||
success: composed => new DeployComposed(msg, replyTo, composed.Result, composed.Dispatch),
|
||||
@@ -391,7 +409,11 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
msg.Request.CorrelationId));
|
||||
}
|
||||
|
||||
/// <summary>Per-phase wall-clock of one deploy compose; logs only when the compose is slow (lmxopcua#949).</summary>
|
||||
/// <summary>
|
||||
/// Per-phase wall-clock of one StartDeployment, from the actor's receipt to the commit; logs only when it is
|
||||
/// slow (lmxopcua#949, widened by #955 to the thread-pool hand-off and the in-flight check). Used by one
|
||||
/// logical flow at a time (sequential awaits), so it needs no locking.
|
||||
/// </summary>
|
||||
private sealed class ComposePhaseTimer
|
||||
{
|
||||
/// <summary>A compose slower than this is logged with its phase breakdown (normal ones take 60-630 ms).</summary>
|
||||
@@ -418,6 +440,15 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transaction-owned exclusive app lock serialising deploy composes (#955). Waits up to 20 s (inside the
|
||||
/// 30 s command timeout), then fails the compose rather than composing concurrently.
|
||||
/// </summary>
|
||||
public const string ComposeAppLockSql =
|
||||
"DECLARE @r int; " +
|
||||
"EXEC @r = sp_getapplock @Resource = N'OtOpcUa/DeployCompose', @LockMode = N'Exclusive', @LockOwner = N'Transaction', @LockTimeout = 20000; " +
|
||||
"IF @r < 0 THROW 51955, N'Timed out waiting for another deployment compose to release the compose lock.', 1;";
|
||||
|
||||
/// <summary>Outcome of the off-actor compose: the reply to send, and the dispatch to issue when Accepted.</summary>
|
||||
private sealed record ComposeOutcome(StartDeploymentResult Result, DispatchDeployment? Dispatch);
|
||||
|
||||
@@ -432,26 +463,33 @@ public sealed class AdminOperationsActor : ReceiveActor
|
||||
/// construction (<see cref="_dbFactory"/>, <see cref="_tagConfigValidationMode"/>, <see cref="_log"/> —
|
||||
/// Akka's logging adapter is thread-safe).
|
||||
/// </summary>
|
||||
private async Task<ComposeOutcome> ComposeDeploymentAsync(StartDeployment msg, string sourceNode)
|
||||
private async Task<ComposeOutcome> ComposeDeploymentAsync(StartDeployment msg, string sourceNode, ComposePhaseTimer phases)
|
||||
{
|
||||
// lmxopcua#949: phase timings, logged only when the compose is slow. The one observed 10.5 s compose
|
||||
// (H4 gate, 2026-11-28) was never explained — the Deployment row's CreatedAtUtc is stamped AFTER the
|
||||
// snapshot read, so the time went into the lock / read / validate / compose phases, not the insert —
|
||||
// and nothing recorded which. A repeat now names the phase.
|
||||
var phases = new ComposePhaseTimer();
|
||||
// lmxopcua#949: phase timings, logged only when the deploy is slow (see ComposePhaseTimer).
|
||||
await using var db = await _dbFactory.CreateDbContextAsync();
|
||||
// The singleton update obtains a write lock BEFORE reading the draft. The revision and
|
||||
// deployment row remain in this same transaction until the sealed artifact is committed.
|
||||
// ONE transaction holds the snapshot read, the scope-revision advance and the Deployment row, committed
|
||||
// together. It is SNAPSHOT, not SERIALIZABLE (#955): a serializable read range-locked every config table
|
||||
// for the whole validate + compose + gzip, so any concurrent writer (an AdminUI save) either waited for
|
||||
// the deploy, made the deploy wait for IT, or deadlocked with it — the deploy then stalled ~5 s per
|
||||
// deadlock-monitor cycle or failed outright (reproduced: DeployComposeIsolationSqlServerTests). A
|
||||
// snapshot read takes no shared locks, so it neither waits on nor blocks an edit; an edit that commits
|
||||
// after the snapshot is simply not in this artifact (R03:3.2), and the next deploy carries it.
|
||||
await using var transaction = db.Database.IsRelational()
|
||||
? await db.Database.BeginTransactionAsync(System.Data.IsolationLevel.Serializable)
|
||||
? await db.Database.BeginTransactionAsync(System.Data.IsolationLevel.Snapshot)
|
||||
: null;
|
||||
DataConnectionScopeState scopeState;
|
||||
if (transaction is not null)
|
||||
{
|
||||
// Composes are serialised by an app lock taken BEFORE any data is touched: under SNAPSHOT the
|
||||
// version clock starts at the first data access, so a compose that queued behind another one
|
||||
// reads (and advances) the scope revision that compose committed. Taking the row lock first instead
|
||||
// would fail the second compose with an update conflict (3960). The singleton actor already runs
|
||||
// one compose at a time; this covers two singletons overlapping across a handover.
|
||||
await db.Database.ExecuteSqlRawAsync(ComposeAppLockSql);
|
||||
var locked = await db.Database.ExecuteSqlRawAsync("UPDATE DataConnectionScopeState SET ScopeVersion = ScopeVersion WHERE Id = 1");
|
||||
if (locked != 1) throw new InvalidOperationException("Authoritative data scope state is unavailable.");
|
||||
scopeState = await db.DataConnectionScopeStates.SingleAsync(x => x.Id == 1);
|
||||
phases.Mark("serializable-begin+scope-lock");
|
||||
phases.Mark("snapshot-begin+compose-lock");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user