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
@@ -31,7 +31,7 @@ public sealed class IndexSnapshot
|
||||
try
|
||||
{
|
||||
if (!File.Exists(path)) return null;
|
||||
data = File.ReadAllBytes(path);
|
||||
data = AtomicFile.ReadAllBytes(path);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
|
||||
@@ -32,7 +32,7 @@ public sealed record IngestJournal(IReadOnlyList<SidecarFrame> Frames)
|
||||
IngestJournal? journal;
|
||||
try
|
||||
{
|
||||
journal = JsonSerializer.Deserialize(File.ReadAllBytes(sequence.IngestJournalPath), ArchiveJson.Default.IngestJournal);
|
||||
journal = JsonSerializer.Deserialize(AtomicFile.ReadAllBytes(sequence.IngestJournalPath), ArchiveJson.Default.IngestJournal);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
|
||||
@@ -28,6 +28,9 @@ public sealed class LeaseFile : IDisposable
|
||||
private const string Extension = ".lease";
|
||||
private const string StaleExtension = ".stale";
|
||||
private const int ReadAttempts = 5;
|
||||
|
||||
/// <summary>Creates tried, some 130 ms in all, while a released lease's name is still taken by a reader (<see cref="TryCreate"/>).</summary>
|
||||
private const int CreateAttempts = 8;
|
||||
private static readonly TimeSpan ReadRetryDelay = TimeSpan.FromMilliseconds(20);
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Timer _renewal;
|
||||
@@ -202,10 +205,16 @@ public sealed class LeaseFile : IDisposable
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (IOException) when (attempt < 3)
|
||||
catch (IOException) when (attempt < CreateAttempts)
|
||||
{
|
||||
// The holder released the lease between the failed create and the check: the name is free again.
|
||||
}
|
||||
catch (UnauthorizedAccessException) when (attempt < CreateAttempts)
|
||||
{
|
||||
// On Windows a lease released while a reader still holds it open (readers share deletion) stays pending
|
||||
// deletion, and creating its name answers "access denied" until the reader lets go, a moment later.
|
||||
Thread.Sleep(1 << (attempt - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ public sealed class NewerFormatException(string path, int version)
|
||||
public static class ManifestStore
|
||||
{
|
||||
/// <exception cref="InvalidDataException">The file is not valid JSON or lacks required fields.</exception>
|
||||
public static ArchiveManifest ReadArchive(string path) => Parse(File.ReadAllBytes(path), path, ArchiveJson.Default.ArchiveManifest);
|
||||
public static ArchiveManifest ReadArchive(string path) => Parse(AtomicFile.ReadAllBytes(path), path, ArchiveJson.Default.ArchiveManifest);
|
||||
|
||||
public static void WriteArchive(string path, ArchiveManifest manifest)
|
||||
{
|
||||
@@ -28,7 +28,7 @@ public static class ManifestStore
|
||||
}
|
||||
|
||||
/// <exception cref="InvalidDataException">The file is not valid JSON or lacks required fields.</exception>
|
||||
public static SequenceManifest ReadSequence(string path) => ParseSequence(File.ReadAllBytes(path), path);
|
||||
public static SequenceManifest ReadSequence(string path) => ParseSequence(AtomicFile.ReadAllBytes(path), path);
|
||||
|
||||
/// <summary>Parses sidecar bytes (from disk or from the index snapshot).</summary>
|
||||
public static SequenceManifest ParseSequence(ReadOnlySpan<byte> utf8, string source) => Parse(utf8, source, ArchiveJson.Default.SequenceManifest);
|
||||
|
||||
@@ -4,10 +4,45 @@ namespace Titano.Core.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Crash-safe writes: content goes to a temporary sibling, is flushed to disk, then renamed over the
|
||||
/// target (BRIEF §7 "temp → fsync → rename").
|
||||
/// target (BRIEF §7 "temp → fsync → rename"). The readers of sidecars and projects take no lease (a thumbnail pass reads
|
||||
/// a sidecar while an upload commits to it), and Windows refuses to rename over a file someone has open: a write tries the
|
||||
/// rename again for a moment (<see cref="Replace"/>), and <see cref="ReadAllBytes"/> and its siblings read such files whole
|
||||
/// and at once, sharing deletion where the file system allows a rename over an open file.
|
||||
/// </summary>
|
||||
public static class AtomicFile
|
||||
{
|
||||
/// <summary>Renames tried, some 1 s in all, before a replace held by a reader or another program (an indexer, an antivirus) fails.</summary>
|
||||
private const int ReplaceAttempts = 15;
|
||||
|
||||
private const int SharingViolation = unchecked((int)0x80070020);
|
||||
private const int LockViolation = unchecked((int)0x80070021);
|
||||
|
||||
/// <summary>The whole file as it was when opened, read so that a writer may replace it meanwhile.</summary>
|
||||
/// <exception cref="IOException">The file is larger than an array holds (2 GB), as <see cref="File.ReadAllBytes"/> says.</exception>
|
||||
public static byte[] ReadAllBytes(string path)
|
||||
{
|
||||
using var stream = OpenShared(path, FileOptions.SequentialScan);
|
||||
var bytes = new byte[CheckedLength(stream, path)];
|
||||
stream.ReadExactly(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ReadAllBytes"/>
|
||||
public static async Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var stream = OpenShared(path, FileOptions.SequentialScan | FileOptions.Asynchronous);
|
||||
var bytes = new byte[CheckedLength(stream, path)];
|
||||
await stream.ReadExactlyAsync(bytes, cancellationToken).ConfigureAwait(false);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/// <summary>The file's text (UTF-8 unless it starts with another byte order mark), read as <see cref="ReadAllBytes"/>.</summary>
|
||||
public static string ReadAllText(string path)
|
||||
{
|
||||
using var reader = new StreamReader(OpenShared(path, FileOptions.SequentialScan), Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
public static void WriteAllBytes(string path, ReadOnlySpan<byte> content)
|
||||
{
|
||||
var temp = TempPathFor(path);
|
||||
@@ -18,7 +53,7 @@ public static class AtomicFile
|
||||
fs.Write(content);
|
||||
fs.Flush(flushToDisk: true);
|
||||
}
|
||||
File.Move(temp, path, overwrite: true);
|
||||
Replace(temp, path);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -41,7 +76,7 @@ public static class AtomicFile
|
||||
await fs.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
fs.Flush(flushToDisk: true);
|
||||
}
|
||||
File.Move(temp, path, overwrite: true);
|
||||
await ReplaceAsync(temp, path, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -55,7 +90,7 @@ public static class AtomicFile
|
||||
{
|
||||
using (var fs = new FileStream(tempPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
||||
fs.Flush(flushToDisk: true);
|
||||
File.Move(tempPath, path, overwrite: true);
|
||||
Replace(tempPath, path);
|
||||
}
|
||||
|
||||
public static string TempPathFor(string path)
|
||||
@@ -65,6 +100,70 @@ public static class AtomicFile
|
||||
return Path.Combine(dir, "." + Path.GetFileName(path) + "." + Guid.NewGuid().ToString("N")[..8] + ".tmp");
|
||||
}
|
||||
|
||||
/// <summary>Opened for reading whole: other readers and a rename over it may come, a writer in place may not.</summary>
|
||||
private static FileStream OpenShared(string path, FileOptions options) =>
|
||||
new(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 1 << 16, options);
|
||||
|
||||
private static int CheckedLength(FileStream stream, string path) =>
|
||||
stream.Length <= Array.MaxLength ? (int)stream.Length : throw new IOException($"{path} is larger than 2 GB.");
|
||||
|
||||
/// <summary>
|
||||
/// Renames <paramref name="temp"/> over <paramref name="path"/>, trying again for a moment while another handle holds
|
||||
/// the target (<see cref="Holds"/>); anything else fails at once.
|
||||
/// </summary>
|
||||
private static void Replace(string temp, string path)
|
||||
{
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Move(temp, path, overwrite: true);
|
||||
return;
|
||||
}
|
||||
catch (Exception ex) when (attempt < ReplaceAttempts && Holds(ex, temp, path))
|
||||
{
|
||||
Thread.Sleep(10 * attempt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Replace"/>
|
||||
private static async Task ReplaceAsync(string temp, string path, CancellationToken cancellationToken)
|
||||
{
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Move(temp, path, overwrite: true);
|
||||
return;
|
||||
}
|
||||
catch (Exception ex) when (attempt < ReplaceAttempts && Holds(ex, temp, path))
|
||||
{
|
||||
await Task.Delay(10 * attempt, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the rename failed because another handle holds the target, which clears once it closes: a sharing or lock
|
||||
/// violation, or the "access denied" Windows answers for an open file that is neither read-only nor a folder (those
|
||||
/// never clear, so they fail at once, as a missing file or folder does).
|
||||
/// </summary>
|
||||
private static bool Holds(Exception ex, string temp, string path)
|
||||
{
|
||||
if (!File.Exists(temp)) return false;
|
||||
if (ex is IOException { HResult: SharingViolation or LockViolation }) return true;
|
||||
if (ex is not UnauthorizedAccessException) return false;
|
||||
try
|
||||
{
|
||||
return (File.GetAttributes(path) & (FileAttributes.ReadOnly | FileAttributes.Directory)) == 0;
|
||||
}
|
||||
catch (Exception e) when (e is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryDelete(string path)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in new issue
Block a user