fix(deployment): take the operation lock before validating the state transition (DEP-2, WP7 task 7.4)
Deploy/Disable/Enable/Delete now acquire the per-instance lock first, then reload the instance (new IDeploymentManagerRepository.ReloadInstanceAsync) and validate on the fresh state, so a Delete queued behind a deploy no longer takes the NotDeployed shortcut and orphans the running instance.
This commit is contained in:
+10
@@ -414,6 +414,16 @@ public interface IDeploymentManagerRepository
|
||||
/// <returns>The instance, or null if not found.</returns>
|
||||
Task<Instance?> GetInstanceByIdAsync(int instanceId, CancellationToken cancellationToken = default);
|
||||
/// <summary>
|
||||
/// Refreshes <paramref name="instance"/> in place from the database so a caller that
|
||||
/// waited for the per-instance operation lock can validate the state the command ahead
|
||||
/// of it left behind (DEP-2). A tracked entity is reloaded; an untracked one has its
|
||||
/// <see cref="Instance.State"/> re-read.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance previously loaded by <see cref="GetInstanceByIdAsync"/>.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <returns><see langword="true"/> when the instance no longer exists (it was deleted meanwhile); otherwise <see langword="false"/>.</returns>
|
||||
Task<bool> ReloadInstanceAsync(Instance instance, CancellationToken cancellationToken = default);
|
||||
/// <summary>
|
||||
/// Gets an instance by unique name.
|
||||
/// </summary>
|
||||
/// <param name="uniqueName">The unique instance name.</param>
|
||||
|
||||
+23
@@ -778,6 +778,29 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
|
||||
// --- Instance lookups for deployment pipeline ---
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> ReloadInstanceAsync(Instance instance, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entry = _dbContext.Entry(instance);
|
||||
if (entry.State == EntityState.Detached)
|
||||
{
|
||||
// Loaded without tracking (a NoTracking circuit context): there is no entry to
|
||||
// reload, so re-read the one column the transition check depends on.
|
||||
var fresh = await _dbContext.Set<Instance>()
|
||||
.AsNoTracking()
|
||||
.Where(i => i.Id == instance.Id)
|
||||
.Select(i => (InstanceState?)i.State)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
if (fresh is null)
|
||||
return true;
|
||||
instance.State = fresh.Value;
|
||||
return false;
|
||||
}
|
||||
|
||||
// EF detaches the entity when the row has been deleted meanwhile.
|
||||
await entry.ReloadAsync(cancellationToken);
|
||||
return entry.State == EntityState.Detached;
|
||||
}
|
||||
|
||||
public async Task<Instance?> GetInstanceByIdAsync(int instanceId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _dbContext.Set<Instance>()
|
||||
|
||||
@@ -281,17 +281,27 @@ public class DeploymentService
|
||||
if (instance == null)
|
||||
return PreparedDeployment.Resolved(Result<DeploymentRecord>.Failure($"Instance with ID {instanceId} not found."));
|
||||
|
||||
// Validate state transition
|
||||
var transitionError = StateTransitionValidator.ValidateTransition(instance.State, "deploy");
|
||||
if (transitionError != null)
|
||||
return PreparedDeployment.Resolved(Result<DeploymentRecord>.Failure(transitionError));
|
||||
|
||||
// Acquire per-instance operation lock
|
||||
// Acquire the per-instance operation lock BEFORE validating the state transition:
|
||||
// a command that waited behind another one must judge the state that command left
|
||||
// behind, not the copy loaded before the wait (DEP-2).
|
||||
var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
if (await _repository.ReloadInstanceAsync(instance, cancellationToken))
|
||||
{
|
||||
lockHandle.Dispose();
|
||||
return PreparedDeployment.Resolved(Result<DeploymentRecord>.Failure($"Instance with ID {instanceId} not found."));
|
||||
}
|
||||
|
||||
var transitionError = StateTransitionValidator.ValidateTransition(instance.State, "deploy");
|
||||
if (transitionError != null)
|
||||
{
|
||||
lockHandle.Dispose();
|
||||
return PreparedDeployment.Resolved(Result<DeploymentRecord>.Failure(transitionError));
|
||||
}
|
||||
|
||||
return await PrepareUnderLockAsync(
|
||||
instance, user, session, reconcileGate, lockHandle, cancellationToken);
|
||||
}
|
||||
@@ -1626,13 +1636,18 @@ public class DeploymentService
|
||||
if (instance == null)
|
||||
return Result<InstanceLifecycleResponse>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
// Lock first, then validate against the state as it is NOW (DEP-2): a command that
|
||||
// waited behind another one must not act on the copy loaded before the wait.
|
||||
using var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
if (await _repository.ReloadInstanceAsync(instance, cancellationToken))
|
||||
return Result<InstanceLifecycleResponse>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
var transitionError = StateTransitionValidator.ValidateTransition(instance.State, "disable");
|
||||
if (transitionError != null)
|
||||
return Result<InstanceLifecycleResponse>.Failure(transitionError);
|
||||
|
||||
using var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
var commandId = Guid.NewGuid().ToString("N");
|
||||
var siteId = await ResolveSiteIdentifierAsync(instance.SiteId, cancellationToken);
|
||||
var command = new DisableInstanceCommand(commandId, instance.UniqueName, DateTimeOffset.UtcNow);
|
||||
@@ -1696,13 +1711,18 @@ public class DeploymentService
|
||||
if (instance == null)
|
||||
return Result<InstanceLifecycleResponse>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
// Lock first, then validate against the state as it is NOW (DEP-2): a command that
|
||||
// waited behind another one must not act on the copy loaded before the wait.
|
||||
using var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
if (await _repository.ReloadInstanceAsync(instance, cancellationToken))
|
||||
return Result<InstanceLifecycleResponse>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
var transitionError = StateTransitionValidator.ValidateTransition(instance.State, "enable");
|
||||
if (transitionError != null)
|
||||
return Result<InstanceLifecycleResponse>.Failure(transitionError);
|
||||
|
||||
using var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
var commandId = Guid.NewGuid().ToString("N");
|
||||
var siteId = await ResolveSiteIdentifierAsync(instance.SiteId, cancellationToken);
|
||||
var command = new EnableInstanceCommand(commandId, instance.UniqueName, DateTimeOffset.UtcNow);
|
||||
@@ -1765,13 +1785,18 @@ public class DeploymentService
|
||||
if (instance == null)
|
||||
return Result<InstanceLifecycleResponse>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
// Lock first, then validate against the state as it is NOW (DEP-2): a command that
|
||||
// waited behind another one must not act on the copy loaded before the wait.
|
||||
using var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
if (await _repository.ReloadInstanceAsync(instance, cancellationToken))
|
||||
return Result<InstanceLifecycleResponse>.Failure($"Instance with ID {instanceId} not found.");
|
||||
|
||||
var transitionError = StateTransitionValidator.ValidateTransition(instance.State, "delete");
|
||||
if (transitionError != null)
|
||||
return Result<InstanceLifecycleResponse>.Failure(transitionError);
|
||||
|
||||
using var lockHandle = await _lockManager.AcquireAsync(
|
||||
instance.UniqueName, _options.OperationLockTimeout, cancellationToken);
|
||||
|
||||
var commandId = Guid.NewGuid().ToString("N");
|
||||
|
||||
// A NotDeployed instance has no live Instance Actor at any site, so Delete is
|
||||
|
||||
Reference in New Issue
Block a user