fix(copies): close the ninth verification round of owner review 6
- A merge leaves each second version out of Main as it files it (Copy stage keep-both and SameUnhashed versions, Verify stage versions), so a merge abandoned before its Details stage (details changed meanwhile, a stale stage, a cancel) no longer leaves both versions in Main for good, which a new plan could not undo (owner Q3). - A merge plan whose kept sequence has tombstones hashes the other's unhashed frames matched by size and time, so content the kept sequence deleted is listed as a last copy; a verify that still finds such content drops the journal and fails as a stale plan instead of failing on every retry. - Download originals uses a key of the job only when the holder of the server sequence's key is kept apart from it or linked elsewhere: a link a 404 forgot (pair Candidate) refills and re-links the local copy instead of duplicating every photo into a new sequence. - A 404 for a server sequence no local copy is linked to deletes its Candidate and Kept apart pairs, so Upload all local sends the local sequence again instead of skipping the only copy as a suspected one (owner Q8). Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aad8ae46dd
commit
c1c02d8ef7
10 files changed
+286
-16
No files matched your search
@@ -372,12 +372,57 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
var frame = await _db.Frames.GetAsync(copy.DropFrameId, _ct).ConfigureAwait(false) ?? throw Changed(_dropTitle);
|
||||
await context.ReportAsync(Done(), Total, new JobDetail(JobStep.CopyPhoto, copy.FileName, n, journal.Copies.Count)).ConfigureAwait(false);
|
||||
var result = await FileAsync(frame, copy.Sha256, planned: copy.Sha256 is { Length: > 0 }).ConfigureAwait(false);
|
||||
// A version kept beside the kept sequence's own (a Union keep-both, or another photo after all) is left out of
|
||||
// Main as it is filed, before the journal says so: a resumed run files it again (a duplicate) and asks again.
|
||||
if (copy is { Action: PhotoAction.FetchVersion, Sha256: { Length: > 0 } version } && journal.Replaces.All(r => r.Key != copy.Key))
|
||||
await LeaveOutOfMainAsync(version).ConfigureAwait(false);
|
||||
journal.Committed[copy.DropFrameId] = result.FrameId;
|
||||
Save();
|
||||
context.Log(Loc.L("Archive.Log.MergeCopied", copy.FileName));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Leaves the second version <paramref name="sha"/> just filed in the kept sequence out of Main at once (owner Q3), not
|
||||
/// only at the Details stage: a merge abandoned before that stage (its details changed meanwhile, a stale stage, a
|
||||
/// cancel) would otherwise leave both versions in Main for good, and a new plan, which reads both as the kept
|
||||
/// sequence's own photos, leaves neither out. Written under the kept sequence's lease at the settings version read
|
||||
/// just before (asked again when an edit lands in between); when that was the version the plan read, the Details
|
||||
/// stage then writes at the version this write made (the plan's payload leaves the version out as well). Settings
|
||||
/// that are not a project of this Chronos are left as they are, as the plan leaves them. Not cancelled halfway: a
|
||||
/// filed version is never left in Main by a cancel.
|
||||
/// </summary>
|
||||
private async Task LeaveOutOfMainAsync(string sha)
|
||||
{
|
||||
var ct = CancellationToken.None;
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
var keep = await _db.Sequences.GetAsync(journal.KeepId, ct).ConfigureAwait(false) ?? throw new StaleCopyPlanException();
|
||||
var basis = CopyPayload.Portable(SyncPayload.Of(keep), await SequencePayloadWriter.PhotosAsync(_db, journal.KeepId, null, ct).ConfigureAwait(false));
|
||||
SyncPayload excluded;
|
||||
try
|
||||
{
|
||||
excluded = CopyPayload.ExcludeVersions(basis, [sha.ToLowerInvariant()]);
|
||||
}
|
||||
catch (Exception ex) when (ex is System.Text.Json.JsonException or InvalidDataException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (excluded.SameAs(basis)) return;
|
||||
try
|
||||
{
|
||||
var updated = await new SequencePayloadWriter(_db, owner.Archives).ApplyAsync(journal.KeepId, excluded, keep.SettingsVersion, ct)
|
||||
.ConfigureAwait(false);
|
||||
if (journal.KeepSettingsVersion == keep.SettingsVersion) journal.KeepSettingsVersion = updated.SettingsVersion;
|
||||
return;
|
||||
}
|
||||
catch (SyncConflictChangedException) when (attempt < 3)
|
||||
{
|
||||
// Edited between the read and the lease: read again.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a frame of the other into staging (hash-checked against <paramref name="expected"/>) and files it in the
|
||||
/// kept sequence. <paramref name="planned"/>: the plan knew the photo's content (else a tombstone of the kept sequence
|
||||
@@ -598,7 +643,7 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
|
||||
var keepRemoved = (await _db.FrameRemovals.ListBySequenceAsync(journal.KeepId, _ct).ConfigureAwait(false))
|
||||
.Select(r => r.Sha256.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
|
||||
var failed = new List<(Guid FrameId, string Sha256)>();
|
||||
var failed = new List<(Guid FrameId, string Sha256, bool Unhashed)>();
|
||||
foreach (var frame in dropFrames)
|
||||
{
|
||||
_ct.ThrowIfCancellationRequested();
|
||||
@@ -625,13 +670,14 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
{
|
||||
// Compared by size and time only, and another photo after all: filed in the kept sequence as a second version.
|
||||
var filed = await FileAsync(frame, sha, planned: false).ConfigureAwait(false);
|
||||
await LeaveOutOfMainAsync(sha).ConfigureAwait(false);
|
||||
journal.SecondVersions.Add(frame.Id);
|
||||
journal.Verified[frame.Id] = new MergeVerified(sha, filed.FrameId);
|
||||
held.TryAdd(sha, filed.FrameId);
|
||||
Save();
|
||||
continue;
|
||||
}
|
||||
failed.Add((frame.Id, sha));
|
||||
failed.Add((frame.Id, sha, frame.Sha256 is not { Length: > 0 }));
|
||||
}
|
||||
if (failed.Count > 0)
|
||||
{
|
||||
@@ -639,7 +685,16 @@ internal sealed class MergeJobHandler(ChronosDatabase db, SequenceMerger merger,
|
||||
// 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.Select(f => f.Sha256)], [journal.DropId, journal.KeepId], _ct)
|
||||
.ConfigureAwait(false);
|
||||
var lost = failed.Where(f => !others.Contains(f.Sha256)).Select(f => f.Sha256).Distinct(StringComparer.Ordinal).Count();
|
||||
var lostFrames = failed.Where(f => !others.Contains(f.Sha256)).ToList();
|
||||
if (lostFrames.Any(f => f.Unhashed && keepRemoved.Contains(f.Sha256)))
|
||||
{
|
||||
// Content the kept sequence deleted, which the plan never compared by content (unhashed, matched by size
|
||||
// and time): a last copy the plan did not list. Retrying this journal would fail the same way: it goes,
|
||||
// and the new plan lists it.
|
||||
ForgetJournal();
|
||||
throw new StaleCopyPlanException();
|
||||
}
|
||||
var lost = lostFrames.Select(f => f.Sha256).Distinct(StringComparer.Ordinal).Count();
|
||||
if (lost > 0) throw new InvalidOperationException(Loc.Plural("Errors.Copies.VerifyFailed", lost));
|
||||
journal.Settled.AddRange(failed.Select(f => f.FrameId));
|
||||
// Held by a third sequence now: asked again before the trash, which that sequence may not wait for.
|
||||
|
||||
Reference in new issue
Block a user