fix(convert): reserve the photos still writing, keep photo ids out of the job row, never copy a source twice

Stage A's space guard reserved, before each photo, only that photo and the
ones after it, so at the end of a sequence it counted a single photo while
up to twice the parallelism were still converting: a volume filling
meanwhile could go into the reserve. It now reserves the photo and every
photo started whose converted file has not landed yet (a file lands whole,
by a rename, before it is verified), so nothing in flight is missed and
nothing landed is counted twice, at the end of a sequence as anywhere.

The confirmed photo ids (about 21 MB for a million photos) lived in the
job's parameters, read by every dry run's busy check, by each sequence turn
of the conversion and by the job lists. They are now written whole, under a
job id chosen before the job is queued, into convert-<job>.photos beside
the journal, and go with it; the row keeps each sequence's digest, so the
job still converts exactly the photos confirmed (without the file, the
whole plan made again must match). The busy check reads only ids, kinds and
targets (and a verify's parameters), never the asking job's row; a
conversion's turn reads only its creation time; the server's job list and
dashboard and the engine's queue peeks read jobs without parameters.

A retried New sequence conversion could make a second copy. The journal now
names the copy as soon as its photos are filed; a retry finishes that copy
whatever changed in the source since, never remakes one deleted since, and
needs no space again for what it holds; and a source another conversion
copied in the same format after this one was confirmed is done with, with
no copy made (new outcome Superseded, with its job log line).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 09:36:00 +01:00
1 parent b471a33bfc
commit 8ea25228eb
14 files changed
+966 -201

No files matched your search

@@ -56,55 +56,29 @@ public sealed record MoveSequence(Guid Id);
/// <see cref="JobKind.Convert"/> (owner review 2, R7): targets = the sequences converted, so the jobs that would touch them
/// wait; the archive, the target format and JPEG quality, the mode chosen for this run, the sequences of the confirmed plan
/// in its order, each with what it converts, and the plan's fingerprint (<see cref="ConvertPlan.Fingerprint"/>). A sequence
/// whose photos to convert are no longer those confirmed is left as it is and reported.
/// whose photos to convert are no longer those confirmed is left as it is and reported. Which photos those are is not
/// here but in the job's own file beside its journal (<see cref="ConfirmedPhotos"/>), so that the row stays as small as the
/// number of sequences, whatever the number of photos, for everything that reads jobs.
/// </summary>
public sealed record ConvertJobParameters(Guid ArchiveId, ConvertTarget Target, int Quality, ConvertMode Mode, IReadOnlyList<ConvertSequence> Sequences,
string Fingerprint);
/// <summary>
/// A sequence of a <see cref="ConvertJobParameters"/>: the frames version it was planned at, the digest of the photos it
/// converts, each with its content (<see cref="ConvertSequencePlan.Digest"/>), and which photos those are (<see cref="Photos"/>).
/// Under the sequence's lease, before it converts anything, the job takes those photos from its plan made again and goes
/// on only while they have the digest confirmed: photos that came in since (an upload while the job waited its turn) stay
/// as they are, and a photo the plan did not list (one another program held while it was made) is never converted. Null
/// in a job an earlier build queued, which checks the frames version only (no digest), or the whole plan's digest (no photos).
/// A sequence of a <see cref="ConvertJobParameters"/>: the frames version it was planned at, and the digest of the photos it
/// converts, each with its content (<see cref="ConvertSequencePlan.Digest"/>). Under the sequence's lease, before it
/// converts anything, the job takes the photos confirmed (<see cref="ConfirmedPhotos"/>) from its plan made again and goes
/// on only while they have that digest: photos that came in since (an upload while the job waited its turn) stay as they
/// are, and a photo the plan did not list (one another program held while it was made) is never converted. Without the
/// confirmed photos, the whole plan made again must have the digest. Null in a job an earlier build queued, which checks
/// the frames version only.
/// </summary>
/// <param name="Photos">The frame ids of the photos converted, in the plan's order, as <see cref="EncodePhotos"/> writes them.</param>
public sealed record ConvertSequence(Guid Id, int FramesVersion, string? Frames = null, string? Photos = null)
{
/// <summary>
/// Frame ids as a job's parameters hold them: their 16 bytes each, one after the other, in base64 (a whole-archive
/// conversion lists every photo it converts, so the job row stays about half the size of a list of ids).
/// </summary>
public static string EncodePhotos(IEnumerable<Guid> ids)
{
ArgumentNullException.ThrowIfNull(ids);
IReadOnlyCollection<Guid> frames = ids as IReadOnlyCollection<Guid> ?? [.. ids];
var bytes = new byte[frames.Count * 16];
var at = 0;
foreach (var frame in frames)
{
frame.TryWriteBytes(bytes.AsSpan(at, 16));
at += 16;
}
return Convert.ToBase64String(bytes);
}
public sealed record ConvertSequence(Guid Id, int FramesVersion, string? Frames = null);
/// <summary>The frame ids <see cref="Photos"/> holds, in order; null when it holds none (a job an earlier build queued).</summary>
/// <exception cref="FormatException">The text is not ids as <see cref="EncodePhotos"/> writes them.</exception>
public IReadOnlyList<Guid>? PhotoIds()
{
if (Photos is null) return null;
var bytes = Convert.FromBase64String(Photos);
if (bytes.Length % 16 != 0) throw new FormatException($"The photos of sequence {Id} are not frame ids.");
var ids = new Guid[bytes.Length / 16];
for (var i = 0; i < ids.Length; i++) ids[i] = new Guid(bytes.AsSpan(i * 16, 16));
return ids;
}
}
/// <summary>A job ready to enqueue.</summary>
public sealed record ArchiveJobRequest(JobKind Kind, IReadOnlyList<Guid> Targets, string? Label, string? ParametersJson, JobPriority Priority);
/// <summary>
/// A job ready to enqueue; <paramref name="Id"/>, when set, is the id it is queued under (chosen first, for what the job
/// reads beside its journal: <see cref="ConfirmedPhotos"/>), otherwise the engine gives it one.
/// </summary>
public sealed record ArchiveJobRequest(JobKind Kind, IReadOnlyList<Guid> Targets, string? Label, string? ParametersJson, JobPriority Priority, Guid? Id = null);
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true)]
[JsonSerializable(typeof(ScanParameters))]
@@ -120,6 +94,7 @@ public sealed record ArchiveJobRequest(JobKind Kind, IReadOnlyList<Guid> Targets
[JsonSerializable(typeof(ConvertJournal))]
[JsonSerializable(typeof(StagedLine))]
[JsonSerializable(typeof(PinnedSequence))]
[JsonSerializable(typeof(ConfirmedLine))]
[JsonSerializable(typeof(CardProfile))]
internal sealed partial class ArchiveJobJson : JsonSerializerContext
{