feat: expose mapped raw and uns data through grpc adapters
This commit is contained in:
@@ -66,6 +66,9 @@ public interface IScriptRunner
|
||||
IReadOnlyCollection<string> QuarantinedKeys(ScriptContextKind kind);
|
||||
}
|
||||
|
||||
/// <summary>A runner that owns invocation physical leases until underlying execution actually completes.</summary>
|
||||
public interface IPhysicalScriptRunner : IScriptRunner { }
|
||||
|
||||
/// <summary>Which script context (and result type) a script compiles against.</summary>
|
||||
public enum ScriptContextKind
|
||||
{
|
||||
@@ -86,13 +89,15 @@ public enum ScriptContextKind
|
||||
/// <param name="Inputs">The read cache: dependency snapshots by path. Not mutated until the run completes.</param>
|
||||
/// <param name="Timeout">The soft budget <c>T</c>.</param>
|
||||
/// <param name="ScriptLogger">The per-evaluation script logger <c>ctx.Logger</c> writes through; null ⇒ a silent logger.</param>
|
||||
/// <param name="PhysicalLease">Retained input ownership transferred to a physical-lifetime-aware runner.</param>
|
||||
public sealed record ScriptInvocation(
|
||||
string Key,
|
||||
ScriptContextKind Kind,
|
||||
string Source,
|
||||
IReadOnlyDictionary<string, DataValueSnapshot> Inputs,
|
||||
TimeSpan Timeout,
|
||||
Serilog.ILogger? ScriptLogger = null);
|
||||
Serilog.ILogger? ScriptLogger = null,
|
||||
IDisposable? PhysicalLease = null);
|
||||
|
||||
/// <summary>How an evaluation ended.</summary>
|
||||
public enum ScriptRunStatus
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||
/// existing guard code, so the default mode is unchanged byte for byte. Unlike those paths, this runner remembers
|
||||
/// compile failures by source for every kind (Calculation already did, R07:P4).
|
||||
/// </remarks>
|
||||
public sealed class InProcessScriptRunner : IScriptRunner, IDisposable
|
||||
public sealed class InProcessScriptRunner : IPhysicalScriptRunner, IDisposable
|
||||
{
|
||||
private readonly CompiledScriptCache<VirtualTagContext, object?> _valueCache = new();
|
||||
private readonly CompiledScriptCache<AlarmPredicateContext, bool> _predicateCache = new();
|
||||
@@ -40,6 +40,13 @@ public sealed class InProcessScriptRunner : IScriptRunner, IDisposable
|
||||
public async Task<ScriptRunOutcome> RunAsync(ScriptInvocation invocation, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(invocation);
|
||||
var ownership = new PhysicalLeaseOwnership(invocation.PhysicalLease);
|
||||
try { return await RunOwnedAsync(invocation, ownership, ct).ConfigureAwait(false); }
|
||||
finally { ownership.DisposeIfUntransferred(); }
|
||||
}
|
||||
|
||||
private async Task<ScriptRunOutcome> RunOwnedAsync(ScriptInvocation invocation, PhysicalLeaseOwnership ownership, CancellationToken ct)
|
||||
{
|
||||
var key = ScopedKey(invocation.Kind, invocation.Key);
|
||||
var failureKey = ScopedKey(invocation.Kind, invocation.Source);
|
||||
if (_compileFailures.TryGetValue(failureKey, out var known)) return ScriptRunOutcome.Failed(known);
|
||||
@@ -54,7 +61,7 @@ public sealed class InProcessScriptRunner : IScriptRunner, IDisposable
|
||||
{
|
||||
var evaluator = await Task.Run(() => _valueCache.GetOrCompile(invocation.Source), ct).ConfigureAwait(false);
|
||||
var context = new VirtualTagContext(invocation.Inputs, static (_, _) => { }, logger);
|
||||
var value = await _guard.RunAsync(key, invocation.Source, evaluator, context, invocation.Timeout, ct)
|
||||
var value = await _guard.RunAsync(key, invocation.Source, evaluator, context, invocation.Timeout, ct, ownership.Transfer())
|
||||
.ConfigureAwait(false);
|
||||
return ScriptRunOutcome.Ok(value);
|
||||
}
|
||||
@@ -62,7 +69,7 @@ public sealed class InProcessScriptRunner : IScriptRunner, IDisposable
|
||||
{
|
||||
var evaluator = await Task.Run(() => _predicateCache.GetOrCompile(invocation.Source), ct).ConfigureAwait(false);
|
||||
var context = new AlarmPredicateContext(invocation.Inputs, logger);
|
||||
var value = await _guard.RunAsync(key, invocation.Source, evaluator, context, invocation.Timeout, ct)
|
||||
var value = await _guard.RunAsync(key, invocation.Source, evaluator, context, invocation.Timeout, ct, ownership.Transfer())
|
||||
.ConfigureAwait(false);
|
||||
return ScriptRunOutcome.Ok(value);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||
|
||||
/// <summary>Idempotent transfer of a retained physical payload to a runner completion callback.</summary>
|
||||
public sealed class PhysicalLeaseOwnership(IDisposable? lease) : IDisposable
|
||||
{
|
||||
private IDisposable? _lease = lease;
|
||||
private int _transferred;
|
||||
public IDisposable? Transfer() { Interlocked.Exchange(ref _transferred, 1); return this; }
|
||||
public void DisposeIfUntransferred() { if (Volatile.Read(ref _transferred) == 0) Dispose(); }
|
||||
public void Dispose() => Interlocked.Exchange(ref _lease, null)?.Dispose();
|
||||
}
|
||||
@@ -174,15 +174,20 @@ public sealed class ScriptRunGuard
|
||||
/// <exception cref="ScriptRunRefusedException">The key's previous run is still executing.</exception>
|
||||
/// <exception cref="ScriptTimeoutException">The run exceeded <paramref name="timeout"/>.</exception>
|
||||
/// <exception cref="OperationCanceledException"><paramref name="ct"/> was cancelled.</exception>
|
||||
/// <param name="physicalLease">Input ownership released on nonadmission or actual evaluator completion.</param>
|
||||
public async Task<TResult> RunAsync<TContext, TResult>(
|
||||
string key,
|
||||
string? sourceIdentity,
|
||||
ScriptEvaluator<TContext, TResult> evaluator,
|
||||
TContext context,
|
||||
TimeSpan timeout,
|
||||
CancellationToken ct = default)
|
||||
CancellationToken ct = default,
|
||||
IDisposable? physicalLease = null)
|
||||
where TContext : ScriptContext
|
||||
{
|
||||
var transferred = false;
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(key);
|
||||
ArgumentNullException.ThrowIfNull(evaluator);
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
@@ -267,14 +272,19 @@ public sealed class ScriptRunGuard
|
||||
// evaluation still running" — a Bad publish for a script that had finished. Seen under load:
|
||||
// 24 of 4,800 sequential same-key evaluations. A late-finishing orphan that breached the
|
||||
// allocation budget quarantines its key first, so no re-trigger slips in between.
|
||||
if (succeeded && BreachedBudget(slot.AllocatedBytes))
|
||||
Quarantine(key, state, AllocationReason(slot.AllocatedBytes), "allocation");
|
||||
SlotReleasingForTest?.Invoke(key);
|
||||
Volatile.Write(ref state.RunStartedTimestamp, 0);
|
||||
Interlocked.Exchange(ref state.InFlight, 0);
|
||||
Interlocked.Decrement(ref _inFlight);
|
||||
try
|
||||
{
|
||||
if (succeeded && BreachedBudget(slot.AllocatedBytes))
|
||||
Quarantine(key, state, AllocationReason(slot.AllocatedBytes), "allocation");
|
||||
SlotReleasingForTest?.Invoke(key);
|
||||
Volatile.Write(ref state.RunStartedTimestamp, 0);
|
||||
Interlocked.Exchange(ref state.InFlight, 0);
|
||||
Interlocked.Decrement(ref _inFlight);
|
||||
}
|
||||
finally { physicalLease?.Dispose(); }
|
||||
}
|
||||
});
|
||||
transferred = true;
|
||||
|
||||
_ = runTask.ContinueWith(t =>
|
||||
{
|
||||
@@ -320,6 +330,8 @@ public sealed class ScriptRunGuard
|
||||
throw new ScriptQuarantinedException(key, state.QuarantineReason!);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
finally { if (!transferred) physicalLease?.Dispose(); }
|
||||
}
|
||||
|
||||
/// <summary>Resolve the key's state, rolling it over when the source identity changed, and refuse a quarantined key.</summary>
|
||||
|
||||
Reference in New Issue
Block a user