feat(deploy): batch instance staleness over one flatten session

DeploymentService.GetInstanceStalenessAsync shares a FlattenSession across
the batch, reads only the (InstanceId, RevisionHash) projection and pays the
snapshot read + I-1 List normalisation only for a mismatch, so the fleet
aggregate agrees exactly with the per-instance Topology badge. The I-1 logic
is extracted from GetDeploymentComparisonAsync unchanged.

Claude-Session: https://claude.ai/code/session_01Y5ef34iUUMjmwnMpL9xgmB
This commit is contained in:
Joseph Doherty
2026-09-14 13:40:19 -04:00
parent dd202e5dde
commit dc60ba1212
2 changed files with 425 additions and 34 deletions
@@ -1707,40 +1707,11 @@ public class DeploymentService
// A snapshot that cannot be deserialized (corrupt /
// older schema) still yields the frozen-hash staleness result, with a
// null diff.
var deployedRevisionHash = snapshot.RevisionHash;
ConfigurationDiff? diff = null;
try
{
var deployedConfig = JsonSerializer.Deserialize<FlattenedConfiguration>(snapshot.ConfigurationJson);
if (deployedConfig != null)
{
deployedConfig = NormalizeListAttributeValues(deployedConfig);
// Recompute the deployed-side hash from the normalized snapshot so
// an old-form List value is not flagged stale against the native
// re-flatten. For a faithfully-stored scalar-only snapshot this
// reproduces the frozen RevisionHash exactly, so behaviour is
// unchanged outside the List-normalization case.
deployedRevisionHash = _revisionHashService.ComputeHash(deployedConfig);
diff = _diffService.ComputeDiff(
deployedConfig, currentConfig, deployedRevisionHash, currentHash);
}
else
{
_logger.LogWarning(
"Deployed snapshot for instance {InstanceId} deserialized to null; " +
"returning hash-based comparison without a structured diff",
instanceId);
}
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Could not deserialize deployed snapshot for instance {InstanceId}; " +
"returning hash-based comparison without a structured diff",
instanceId);
}
var deployedRevisionHash = ComputeDeployedHash(snapshot, instanceId, out var deployedConfig);
var diff = deployedConfig == null
? null
: _diffService.ComputeDiff(
deployedConfig, currentConfig, deployedRevisionHash, currentHash);
var isStale = deployedRevisionHash != currentHash;
@@ -1755,6 +1726,153 @@ public class DeploymentService
return Result<DeploymentComparisonResult>.Success(result);
}
/// <summary>
/// Derives the deployed-side staleness hash from a stored snapshot, applying
/// the I-1 List normalization: the snapshot's <c>ConfigurationJson</c> is
/// deserialized, its <see cref="DataType.List"/> values round-tripped to native
/// form, and the hash recomputed over the result — so a List attribute deployed
/// in the old quoted form is not flagged stale against a native re-flatten. For
/// a faithfully-stored scalar-only snapshot this reproduces the frozen
/// <see cref="DeployedConfigSnapshot.RevisionHash"/> exactly.
///
/// <para>
/// A snapshot that cannot be deserialized (corrupt / older schema) falls back to
/// the frozen hash and yields a <see langword="null"/> normalized config, which
/// the comparison path renders as "no structured diff". Shared by
/// <see cref="GetDeploymentComparisonAsync"/> and
/// <see cref="GetInstanceStalenessAsync"/> so the per-instance badge and the
/// fleet aggregate can never disagree.
/// </para>
/// </summary>
/// <param name="snapshot">The stored deployed-config snapshot.</param>
/// <param name="instanceId">Instance the snapshot belongs to; named in the warning logs.</param>
/// <param name="normalizedDeployedConfig">
/// The normalized deployed configuration, or <see langword="null"/> when the
/// snapshot could not be deserialized.
/// </param>
/// <returns>The hash to compare the current re-flatten's hash against.</returns>
private string ComputeDeployedHash(
DeployedConfigSnapshot snapshot,
int instanceId,
out FlattenedConfiguration? normalizedDeployedConfig)
{
normalizedDeployedConfig = null;
try
{
var deployedConfig = JsonSerializer.Deserialize<FlattenedConfiguration>(snapshot.ConfigurationJson);
if (deployedConfig != null)
{
normalizedDeployedConfig = NormalizeListAttributeValues(deployedConfig);
return _revisionHashService.ComputeHash(normalizedDeployedConfig);
}
_logger.LogWarning(
"Deployed snapshot for instance {InstanceId} deserialized to null; " +
"returning hash-based comparison without a structured diff",
instanceId);
}
catch (JsonException ex)
{
_logger.LogWarning(ex,
"Could not deserialize deployed snapshot for instance {InstanceId}; " +
"returning hash-based comparison without a structured diff",
instanceId);
}
return snapshot.RevisionHash;
}
/// <summary>
/// Evaluates staleness for a batch of instances — the fleet-wide read behind
/// the pending-updates aggregate — agreeing exactly with the per-instance
/// Topology badge (<see cref="GetDeploymentComparisonAsync"/>) while costing a
/// fraction of running that method N times.
///
/// <para>
/// <b>How it stays cheap.</b> All the flattens share ONE
/// <see cref="FlattenSession"/>, so a template chain used by N instances is
/// walked once (the same trick <see cref="DeploySiteAsync"/> uses). The deployed
/// side is read as the <c>(InstanceId, RevisionHash)</c> projection
/// (<see cref="IDeploymentManagerRepository.GetDeployedRevisionHashesAsync"/>),
/// never the fat <c>ConfigurationJson</c> for the whole fleet. Only when that
/// projected hash DISAGREES with the re-flatten is the snapshot read and the
/// I-1 List normalization applied — which is what makes the aggregate agree with
/// the badge on an old-form List snapshot without paying for it fleet-wide.
/// </para>
///
/// <para>
/// <b>Sequential by necessity.</b> The repository's <c>DbContext</c> and the
/// session are both single-threaded (see <see cref="FlattenSession"/>
/// "Threading"), so the loop must not be parallelised.
/// </para>
///
/// <para>
/// <b>Result shape.</b> An instance with no deployed snapshot is OMITTED — it
/// has nothing to be stale against, and a snapshot that vanished between the
/// projection read and the snapshot read means it was undeployed concurrently,
/// which is treated the same way. <see langword="null"/> means "could not
/// evaluate" (its flatten failed): unknown, never counted as stale.
/// </para>
/// </summary>
/// <param name="instanceIds">Instances to evaluate; duplicates are collapsed.</param>
/// <param name="cancellationToken">Cancellation token, honoured between instances.</param>
/// <returns>
/// A map of instance id → <see langword="true"/> (stale), <see langword="false"/>
/// (up to date) or <see langword="null"/> (could not evaluate), omitting
/// instances with no deployed snapshot.
/// </returns>
public async Task<IReadOnlyDictionary<int, bool?>> GetInstanceStalenessAsync(
IReadOnlyCollection<int> instanceIds,
CancellationToken cancellationToken = default)
{
var staleness = new Dictionary<int, bool?>();
if (instanceIds.Count == 0)
return staleness;
var deployedHashes = await _repository.GetDeployedRevisionHashesAsync(cancellationToken);
var session = _flatteningPipeline.CreateSession();
foreach (var instanceId in instanceIds.Distinct())
{
cancellationToken.ThrowIfCancellationRequested();
if (!deployedHashes.TryGetValue(instanceId, out var deployedHash))
continue;
// Read-only path: hash only, so skip the Roslyn compile — the deploy
// gate remains the authoritative compile.
var currentResult = await _flatteningPipeline.FlattenAndValidateAsync(
instanceId, cancellationToken, validateScripts: false, session);
if (currentResult.IsFailure)
{
_logger.LogWarning(
"Could not evaluate staleness of instance {InstanceId}: {Error}",
instanceId, currentResult.Error);
staleness[instanceId] = null;
continue;
}
var currentHash = currentResult.Value.RevisionHash;
if (string.Equals(currentHash, deployedHash, StringComparison.Ordinal))
{
staleness[instanceId] = false;
continue;
}
// Mismatch only: pay the fat snapshot read + I-1 normalization, so an
// old-form List snapshot reads exactly as the Topology badge renders it.
var snapshot = await _repository.GetDeployedSnapshotByInstanceIdAsync(instanceId, cancellationToken);
if (snapshot == null)
continue;
var normalizedDeployedHash = ComputeDeployedHash(snapshot, instanceId, out _);
staleness[instanceId] =
!string.Equals(normalizedDeployedHash, currentHash, StringComparison.Ordinal);
}
return staleness;
}
/// <summary>
/// I-1 (latent): returns a copy of <paramref name="config"/> whose
/// <see cref="DataType.List"/> attribute values have been round-tripped through