A desktop sharing the archive that imports into an existing sequence lists the new frames in the sidecar itself and keeps its ThumbsBuilt and ProxiesBuilt. The server scan cleared the flags only when SequenceAdopter.Reconcile found unindexed photos, so it queued no image pass: the new frames, and every frame they renumbered, answered 404 on /thumb and /proxy until a pass was forced. A scan now also clears AnalyzedLight, ThumbsBuilt and ProxiesBuilt when the sidecar's frames version differs from the index row's, decided under the sequence lease, and its follow-ups queue the light analysis and both passes. Writers that change only part of a sequence wrote the whole index row back from a sidecar read under a lease they had already released (decode tiers) or without any (verify stamp): an ingest or a PATCH committed in between was undone, and a row written from a sidecar the index had not synced claimed its frames version, hiding the change from the scan. FrameTiers.RecordAsync and ArchiveVerifier now write the sidecar and only their own columns (SequenceRepository.RecordTiersAsync, RecordVerifiedAsync) under one lease. They and the image passes record the folder's scan stamps only while the index holds the sidecar's frames version (ArchiveIndexer.RecordStampsInStepAsync), so the next scan still sees frames another process added. An image pass decided ThumbsBuilt/ProxiesBuilt from the sidecar it read at its start and set the flag again after a scan had cleared it for photos added meanwhile, so no pass was queued for them. MarkAsync now holds the lease for the sidecar and the index, and sets the flag only while the sidecar lists the photos the pass read at the same indexes; otherwise it clears it. A rebuild deleted every row whose folder was not among those listed when it began, so a sequence that got its first frame or was created by an upload session while it ran lost its row (404 for the Uploader, a second '(2)' sequence for the session). A row is now checked again under its lease, as it and its folder are then, before it goes. Tests: a desktop's ArchiveIngest commit followed by a server scan (Data and Server, /thumb and /proxy answer 200 for every frame); a thumbnail pass with a scan's reconcile in between; tier records and a verify stamp interleaved with a PATCH and with another writer's frames, on SQLite and PostgreSQL; a rebuild during which uploads place and create sequences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
142 lines
7.2 KiB
C#
142 lines
7.2 KiB
C#
using Titano.Core.Archiving;
|
||
using Titano.Core.Imaging;
|
||
using Titano.Core.Model;
|
||
using Titano.Data.Archiving;
|
||
using Titano.Data.Repositories;
|
||
using Titano.Data.Tests.Infrastructure;
|
||
using Titano.Testing;
|
||
using static Titano.Data.Tests.Archiving.ArchiveFixture;
|
||
|
||
namespace Titano.Data.Tests.Archiving;
|
||
|
||
/// <summary>
|
||
/// Writers that change part of a sequence's sidecar and index row (decode tiers, the verify stamp) while a settings edit
|
||
/// or another writer commits, on both dialects: they write only what they own, under the sequence lease.
|
||
/// </summary>
|
||
[Category("archive")]
|
||
public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(context)
|
||
{
|
||
private async Task<ArchiveFixture> FixtureAsync() => Own(await CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
|
||
|
||
/// <summary>One indexed sequence of three photos in <c>a</c>.</summary>
|
||
private static async Task<Sequence> IndexedAsync(ArchiveFixture fx)
|
||
{
|
||
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 3);
|
||
await fx.ScanAsync();
|
||
var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id, fx.Ct)).Single();
|
||
return (await fx.Db.Sequences.GetAsync(location.Id, fx.Ct))!;
|
||
}
|
||
|
||
/// <summary>Records decode tiers as analyses, image passes and renders do.</summary>
|
||
private static FrameTiers Tiers(ArchiveFixture fx)
|
||
{
|
||
var writer = new SequenceWriter(new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) });
|
||
return new FrameTiers(fx.Db, writer, new ArchiveIndexer(fx.Db, writer));
|
||
}
|
||
|
||
/// <summary>The frame decodes only through its 48 × 32 embedded preview.</summary>
|
||
private static Dictionary<Guid, FileTier> Preview(Guid frameId) =>
|
||
new() { [frameId] = new FileTier(ImageFileFormat.Jpeg, DecodeTier.EmbeddedPreview, "test-preview", null, 48, 32) };
|
||
|
||
/// <summary>Commits <paramref name="tx"/> once the writer under test had time to read the row and wait for it.</summary>
|
||
private static async Task CommitLaterAsync(TitanoTransaction tx, CancellationToken cancellationToken)
|
||
{
|
||
await using (tx)
|
||
{
|
||
await Task.Delay(500, cancellationToken);
|
||
await tx.CommitAsync(cancellationToken);
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public async Task TiersRecordedWhileASettingsEditCommitsLeaveTheEdit()
|
||
{
|
||
var fx = await FixtureAsync();
|
||
var sequence = await IndexedAsync(fx);
|
||
var frames = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
|
||
|
||
// The server's PATCH holds the row (and SQLite's write lock) while an analysis records a tier.
|
||
Task record;
|
||
await using (var tx = await fx.Db.BeginAsync(fx.Ct))
|
||
{
|
||
var edit = await tx.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = """{"heroFrame":2}""" }, sequence.SettingsVersion, fx.Ct);
|
||
Assert.Equal(SequenceEditOutcome.Updated, edit.Outcome);
|
||
record = Tiers(fx).RecordAsync(fx.Archive, sequence, Preview(frames[0].Id), proxies: false, fx.Ct);
|
||
await Task.WhenAny(record, Task.Delay(500, fx.Ct));
|
||
Assert.False(record.IsCompleted, "the record waits for the row");
|
||
await tx.CommitAsync(fx.Ct);
|
||
}
|
||
await record;
|
||
|
||
// The tier and what follows from it are recorded, and the edit committed meanwhile stays.
|
||
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
|
||
Assert.Contains("heroFrame", row.SettingsJson);
|
||
Assert.Equal((sequence.SettingsVersion + 1, 1), (row.SettingsVersion, row.PreviewOnlyCount));
|
||
Assert.Equal(FrameFlags.PreviewOnly, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[0].Flags);
|
||
}
|
||
|
||
[Test]
|
||
public async Task TiersRecordedAfterAnotherWriterAddedFramesLeaveThoseFramesToTheNextScan()
|
||
{
|
||
var fx = await FixtureAsync();
|
||
var sequence = await IndexedAsync(fx);
|
||
var frames = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
|
||
|
||
// A desktop sharing the archive imports a photo into the sequence: its own index follows the sidecar, this one not yet.
|
||
var (desktop, archive) = await fx.OtherWriterAsync();
|
||
var staged = fx.Stage(Start.AddMinutes(1), seed: 90);
|
||
await desktop.Ingest.CommitAsync(new IngestRequest(archive.Id, sequence.Id, null, null, staged.Path, "IMG_0004.JPG", staged.Size, staged.Sha256, null, null),
|
||
fx.Ct);
|
||
|
||
// A tier recorded meanwhile changes the tiers here, not the frames this index holds.
|
||
await Tiers(fx).RecordAsync(fx.Archive, sequence, Preview(frames[0].Id), proxies: false, fx.Ct);
|
||
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
|
||
Assert.Equal((3, sequence.FramesVersion, 1), (row.FrameCount, row.FramesVersion, row.PreviewOnlyCount));
|
||
|
||
// The next scan still finds the folder changed, and indexes the photo.
|
||
Assert.SequenceEqual([sequence.Id], (await fx.ScanAsync()).Sequences);
|
||
var sidecar = ManifestStore.ReadSequence(fx.Paths.Sequence("a").ManifestPath);
|
||
row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
|
||
Assert.Equal((4, sidecar.Versions.Frames), (row.FrameCount, row.FramesVersion));
|
||
Assert.SequenceEqual(sidecar.Frames.Select(f => f.Id), (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)).Select(f => f.Id));
|
||
}
|
||
|
||
[Test]
|
||
public async Task AVerifyStampedWhileASettingsEditCommitsLeavesTheEdit()
|
||
{
|
||
var fx = await FixtureAsync();
|
||
var sequence = await IndexedAsync(fx);
|
||
// Hashed by a first verify, so the next one changes no frame and only stamps the sequence.
|
||
await fx.Service.Verifier.VerifyAsync(fx.Archive, [sequence.Id], default, (_, _, _) => ValueTask.CompletedTask, null, fx.Ct);
|
||
sequence = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
|
||
|
||
// The server's PATCH takes the row as the last photo is checked, and commits while the verify stamps the sequence.
|
||
Task? commit = null;
|
||
await fx.Service.Verifier.VerifyAsync(fx.Archive, [sequence.Id], default, (_, _, _) => ValueTask.CompletedTask, async (done, total) =>
|
||
{
|
||
if (done < total || commit is not null) return;
|
||
var tx = await fx.Db.BeginAsync(fx.Ct);
|
||
await tx.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = """{"heroFrame":1}""" }, sequence.SettingsVersion, fx.Ct);
|
||
commit = CommitLaterAsync(tx, fx.Ct);
|
||
}, fx.Ct);
|
||
await Assert.NotNull(commit);
|
||
|
||
var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!;
|
||
Assert.Contains("heroFrame", row.SettingsJson);
|
||
Assert.Equal(sequence.SettingsVersion + 1, row.SettingsVersion);
|
||
Assert.True(row.Status.HasFlag(SequenceStatus.Verified), row.Status.ToString());
|
||
Assert.True(row.VerifiedAt > sequence.VerifiedAt, "stamped again");
|
||
}
|
||
}
|
||
|
||
public sealed class SqlitePartialRowWriterTests(TestContext context) : PartialRowWriterTests(context)
|
||
{
|
||
protected override Backend Backend => Backend.SqliteFile;
|
||
}
|
||
|
||
[RequiresEnvironment(PgTestServer.Variable)]
|
||
public sealed class PostgresPartialRowWriterTests(TestContext context) : PartialRowWriterTests(context)
|
||
{
|
||
protected override Backend Backend => Backend.Postgres;
|
||
}
|