fix(deployment): pending-updates tick uses a slim instance projection, hash-only flatten, one untracked mismatch snapshot read and a per-tick secret lookup memo (review 2026-10-21 04/P2)

This commit is contained in:
Joseph Doherty
2026-10-22 06:39:45 -04:00
parent 971a9886b8
commit df67fabe68
17 changed files with 443 additions and 128 deletions
@@ -1811,12 +1811,15 @@ public class DeploymentService
/// <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
/// walked once (the same trick <see cref="DeploySiteAsync"/> uses), and each one is
/// hash-only (<see cref="IFlatteningPipeline.ComputeRevisionHashAsync"/>) — no
/// validation pass whose result would be discarded. 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
/// projected hash DISAGREES with the re-flatten is the snapshot read — in ONE
/// untracked batch for all such instances — 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>
///
@@ -1862,6 +1865,10 @@ public class DeploymentService
var firstFailureInstanceId = 0;
string? firstFailureError = null;
// Instances whose re-flattened hash disagrees with the projected deployed hash,
// resolved together after the loop with ONE untracked snapshot read.
var mismatched = new Dictionary<int, string>();
foreach (var instanceId in instanceIds.Distinct())
{
cancellationToken.ThrowIfCancellationRequested();
@@ -1871,10 +1878,10 @@ public class DeploymentService
evaluated++;
// 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);
// Read-only path: hash only — no validation (its result was always discarded
// here), so no shared-script/schema loads and no structural/semantic pass.
var currentResult = await _flatteningPipeline.ComputeRevisionHashAsync(
instanceId, session, cancellationToken);
if (currentResult.IsFailure)
{
_logger.LogDebug(
@@ -1891,22 +1898,34 @@ public class DeploymentService
continue;
}
var currentHash = currentResult.Value.RevisionHash;
var currentHash = currentResult.Value;
if (string.Equals(currentHash, deployedHash, StringComparison.Ordinal))
{
staleness[instanceId] = false;
continue;
}
mismatched[instanceId] = currentHash;
}
if (mismatched.Count > 0)
{
// 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;
// One untracked batch read — the snapshots' JSON is hashed and dropped,
// never held by the scoped change tracker.
var snapshots = await _repository.GetDeployedSnapshotsByInstanceIdsAsync(
mismatched.Keys.ToList(), cancellationToken);
foreach (var (instanceId, currentHash) in mismatched)
{
// Vanished between the projection read and now: undeployed concurrently.
if (!snapshots.TryGetValue(instanceId, out var snapshot))
continue;
var normalizedDeployedHash = ComputeDeployedHash(snapshot, instanceId, out _);
staleness[instanceId] =
!string.Equals(normalizedDeployedHash, currentHash, StringComparison.Ordinal);
var normalizedDeployedHash = ComputeDeployedHash(snapshot, instanceId, out _);
staleness[instanceId] =
!string.Equals(normalizedDeployedHash, currentHash, StringComparison.Ordinal);
}
}
if (failedCount > 0)