Merge branch 'review2-p5a-fix4-projects' into review2-p5a-int4

This commit is contained in:
Alby96 committed 2026-11-03 14:23:47 +01:00
commit 8afd3be2e8
4 files changed
+449 -45

No files matched your search

+126 -18
View File
@@ -1,4 +1,8 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Model;
@@ -24,8 +28,11 @@ internal sealed record ProjectReconcile(bool Missing, IReadOnlySet<Guid> Seen, I
/// behind on moves forward, a name or deletion recorded since is taken, and a row the index marked deleted is listed again
/// when its live record says it is not. Only a record that says so, live or in the projects' trash, deletes a project:
/// never a folder that is not there (moved for a moment, a backup put back, a sync tool) nor a record that cannot be read,
/// whose project stays as the index has it (<see cref="ProjectReconcile.HeldBack"/>); a record missing from the folder of a
/// live project the index holds is written again from the index when the archive takes writes. A version file written past its
/// whose project stays as the index has it (<see cref="ProjectReconcile.HeldBack"/>). The projects' trash is read before the
/// live folders, so a deletion is never undone by what is left of a live folder. A record missing from the folder of a live
/// project the index holds is written again from the index when the archive takes writes, once the folder has settled and
/// never while the trash holds a folder of that project. A record older than what the index knows (behind its head, or
/// written before it) never gives the index its name. A version file written past its
/// record's head by a push a crash cut short is adopted when it follows the head (its file is complete: versions are
/// written atomically, then the record); any other file past the head, or a second file of a number the record's chain
/// names another for, is moved aside to <c>versions/.stray</c>, never deleted. Runs under the sequence's lease, or without
@@ -38,6 +45,15 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
/// <summary>Versions read at once when a project's sidecars are written again from the index.</summary>
private const int WriteBackPage = 500;
/// <summary>The live folders with no record seen by a reconcile, by project: what they held then, and when (<see cref="Rebuildable"/>).</summary>
private readonly ConcurrentDictionary<Guid, (string Look, long Since)> _unsettled = new();
/// <summary>
/// How long a live folder with no record must look the same before its record is written again from the index (a sync
/// tool bringing the real one has it win meanwhile). Tests stand in a shorter one.
/// </summary>
internal TimeSpan RecordSettle { get; set; } = TimeSpan.FromMinutes(2);
/// <summary>Reconciles the projects of <paramref name="sequenceId"/> in <paramref name="folder"/>; the caller holds the sequence's lease.</summary>
/// <param name="writable">False for a read-only archive: the sidecars are read as they are and only the index is written
/// (a version past its record's head is indexed, not adopted into the record; nothing is moved aside).</param>
@@ -72,18 +88,22 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
Log.Info("projects", $"{directory} holds no project record; it is left as it is");
continue;
}
// A record lost (a sync tool, a disk) of a live project the index holds: written again from the index.
if (writable && row is { DeletedAt: null } && row.SequenceId == sequenceId)
// A record lost (a sync tool, a disk) of a live project the index holds: written again from the index, once the
// folder has settled and no deletion of it is on its way (Rebuildable).
if (writable && row is { DeletedAt: null } && row.SequenceId == sequenceId && Rebuildable(folder, directory, named))
record = await RecordFromIndexAsync(folder, row, recordPath, cancellationToken).ConfigureAwait(false);
}
if (record is null)
{
Log.Warning("projects", $"The project in {directory} cannot be read; it stays as the index has it", ex);
// Its row stays as it is, never taken for gone nor written again, and a live one is held back (it may be Main).
if (seen.Add(named) && !trashed) heldBack.Add(named);
// Its row stays as it is, never taken for gone nor written again, and a live one is held back (it may be Main):
// also when the record that cannot be read is in the projects' trash, which may say a project the index has
// live was deleted (the trash is read first, so its live folder is not taken either).
if (seen.Add(named) && (!trashed || await LiveHereAsync(named, sequenceId, cancellationToken).ConfigureAwait(false))) heldBack.Add(named);
continue;
}
}
_unsettled.TryRemove(record.Id, out _);
// A sequence folder copied by hand carries the projects of its source, which stay that sequence's.
if (record.SequenceId != sequenceId || !seen.Add(record.Id)) continue;
if (trashed && record.Deleted is null) record = record with { Deleted = Directory.GetLastWriteTimeUtc(directory) };
@@ -171,7 +191,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
foreach (var loser in claimants.Skip(1))
{
if (await db.Projects.GetAsync(loser.Id, cancellationToken).ConfigureAwait(false) is not { IsMain: false, DeletedAt: null } row) continue;
var record = live[loser.Id] with { IsMain = false, Name = row.Name, Mirrored = null, Updated = DateTimeOffset.UtcNow };
var record = live[loser.Id] with { IsMain = false, Name = row.Name, Mirrored = null, Updated = UpdatedAfter(live[loser.Id].Updated) };
try
{
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(loser.Id), record);
@@ -189,7 +209,8 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
/// The record of a live project the index holds, written again from the index when its folder has none (a sync tool, a
/// disk lost it): the version files the folder lacks first (those there are left as they are), then the record naming
/// the index's head and, for Main, the version this index mirrored. The versions the folder holds past that head are
/// then adopted as after any push a crash cut short. Null when it cannot be written now (tried again next time).
/// then adopted as after any push a crash cut short. Only once <see cref="Rebuildable"/>. Null when it cannot be written
/// now (tried again next time).
/// </summary>
private async Task<ProjectRecord?> RecordFromIndexAsync(SequencePaths folder, SequenceProject row, string path, CancellationToken cancellationToken)
{
@@ -211,6 +232,30 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
}
}
/// <summary>
/// Whether <paramref name="record"/>'s head is one of the versions the index holds below its head (or it names none while
/// the index has one): the record is behind. A record whose head the index does not hold at its number (another chain, a
/// version this Chronos cannot read) is not: it is never written over.
/// </summary>
internal static async Task<bool> BehindAsync(ChronosDatabase db, SequenceProject project, ProjectRecord record, CancellationToken cancellationToken)
{
if (project.HeadVersionId is null) return false;
if (record.Head is not { } head) return true;
return head.Number < project.HeadNumber && await db.Projects.GetVersionAsync(head.Id, cancellationToken).ConfigureAwait(false) is { } version
&& version.ProjectId == project.Id && version.Number == head.Number;
}
/// <summary>
/// The time a record written now from the index says it was updated: now, or just after <paramref name="previous"/> (the
/// time the record it replaces says) when a clock behind the one that wrote it would go back. A record's time never goes
/// back, so the newest write of a project (a rename on another PC) is always seen as newer than the one before.
/// </summary>
internal static DateTimeOffset UpdatedAfter(DateTimeOffset previous)
{
var now = DateTimeOffset.UtcNow;
return now > previous ? now : previous.AddMilliseconds(1);
}
/// <summary>The version files of a project numbered above <paramref name="after"/> written from the index (immutable: one there already is left).</summary>
internal static async Task WriteVersionsAsync(ChronosDatabase db, SequencePaths folder, Guid projectId, int after, CancellationToken cancellationToken)
{
@@ -239,18 +284,77 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
return candidate;
}
/// <summary>The project folders of a sequence: the live ones, then those in the projects' trash (<c>&lt;id&gt;-&lt;time&gt;</c>).</summary>
/// <summary>
/// The project folders of a sequence: those in the projects' trash (<c>&lt;id&gt;-&lt;time&gt;</c>), then the live ones.
/// The trash comes first so a deletion wins over what is left of the project's live folder (a sync tool moving it into
/// the trash file by file, another PC's delete arriving): a deletion is never undone by a reconcile.
/// </summary>
private static IEnumerable<(string Directory, bool Trashed)> ProjectFolders(SequencePaths folder)
{
if (Directory.Exists(folder.ProjectsTrashDirectory))
{
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Order(StringComparer.Ordinal))
{
if (File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))) yield return (directory, true);
}
}
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsDirectory).Order(StringComparer.Ordinal))
{
if (Guid.TryParseExact(Path.GetFileName(directory), "N", out _)) yield return (directory, false);
}
if (!Directory.Exists(folder.ProjectsTrashDirectory)) yield break;
foreach (var directory in Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Order(StringComparer.Ordinal))
}
/// <summary>Whether the index holds project <paramref name="projectId"/> live, of this sequence.</summary>
private async Task<bool> LiveHereAsync(Guid projectId, Guid sequenceId, CancellationToken cancellationToken) =>
await db.Projects.GetAsync(projectId, cancellationToken).ConfigureAwait(false) is { DeletedAt: null } row && row.SequenceId == sequenceId;
/// <summary>
/// Whether the live folder <paramref name="directory"/> of project <paramref name="projectId"/>, which has no record, may
/// have its record written again from the index now. Never while the projects' trash holds a folder of it (a deletion on
/// its way, its record maybe not there yet), and only once the folder has looked the same, file by file, on two
/// reconciles <see cref="RecordSettle"/> apart: a sync tool still bringing the real record (with a rename, a newer head)
/// has it win. Until then the project is held back.
/// </summary>
private bool Rebuildable(SequencePaths folder, string directory, Guid projectId)
{
var prefix = projectId.ToString("N") + "-";
try
{
if (File.Exists(Path.Combine(directory, ArchiveLayout.ProjectFile))) yield return (directory, true);
if (Directory.Exists(folder.ProjectsTrashDirectory)
&& Directory.EnumerateDirectories(folder.ProjectsTrashDirectory).Any(d => Path.GetFileName(d).StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
{
Log.Info("projects", $"Project {projectId} has a folder in the projects' trash; its record is not written again");
return false;
}
var look = Look(directory);
var now = Stopwatch.GetTimestamp();
if (_unsettled.TryGetValue(projectId, out var earlier) && earlier.Look == look)
{
if (Stopwatch.GetElapsedTime(earlier.Since, now) < RecordSettle) return false;
_unsettled.TryRemove(projectId, out _);
return true;
}
_unsettled[projectId] = (look, now);
Log.Info("projects", $"Project {projectId} has no record in its folder; it is written again from the index once the folder settles");
return false;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("projects", $"The folder of project {projectId} cannot be read now", ex);
return false;
}
}
/// <summary>What a project folder holds now, file by file (path, size, time written), as one hash.</summary>
private static string Look(string directory)
{
var text = new StringBuilder();
foreach (var file in new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories).OrderBy(f => f.FullName, StringComparer.Ordinal))
{
text.Append(Path.GetRelativePath(directory, file.FullName)).Append('|').Append(file.Length.ToString(CultureInfo.InvariantCulture)).Append('|')
.Append(file.LastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).Append('\n');
}
return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString())));
}
/// <summary>
@@ -296,7 +400,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
SetAside(versions, stray.Path, $"does not follow the head of project {record.Id}");
}
if (!moved) return record;
var rolled = record with { Head = head, Updated = DateTimeOffset.UtcNow };
var rolled = record with { Head = head, Updated = UpdatedAfter(record.Updated) };
if (!writable) return rolled;
try
{
@@ -351,8 +455,10 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
row = project with { HeadVersionId = null, HeadNumber = 0 };
await db.Projects.InsertAsync(row, cancellationToken).ConfigureAwait(false);
}
// A record behind the index (a projects folder put back from an older backup) says what was: its name is not taken.
var behind = row.HeadVersionId is not null && (record.Head?.Number ?? 0) < row.HeadNumber;
// A record behind the index (a projects folder put back from an older backup: its head is a version the index holds
// below its own, or it was written before what the index knows of the project, as a rename made since) says what was:
// its name is not taken, and the heal writes the index's back (ProjectService). A record on another chain is not behind.
var stale = await BehindAsync(db, row, record, cancellationToken).ConfigureAwait(false) || record.Updated < row.UpdatedAt;
await IndexVersionsAsync(record, directory, writable, cancellationToken).ConfigureAwait(false);
var (headId, headNumber) = (row.HeadVersionId, row.HeadNumber);
var reached = true;
@@ -376,7 +482,7 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
{
updated = updated with { DeletedAt = row.DeletedAt ?? record.Deleted };
}
else if (row.DeletedAt is not null || (row.Name != record.Name && !behind))
else if (row.DeletedAt is not null || (row.Name != record.Name && !stale))
{
var others = (await db.Projects.ListBySequenceAsync(record.SequenceId, includeDeleted: false, cancellationToken).ConfigureAwait(false))
.Where(p => p.Id != record.Id).ToList();
@@ -389,10 +495,12 @@ internal sealed class ProjectIndexer(ChronosDatabase db)
// The record's name (unless it is behind), else (another project has it) the name the index has, else one free.
updated = updated with
{
Name = !behind && FreeName(others, record.Name, record.Id) == record.Name ? record.Name
Name = !stale && FreeName(others, record.Name, record.Id) == record.Name ? record.Name
: FreeName(others, row.Name, record.Id) == row.Name ? row.Name
: FreeName(others, behind ? row.Name : record.Name, record.Id),
: FreeName(others, stale ? row.Name : record.Name, record.Id),
};
// What the index knows of the project is now as new as the record, so an older one put back later is seen as such.
if (!stale && record.Updated > updated.UpdatedAt) updated = updated with { UpdatedAt = record.Updated };
}
if (updated != row) await db.Projects.UpdateAsync(updated, cancellationToken).ConfigureAwait(false);
return reached || record.Deleted is not null || trashed;
+114 -26
View File
@@ -132,6 +132,13 @@ public sealed class ProjectService
/// <summary>Whether an archive takes writes now (<see cref="ArchiveVolume.Probe"/>; tests stand in a read-only one).</summary>
internal Func<ArchivePaths, ArchiveAvailability> Availability { get; set; } = ArchiveVolume.Probe;
/// <summary>How long a live project folder with no record must look the same before its record is written again (tests stand in a shorter one).</summary>
internal TimeSpan RecordSettle
{
get => _indexer.RecordSettle;
set => _indexer.RecordSettle = value;
}
/// <summary>Called at each named step between two writes, so a test can stop there as a crash would.</summary>
internal Action<string>? Cut { get; set; }
@@ -228,7 +235,7 @@ public sealed class ProjectService
if (current.Name == trimmed) return current;
await EnsureNameFreeAsync(current.SequenceId, projectId, trimmed, ct).ConfigureAwait(false);
var record = await OwnRecordAsync(context, current, ct).ConfigureAwait(false);
var renamed = current with { Name = trimmed, UpdatedAt = DateTimeOffset.UtcNow };
var renamed = current with { Name = trimmed, UpdatedAt = ProjectIndexer.UpdatedAfter(record.Updated) };
ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(projectId), ProjectRecord.Of(renamed, record.Mirrored));
await _db.Projects.UpdateAsync(renamed, ct).ConfigureAwait(false);
return renamed;
@@ -250,7 +257,7 @@ public sealed class ProjectService
var current = await LiveAsync(projectId, ct).ConfigureAwait(false);
var record = await OwnRecordAsync(context, current, ct).ConfigureAwait(false);
var now = DateTimeOffset.UtcNow;
var deleted = current with { DeletedAt = now, UpdatedAt = now };
var deleted = current with { DeletedAt = now, UpdatedAt = ProjectIndexer.UpdatedAfter(record.Updated) };
Directory.CreateDirectory(context.Folder.ProjectsTrashDirectory);
var trashed = Path.Combine(context.Folder.ProjectsTrashDirectory,
projectId.ToString("N") + "-" + now.UtcTicks.ToString(System.Globalization.CultureInfo.InvariantCulture));
@@ -575,8 +582,10 @@ public sealed class ProjectService
/// whose mirror a crash cut short is finished next time, by this index or by one rebuilt from the sidecars: merged again
/// over the same basis, it is the merge version itself.
/// </summary>
/// <param name="caughtUp">The merge takes in settings a mirror found written into <c>project.json</c> since
/// (<see cref="CatchUpSharedAsync"/>): its own mirror does not catch up again.</param>
private async Task MergeIntoMainAsync(LeaseContext context, SequenceProject main, ProjectVersion basis, ProjectVersion head, string theirs,
FramePhotos photos, CancellationToken cancellationToken)
FramePhotos photos, CancellationToken cancellationToken, bool caughtUp = false)
{
ProjectMergeResult merged;
try
@@ -594,7 +603,7 @@ public sealed class ProjectService
var text = ProjectContent.Canonical(merged.Merged.ToJson());
if (ProjectContent.SameChoices(head.SettingsJson, text, photos))
{
await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
await MirrorAsync(context, main, head, cancellationToken, theirs, caughtUp).ConfigureAwait(false);
return;
}
if (merged.HasConflicts)
@@ -605,7 +614,7 @@ public sealed class ProjectService
var version = await AppendDerivedAsync(context, main, head, text, ProjectVersionKind.Merge, cancellationToken).ConfigureAwait(false);
main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!;
Step("catchup.merge");
await MirrorAsync(context, main, version, cancellationToken).ConfigureAwait(false);
await MirrorAsync(context, main, version, cancellationToken, theirs, caughtUp).ConfigureAwait(false);
}
/// <summary>
@@ -668,7 +677,7 @@ public sealed class ProjectService
var headMirrored = context.Reconcile.Live.TryGetValue(main.Id, out var record) && record.Mirrored?.Id == head.Id;
if (!headMirrored && await MirroredBeforeAsync(main, head, recorded, shared, photos, cancellationToken).ConfigureAwait(false))
{
await MirrorAsync(context, main, head, cancellationToken).ConfigureAwait(false);
await MirrorAsync(context, main, head, cancellationToken, shared).ConfigureAwait(false);
return;
}
if (recorded is not null)
@@ -790,18 +799,30 @@ public sealed class ProjectService
/// <summary>
/// The records the archive has whose head is behind the index's (a projects folder put back from an older backup, a sync
/// tool that brought an older record back): the versions they lack and the record written again from the index, which
/// holds the newer chain, so the sidecars hold every version again. A record ahead of the index, or naming a head the
/// index does not have at its number, is never written over (<see cref="WritableRecordAsync"/>).
/// holds the newer chain, so the sidecars hold every version again. A record at the index's head written before what the
/// index knows of the project (its time is older: a backup made before a rename put back) has the index's name written
/// back. A record ahead of the index, or naming a head the index does not have at its number, is never written over
/// (<see cref="WritableRecordAsync"/>).
/// </summary>
private async Task HealBehindAsync(LeaseContext context, CancellationToken cancellationToken)
{
foreach (var (id, record) in context.Reconcile.Live)
{
if (context.Reconcile.HeldBack.Contains(id) || await _db.Projects.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { DeletedAt: null } row
|| !await BehindAsync(row, record, cancellationToken).ConfigureAwait(false)) continue;
if (context.Reconcile.HeldBack.Contains(id) || await _db.Projects.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { DeletedAt: null } row)
continue;
try
{
await HealAsync(context.Folder, row, record, cancellationToken).ConfigureAwait(false);
if (await BehindAsync(row, record, cancellationToken).ConfigureAwait(false))
{
await HealAsync(context.Folder, row, record, cancellationToken).ConfigureAwait(false);
}
else if (record.Head?.Id == row.HeadVersionId && record.Name != row.Name && record.Updated < row.UpdatedAt)
{
// At the index's head but written before what the index knows (a backup made before a rename put back): the
// index's name written back, the rest as the record has it.
ProjectSidecars.WriteRecord(context.Folder.ProjectRecordPath(id), record with { Name = row.Name, Updated = ProjectIndexer.UpdatedAfter(row.UpdatedAt) });
Log.Warning("projects", $"The record of project {id} named it '{record.Name}' from before the index's '{row.Name}'; the index's name is written back");
}
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
@@ -810,14 +831,8 @@ public sealed class ProjectService
}
}
/// <summary>Whether <paramref name="record"/>'s head is one of the versions the index holds below its head: the record is behind.</summary>
private async Task<bool> BehindAsync(SequenceProject project, ProjectRecord record, CancellationToken cancellationToken)
{
if (project.HeadVersionId is null) return false;
if (record.Head is not { } head) return true;
return head.Number < project.HeadNumber && await _db.Projects.GetVersionAsync(head.Id, cancellationToken).ConfigureAwait(false) is { } version
&& version.ProjectId == project.Id && version.Number == head.Number;
}
private Task<bool> BehindAsync(SequenceProject project, ProjectRecord record, CancellationToken cancellationToken) =>
ProjectIndexer.BehindAsync(_db, project, record, cancellationToken);
/// <summary>
/// A record behind the index made whole: the version files past its head written from the index (those there are left
@@ -826,7 +841,7 @@ public sealed class ProjectService
private async Task<ProjectRecord> HealAsync(SequencePaths folder, SequenceProject project, ProjectRecord record, CancellationToken cancellationToken)
{
await WriteVersionsAsync(folder, project.Id, record.Head?.Number ?? 0, cancellationToken).ConfigureAwait(false);
var healed = ProjectRecord.Of(project with { UpdatedAt = DateTimeOffset.UtcNow }, await MirroredHeadAsync(project, record.Mirrored, cancellationToken)
var healed = ProjectRecord.Of(project with { UpdatedAt = ProjectIndexer.UpdatedAfter(record.Updated) }, await MirroredHeadAsync(project, record.Mirrored, cancellationToken)
.ConfigureAwait(false));
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), healed);
Log.Warning("projects", $"The record of project {project.Id} named version {record.Head?.Number} as its head, behind the index's {project.HeadNumber}; "
@@ -975,7 +990,7 @@ public sealed class ProjectService
private async Task<SequenceProject> AppendAsync(SequencePaths folder, SequenceProject project, ProjectVersion version, CancellationToken cancellationToken)
{
var record = await WritableRecordAsync(folder, project, cancellationToken).ConfigureAwait(false);
var now = DateTimeOffset.UtcNow;
var now = ProjectIndexer.UpdatedAfter(record.Updated);
var advanced = project with { HeadVersionId = version.Id, HeadNumber = version.Number, UpdatedAt = now };
ProjectSidecars.WriteVersion(folder.VersionPath(project.Id, version.Number, version.Id), version);
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(project.Id), ProjectRecord.Of(advanced, record.Mirrored));
@@ -1025,12 +1040,19 @@ public sealed class ProjectService
}
/// <summary>
/// Writes Main's <paramref name="version"/> into the sequence: its positions placed on the sequence's frames, then
/// <c>project.json</c>, Main's record naming it as mirrored, and the index's settings (and hero frame) with the settings
/// version as the check. Recorded as mirrored when the index took it; a settings edit that won the race is caught up by
/// the next operation (merged). A version this Chronos cannot read (of a newer one) is never mirrored: nothing is written.
/// Writes Main's <paramref name="version"/> (its head) into the sequence: its positions placed on the sequence's frames,
/// then <c>project.json</c>, Main's record naming it as mirrored, and the index's settings (and hero frame) with the
/// settings version as the check. Recorded as mirrored when the index took it; a settings edit that won the race is caught
/// up by the next operation (merged). A version this Chronos cannot read (of a newer one) is never mirrored: nothing is
/// written. Nor is <c>project.json</c> written over settings written there since this index last knew what it held (by
/// another PC or an older client, not scanned here yet: <see cref="MovedSharedAsync"/>): they are caught up first, as
/// the catch-up does, and the result mirrored (<see cref="CatchUpSharedAsync"/>).
/// </summary>
private async Task MirrorAsync(LeaseContext context, SequenceProject main, ProjectVersion version, CancellationToken cancellationToken)
/// <param name="expected">Settings the caller has just taken in (from <c>project.json</c> or the index), which it may hold.</param>
/// <param name="caughtUp">This mirror follows such a catch-up: when <c>project.json</c> moved again meanwhile, nothing is
/// written and the next operation catches up again.</param>
private async Task MirrorAsync(LeaseContext context, SequenceProject main, ProjectVersion version, CancellationToken cancellationToken, string? expected = null,
bool caughtUp = false)
{
var frames = await LoadFramesAsync(context.Row.Id, cancellationToken).ConfigureAwait(false);
ProjectSettings placed;
@@ -1046,6 +1068,16 @@ public sealed class ProjectService
Log.Warning("projects", $"Version {version.Number} of Main of sequence {context.Row.Id} cannot be mirrored into its settings; they stay as they are", ex);
return;
}
if (await MovedSharedAsync(context, main, version, expected, frames.Photos, cancellationToken).ConfigureAwait(false) is { } moved)
{
if (caughtUp || main.HeadVersionId != version.Id)
{
Log.Info("projects", $"The project.json of sequence {context.Row.Id} changed while version {version.Number} of Main was mirrored; caught up next time");
return;
}
await CatchUpSharedAsync(context, main, version, moved.Shared, moved.Basis, frames.Photos, cancellationToken).ConfigureAwait(false);
return;
}
var json = placed.ToJson();
Directory.CreateDirectory(context.Folder.MetaDirectory);
AtomicFile.WriteAllText(context.Folder.ProjectPath, json);
@@ -1064,6 +1096,62 @@ public sealed class ProjectService
}
}
/// <summary>
/// The portable settings of the sequence's <c>project.json</c> when they moved since this index last knew what it held:
/// they are none of <paramref name="version"/>, <paramref name="expected"/> and the versions mirrored last (by this
/// index, and by any as Main's record names it), so another PC or an older client wrote them and this index has not
/// scanned them yet. With them, the newest of those mirrored versions: what they were written over. Null when they did
/// not move, or there is no <c>project.json</c> or none this Chronos reads.
/// </summary>
private async Task<(string Shared, ProjectVersion? Basis)?> MovedSharedAsync(LeaseContext context, SequenceProject main, ProjectVersion version, string? expected,
FramePhotos photos, CancellationToken cancellationToken)
{
if (!File.Exists(context.Folder.ProjectPath) || SharedPortable(context, photos) is not { } shared) return null;
if (ProjectContent.SameChoices(version.SettingsJson, shared, photos) || (expected is not null && ProjectContent.SameChoices(expected, shared, photos))) return null;
ProjectHead? recorded = null;
try
{
recorded = ProjectSidecars.ReadRecord(context.Folder.ProjectRecordPath(main.Id)).Mirrored;
}
catch (Exception ex) when (ex is IOException or InvalidDataException or UnauthorizedAccessException)
{
Log.Warning("projects", $"The record of Main of sequence {context.Row.Id} cannot be read; project.json is compared with the version this index mirrored", ex);
}
var mirrored = new List<ProjectVersion>();
foreach (var id in new[] { main.MirroredVersionId, recorded?.Id }.OfType<Guid>().Distinct())
{
if (await _db.Projects.GetVersionAsync(id, cancellationToken).ConfigureAwait(false) is not { } known || known.ProjectId != main.Id
|| known.Number > version.Number || IsNewer(known)) continue;
if (ProjectContent.SameChoices(known.SettingsJson, shared, photos)) return null;
mirrored.Add(known);
}
return (shared, mirrored.MaxBy(v => v.Number));
}
/// <summary>
/// Settings written into <c>project.json</c> since <paramref name="basis"/> was (<see cref="MovedSharedAsync"/>) taken in
/// before <paramref name="head"/>, Main's head, is mirrored, as the catch-up does, so they are never written over: merged
/// with the versions after <paramref name="basis"/> (the settings written since win where both changed, and the head's
/// values stay in its version), or an external version when <paramref name="basis"/> is the head itself. With no
/// mirrored version known, the head's parent stands as the basis.
/// </summary>
private async Task CatchUpSharedAsync(LeaseContext context, SequenceProject main, ProjectVersion head, string shared, ProjectVersion? basis, FramePhotos photos,
CancellationToken cancellationToken)
{
if (basis is null && head.ParentId is { } parentId && await _db.Projects.GetVersionAsync(parentId, cancellationToken).ConfigureAwait(false) is { } parent
&& parent.ProjectId == main.Id && !IsNewer(parent)) basis = parent;
Log.Info("projects", $"The project.json of sequence {context.Row.Id} holds settings written since version {basis?.Number} of Main; "
+ $"they are taken in before version {head.Number} is mirrored");
if (basis is null || basis.Id == head.Id)
{
var external = await AppendDerivedAsync(context, main, head, shared, ProjectVersionKind.External, cancellationToken).ConfigureAwait(false);
main = (await _db.Projects.GetAsync(main.Id, cancellationToken).ConfigureAwait(false))!;
await MirrorAsync(context, main, external, cancellationToken, shared, caughtUp: true).ConfigureAwait(false);
return;
}
await MergeIntoMainAsync(context, main, basis, head, shared, photos, cancellationToken, caughtUp: true).ConfigureAwait(false);
}
/// <summary>Whether settings this Chronos reads strictly (a member of the wrong type is refused): a version that can be mirrored and edited.</summary>
private static bool Readable(string json)
{
@@ -302,6 +302,53 @@ public abstract class MainMergeTests(TestContext context) : DatabaseTest(context
Assert.Count(4, await fx.Service.Projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
[Test]
public async Task AnEditMadeOnlyInProjectJsonIsNeverWrittenOverByAMirror()
{
var fx = await FixtureAsync();
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var second = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(await projects.HeadAsync(main.Id, fx.Ct), new ProjectSettings { ExcludedFrames = [0] },
frames), fx.Ct)).Version!;
var folder = fx.Paths.Sequence(sequence.Folder);
// Another PC (an older client) edits project.json only, and this index has not scanned it: a push to Main merges it first.
File.WriteAllText(folder.ProjectPath, new ProjectSettings { ExcludedFrames = [0], Exposure = new ExposureSettings { ExposureOffsetEv = 1.0 } }.ToJson());
var third = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(second, new ProjectSettings
{
ExcludedFrames = [0, 2],
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
}, frames), fx.Ct)).Version!;
var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.Commit, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
Assert.Equal((third.Id, 0.5), (history[1].Id, ProjectSettings.FromJson(history[1].SettingsJson).Exposure.ExposureOffsetEv));
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
{
// Where both changed the outside edit wins; the push's other change is in force too.
Assert.Equal(1.0, settings.Exposure.ExposureOffsetEv);
Assert.SequenceEqual([0, 2], settings.ExcludedFrames);
}
Assert.Count(4, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
// The merge's mirror wrote project.json and the record but not the index (still at version 3), and project.json is
// edited again: the mirror completing it keeps the edit as an external version.
var head = await projects.HeadAsync(main.Id, fx.Ct);
var stale = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { ExcludedFrames = [0, 2], Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 } });
await fx.Db.Projects.SetMirroredAsync(main.Id, third.Id, stale.SettingsVersion, fx.Ct);
File.WriteAllText(folder.ProjectPath, new ProjectSettings { ExcludedFrames = [1], Exposure = new ExposureSettings { ExposureOffsetEv = -1 } }.ToJson());
history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
Assert.Equal((ProjectVersionKind.External, head.Id), (history[0].Kind, history[0].ParentId));
foreach (var settings in new[] { ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!),
ProjectSettings.FromJson(File.ReadAllText(folder.ProjectPath)) })
{
Assert.Equal(-1, settings.Exposure.ExposureOffsetEv);
Assert.SequenceEqual([1], settings.ExcludedFrames);
}
Assert.Count(5, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
}
[Test]
public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile()
{
@@ -643,7 +643,14 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c
File.Delete(folder.ProjectRecordPath(main.Id));
File.Delete(folder.VersionPath(vertical.Id, 1, first.Id));
// Nothing is held back: the records are written again from the index, the version on disk past its head adopted.
// The first look holds them back and writes nothing (a sync tool may still be bringing the records); once the folders
// looked the same twice, nothing is held back: the records are written again from the index, the version on disk past
// its head adopted.
projects.RecordSettle = TimeSpan.Zero;
Assert.SequenceEqual([(main.Id, true, 2), (vertical.Id, false, 2)],
(await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain, p.HeadNumber)));
Assert.False(File.Exists(folder.ProjectRecordPath(vertical.Id)));
Assert.False(File.Exists(folder.ProjectRecordPath(main.Id)));
Assert.SequenceEqual([(main.Id, true, 2), (vertical.Id, false, 3)],
(await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => (p.Id, p.IsMain, p.HeadNumber)));
var verticalRecord = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
@@ -686,6 +693,160 @@ public abstract class ProjectRecoveryTests(TestContext context) : DatabaseTest(c
Assert.Equal("Square", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
}
[Test]
public async Task ARenameOnlyMadeAfterABackupStaysWhenTheBackupIsPutBack()
{
var fx = await FixtureAsync();
var (sequence, _) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
var backup = folder.ProjectsDirectory + "-backup";
CopyDirectory(folder.ProjectsDirectory, backup);
await projects.RenameAsync(vertical.Id, "Square", fx.Ct);
// The backup made before the rename (the same head) is put back: its record was written before the index's name.
Directory.Delete(folder.ProjectsDirectory, recursive: true);
CopyDirectory(backup, folder.ProjectsDirectory);
Assert.Equal("Square", (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
Assert.Equal(("Square", 1), (record.Name, record.Head!.Number));
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
Assert.Equal("Square", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
// A rename another PC makes is newer than what this index knows, even from a clock behind the one this index wrote
// with (ten minutes ahead here): it is taken.
var (other, _) = await fx.OtherWriterAsync();
Assert.Equal("Square", (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
var ahead = DateTimeOffset.UtcNow.AddMinutes(10);
var written = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(vertical.Id), written with { Updated = ahead });
await fx.Db.Projects.UpdateAsync((await fx.Db.Projects.GetAsync(vertical.Id))! with { UpdatedAt = ahead }, fx.Ct);
await other.Projects.RenameAsync(vertical.Id, "Wide", fx.Ct);
Assert.True(ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Updated > ahead);
Assert.Equal("Wide", (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
Assert.Equal("Wide", ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)).Name);
}
[Test]
public async Task ARecordOnAnotherChainBelowTheHeadKeepsItsName()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var first = await projects.HeadAsync(vertical.Id, fx.Ct);
var second = (await projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct)).Version!;
await projects.PushAsync(vertical.Id, Draft(second, new ProjectSettings { ExcludedFrames = [2] }, frames), fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
// Another PC's record, on a chain of its own from version 1, renamed there: its head number is below the index's, but
// it is not a version the index holds, so the record is not behind and its name is taken.
var theirs = Draft(first, new ProjectSettings { ExcludedFrames = [0] }, frames) with { StoredAt = DateTimeOffset.UtcNow };
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, theirs.Id), theirs);
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id));
var apart = record with { Name = "Square", Head = new ProjectHead(theirs.Id, 2), Updated = DateTimeOffset.UtcNow.AddSeconds(1) };
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(vertical.Id), apart);
var listed = (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
Assert.Equal(("Square", 3), (listed.Name, listed.HeadNumber));
// Index and record agree on the name, and the record is never written over.
Assert.Equal(apart, ProjectSidecars.ReadRecord(folder.ProjectRecordPath(vertical.Id)));
Assert.Equal("Square", (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id).Name);
}
[Test]
public async Task ADeletionArrivingHalfSyncedIsTakenAndNeverUndone()
{
var fx = await FixtureAsync();
var (sequence, _) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var main = await projects.MainAsync(sequence.Id, fx.Ct);
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var square = await projects.CreateAsync(sequence.Id, "Square", cancellationToken: fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
var (other, _) = await fx.OtherWriterAsync();
Assert.SequenceEqual([main.Id, vertical.Id, square.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
projects.RecordSettle = TimeSpan.Zero;
other.Projects.RecordSettle = TimeSpan.Zero;
// This PC deletes Vertical; on the other PC a sync tool moving it file by file into the trash has left its live folder
// with a version file and no record. However often the other index looks, it takes the deletion and writes nothing.
await projects.DeleteAsync(vertical.Id, fx.Ct);
var trashed = Directory.GetDirectories(folder.ProjectsTrashDirectory).Single();
Directory.CreateDirectory(folder.ProjectVersionsDirectory(vertical.Id));
foreach (var file in Directory.GetFiles(Path.Combine(trashed, ArchiveLayout.ProjectVersionsFolder)))
File.Copy(file, Path.Combine(folder.ProjectVersionsDirectory(vertical.Id), Path.GetFileName(file)));
for (var i = 0; i < 2; i++)
{
Assert.SequenceEqual([main.Id, square.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
Assert.SequenceEqual([main.Id, square.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
}
Assert.NotNull((await other.Projects.GetAsync(vertical.Id, fx.Ct))!.DeletedAt);
Assert.False(File.Exists(folder.ProjectRecordPath(vertical.Id)));
// Square's deletion arrives with its versions in the trash before its record, and its live record gone already: it is
// held back (listed, nothing written), never written again as live, and deleted once the trash's record is there.
var squareTrash = Path.Combine(folder.ProjectsTrashDirectory, square.Id.ToString("N") + "-" + DateTimeOffset.UtcNow.UtcTicks);
CopyDirectory(folder.ProjectDirectory(square.Id), squareTrash);
var squareRecord = ProjectSidecars.ReadRecord(Path.Combine(squareTrash, ArchiveLayout.ProjectFile));
File.Delete(Path.Combine(squareTrash, ArchiveLayout.ProjectFile));
File.Delete(folder.ProjectRecordPath(square.Id));
for (var i = 0; i < 2; i++)
Assert.SequenceEqual([main.Id, square.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
Assert.False(File.Exists(folder.ProjectRecordPath(square.Id)));
ProjectSidecars.WriteRecord(Path.Combine(squareTrash, ArchiveLayout.ProjectFile), squareRecord with { Deleted = DateTimeOffset.UtcNow });
Assert.SequenceEqual([main.Id], (await other.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
Assert.SequenceEqual([main.Id], (await projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
Assert.False(File.Exists(folder.ProjectRecordPath(square.Id)));
// A rebuilt index has both deleted too.
await fx.ReopenWithEmptyIndexAsync();
await fx.ScanAsync(ScanMode.Rebuild);
Assert.SequenceEqual([main.Id], (await fx.Service.Projects.ListAsync(sequence.Id, fx.Ct)).Select(p => p.Id));
}
[Test]
public async Task ALostRecordIsWrittenAgainOnlyOnceItsFolderHasSettledSoAnIncomingOneWins()
{
var fx = await FixtureAsync();
var (sequence, frames) = await UploadedAsync(fx, 3);
var projects = fx.Service.Projects;
var vertical = await projects.CreateAsync(sequence.Id, "Vertical", cancellationToken: fx.Ct);
var first = await projects.HeadAsync(vertical.Id, fx.Ct);
var folder = fx.Paths.Sequence(sequence.Folder);
var path = folder.ProjectRecordPath(vertical.Id);
// A sync tool is bringing the record another PC wrote (a rename, a push): meanwhile the folder holds none. However often
// the index looks within the settling time, it writes none of its own and takes nothing for the project.
var incoming = ProjectSidecars.ReadRecord(path);
File.Delete(path);
for (var i = 0; i < 2; i++)
{
var held = (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
Assert.Equal(("Vertical", 1), (held.Name, held.HeadNumber));
}
Assert.False(File.Exists(path));
await Assert.ThrowsAsync<ProjectsUnreadableException>(() => projects.PushAsync(vertical.Id, Draft(first, new ProjectSettings { ExcludedFrames = [1] }, frames), fx.Ct));
// The record arrives, renamed and moved on there: it wins.
var theirs = Draft(first, new ProjectSettings { ExcludedFrames = [2] }, frames) with { StoredAt = DateTimeOffset.UtcNow };
ProjectSidecars.WriteVersion(folder.VersionPath(vertical.Id, 2, theirs.Id), theirs);
ProjectSidecars.WriteRecord(path, incoming with { Name = "Square", Head = new ProjectHead(theirs.Id, 2), Updated = DateTimeOffset.UtcNow });
var listed = (await projects.ListAsync(sequence.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
Assert.Equal(("Square", theirs.Id), (listed.Name, listed.HeadVersionId));
// A record really lost is written again from the index once the folder looked the same on two reconciles a settling time apart.
File.Delete(path);
projects.RecordSettle = TimeSpan.Zero;
await projects.ListAsync(sequence.Id, fx.Ct);
Assert.False(File.Exists(path));
await projects.ListAsync(sequence.Id, fx.Ct);
var written = ProjectSidecars.ReadRecord(path);
Assert.Equal(("Square", theirs.Id), (written.Name, written.Head!.Id));
Assert.Equal(PushOutcome.Stored, (await projects.PushAsync(vertical.Id, Draft(theirs, new ProjectSettings(), frames), fx.Ct)).Outcome);
}
[Test]
public async Task AProjectsFolderPutBackFromAnOlderBackupIsMadeWholeFromTheIndex()
{