fix(data): light statistics lock their sequences before their frames
FrameRepository.UpdateLightStatsAsync updated every frame of a sequence in batches without locking the sequence row. On PostgreSQL a scan's or rebuild's sync holds the row and rewrites every frame in its own order, so a deep analysis writing its statistics at the same time could hold some frames while waiting for others the sync held, and PostgreSQL aborted one of the two jobs with a deadlock. On PostgreSQL the method now locks the rows of its frames' sequences first, in the same transaction, like every other writer of a sequence's frames. SQLite's write lock already serialises writers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+46
-1
No files matched your search
@@ -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);
|
||||
|
||||
/// <summary>Writes analysis results for many frames in one transaction; returns the number of frames updated.</summary>
|
||||
/// <summary>
|
||||
/// 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 (<see cref="SequenceRepository.GetForUpdateAsync"/>).
|
||||
/// </summary>
|
||||
public Task<int> UpdateLightStatsAsync(IReadOnlyList<FrameLightStats> 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)
|
||||
{
|
||||
|
||||
@@ -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<int> 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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user