feat(copies): engine composition, automatic photos, metered hold and App wiring (owner review 6, P5)

CopyEngine puts the planners together behind ICopyEngine so the App can find,
compare, integrate, merge, link, unlink and keep pairs apart. It also moves
photos that one side deleted to the other side's trash. Each trash step
re-reads both sides when applied, and nothing moves if any photo was restored
since the dry run. The sync agent now queues the new photos of pairs that were
integrated with automatic photos, both ways, at background priority. Failures
back off, and three in a row are reported so the pair can show "not sent". On
a metered connection, integrations are held like other server transfers.
AppServices builds the engine and registers the Integrate and Merge handlers.
A CopyWatch looks for local copies after start and after jobs that change
photos. Merges wait for workspace work that has not been sent home.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-17 12:03:56 +01:00
1 parent 461dfcf4a3
commit 96b5b4078b
11 files changed
+1377 -8

No files matched your search

+536
View File
@@ -0,0 +1,536 @@
using System.Net;
using Chronos.Core.Diagnostics;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Data.Repositories;
using Chronos.Data.Sync;
using Chronos.Protocol;
using Chronos.Protocol.Client;
namespace Chronos.Data.Copies;
/// <summary>
/// The matches engine of this PC (owner review 6, #73; design §5, §6): it lists the pairs with the facts of both sides,
/// looks for new ones (on the signed-in server through a refresh, and among this index's archives), compares a pair photo
/// by photo, plans and queues Integrate (<see cref="IntegratePlanner"/>, <see cref="CopyJobs"/>) and Merge
/// (<see cref="SequenceMerger"/>), records Keep apart and Ask again, links and unlinks, and moves photos a side deleted to a
/// trash after a dry run, re-checking at apply time that the other side still deleted them. Every pair it finds, or a
/// refresh of <see cref="SyncService"/> finds, is told through <see cref="Found"/>.
/// </summary>
/// <param name="context">What the planners know of this PC and the signed-in account, for a server's remote (null: no server).</param>
/// <param name="workspace">Whether a sequence has workspace work not sent home (a merge waits for it); null: none ever has.</param>
/// <param name="refresh">A full comparison with a server, which looks for matches on it (default: <see cref="SyncService.RefreshAsync"/>).</param>
/// <param name="serverNames">The name a server key is shown by (default: the key).</param>
/// <param name="followUps">The engine that queues what photos moved to a trash need again (null: none).</param>
/// <param name="workspaceProjects">The workspace projects by home and name, for the Edit picker's twins (null: none).</param>
public sealed class CopyEngine(SyncService sync, ArchiveService archives, IntegratePlanner integrate, SequenceMerger merge, CopyReviewBuilder reviews,
Func<ISyncRemote?, CopyPlanContext> context, IWorkspaceGuard? workspace = null, Func<ISyncRemote, CancellationToken, Task>? refresh = null,
Func<string, string?>? serverNames = null, Func<JobEngine?>? followUps = null,
Func<CancellationToken, Task<IReadOnlyList<(BundleHome Home, string Name)>>>? workspaceProjects = null) : ICopyEngine, IDisposable
{
private const string LogCategory = "copies";
/// <summary>Parameters per statement of the batched counts.</summary>
private const int Batch = 500;
private readonly SyncService _sync = sync ?? throw new ArgumentNullException(nameof(sync));
private readonly ArchiveService _archives = archives ?? throw new ArgumentNullException(nameof(archives));
private readonly IntegratePlanner _integrate = integrate ?? throw new ArgumentNullException(nameof(integrate));
private readonly SequenceMerger _merge = merge ?? throw new ArgumentNullException(nameof(merge));
private readonly CopyReviewBuilder _reviews = reviews ?? throw new ArgumentNullException(nameof(reviews));
private readonly Func<ISyncRemote?, CopyPlanContext> _context = context ?? throw new ArgumentNullException(nameof(context));
private readonly CopySides _sides = new(sync.Database);
private readonly DbAccess _access = new(sync.Database.Database);
/// <summary>One local detection at a time (Find matches and the watch after scans).</summary>
private readonly SemaphoreSlim _local = new(1, 1);
/// <summary>1 once <see cref="SyncService.CopiesFound"/> is followed (from the first listener of <see cref="Found"/> on).</summary>
private int _subscribed;
private Action<CopyFindResult>? _found;
public event Action<CopyFindResult>? Found
{
add
{
if (Interlocked.Exchange(ref _subscribed, 1) == 0) _sync.CopiesFound += OnCopiesFound;
_found += value;
}
remove => _found -= value;
}
private FoundCopyStore Store => _sync.Copies;
private ChronosDatabase Db => _sync.Database;
public void Dispose()
{
_sync.CopiesFound -= OnCopiesFound;
_local.Dispose();
}
// ---------------- List and find ----------------
public async Task<IReadOnlyList<FoundCopyView>> ListAsync(CancellationToken ct)
{
var rows = (await Store.ListAsync(ct).ConfigureAwait(false)).Where(c => c.IsListed).ToList();
if (rows.Count == 0) return [];
var plan = _context(null);
var archives = (await Db.Archives.ListAsync(ct).ConfigureAwait(false)).ToDictionary(a => a.Id, a => a.Name);
var linked = await _access.ListAsync("SELECT server, local_id FROM sync_links WHERE local_id IS NOT NULL", [],
r => (Server: r.GetString(0), Local: r.GetGuid(1)), ct).ConfigureAwait(false);
var serversOf = linked.ToLookup(l => l.Local, l => ServerName(l.Server));
var locals = new Dictionary<Guid, Sequence?>();
foreach (var id in rows.SelectMany(r => r.OtherId is { } other ? new[] { r.LocalId, other } : [r.LocalId]).Distinct())
locals[id] = await Db.Sequences.GetAsync(id, ct).ConfigureAwait(false);
var hashed = await HashedCountsAsync([.. locals.Keys], ct).ConfigureAwait(false);
var links = new Dictionary<string, Dictionary<Guid, SyncLink>>(StringComparer.Ordinal);
CopyFacts LocalFacts(Sequence s) => new(s.DisplayTitle, archives.GetValueOrDefault(s.ArchiveId) ?? string.Empty, s.ArchiveId, s.FrameCount, s.TotalBytes,
s.FirstCapture, s.LastCapture, s.CameraModel, hashed.GetValueOrDefault(s.Id), s.Origin, [.. serversOf[s.Id].Distinct().Order(StringComparer.CurrentCulture)],
plan.ServerOriginalsArchives.Contains(s.ArchiveId));
var views = new List<FoundCopyView>(rows.Count);
foreach (var row in rows)
{
if (locals.GetValueOrDefault(row.LocalId) is not { } local) continue;
CopyFacts there;
if (row.Kind == CopyKind.LocalLocal)
{
if (locals.GetValueOrDefault(row.OtherId!.Value) is not { } other) continue;
there = LocalFacts(other);
}
else
{
var server = row.Server!;
if (!links.TryGetValue(server, out var byRemote))
links[server] = byRemote = (await _sync.Links.ListAsync(server, ct).ConfigureAwait(false)).ToDictionary(l => l.RemoteId);
var link = byRemote.GetValueOrDefault(row.RemoteId!.Value);
there = new CopyFacts(link?.Title ?? string.Empty, ServerName(server), null, link?.FrameCount ?? 0, link?.TotalBytes ?? 0, null, null, null, 0, null, [],
false);
}
views.Add(new FoundCopyView(row, LocalFacts(local), there));
}
return views;
}
public async Task<CopyFindResult> FindAsync(ISyncRemote? remote, CancellationToken ct)
{
var server = CopyFindResult.Empty;
if (remote is not null)
{
await _sync.Finder.ResetProbesAsync(remote.Server, ct).ConfigureAwait(false);
// What the refresh finds is told by SyncService.CopiesFound (forwarded to Found); collected here for the caller.
var collected = new List<CopyFindResult>();
void Collect(string key, CopyFindResult result)
{
if (!string.Equals(key, remote.Server, StringComparison.Ordinal)) return;
lock (collected) collected.Add(result);
}
_sync.CopiesFound += Collect;
try
{
await (refresh is { } run ? run(remote, ct) : _sync.RefreshAsync(remote, new SyncPolicy(), ct)).ConfigureAwait(false);
}
finally
{
_sync.CopiesFound -= Collect;
}
lock (collected) server = collected.Aggregate(CopyFindResult.Empty, Join);
}
await _sync.Finder.ResetProbesAsync(CopyFinder.LocalScope, ct).ConfigureAwait(false);
var local = await FindLocalAsync(ct).ConfigureAwait(false);
return Join(server, local);
}
/// <summary>Local detection (design §3.4) without forgetting what was probed: what changed since is compared; told through <see cref="Found"/>.</summary>
public async Task<CopyFindResult> FindLocalAsync(CancellationToken ct)
{
await _local.WaitAsync(ct).ConfigureAwait(false);
CopyFindResult result;
try
{
result = await _sync.Finder.FindLocalAsync(ct).ConfigureAwait(false);
}
finally
{
_local.Release();
}
if (!result.IsEmpty) Tell(result);
return result;
}
// ---------------- Compare, integrate, merge ----------------
public async Task<CopyReview> CompareAsync(Guid copyId, ISyncRemote? remote, CancellationToken ct)
{
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
return await _reviews.BuildAsync(copy, copy.Kind == CopyKind.LocalServer ? remote : null, _context(remote), ct).ConfigureAwait(false);
}
public async Task<IntegratePlan> PlanIntegrateAsync(Guid copyId, ISyncRemote remote, IntegrateChoices choices, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(remote);
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
return await _integrate.PlanAsync(remote, copy, choices, _context(remote), ct).ConfigureAwait(false);
}
public async Task<Job> EnqueueIntegrateAsync(JobEngine engine, IntegratePlan plan, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(plan);
var local = await Db.Sequences.GetAsync(plan.LocalId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
return await CopyJobs.EnqueueIntegrateAsync(engine, Store, plan, local.DisplayTitle, ct).ConfigureAwait(false);
}
public async Task<MergePlan> PlanMergeAsync(Guid copyId, Guid? keepId, MergeChoices choices, CancellationToken ct)
{
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
if (copy is not { Kind: CopyKind.LocalLocal, OtherId: { } second })
throw new ArgumentException($"Pair {copyId} is not two sequences of this PC.", nameof(copyId));
// The sequence that goes is the one not kept; with none chosen the planner keeps the first unless one is preselected.
var other = keepId is { } keep ? keep == copy.LocalId ? second : copy.LocalId : second;
var plan = _context(null);
if (workspace is not null)
{
var unsent = new HashSet<Guid>(plan.UnsentWorkspaceSequences);
foreach (var id in new[] { copy.LocalId, second })
{
if (await workspace.HasUnsentAsync(id, ct).ConfigureAwait(false)) unsent.Add(id);
}
plan = plan with { UnsentWorkspaceSequences = unsent };
}
return await _merge.PlanAsync(copyId, keepId, other, choices, plan, ct).ConfigureAwait(false);
}
public async Task<Job> EnqueueMergeAsync(JobEngine engine, MergePlan plan, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(plan);
var keep = await Db.Sequences.GetAsync(plan.KeepId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
return await _merge.EnqueueAsync(engine, Store, plan, keep.DisplayTitle, ct).ConfigureAwait(false);
}
// ---------------- Answers and links ----------------
public async Task KeepApartAsync(Guid copyId, CancellationToken ct)
{
for (var attempt = 0; attempt < 3; attempt++)
{
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
switch (copy.State)
{
case CopyState.Integrating:
throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
case CopyState.KeptApart:
return;
case CopyState.Linked or CopyState.Integrated when copy is { Kind: CopyKind.LocalServer, Server: { } server, RemoteId: { } remoteId }:
// Kept apart from the server's copy it is linked to: the link goes (the server row stays as an observation).
await _sync.UnlinkAsync(server, remoteId, ct).ConfigureAwait(false);
if (await StateAsync(copyId, ct).ConfigureAwait(false) == CopyState.KeptApart) return;
break;
}
if (await Store.SetStateAsync(copyId, copy.State, CopyState.KeptApart, Answer(copy), ct).ConfigureAwait(false))
{
Log.Info(LogCategory, $"Pair {copyId} kept apart");
return;
}
}
throw new StaleCopyPlanException();
}
public async Task AskAgainAsync(Guid copyId, CancellationToken ct)
{
_ = await RequireAsync(copyId, ct).ConfigureAwait(false);
if (await Store.SetStateAsync(copyId, CopyState.KeptApart, CopyState.Candidate, null, ct).ConfigureAwait(false))
Log.Info(LogCategory, $"Pair {copyId} asked again");
}
public async Task LinkAsync(Guid copyId, ISyncRemote remote, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(remote);
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
if (copy is not { Kind: CopyKind.LocalServer, Server: { } server, RemoteId: { } remoteId } || !string.Equals(server, remote.Server, StringComparison.Ordinal))
throw new ArgumentException($"Pair {copyId} is not a pair with a sequence of {remote.Server}.", nameof(copyId));
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
var local = await Db.Sequences.GetAsync(copy.LocalId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
var mine = await _sync.LinkOfAsync(server, local.Id, ct).ConfigureAwait(false);
var theirs = await _sync.Links.GetAsync(server, remoteId, ct).ConfigureAwait(false);
if ((mine is not null && mine.RemoteId != remoteId) || (theirs?.LocalId is { } holder && holder != local.Id))
throw CopyLinkTakenException.For(local.DisplayTitle, ServerName(server));
var before = copy.State;
var moved = before is not (CopyState.Linked or CopyState.Integrated);
if (moved && !await Store.SetStateAsync(copyId, before, CopyState.Linked, null, ct).ConfigureAwait(false)) throw new StaleCopyPlanException();
try
{
await _sync.LinkAsync(server, remoteId, local.Id, ct).ConfigureAwait(false);
}
catch
{
if (moved) await Store.SetStateAsync(copyId, CopyState.Linked, before, null, CancellationToken.None).ConfigureAwait(false);
throw;
}
Log.Info(LogCategory, $"{local.DisplayTitle} linked to sequence {remoteId} of {server}");
// Compared as a new link: equal details agree, different ones become a conflict; nothing is pushed by itself.
await _sync.RefreshSequencesAsync(remote, [remoteId], SyncService.CompareOnly, ct).ConfigureAwait(false);
}
public async Task UnlinkAsync(Guid copyId, ISyncRemote? remote, CancellationToken ct)
{
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
if (copy is not { Kind: CopyKind.LocalServer, Server: { } server, RemoteId: { } remoteId })
{
await KeepApartAsync(copyId, ct).ConfigureAwait(false);
return;
}
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
await _sync.UnlinkAsync(server, remoteId, ct).ConfigureAwait(false);
// No link held the pair (a candidate): it is kept apart all the same.
if (await StateAsync(copyId, ct).ConfigureAwait(false) is { } state && state != CopyState.KeptApart)
await Store.SetStateAsync(copyId, state, CopyState.KeptApart, Answer(copy), ct).ConfigureAwait(false);
}
public async Task KeepHereAsync(Guid copyId, CancellationToken ct)
{
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
// Kept apart: the gone mark stays, so the row reads "kept here" and Upload all local skips it.
if (!await Store.SetStateAsync(copyId, copy.State, CopyState.KeptApart, Answer(copy), ct).ConfigureAwait(false)) throw new StaleCopyPlanException();
Log.Info(LogCategory, $"Pair {copyId} kept here after its server copy was deleted");
}
// ---------------- Deleted on one side ----------------
public async Task<FrameDeletePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(photoKeys);
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
var sequence = await Db.Sequences.GetAsync(copy.LocalId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
var frames = FramesOf(here, SyncTombstones.FromDtos(here.Removed, null), photoKeys);
if (frames.Count == 0) throw new InvalidOperationException(Loc.L("Errors.Copies.NotThere"));
return await _archives.FrameTrash.PlanDeleteAsync(copy.LocalId, [.. frames.Select(f => f.FrameId)], sequence.FramesVersion, ct).ConfigureAwait(false);
}
public async Task<int> TrashHereAsync(Guid copyId, FrameDeletePlan plan, ISyncRemote? remote, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(plan);
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
if (plan.Sequence.Id != copy.LocalId) throw new ArgumentException($"The plan is not of pair {copyId}'s sequence.", nameof(plan));
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
// The other side as it is now: each photo must still be deleted there, and not held there again (restored from its trash).
CopySideInput there;
if (copy.Kind == CopyKind.LocalServer)
{
if (remote is null || !string.Equals(remote.Server, copy.Server, StringComparison.Ordinal))
throw new ArgumentException("A pair with a server sequence is checked over that server's session.", nameof(remote));
there = await _sides.RemoteAsync(remote, copy.RemoteId!.Value, ct).ConfigureAwait(false);
}
else
{
there = await _sides.LocalAsync(copy.OtherId!.Value, ct).ConfigureAwait(false);
}
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
var tombstones = SyncTombstones.FromDtos(here.Removed, there.Removed);
var held = Keys(there, tombstones);
var refused = plan.Frames.Count(f => !tombstones.DeletedRemotely(f.Sha256) || held.Contains(tombstones.Canonical(f.Sha256) ?? string.Empty));
if (refused > 0)
{
Log.Info(LogCategory, $"Pair {copyId}: {refused} photo(s) are no longer deleted on the other side; none went to the trash");
throw new StaleCopyPlanException(Loc.L("Errors.Copies.NotThere"));
}
var result = await _archives.FrameTrash.DeleteAsync(plan, _context(remote).PcName, followUps?.Invoke(), ct).ConfigureAwait(false);
Log.Info(LogCategory, $"{plan.Frames.Count} photo(s) of {result.Sequence.DisplayTitle} deleted on the other side went to the trash");
await RecountAsync(copy, remote, ct).ConfigureAwait(false);
return plan.Frames.Count;
}
public async Task<TrashTherePlan> PlanTrashThereAsync(Guid copyId, ISyncRemote remote, IReadOnlyCollection<string> photoKeys, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(remote);
ArgumentNullException.ThrowIfNull(photoKeys);
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
var (server, remoteId) = ServerSide(copy, remote);
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
var there = await _sides.RemoteAsync(remote, remoteId, ct).ConfigureAwait(false);
var frames = FramesOf(there, SyncTombstones.FromDtos(here.Removed, there.Removed), photoKeys);
if (frames.Count == 0) throw new InvalidOperationException(Loc.L("Errors.Copies.NotThere"));
var dryRun = await ServerAsync(() => remote.DeleteFramesAsync(remoteId, new FrameDeleteRequest([.. frames.Select(f => f.FrameId)], null, DryRun: true), ct))
.ConfigureAwait(false);
var hashes = frames.Select(f => f.Sha256).OfType<string>().ToList();
var elsewhere = await _sides.HeldElsewhereAsync(hashes, [], ct).ConfigureAwait(false);
var lasts = frames.Where(f => f.Sha256 is not { Length: > 0 } sha || !elsewhere.Contains(sha.ToLowerInvariant()))
.Select(f => new LastCopy(f.Sha256?.ToLowerInvariant() ?? string.Empty, CopySide.Other, f.FileName, f.Size)).ToList();
var keys = photoKeys.Where(k => frames.Any(f => Matches(f, k, SyncTombstones.FromDtos(here.Removed, there.Removed)))).Distinct(StringComparer.Ordinal).ToList();
return new TrashTherePlan(copyId, server, remoteId, [.. frames.Select(f => f.FrameId)], keys, dryRun.Photos, dryRun.Bytes, dryRun.FramesVersion, lasts);
}
public async Task<int> TrashThereAsync(TrashTherePlan plan, ISyncRemote remote, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(plan);
ArgumentNullException.ThrowIfNull(remote);
var copy = await RequireAsync(plan.CopyId, ct).ConfigureAwait(false);
var (_, remoteId) = ServerSide(copy, remote);
if (remoteId != plan.RemoteId) throw new ArgumentException($"The plan is not of pair {plan.CopyId}'s server sequence.", nameof(plan));
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
// This side as it is now: each photo must still be deleted here, and not held here again (restored from the trash).
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
var there = await _sides.RemoteAsync(remote, remoteId, ct).ConfigureAwait(false);
var tombstones = SyncTombstones.FromDtos(here.Removed, there.Removed);
var held = Keys(here, tombstones);
var refused = plan.Keys.Count(k => !tombstones.DeletedLocally(k) || held.Contains(tombstones.Canonical(k) ?? string.Empty));
if (plan.Keys.Count == 0 || refused > 0)
{
Log.Info(LogCategory, $"Pair {plan.CopyId}: {refused} photo(s) are no longer deleted on this PC; none went to the server's trash");
throw new StaleCopyPlanException(Loc.L("Errors.Copies.NotThere"));
}
var done = await ServerAsync(() => remote.DeleteFramesAsync(remoteId, new FrameDeleteRequest(plan.FrameIds, plan.FramesVersion, DryRun: false), ct))
.ConfigureAwait(false);
Log.Info(LogCategory, $"{done.Photos} photo(s) of server sequence {remoteId} deleted on this PC went to the server's trash");
await _sync.RefreshSequencesAsync(remote, [remoteId], SyncService.CompareOnly, ct).ConfigureAwait(false);
return done.Photos;
}
// ---------------- Twins ----------------
public async Task<IReadOnlyList<CopyTwin>> TwinsAsync(CancellationToken ct)
{
if (workspaceProjects is null) return [];
var pairs = (await Store.ListAsync(ct).ConfigureAwait(false))
.Where(c => c is { Kind: CopyKind.LocalServer, State: CopyState.Linked or CopyState.Integrated, Server: not null, RemoteId: not null }).ToList();
if (pairs.Count == 0) return [];
var projects = await workspaceProjects(ct).ConfigureAwait(false);
var identity = _sync.Options.Identity?.Invoke() ?? ServerIdentity.None;
var archives = (await Db.Archives.ListAsync(ct).ConfigureAwait(false)).ToDictionary(a => a.Id, a => a.Name);
var twins = new List<CopyTwin>();
foreach (var pair in pairs)
{
var here = projects.FirstOrDefault(p => p.Home.IsArchive && p.Home.SequenceId == pair.LocalId).Name;
var there = projects.FirstOrDefault(p => !p.Home.IsArchive && p.Home.SequenceId == pair.RemoteId && identity.Same(p.Home.Server, pair.Server)).Name;
if (here is null && there is null) continue;
var local = await Db.Sequences.GetAsync(pair.LocalId, ct).ConfigureAwait(false);
twins.Add(new CopyTwin(pair.Id, pair.LocalId, local is null ? string.Empty : archives.GetValueOrDefault(local.ArchiveId) ?? string.Empty, here, pair.Server!,
pair.RemoteId!.Value, there));
}
return twins;
}
// ---------------- Helpers ----------------
private void OnCopiesFound(string server, CopyFindResult result)
{
if (!result.IsEmpty) Tell(result);
}
/// <summary>Tells <see cref="Found"/>; what a listener throws is logged, never stops detection.</summary>
private void Tell(CopyFindResult result)
{
try
{
_found?.Invoke(result);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
Log.Warning(LogCategory, "Matches found were not told", ex);
}
}
private static CopyFindResult Join(CopyFindResult a, CopyFindResult b) =>
a.IsEmpty ? b : b.IsEmpty ? a : new([.. a.New, .. b.New], [.. a.AutoLinked, .. b.AutoLinked], [.. a.AskedAgain, .. b.AskedAgain]);
private async Task<FoundCopy> RequireAsync(Guid copyId, CancellationToken ct) =>
await Store.GetAsync(copyId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
private async Task<CopyState?> StateAsync(Guid copyId, CancellationToken ct) => (await Store.GetAsync(copyId, ct).ConfigureAwait(false))?.State;
/// <summary>The answer of Keep apart: the fingerprint and shared count it was given at, so a large later change asks again.</summary>
private static CopyAnswer Answer(FoundCopy copy) => new(copy.Fingerprint, copy.Score.Shared, null, null, DateTimeOffset.UtcNow);
private string ServerName(string server) => serverNames?.Invoke(server) is { Length: > 0 } name ? name : server;
private static (string Server, Guid RemoteId) ServerSide(FoundCopy copy, ISyncRemote remote)
{
if (copy is not { Kind: CopyKind.LocalServer, Server: { } server, RemoteId: { } remoteId } || !string.Equals(server, remote.Server, StringComparison.Ordinal))
throw new ArgumentException($"Pair {copy.Id} is not a pair with a sequence of {remote.Server}.", nameof(copy));
return (server, remoteId);
}
/// <summary>The usable frames of <paramref name="side"/> holding one of <paramref name="keys"/> (every copy of the photo in it).</summary>
private static List<CopyPhotoInput> FramesOf(CopySideInput side, SyncTombstones tombstones, IReadOnlyCollection<string> keys)
{
var wanted = keys.ToHashSet(StringComparer.Ordinal);
return [.. side.Photos.Where(p => !p.Unusable && wanted.Any(k => Matches(p, k, tombstones))).OrderBy(p => p.Index)];
}
/// <summary>A photo key (design §4.1) names this frame: its SHA-256 as compared or as it is, or its size and capture time while unhashed.</summary>
private static bool Matches(CopyPhotoInput frame, string key, SyncTombstones tombstones) => string.IsNullOrEmpty(frame.Sha256)
? string.Equals(CopyComparer.UnhashedKey(frame.Size, frame.CapturedAt, frame.FrameId), key, StringComparison.Ordinal)
: string.Equals(frame.Sha256, key, StringComparison.OrdinalIgnoreCase) || string.Equals(tombstones.Canonical(frame.Sha256), key, StringComparison.Ordinal);
/// <summary>The photo keys a side holds in a usable frame, as compared.</summary>
private static HashSet<string> Keys(CopySideInput side, SyncTombstones tombstones) =>
side.Photos.Where(p => !p.Unusable && !string.IsNullOrEmpty(p.Sha256)).Select(p => tombstones.Canonical(p.Sha256)!).ToHashSet(StringComparer.Ordinal);
/// <summary>The deletion counts of a pair once photos went to a trash: compared again (server), or counted again here (two local copies).</summary>
private async Task RecountAsync(FoundCopy copy, ISyncRemote? remote, CancellationToken ct)
{
try
{
if (copy is { Kind: CopyKind.LocalServer, RemoteId: { } remoteId } && remote is not null)
{
await _sync.RefreshSequencesAsync(remote, [remoteId], SyncService.CompareOnly, ct).ConfigureAwait(false);
return;
}
if (copy.OtherId is not { } otherId) return;
var first = await Db.Sequences.GetAsync(copy.LocalId, ct).ConfigureAwait(false);
var second = await Db.Sequences.GetAsync(otherId, ct).ConfigureAwait(false);
if (first is null || second is null) return;
var score = CopyComparer.Compare(await _sides.LocalAsync(first.Id, ct).ConfigureAwait(false), await _sides.LocalAsync(second.Id, ct).ConfigureAwait(false))
.Score with
{ Hints = copy.Score.Hints };
await Store.UpdateScoreAsync(copy.Id, score, first.FramesVersion, second.FramesVersion,
CopyRules.Fingerprint(score, first.FramesVersion, second.FramesVersion), ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is DatabaseException or HttpRequestException or ChronosApiException or IOException)
{
// The trash step is done: the counts follow at the next comparison.
Log.Warning(LogCategory, $"The deletion counts of pair {copy.Id} could not be updated", ex);
}
}
/// <summary>A server's refusal of a trash step as the dialog shows it: the sequence moved (a stale plan) or the Delete right is missing.</summary>
private static async Task<T> ServerAsync<T>(Func<Task<T>> call)
{
try
{
return await call().ConfigureAwait(false);
}
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.Conflict)
{
throw new StaleCopyPlanException(Loc.L("Errors.Copies.StalePlan"), ex);
}
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.Forbidden)
{
throw new InvalidOperationException(Loc.L("Errors.Copies.NoDeleteRight"), ex);
}
}
/// <summary>How many frames of each sequence the index has hashed.</summary>
private async Task<Dictionary<Guid, int>> HashedCountsAsync(IReadOnlyList<Guid> ids, CancellationToken ct)
{
var counts = new Dictionary<Guid, int>();
foreach (var chunk in ids.Distinct().Chunk(Batch))
{
var p = new SqlParameters();
var list = string.Join(", ", chunk.Select(id => p.Add(id)));
var rows = await _access.ListAsync(
$"SELECT sequence_id, count(*) FROM frames WHERE sequence_id IN ({list}) AND sha256 IS NOT NULL AND sha256 <> '' GROUP BY sequence_id",
p.ToArray(), r => (Id: r.GetGuid(0), Count: r.GetInt32(1)), ct).ConfigureAwait(false);
foreach (var (id, count) in rows) counts[id] = count;
}
return counts;
}
}
+104 -1
View File
@@ -3,6 +3,7 @@ using System.Text.Json;
using System.Threading.Channels;
using Chronos.Core.Diagnostics;
using Chronos.Core.Model;
using Chronos.Data.Copies;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Protocol.Client;
@@ -24,13 +25,21 @@ public sealed record SyncAgentOptions
public TimeSpan MaxBackoff { get; init; } = TimeSpan.FromMinutes(5);
}
/// <summary>
/// The automatic photo transfers of an integrated pair that failed in a row (owner review 6, #73; design §7.3): how many,
/// and why the last one did.
/// </summary>
public sealed record AutoPhotoFailure(int Count, string Reason);
/// <summary>
/// Keeps the sync table of one signed-in server current in the background: a full comparison at start, on request
/// and every <see cref="SyncAgentOptions.RefreshInterval"/>; the sequences named by the server's <c>sequenceChanged</c>
/// events (WebSocket) compared again within a second, except those an upload or download is working on (it compares
/// them when it ends); failures retried with exponential backoff; and, with
/// <see cref="SyncPolicy.KeepProxiesOffline"/>, background downloads that bring stale proxy caches of the sequences
/// this PC opened with proxies up to date (a failed one waits with the same backoff). Dispose to stop.
/// this PC opened with proxies up to date (a failed one waits with the same backoff); and, for the pairs integrated with
/// automatic photos (owner review 6, #73), background transfers of the photos one side added since (a failed one waits
/// with the same backoff, and three in a row are told through <see cref="PhotosNotSent"/>). Dispose to stop.
/// </summary>
public sealed class SyncAgent : IAsyncDisposable
{
@@ -57,6 +66,14 @@ public sealed class SyncAgent : IAsyncDisposable
/// </summary>
private readonly Dictionary<Guid, DateTimeOffset> _standInsCompared = [];
/// <summary>Automatic photo transfers this agent queued, by local sequence and direction (true: upload).</summary>
private readonly Dictionary<(Guid Local, bool Upload), Guid> _photoJobs = [];
/// <summary>When the next automatic transfer of a local sequence whose last one failed may be queued.</summary>
private readonly Dictionary<Guid, DateTime> _photoNotBefore = [];
private readonly ConcurrentDictionary<Guid, AutoPhotoFailure> _photoFailures = new();
private readonly SemaphoreSlim _refreshing = new(1, 1);
private Task _running = Task.CompletedTask;
@@ -102,6 +119,15 @@ public sealed class SyncAgent : IAsyncDisposable
/// </summary>
public event Action<ProjectChangedEvent?>? ProjectChanged;
/// <summary>Failed automatic photo transfers in a row, by local sequence of an integrated pair; a finished one clears its entry.</summary>
public IReadOnlyDictionary<Guid, AutoPhotoFailure> PhotoFailures => _photoFailures;
/// <summary>The automatic photo transfers of a local sequence failed <see cref="NotSentAfter"/> times in a row (any thread).</summary>
public event Action<Guid, AutoPhotoFailure>? PhotosNotSent;
/// <summary>Failed automatic transfers in a row after which a pair reads "not sent".</summary>
public const int NotSentAfter = 3;
/// <summary>Starts the comparison and event loops.</summary>
public void Start()
{
@@ -130,6 +156,7 @@ public sealed class SyncAgent : IAsyncDisposable
Volatile.Write(ref _snapshot, snapshot);
Volatile.Write(ref _lastError, null);
if (policy.KeepProxiesOffline && _engine is not null) await QueueProxiesAsync(snapshot, linked.Token).ConfigureAwait(false);
if (_engine is not null) await QueuePhotosAsync(snapshot, linked.Token).ConfigureAwait(false);
Changed?.Invoke();
if (reopened && Interlocked.CompareExchange(ref _projectsMissed, InStep, Reopened) == Reopened) TellProjects(null);
return snapshot;
@@ -324,6 +351,82 @@ public sealed class SyncAgent : IAsyncDisposable
}
}
/// <summary>
/// Queues (background priority) the photos one side of a pair integrated with automatic photos added since: an upload
/// when the server lacks originals of the local copy, a download into the local copy's archive when this PC lacks the
/// server's. Never for a pair only linked, one integrated without automatic photos, or one whose server copy looks
/// deleted. A local sequence whose last automatic transfer failed waits for its backoff. What fails here is logged: the
/// comparison it follows stands.
/// </summary>
private async Task QueuePhotosAsync(SyncSnapshot snapshot, CancellationToken cancellationToken)
{
try
{
await CheckPhotoJobsAsync(cancellationToken).ConfigureAwait(false);
var pairs = (await _sync.Copies.ListByServerAsync(Server, cancellationToken).ConfigureAwait(false))
.Where(c => c is { Kind: CopyKind.LocalServer, State: CopyState.Integrated, AutoPhotos: true, RemoteGoneHits: 0, RemoteId: not null })
.ToDictionary(c => c.LocalId, c => c.RemoteId!.Value);
if (pairs.Count == 0) return;
foreach (var entry in snapshot.Entries)
{
if (entry is not { Local: { } local, Link: { } link, Transfer: null }) continue;
if (!pairs.TryGetValue(local.Id, out var remoteId) || remoteId != link.RemoteId) continue;
if (_photoNotBefore.TryGetValue(local.Id, out var notBefore) && DateTime.UtcNow < notBefore) continue;
if (link.MissingRemote > 0 && !_photoJobs.ContainsKey((local.Id, true)))
{
var job = await _sync.EnqueueUploadAsync(_engine!, Server, local, JobPriority.Background, cancellationToken).ConfigureAwait(false);
_photoJobs[(local.Id, true)] = job.Id;
}
if (link.MissingLocal > 0 && !_photoJobs.ContainsKey((local.Id, false)))
{
var job = await _sync.EnqueueDownloadAsync(_engine!, Server, link.RemoteId, link.Title, SyncDownloadMode.Originals, local.ArchiveId,
JobPriority.Background, cancellationToken).ConfigureAwait(false);
_photoJobs[(local.Id, false)] = job.Id;
}
}
}
catch (Exception ex) when (!cancellationToken.IsCancellationRequested && ex is not OutOfMemoryException)
{
Log.Warning(LogCategory, $"New photos of integrated pairs on {Server} were not queued", ex);
}
}
/// <summary>Settles the automatic transfers that ended: a failed one counts and backs off, a finished one clears the count.</summary>
private async Task CheckPhotoJobsAsync(CancellationToken cancellationToken)
{
foreach (var (key, jobId) in _photoJobs.ToList())
{
var job = await _sync.Database.Jobs.GetAsync(jobId, cancellationToken).ConfigureAwait(false);
if (job?.State is JobState.Queued or JobState.Running or JobState.Paused) continue;
_photoJobs.Remove(key);
if (job?.State == JobState.Failed)
{
var failure = _photoFailures.AddOrUpdate(key.Local, _ => new AutoPhotoFailure(1, job.Error ?? string.Empty),
(_, last) => new AutoPhotoFailure(last.Count + 1, job.Error ?? last.Reason));
_photoNotBefore[key.Local] = DateTime.UtcNow + Backoff(failure.Count);
Log.Info(LogCategory, $"Automatic photo transfer of sequence {key.Local} with {Server} failed ({failure.Count} in a row): {failure.Reason}");
if (failure.Count == NotSentAfter) TellNotSent(key.Local, failure);
}
else if (job?.State == JobState.Done)
{
_photoFailures.TryRemove(key.Local, out _);
_photoNotBefore.Remove(key.Local);
}
}
}
private void TellNotSent(Guid localId, AutoPhotoFailure failure)
{
try
{
PhotosNotSent?.Invoke(localId, failure);
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
Log.Warning(LogCategory, $"Photos not sent to {Server} were not told", ex);
}
}
/// <summary>Whether the cache was compared with the server's sequence, or its download queued, at the sequence's current update time.</summary>
private bool ComparedNow(SyncLink link) =>
_standInsCompared.TryGetValue(link.RemoteId, out var compared) && SyncService.SameMoment(compared, link.RemoteUpdatedAt);