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>
This commit is contained in:
1 parent
34d03051f5
commit
43b6871cdc
228 files changed
+3535
-793
No files matched your search
@@ -145,6 +145,22 @@ public sealed class JobListModelTests
|
||||
Assert.False(JobListModel.ClaimsBefore(newer, older));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThePriorityQueueOrdersTheQueueAsTheEngineClaimsIt()
|
||||
{
|
||||
var model = new JobListModel();
|
||||
var verify = Job(JobState.Queued, created: -30, priority: JobPriority.User, kind: JobKind.Verify);
|
||||
var scan = Job(JobState.Queued, created: -20, kind: JobKind.Scan);
|
||||
var render = Job(JobState.Queued, created: -1, kind: JobKind.Render);
|
||||
foreach (var j in new[] { verify, scan, render }) model.Apply(j);
|
||||
Assert.SequenceEqual(new[] { verify.Id, scan.Id, render.Id }, model.Ordered().Select(e => e.Id));
|
||||
|
||||
// Owner review 1: on, renders come first and verifies last, whatever their age and priority.
|
||||
model.PriorityQueue = new JobPriorityQueue { Enabled = true };
|
||||
Assert.SequenceEqual(new[] { render.Id, scan.Id, verify.Id }, model.Ordered().Select(e => e.Id));
|
||||
Assert.Equal((1, 2, 3), (model.QueuePosition(render.Id), model.QueuePosition(scan.Id), model.QueuePosition(verify.Id)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TextsDescribeTheJob()
|
||||
{
|
||||
|
||||
@@ -36,8 +36,8 @@ public sealed class JobsScreenSmokeTests
|
||||
Assert.Greater(running, 0);
|
||||
Assert.Equal(running, jobs.Badge);
|
||||
Assert.Equal(Loc.Plural("Shell.Status.Jobs", running), shell.JobsText);
|
||||
// The capture engine starts no workers: there is no "Workers n / 0" to show.
|
||||
Assert.False(jobs.HasWorkers);
|
||||
// The capture engine runs no jobs: there is no "8 photos at once" to show.
|
||||
Assert.False(jobs.HasEngine);
|
||||
|
||||
// Tab reaches every row button: the rows take no focus, the list passes Tab on to their content.
|
||||
var list = app.FindAll<ListBox>().Single(l => l.Name == "JobList");
|
||||
|
||||
@@ -122,7 +122,8 @@ public sealed class ServerScreenSmokeTests
|
||||
Assert.SequenceEqual([SyncAction.Pause], Actions(comet));
|
||||
Assert.Contains("46", comet.ServerText);
|
||||
Assert.Equal(StatusKind.Queued, Row(table, "Fog lifting, Asti").StateKind);
|
||||
Assert.Equal((StatusKind.Running, Loc.L("Server.Sync.State.Downloading")), (Row(table, "Sunrise over Superga").StateKind, Row(table, "Sunrise over Superga").StateText));
|
||||
// One job at a time (owner review 1): the download halfway waits for the upload.
|
||||
Assert.Equal((StatusKind.Queued, Loc.L("Server.Sync.State.Waiting")), (Row(table, "Sunrise over Superga").StateKind, Row(table, "Sunrise over Superga").StateText));
|
||||
Assert.SequenceEqual([SyncAction.Resume], Actions(Row(table, "Lake Como storm")));
|
||||
await app.ExpectAsync<ServerView>("server-sync");
|
||||
|
||||
|
||||
@@ -181,34 +181,39 @@ public sealed class SettingsLogicTests
|
||||
var settings = new Titano.App.Infrastructure.AppSettings { Quality = Titano.App.Infrastructure.QualityProfile.Best, Parallelism = 3, MemoryBudgetGb = 6 };
|
||||
var basis = new Titano.Data.Processing.ProcessingOptions();
|
||||
|
||||
// Three jobs at once share the 6 GB: 2 GB each; the user's 3 also caps the decodes and the analysis threads.
|
||||
// One job runs at a time (owner review 1): it has the whole 6 GB, and decodes and measures 3 photos at once.
|
||||
var best = ProcessingTuning.Apply(basis, settings, 64L << 30, 16, @"C:\cache");
|
||||
Assert.Equal((2L << 30, @"C:\cache", 8, 2), (best.FrameWindow.MemoryBudget, best.FrameWindow.SpillDirectory, best.FrameWindow.Ahead, best.FrameWindow.MaxConcurrentLoads));
|
||||
Assert.Equal((6L << 30, @"C:\cache", 8, 3), (best.FrameWindow.MemoryBudget, best.FrameWindow.SpillDirectory, best.FrameWindow.Ahead, best.FrameWindow.MaxConcurrentLoads));
|
||||
Assert.Equal((1024, Titano.Core.Project.FlowQualitySetting.Best), (best.Render.AnalysisLongEdge, best.Render.DefaultFlowQuality));
|
||||
Assert.Equal(int.Min(3, Titano.Core.Analysis.Deep.DeepAnalysisOptions.Default.MaxDegreeOfParallelism), best.DeepAnalysis.MaxDegreeOfParallelism);
|
||||
Assert.Equal(3, best.DeepAnalysis.MaxDegreeOfParallelism);
|
||||
settings.Parallelism = 1;
|
||||
var single = ProcessingTuning.Apply(basis, settings, 64L << 30, 16, @"C:\cache");
|
||||
Assert.Equal((6L << 30, 1, 1), (single.FrameWindow.MemoryBudget, single.FrameWindow.MaxConcurrentLoads, single.DeepAnalysis.MaxDegreeOfParallelism));
|
||||
// A parallelism beyond the read-ahead widens the window, so the decodes have frames to work on.
|
||||
settings.Parallelism = 12;
|
||||
var wide = ProcessingTuning.Apply(basis, settings, 64L << 30, 16, @"C:\cache");
|
||||
Assert.Equal((12, 12, 12), (wide.FrameWindow.Ahead, wide.FrameWindow.MaxConcurrentLoads, wide.DeepAnalysis.MaxDegreeOfParallelism));
|
||||
|
||||
// Auto: a quarter of 32 GB shared by the engine's 8 workers on 16 cores; the jobs keep their own thread counts.
|
||||
// Auto: a quarter of 32 GB, and half the 16 cores at once, with no cap of the analysis threads.
|
||||
settings.Quality = Titano.App.Infrastructure.QualityProfile.Fast;
|
||||
settings.MemoryBudgetGb = null;
|
||||
settings.Parallelism = null;
|
||||
var fast = ProcessingTuning.Apply(basis, settings, 32L << 30, 16, @"C:\cache");
|
||||
Assert.Equal((1L << 30, 4), (fast.FrameWindow.MemoryBudget, fast.FrameWindow.Ahead));
|
||||
Assert.Equal((basis.FrameWindow.MaxConcurrentLoads, basis.DeepAnalysis.MaxDegreeOfParallelism), (fast.FrameWindow.MaxConcurrentLoads, fast.DeepAnalysis.MaxDegreeOfParallelism));
|
||||
Assert.Equal((8L << 30, 8), (fast.FrameWindow.MemoryBudget, fast.FrameWindow.Ahead));
|
||||
Assert.Equal((8, 8), (fast.FrameWindow.MaxConcurrentLoads, fast.DeepAnalysis.MaxDegreeOfParallelism));
|
||||
Assert.Equal((256, Titano.Core.Project.FlowQualitySetting.Draft), (fast.Render.AnalysisLongEdge, fast.Render.DefaultFlowQuality));
|
||||
Assert.Equal(512, ProcessingTuning.PreparationLongEdge(Titano.App.Infrastructure.QualityProfile.Balanced));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParallelismAutoIsTheJobEnginesDefault()
|
||||
public void ParallelismAutoIsHalfTheCores()
|
||||
{
|
||||
// BRIEF §14.6: workers default to half the cores; a 4-core laptop keeps two, so a long render never blocks a scan.
|
||||
// Photos the running job processes at once (owner review 1): half the cores, the archive engine's own default.
|
||||
Assert.Equal(8, ProcessingTuning.ParallelismAuto(16));
|
||||
Assert.Equal(2, ProcessingTuning.ParallelismAuto(4));
|
||||
Assert.Equal(1, ProcessingTuning.ParallelismAuto(1));
|
||||
Assert.Equal(new Titano.Data.Jobs.JobEngineOptions().Workers, ProcessingTuning.ParallelismAuto(Environment.ProcessorCount));
|
||||
Assert.Equal(Titano.Data.Archiving.ArchiveServiceOptions.DefaultParallelism, ProcessingTuning.ParallelismAuto(Environment.ProcessorCount));
|
||||
Assert.Equal(1, new Titano.Data.Jobs.JobEngineOptions().Workers);
|
||||
Assert.Equal(8, ProcessingTuning.Parallelism(new Titano.App.Infrastructure.AppSettings(), 16));
|
||||
Assert.Equal(5, ProcessingTuning.Parallelism(new Titano.App.Infrastructure.AppSettings { Parallelism = 5 }, 16));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,30 @@ public sealed class LeaseFileTests(TestContext context)
|
||||
Assert.Equal("client", LeaseFile.Read(LeaseFile.PathFor(locks, "seq-1"))!.Owner);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ALeaseReleasedWhileAReaderHoldsItIsTakenOnceTheReaderLetsGo()
|
||||
{
|
||||
// Found under load while testing owner review 1: on Windows the released file stays pending deletion while a reader
|
||||
// has it open, and creating the name answers "access denied" meanwhile instead of succeeding or saying "taken".
|
||||
var locks = context.CreateTempDirectory();
|
||||
var held = LeaseFile.TryAcquire(locks, "seq-1", "server")!;
|
||||
var path = LeaseFile.PathFor(locks, "seq-1");
|
||||
using var opened = new ManualResetEventSlim();
|
||||
var reader = new Thread(() =>
|
||||
{
|
||||
using var reading = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
opened.Set();
|
||||
Thread.Sleep(30);
|
||||
});
|
||||
reader.Start();
|
||||
Assert.True(opened.Wait(TimeSpan.FromSeconds(10)), "the reader opened the lease");
|
||||
held.Dispose();
|
||||
using var taken = LeaseFile.TryAcquire(locks, "seq-1", "client");
|
||||
reader.Join();
|
||||
Assert.NotNull(taken);
|
||||
Assert.Equal("client", LeaseFile.Read(path)!.Owner);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnExpiredLeaseOfACrashedHolderIsTakenOver()
|
||||
{
|
||||
|
||||
@@ -58,6 +58,34 @@ public sealed class ManifestStoreTests(TestContext context)
|
||||
Assert.Contains(field, json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ASidecarBeingReadIsReplacedOnceTheReaderIsDone()
|
||||
{
|
||||
// A thumbnail pass reads a sidecar without a lease while an upload commits to it (owner review 1). Windows refuses
|
||||
// to rename over a file someone has open, so the write tries again for a moment instead of failing the upload.
|
||||
var path = SidecarPath();
|
||||
var first = Sample();
|
||||
ManifestStore.WriteSequence(path, first);
|
||||
// The reader runs on its own thread, so it lets go on time however busy the thread pool is.
|
||||
using var opened = new ManualResetEventSlim();
|
||||
string? read = null;
|
||||
var reader = new Thread(() =>
|
||||
{
|
||||
using var reading = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
|
||||
opened.Set();
|
||||
Thread.Sleep(150);
|
||||
using var old = new StreamReader(reading);
|
||||
read = old.ReadToEnd();
|
||||
});
|
||||
reader.Start();
|
||||
Assert.True(opened.Wait(TimeSpan.FromSeconds(10)), "the reader opened the sidecar");
|
||||
ManifestStore.WriteSequence(path, first with { Title = "Rooftop" });
|
||||
reader.Join();
|
||||
Assert.Contains("Harbour sunset", read);
|
||||
Assert.Equal("Rooftop", ManifestStore.ReadSequence(path).Title);
|
||||
Assert.Contains("Rooftop", AtomicFile.ReadAllText(path));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnknownFieldsAndEnumValuesFromANewerBuildAreReadAndKept()
|
||||
{
|
||||
|
||||
@@ -16,6 +16,47 @@ public sealed class AtomicFileTests(TestContext context)
|
||||
Assert.Count(1, Directory.GetFiles(dir));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[WindowsOnly]
|
||||
public void AReadOnlyTargetFailsAtOnceAndTheTemporaryFileGoes()
|
||||
{
|
||||
// Only a handle that holds the target clears by itself (owner review 1); a read-only file never does, so the write
|
||||
// must not keep the caller waiting (a settings field on the UI thread, a pass over hundreds of sidecars).
|
||||
var dir = context.CreateTempDirectory();
|
||||
var path = Path.Combine(dir, "settings.json");
|
||||
AtomicFile.WriteAllText(path, "stable");
|
||||
File.SetAttributes(path, FileAttributes.ReadOnly);
|
||||
try
|
||||
{
|
||||
var clock = System.Diagnostics.Stopwatch.StartNew();
|
||||
Assert.Throws<UnauthorizedAccessException>(() => AtomicFile.WriteAllText(path, "new"));
|
||||
Assert.Less(clock.Elapsed.TotalMilliseconds, 500, "no retries");
|
||||
Assert.Equal("stable", File.ReadAllText(path));
|
||||
Assert.Count(1, Directory.GetFiles(dir));
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.SetAttributes(path, FileAttributes.Normal);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadsAreWholeAndShareTheFileWithOtherReaders()
|
||||
{
|
||||
var dir = context.CreateTempDirectory();
|
||||
var path = Path.Combine(dir, "project.json");
|
||||
File.WriteAllText(path, "é");
|
||||
using (new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete))
|
||||
{
|
||||
// A byte order mark is read as File.ReadAllText reads it.
|
||||
Assert.Equal("é", AtomicFile.ReadAllText(path));
|
||||
Assert.SequenceEqual(File.ReadAllBytes(path), AtomicFile.ReadAllBytes(path));
|
||||
}
|
||||
File.WriteAllText(path, string.Empty);
|
||||
Assert.Equal(string.Empty, AtomicFile.ReadAllText(path));
|
||||
Assert.Empty(AtomicFile.ReadAllBytes(path));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FailedWriteKeepsPreviousContent()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Testing;
|
||||
using static Titano.Data.Tests.Archiving.ArchiveFixture;
|
||||
|
||||
namespace Titano.Data.Tests.Archiving;
|
||||
|
||||
/// <summary>
|
||||
/// Thumbnail and proxy passes working on several photos at once (owner review 1, R2: the parallelism decides how many
|
||||
/// photos the running job processes at once): the same images as one at a time, written in frame order, and checkpoints
|
||||
/// that still mean every frame before them is done.
|
||||
/// </summary>
|
||||
[Category("archive")]
|
||||
public sealed class ParallelPassTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task SeveralPhotosAtOnceDrawTheSameThumbnailsInFrameOrder()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
// The pass commits its pack and checkpoints every 200 frames.
|
||||
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 430);
|
||||
await fx.ScanAsync();
|
||||
var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single();
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(location.Id))!;
|
||||
var folder = fx.Paths.Sequence("a");
|
||||
|
||||
var parallel = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", Parallelism = () => 4 });
|
||||
var checkpoints = new List<(int Next, int Total)>();
|
||||
var result = await parallel.Images.BuildThumbnailsAsync(fx.Archive, sequence, false, 0, (next, total) =>
|
||||
{
|
||||
checkpoints.Add((next, total));
|
||||
return ValueTask.CompletedTask;
|
||||
}, fx.Ct);
|
||||
Assert.Equal((430, 0, 0), (result.Built, result.Reused, result.Undecodable));
|
||||
Assert.SequenceEqual([(200, 430), (400, 430)], checkpoints);
|
||||
|
||||
Dictionary<int, byte[]> drawn;
|
||||
using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath))
|
||||
{
|
||||
var entries = pack.Entries.Values.OrderBy(e => e.FrameIndex).ToList();
|
||||
Assert.Count(430, entries);
|
||||
// Appended in frame order, as one at a time.
|
||||
Assert.SequenceEqual(entries.Select(e => e.Offset).Order(), entries.Select(e => e.Offset));
|
||||
drawn = entries.ToDictionary(e => e.FrameIndex, e => pack.Read(e.FrameIndex)!);
|
||||
}
|
||||
|
||||
var single = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", Parallelism = () => 1 });
|
||||
await single.Images.BuildThumbnailsAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath))
|
||||
{
|
||||
foreach (var (index, jpeg) in drawn) Assert.SequenceEqual(jpeg, pack.Read(index)!);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SeveralPhotosAtOnceWriteEveryProxyAndAResumedPassChecksWhatTheEarlierRunDid()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 24);
|
||||
await fx.ScanAsync();
|
||||
var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single();
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(location.Id))!;
|
||||
var folder = fx.Paths.Sequence("a");
|
||||
var parallel = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", Parallelism = () => 6 });
|
||||
|
||||
// Resumed at frame 10: it makes the proxies from there on, and those before it, which the earlier run should have
|
||||
// made, are missing, so the sequence is not built.
|
||||
var resumed = await parallel.Images.BuildProxiesAsync(fx.Archive, sequence, false, 10, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((14, 0, 0), (resumed.Built, resumed.Reused, resumed.Undecodable));
|
||||
Assert.FileMissing(folder.Proxy(0));
|
||||
Assert.FileExists(folder.Proxy(10));
|
||||
Assert.False((await fx.Db.Sequences.GetAsync(location.Id))!.Status.HasFlag(Titano.Core.Model.SequenceStatus.ProxiesBuilt), "not built");
|
||||
|
||||
var full = await parallel.Images.BuildProxiesAsync(fx.Archive, sequence, false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((10, 14), (full.Built, full.Reused));
|
||||
foreach (var frame in ManifestStore.ReadSequence(folder.ManifestPath).Frames)
|
||||
Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(frame.I), frame.CapturedAt, frame.W, frame.H, null), $"proxy of {frame.File}");
|
||||
Assert.True((await fx.Db.Sequences.GetAsync(location.Id))!.Status.HasFlag(Titano.Core.Model.SequenceStatus.ProxiesBuilt), "built");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LargeOriginalsDecodeFewerAtOnceWithinTheMemoryBudget()
|
||||
{
|
||||
// 24 MP takes some 768 MB while it is decoded.
|
||||
const long sensor24 = 6000 * 4000;
|
||||
Assert.Equal(2, DecodeBudget.PhotosAtOnce(8, 2L << 30, sensor24));
|
||||
Assert.Equal(1, DecodeBudget.PhotosAtOnce(8, 512L << 20, sensor24));
|
||||
Assert.Equal(8, DecodeBudget.PhotosAtOnce(8, 64L << 30, sensor24));
|
||||
Assert.Equal(1, DecodeBudget.PhotosAtOnce(1, 64L << 30, sensor24));
|
||||
// Small photos all fit; an unknown size counts as 24 MP.
|
||||
Assert.Equal(8, DecodeBudget.PhotosAtOnce(8, 2L << 30, 48 * 32));
|
||||
Assert.Equal(2, DecodeBudget.PhotosAtOnce(8, 2L << 30, 0));
|
||||
// The size a damaged file claims never overflows: one decode at a time.
|
||||
Assert.Equal(1, DecodeBudget.PhotosAtOnce(8, 64L << 30, 1L << 59));
|
||||
Assert.Equal(1, DecodeBudget.PhotosAtOnce(8, 64L << 30, long.MaxValue));
|
||||
}
|
||||
}
|
||||
@@ -252,63 +252,186 @@ public sealed class JobEngineTests(TestContext context)
|
||||
Assert.Empty((await db.Jobs.ListAsync(null, JobKind.LightAnalyse)).Items);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheNumberOfWorkersChangesWhileTheEngineRuns()
|
||||
/// <summary>Waits until <paramref name="condition"/> holds, failing with <paramref name="what"/> after ten seconds.</summary>
|
||||
private static async Task UntilAsync(Func<bool> condition, string what)
|
||||
{
|
||||
await using var db = await OpenAsync();
|
||||
var running = 0;
|
||||
var started = 0;
|
||||
var release = new SemaphoreSlim(0);
|
||||
var handler = new CallbackHandler(JobKind.Thumbs, async c =>
|
||||
var until = DateTime.UtcNow.AddSeconds(10);
|
||||
while (!condition())
|
||||
{
|
||||
Interlocked.Increment(ref running);
|
||||
Interlocked.Increment(ref started);
|
||||
Assert.True(DateTime.UtcNow < until, what);
|
||||
await Task.Delay(20);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A handler whose jobs run until released, counting those running and started, by kind.</summary>
|
||||
private sealed class GatedHandler(JobKind kind, SemaphoreSlim release) : IJobHandler
|
||||
{
|
||||
public int Running;
|
||||
public int Started;
|
||||
|
||||
public JobKind Kind => kind;
|
||||
|
||||
public async Task RunAsync(JobContext c)
|
||||
{
|
||||
Interlocked.Increment(ref Running);
|
||||
Interlocked.Increment(ref Started);
|
||||
try
|
||||
{
|
||||
await release.WaitAsync(c.Cancellation);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Interlocked.Decrement(ref running);
|
||||
}
|
||||
});
|
||||
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync();
|
||||
var jobs = new List<Job>();
|
||||
for (var i = 0; i < 6; i++) jobs.Add(await engine.EnqueueAsync(JobKind.Thumbs, []));
|
||||
|
||||
async Task UntilAsync(Func<bool> condition, string what)
|
||||
{
|
||||
var until = DateTime.UtcNow.AddSeconds(10);
|
||||
while (!condition())
|
||||
{
|
||||
Assert.True(DateTime.UtcNow < until, what);
|
||||
await Task.Delay(20);
|
||||
Interlocked.Decrement(ref Running);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// One worker: one job at a time.
|
||||
await UntilAsync(() => Volatile.Read(ref running) == 1, "the first job runs");
|
||||
[Test]
|
||||
public async Task JobsRunOneAtATimeAndPausingStartsTheNext()
|
||||
{
|
||||
// Owner review 1 (R2): one job at a time by default; pausing the running job lets the next one start.
|
||||
await using var db = await OpenAsync();
|
||||
var release = new SemaphoreSlim(0);
|
||||
var handler = new GatedHandler(JobKind.Thumbs, release);
|
||||
await using var engine = new JobEngine(db, [handler], new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
Assert.Equal(1, engine.Options.Workers);
|
||||
await engine.StartAsync();
|
||||
var jobs = new List<Job>();
|
||||
for (var i = 0; i < 3; i++) jobs.Add(await engine.EnqueueAsync(JobKind.Thumbs, []));
|
||||
|
||||
await UntilAsync(() => Volatile.Read(ref handler.Running) == 1, "the first job runs");
|
||||
await Task.Delay(300);
|
||||
Assert.Equal(1, Volatile.Read(ref running));
|
||||
Assert.Equal((1, 1), (Volatile.Read(ref handler.Running), Volatile.Read(ref handler.Started)));
|
||||
Assert.Count(1, engine.RunningJobIds);
|
||||
var first = engine.RunningJobIds.First();
|
||||
Assert.Equal("pc", (await db.Jobs.GetAsync(first))!.NodeId);
|
||||
|
||||
// Three workers: two more jobs start at once, and no fourth.
|
||||
engine.SetWorkers(3);
|
||||
Assert.Equal(3, engine.Workers);
|
||||
await UntilAsync(() => Volatile.Read(ref running) == 3, "three jobs run together");
|
||||
await engine.PauseAsync(first);
|
||||
await UntilAsync(() => Volatile.Read(ref handler.Started) == 2 && Volatile.Read(ref handler.Running) == 1, "the next job starts");
|
||||
Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(first))!.State);
|
||||
|
||||
release.Release(10);
|
||||
foreach (var job in jobs.Where(j => j.Id != first)) await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(first))!.State);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task WithoutThePriorityQueueARunningJobIsNeverInterrupted()
|
||||
{
|
||||
await using var db = await OpenAsync();
|
||||
var release = new SemaphoreSlim(0);
|
||||
var scan = new GatedHandler(JobKind.Scan, release);
|
||||
var render = new GatedHandler(JobKind.Render, release);
|
||||
await using var engine = new JobEngine(db, [scan, render], new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync();
|
||||
var scanning = await engine.EnqueueAsync(JobKind.Scan, []);
|
||||
await UntilAsync(() => Volatile.Read(ref scan.Running) == 1, "the scan runs");
|
||||
var rendering = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
|
||||
await Task.Delay(400);
|
||||
Assert.Equal((1, 0), (Volatile.Read(ref scan.Running), Volatile.Read(ref render.Started)));
|
||||
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(rendering.Id))!.State);
|
||||
|
||||
release.Release(10);
|
||||
await WaitForStateAsync(db, scanning.Id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
await WaitForStateAsync(db, rendering.Id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
Assert.Equal(1, Volatile.Read(ref scan.Started));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AHigherJobPausesTheRunningOneWhichResumesFromItsCheckpoint()
|
||||
{
|
||||
// Owner review 1 (R2): with render at the highest level, starting a render while a scan runs pauses the scan
|
||||
// without asking, renders, and resumes the scan by itself where it was.
|
||||
await using var db = await OpenAsync();
|
||||
var checkpointed = new TaskCompletionSource();
|
||||
var go = new SemaphoreSlim(0);
|
||||
var scan = new CountingHandler(JobKind.Scan, 4, async (i, ct) =>
|
||||
{
|
||||
if (i < 2) return;
|
||||
checkpointed.TrySetResult();
|
||||
await go.WaitAsync(ct);
|
||||
});
|
||||
var renderRelease = new SemaphoreSlim(0);
|
||||
var render = new GatedHandler(JobKind.Render, renderRelease);
|
||||
var queue = new JobPriorityQueue { Enabled = true };
|
||||
Assert.Equal((JobKindPriority.High, JobKindPriority.Normal), (queue.LevelOf(JobKind.Render), queue.LevelOf(JobKind.Scan)));
|
||||
await using var engine = new JobEngine(db, [scan, render],
|
||||
new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50), PriorityQueue = () => queue });
|
||||
await engine.StartAsync();
|
||||
var scanning = await engine.EnqueueAsync(JobKind.Scan, []);
|
||||
await checkpointed.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
||||
|
||||
var rendering = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
|
||||
await UntilAsync(() => Volatile.Read(ref render.Running) == 1, "the render starts at once");
|
||||
var paused = await db.Jobs.GetAsync(scanning.Id);
|
||||
Assert.Equal((JobState.Queued, "2", (string?)null), (paused!.State, paused.Checkpoint, paused.NodeId));
|
||||
Assert.Contains(Titano.Core.Localization.Loc.L("Jobs.Kind.Render"), paused.LogTail);
|
||||
|
||||
// The scan waits while the render runs, then resumes from its checkpoint without being asked.
|
||||
go.Release(10);
|
||||
await Task.Delay(300);
|
||||
Assert.Equal(3, Volatile.Read(ref running));
|
||||
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(scanning.Id))!.State);
|
||||
renderRelease.Release();
|
||||
await WaitForStateAsync(db, rendering.Id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
await WaitForStateAsync(db, scanning.Id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
lock (scan.SeenCheckpoints) Assert.SequenceEqual([null, "2"], scan.SeenCheckpoints);
|
||||
Assert.Equal(1, scan.Completed);
|
||||
}
|
||||
|
||||
// Back to one: the running jobs finish undisturbed, then a single worker carries on.
|
||||
engine.SetWorkers(1);
|
||||
release.Release(3);
|
||||
await UntilAsync(() => Volatile.Read(ref started) == 4 && Volatile.Read(ref running) == 1, "one job after the three");
|
||||
await Task.Delay(300);
|
||||
Assert.Equal((4, 1), (Volatile.Read(ref started), Volatile.Read(ref running)));
|
||||
[Test]
|
||||
public async Task OnlyAHigherLevelInterruptsAndNeverTheJobsOwnFollowUps()
|
||||
{
|
||||
await using var db = await OpenAsync();
|
||||
var release = new SemaphoreSlim(0);
|
||||
var queued = new TaskCompletionSource<Job?>();
|
||||
// The light analysis queues a deep analysis of the same sequence, which the user put higher.
|
||||
var light = new CallbackHandler(JobKind.LightAnalyse, async c =>
|
||||
{
|
||||
queued.TrySetResult(await c.EnqueueFollowUpAsync(JobKind.DeepAnalyse, [Guid.NewGuid()], "Roof", null, JobPriority.Background));
|
||||
await release.WaitAsync(c.Cancellation);
|
||||
});
|
||||
var deep = new GatedHandler(JobKind.DeepAnalyse, release);
|
||||
var thumbs = new GatedHandler(JobKind.Thumbs, release);
|
||||
var queue = new JobPriorityQueue
|
||||
{
|
||||
Enabled = true,
|
||||
Levels = new Dictionary<JobKind, JobKindPriority> { [JobKind.DeepAnalyse] = JobKindPriority.High, [JobKind.Thumbs] = JobKindPriority.Normal },
|
||||
};
|
||||
await using var engine = new JobEngine(db, [light, deep, thumbs],
|
||||
new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50), PriorityQueue = () => queue });
|
||||
await engine.StartAsync();
|
||||
var analysing = await engine.EnqueueAsync(JobKind.LightAnalyse, []);
|
||||
var followUp = Assert.NotNull(await queued.Task.WaitAsync(TimeSpan.FromSeconds(10)));
|
||||
// A job of the same level waits too.
|
||||
var drawing = await engine.EnqueueAsync(JobKind.Thumbs, [], priority: JobPriority.User);
|
||||
await Task.Delay(400);
|
||||
Assert.Equal(JobState.Running, (await db.Jobs.GetAsync(analysing.Id))!.State);
|
||||
Assert.Equal((0, 0), (Volatile.Read(ref deep.Started), Volatile.Read(ref thumbs.Started)));
|
||||
Assert.False(await engine.ReviewAsync(context.Cancellation));
|
||||
|
||||
release.Release(3);
|
||||
foreach (var job in jobs) await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => engine.SetWorkers(0));
|
||||
// Another deep analysis, not its own, interrupts it.
|
||||
var other = await engine.EnqueueAsync(JobKind.DeepAnalyse, [Guid.NewGuid()], priority: JobPriority.User);
|
||||
await UntilAsync(() => Volatile.Read(ref deep.Running) == 1, "a higher job runs");
|
||||
Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(analysing.Id))!.State);
|
||||
release.Release(20);
|
||||
foreach (var id in new[] { analysing.Id, followUp.Id, drawing.Id, other.Id }) await WaitForStateAsync(db, id, JobState.Done, TimeSpan.FromSeconds(10));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RequeueingAYieldedJobLeavesPausesAndOtherNodesAlone()
|
||||
{
|
||||
await using var db = await OpenAsync();
|
||||
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Verify, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
|
||||
await db.Jobs.EnqueueAsync(job);
|
||||
Assert.NotNull(await db.Jobs.ClaimNextAsync("pc"));
|
||||
Assert.False(await db.Jobs.RequeueAsync(job.Id, "other"));
|
||||
await db.Jobs.SetStateAsync(job.Id, JobState.Paused);
|
||||
Assert.False(await db.Jobs.RequeueAsync(job.Id, "pc"));
|
||||
Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(job.Id))!.State);
|
||||
await db.Jobs.SetStateAsync(job.Id, JobState.Queued);
|
||||
Assert.NotNull(await db.Jobs.ClaimNextAsync("pc"));
|
||||
Assert.True(await db.Jobs.RequeueAsync(job.Id, "pc"));
|
||||
Assert.Equal((JobState.Queued, (string?)null), ((await db.Jobs.GetAsync(job.Id))!.State, (await db.Jobs.GetAsync(job.Id))!.NodeId));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data.Jobs;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Data.Tests.Jobs;
|
||||
|
||||
/// <summary>The priority queue of the settings (owner review 1, R2): levels per kind of job, the claim order and what interrupts.</summary>
|
||||
[Category("jobs")]
|
||||
public sealed class JobPriorityQueueTests
|
||||
{
|
||||
private static readonly DateTimeOffset T0 = new(2026, 10, 16, 9, 0, 0, TimeSpan.Zero);
|
||||
|
||||
[Test]
|
||||
public void RendersImportsAndDownloadsComeFirstAndTheLongResumablePassesLast()
|
||||
{
|
||||
var queue = new JobPriorityQueue();
|
||||
Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Render));
|
||||
Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Import));
|
||||
Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Download));
|
||||
Assert.Equal(JobKindPriority.Low, queue.LevelOf(JobKind.Verify));
|
||||
Assert.Equal(JobKindPriority.Low, queue.LevelOf(JobKind.Proxies));
|
||||
foreach (var kind in new[] { JobKind.Scan, JobKind.Thumbs, JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Maintenance })
|
||||
Assert.Equal(JobKindPriority.Normal, queue.LevelOf(kind));
|
||||
// A level the user set wins over the default.
|
||||
Assert.Equal(JobKindPriority.Low, (queue with { Levels = new Dictionary<JobKind, JobKindPriority> { [JobKind.Render] = JobKindPriority.Low } }).LevelOf(JobKind.Render));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OnlyAHigherLevelInterruptsAndOnlyWhileTheQueueIsOn()
|
||||
{
|
||||
var on = new JobPriorityQueue { Enabled = true };
|
||||
Assert.True(on.Interrupts(JobKind.Render, JobKind.Scan));
|
||||
Assert.True(on.Interrupts(JobKind.Scan, JobKind.Verify));
|
||||
Assert.False(on.Interrupts(JobKind.Scan, JobKind.Render));
|
||||
Assert.False(on.Interrupts(JobKind.Thumbs, JobKind.Scan));
|
||||
Assert.False(on.Interrupts(JobKind.Import, JobKind.Render));
|
||||
Assert.False(JobPriorityQueue.Off.Interrupts(JobKind.Render, JobKind.Verify));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheQueueOrdersByLevelThenPriorityThenAge()
|
||||
{
|
||||
var on = new JobPriorityQueue { Enabled = true };
|
||||
var oldVerify = Job(JobKind.Verify, JobPriority.User, -30);
|
||||
var backgroundScan = Job(JobKind.Scan, JobPriority.Background, -20);
|
||||
var userThumbs = Job(JobKind.Thumbs, JobPriority.User, -10);
|
||||
var render = Job(JobKind.Render, JobPriority.Normal, 0);
|
||||
Job[] jobs = [oldVerify, backgroundScan, userThumbs, render];
|
||||
|
||||
Assert.SequenceEqual([render, userThumbs, backgroundScan, oldVerify], Sorted(jobs, on));
|
||||
// Off, the strict queue of before: priority, then age.
|
||||
Assert.SequenceEqual([oldVerify, userThumbs, render, backgroundScan], Sorted(jobs, JobPriorityQueue.Off));
|
||||
// Two jobs alike fall back to the id.
|
||||
var twin = render with { Id = Guid.CreateVersion7() };
|
||||
Assert.True(on.ClaimsBefore(render, twin) != on.ClaimsBefore(twin, render));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SettingsKeepOnlyTheLevelsThatDifferAndSkipWhatTheyCannotRead()
|
||||
{
|
||||
var queue = JobPriorityQueue.From(true, new Dictionary<string, string>
|
||||
{
|
||||
["deep-analyse"] = "high",
|
||||
["Scan"] = "LOW",
|
||||
["render"] = "high",
|
||||
["teleport"] = "high",
|
||||
["verify"] = "urgent",
|
||||
["99"] = "low",
|
||||
});
|
||||
Assert.True(queue.Enabled);
|
||||
Assert.Equal((JobKindPriority.High, JobKindPriority.Low, JobKindPriority.Low), (queue.LevelOf(JobKind.DeepAnalyse), queue.LevelOf(JobKind.Scan), queue.LevelOf(JobKind.Verify)));
|
||||
|
||||
var stored = JobPriorityQueue.Changed(Enum.GetValues<JobKind>().ToDictionary(k => k, queue.LevelOf));
|
||||
Assert.SequenceEqual(["deep-analyse", "scan"], stored.Keys.Order(StringComparer.Ordinal));
|
||||
Assert.Equal(("high", "low"), (stored["deep-analyse"], stored["scan"]));
|
||||
var reread = JobPriorityQueue.From(true, stored);
|
||||
foreach (var kind in Enum.GetValues<JobKind>()) Assert.Equal(queue.LevelOf(kind), reread.LevelOf(kind));
|
||||
Assert.Equal(JobKindPriority.High, JobPriorityQueue.From(false, stored).LevelOf(JobKind.DeepAnalyse));
|
||||
Assert.False(JobPriorityQueue.From(false, null).Enabled);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheClaimOrderOfTheDatabaseListsOnlyTheLevelsOtherThanNormal()
|
||||
{
|
||||
var on = new JobPriorityQueue { Enabled = true };
|
||||
Assert.Equal(string.Empty, JobPriorityQueue.Off.OrderSql([JobKind.Render, JobKind.Verify]));
|
||||
Assert.Equal(string.Empty, on.OrderSql([JobKind.Scan, JobKind.Thumbs]));
|
||||
Assert.Equal("CASE WHEN kind IN (7) THEN 2 WHEN kind IN (2) THEN 0 ELSE 1 END DESC, ", on.OrderSql([JobKind.Render, JobKind.Verify, JobKind.Scan, JobKind.Render]));
|
||||
}
|
||||
|
||||
private static Job[] Sorted(Job[] jobs, JobPriorityQueue queue)
|
||||
{
|
||||
var list = jobs.ToList();
|
||||
list.Sort((a, b) => a.Id == b.Id ? 0 : queue.ClaimsBefore(a, b) ? -1 : 1);
|
||||
return [.. list];
|
||||
}
|
||||
|
||||
private static Job Job(JobKind kind, JobPriority priority, int minutes) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = kind,
|
||||
Priority = priority,
|
||||
State = JobState.Queued,
|
||||
Created = T0.AddMinutes(minutes),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
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)]);
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,27 @@ public abstract class JobRepositoryTests(TestContext context) : RepositoryTest(c
|
||||
Assert.Equal(job with { TargetIds = read.TargetIds }, read);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ThePriorityQueueClaimsTheHigherKindsFirst()
|
||||
{
|
||||
// Owner review 1 (R2): on, the level of the kind comes before priority and age; off, the strict queue stays.
|
||||
var db = await OpenTitanoAsync();
|
||||
var oldVerify = TestData.Job(JobKind.Verify, JobPriority.User, TestData.Now.AddMinutes(-30));
|
||||
var backgroundScan = TestData.Job(JobKind.Scan, JobPriority.Background, TestData.Now.AddMinutes(-20));
|
||||
var userThumbs = TestData.Job(JobKind.Thumbs, JobPriority.User, TestData.Now.AddMinutes(-10));
|
||||
var render = TestData.Job(JobKind.Render, JobPriority.Normal, TestData.Now);
|
||||
foreach (var job in new[] { oldVerify, backgroundScan, userThumbs, render }) await db.Jobs.EnqueueAsync(job, Ct);
|
||||
var queue = new Titano.Data.Jobs.JobPriorityQueue { Enabled = true };
|
||||
JobKind[] kinds = [JobKind.Verify, JobKind.Scan, JobKind.Thumbs, JobKind.Render];
|
||||
|
||||
Assert.SequenceEqual([render.Id, userThumbs.Id, backgroundScan.Id, oldVerify.Id], (await db.Jobs.ListQueuedAsync(kinds, 10, queue, Ct)).Select(j => j.Id));
|
||||
Assert.SequenceEqual([oldVerify.Id, userThumbs.Id, render.Id, backgroundScan.Id], (await db.Jobs.ListQueuedAsync(kinds, 10, Ct)).Select(j => j.Id));
|
||||
Assert.Equal(userThumbs.Id, (await db.Jobs.ClaimNextAsync("server", [JobKind.Scan, JobKind.Thumbs, JobKind.Verify], queue, Ct))?.Id);
|
||||
var order = new List<Guid>();
|
||||
while (await db.Jobs.ClaimNextAsync("server", null, queue, Ct) is { } claimed) order.Add(claimed.Id);
|
||||
Assert.SequenceEqual([render.Id, backgroundScan.Id, oldVerify.Id], order);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ClaimsByPriorityThenAgeAndKind()
|
||||
{
|
||||
|
||||
@@ -3,7 +3,10 @@ using System.Net.Http.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data.Jobs;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Hosting;
|
||||
using Titano.Testing;
|
||||
|
||||
@@ -53,6 +56,59 @@ public sealed class AdminTests(TestContext context)
|
||||
Assert.Equal("Language", (await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheParallelismAndThePriorityQueueApplyAtOnce()
|
||||
{
|
||||
// Owner review 1 (R2): jobs run one at a time; the parallelism (photos at once) and the priority queue need no restart.
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var read = await admin.GetAsync("/api/v1/server/settings");
|
||||
var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto);
|
||||
Assert.Equal((2, false), (settings.Parallelism, settings.PriorityQueue));
|
||||
Assert.Equal(ServerSettingsStore.Kinds.Count, settings.JobPriorities.Count);
|
||||
Assert.Equal(("render", "high"), (settings.JobPriorities[0].Kind, settings.JobPriorities[0].Level));
|
||||
Assert.Equal("low", settings.JobPriorities.Single(p => p.Kind == "verify").Level);
|
||||
Assert.DoesNotContain("download", settings.JobPriorities.Select(p => p.Kind));
|
||||
|
||||
// The test server listens on ports the system chose (0), which no request may set.
|
||||
var edited = settings with
|
||||
{
|
||||
HttpPort = 18080,
|
||||
HttpsPort = 18443,
|
||||
Parallelism = 3,
|
||||
PriorityQueue = true,
|
||||
JobPriorities = [.. settings.JobPriorities.Select(p => p.Kind == "scan" ? p with { Level = "high" } : p)],
|
||||
};
|
||||
using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
|
||||
await saved.ExpectAsync(HttpStatusCode.OK);
|
||||
// Applied at once, while the ports wait for the restart.
|
||||
Assert.Equal((3, true, 0), (server.Options.Parallelism, server.Options.PriorityQueue, server.Options.HttpPort));
|
||||
var queue = ServerSettingsStore.PriorityQueue(server.Options);
|
||||
Assert.True(queue.Enabled);
|
||||
Assert.Equal((JobKindPriority.High, JobKindPriority.High), (queue.LevelOf(JobKind.Scan), queue.LevelOf(JobKind.Render)));
|
||||
// Only the level that differs from the defaults is stored.
|
||||
var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject();
|
||||
Assert.Equal((3, true), (file["Parallelism"]!.GetValue<int>(), file["PriorityQueue"]!.GetValue<bool>()));
|
||||
Assert.Equal("""{"scan":"high"}""", file["JobPriorities"]!.ToJsonString());
|
||||
Assert.False(file.ContainsKey("Workers"));
|
||||
|
||||
using (var dashboard = await admin.GetAsync("/api/v1/server/dashboard"))
|
||||
{
|
||||
var d = await dashboard.ReadAsync(ProtocolJson.Default.DashboardDto);
|
||||
Assert.Equal((3, true), (d.Parallelism, d.PriorityQueue));
|
||||
}
|
||||
foreach (var (invalid, setting) in new (ServerSettingsDto, string)[]
|
||||
{
|
||||
(edited with { Parallelism = 0 }, "Parallelism"),
|
||||
(edited with { JobPriorities = [new JobPriorityDto("download", "high")] }, "JobPriorities"),
|
||||
(edited with { JobPriorities = [new JobPriorityDto("scan", "urgent")] }, "JobPriorities"),
|
||||
})
|
||||
{
|
||||
using var refused = await admin.PutAsJsonAsync("/api/v1/server/settings", invalid, ProtocolJson.Default.ServerSettingsDto);
|
||||
Assert.Equal(setting, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LogsAreReadableButOnlyByName()
|
||||
{
|
||||
|
||||
@@ -75,6 +75,37 @@ public sealed class StartupTests(TestContext context)
|
||||
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "many" }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheFormerWorkersSettingStillSetsTheParallelism()
|
||||
{
|
||||
// Owner review 1: "workers" (jobs at once) became the parallelism (photos the running job processes at once).
|
||||
var root = context.CreateTempDirectory("workers");
|
||||
File.WriteAllText(Path.Combine(root, "server.json"), """{ "Workers": 6 }""");
|
||||
Assert.Equal(6, ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).Parallelism);
|
||||
|
||||
var former = ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "5" });
|
||||
Assert.Equal((5, true), (former.Parallelism, former.FormerWorkersVariable));
|
||||
Assert.SequenceEqual(["Database", "Parallelism"], former.Locked);
|
||||
var both = ServerOptions.Load(new Dictionary<string, string?>
|
||||
{
|
||||
["TITANO_DATA_PATH"] = root,
|
||||
["TITANO_DB"] = "postgres",
|
||||
["TITANO_WORKERS"] = "5",
|
||||
["TITANO_PARALLELISM"] = "7",
|
||||
["TITANO_PRIORITY_QUEUE"] = "true",
|
||||
});
|
||||
Assert.Equal((7, true), (both.Parallelism, both.PriorityQueue));
|
||||
Assert.SequenceEqual(["Database", "Parallelism", "PriorityQueue"], both.Locked);
|
||||
Assert.False(ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PARALLELISM"] = "2" })
|
||||
.FormerWorkersVariable);
|
||||
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PRIORITY_QUEUE"] = "often" }));
|
||||
// A value the admin panel would refuse is brought within its range, so the panel can still save the other settings.
|
||||
Assert.Equal(ServerOptions.MaxParallelism,
|
||||
ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PARALLELISM"] = "96" }).Parallelism);
|
||||
Assert.Equal(1, ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "0" }).Parallelism);
|
||||
Assert.InRange(new ServerOptions().Parallelism, 1, ServerOptions.MaxParallelism);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TrustedProxiesAreAddressesOrNetworks()
|
||||
{
|
||||
|
||||
@@ -64,7 +64,7 @@ public sealed class JobApiTests(TestContext context)
|
||||
public async Task WaitingJobsChangePriorityAndRightsAreChecked()
|
||||
{
|
||||
var handler = new BlockingJobHandler(JobKind.Thumbs);
|
||||
await using var server = await TestServer.StartAsync(context, o => o.Workers = 1, s => s.AddSingleton<IJobHandler>(handler));
|
||||
await using var server = await TestServer.StartAsync(context, o => o.Parallelism = 1, s => s.AddSingleton<IJobHandler>(handler));
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
|
||||
var sequence = await CreateSequenceAsync(editor);
|
||||
|
||||
@@ -35,6 +35,21 @@ public sealed class ProcessingModuleTests(TestContext context)
|
||||
ProcessingModule.Register(services);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheRunningJobHasTheParallelismAndTheHostsMemoryBudget()
|
||||
{
|
||||
// Owner review 1: one job at a time, the parallelism working inside it, its frame window sized to the host.
|
||||
await using var server = await TestServer.StartAsync(context, o => o.Parallelism = 3, Production);
|
||||
var processing = server.Services.GetRequiredService<ProcessingService>();
|
||||
var tuned = processing.Options.Tune!(processing.Options);
|
||||
Assert.Equal((3, 3, 3), (tuned.DeepAnalysis.MaxDegreeOfParallelism, tuned.FrameWindow.MaxConcurrentLoads, processing.Options.Archive.Parallelism()));
|
||||
Assert.Equal(processing.Options.Archive.MemoryBudget(), tuned.FrameWindow.MemoryBudget);
|
||||
Assert.True(tuned.FrameWindow.MemoryBudget >= 1L << 30, "at least the window's own default");
|
||||
// A change in the admin panel applies to the next job.
|
||||
server.Options.Parallelism = 5;
|
||||
Assert.Equal(5, processing.Options.Tune!(processing.Options).FrameWindow.MaxConcurrentLoads);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UploadedFramesAreLightAnalysedAndAdvised()
|
||||
{
|
||||
|
||||
@@ -61,7 +61,7 @@ public sealed class TestServer : IAsyncDisposable
|
||||
TlsMode = TlsMode.Auto,
|
||||
Database = DatabaseKind.Sqlite,
|
||||
Discovery = false,
|
||||
Workers = 2,
|
||||
Parallelism = 2,
|
||||
AdminUser = "admin",
|
||||
AdminPassword = AdminPassword,
|
||||
// Tests sign in many times from one address; the rate-limit test sets the production value.
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Titano.Tools.Tests;
|
||||
|
||||
/// <summary><c>i18n check</c>: English texts that count something need plural forms (BRIEF §16), and every error code
|
||||
/// of the server has its message, whose key is built at run time.</summary>
|
||||
public sealed class I18nCommandTests
|
||||
public sealed class I18nCommandTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public void ANumberInFrontOfACountedNounNeedsPluralForms()
|
||||
@@ -59,6 +59,32 @@ public sealed class I18nCommandTests
|
||||
Assert.Null(I18nCommand.CountedTally("Import.Result.Skipped.other", "{0:N0} copied, {1:N0} skipped"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PruneRemovesOnlyTheKeysEnglishNoLongerHas()
|
||||
{
|
||||
var dir = context.CreateTempDirectory();
|
||||
void Write(string culture, string file, params (string Key, string Value)[] entries)
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(dir, culture));
|
||||
var data = string.Concat(entries.Select(e => $" <data name=\"{e.Key}\" xml:space=\"preserve\"><value>{e.Value}</value></data>\n"));
|
||||
File.WriteAllText(Path.Combine(dir, culture, file), $"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n{data}</root>\n");
|
||||
}
|
||||
Write("en", "Jobs.resx", ("Jobs.Kind.Render", "Render"), ("Jobs.Parallelism.one", "{0} photo at once"), ("Jobs.Parallelism.other", "{0} photos at once"));
|
||||
Write("it", "Jobs.it.resx", ("Jobs.Kind.Render", "Render"), ("Jobs.Workers", "Processi {0} / {1}"), ("Jobs.Parallelism.one", "{0} foto alla volta"));
|
||||
// Polish adds plural forms English has not: they stay.
|
||||
Write("pl", "Jobs.pl.resx", ("Jobs.Parallelism.few", "{0} zdjęcia naraz"), ("Jobs.Parallelism.many", "{0} zdjęć naraz"));
|
||||
var untouched = File.ReadAllText(Path.Combine(dir, "pl", "Jobs.pl.resx"));
|
||||
|
||||
Assert.Equal(1, I18nCommand.PruneOrphans(dir, I18nCommand.Catalog.Load(dir)));
|
||||
|
||||
var italian = I18nCommand.Catalog.Load(dir).Cultures["it"];
|
||||
Assert.SequenceEqual(["Jobs.Kind.Render", "Jobs.Parallelism.one"], italian.Keys.Order(StringComparer.Ordinal));
|
||||
Assert.Equal("{0} foto alla volta", italian["Jobs.Parallelism.one"].Value);
|
||||
Assert.Equal(untouched, File.ReadAllText(Path.Combine(dir, "pl", "Jobs.pl.resx")));
|
||||
Assert.Equal(3, I18nCommand.Catalog.Load(dir).Cultures["en"].Count);
|
||||
Assert.Equal(0, I18nCommand.PruneOrphans(dir, I18nCommand.Catalog.Load(dir)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorCodesAreTheLiteralsPassedToTheServerErrorHelpers()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user