diff --git a/src/Titano.Data/Archiving/ArchiveMaintenance.cs b/src/Titano.Data/Archiving/ArchiveMaintenance.cs index 9d137b18..8122a8ac 100644 --- a/src/Titano.Data/Archiving/ArchiveMaintenance.cs +++ b/src/Titano.Data/Archiving/ArchiveMaintenance.cs @@ -261,8 +261,16 @@ public sealed class ArchiveMaintenance ManifestStore.WriteSequence(folder.ManifestPath, SequenceFrames.Update(manifest, changes)); var setIds = bySequence.Where(x => x.Set).Select(x => x.Key).ToList(); var clearIds = bySequence.Where(x => !x.Set).Select(x => x.Key).ToList(); - if (setIds.Count > 0) await _db.Frames.UpdateFlagsAsync(setIds, FrameFlags.Duplicate, FrameFlags.None, cancellationToken).ConfigureAwait(false); - if (clearIds.Count > 0) await _db.Frames.UpdateFlagsAsync(clearIds, FrameFlags.None, FrameFlags.Duplicate, cancellationToken).ConfigureAwait(false); + await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) + { + // The row before its frames (SequenceRepository.GetForUpdateAsync), so a scan writing them waits or is waited for. + if (await tx.Sequences.GetForUpdateAsync(sequence.Id, cancellationToken).ConfigureAwait(false) is not null) + { + if (setIds.Count > 0) await tx.Frames.UpdateFlagsAsync(setIds, FrameFlags.Duplicate, FrameFlags.None, cancellationToken).ConfigureAwait(false); + if (clearIds.Count > 0) await tx.Frames.UpdateFlagsAsync(clearIds, FrameFlags.None, FrameFlags.Duplicate, cancellationToken).ConfigureAwait(false); + } + await tx.CommitAsync(cancellationToken).ConfigureAwait(false); + } if (inStep) await _indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false); } } diff --git a/src/Titano.Data/Archiving/FrameTiers.cs b/src/Titano.Data/Archiving/FrameTiers.cs index 86110d20..62745106 100644 --- a/src/Titano.Data/Archiving/FrameTiers.cs +++ b/src/Titano.Data/Archiving/FrameTiers.cs @@ -114,10 +114,12 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi ManifestStore.WriteSequence(folder.ManifestPath, manifest); await using (var tx = await db.BeginAsync(cancellationToken).ConfigureAwait(false)) { - await tx.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken) - .ConfigureAwait(false); + // The row before its frames (SequenceRepository.GetForUpdateAsync): a scan holding the row would otherwise wait + // for these frames while this waits for the row, and PostgreSQL would abort one of them. await tx.Sequences.RecordTiersAsync(sequence.Id, gained ? SequenceStatus.HasWarnings : SequenceStatus.None, frames.Count(f => (f.Flags & FrameFlags.PreviewOnly) != 0), cancellationToken).ConfigureAwait(false); + await tx.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken) + .ConfigureAwait(false); await tx.CommitAsync(cancellationToken).ConfigureAwait(false); } if (inStep) await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false); diff --git a/src/Titano.Data/Repositories/SequenceRepository.cs b/src/Titano.Data/Repositories/SequenceRepository.cs index 9a17e6f3..7eb69fe2 100644 --- a/src/Titano.Data/Repositories/SequenceRepository.cs +++ b/src/Titano.Data/Repositories/SequenceRepository.cs @@ -171,6 +171,8 @@ public sealed class SequenceRepository /// inside a transaction, the row locked until it ends (PostgreSQL; SQLite's write lock already /// serialises writers): a transaction that writes the whole row back from this read cannot undo an edit that another /// one commits meanwhile, as takes the same lock. + /// A transaction that writes frames of the sequence takes this lock, or writes the row, before the frames: with one + /// order no two of them wait for each other's rows (PostgreSQL would abort one with a deadlock). /// public Task GetForUpdateAsync(Guid id, CancellationToken cancellationToken = default) => _db.SingleAsync($"{_select} WHERE s.id = $1{_db.Sql.ForUpdate}", [id], Map, cancellationToken); diff --git a/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs b/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs index 2ec4c3b7..c7354f7b 100644 --- a/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs +++ b/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs @@ -11,8 +11,9 @@ namespace Titano.Data.Tests.Archiving; /// /// Writers that change part of a sequence's sidecar and index row (decode tiers, the verify, title, tags, rating and -/// notes, a rename) while a settings edit or another writer commits, on both dialects: they write only what they own, -/// under the sequence lease, and leave what another process changed in the sidecar to the next scan. +/// notes, a rename, an upload) while a settings edit, a scan or another writer commits, on both dialects: they write only +/// what they own, under the sequence lease, take the row before the frames, and leave what another process changed in +/// the sidecar to the next scan or index it whole. /// [Category("archive")] public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(context) @@ -76,6 +77,32 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest( Assert.Equal(FrameFlags.PreviewOnly, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[0].Flags); } + [Test] + public async Task TiersRecordedWhileAScanHoldsTheRowWaitForItBeforeTheFrames() + { + var fx = await FixtureAsync(); + var sequence = await IndexedAsync(fx); + var frames = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct); + + // A scan's sync holds the row, as it does before it rewrites the frames, while an analysis records a tier. + Task record; + await using (var tx = await fx.Db.BeginAsync(fx.Ct)) + { + await tx.Sequences.GetForUpdateAsync(sequence.Id, fx.Ct); + 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"); + // The record touched no frame yet, so the scan rewrites them all without waiting for it. Had it locked its frame + // before the row, each would wait for the other and PostgreSQL would abort one of them. + await tx.Frames.SyncSequenceAsync(sequence.Id, frames, fx.Ct); + await tx.CommitAsync(fx.Ct); + } + await record; + + Assert.Equal(FrameFlags.PreviewOnly, (await fx.Db.Frames.GetAsync(frames[0].Id, fx.Ct))!.Flags); + Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + } + [Test] public async Task TiersRecordedAfterAnotherWriterAddedFramesLeaveThoseFramesToTheNextScan() {