Files
ScadaBridge/src/ZB.MOM.WW.ScadaBridge.SiteRuntime/Deployment/DeployCompileValidator.cs
T
Joseph Doherty 999653a6b9 perf(site-runtime): warm the next startup batch during the stagger delay and compile a batch's distinct units in parallel (review 2026-10-21 01/SR-P3)
DeployCompileValidator.WarmCompileCache dedupes script bodies/trigger
expressions across a batch and compiles them with Parallel.ForEach (half the
cores). CreateInstanceActorBatch starts the next batch's warm as it arms the
stagger timer; creation order and delay are unchanged. The warm's parsed
configs are reused at creation.
2026-10-22 09:10:26 -04:00

284 lines
14 KiB
C#

using System.Text.Json;
using Akka.Actor;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Deployment;
/// <summary>
/// Pure, thread-safe deploy-time gate: given a flattened-configuration JSON, it
/// compiles every script, every alarm on-trigger script, and every Expression
/// trigger just as <see cref="Actors.InstanceActor.CreateChildActors"/> does at
/// runtime — but WITHOUT touching any actor state. It returns the collected
/// compile errors so the Deployment Manager can reject a deployment whose scripts
/// do not compile on the site (design spec: site-side compile failure rejects the
/// deployment; no partial state is applied).
///
/// Because it holds no actor state and only calls <see cref="ScriptCompilationService"/>
/// (whose only state is a thread-safe compile cache), it is pure and thread-safe; the
/// Deployment Manager nevertheless invokes it <em>synchronously on the singleton's actor
/// thread</em> as a pure prefix step, preserving mailbox-FIFO deploy ordering (see
/// <see cref="Actors.DeploymentManagerActor.HandleDeploy"/>). Compiles of unchanged
/// script bodies are deduped by that cache, which is shared across the whole site because
/// the compilation service is a DI singleton.
/// </summary>
public sealed class DeployCompileValidator
{
private readonly ScriptCompilationService _compilationService;
/// <summary>Initializes a new instance of the <see cref="DeployCompileValidator"/>.</summary>
/// <param name="compilationService">The same compilation service the Instance Actor uses at runtime.</param>
public DeployCompileValidator(ScriptCompilationService compilationService)
=> _compilationService = compilationService;
/// <summary>
/// Prefix carried by every runtime-name error, so a caller can tell a name rejection
/// (<see cref="IsActorNameError"/>) from a compile failure when wording its reply.
/// </summary>
private const string ActorNameErrorPrefix = "Invalid runtime name: ";
/// <summary>True when <paramref name="error"/> came from the runtime-name check.</summary>
/// <param name="error">One error returned by <see cref="Validate"/>.</param>
/// <returns><see langword="true"/> for a runtime-name error.</returns>
public static bool IsActorNameError(string error) =>
error.StartsWith(ActorNameErrorPrefix, StringComparison.Ordinal);
/// <summary>
/// Validates every compilable artifact in the flattened configuration.
/// Returns an empty list when the whole configuration compiles; otherwise
/// one human-readable error per failing artifact. Malformed JSON is itself a
/// single error (this also fronts the poison-config case for startup load).
/// </summary>
/// <param name="configJson">The flattened-configuration JSON to validate.</param>
/// <param name="instanceName">
/// The instance unique name the Deployment Manager will create the Instance Actor under.
/// When supplied it is checked as an actor name along with every script/alarm/native-source
/// child name (review 2026-10-21 01/SR-C1); <see langword="null"/> checks the child names only.
/// </param>
/// <returns>A list of compile-error messages; empty if the configuration is clean.</returns>
public IReadOnlyList<string> Validate(string configJson, string? instanceName = null)
{
if (!TryParse(configJson, out var config, out var parseError))
return [parseError!];
return Validate(config!, instanceName);
}
/// <summary>
/// Deserializes a flattened-configuration JSON exactly as <see cref="Validate(string, string?)"/>
/// does, so a caller that needs the parsed object as well (the Deployment Manager, which hands
/// it on to the Instance Actor — review 2026-10-21 01/SR-P5) parses once instead of once per
/// consumer. The error text is the one <see cref="Validate(string, string?)"/> reports.
/// </summary>
/// <param name="configJson">The flattened-configuration JSON.</param>
/// <param name="config">The parsed configuration, or <see langword="null"/> on failure.</param>
/// <param name="error">The deserialization error, or <see langword="null"/> on success.</param>
/// <returns><see langword="true"/> when the JSON produced a configuration.</returns>
public static bool TryParse(string configJson, out FlattenedConfiguration? config, out string? error)
{
try
{
config = JsonSerializer.Deserialize<FlattenedConfiguration>(configJson);
}
catch (JsonException ex)
{
config = null;
error = $"Configuration JSON failed to deserialize: {ex.Message}";
return false;
}
if (config == null)
{
error = "Configuration JSON failed to deserialize: result was null.";
return false;
}
error = null;
return true;
}
/// <summary>
/// Validates an already-parsed flattened configuration — the same checks as
/// <see cref="Validate(string, string?)"/> without a second deserialization (01/SR-P5).
/// </summary>
/// <param name="config">The parsed flattened configuration.</param>
/// <param name="instanceName">See <see cref="Validate(string, string?)"/>.</param>
/// <returns>A list of error messages; empty if the configuration is clean.</returns>
public IReadOnlyList<string> Validate(FlattenedConfiguration config, string? instanceName = null)
{
var errors = new List<string>();
// Runtime names first: they are cheap, and a name Akka refuses would otherwise surface
// as an InvalidActorNameException at Context.ActorOf instead of a readable rejection.
AddActorNameErrors(errors, config, instanceName);
// Scripts: full execution-path compile against ScriptGlobals.
foreach (var script in config.Scripts)
{
var result = _compilationService.Compile(script.CanonicalName, script.Code);
if (!result.IsSuccess)
errors.Add($"Script '{script.CanonicalName}': {string.Join("; ", result.Errors)}");
// Expression-triggered scripts also compile their trigger expression.
AddTriggerExpressionError(errors, script.TriggerType, script.TriggerConfiguration,
$"script-trigger-{script.CanonicalName}");
}
// Alarms: on-trigger script reference must resolve + compile, and any
// Expression trigger must compile — mirrors InstanceActor.CreateChildActors.
foreach (var alarm in config.Alarms)
{
if (!string.IsNullOrEmpty(alarm.OnTriggerScriptCanonicalName))
{
var triggerScriptDef = config.Scripts
.FirstOrDefault(s => s.CanonicalName == alarm.OnTriggerScriptCanonicalName);
if (triggerScriptDef == null)
{
errors.Add($"Alarm '{alarm.CanonicalName}': on-trigger script " +
$"'{alarm.OnTriggerScriptCanonicalName}' is not defined.");
}
else
{
var result = _compilationService.Compile(
$"alarm-trigger-{alarm.CanonicalName}", triggerScriptDef.Code);
if (!result.IsSuccess)
errors.Add($"Alarm '{alarm.CanonicalName}' on-trigger script " +
$"'{alarm.OnTriggerScriptCanonicalName}': {string.Join("; ", result.Errors)}");
}
}
AddTriggerExpressionError(errors, alarm.TriggerType, alarm.TriggerConfiguration,
$"alarm-trigger-expr-{alarm.CanonicalName}");
}
return errors;
}
/// <summary>
/// Populates the shared compile cache for a whole startup batch (review 2026-10-21 01/SR-P3):
/// every DISTINCT script body and trigger expression across <paramref name="configs"/> is
/// compiled once, up to <paramref name="maxDegreeOfParallelism"/> at a time. Same compile units
/// as <see cref="Validate(FlattenedConfiguration, string?)"/> — scripts and alarm on-trigger
/// scripts against <c>ScriptGlobals</c>, Expression triggers against
/// <c>TriggerExpressionGlobals</c> — but deduplicated first, because the cache is keyed on
/// code: N instances of one template would otherwise race N identical cold compiles in
/// parallel. Best-effort: verdicts are discarded and a throwing unit is skipped, since every
/// Instance Actor's own compile remains the correctness backstop.
/// </summary>
/// <param name="configs">The batch's parsed configurations.</param>
/// <param name="maxDegreeOfParallelism">Upper bound on concurrent Roslyn compiles.</param>
public void WarmCompileCache(IReadOnlyList<FlattenedConfiguration> configs, int maxDegreeOfParallelism)
{
var units = new Dictionary<(bool IsTrigger, string Code), string>();
foreach (var config in configs)
{
foreach (var script in config.Scripts)
{
units.TryAdd((false, script.Code), script.CanonicalName);
AddTriggerUnit(units, script.TriggerType, script.TriggerConfiguration,
$"script-trigger-{script.CanonicalName}");
}
// Alarm on-trigger scripts compile the referenced SCRIPT's body against ScriptGlobals,
// i.e. the same cache key as that script — already a unit above.
foreach (var alarm in config.Alarms)
AddTriggerUnit(units, alarm.TriggerType, alarm.TriggerConfiguration,
$"alarm-trigger-expr-{alarm.CanonicalName}");
}
Parallel.ForEach(
units,
new ParallelOptions { MaxDegreeOfParallelism = Math.Max(1, maxDegreeOfParallelism) },
unit =>
{
try
{
if (unit.Key.IsTrigger)
_compilationService.CompileTriggerExpression(unit.Value, unit.Key.Code);
else
_compilationService.Compile(unit.Value, unit.Key.Code);
}
catch
{
// Best-effort warm — the Instance Actor's PreStart compiles it the hard way.
}
});
}
private static void AddTriggerUnit(
Dictionary<(bool IsTrigger, string Code), string> units,
string? triggerType, string? triggerConfigJson, string compileName)
{
if (!string.Equals(triggerType, "Expression", StringComparison.OrdinalIgnoreCase))
return;
var expression = TriggerExpressionGlobals.ExtractExpression(triggerConfigJson);
if (expression != null)
units.TryAdd((true, expression), compileName);
}
/// <summary>
/// Review 2026-10-21 01/SR-C1: every name that becomes an Akka actor name on the site — the
/// Instance Actor itself (<c>DeploymentManagerActor.CreateInstanceActor</c>) and its
/// <c>script-</c>/<c>alarm-</c>/<c>native-alarm-</c> children
/// (<c>InstanceActor.CreateChildActors</c>) — must be a legal path element. An instance named
/// <c>"Furnace #3"</c> used to make <c>Context.ActorOf</c> throw on the Deployment Manager
/// singleton's own thread, restarting it and every Instance Actor on the site.
///
/// <para>
/// Decided with Akka's own <see cref="ActorPath.IsValidPathElement"/>, not the Commons mirror
/// <c>ActorNameRules</c>: the mirror is currently STRICTER (it refuses <c>"</c>, <c>(</c> and
/// <c>)</c>, which Akka accepts), so gating on it would reject deployments that work today.
/// </para>
/// </summary>
private static void AddActorNameErrors(
List<string> errors, FlattenedConfiguration config, string? instanceName)
{
if (instanceName != null)
AddActorNameError(errors, "Instance", instanceName, instanceName);
foreach (var script in config.Scripts)
AddActorNameError(errors, "Script", script.CanonicalName, $"script-{script.CanonicalName}");
foreach (var alarm in config.Alarms)
AddActorNameError(errors, "Alarm", alarm.CanonicalName, $"alarm-{alarm.CanonicalName}");
foreach (var source in config.NativeAlarmSources)
AddActorNameError(errors, "Native alarm source", source.CanonicalName,
$"native-alarm-{source.CanonicalName}");
}
private static void AddActorNameError(
List<string> errors, string kind, string? name, string actorName)
{
if (ActorPath.IsValidPathElement(actorName))
return;
errors.Add($"{ActorNameErrorPrefix}{kind} name '{name}' cannot be used as a runtime actor " +
"name — use ASCII letters, digits and -_.*$+:@&=,!~';() only (no spaces, '#', " +
"'/', '[', ']', '%' or non-ASCII letters), and do not start it with '$'.");
}
/// <summary>
/// Compiles the boolean trigger expression for an Expression-triggered script
/// or alarm and records any failure. No-op for non-Expression triggers or a
/// blank/malformed expression config — mirroring
/// <c>InstanceActor.CompileTriggerExpression</c>, where those cases yield an
/// inert trigger rather than a deployment error.
/// </summary>
private void AddTriggerExpressionError(
List<string> errors, string? triggerType, string? triggerConfigJson, string compileName)
{
if (!string.Equals(triggerType, "Expression", StringComparison.OrdinalIgnoreCase))
return;
var expression = TriggerExpressionGlobals.ExtractExpression(triggerConfigJson);
if (expression == null)
return;
var result = _compilationService.CompileTriggerExpression(compileName, expression);
if (!result.IsSuccess)
errors.Add($"Trigger expression '{compileName}': {string.Join("; ", result.Errors)}");
}
}