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:
1 parent
b471a33bfc
commit
8ea25228eb
14 files changed
+966
-201
No files matched your search
@@ -7,6 +7,12 @@ namespace Chronos.Data.Repositories;
|
||||
/// <summary>Jobs per state; <see cref="DoneSince"/> and <see cref="FailedSince"/> count those finished since a given time.</summary>
|
||||
public sealed record JobCounts(int Queued, int Running, int Paused, int DoneSince, int FailedSince);
|
||||
|
||||
/// <summary>
|
||||
/// What a running or paused job works on (<see cref="JobRepository.ListHoldsAsync"/>): its targets and, for a verify, its
|
||||
/// parameters, which name the sequences it holds; null for every other kind, whose parameters are not read.
|
||||
/// </summary>
|
||||
public sealed record JobHold(Guid Id, JobKind Kind, IReadOnlyList<Guid> TargetIds, string? ParametersJson);
|
||||
|
||||
/// <summary>
|
||||
/// The persistent job queue (BRIEF §14.6): workers claim the oldest job of the highest priority atomically,
|
||||
/// write checkpoints while running, and on startup requeue what a crash left running.
|
||||
@@ -17,6 +23,14 @@ public sealed class JobRepository
|
||||
"id, kind, target_ids, target_label, parameters_json, state, priority, progress_done, progress_total, items_per_second, " +
|
||||
"eta_seconds, checkpoint, log_tail, created_by, node_id, attempts, error, created, started, finished, schedule_id";
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Columns"/> as lists read them (<see cref="ListBriefAsync"/>): the parameters left out, null. A conversion's or
|
||||
/// an import's name every sequence or file they work on; the job reads them, and so do the screens that act on that one job.
|
||||
/// </summary>
|
||||
private const string BriefColumns =
|
||||
"id, kind, target_ids, target_label, CAST(NULL AS TEXT), state, priority, progress_done, progress_total, items_per_second, " +
|
||||
"eta_seconds, checkpoint, log_tail, created_by, node_id, attempts, error, created, started, finished, schedule_id";
|
||||
|
||||
/// <summary>The states of a job that has not ended: queued, running, paused.</summary>
|
||||
private static readonly string PendingStates =
|
||||
string.Join(", ", new[] { JobState.Queued, JobState.Running, JobState.Paused }.Select(s => ((int)s).ToString(CultureInfo.InvariantCulture)));
|
||||
@@ -37,6 +51,11 @@ public sealed class JobRepository
|
||||
public Task<Job?> GetAsync(Guid id, CancellationToken cancellationToken = default) =>
|
||||
_db.SingleAsync($"SELECT {Columns} FROM jobs WHERE id = $1", [id], Map, cancellationToken);
|
||||
|
||||
/// <summary>When the job <paramref name="id"/> was queued; null when there is no such job. Reads that column alone.</summary>
|
||||
public async Task<DateTimeOffset?> GetCreatedAsync(Guid id, CancellationToken cancellationToken = default) =>
|
||||
(await _db.ListAsync("SELECT created FROM jobs WHERE id = $1", [id], r => r.GetDateTimeOffset(0), cancellationToken).ConfigureAwait(false))
|
||||
is [var created] ? created : null;
|
||||
|
||||
/// <summary>
|
||||
/// Marks the next queued job (highest priority, then oldest) of the given kinds as running for
|
||||
/// <paramref name="workerId"/> and returns it, or null when none waits. Concurrent claimers never get the same
|
||||
@@ -93,7 +112,17 @@ public sealed class JobRepository
|
||||
ListAsync(state, kind: null, limit, cursor, cancellationToken);
|
||||
|
||||
/// <summary>Jobs, newest first, optionally of one state and one kind.</summary>
|
||||
public Task<Page<Job>> ListAsync(JobState? state, JobKind? kind, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default)
|
||||
public Task<Page<Job>> ListAsync(JobState? state, JobKind? kind, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default) =>
|
||||
PageAsync(Columns, state, kind, limit, cursor, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ListAsync(JobState?, JobKind?, int, string?, CancellationToken)"/> as lists show jobs: every field but
|
||||
/// <see cref="Job.ParametersJson"/>, which stays null (<see cref="GetAsync"/> reads a job whole).
|
||||
/// </summary>
|
||||
public Task<Page<Job>> ListBriefAsync(JobState? state, JobKind? kind, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default) =>
|
||||
PageAsync(BriefColumns, state, kind, limit, cursor, cancellationToken);
|
||||
|
||||
private Task<Page<Job>> PageAsync(string columns, JobState? state, JobKind? kind, int limit, string? cursor, CancellationToken cancellationToken)
|
||||
{
|
||||
var p = new SqlParameters();
|
||||
var where = new List<string>();
|
||||
@@ -105,7 +134,7 @@ public sealed class JobRepository
|
||||
where.Add(Cursor.After("created", "id", descending: true, value, id, p.Add));
|
||||
}
|
||||
var take = Cursor.ClampLimit(limit);
|
||||
var sql = $"SELECT {Columns} FROM jobs{(where.Count > 0 ? " WHERE " + string.Join(" AND ", where) : string.Empty)} " +
|
||||
var sql = $"SELECT {columns} FROM jobs{(where.Count > 0 ? " WHERE " + string.Join(" AND ", where) : string.Empty)} " +
|
||||
$"ORDER BY created DESC, id DESC LIMIT {p.Add(take + 1)}";
|
||||
return _db.RunAsync(async session =>
|
||||
{
|
||||
@@ -158,11 +187,22 @@ public sealed class JobRepository
|
||||
ListQueuedAsync(kinds, limit, JobPriorityQueue.Off, cancellationToken);
|
||||
|
||||
/// <summary>Queued jobs of the given kinds in the claim order of <paramref name="queue"/>.</summary>
|
||||
public Task<IReadOnlyList<Job>> ListQueuedAsync(IReadOnlyCollection<JobKind> kinds, int limit, JobPriorityQueue queue, CancellationToken cancellationToken = default)
|
||||
public Task<IReadOnlyList<Job>> ListQueuedAsync(IReadOnlyCollection<JobKind> kinds, int limit, JobPriorityQueue queue, CancellationToken cancellationToken = default) =>
|
||||
QueuedAsync(Columns, kinds, limit, queue, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ListQueuedAsync(IReadOnlyCollection{JobKind}, int, JobPriorityQueue, CancellationToken)"/> without the
|
||||
/// parameters (null), for a look at what would be claimed next: claiming a job reads it whole.
|
||||
/// </summary>
|
||||
public Task<IReadOnlyList<Job>> ListQueuedBriefAsync(IReadOnlyCollection<JobKind> kinds, int limit, JobPriorityQueue queue, CancellationToken cancellationToken = default) =>
|
||||
QueuedAsync(BriefColumns, kinds, limit, queue, cancellationToken);
|
||||
|
||||
private Task<IReadOnlyList<Job>> QueuedAsync(string columns, IReadOnlyCollection<JobKind> kinds, int limit, JobPriorityQueue queue, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(kinds);
|
||||
ArgumentNullException.ThrowIfNull(queue);
|
||||
return kinds.Count == 0 ? Task.FromResult<IReadOnlyList<Job>>([]) : _db.ListAsync(
|
||||
$"SELECT {Columns} FROM jobs WHERE state = {(int)JobState.Queued} AND kind IN ({KindList(kinds)}) ORDER BY {queue.OrderSql(kinds)}priority DESC, created, id LIMIT $1",
|
||||
$"SELECT {columns} FROM jobs WHERE state = {(int)JobState.Queued} AND kind IN ({KindList(kinds)}) ORDER BY {queue.OrderSql(kinds)}priority DESC, created, id LIMIT $1",
|
||||
[Cursor.ClampLimit(limit)], Map, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -195,6 +235,18 @@ public sealed class JobRepository
|
||||
return await _db.ScalarAsync<long>(sql, p.ToArray(), cancellationToken).ConfigureAwait(false) > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What the running and paused jobs hold, but the job <paramref name="except"/> (the one asking): each one's id, kind and
|
||||
/// targets, and only a verify's parameters (<see cref="JobHold"/>). Neither the other parameters nor the targets of
|
||||
/// <paramref name="except"/> are read: a conversion asks at every sequence it comes to, and its own row names them all.
|
||||
/// </summary>
|
||||
public Task<IReadOnlyList<JobHold>> ListHoldsAsync(Guid? except = null, CancellationToken cancellationToken = default) =>
|
||||
_db.ListAsync(
|
||||
$"SELECT id, kind, target_ids, CASE WHEN kind = {(int)JobKind.Verify} THEN parameters_json END FROM jobs " +
|
||||
$"WHERE state IN ({(int)JobState.Running}, {(int)JobState.Paused}){(except is null ? string.Empty : " AND id <> $1")} ORDER BY created, id",
|
||||
except is { } self ? [self] : [],
|
||||
r => new JobHold(r.GetGuid(0), (JobKind)r.GetInt32(1), Targets(r.GetString(2)), r.GetStringOrNull(3)), cancellationToken);
|
||||
|
||||
/// <summary>The targets of the queued, running and paused jobs of <paramref name="kind"/> (the newest 5000 such jobs).</summary>
|
||||
public Task<HashSet<Guid>> ListPendingTargetsAsync(JobKind kind, CancellationToken cancellationToken = default) =>
|
||||
PendingTargetsAsync(kind, null, cancellationToken);
|
||||
@@ -306,11 +358,14 @@ public sealed class JobRepository
|
||||
|
||||
private static string KindList(IEnumerable<JobKind> kinds) => string.Join(", ", kinds.Select(k => ((int)k).ToString(CultureInfo.InvariantCulture)));
|
||||
|
||||
/// <summary>Target ids as the table stores them: comma-joined, empty for none.</summary>
|
||||
private static Guid[] Targets(string ids) => ids.Length > 0 ? Array.ConvertAll(ids.Split(','), Guid.Parse) : [];
|
||||
|
||||
private static Job Map(DbRow r) => new()
|
||||
{
|
||||
Id = r.GetGuid(0),
|
||||
Kind = (JobKind)r.GetInt32(1),
|
||||
TargetIds = r.GetString(2) is { Length: > 0 } ids ? Array.ConvertAll(ids.Split(','), Guid.Parse) : [],
|
||||
TargetIds = Targets(r.GetString(2)),
|
||||
TargetLabel = r.GetStringOrNull(3),
|
||||
ParametersJson = r.GetStringOrNull(4),
|
||||
State = (JobState)r.GetInt32(5),
|
||||
|
||||
Reference in new issue
Block a user