namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
///
/// Opportunistic batching for a seam whose evaluations are submitted independently โ one virtual-tag actor each โ
/// but often become ready together (one upstream burst fans out to many tags): evaluations submitted while another
/// submission is in progress are handed to together, in arrival order, by the
/// submitter already holding the flush (lmxopcua#780, design ยง4.5).
///
///
/// There is no timer: an evaluation on an idle seam is submitted at once by its own caller, so batching never
/// adds latency. Per-item semantics are 's โ each item keeps its own budget, slot
/// and blame.
/// Each caller's task completes synchronously on the thread that completes its run (the runner's result
/// dispatcher), exactly as awaiting directly does, so there is no extra thread
/// hop.
/// A key submitted twice concurrently is refused for the second exactly as two concurrent
/// calls would be; the virtual-tag actor never has two evaluations in flight.
///
public sealed class ScriptBatchCoalescer
{
private readonly IScriptRunner _runner;
private readonly object _lock = new();
private List<(ScriptInvocation Invocation, TaskCompletionSource Done)> _queue = [];
private bool _flushing;
/// Initializes the coalescer.
/// The runner every batch goes to.
public ScriptBatchCoalescer(IScriptRunner runner)
{
_runner = runner ?? throw new ArgumentNullException(nameof(runner));
}
/// Test hook: runs once per call, with the batch size.
internal Action? BatchSubmittedForTest { get; set; }
/// Submits one evaluation, together with any submitted concurrently.
/// What to run.
/// The outcome; never faults.
public Task RunAsync(ScriptInvocation invocation)
{
ArgumentNullException.ThrowIfNull(invocation);
// Synchronous continuations: the caller resumes where its run completed, as with a direct await of RunAsync.
var done = new TaskCompletionSource(TaskCreationOptions.None);
lock (_lock)
{
_queue.Add((invocation, done));
if (_flushing) return done.Task;
_flushing = true;
}
Flush();
return done.Task;
}
private void Flush()
{
while (true)
{
List<(ScriptInvocation Invocation, TaskCompletionSource Done)> batch;
lock (_lock)
{
if (_queue.Count == 0)
{
_flushing = false;
return;
}
batch = _queue;
_queue = [];
}
IReadOnlyList> outcomes;
try
{
var invocations = new ScriptInvocation[batch.Count];
for (var i = 0; i < invocations.Length; i++) invocations[i] = batch[i].Invocation;
BatchSubmittedForTest?.Invoke(invocations.Length);
outcomes = _runner.RunBatch(invocations);
}
catch (Exception ex)
{
// A defect in the runner must not strand the callers (nor the flush flag).
foreach (var (_, done) in batch) done.TrySetResult(ScriptRunOutcome.Failed($"script runner threw: {ex.Message}"));
continue;
}
for (var i = 0; i < batch.Count; i++) Link(outcomes[i], batch[i].Done);
}
}
private static void Link(Task outcome, TaskCompletionSource done)
{
if (outcome.IsCompleted)
{
Settle(outcome, done);
return;
}
outcome.ContinueWith(static (t, s) => Settle(t, (TaskCompletionSource)s!), done,
CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
private static void Settle(Task outcome, TaskCompletionSource done)
{
if (outcome.IsCompletedSuccessfully) done.TrySetResult(outcome.Result);
else done.TrySetResult(ScriptRunOutcome.Failed(
$"script runner threw: {outcome.Exception?.GetBaseException().Message ?? "cancelled"}"));
}
}