fix(copies): address the owner review 6 findings
The review of the found-copies work showed pairs that could stick, jobs that could act on a pair the user had answered otherwise, and detection gaps: - forgetting a link on an address-keyed server deletes the pair and Candidate rows of that remote and resets the probe, so the sequence is found again when it is back; a Linked/Integrated row with no link is asked again - 404s count only on id-keyed servers, at most once per recheck interval - probes store the server's newest update time (copy_probes.server_newest, migration 0012) instead of comparing with this PC's clock; the lookup sample carries hashed photos by size and capture time too, for servers that have not hashed their copy - the older-server gate no longer re-compares unchanged manifests - a failed Integrate keeps its job id so a retry resumes only an untouched pair; cancel and stale plans release the pair (ReleaseAsync) - automatic photo transfers never create or retarget a sequence - a first upload files photos the server holds elsewhere into the new sequence - Unlink re-compares before keeping apart; Trash here names only true last copies; Drop... trashes one version on both sides after re-checking the other - a merge keeping the pair's other sequence swaps the dialog's columns and re-keys Different choices; Upload all reads every pair Each fix has a regression test where it makes sense. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
96b5b4078b
commit
fe752a0a21
33 files changed
+957
-123
No files matched your search
@@ -97,10 +97,8 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
{
|
||||
Log.Warning("archive", $"The journal of the cancelled merge {job.Id} was not removed", ex);
|
||||
}
|
||||
var store = new FoundCopyStore(db);
|
||||
if (await store.GetAsync(parameters.CopyId, CancellationToken.None).ConfigureAwait(false) is { JobId: { } owner } copy && owner == job.Id
|
||||
&& copy.State != CopyState.Integrating)
|
||||
await store.SetStateAsync(copy.Id, copy.State, copy.State, null, CancellationToken.None).ConfigureAwait(false);
|
||||
// By the stored row: the engine marked the job Cancelled first, so the row already reads as its state before.
|
||||
await new FoundCopyStore(db).ReleaseAsync(parameters.CopyId, job.Id, null, keepJob: false, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>The first run: plans again, refuses a plan that moved or is blocked, and journals it.</summary>
|
||||
@@ -416,8 +414,9 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
}
|
||||
if (failed.Count > 0)
|
||||
{
|
||||
// A photo held by another sequence of the index is not lost with the other; any other is.
|
||||
var others = await new CopySides(_db).HeldElsewhereAsync(failed, [journal.DropId], _ct).ConfigureAwait(false);
|
||||
// A photo held by another sequence of the index is not lost with the other; any other is. The kept
|
||||
// sequence's index rows prove nothing: its files were hashed again above and did not hold it.
|
||||
var others = await new CopySides(_db).HeldElsewhereAsync(failed, [journal.DropId, journal.KeepId], _ct).ConfigureAwait(false);
|
||||
var lost = failed.Count(s => !others.Contains(s));
|
||||
if (lost > 0) throw new InvalidOperationException(Loc.Plural("Errors.Copies.VerifyFailed", lost));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
@@ -118,6 +119,8 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
return views;
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<FoundCopy>> ListAllPairsAsync(CancellationToken ct) => Store.ListAsync(ct);
|
||||
|
||||
public async Task<CopyFindResult> FindAsync(ISyncRemote? remote, CancellationToken ct)
|
||||
{
|
||||
var server = CopyFindResult.Empty;
|
||||
@@ -286,6 +289,18 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
return;
|
||||
}
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
if (copy.State is CopyState.Linked or CopyState.Integrated && remote is not null && string.Equals(remote.Server, server, StringComparison.Ordinal))
|
||||
{
|
||||
// The answer is measured against what the two hold now, not at the last comparison.
|
||||
try
|
||||
{
|
||||
await _sync.RecompareAsync(remote, remoteId, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or ChronosApiException { Status: not HttpStatusCode.Unauthorized })
|
||||
{
|
||||
Log.Warning(LogCategory, $"Pair {copyId} could not be compared again before its Unlink; its last comparison is kept", ex);
|
||||
}
|
||||
}
|
||||
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)
|
||||
@@ -303,7 +318,7 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
|
||||
// ---------------- Deleted on one side ----------------
|
||||
|
||||
public async Task<FrameDeletePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct)
|
||||
public async Task<TrashHerePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(photoKeys);
|
||||
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
|
||||
@@ -311,7 +326,83 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
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);
|
||||
var plan = await _archives.FrameTrash.PlanDeleteAsync(copy.LocalId, [.. frames.Select(f => f.FrameId)], sequence.FramesVersion, ct).ConfigureAwait(false);
|
||||
// The other side deleted these photos: what no other sequence of this index holds goes with its last copy.
|
||||
var lasts = await LastCopiesAsync(frames, CopySide.ThisPc, [copy.LocalId], ct).ConfigureAwait(false);
|
||||
return new TrashHerePlan(plan, lasts);
|
||||
}
|
||||
|
||||
public async Task<DropVersionPlan> PlanDropVersionAsync(Guid copyId, string key, ISyncRemote? remote, CancellationToken ct)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(key);
|
||||
var copy = await RequireAsync(copyId, ct).ConfigureAwait(false);
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
var (here, there, remoteId) = await BothSidesAsync(copy, remote, ct).ConfigureAwait(false);
|
||||
var tombstones = SyncTombstones.FromDtos(here.Removed, there.Removed);
|
||||
var partner = PartnerOf(CopyComparer.Compare(here, there), key) ?? throw new StaleCopyPlanException(Loc.L("Errors.Copies.OtherVersionGone"));
|
||||
var mine = FramesOf(here, tombstones, [key]);
|
||||
var theirs = FramesOf(there, tombstones, [key]);
|
||||
if (mine.Count == 0 || theirs.Count == 0) throw new StaleCopyPlanException(Loc.L("Errors.Copies.NotThere"));
|
||||
var sequence = await Db.Sequences.GetAsync(copy.LocalId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
var herePlan = await _archives.FrameTrash.PlanDeleteAsync(copy.LocalId, [.. mine.Select(f => f.FrameId)], sequence.FramesVersion, ct).ConfigureAwait(false);
|
||||
FrameDeletePlan? otherPlan = null;
|
||||
var (photos, bytes, version) = (0, 0L, 0);
|
||||
if (remoteId is { } id)
|
||||
{
|
||||
var dryRun = await ServerAsync(() => remote!.DeleteFramesAsync(id, new FrameDeleteRequest([.. theirs.Select(f => f.FrameId)], null, DryRun: true), ct))
|
||||
.ConfigureAwait(false);
|
||||
(photos, bytes, version) = (dryRun.Photos, dryRun.Bytes, dryRun.FramesVersion);
|
||||
}
|
||||
else
|
||||
{
|
||||
var other = await Db.Sequences.GetAsync(copy.OtherId!.Value, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
otherPlan = await _archives.FrameTrash.PlanDeleteAsync(other.Id, [.. theirs.Select(f => f.FrameId)], other.FramesVersion, ct).ConfigureAwait(false);
|
||||
}
|
||||
Guid[] pairSides = copy.OtherId is { } o ? [copy.LocalId, o] : [copy.LocalId];
|
||||
var lasts = await LastCopiesAsync([.. mine.Take(1)], CopySide.ThisPc, pairSides, ct).ConfigureAwait(false);
|
||||
return new DropVersionPlan(copyId, key, partner, herePlan, otherPlan, remoteId, remoteId is null ? [] : [.. theirs.Select(f => f.FrameId)], photos, bytes,
|
||||
version, lasts);
|
||||
}
|
||||
|
||||
public async Task<int> DropVersionAsync(DropVersionPlan plan, ISyncRemote? remote, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(plan);
|
||||
var copy = await RequireAsync(plan.CopyId, ct).ConfigureAwait(false);
|
||||
if (plan.Here.Sequence.Id != copy.LocalId) throw new ArgumentException($"The plan is not of pair {plan.CopyId}'s sequence.", nameof(plan));
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
var (here, there, remoteId) = await BothSidesAsync(copy, remote, ct).ConfigureAwait(false);
|
||||
if (remoteId != plan.RemoteId) throw new ArgumentException($"The plan is not of pair {plan.CopyId}'s other sequence.", nameof(plan));
|
||||
// The other version must still be on both sides, by content: this PC's files hashed again now, the server's fresh manifest.
|
||||
var tombstones = SyncTombstones.FromDtos(here.Removed, there.Removed);
|
||||
if (!await HoldsVerifiedAsync(copy.LocalId, FramesOf(here, tombstones, [plan.PartnerKey]), ct).ConfigureAwait(false)
|
||||
|| !(remoteId is not null
|
||||
? FramesOf(there, tombstones, [plan.PartnerKey]).Count > 0
|
||||
: await HoldsVerifiedAsync(copy.OtherId!.Value, FramesOf(there, tombstones, [plan.PartnerKey]), ct).ConfigureAwait(false)))
|
||||
{
|
||||
Log.Info(LogCategory, $"Pair {plan.CopyId}: the version kept is no longer on both sides; nothing went to a trash");
|
||||
throw new StaleCopyPlanException(Loc.L("Errors.Copies.OtherVersionGone"));
|
||||
}
|
||||
// The local plans are checked before the server moves anything, so a stale one moves nothing anywhere.
|
||||
foreach (var local in new[] { plan.Here, plan.Other }.OfType<FrameDeletePlan>())
|
||||
{
|
||||
if ((await Db.Sequences.GetAsync(local.Sequence.Id, ct).ConfigureAwait(false))?.FramesVersion != local.Sequence.FramesVersion)
|
||||
throw new StaleCopyPlanException();
|
||||
}
|
||||
var moved = 0;
|
||||
if (remoteId is { } id)
|
||||
{
|
||||
var done = await ServerAsync(() => remote!.DeleteFramesAsync(id, new FrameDeleteRequest(plan.RemoteFrameIds, plan.RemoteFramesVersion, DryRun: false), ct))
|
||||
.ConfigureAwait(false);
|
||||
moved += done.Photos;
|
||||
}
|
||||
foreach (var local in new[] { plan.Here, plan.Other }.OfType<FrameDeletePlan>())
|
||||
{
|
||||
await _archives.FrameTrash.DeleteAsync(local, _context(remote).PcName, followUps?.Invoke(), ct).ConfigureAwait(false);
|
||||
moved += local.Frames.Count;
|
||||
}
|
||||
Log.Info(LogCategory, $"Pair {plan.CopyId}: one version of a photo kept in two went to the trash of both sides");
|
||||
await RecountAsync(copy, remote, ct).ConfigureAwait(false);
|
||||
return moved;
|
||||
}
|
||||
|
||||
public async Task<int> TrashHereAsync(Guid copyId, FrameDeletePlan plan, ISyncRemote? remote, CancellationToken ct)
|
||||
@@ -470,6 +561,58 @@ public sealed class CopyEngine(SyncService sync, ArchiveService archives, Integr
|
||||
? 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 pair's two sides as they are now, with the server sequence of a server pair (checked over <paramref name="remote"/>).</summary>
|
||||
private async Task<(CopySideInput Here, CopySideInput There, Guid? RemoteId)> BothSidesAsync(FoundCopy copy, ISyncRemote? remote, CancellationToken ct)
|
||||
{
|
||||
var here = await _sides.LocalAsync(copy.LocalId, ct).ConfigureAwait(false);
|
||||
if (copy.Kind == CopyKind.LocalLocal) return (here, await _sides.LocalAsync(copy.OtherId!.Value, ct).ConfigureAwait(false), null);
|
||||
if (remote is null) throw new ArgumentException("A pair with a server sequence is checked over that server's session.", nameof(remote));
|
||||
var (_, remoteId) = ServerSide(copy, remote);
|
||||
return (here, await _sides.RemoteAsync(remote, remoteId, ct).ConfigureAwait(false), remoteId);
|
||||
}
|
||||
|
||||
/// <summary>The other version of <paramref name="key"/>: the one Versions photo at its capture millisecond; null when it is no longer one.</summary>
|
||||
private static string? PartnerOf(CopyComparisonResult compared, string key)
|
||||
{
|
||||
var versions = compared.Photos.Where(p => p.Class == PhotoClass.Versions && p.Here?.CapturedAt is not null).ToList();
|
||||
if (versions.FirstOrDefault(p => string.Equals(p.Key, key, StringComparison.Ordinal)) is not { } photo) return null;
|
||||
var ms = CopyComparer.Millisecond(photo.Here!.CapturedAt!.Value);
|
||||
var partners = versions.Where(p => !string.Equals(p.Key, key, StringComparison.Ordinal) && CopyComparer.Millisecond(p.Here!.CapturedAt!.Value) == ms).ToList();
|
||||
return partners.Count == 1 ? partners[0].Key : null;
|
||||
}
|
||||
|
||||
/// <summary>Whether one of <paramref name="frames"/> (usable frames of the local sequence) is on disk with the content its index row records.</summary>
|
||||
private async Task<bool> HoldsVerifiedAsync(Guid sequenceId, IReadOnlyList<CopyPhotoInput> frames, CancellationToken ct)
|
||||
{
|
||||
if (frames.Count == 0) return false;
|
||||
var sequence = await Db.Sequences.GetAsync(sequenceId, ct).ConfigureAwait(false);
|
||||
var archive = sequence is null ? null : await Db.Archives.GetAsync(sequence.ArchiveId, ct).ConfigureAwait(false);
|
||||
if (archive is null) return false;
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
foreach (var input in frames)
|
||||
{
|
||||
if (input.Sha256 is not { Length: > 0 } sha || await Db.Frames.GetAsync(input.FrameId, ct).ConfigureAwait(false) is not { } frame) continue;
|
||||
try
|
||||
{
|
||||
if (string.Equals(await ContentHash.Sha256Async(paths.FromRelative(frame.RelativePath), null, ct).ConfigureAwait(false), sha, StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException or UnauthorizedAccessException)
|
||||
{
|
||||
// Not readable: not verified.
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>The frames whose photo no sequence of this index but <paramref name="except"/> holds (an unhashed one always counts).</summary>
|
||||
private async Task<IReadOnlyList<LastCopy>> LastCopiesAsync(IReadOnlyList<CopyPhotoInput> frames, CopySide side, IReadOnlyCollection<Guid> except, CancellationToken ct)
|
||||
{
|
||||
var elsewhere = await _sides.HeldElsewhereAsync([.. frames.Select(f => f.Sha256).OfType<string>()], except, ct).ConfigureAwait(false);
|
||||
return [.. frames.Where(f => f.Sha256 is not { Length: > 0 } sha || !elsewhere.Contains(sha.ToLowerInvariant()))
|
||||
.Select(f => new LastCopy(f.Sha256?.ToLowerInvariant() ?? string.Empty, side, f.FileName, f.Size))];
|
||||
}
|
||||
|
||||
/// <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);
|
||||
|
||||
@@ -105,8 +105,10 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
{
|
||||
var probe = await _store.GetProbeAsync(local.Id, server, ct).ConfigureAwait(false);
|
||||
probes[local.Id] = probe;
|
||||
// The server's update times are compared only with the newest one seen at the last probe, never with this PC's
|
||||
// clock (a server behind it would hide its changes; microseconds, what both databases keep).
|
||||
if (probe is null || probe.FramesVersion != local.FramesVersion || probe.Hashed != hashed.GetValueOrDefault(local.Id)
|
||||
|| (newest is { } changed && changed > probe.ProbedAt))
|
||||
|| (newest is { } changed && (probe.ServerNewest is not { } seen || Micros(changed) > Micros(seen))))
|
||||
due.Add(local);
|
||||
}
|
||||
due = [.. due.OrderBy(l => probes[l.Id]?.ProbedAt ?? DateTimeOffset.MinValue).ThenBy(l => l.Id)];
|
||||
@@ -161,16 +163,26 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
continue;
|
||||
}
|
||||
var existing = pairs.GetValueOrDefault((local.Id, dto.Id));
|
||||
if (existing is { State: CopyState.Linked or CopyState.Integrated or CopyState.Integrating }) continue;
|
||||
if (existing is { State: CopyState.Integrating }) continue;
|
||||
var revived = false;
|
||||
if (existing is { State: CopyState.Linked or CopyState.Integrated } stale)
|
||||
{
|
||||
// No link joins the two any more (neither side is linked here): the pair is asked about again.
|
||||
if (!await _store.SetStateAsync(stale.Id, stale.State, CopyState.Candidate, null, ct).ConfigureAwait(false)) continue;
|
||||
existing = stale with { State = CopyState.Candidate, AutoPhotos = false };
|
||||
pairs[(local.Id, dto.Id)] = existing;
|
||||
revived = true;
|
||||
}
|
||||
var fresh = existing is null || revived;
|
||||
var probe = probes.GetValueOrDefault(local.Id);
|
||||
if (existing is not null && existing.LocalFramesVersion == local.FramesVersion && existing.OtherFramesVersion == dto.FramesVersion
|
||||
if (!revived && existing is not null && existing.LocalFramesVersion == local.FramesVersion && existing.OtherFramesVersion == dto.FramesVersion
|
||||
&& probe is not null && probe.Hashed == hashed.GetValueOrDefault(local.Id))
|
||||
continue;
|
||||
// Compared already while neither side moved (a pair or not): a local sequence with more candidates than one refresh
|
||||
// compares gets through them over the next refreshes.
|
||||
var compared = string.Create(CultureInfo.InvariantCulture,
|
||||
$"{server}|{local.Id:N}:{local.FramesVersion}:{hashed.GetValueOrDefault(local.Id)}|{dto.Id:N}:{dto.FramesVersion}");
|
||||
if (_compared.ContainsKey(compared)) continue;
|
||||
if (!revived && _compared.ContainsKey(compared)) continue;
|
||||
if (exact >= limit)
|
||||
{
|
||||
deferred.Add(local.Id);
|
||||
@@ -179,7 +191,7 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
exact++;
|
||||
try
|
||||
{
|
||||
var row = await CompareServerAsync(remote, local, dto, item.Evidence, existing, linking, linkedLocal, linkedRemote, link, found, ct)
|
||||
var row = await CompareServerAsync(remote, local, dto, item.Evidence, existing, fresh, linking, linkedLocal, linkedRemote, link, found, ct)
|
||||
.ConfigureAwait(false);
|
||||
if (row is null) pairs.Remove((local.Id, dto.Id));
|
||||
else pairs[(local.Id, dto.Id)] = row;
|
||||
@@ -195,19 +207,21 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
// 6. Probes: a local sequence whose candidates were all compared is not probed again until it (or the server) moves.
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
foreach (var local in due.Where(l => answered.Contains(l.Id) && !deferred.Contains(l.Id)))
|
||||
await _store.SetProbeAsync(new CopyProbe(local.Id, server, local.FramesVersion, hashed.GetValueOrDefault(local.Id), now), ct).ConfigureAwait(false);
|
||||
await _store.SetProbeAsync(new CopyProbe(local.Id, server, local.FramesVersion, hashed.GetValueOrDefault(local.Id), now, newest), ct).ConfigureAwait(false);
|
||||
return found.Result();
|
||||
|
||||
static long Micros(DateTimeOffset at) => at.UtcTicks / 10;
|
||||
}
|
||||
|
||||
/// <summary>The lookup requests of the due local sequences (at most 200 probes and 20,000 items each) and the hits worth comparing.</summary>
|
||||
private async Task LookupAsync(ISyncRemote remote, IReadOnlyList<Sequence> due, IReadOnlyDictionary<Guid, SequenceDto> listed,
|
||||
Dictionary<(Guid, Guid), FollowUp> follow, HashSet<Guid> answered, CancellationToken ct)
|
||||
{
|
||||
var batch = new List<(Sequence Local, PhotoProbe Probe)>();
|
||||
var batch = new List<(Sequence Local, PhotoProbe Probe, int Weight)>();
|
||||
var items = 0;
|
||||
foreach (var local in due)
|
||||
{
|
||||
var probe = await ProbeOfAsync(local, ct).ConfigureAwait(false);
|
||||
var (probe, weight) = await ProbeOfAsync(local, ct).ConfigureAwait(false);
|
||||
var size = Items(probe);
|
||||
if (batch.Count > 0 && (batch.Count >= PhotoLookupRequest.MaxProbes || items + size > PhotoLookupRequest.MaxItems))
|
||||
{
|
||||
@@ -215,12 +229,12 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
batch.Clear();
|
||||
items = 0;
|
||||
}
|
||||
batch.Add((local, probe));
|
||||
batch.Add((local, probe, weight));
|
||||
items += size;
|
||||
}
|
||||
if (batch.Count > 0) await AskAsync(batch).ConfigureAwait(false);
|
||||
|
||||
async Task<bool> AskAsync(List<(Sequence Local, PhotoProbe Probe)> probes)
|
||||
async Task<bool> AskAsync(List<(Sequence Local, PhotoProbe Probe, int Weight)> probes)
|
||||
{
|
||||
PhotoLookupResponse response;
|
||||
try
|
||||
@@ -234,11 +248,11 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
}
|
||||
var results = new Dictionary<string, ProbeResult>(StringComparer.Ordinal);
|
||||
foreach (var result in response.Results) results.TryAdd(result.Key, result);
|
||||
foreach (var (local, probe) in probes)
|
||||
foreach (var (local, probe, weight) in probes)
|
||||
{
|
||||
answered.Add(local.Id);
|
||||
if (!results.TryGetValue(probe.Key, out var result)) continue;
|
||||
var threshold = Math.Max(1, Items(probe) / 10);
|
||||
var threshold = Math.Max(1, weight / 10);
|
||||
foreach (var hit in result.Hits)
|
||||
{
|
||||
if (!listed.TryGetValue(hit.SequenceId, out var dto)) continue;
|
||||
@@ -255,10 +269,11 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
|
||||
/// <summary>
|
||||
/// A local sequence's sample (design §3.3.3): up to 64 SHA-256 evenly spaced over its photos, each with the other encodings
|
||||
/// its conversions link it to (the server may hold either), up to 64 unhashed photos by size and capture time, and its
|
||||
/// back-reference key.
|
||||
/// its conversions link it to (the server may hold either), up to 64 unhashed photos by size and capture time, the hashed
|
||||
/// sample by size and capture time too (for a server that has not hashed its copy), and its back-reference key; with the
|
||||
/// weight the lookup's threshold counts (each sampled photo once).
|
||||
/// </summary>
|
||||
private async Task<PhotoProbe> ProbeOfAsync(Sequence local, CancellationToken ct)
|
||||
private async Task<(PhotoProbe Probe, int Weight)> ProbeOfAsync(Sequence local, CancellationToken ct)
|
||||
{
|
||||
var side = await _sides.LocalAsync(local.Id, ct).ConfigureAwait(false);
|
||||
var tombstones = SyncTombstones.FromDtos(side.Removed, null);
|
||||
@@ -290,7 +305,18 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
var unhashed = usable.Where(p => string.IsNullOrEmpty(p.Sha256) && p.CapturedAt is not null)
|
||||
.Select(p => new FramePhoto(null, p.Size, p.CapturedAt)).ToList();
|
||||
var keys = CopyRules.Probe(unhashed).Select(p => new PhotoKeyDto(p.Size, p.CapturedAt!.Value)).ToList();
|
||||
return new PhotoProbe(local.Id.ToString("N"), hashes, keys.Count == 0 ? null : keys, SessionKeyPrefix + local.Id.ToString("N"));
|
||||
// The hashed sample by size and capture time too: a server that has not hashed its copy yet knows it only so.
|
||||
var known = keys.ToHashSet();
|
||||
var mirrored = 0;
|
||||
foreach (var photo in CopyRules.Probe([.. hashedPhotos.Where(p => p.CapturedAt is not null)]))
|
||||
{
|
||||
if (!known.Add(new PhotoKeyDto(photo.Size, photo.CapturedAt!.Value))) continue;
|
||||
keys.Add(new PhotoKeyDto(photo.Size, photo.CapturedAt.Value));
|
||||
mirrored++;
|
||||
}
|
||||
var probe = new PhotoProbe(local.Id.ToString("N"), hashes, keys.Count == 0 ? null : keys, SessionKeyPrefix + local.Id.ToString("N"));
|
||||
// The threshold counts each sampled photo once (a mirrored key and its hash are one photo).
|
||||
return (probe, probe.Sha256.Count + keys.Count - mirrored + 1);
|
||||
}
|
||||
|
||||
/// <summary>An older server's candidate (design §3.3.4): 1.1's gate (the same first-capture second and camera), or hints of at least 40.</summary>
|
||||
@@ -309,7 +335,7 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
/// Returns the pair's row, or null when the two are not a pair.
|
||||
/// </summary>
|
||||
private async Task<FoundCopy?> CompareServerAsync(ISyncRemote remote, Sequence local, SequenceDto dto, CopyEvidence evidence, FoundCopy? existing,
|
||||
CopyLinking linking, Dictionary<Guid, Guid> linkedLocal, Dictionary<Guid, Guid> linkedRemote, Func<FoundCopy, SequenceDto, Sequence, Task<bool>> link,
|
||||
bool fresh, CopyLinking linking, Dictionary<Guid, Guid> linkedLocal, Dictionary<Guid, Guid> linkedRemote, Func<FoundCopy, SequenceDto, Sequence, Task<bool>> link,
|
||||
Found found, CancellationToken ct)
|
||||
{
|
||||
var server = remote.Server;
|
||||
@@ -359,7 +385,7 @@ public sealed class CopyFinder(ChronosDatabase db, FoundCopyStore store, CopySid
|
||||
found.AutoLinked.Add(row);
|
||||
Log.Info(LogCategory, $"{local.DisplayTitle} holds the photos of {dto.DisplayTitle} on {server}: linked");
|
||||
}
|
||||
else if (existing is null)
|
||||
else if (fresh)
|
||||
{
|
||||
row = await _store.GetAsync(row.Id, ct).ConfigureAwait(false) ?? row;
|
||||
if (row.State == CopyState.Candidate) found.New.Add(row);
|
||||
|
||||
@@ -175,8 +175,11 @@ public sealed record FoundCopy
|
||||
public bool Names(Guid sequenceId) => LocalId == sequenceId || OtherId == sequenceId;
|
||||
}
|
||||
|
||||
/// <summary>What detection last sent or compared of a local sequence, per scope (a server key, or "" for this PC's archives).</summary>
|
||||
public sealed record CopyProbe(Guid SequenceId, string Scope, int FramesVersion, int Hashed, DateTimeOffset ProbedAt);
|
||||
/// <summary>
|
||||
/// What detection last sent or compared of a local sequence, per scope (a server key, or "" for this PC's archives), and the
|
||||
/// newest update time of the server sequences it was probed against, by the server's own clock (compared only with itself).
|
||||
/// </summary>
|
||||
public sealed record CopyProbe(Guid SequenceId, string Scope, int FramesVersion, int Hashed, DateTimeOffset ProbedAt, DateTimeOffset? ServerNewest = null);
|
||||
|
||||
/// <summary>What one detection run found: new pairs, pairs it linked by itself, and kept-apart pairs that ask again.</summary>
|
||||
public sealed record CopyFindResult(IReadOnlyList<FoundCopy> New, IReadOnlyList<FoundCopy> AutoLinked, IReadOnlyList<FoundCopy> AskedAgain)
|
||||
|
||||
@@ -111,6 +111,24 @@ public sealed record CopyPlanContext(string PcName, string? ServerName, bool Can
|
||||
public sealed record TrashTherePlan(Guid CopyId, string Server, Guid RemoteId, IReadOnlyList<Guid> FrameIds, IReadOnlyList<string> Keys, int Photos,
|
||||
long Bytes, int FramesVersion, IReadOnlyList<LastCopy> LastCopies);
|
||||
|
||||
/// <summary>The dry run of moving photos of a pair's local copy to its archive trash, with the ones no other sequence of this index holds.</summary>
|
||||
public sealed record TrashHerePlan(FrameDeletePlan Plan, IReadOnlyList<LastCopy> LastCopies);
|
||||
|
||||
/// <summary>
|
||||
/// The dry run of dropping one version of a photo kept in two versions (design §4.1, Versions): the version
|
||||
/// <see cref="Key"/> goes to the trash of both sides (this PC's copy and the server's, or both local copies of a merge),
|
||||
/// the other version <see cref="PartnerKey"/> stays on both. Applied only while that other version is still held,
|
||||
/// verified by content, on both sides.
|
||||
/// </summary>
|
||||
/// <param name="Other">The other local copy's dry run (a pair of two local copies); null for a server pair.</param>
|
||||
/// <param name="RemoteFrameIds">The server's frames of the version (a server pair), at <see cref="RemoteFramesVersion"/>.</param>
|
||||
public sealed record DropVersionPlan(Guid CopyId, string Key, string PartnerKey, FrameDeletePlan Here, FrameDeletePlan? Other, Guid? RemoteId,
|
||||
IReadOnlyList<Guid> RemoteFrameIds, int RemotePhotos, long RemoteBytes, int RemoteFramesVersion, IReadOnlyList<LastCopy> LastCopies)
|
||||
{
|
||||
/// <summary>Every frame the drop moves, on both sides.</summary>
|
||||
public int Photos => Here.Frames.Count + (Other?.Frames.Count ?? 0) + RemotePhotos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The journal of one project copied to another home: the intended name, when it was asked, the copy once made, the version
|
||||
/// the copy was made from, and the versions pushed so far (source version id to the copy's).
|
||||
|
||||
@@ -32,7 +32,14 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
|
||||
private static readonly string Select = $"SELECT {Columns}, {Effective} AS effective_state FROM found_copies";
|
||||
|
||||
private static readonly string ProbeUpsert = SqlDialect.Upsert("copy_probes", ["sequence_id", "scope", "frames_version", "hashed", "probed_at"], 2);
|
||||
private static readonly string ProbeUpsert =
|
||||
SqlDialect.Upsert("copy_probes", ["sequence_id", "scope", "frames_version", "hashed", "probed_at", "server_newest"], 2);
|
||||
|
||||
/// <summary>
|
||||
/// How long after a pair's first 404 another 404 counts as the second (design §6.3): two answers of one moment (a
|
||||
/// transfer's and the refresh after it) are one deletion seen twice.
|
||||
/// </summary>
|
||||
public static readonly TimeSpan GoneRecheck = TimeSpan.FromMinutes(10);
|
||||
|
||||
/// <summary>The refresh that last counted a 404 for each remote, per database (a refresh id is new after every start).</summary>
|
||||
private static readonly ConditionalWeakTable<IDatabase, ConcurrentDictionary<(string Server, Guid RemoteId), Guid>> GoneRefreshes = new();
|
||||
@@ -185,8 +192,8 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
|
||||
/// <summary>
|
||||
/// A 404 for the remote side (design §6.3): nothing when <paramref name="localId"/> is null (an observation-only link);
|
||||
/// else the pair's row (made when the link had none) counts one hit per <paramref name="refreshId"/> and every Candidate
|
||||
/// row naming that remote is deleted.
|
||||
/// else the pair's row (made when the link had none) counts one hit per <paramref name="refreshId"/>, and a hit after the
|
||||
/// first only once <see cref="GoneRecheck"/> has passed since it, and every Candidate row naming that remote is deleted.
|
||||
/// </summary>
|
||||
public async Task MarkRemoteGoneAsync(string server, Guid remoteId, Guid? localId, Guid refreshId, DateTimeOffset at, CancellationToken ct)
|
||||
{
|
||||
@@ -203,7 +210,7 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
row = LinkRow(server, remoteId, local, _time.GetUtcNow());
|
||||
await InsertAsync(inner, row, _time.GetUtcNow(), ct).ConfigureAwait(false);
|
||||
}
|
||||
if (!counted)
|
||||
if (!counted && (row.RemoteGoneHits == 0 || row.RemoteGoneAt is not { } first || at - first >= GoneRecheck))
|
||||
{
|
||||
await inner.ExecuteAsync(
|
||||
"UPDATE found_copies SET remote_gone_hits = remote_gone_hits + 1, remote_gone_at = COALESCE(remote_gone_at, $2), updated_at = $3 WHERE id = $1",
|
||||
@@ -226,6 +233,40 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
[server, remoteId, _time.GetUtcNow()], ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A link forgotten without counting a 404 (a server known only by its address, or one that now serves something else):
|
||||
/// the Linked and Integrated rows it joined and the Candidates naming that remote go (nothing joins the two any more, and
|
||||
/// a server sequence listed again is found anew); a Kept apart answer stays. The local copy is probed again.
|
||||
/// </summary>
|
||||
public Task SettleForgottenAsync(string server, Guid remoteId, Guid localId, CancellationToken ct)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(server);
|
||||
return _db.InTransactionAsync(async session =>
|
||||
{
|
||||
var inner = new DbAccess(session);
|
||||
await inner.ExecuteAsync(
|
||||
$"DELETE FROM found_copies WHERE kind = 0 AND server = $1 AND remote_id = $2 AND ({Effective} = 0 OR (local_id = $3 AND {Effective} IN (2, 4)))",
|
||||
[server, remoteId, localId], ct).ConfigureAwait(false);
|
||||
await inner.ExecuteAsync("DELETE FROM copy_probes WHERE sequence_id = $2 AND scope = $1", [server, localId], ct).ConfigureAwait(false);
|
||||
return 0;
|
||||
}, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The pair of a job that stopped (failed or cancelled) leaves <see cref="CopyState.Integrating"/>, by its stored columns
|
||||
/// (a cancelled job's row already reads as its state before): to <paramref name="target"/>, or the state it had before.
|
||||
/// <paramref name="keepJob"/> keeps the job's id on the row, so a retry of that job (and only of it) can take the pair
|
||||
/// again while nothing else touched it. False when the row is not Integrating with that job any more.
|
||||
/// </summary>
|
||||
public async Task<bool> ReleaseAsync(Guid id, Guid jobId, CopyState? target, bool keepJob, CancellationToken ct)
|
||||
{
|
||||
var changed = await _db.ExecuteAsync(
|
||||
"UPDATE found_copies SET state = COALESCE($3, previous_state, 0), previous_state = NULL, " +
|
||||
$"job_id = {(keepJob ? "$2" : "NULL")}, auto_photos = $4, updated_at = $5 WHERE id = $1 AND state = 3 AND job_id = $2",
|
||||
[id, jobId, (int?)target, false, _time.GetUtcNow()], ct).ConfigureAwait(false);
|
||||
return changed == 1;
|
||||
}
|
||||
|
||||
public Task DeleteAsync(Guid id, CancellationToken ct) =>
|
||||
_db.ExecuteAsync("DELETE FROM found_copies WHERE id = $1", [id], ct);
|
||||
|
||||
@@ -271,7 +312,7 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
{
|
||||
if (links.TryGetValue(row.RemoteId!.Value, out var local) && local == row.LocalId) continue;
|
||||
if (!links.ContainsKey(row.RemoteId.Value) && !links.ContainsValue(row.LocalId)) continue;
|
||||
await inner.ExecuteAsync("UPDATE found_copies SET state = 0, auto_photos = $2, updated_at = $3 WHERE id = $1 AND state IN (2, 4)",
|
||||
await inner.ExecuteAsync("UPDATE found_copies SET state = 0, job_id = NULL, auto_photos = $2, updated_at = $3 WHERE id = $1 AND state IN (2, 4)",
|
||||
[row.Id, false, now], ct).ConfigureAwait(false);
|
||||
}
|
||||
await inner.ExecuteAsync(
|
||||
@@ -359,7 +400,8 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
continue;
|
||||
}
|
||||
await inner.ExecuteAsync(
|
||||
"UPDATE found_copies SET local_id = $2, state = $3, auto_photos = $4, local_frames_version = -1, updated_at = $5 WHERE id = $1",
|
||||
"UPDATE found_copies SET local_id = $2, state = $3, job_id = CASE WHEN $3 = 3 THEN job_id ELSE NULL END, auto_photos = $4, " +
|
||||
"local_frames_version = -1, updated_at = $5 WHERE id = $1",
|
||||
[row.Id, keep, (int)state, false, now], ct).ConfigureAwait(false);
|
||||
pairs++;
|
||||
continue;
|
||||
@@ -377,7 +419,8 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
continue;
|
||||
}
|
||||
await inner.ExecuteAsync(
|
||||
"UPDATE found_copies SET local_id = $2, other_id = $3, state = $4, auto_photos = $5, local_frames_version = -1, " +
|
||||
"UPDATE found_copies SET local_id = $2, other_id = $3, state = $4, job_id = CASE WHEN $4 = 3 THEN job_id ELSE NULL END, auto_photos = $5, " +
|
||||
"local_frames_version = -1, " +
|
||||
"other_frames_version = -1, updated_at = $6 WHERE id = $1",
|
||||
[row.Id, low, high, (int)state, false, now], ct).ConfigureAwait(false);
|
||||
pairs++;
|
||||
@@ -387,13 +430,13 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
}
|
||||
|
||||
public Task<CopyProbe?> GetProbeAsync(Guid sequenceId, string scope, CancellationToken ct) =>
|
||||
_db.SingleAsync("SELECT sequence_id, scope, frames_version, hashed, probed_at FROM copy_probes WHERE sequence_id = $1 AND scope = $2",
|
||||
[sequenceId, scope], r => new CopyProbe(r.GetGuid(0), r.GetString(1), r.GetInt32(2), r.GetInt32(3), r.GetDateTimeOffset(4)), ct);
|
||||
_db.SingleAsync("SELECT sequence_id, scope, frames_version, hashed, probed_at, server_newest FROM copy_probes WHERE sequence_id = $1 AND scope = $2",
|
||||
[sequenceId, scope], r => new CopyProbe(r.GetGuid(0), r.GetString(1), r.GetInt32(2), r.GetInt32(3), r.GetDateTimeOffset(4), r.GetDateTimeOffsetOrNull(5)), ct);
|
||||
|
||||
public Task SetProbeAsync(CopyProbe probe, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(probe);
|
||||
return _db.ExecuteAsync(ProbeUpsert, [probe.SequenceId, probe.Scope, probe.FramesVersion, probe.Hashed, probe.ProbedAt], ct);
|
||||
return _db.ExecuteAsync(ProbeUpsert, [probe.SequenceId, probe.Scope, probe.FramesVersion, probe.Hashed, probe.ProbedAt, probe.ServerNewest], ct);
|
||||
}
|
||||
|
||||
/// <summary>Forgets what was probed in <paramref name="scope"/>: the next detection probes every sequence again (Find matches).</summary>
|
||||
@@ -465,7 +508,7 @@ public sealed class FoundCopyStore(ChronosDatabase db, TimeProvider? time = null
|
||||
/// <summary>Linked and Integrated rows of the server sequence with another local copy, or of the local copy with another server sequence, become Candidates.</summary>
|
||||
private static Task<int> DemoteAsync(DbAccess access, string server, Guid remoteId, Guid localId, DateTimeOffset now, CancellationToken ct) =>
|
||||
access.ExecuteAsync(
|
||||
"UPDATE found_copies SET state = 0, auto_photos = $4, updated_at = $5 WHERE kind = 0 AND server = $1 AND state IN (2, 4) " +
|
||||
"UPDATE found_copies SET state = 0, job_id = NULL, auto_photos = $4, updated_at = $5 WHERE kind = 0 AND server = $1 AND state IN (2, 4) " +
|
||||
"AND ((remote_id = $2 AND local_id <> $3) OR (local_id = $3 AND remote_id <> $2))",
|
||||
[server, remoteId, localId, false, now], ct);
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@ public interface ICopyEngine
|
||||
/// <summary>Every listed pair with the facts of both sides.</summary>
|
||||
Task<IReadOnlyList<FoundCopyView>> ListAsync(CancellationToken ct);
|
||||
|
||||
/// <summary>Every pair as stored now, listed or not (the plain links too): what Upload all local reads when it is clicked.</summary>
|
||||
Task<IReadOnlyList<FoundCopy>> ListAllPairsAsync(CancellationToken ct);
|
||||
|
||||
/// <summary>Looks for matches now: on <paramref name="remote"/>'s server when given, and among this PC's archives always.</summary>
|
||||
Task<CopyFindResult> FindAsync(ISyncRemote? remote, CancellationToken ct);
|
||||
|
||||
@@ -41,8 +44,8 @@ public interface ICopyEngine
|
||||
|
||||
Task UnlinkAsync(Guid copyId, ISyncRemote? remote, CancellationToken ct);
|
||||
|
||||
/// <summary>The dry run of moving photos of the local copy (by photo key) to its archive trash.</summary>
|
||||
Task<FrameDeletePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct);
|
||||
/// <summary>The dry run of moving photos of the local copy (by photo key) to its archive trash, with the last copies among them.</summary>
|
||||
Task<TrashHerePlan> PlanTrashHereAsync(Guid copyId, IReadOnlyCollection<string> photoKeys, CancellationToken ct);
|
||||
|
||||
/// <summary>Applies <paramref name="plan"/>, refusing photos no longer deleted on the other side; returns the photos moved.</summary>
|
||||
Task<int> TrashHereAsync(Guid copyId, FrameDeletePlan plan, ISyncRemote? remote, CancellationToken ct);
|
||||
@@ -53,6 +56,15 @@ public interface ICopyEngine
|
||||
/// <summary>Applies <paramref name="plan"/> on the server at the plan's frames version; returns the photos moved.</summary>
|
||||
Task<int> TrashThereAsync(TrashTherePlan plan, ISyncRemote remote, CancellationToken ct);
|
||||
|
||||
/// <summary>The dry run of dropping one version (<paramref name="key"/>) of a photo kept in two versions, from both sides of the pair.</summary>
|
||||
Task<DropVersionPlan> PlanDropVersionAsync(Guid copyId, string key, ISyncRemote? remote, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Applies <paramref name="plan"/>, refusing it unless the other version is still held, verified by content, on both
|
||||
/// sides; the server's trash step first, at the plan's frames version. Returns the frames moved.
|
||||
/// </summary>
|
||||
Task<int> DropVersionAsync(DropVersionPlan plan, ISyncRemote? remote, CancellationToken ct);
|
||||
|
||||
/// <summary>Keeps this PC's sequence of a pair whose server copy was deleted, and stops asking.</summary>
|
||||
Task KeepHereAsync(Guid copyId, CancellationToken ct);
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
?? throw new InvalidOperationException(Loc.L("Errors.Sync.NotConnected"));
|
||||
var journal = IntegrateJournal.Read(paths, job.Id);
|
||||
if (journal is null) journal = await StartAsync(context, remote, parameters, paths).ConfigureAwait(false);
|
||||
else await ResumeAsync(job, parameters, ct).ConfigureAwait(false);
|
||||
else await ResumeAsync(job, parameters, paths, ct).ConfigureAwait(false);
|
||||
started = true;
|
||||
await new Run(this, context, remote, journal, paths).RunAsync().ConfigureAwait(false);
|
||||
}
|
||||
@@ -65,12 +65,13 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
catch (StaleCopyPlanException) when (!started)
|
||||
{
|
||||
// Nothing was applied: the pair returns to the state it had.
|
||||
await SetBackAsync(job.Id, parameters, linked: false).ConfigureAwait(false);
|
||||
await SetBackAsync(job.Id, parameters, linked: false, keepJob: false).ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await SetBackAsync(job.Id, parameters, linked: true).ConfigureAwait(false);
|
||||
// The job's id stays on the pair: a retry of this job resumes its journal while nothing else touched the pair.
|
||||
await SetBackAsync(job.Id, parameters, linked: true, keepJob: true).ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -106,21 +107,21 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
Log.Warning("sync", $"The journal of the cancelled Integrate {job.Id} was not removed", ex);
|
||||
}
|
||||
}
|
||||
await SetBackAsync(job.Id, parameters, linked: true).ConfigureAwait(false);
|
||||
await SetBackAsync(job.Id, parameters, linked: true, keepJob: false).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The pair of a job that stopped for good goes back: to Linked when <paramref name="linked"/> and the link exists (the
|
||||
/// photos moved so far stay integrated), else to the state it had before; only while it is still this job's.
|
||||
/// The pair of a job that stopped goes back: to Linked when <paramref name="linked"/> and the link exists (the photos
|
||||
/// moved so far stay integrated), else to the state it had before; only while it is still this job's, by the stored row
|
||||
/// (a cancelled job's row already reads as its state before). <paramref name="keepJob"/>: see <see cref="FoundCopyStore.ReleaseAsync"/>.
|
||||
/// </summary>
|
||||
private async Task SetBackAsync(Guid jobId, IntegrateJobParameters parameters, bool linked)
|
||||
private async Task SetBackAsync(Guid jobId, IntegrateJobParameters parameters, bool linked, bool keepJob)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (await copies.GetAsync(parameters.CopyId, CancellationToken.None).ConfigureAwait(false) is not { } copy || copy.JobId != jobId) return;
|
||||
var link = linked ? await sync.Links.GetAsync(parameters.Server, parameters.RemoteId, CancellationToken.None).ConfigureAwait(false) : null;
|
||||
var state = link?.LocalId == parameters.LocalId ? CopyState.Linked : copy.PreviousState ?? CopyState.Candidate;
|
||||
await copies.SetStateAsync(copy.Id, null, state, null, CancellationToken.None).ConfigureAwait(false);
|
||||
await copies.ReleaseAsync(parameters.CopyId, jobId, link?.LocalId == parameters.LocalId ? CopyState.Linked : null, keepJob, CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is DatabaseException or InvalidOperationException)
|
||||
{
|
||||
@@ -128,15 +129,28 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A resumed run (retried after a failure): the pair is this job's again.</summary>
|
||||
private async Task ResumeAsync(Job job, IntegrateJobParameters parameters, CancellationToken ct)
|
||||
/// <summary>
|
||||
/// A resumed run (a pause, a crash, or a retry after a failure): the pair is this job's again only while it still names
|
||||
/// this job (a failure keeps the id; an answer, a link change or another job since clears it). A pair that moved on makes
|
||||
/// the journal stale: it is removed and nothing more is applied.
|
||||
/// </summary>
|
||||
private async Task ResumeAsync(Job job, IntegrateJobParameters parameters, ArchivePaths paths, CancellationToken ct)
|
||||
{
|
||||
var copy = await copies.GetAsync(parameters.CopyId, ct).ConfigureAwait(false) ?? throw new InvalidOperationException(Loc.L("Errors.Copies.StalePlan"));
|
||||
var copy = await copies.GetAsync(parameters.CopyId, ct).ConfigureAwait(false);
|
||||
if (copy is { State: CopyState.Integrating } && copy.JobId == job.Id) return;
|
||||
if (copy.State == CopyState.Integrating) throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
if (!await copies.SetStateAsync(copy.Id, copy.State, CopyState.Integrating, new CopyAnswer(null, null, job.Id, null, DateTimeOffset.UtcNow), ct)
|
||||
if (copy is not null && copy.State != CopyState.Integrating && copy.JobId == job.Id
|
||||
&& await copies.SetStateAsync(copy.Id, copy.State, CopyState.Integrating, new CopyAnswer(null, null, job.Id, null, DateTimeOffset.UtcNow), ct)
|
||||
.ConfigureAwait(false))
|
||||
throw new InvalidOperationException(Loc.L("Errors.Copies.Busy"));
|
||||
return;
|
||||
try
|
||||
{
|
||||
IntegrateJournal.Delete(paths, job.Id);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
Log.Warning("sync", $"The stale journal of Integrate {job.Id} was not removed", ex);
|
||||
}
|
||||
throw new StaleCopyPlanException();
|
||||
}
|
||||
|
||||
/// <summary>The first run: plans again, refuses a plan that moved or is blocked, and journals it.</summary>
|
||||
@@ -144,7 +158,7 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
var copy = await copies.GetAsync(parameters.CopyId, ct).ConfigureAwait(false);
|
||||
if (copy is null || copy.JobId != context.Job.Id) throw new StaleCopyPlanException();
|
||||
if (copy is not { State: CopyState.Integrating } || copy.JobId != context.Job.Id) throw new StaleCopyPlanException();
|
||||
var planContext = (parameters.Context ?? IntegrateJobContext.Permissive).ToPlanContext();
|
||||
var plan = await planner.PlanAsync(remote, copy, parameters.Choices, planContext, context.Job.Id, ct).ConfigureAwait(false);
|
||||
if (!plan.CanApply) throw new InvalidOperationException(await planner.BlockMessageAsync(plan, remote, ct).ConfigureAwait(false));
|
||||
@@ -467,8 +481,10 @@ internal sealed class IntegrateJobHandler(SyncService sync, IntegratePlanner pla
|
||||
|
||||
private async Task<Exception> GoneAsync(ChronosApiException ex)
|
||||
{
|
||||
await owner.Copies.MarkRemoteGoneAsync(journal.Server, journal.RemoteId, journal.LocalId, Guid.NewGuid(), DateTimeOffset.UtcNow, CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
// As a refresh counts it: only on a server keyed by its id, and at most once within FoundCopyStore.GoneRecheck.
|
||||
if (ServerIdentity.IsIdKey(journal.Server))
|
||||
await owner.Copies.MarkRemoteGoneAsync(journal.Server, journal.RemoteId, journal.LocalId, Guid.NewGuid(), DateTimeOffset.UtcNow, CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
return new RemoteGoneException(Loc.L("Errors.Copies.RemoteGone"), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ CREATE TABLE copy_probes (
|
||||
frames_version integer NOT NULL,
|
||||
hashed integer NOT NULL,
|
||||
probed_at timestamptz NOT NULL,
|
||||
server_newest timestamptz,
|
||||
PRIMARY KEY (sequence_id, scope)
|
||||
);
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ CREATE TABLE copy_probes (
|
||||
frames_version INTEGER NOT NULL,
|
||||
hashed INTEGER NOT NULL,
|
||||
probed_at TEXT NOT NULL,
|
||||
server_newest TEXT,
|
||||
PRIMARY KEY (sequence_id, scope)
|
||||
);
|
||||
|
||||
|
||||
@@ -374,13 +374,13 @@ public sealed class SyncAgent : IAsyncDisposable
|
||||
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);
|
||||
var job = await _sync.EnqueueAutomaticUploadAsync(_engine!, Server, local, 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);
|
||||
var job = await _sync.EnqueueAutomaticDownloadAsync(_engine!, Server, link.RemoteId, link.Title, local.ArchiveId, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
_photoJobs[(local.Id, false)] = job.Id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,16 @@ public enum SyncTransferKind
|
||||
/// sequence for a download.
|
||||
/// </summary>
|
||||
/// <param name="ArchiveId">Archive that receives downloaded originals when the sequence has no local copy yet.</param>
|
||||
internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null, SyncUploadMode? Upload = null)
|
||||
/// <param name="Automatic">
|
||||
/// The new photos of a pair integrated with automatic photos (owner review 6, design §7.3): they go only into the two
|
||||
/// sequences of that pair, never into one the job would create or retarget.
|
||||
/// </param>
|
||||
internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null, SyncUploadMode? Upload = null,
|
||||
bool? Automatic = null)
|
||||
{
|
||||
[JsonIgnore]
|
||||
public bool IsAutomatic => Automatic == true;
|
||||
|
||||
public static SyncJobParameters? Read(Job job)
|
||||
{
|
||||
if (job.Kind is not (JobKind.UploadCommit or JobKind.Download) || job.ParametersJson is not { } json) return null;
|
||||
@@ -74,7 +82,7 @@ internal sealed class SyncJobHandler(JobKind kind, SyncTransfers transfers, ISyn
|
||||
switch (parameters.Kind(kind))
|
||||
{
|
||||
case SyncTransferKind.Upload:
|
||||
await transfers.UploadAsync(context, remote, target).ConfigureAwait(false);
|
||||
await transfers.UploadAsync(context, remote, target, parameters.IsAutomatic).ConfigureAwait(false);
|
||||
break;
|
||||
case SyncTransferKind.SendExports:
|
||||
await transfers.SendExportsAsync(context, remote, target).ConfigureAwait(false);
|
||||
@@ -86,7 +94,7 @@ internal sealed class SyncJobHandler(JobKind kind, SyncTransfers transfers, ISyn
|
||||
await transfers.GetExportsAsync(context, remote, target).ConfigureAwait(false);
|
||||
break;
|
||||
default:
|
||||
await transfers.DownloadOriginalsAsync(context, remote, target, parameters.ArchiveId).ConfigureAwait(false);
|
||||
await transfers.DownloadOriginalsAsync(context, remote, target, parameters.ArchiveId, parameters.IsAutomatic).ConfigureAwait(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public sealed class SyncService
|
||||
private const int SweepLinks = 5;
|
||||
|
||||
/// <summary>How long a pair marked gone once waits before its remote is asked for again (design §6.3).</summary>
|
||||
private static readonly TimeSpan GoneRecheck = TimeSpan.FromMinutes(10);
|
||||
private static readonly TimeSpan GoneRecheck = FoundCopyStore.GoneRecheck;
|
||||
|
||||
/// <summary>Recomputes states without sending or taking settings on the user's behalf.</summary>
|
||||
internal static readonly SyncPolicy CompareOnly = new(AutoSyncSettings: false);
|
||||
@@ -390,6 +390,21 @@ public sealed class SyncService
|
||||
public Task<Job> EnqueueUploadAsync(JobEngine engine, string server, Sequence local, JobPriority priority, CancellationToken cancellationToken = default) =>
|
||||
EnqueueAsync(engine, JobKind.UploadCommit, local.Id, local.DisplayTitle, new SyncJobParameters(server), priority, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Queues, in the background, the new photos of a pair integrated with automatic photos to its server sequence (owner
|
||||
/// review 6, design §7.3): the job sends only into the sequence the pair names, and fails rather than create another
|
||||
/// when that one is gone or the pair changed.
|
||||
/// </summary>
|
||||
internal Task<Job> EnqueueAutomaticUploadAsync(JobEngine engine, string server, Sequence local, CancellationToken cancellationToken = default) =>
|
||||
EnqueueAsync(engine, JobKind.UploadCommit, local.Id, local.DisplayTitle, new SyncJobParameters(server, Automatic: true), JobPriority.Background,
|
||||
cancellationToken);
|
||||
|
||||
/// <summary>The other way of <see cref="EnqueueAutomaticUploadAsync"/>: the server's new photos into the pair's local sequence only.</summary>
|
||||
internal Task<Job> EnqueueAutomaticDownloadAsync(JobEngine engine, string server, Guid remoteId, string title, Guid archiveId,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
EnqueueAsync(engine, JobKind.Download, remoteId, title, new SyncJobParameters(server, SyncDownloadMode.Originals, archiveId, Automatic: true),
|
||||
JobPriority.Background, cancellationToken);
|
||||
|
||||
/// <summary>Queues sending the renders of a local copy the server lacks (BRIEF §14.5), unless that is already waiting or running.</summary>
|
||||
public Task<Job> EnqueueSendExportsAsync(JobEngine engine, string server, Sequence local, CancellationToken cancellationToken = default) =>
|
||||
EnqueueAsync(engine, JobKind.UploadCommit, local.Id, local.DisplayTitle, new SyncJobParameters(server, Upload: SyncUploadMode.Exports), JobPriority.User,
|
||||
@@ -879,8 +894,9 @@ public sealed class SyncService
|
||||
|
||||
/// <summary>
|
||||
/// Keeps the deletion counts of a linked pair current at each comparison of its originals (design §6.2): photos one side
|
||||
/// deleted that the other still holds. A pair scored never before (a plain link an upload or download made) gets the
|
||||
/// whole detection score once, so a Keep apart after an Unlink is measured against what the two held when it was answered.
|
||||
/// deleted that the other still holds. A pair scored never before (a plain link an upload or download made), or whose
|
||||
/// photos moved on either side since its score, gets the whole detection score again, so a Keep apart after an Unlink is
|
||||
/// measured against what the two hold when it is answered.
|
||||
/// </summary>
|
||||
private async Task ScoreLinkedAsync(SyncLink link, Sequence local, SequenceManifest manifest, IReadOnlyDictionary<Guid, DateTime?>? captured,
|
||||
IReadOnlyCollection<FramePhoto> here, IReadOnlyCollection<FramePhoto> there, SyncTombstones tombstones, CancellationToken cancellationToken)
|
||||
@@ -888,7 +904,8 @@ public sealed class SyncService
|
||||
if (await Copies.FindRemoteAsync(local.Id, link.Server, link.RemoteId, cancellationToken).ConfigureAwait(false)
|
||||
is not { State: CopyState.Linked or CopyState.Integrated } row) return;
|
||||
CopyScore score;
|
||||
if (row.Score.LocalPhotos == 0 && row.Score.OtherPhotos == 0)
|
||||
if ((row.Score.LocalPhotos == 0 && row.Score.OtherPhotos == 0)
|
||||
|| row.LocalFramesVersion != local.FramesVersion || row.OtherFramesVersion != manifest.FramesVersion)
|
||||
{
|
||||
var side = await new CopySides(_db).LocalAsync(local.Id, cancellationToken).ConfigureAwait(false);
|
||||
score = CopyFinder.Detection(CopyComparer.Compare(side, CopyFinder.ServerSide(manifest, captured)).Score) with { Hints = row.Score.Hints };
|
||||
@@ -1178,12 +1195,16 @@ public sealed class SyncService
|
||||
|
||||
/// <summary>
|
||||
/// Forgets a link the server answered 404 for. With <paramref name="refreshId"/>, on a server keyed by its id (an address
|
||||
/// key may name another server tomorrow), the pair counts the 404 first (<see cref="FoundCopyStore.MarkRemoteGoneAsync"/>).
|
||||
/// key may name another server tomorrow), the pair counts the 404 first (<see cref="FoundCopyStore.MarkRemoteGoneAsync"/>);
|
||||
/// otherwise the pairs the link joined are settled (<see cref="FoundCopyStore.SettleForgottenAsync"/>), never left Linked
|
||||
/// with no link behind them.
|
||||
/// </summary>
|
||||
private async Task ForgetAsync(SyncLink link, Guid? refreshId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (refreshId is { } id && ServerIdentity.IsIdKey(link.Server))
|
||||
await Copies.MarkRemoteGoneAsync(link.Server, link.RemoteId, link.LocalId, id, DateTimeOffset.UtcNow, cancellationToken).ConfigureAwait(false);
|
||||
else if (link.LocalId is { } localId)
|
||||
await Copies.SettleForgottenAsync(link.Server, link.RemoteId, localId, cancellationToken).ConfigureAwait(false);
|
||||
await _links.DeleteAsync(link.Server, link.RemoteId, cancellationToken).ConfigureAwait(false);
|
||||
Proxies.Delete(link.Server, link.RemoteId);
|
||||
Log.Info(LogCategory, $"{link.Title} is no longer on {link.Server}");
|
||||
@@ -1277,7 +1298,9 @@ public sealed class SyncService
|
||||
{
|
||||
var running = TransferKind(job);
|
||||
var outranked = running == SyncTransferKind.DownloadProxies && job.Priority < priority;
|
||||
if (running == wanted && !outranked) return job;
|
||||
// An automatic transfer never creates a sequence: it does not stand for the user's own.
|
||||
var narrower = SyncJobParameters.Read(job)?.IsAutomatic == true && !parameters.IsAutomatic;
|
||||
if (running == wanted && !outranked && !narrower) return job;
|
||||
if (outranked && wanted is SyncTransferKind.DownloadProxies or SyncTransferKind.DownloadOriginals)
|
||||
await engine.CancelAsync(job.Id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -40,14 +40,21 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
/// <summary>Space a cached proxy is assumed to take before it is downloaded (1920 px JPEG, BRIEF §8.5).</summary>
|
||||
private const long ProxyEstimate = 3L << 19;
|
||||
|
||||
public async Task UploadAsync(JobContext context, ISyncRemote remote, Guid localId)
|
||||
/// <param name="automatic">
|
||||
/// The new photos of a pair integrated with automatic photos: only into the server sequence that pair names, which must
|
||||
/// still exist; never a new one (<see cref="AutomaticPairAsync"/>).
|
||||
/// </param>
|
||||
public async Task UploadAsync(JobContext context, ISyncRemote remote, Guid localId, bool automatic = false)
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
var db = sync.Database;
|
||||
var local = await db.Sequences.GetAsync(localId, ct).ConfigureAwait(false) ?? throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
|
||||
var archive = await db.Archives.GetAsync(local.ArchiveId, ct).ConfigureAwait(false) ?? throw new InvalidOperationException(Loc.L("Errors.Sync.NoArchive"));
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
var (remoteId, created) = await TargetAsync(context, remote, local).ConfigureAwait(false);
|
||||
var (remoteId, created) = automatic
|
||||
? (await AutomaticPairAsync(remote, await sync.Links.FindByLocalAsync(remote.Server, local.Id, ct).ConfigureAwait(false), local.Id, ct)
|
||||
.ConfigureAwait(false), false)
|
||||
: await TargetAsync(context, remote, local).ConfigureAwait(false);
|
||||
|
||||
var frames = (await SequenceFrames.ListAsync(db, local.Id, ct).ConfigureAwait(false)).Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList();
|
||||
var manifest = remoteId is { } known ? await remote.GetManifestAsync(known, ct).ConfigureAwait(false) : null;
|
||||
@@ -70,12 +77,15 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
var hashes = new ConcurrentDictionary<Guid, string>();
|
||||
|
||||
// Without a server sequence the originals go out one by one under this sequence's session key until the
|
||||
// server has filed one, which creates the sequence (an original it already holds elsewhere creates nothing).
|
||||
// server has filed one, which creates the sequence (an original it already holds elsewhere creates nothing, and
|
||||
// is filed into the new sequence once there is one).
|
||||
var heldElsewhere = new List<Frame>();
|
||||
while (remoteId is null && pending.TryDequeue(out var first))
|
||||
{
|
||||
var sent = await UploadFrameAsync(context, remote, null, SyncService.SessionKey(local.Id), paths.FromRelative(first.RelativePath), first, tombstones,
|
||||
hashes, ct).ConfigureAwait(false);
|
||||
if (sent is null) skipped++;
|
||||
else if (sent.SequenceId == Guid.Empty && sent.FrameId != Guid.Empty) heldElsewhere.Add(first);
|
||||
else if (sent.SequenceId != Guid.Empty)
|
||||
{
|
||||
remoteId = sent.SequenceId;
|
||||
@@ -89,6 +99,16 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
// A server that files a photo it holds in another sequence into this one (owner review 6): the map's "missing on the
|
||||
// server" reaches 0 once every original is in the target, not merely somewhere on the server.
|
||||
var into = (await remote.CapabilitiesAsync(ct).ConfigureAwait(false)).Contains(ServerCapabilities.UploadInto);
|
||||
if (into)
|
||||
{
|
||||
// Counted already (they reached the server before the target existed): filed into it now, nothing stepped again.
|
||||
foreach (var frame in heldElsewhere)
|
||||
{
|
||||
if (await UploadFrameAsync(context, remote, target, null, paths.FromRelative(frame.RelativePath), frame, tombstones, hashes, ct, into)
|
||||
.ConfigureAwait(false) is null)
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
await Parallel.ForEachAsync(pending, Workers(sync.Options.Uploads, ct), async (frame, token) =>
|
||||
{
|
||||
var sent = await UploadFrameAsync(context, remote, target, null, paths.FromRelative(frame.RelativePath), frame, tombstones, hashes, token, into)
|
||||
@@ -315,10 +335,34 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
/// <summary>The server sequence of an Integrate answered 404: the pair counts it (design §6.3) and the job fails, nothing more sent.</summary>
|
||||
private async Task<Exception> GoneAsync(ISyncRemote remote, Guid remoteId, Guid localId, ChronosApiException ex)
|
||||
{
|
||||
await sync.Copies.MarkRemoteGoneAsync(remote.Server, remoteId, localId, Guid.NewGuid(), DateTimeOffset.UtcNow, CancellationToken.None).ConfigureAwait(false);
|
||||
// As a refresh counts it: only on a server keyed by its id, and at most once within FoundCopyStore.GoneRecheck.
|
||||
if (ServerIdentity.IsIdKey(remote.Server))
|
||||
await sync.Copies.MarkRemoteGoneAsync(remote.Server, remoteId, localId, Guid.NewGuid(), DateTimeOffset.UtcNow, CancellationToken.None).ConfigureAwait(false);
|
||||
return new RemoteGoneException(Loc.L("Errors.Copies.RemoteGone"), ex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The server sequence an automatic transfer (owner review 6, design §7.3) may fill or read: the one <paramref name="link"/>
|
||||
/// joins to <paramref name="localId"/>, while their pair is integrated with automatic photos, not marked gone, and the
|
||||
/// server still has it (a 404 marks the pair and fails). Anything else fails: such a job never creates or retargets.
|
||||
/// </summary>
|
||||
private async Task<Guid> AutomaticPairAsync(ISyncRemote remote, SyncLink? link, Guid localId, CancellationToken ct)
|
||||
{
|
||||
if (link is not { RemoteId: var remoteId } || link.LocalId != localId) throw new RemoteGoneException(Loc.L("Errors.Copies.RemoteGone"));
|
||||
var pair = await sync.Copies.FindRemoteAsync(localId, remote.Server, remoteId, ct).ConfigureAwait(false);
|
||||
if (pair is { RemoteGoneHits: > 0 }) throw new RemoteGoneException(Loc.L("Errors.Copies.RemoteGone"));
|
||||
if (pair is not { State: CopyState.Integrated, AutoPhotos: true }) throw new StaleCopyPlanException();
|
||||
try
|
||||
{
|
||||
await remote.GetSequenceAsync(remoteId, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (ChronosApiException ex) when (ex.Status == HttpStatusCode.NotFound)
|
||||
{
|
||||
throw await GoneAsync(remote, remoteId, localId, ex).ConfigureAwait(false);
|
||||
}
|
||||
return remoteId;
|
||||
}
|
||||
|
||||
/// <summary>A photo's key as <see cref="CopyComparer"/> makes it: its canonical SHA-256, else its size and capture time.</summary>
|
||||
internal static string KeyOf(CopyPhotoInput photo, SyncTombstones tombstones) => KeyOf(photo.Sha256, photo.Size, photo.CapturedAt, photo.FrameId, tombstones);
|
||||
|
||||
@@ -433,12 +477,21 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DownloadOriginalsAsync(JobContext context, ISyncRemote remote, Guid remoteId, Guid? targetArchive)
|
||||
/// <param name="automatic">
|
||||
/// The new photos of a pair integrated with automatic photos: only into the local sequence that pair names, which must
|
||||
/// still exist; never a new one (<see cref="AutomaticPairAsync"/>).
|
||||
/// </param>
|
||||
public async Task DownloadOriginalsAsync(JobContext context, ISyncRemote remote, Guid remoteId, Guid? targetArchive, bool automatic = false)
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
var db = sync.Database;
|
||||
var link = await sync.Links.GetAsync(remote.Server, remoteId, ct).ConfigureAwait(false);
|
||||
var local = link?.LocalId is { } id ? await db.Sequences.GetAsync(id, ct).ConfigureAwait(false) : null;
|
||||
if (automatic)
|
||||
{
|
||||
if (local is null) throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
|
||||
await AutomaticPairAsync(remote, link, local.Id, ct).ConfigureAwait(false);
|
||||
}
|
||||
var archive = (local?.ArchiveId ?? targetArchive) is { } archiveId ? await db.Archives.GetAsync(archiveId, ct).ConfigureAwait(false) : null;
|
||||
if (archive is null) throw new InvalidOperationException(Loc.L("Errors.Sync.NoArchive"));
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
|
||||
Reference in new issue
Block a user