From db98385eb03819289b14e34842a0cf3b74a02b1f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 17:05:43 +0000 Subject: [PATCH] feat(app): render node follows the opt-in and the server session RenderNodeHost starts, restarts or stops this PC's render node in order and off the UI thread whenever the opt-in, the session or the index changes; a node that cannot start is retried by the next change and exit stops it. The server test covers managed renders on the server and MP4 jobs left queued for render nodes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Infrastructure/AppServices.cs | 73 +++++++------- .../Infrastructure/RenderNodeHost.cs | 98 +++++++++++++++++++ .../Server/RenderNodeHostTests.cs | 90 +++++++++++++++++ .../Storage/ProcessingModuleTests.cs | 66 ++++++++++--- 4 files changed, 276 insertions(+), 51 deletions(-) create mode 100644 src/Titano.App/Infrastructure/RenderNodeHost.cs create mode 100644 tests/Titano.App.Tests/Server/RenderNodeHostTests.cs diff --git a/src/Titano.App/Infrastructure/AppServices.cs b/src/Titano.App/Infrastructure/AppServices.cs index 5ed5098f..0f2a9571 100644 --- a/src/Titano.App/Infrastructure/AppServices.cs +++ b/src/Titano.App/Infrastructure/AppServices.cs @@ -41,9 +41,8 @@ public sealed class AppServices : IAsyncDisposable private readonly CancellationTokenSource _stopping = new(); private readonly Dictionary _scans = []; private Task _polling = Task.CompletedTask; + private readonly RenderNodeHost _renderNode; private ServerSession _server = null!; - private Task _nodeSwitch = Task.CompletedTask; - private (RenderNodeClient Node, TitanoClient Client, Uri Address, string Token)? _node; public AppServices(AppEnvironment environment, AppSettings settings, Action saveSettings, IAppDialogs dialogs) { @@ -51,6 +50,7 @@ public sealed class AppServices : IAsyncDisposable Settings = settings; SaveSettingsAction = saveSettings; Dialogs = dialogs; + _renderNode = new RenderNodeHost(StartRenderNode); ArchiveSetup = new EngineArchiveSetup(this); Server = new ServerSession(settings, saveSettings, new TitanoServerApiFactory(), new DpapiTokenProtector()); Encoders = new MediaFoundationInventory(); @@ -136,8 +136,11 @@ public sealed class AppServices : IAsyncDisposable UpdateRenderNode(); } - /// Starts or stops the render node to match the opt-in and the server session (BRIEF §14.7); switches run in order. - public void UpdateRenderNode() => _nodeSwitch = SwitchRenderNodeAsync(_nodeSwitch); + /// Starts or stops the render node to match the opt-in and the server session (BRIEF §14.7). + public void UpdateRenderNode() => + _renderNode.Update(Settings.RenderNodeOptIn && _server.IsConnected && Processing is not null && _server.Flow is { Address: { } address, Token: { } token } + ? new RenderNodeSession(address, token) + : null); /// Opens (or creates) the index at , migrates it and starts the job engine. public async Task OpenAsync(string databasePath, CancellationToken ct = default) @@ -172,8 +175,7 @@ public sealed class AppServices : IAsyncDisposable await _polling; _stopping.Dispose(); _server.PropertyChanged -= OnServerChanged; - await _nodeSwitch; - await StopRenderNodeAsync(); + await _renderNode.DisposeAsync(); (Jobs as IDisposable)?.Dispose(); if (Engine is not null) await Engine.DisposeAsync(); Thumbnails?.Dispose(); @@ -237,45 +239,36 @@ public sealed class AppServices : IAsyncDisposable private void OnServerChanged(object? sender, PropertyChangedEventArgs e) => UpdateRenderNode(); - /// Waits for the previous switch, then runs the node for the current session, or stops it. - private async Task SwitchRenderNodeAsync(Task previous) + /// + /// The render node of : this PC's encoders (Media Foundation, then managed), downloads and + /// outputs in the cache folder within its size limit. + /// + private IAsyncDisposable StartRenderNode(RenderNodeSession session) { - await previous; - var flow = _server.Flow; - (ProcessingService Engine, Uri Address, string Token)? wanted = - Settings.RenderNodeOptIn && _server.IsConnected && Processing is { } processing && flow.Address is { } address && flow.Token is { } token - ? (processing, address, token) - : null; - if (_node is { } running && wanted is { } same && running.Address == same.Address && running.Token == same.Token) return; - await StopRenderNodeAsync(); - if (wanted is not { } start) return; - try + var processing = Processing ?? throw new InvalidOperationException("The index is not open."); + var cache = CacheStore.Folder(Environment, Settings); + CacheStore.Prepare(cache); + var client = new TitanoClient(session.Address, new CertificatePinning(_server.Fingerprints), $"Titano ({System.Environment.MachineName})") { - var cache = CacheStore.Folder(Environment, Settings); - CacheStore.Prepare(cache); - var client = new TitanoClient(start.Address, new CertificatePinning(_server.Fingerprints), $"Titano ({System.Environment.MachineName})") { Token = start.Token }; - var node = new RenderNodeClient(client, start.Engine.Options, new RenderNodeOptions - { - ClientName = System.Environment.MachineName, - CacheDirectory = Path.Combine(cache, "render-node"), - CacheLimitBytes = Settings.CacheLimitGb * (1L << 30), - }); - node.Start(); - _node = (node, client, start.Address, start.Token); - Log.Info("render-node", $"Offering this PC as render node to {start.Address}"); - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + Token = session.Token, + }; + var node = new RenderNodeClient(client, processing.Options, new RenderNodeOptions { - Log.Warning("render-node", "The render node could not start", ex); - } + ClientName = System.Environment.MachineName, + CacheDirectory = Path.Combine(cache, "render-node"), + CacheLimitBytes = Settings.CacheLimitGb * (1L << 30), + }); + node.Start(); + return new RunningNode(node, client); } - private async Task StopRenderNodeAsync() + /// A started node and the client it owns. + private sealed class RunningNode(RenderNodeClient node, TitanoClient client) : IAsyncDisposable { - if (_node is not { } running) return; - _node = null; - await running.Node.DisposeAsync(); - running.Client.Dispose(); - Log.Info("render-node", "Render node stopped"); + public async ValueTask DisposeAsync() + { + await node.DisposeAsync().ConfigureAwait(false); + client.Dispose(); + } } } diff --git a/src/Titano.App/Infrastructure/RenderNodeHost.cs b/src/Titano.App/Infrastructure/RenderNodeHost.cs new file mode 100644 index 00000000..18c4e9ad --- /dev/null +++ b/src/Titano.App/Infrastructure/RenderNodeHost.cs @@ -0,0 +1,98 @@ +using Titano.Core.Diagnostics; + +namespace Titano.App.Infrastructure; + +/// The server session a render node works for: a change of either value means another node. +public sealed record RenderNodeSession(Uri Address, string Token); + +/// +/// Keeps this PC's render node (BRIEF §14.7) in line with "Offer this PC as render node" and the server session: +/// starts the node for the wanted session, restarts it for another session and stops it when none +/// is wanted. Switches run one after the other, off the caller's thread, and never fail: a node that cannot start is +/// logged and stays off until the next update. +/// +public sealed class RenderNodeHost(Func start) : IAsyncDisposable +{ + private readonly Lock _gate = new(); + private Task _switching = Task.CompletedTask; + private (RenderNodeSession Session, IAsyncDisposable Node)? _running; + private bool _disposed; + + /// The session the node runs for; null when it is off. + public RenderNodeSession? Session + { + get + { + lock (_gate) return _running?.Session; + } + } + + /// Completes when every switch requested so far has run. + public Task Switching + { + get + { + lock (_gate) return _switching; + } + } + + /// Runs the node for , or stops it when null. + public void Update(RenderNodeSession? wanted) + { + lock (_gate) + { + if (_disposed) return; + var previous = _switching; + _switching = Task.Run(async () => + { + await previous.ConfigureAwait(false); + await SwitchAsync(wanted).ConfigureAwait(false); + }); + } + } + + /// Stops the node after the pending switches; later updates are ignored. + public async ValueTask DisposeAsync() + { + Task pending; + lock (_gate) + { + _disposed = true; + pending = _switching; + } + await pending.ConfigureAwait(false); + await SwitchAsync(null).ConfigureAwait(false); + } + + private async Task SwitchAsync(RenderNodeSession? wanted) + { + (RenderNodeSession Session, IAsyncDisposable Node)? running; + lock (_gate) running = _running; + if (running?.Session == wanted) return; + if (running is { } old) + { + lock (_gate) _running = null; + try + { + await old.Node.DisposeAsync().ConfigureAwait(false); + Log.Info("render-node", "Render node stopped"); + } + catch (Exception ex) + { + // A node that fails to stop is gone anyway; the next one must still be able to start. + Log.Warning("render-node", "The render node did not stop cleanly", ex); + } + } + if (wanted is null) return; + try + { + var node = start(wanted); + lock (_gate) _running = (wanted, node); + Log.Info("render-node", $"Offering this PC as render node to {wanted.Address}"); + } + catch (Exception ex) + { + Log.Warning("render-node", "The render node could not start", ex); + } + } +} diff --git a/tests/Titano.App.Tests/Server/RenderNodeHostTests.cs b/tests/Titano.App.Tests/Server/RenderNodeHostTests.cs new file mode 100644 index 00000000..b600272e --- /dev/null +++ b/tests/Titano.App.Tests/Server/RenderNodeHostTests.cs @@ -0,0 +1,90 @@ +using System.IO; +using Titano.App.Infrastructure; +using Titano.Testing; + +namespace Titano.App.Tests.Server; + +public sealed class RenderNodeHostTests +{ + private static readonly RenderNodeSession Nas = new(new Uri("https://nas.local:8443/"), "token-a"); + + /// Records the nodes it starts; fails the first starts. + private sealed class Nodes + { + public List<(RenderNodeSession Session, Node Node)> Started { get; } = []; + public int Failures { get; set; } + + public IAsyncDisposable Start(RenderNodeSession session) + { + if (Failures-- > 0) throw new IOException("cache folder unavailable"); + var node = new Node(); + Started.Add((session, node)); + return node; + } + } + + private sealed class Node : IAsyncDisposable + { + public bool Stopped { get; private set; } + + public ValueTask DisposeAsync() + { + Stopped = true; + return ValueTask.CompletedTask; + } + } + + [Test] + public async Task TheNodeFollowsTheWantedSession() + { + var nodes = new Nodes(); + await using var host = new RenderNodeHost(nodes.Start); + + host.Update(Nas); + host.Update(Nas); + await host.Switching; + Assert.Equal(Nas, host.Session); + Assert.Count(1, nodes.Started); + + // A new token (signed in again) is another session: the old node stops before the new one starts. + var renewed = Nas with { Token = "token-b" }; + host.Update(renewed); + await host.Switching; + Assert.True(nodes.Started[0].Node.Stopped, "old node stopped"); + Assert.Equal((renewed, false), (nodes.Started[1].Session, nodes.Started[1].Node.Stopped)); + + host.Update(null); + await host.Switching; + Assert.Null(host.Session); + Assert.True(nodes.Started[1].Node.Stopped, "stopped when no longer wanted"); + } + + [Test] + public async Task ANodeThatCannotStartIsRetriedByTheNextUpdate() + { + var nodes = new Nodes { Failures = 1 }; + await using var host = new RenderNodeHost(nodes.Start); + + host.Update(Nas); + await host.Switching; + Assert.Null(host.Session); + + host.Update(Nas); + await host.Switching; + Assert.Equal(Nas, host.Session); + } + + [Test] + public async Task DisposingStopsTheNodeAndIgnoresLaterUpdates() + { + var nodes = new Nodes(); + var host = new RenderNodeHost(nodes.Start); + host.Update(Nas); + await host.DisposeAsync(); + + Assert.True(nodes.Started.Single().Node.Stopped, "stopped on exit"); + host.Update(Nas); + await host.Switching; + Assert.Count(1, nodes.Started); + } +} diff --git a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs index 61307753..059432c1 100644 --- a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs +++ b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs @@ -5,15 +5,23 @@ using Titano.Core.Diagnostics.Synthetic; using Titano.Core.Metadata; using Titano.Core.Model; using Titano.Core.Project; +using Titano.Data.Processing; +using Titano.Server.Jobs; using Titano.Server.Storage; +using Titano.Server.Tests.Jobs; using Titano.Testing; namespace Titano.Server.Tests.Storage; -/// The production composition: frames committed by the archive engine are light-analysed by the processing handlers. +/// +/// The production composition: frames committed by the archive engine are light-analysed by the processing handlers, +/// and the server renders the managed formats itself. +/// [Category("processing")] public sealed class ProcessingModuleTests(TestContext context) { + private const int Frames = 10; + /// The registrations of Program.cs. private static void Production(IServiceCollection services) { @@ -26,15 +34,7 @@ public sealed class ProcessingModuleTests(TestContext context) { await using var server = await TestServer.StartAsync(context, services: Production); var (editor, _, _) = await server.UserAsync("alberto", "editor"); - var scene = SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = 10, StartEv = 0.5, EndEv = -0.5 }); - Guid sequenceId = default; - for (var i = 0; i < scene.Count; i++) - { - using var image = scene.Render(i); - var exif = ExifWriter.BuildJpegApp1(scene.Metadata[i]); - var photo = JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] }); - sequenceId = (await Uploads.UploadAsync(editor, photo, $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId; - } + var sequenceId = await UploadSceneAsync(editor); // The commits queue a light analysis; its handler comes from the processing module. var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); @@ -42,7 +42,7 @@ public sealed class ProcessingModuleTests(TestContext context) while (true) { sequence = (await server.Database.Sequences.GetAsync(sequenceId))!; - if (sequence.Status.HasFlag(SequenceStatus.AnalyzedLight) && sequence.FrameCount == scene.Count + if (sequence.Status.HasFlag(SequenceStatus.AnalyzedLight) && sequence.FrameCount == Frames && !await server.Database.Jobs.IsQueuedAsync(JobKind.LightAnalyse, sequenceId)) break; Assert.True(DateTime.UtcNow < until, $"not analysed: {sequence.Status}, {sequence.FrameCount} frames"); await Task.Delay(100); @@ -53,4 +53,48 @@ public sealed class ProcessingModuleTests(TestContext context) Assert.FileExists(folder.ProjectPath); Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Status.HasFlag(SequenceStatus.AnalyzedLight), "sidecar status"); } + + [Test] + public async Task TheServerRendersManagedFormatsAndLeavesMp4ToRenderNodes() + { + await using var server = await TestServer.StartAsync(context, services: Production); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var sequence = (await server.Database.Sequences.GetAsync(await UploadSceneAsync(editor)))!; + // A client's export folder means nothing on the server: the output still goes to the sequence's exports. + var avi = new ExportSettings + { + Preset = new Auto().WithManual("mjpeg-avi"), + Overrides = new ExportOverrides { Width = 96, Height = 64 }, + NameTemplate = "clip", + Folder = context.CreateTempDirectory("elsewhere"), + }; + var hevc = new ExportSettings { Preset = new Auto().WithManual("4k-hevc") }; + + var mp4 = await JobApiTests.CreateAsync(editor, "render", sequence.Id, ProcessingJobs.Render(sequence, hevc, 96, 64).ParametersJson); + var managed = await JobApiTests.CreateAsync(editor, "render", sequence.Id, ProcessingJobs.Render(sequence, avi, 96, 64).ParametersJson); + var done = await JobApiTests.WaitForAsync(editor, managed.Id, "done"); + + Assert.Equal(JobEngineHost.WorkerId, done.NodeId?.Split('#')[0]); + var render = (await server.Database.Renders.ListBySequenceAsync(sequence.Id)).Single(); + Assert.Equal(($"{sequence.Folder}/exports/clip.avi", server.Options.ServerName, "Mjpeg"), (render.OutputPath, render.ProducedBy, render.Codec)); + Assert.FileExists(Path.Combine(server.Options.ArchivePath, render.OutputPath)); + Assert.Empty(Directory.EnumerateFileSystemEntries(avi.Folder)); + var waiting = await JobApiTests.GetAsync(editor, mp4.Id); + Assert.Equal(("queued", (string?)null), (waiting.State, waiting.NodeId)); + } + + /// Uploads a synthetic scene with Exif into one new sequence; returns its id. + private static async Task UploadSceneAsync(HttpClient editor) + { + var scene = SyntheticSequence.Create(new SyntheticSequenceOptions { FrameCount = Frames, StartEv = 0.5, EndEv = -0.5 }); + Guid sequenceId = default; + for (var i = 0; i < scene.Count; i++) + { + using var image = scene.Render(i); + var exif = ExifWriter.BuildJpegApp1(scene.Metadata[i]); + var photo = JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [exif[4..]] }); + sequenceId = (await Uploads.UploadAsync(editor, photo, $"IMG_{i + 1:0000}.JPG", sessionKey: "roof")).SequenceId; + } + return sequenceId; + } }