diff --git a/src/Titano.Data/Repositories/FrameRepository.cs b/src/Titano.Data/Repositories/FrameRepository.cs index 32dde150..61beae27 100644 --- a/src/Titano.Data/Repositories/FrameRepository.cs +++ b/src/Titano.Data/Repositories/FrameRepository.cs @@ -80,13 +80,33 @@ public sealed class FrameRepository _db.ListAsync("SELECT id, idx FROM frames WHERE sequence_id = $1 ORDER BY idx", [sequenceId], r => new FramePosition(r.GetGuid(0), r.GetInt32(1)), cancellationToken); - /// Writes analysis results for many frames in one transaction; returns the number of frames updated. + /// + /// Writes analysis results for many frames in one transaction; returns the number of frames updated. The rows of their + /// sequences are locked first, as every writer of a sequence's frames does (). + /// public Task UpdateLightStatsAsync(IReadOnlyList stats, CancellationToken cancellationToken = default) => _db.InTransactionAsync(async session => { var updated = 0; if (_db.Sql.IsPostgres) { + // The rows before any frame: a scan's sync holds its sequence's row while it rewrites every frame in its own + // order, so frames locked here first would wait for it while it waits for them, and PostgreSQL would abort + // one of the two. SQLite's write lock already serialises writers. + for (var start = 0; start < stats.Count; start += Batch) + { + var count = Math.Min(Batch, stats.Count - start); + var args = new DbParam[count]; + var placeholders = new StringBuilder(); + for (var i = 0; i < count; i++) + { + args[i] = stats[start + i].FrameId; + placeholders.Append(i > 0 ? ", $" : "$").Append(i + 1); + } + await session.ExecuteAsync( + $"SELECT id FROM sequences WHERE id IN (SELECT sequence_id FROM frames WHERE id IN ({placeholders})) ORDER BY id{_db.Sql.ForUpdate}", + args, cancellationToken).ConfigureAwait(false); + } // One UPDATE … FROM (VALUES …) per batch instead of a round trip per frame. for (var start = 0; start < stats.Count; start += Batch) { diff --git a/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs b/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs index e10ff4ee..0d0dee07 100644 --- a/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs +++ b/tests/Titano.Data.Tests/Archiving/PartialRowWriterTests.cs @@ -115,6 +115,31 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest( Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); } + [Test] + public async Task LightStatsWrittenWhileAScanHoldsTheRowWaitForItBeforeTheFrames() + { + 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 a light analysis writes its statistics. + Task write; + await using (var tx = await fx.Db.BeginAsync(fx.Ct)) + { + await tx.Sequences.GetForUpdateAsync(sequence.Id, fx.Ct); + write = fx.Db.Frames.UpdateLightStatsAsync([.. frames.Select(f => new FrameLightStats(f.Id, -1.5, 0.01, 0.02))], fx.Ct); + await Task.WhenAny(write, Task.Delay(500, fx.Ct)); + Assert.False(write.IsCompleted, "the statistics wait for the row"); + // The statistics locked no frame yet, so the scan rewrites them all without waiting for them. Frames locked before + // the row, in another order than the scan's, would each wait for the other, and PostgreSQL would abort one. + await tx.Frames.SyncSequenceAsync(sequence.Id, frames, fx.Ct); + await tx.CommitAsync(fx.Ct); + } + Assert.Equal(frames.Count, await write); + + Assert.True((await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)).All(f => f.MeanLogLuminance == -1.5), "written"); + } + [Test] public async Task TiersRecordedAfterAnotherWriterAddedFramesLeaveThoseFramesToTheNextScan() {