Owner review 1, phase 2 (docs/requests/2026-10-15-owner-review-1.md, R2). Jobs now run one at a time on the desktop and on the server. Parallelism (Settings > Rendering, TITANO_PARALLELISM) is how many photos the running job processes at once: thumbnail and proxy passes draw that many photos together and still write the pack and checkpoints in frame order, decode tiers are probed that many at a time, the deep analysis measures that many frames and a render decodes that many ahead. Wherever originals are developed whole (TIFF, PNG, RAW without a usable preview), fewer run at once within the memory budget, which the running job now has whole; the server's frame window is sized to the host instead of 1 GiB. A new job waits its turn and pausing the running job starts the next one. An optional priority queue gives each job kind a level (renders, imports and downloads high, verifies and proxies low by default): a queued job of a higher level pauses the running one without asking, and that job goes back to the queue with its checkpoint and resumes by itself afterwards. Jobs of the same level, and a job's own follow-ups, never interrupt it. The Jobs screen and the admin panel show the parallelism and the queue; Settings > Rendering and the admin panel's Settings set them, at once. TITANO_WORKERS and the Workers key of server.json still set the parallelism (1 to 64, with a warning at start for the variable). Titano.Tools gains i18n prune, which removed the renamed keys from the 36 machine-translated languages; the new strings are translated in all 38. Found while testing: uploads and imports failed when a pass read the sidecar while they wrote it (atomic writes now retry the rename while a reader holds the file, and never for a read-only one), and taking a lease just released while another process read it failed on Windows (the create now waits for the reader). Five adversarial review rounds checked the phase before this commit. Also updates BRIEF (12.8, 14.6, 14.8, 14.9, 24), DECISIONS, CHANGELOG, ARCHITECTURE, STATUS, the request file and the compose/Unraid templates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
177 lines
11 KiB
C#
177 lines
11 KiB
C#
using Titano.Core.Analysis.Deep;
|
|
using Titano.Core.Archiving;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Core.Imaging.Decoders;
|
|
using Titano.Core.Model;
|
|
using Titano.Data.Archiving;
|
|
using Titano.Data.Processing;
|
|
using Titano.Testing;
|
|
using SequenceFrames = Titano.Data.Processing.SequenceFrames;
|
|
|
|
namespace Titano.Data.Tests.Processing;
|
|
|
|
/// <summary>
|
|
/// Photos decoded at once (owner review 1, R2): the whole parallelism, unless developing large originals whole would
|
|
/// exceed the memory budget. Only the decodes that develop an original count: a JPEG at least twice the size asked for
|
|
/// decodes scaled down, and a frame read through its proxy or an embedded preview of that size needs no development.
|
|
/// </summary>
|
|
public sealed class DecodeParallelismTests
|
|
{
|
|
/// <summary>2 GiB: two 24 MP developments (some 768 MB each), one of 45 MP.</summary>
|
|
private const long Budget = 2L << 30;
|
|
|
|
private static readonly ImageDecoder Server = DecoderFactory.CreateServer();
|
|
|
|
[Test]
|
|
public void WhatADecodeDevelopsWholeDependsOnTheFormatTheSizeAskedForAndThePreview()
|
|
{
|
|
const int w = 6000, h = 4000;
|
|
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 320, preferPreview: false));
|
|
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3000, preferPreview: false));
|
|
// A 4K render of a 24 MP JPEG decodes it nearly whole; full size (0) always does.
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3840, preferPreview: false));
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 0, preferPreview: true));
|
|
// A preview reaching the size serves only decodes that prefer previews; a render develops the RAW.
|
|
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: true));
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: false));
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 2048, preferPreview: true));
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, 0, 320, preferPreview: true));
|
|
// Only RAW decoders take a preview by choice: a TIFF's or a HEIF's is never a shortcut.
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, w, 512, preferPreview: true));
|
|
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Heif, w, h, 512, 512, preferPreview: true));
|
|
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Dng, w, h, 1024, 512, preferPreview: true));
|
|
Assert.Equal(DecodeBudget.UnknownPixels, DecodeBudget.Developed(ImageFileFormat.Png, 0, 0, 0, 320, preferPreview: true));
|
|
}
|
|
|
|
[Test]
|
|
public void ThumbnailAndProxyPassesHoldBackOnlyForTheOriginalsTheyDevelop()
|
|
{
|
|
static SidecarFrame Frame(ImageFileFormat format, int width, int height, int? preview = null) =>
|
|
new() { Id = Guid.NewGuid(), File = "f", Format = format, W = width, H = height, PreviewW = preview, PreviewH = preview * 2 / 3 };
|
|
static int Thumbnails(params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, 320, preferPreview: true, Server);
|
|
static int Proxies(int edge, params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, edge, preferPreview: false, Server);
|
|
|
|
Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Cr2, 8192, 5464, preview: 1620)));
|
|
// A HEIF the server reads through its proxy or preview develops nothing either.
|
|
Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Heif, 8192, 5464)));
|
|
// A 45 MP TIFF is decoded whole: one at a time within 2 GiB, whatever the JPEGs beside it.
|
|
Assert.Equal(1, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Tiff, 8192, 5464)));
|
|
// A RAW whose preview is smaller than the thumbnail is developed; a size nobody knows counts as 24 MP.
|
|
Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 160)));
|
|
Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Png, 0, 0)));
|
|
Assert.Equal(1, ArchiveImages.PassParallelism(0, Budget, [Frame(ImageFileFormat.Jpeg, 48, 32)], 320, preferPreview: true, Server));
|
|
// Proxies of 2048 px scale JPEGs down but develop every RAW, preview or not; full-size proxies decode JPEGs whole.
|
|
Assert.Equal(8, Proxies(2048, Frame(ImageFileFormat.Jpeg, 8192, 5464)));
|
|
Assert.Equal(2, Proxies(2048, Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 6000)));
|
|
Assert.Equal(2, Proxies(0, Frame(ImageFileFormat.Jpeg, 6000, 4000)));
|
|
}
|
|
|
|
[Test]
|
|
public void ADeepAnalysisHoldsBackOnlyForTheOriginalsItDevelops()
|
|
{
|
|
var analysis = DeepAnalysisOptions.Default with { MaxDegreeOfParallelism = 8 };
|
|
Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Jpeg, 8192, 5464, 0)), Budget));
|
|
// A DNG whose embedded preview reaches the analysis size (half the long edge, at most 2048 px) develops nothing.
|
|
Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 2784)), Budget));
|
|
Assert.Equal(1, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 1024), (ImageFileFormat.Tiff, 8192, 5464, 0)), Budget));
|
|
// Frames read through their proxies decode as JPEGs.
|
|
Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Tiff, 8192, 5464, 0)) with { Proxied = [true] }, Budget));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AWindowHoldsBackWheneverItsDecodesDevelopOriginals()
|
|
{
|
|
var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 };
|
|
var tiffs = Frames((ImageFileFormat.Tiff, 8192, 5464, 0), (ImageFileFormat.Tiff, 8192, 5464, 0));
|
|
// A render node renders downloaded originals without counting what they decode (Originals unset): still held back.
|
|
await using (var node = new SequenceFrameSource(tiffs, tiffs.LightFrames(), Server, window))
|
|
{
|
|
Assert.Equal(1, node.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
|
|
// A preparation pass decodes TIFFs whole too.
|
|
Assert.Equal(1, node.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads);
|
|
Assert.Equal((Budget, 8), (node.WindowOptions(1920, DecodePurpose.Render).MemoryBudget, node.WindowOptions(1920, DecodePurpose.Render).Ahead));
|
|
}
|
|
// RAWs whose previews reach the preparation's size: the preparation runs them all, the render develops them.
|
|
var raws = Frames((ImageFileFormat.Cr2, 6000, 4000, 1620), (ImageFileFormat.Cr2, 6000, 4000, 1620));
|
|
await using (var raw = new SequenceFrameSource(raws, raws.LightFrames(), Server, window) { Originals = true })
|
|
{
|
|
Assert.Equal(8, raw.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads);
|
|
Assert.Equal(2, raw.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
|
|
}
|
|
// Proxies decode as JPEGs, flagged or found in a proxy cache's .jpg files.
|
|
await using (var proxied = new SequenceFrameSource(tiffs with { Proxied = [true, true] }, tiffs.LightFrames(), Server, window))
|
|
Assert.Equal(8, proxied.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
|
|
var cached = raws with { Files = ["cache/0001.jpg", "cache/0002.JPG"] };
|
|
await using var cache = new SequenceFrameSource(cached, cached.LightFrames(), Server, window);
|
|
Assert.Equal(8, cache.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ADemandLoadWaitsForTheReadAheadOfADevelopingWindow()
|
|
{
|
|
// The window caps its read-ahead only: a render that skips frames (a speed-up) asks for a frame the read-ahead is
|
|
// not decoding while it develops another, and both would run. One development at a time fits this budget.
|
|
var decoder = new CountingDecoder();
|
|
var tiffs = Frames([.. Enumerable.Repeat((ImageFileFormat.Tiff, 8192, 5464, 0), 8)]);
|
|
var frames = tiffs with { Files = [.. tiffs.Files.Select(f => f + ".tif")] };
|
|
var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 };
|
|
await using (var source = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([decoder]), window))
|
|
{
|
|
Assert.Equal(1, source.DevelopingLimit(1920, DecodePurpose.Render));
|
|
foreach (var index in new[] { 0, 2, 4, 6 })
|
|
using (await source.GetFrameAsync(index, 1920, DecodePurpose.Render, CancellationToken.None)) { }
|
|
}
|
|
Assert.Equal(1, decoder.Peak);
|
|
Assert.Greater(decoder.Decodes, 4);
|
|
}
|
|
|
|
/// <summary>Decodes anything as a tiny frame, slowly, and counts how many decodes run at once.</summary>
|
|
private sealed class CountingDecoder : IImageDecoderProvider
|
|
{
|
|
private int _running;
|
|
private int _peak;
|
|
private int _decodes;
|
|
|
|
public int Peak => Volatile.Read(ref _peak);
|
|
|
|
public int Decodes => Volatile.Read(ref _decodes);
|
|
|
|
public string Name => "counting";
|
|
|
|
public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native;
|
|
|
|
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
var now = Interlocked.Increment(ref _running);
|
|
int seen;
|
|
while ((seen = Volatile.Read(ref _peak)) < now && Interlocked.CompareExchange(ref _peak, now, seen) != seen)
|
|
{
|
|
}
|
|
Interlocked.Increment(ref _decodes);
|
|
Thread.Sleep(40);
|
|
Interlocked.Decrement(ref _running);
|
|
return new DecodedImage(new ImageBuffer(8, 6), DecodeTier.Native, Name, 8192, 5464);
|
|
}
|
|
}
|
|
|
|
private static SequenceFrames Frames(params (ImageFileFormat Format, int Width, int Height, int Preview)[] specs)
|
|
{
|
|
var sequence = new Sequence { Id = Guid.NewGuid(), ArchiveId = Guid.NewGuid(), Folder = "a" };
|
|
var frames = specs.Select((s, i) => new Frame
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SequenceId = sequence.Id,
|
|
Index = i,
|
|
RelativePath = $"a/IMG_{i:0000}",
|
|
FileName = $"IMG_{i:0000}",
|
|
CapturedAt = new DateTime(2026, 10, 16, 6, 0, 0).AddSeconds(10 * i),
|
|
Width = s.Width,
|
|
Height = s.Height,
|
|
Format = s.Format,
|
|
PreviewWidth = s.Preview,
|
|
PreviewHeight = s.Preview * 2 / 3,
|
|
}).ToList();
|
|
return new SequenceFrames(sequence, frames, [.. frames.Select(f => (string?)f.RelativePath)]);
|
|
}
|
|
}
|