Files
Chronos/tests/Titano.Data.Tests/Archiving/OrderedWorkTests.cs
T
Alby96andClaude Opus 5.5 43b6871cdc feat: jobs run one at a time, parallelism per photo and a priority queue
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>
2026-10-16 14:56:24 +02:00

133 lines
4.8 KiB
C#

using Titano.Data.Archiving;
using Titano.Testing;
namespace Titano.Data.Tests.Archiving;
/// <summary>Several photos of a pass at once (owner review 1, R2), their results taken in order.</summary>
public sealed class OrderedWorkTests
{
[Test]
public async Task ResultsAreTakenInOrderWhateverFinishesFirst()
{
var taken = new List<int>();
await using (var work = new OrderedWork<int>(4, n =>
{
taken.Add(n);
return ValueTask.CompletedTask;
}, CancellationToken.None))
{
for (var i = 0; i < 24; i++)
{
var n = i;
// The later items are the quicker ones.
await work.AddAsync(_ =>
{
Thread.Sleep((24 - n) * 2);
return n;
});
if (n % 5 == 0) await work.AddAsync(1000 + n);
}
await work.CompleteAsync();
}
int[] expected = [.. Enumerable.Range(0, 24).SelectMany(n => n % 5 == 0 ? new[] { n, 1000 + n } : [n])];
Assert.SequenceEqual(expected, taken);
}
[Test]
public async Task ExactlyTheParallelismRunsAtOnceAndOneRunsInline()
{
var running = 0;
var peak = 0;
await using (var work = new OrderedWork<int>(3, _ => ValueTask.CompletedTask, CancellationToken.None))
{
for (var i = 0; i < 12; i++)
{
await work.AddAsync(_ =>
{
var now = Interlocked.Increment(ref running);
int seen;
while ((seen = Volatile.Read(ref peak)) < now && Interlocked.CompareExchange(ref peak, now, seen) != seen)
{
}
// Each waits for two others, which proves three run together whatever the load (the pool adds threads).
SpinWait.SpinUntil(() => Volatile.Read(ref peak) >= 3, TimeSpan.FromSeconds(10));
Thread.Sleep(2);
Interlocked.Decrement(ref running);
return 0;
});
}
await work.CompleteAsync();
}
Assert.Equal(3, peak);
var caller = Environment.CurrentManagedThreadId;
var threads = new List<int>();
await using (var single = new OrderedWork<int>(1, _ => ValueTask.CompletedTask, CancellationToken.None))
{
for (var i = 0; i < 3; i++)
{
await single.AddAsync(_ =>
{
threads.Add(Environment.CurrentManagedThreadId);
return 0;
});
}
await single.CompleteAsync();
}
Assert.SequenceEqual([caller, caller, caller], threads);
}
[Test]
public async Task AFailureStopsTheWorkStillRunningBeforeThePassEnds()
{
var started = 0;
var finished = 0;
var stopped = 0;
var taken = new List<int>();
var work = new OrderedWork<int>(4, n =>
{
taken.Add(n);
return ValueTask.CompletedTask;
}, CancellationToken.None);
var failure = await Assert.ThrowsAsync<InvalidDataException>(async () =>
{
try
{
for (var i = 0; i < 8; i++)
{
var n = i;
await work.AddAsync(ct =>
{
Interlocked.Increment(ref started);
try
{
if (n == 2) throw new InvalidDataException("damaged photo");
if (n > 2)
{
// The photos after the damaged one take long, until the pass stops them.
if (ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10))) Interlocked.Increment(ref stopped);
ct.ThrowIfCancellationRequested();
}
return n;
}
finally
{
Interlocked.Increment(ref finished);
}
});
}
await work.CompleteAsync();
}
finally
{
await work.DisposeAsync();
}
});
Assert.Equal("damaged photo", failure.Message);
Assert.SequenceEqual([0, 1], taken);
// Nothing the pass started outlives it.
Assert.Equal(Volatile.Read(ref started), Volatile.Read(ref finished));
Assert.Equal(Volatile.Read(ref started) - 3, Volatile.Read(ref stopped));
}
}