fix(copies): close the eleventh verification round of owner review 6
- A merge's Verify stage journals a second version, with the hash it is filed as, before it files it. A run that stops between filing it and leaving it out of Main (a drive dropping out, project.json held, the app closing) now leaves it out on resume, instead of reading it as the kept sequence's own photo and keeping both versions in Main for good. - Upload all local ignores a Kept apart pair whose server sequence is confirmed deleted only when the answer came before the first 404. A Keep here, answered after the deletion was shown, skips the sequence again instead of uploading it and erasing the answer. - An upload's checkpoint records whether the job joined a server sequence this PC already knew. A resumed or retried upload no longer takes such a sequence for one it made and overwrites its details. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e002c583d6
commit
6db4c2e80c
10 files changed
+168
-10
No files matched your search
@@ -599,6 +599,40 @@ public abstract class SequenceMergerTests(TestContext context) : DatabaseTest(co
|
||||
AssertVersions(await new CopySides(fx.Db).VersionsAsync(keep.Id, Ct), Sha(mine, 0), Sha(theirs, 0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AVersionTheVerifyFiledBeforeTheRunStoppedIsLeftOutOfMainOnResume()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (mine, theirs) = EqualSizeSeeds(0);
|
||||
var keep = await SequenceAsync(fx, "keep", [(mine, 0, "IMG_0001.JPG")]);
|
||||
var drop = await SequenceAsync(fx, "drop", [(theirs, 0, "IMG_0001.JPG")]);
|
||||
await new DbAccess(fx.Db.Database).ExecuteAsync("UPDATE frames SET sha256 = NULL WHERE sequence_id = $1 OR sequence_id = $2", [keep.Id, drop.Id], Ct);
|
||||
var copy = await PairAsync(fx, keep.Id, drop.Id);
|
||||
var merger = Merger(fx);
|
||||
var (engine, handler) = Engine(fx, merger, new FakeProjects(), new FakeWorkspace());
|
||||
await engine.StartAsync(Ct);
|
||||
// Filed at the verify, then the exclusion fails (a drive drops out, project.json is held) before the journal says so.
|
||||
var failedOnce = false;
|
||||
handler.BeforeLeaveOutOfMain = _ =>
|
||||
{
|
||||
if (failedOnce) return Task.CompletedTask;
|
||||
failedOnce = true;
|
||||
throw new IOException("project.json is in use");
|
||||
};
|
||||
var plan = await merger.PlanAsync(copy.Id, keep.Id, drop.Id, await ChoicesAsync(fx, keep.Id, drop.Id, CopyPhotoPolicy.Union), NoContext, Ct);
|
||||
Assert.Empty(plan.Photos);
|
||||
var job = await merger.EnqueueAsync(engine, new FoundCopyStore(fx.Db), plan, keep.DisplayTitle, Ct);
|
||||
await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
Assert.True((await HashesAsync(fx, keep.Id)).SetEquals([Sha(mine, 0), Sha(theirs, 0)]));
|
||||
|
||||
await engine.ResumeAsync(job.Id, Ct);
|
||||
await fx.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
// The resumed verify finds the version in the kept sequence and leaves it out of Main: one photo at that instant.
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(drop.Id));
|
||||
AssertVersions(await new CopySides(fx.Db).VersionsAsync(keep.Id, Ct), Sha(mine, 0), Sha(theirs, 0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASecondVersionCopiedInByAMergeAbandonedBeforeItsDetailsStaysOutOfMain()
|
||||
{
|
||||
|
||||
@@ -183,6 +183,30 @@ public sealed class CopyEngineTests(TestContext context)
|
||||
Assert.NotNull(await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task KeepHereOfASequenceDeletedOnTheServerIsLeftOutOfUploadAllLocal()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var local = await f.LocalAsync("Comet", f.NewPhotos(2));
|
||||
var gone = Guid.CreateVersion7();
|
||||
var pair = await f.Sync.Copies.EnsureLinkedAsync(f.Server, gone, local.Id, f.Ct);
|
||||
// Deleted on the server: two 404s the re-check apart, read as deleted on server.
|
||||
var at = DateTimeOffset.UtcNow.AddHours(-2);
|
||||
await f.Sync.Copies.MarkRemoteGoneAsync(f.Server, gone, local.Id, Guid.NewGuid(), at, f.Ct);
|
||||
await f.Sync.Copies.MarkRemoteGoneAsync(f.Server, gone, local.Id, Guid.NewGuid(), at.AddHours(1), f.Ct);
|
||||
using var engine = Engine(f);
|
||||
|
||||
await engine.KeepHereAsync(pair.Id, f.Ct);
|
||||
|
||||
// Kept here: Upload all local leaves it out, and the answer stays.
|
||||
var row = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
|
||||
Assert.Equal((CopyState.KeptApart, true), (row.State, row.RemoteGone));
|
||||
var entry = new SyncEntry(local, null, SyncStatus.LocalOnly);
|
||||
var (upload, skipped) = UploadAllRule.Select([entry], await f.Sync.Copies.ListAsync(f.Ct), new HashSet<Guid>(), new HashSet<Guid>());
|
||||
Assert.Empty(upload);
|
||||
Assert.Equal(local.Id, skipped.Single().Entry.Local!.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LinkIsRefusedWhenTheLocalCopyIsLinkedToAnotherServerSequence()
|
||||
{
|
||||
|
||||
@@ -288,10 +288,10 @@ public abstract class FoundCopyStoreTests(TestContext context) : RepositoryTest(
|
||||
Assert.True(await store.SetStateAsync(linked.Id, CopyState.Candidate, CopyState.Linked, Answer(), Ct));
|
||||
var elsewhere = await store.UpsertFoundAsync(Remote(s[3].Id, Guid.CreateVersion7()), Ct);
|
||||
|
||||
// An observation-only link answers 404: no local copy is linked to it.
|
||||
// After the answers, an observation-only link answers 404: no local copy is linked to it.
|
||||
var first = Guid.NewGuid();
|
||||
await store.MarkRemoteGoneAsync(Nas, remote, null, first, TestData.Now, Ct);
|
||||
await store.MarkRemoteGoneAsync(Nas, remote, null, first, TestData.Now.AddMinutes(1), Ct);
|
||||
await store.MarkRemoteGoneAsync(Nas, remote, null, first, TestData.Now.AddMinutes(2), Ct);
|
||||
|
||||
// The unanswered match is gone; the Keep apart answer stays and counts the 404 once, not read as deleted yet.
|
||||
Assert.Null(await store.GetAsync(candidate.Id, Ct));
|
||||
|
||||
@@ -394,6 +394,62 @@ public sealed class SyncTransferCopyTests(TestContext context)
|
||||
Assert.Equal("Comet", (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Title);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARetriedUploadThatJoinedAServerSequenceItKnewStillComparesItsDetails()
|
||||
{
|
||||
await using var f = Capable(await SyncFixture.CreateAsync(context));
|
||||
var photos = f.NewPhotos(4);
|
||||
var local = await f.LocalAsync("Comet", [photos[0], photos[1]]);
|
||||
var engine = await f.StartEngineAsync();
|
||||
var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local);
|
||||
await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
var first = Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct)).RemoteId;
|
||||
// The link and its pair were forgotten; the server sequence is listed again and renamed there.
|
||||
var observed = Assert.NotNull(await f.Sync.Links.GetAsync(f.Server, first, f.Ct)) with { LocalId = null };
|
||||
await f.Sync.Links.UpsertAsync(observed, f.Ct);
|
||||
await using (var session = await f.Db.Database.OpenAsync(f.Ct))
|
||||
await session.ExecuteAsync("DELETE FROM found_copies WHERE remote_id = $1", [first], f.Ct);
|
||||
var server = f.Remote.Sequences[first];
|
||||
server.Title = "Comet NAS";
|
||||
server.SettingsVersion++;
|
||||
server.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
await f.AddLocalAsync(local.Id, photos[2]);
|
||||
await f.AddLocalAsync(local.Id, photos[3]);
|
||||
// One original unreadable for the first attempt: it fails after joining the known sequence.
|
||||
var damaged = (await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct)).Single(fr => fr.Sha256 == Sha(photos[3]));
|
||||
var path = f.Archive.Paths.FromRelative(damaged.RelativePath);
|
||||
var bytes = await File.ReadAllBytesAsync(path, f.Ct);
|
||||
var broken = bytes.ToArray();
|
||||
broken[^3] ^= 0xFF;
|
||||
await File.WriteAllBytesAsync(path, broken, f.Ct);
|
||||
|
||||
var row = new Job
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = JobKind.UploadCommit,
|
||||
TargetIds = [local.Id],
|
||||
State = JobState.Running,
|
||||
NodeId = "t#0",
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
Started = DateTimeOffset.UtcNow,
|
||||
};
|
||||
await f.Db.Jobs.EnqueueAsync(row, f.Ct);
|
||||
var jobs = new JobEngine(f.Db, [], new JobEngineOptions { WorkerId = "t" });
|
||||
var attempt = new JobContext(jobs, row, f.Ct);
|
||||
await Assert.ThrowsAsync<InvalidDataException>(() => new SyncTransfers(f.Sync).UploadAsync(attempt, f.Remote, local.Id));
|
||||
Assert.Equal(first, Assert.NotNull(await f.Sync.Links.FindByLocalAsync(f.Server, local.Id, f.Ct)).RemoteId);
|
||||
|
||||
// Retried from that attempt's checkpoint, the original readable again.
|
||||
await File.WriteAllBytesAsync(path, bytes, f.Ct);
|
||||
var retry = new JobContext(jobs, row with { Checkpoint = attempt.Checkpoint }, f.Ct);
|
||||
await new SyncTransfers(f.Sync).UploadAsync(retry, f.Remote, local.Id);
|
||||
|
||||
// Still joined, not made by this upload: the server's title is not overwritten unseen.
|
||||
Assert.True(server.Frames.Select(fr => fr.Sha256).ToHashSet().SetEquals(photos.Select(Sha)));
|
||||
Assert.Equal("Comet NAS", server.Title);
|
||||
Assert.Equal("Comet", (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Title);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnAutomaticUploadAfterTheServerCopyIsGoneFailsAndCreatesNothing()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user