fix(archive): hold the thumbnail lease while compacting packs
CompactPacksAsync rewrote thumbs.pack under the sequence lease only, while thumbnail jobs append to the pack under the thumbs-<sequence id> lease, so a compaction racing a thumbnail pass could drop the entries just written. Compaction now takes the thumbnail lease too (CD-5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
3 files changed
+37
-1
No files matched your search
@@ -279,7 +279,7 @@ public sealed class ThumbPackReader : IDisposable
|
|||||||
public void Dispose() => _stream?.Dispose();
|
public void Dispose() => _stream?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Appends thumbnails to a pack; one writer per pack at a time (the sequence lease guarantees it).</summary>
|
/// <summary>Appends thumbnails to a pack; one writer per pack at a time (the <c>thumbs-<sequence id></c> lease guarantees it).</summary>
|
||||||
public sealed class ThumbPackWriter : IDisposable
|
public sealed class ThumbPackWriter : IDisposable
|
||||||
{
|
{
|
||||||
private readonly FileStream _stream;
|
private readonly FileStream _stream;
|
||||||
|
|||||||
@@ -188,7 +188,11 @@ public sealed class ArchiveMaintenance
|
|||||||
if (length == 0 || garbage < length * CompactionRatio) continue;
|
if (length == 0 || garbage < length * CompactionRatio) continue;
|
||||||
compacted.Add(location.Folder);
|
compacted.Add(location.Folder);
|
||||||
if (dryRun) continue;
|
if (dryRun) continue;
|
||||||
|
// The thumbnail builder appends under the thumbs lease only: holding it too keeps the compacted pack from
|
||||||
|
// replacing the file under a writer, whose later thumbnails would land in the orphaned one.
|
||||||
await using (await _writer.LockAsync(paths, location.Id, cancellationToken).ConfigureAwait(false))
|
await using (await _writer.LockAsync(paths, location.Id, cancellationToken).ConfigureAwait(false))
|
||||||
|
using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{location.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait,
|
||||||
|
_writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false))
|
||||||
ThumbPackWriter.Compact(sequence.ThumbsPackPath);
|
ThumbPackWriter.Compact(sequence.ThumbsPackPath);
|
||||||
}
|
}
|
||||||
return compacted;
|
return compacted;
|
||||||
|
|||||||
@@ -150,6 +150,38 @@ public sealed class ArchiveIntegrityTests(TestContext context)
|
|||||||
Assert.FileMissing(Path.Combine(fx.Paths.LocksDirectory, "seq-dead.lease"));
|
Assert.FileMissing(Path.Combine(fx.Paths.LocksDirectory, "seq-dead.lease"));
|
||||||
Assert.True(folder.Frames().All(File.Exists));
|
Assert.True(folder.Frames().All(File.Exists));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task PackCompactionWaitsForTheThumbnailWriter()
|
||||||
|
{
|
||||||
|
await using var fx = await CreateAsync(context);
|
||||||
|
var id = await IngestSeriesAsync(fx, "a", 2, 1);
|
||||||
|
var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder);
|
||||||
|
var hash = new byte[ThumbPack.HashLength];
|
||||||
|
using (var pack = ThumbPackWriter.Open(folder.ThumbsPackPath))
|
||||||
|
{
|
||||||
|
// The same entry written again and again: everything but the last copy is garbage.
|
||||||
|
for (var i = 0; i < 4; i++) pack.Append(0, 48, 32, hash, Photos.Jpeg(Start, 70 + i));
|
||||||
|
pack.Commit();
|
||||||
|
}
|
||||||
|
var length = new FileInfo(folder.ThumbsPackPath).Length;
|
||||||
|
|
||||||
|
// A thumbnail pass holds the pack's own lease (not the sequence's) while it appends.
|
||||||
|
Task<MaintenanceReport> maintenance;
|
||||||
|
using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"thumbs-{id:N}", "thumbnails", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
|
||||||
|
{
|
||||||
|
maintenance = fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: false, 0, _ => ValueTask.CompletedTask, fx.Ct);
|
||||||
|
await Task.Delay(500, fx.Ct);
|
||||||
|
Assert.False(maintenance.IsCompleted);
|
||||||
|
Assert.Equal(length, new FileInfo(folder.ThumbsPackPath).Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
var report = await maintenance;
|
||||||
|
Assert.SequenceEqual([folder.RelativeFolder], report.CompactedPacks);
|
||||||
|
using var compacted = ThumbPackReader.Open(folder.ThumbsPackPath);
|
||||||
|
Assert.Equal(0L, compacted.GarbageBytes);
|
||||||
|
Assert.NotNull(compacted.Read(0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static class SequencePathsExtensions
|
internal static class SequencePathsExtensions
|
||||||
|
|||||||
Reference in new issue
Block a user