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:
1 parent
461dfcf4a3
commit
96b5b4078b
11 files changed
+1377
-8
No files matched your search
@@ -0,0 +1,228 @@
|
||||
using System.Security.Cryptography;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Archiving;
|
||||
using Chronos.Data.Copies;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Data.Tests.Sync;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Copies;
|
||||
|
||||
/// <summary>
|
||||
/// The matches engine of a PC (owner review 6, #73; design §5, §6): what Find matches finds and tells, the facts listed,
|
||||
/// Link, Unlink, Keep apart and Ask again, and the trash steps of photos one side deleted, re-checked when applied.
|
||||
/// </summary>
|
||||
[Category("sync")]
|
||||
public sealed class CopyEngineTests(TestContext context)
|
||||
{
|
||||
private static readonly CopyPlanContext Context =
|
||||
new("STUDIO-PC", "NAS", true, true, true, true, true, false, null, new HashSet<Guid>(), new HashSet<Guid>());
|
||||
|
||||
private static async Task<SyncFixture> FixtureAsync(TestContext context)
|
||||
{
|
||||
var f = await SyncFixture.CreateAsync(context);
|
||||
f.Remote.Capabilities.Add(ServerCapabilities.UploadInto);
|
||||
f.Remote.Capabilities.Add(ServerCapabilities.PhotoLookup);
|
||||
return f;
|
||||
}
|
||||
|
||||
private static CopyEngine Engine(SyncFixture f)
|
||||
{
|
||||
var sides = new CopySides(f.Db);
|
||||
return new CopyEngine(f.Sync, f.Archive.Service, new IntegratePlanner(f.Sync, sides, f.Archive.Service.Projects),
|
||||
new SequenceMerger(f.Db, f.Archive.Service, sides), new CopyReviewBuilder(f.Db, f.Archive.Service.Projects, sides), _ => Context,
|
||||
serverNames: key => key == f.Server ? "NAS" : null);
|
||||
}
|
||||
|
||||
private static string Sha(byte[] bytes) => Convert.ToHexStringLower(SHA256.HashData(bytes));
|
||||
|
||||
private static Task<FoundCopy> PairAsync(SyncFixture f, Guid localId, Guid remoteId) =>
|
||||
f.Sync.Copies.UpsertFoundAsync(new FoundCopy
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Kind = CopyKind.LocalServer,
|
||||
LocalId = localId,
|
||||
Server = f.Server,
|
||||
RemoteId = remoteId,
|
||||
Evidence = CopyEvidence.Hashes,
|
||||
Fingerprint = "f",
|
||||
}, f.Ct);
|
||||
|
||||
private static async Task<CopyState> StateAsync(SyncFixture f, Guid copyId) => (await f.Sync.Copies.GetAsync(copyId, f.Ct))!.State;
|
||||
|
||||
[Test]
|
||||
public async Task FindMatchesTellsAndListsTheServerPairWithTheFactsOfBothSides()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(4);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet (selection)", photos.Take(2));
|
||||
using var engine = Engine(f);
|
||||
var told = new List<CopyFindResult>();
|
||||
engine.Found += told.Add;
|
||||
|
||||
var result = await engine.FindAsync(f.Remote, f.Ct);
|
||||
|
||||
var pair = result.New.Single();
|
||||
Assert.Equal((local.Id, remote.Id), (pair.LocalId, pair.RemoteId!.Value));
|
||||
Assert.True(told.Any(t => t.New.Any(n => n.Id == pair.Id)));
|
||||
var view = (await engine.ListAsync(f.Ct)).Single();
|
||||
Assert.Equal(("Comet", 4, 4), (view.Here.Title, view.Here.Photos, view.Here.Hashed));
|
||||
Assert.Equal(("Comet (selection)", "NAS", 2), (view.There.Title, view.There.Where, view.There.Photos));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FindMatchesWithoutAServerLooksAmongThisPcsArchives()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var first = await f.LocalAsync("Comet", photos);
|
||||
var second = await f.LocalAsync("Comet copy", photos);
|
||||
using var engine = Engine(f);
|
||||
|
||||
var result = await engine.FindAsync(null, f.Ct);
|
||||
|
||||
var pair = result.New.Single();
|
||||
Assert.Equal(CopyKind.LocalLocal, pair.Kind);
|
||||
Assert.SequenceEqual(new[] { first.Id, second.Id }.Order(), new[] { pair.LocalId, pair.OtherId!.Value }.Order());
|
||||
// Asked again later, the pair already known is not new.
|
||||
Assert.True((await engine.FindLocalAsync(f.Ct)).New.Count == 0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LinkUnlinkKeepApartAndAskAgainMoveThePair()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos.Take(2));
|
||||
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
|
||||
var pair = (await f.Sync.Copies.ListAsync(f.Ct)).Single();
|
||||
using var engine = Engine(f);
|
||||
|
||||
await engine.LinkAsync(pair.Id, f.Remote, f.Ct);
|
||||
Assert.Equal(CopyState.Linked, await StateAsync(f, pair.Id));
|
||||
Assert.Equal(remote.Id, (await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct))!.RemoteId);
|
||||
|
||||
await engine.UnlinkAsync(pair.Id, f.Remote, f.Ct);
|
||||
Assert.Equal(CopyState.KeptApart, await StateAsync(f, pair.Id));
|
||||
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
|
||||
|
||||
await engine.AskAgainAsync(pair.Id, f.Ct);
|
||||
Assert.Equal(CopyState.Candidate, await StateAsync(f, pair.Id));
|
||||
|
||||
await engine.KeepApartAsync(pair.Id, f.Ct);
|
||||
var kept = (await f.Sync.Copies.GetAsync(pair.Id, f.Ct))!;
|
||||
Assert.Equal(CopyState.KeptApart, kept.State);
|
||||
Assert.False(kept.IsListed && kept.State == CopyState.Candidate);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task KeepApartOfALinkedPairUnlinksIt()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(2);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos);
|
||||
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
|
||||
var pair = (await f.Sync.Copies.ListAsync(f.Ct)).Single();
|
||||
using var engine = Engine(f);
|
||||
await engine.LinkAsync(pair.Id, f.Remote, f.Ct);
|
||||
|
||||
await engine.KeepApartAsync(pair.Id, f.Ct);
|
||||
|
||||
Assert.Equal(CopyState.KeptApart, await StateAsync(f, pair.Id));
|
||||
Assert.Null(await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct));
|
||||
Assert.NotNull(await f.Sync.Links.GetAsync(f.Server, remote.Id, f.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LinkIsRefusedWhenTheLocalCopyIsLinkedToAnotherServerSequence()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var linked = f.Remote.Add("Comet", photos);
|
||||
var other = f.Remote.Add("Comet selection", photos.Take(2));
|
||||
await f.RefreshAsync(new SyncPolicy { Linking = CopyLinking.AskAlways });
|
||||
await f.Sync.LinkAsync(f.Server, linked.Id, local.Id, f.Ct);
|
||||
var pair = await PairAsync(f, local.Id, other.Id);
|
||||
using var engine = Engine(f);
|
||||
|
||||
await Assert.ThrowsAsync<CopyLinkTakenException>(() => engine.LinkAsync(pair.Id, f.Remote, f.Ct));
|
||||
|
||||
Assert.Equal(linked.Id, (await f.Sync.LinkOfAsync(f.Server, local.Id, f.Ct))!.RemoteId);
|
||||
Assert.Equal(CopyState.Candidate, await StateAsync(f, pair.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TrashHereMovesWhatTheServerDeletedAndRefusesAPhotoRestoredThere()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos);
|
||||
var pair = await PairAsync(f, local.Id, remote.Id);
|
||||
using var engine = Engine(f);
|
||||
var (gone, restored) = (remote.Frames[1], remote.Frames[2]);
|
||||
await f.Remote.DeleteFramesAsync(remote.Id, new FrameDeleteRequest([gone.Id, restored.Id], null, false), f.Ct);
|
||||
|
||||
var plan = await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1]), Sha(photos[2])], f.Ct);
|
||||
Assert.Equal(2, plan.Frames.Count);
|
||||
// Restored on the server after the dry run: nothing moves, all or nothing.
|
||||
remote.AddFrame(photos[2], restored.FileName);
|
||||
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashHereAsync(pair.Id, plan, f.Remote, f.Ct));
|
||||
Assert.Count(3, await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct));
|
||||
|
||||
var again = await engine.PlanTrashHereAsync(pair.Id, [Sha(photos[1])], f.Ct);
|
||||
Assert.Equal(1, await engine.TrashHereAsync(pair.Id, again, f.Remote, f.Ct));
|
||||
var left = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
Assert.SequenceEqual(new[] { Sha(photos[0]), Sha(photos[2]) }.Order(StringComparer.Ordinal), left.Select(fr => fr.Sha256!).Order(StringComparer.Ordinal));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TrashThereSendsThePlansVersionAndAStalePlanMovesNothing()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos);
|
||||
var pair = await PairAsync(f, local.Id, remote.Id);
|
||||
using var engine = Engine(f);
|
||||
var frames = await f.Db.Frames.ListBySequenceAsync(local.Id, cancellationToken: f.Ct);
|
||||
var deleted = frames.Single(fr => fr.Sha256 == Sha(photos[2]));
|
||||
var sequence = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
|
||||
var local3 = await f.Archive.Service.FrameTrash.PlanDeleteAsync(local.Id, [deleted.Id], sequence.FramesVersion, f.Ct);
|
||||
await f.Archive.Service.FrameTrash.DeleteAsync(local3, "test", null, f.Ct);
|
||||
|
||||
var plan = await engine.PlanTrashThereAsync(pair.Id, f.Remote, [Sha(photos[2])], f.Ct);
|
||||
Assert.Equal((1, remote.FramesVersion), (plan.Photos, plan.FramesVersion));
|
||||
// The photo is in this PC's trash only: the server's is its last copy.
|
||||
Assert.Count(1, plan.LastCopies);
|
||||
|
||||
remote.AddFrame(f.NewPhotos(1)[0], "IMG_0009.JPG");
|
||||
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashThereAsync(plan, f.Remote, f.Ct));
|
||||
Assert.Count(0, f.Remote.Trashed);
|
||||
|
||||
var fresh = await engine.PlanTrashThereAsync(pair.Id, f.Remote, [Sha(photos[2])], f.Ct);
|
||||
Assert.Equal(1, await engine.TrashThereAsync(fresh, f.Remote, f.Ct));
|
||||
Assert.Equal(Sha(photos[2]), f.Remote.Trashed.Single().Frame.Sha256);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TrashThereIsRefusedForAPhotoThisPcStillHolds()
|
||||
{
|
||||
await using var f = await FixtureAsync(context);
|
||||
var photos = f.NewPhotos(2);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos);
|
||||
var pair = await PairAsync(f, local.Id, remote.Id);
|
||||
using var engine = Engine(f);
|
||||
|
||||
var plan = await engine.PlanTrashThereAsync(pair.Id, f.Remote, [Sha(photos[1])], f.Ct);
|
||||
|
||||
await Assert.ThrowsAsync<StaleCopyPlanException>(() => engine.TrashThereAsync(plan, f.Remote, f.Ct));
|
||||
Assert.Count(0, f.Remote.Trashed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Copies;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Sync;
|
||||
|
||||
/// <summary>
|
||||
/// The new photos of a pair integrated with automatic photos (owner review 6, #73; design §7.3): after a comparison the
|
||||
/// agent queues them both ways in the background; never for a pair only linked or integrated without automatic photos; a
|
||||
/// failed transfer counts, waits for its backoff, and three in a row are told.
|
||||
/// </summary>
|
||||
[Category("sync")]
|
||||
public sealed class SyncAgentCopyTests(TestContext context)
|
||||
{
|
||||
private static readonly SyncAgentOptions Quick = new()
|
||||
{
|
||||
RefreshInterval = TimeSpan.FromHours(1),
|
||||
EventDelay = TimeSpan.FromMilliseconds(20),
|
||||
MinBackoff = TimeSpan.FromMilliseconds(20),
|
||||
MaxBackoff = TimeSpan.FromMilliseconds(200),
|
||||
};
|
||||
|
||||
/// <summary>An engine that queues but never runs, so the queued jobs can be read and settled by the test.</summary>
|
||||
private static JobEngine Queue(SyncFixture f) => new(f.Db, [.. f.Sync.CreateJobHandlers(new SyncFixture.Connector(f.Remote))],
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
|
||||
/// <summary>A local sequence and a server sequence sharing two photos, each with one the other lacks, linked, in <paramref name="state"/>.</summary>
|
||||
private static async Task<(Sequence Local, FakeSequence Remote)> PairAsync(SyncFixture f, string title, CopyState state, bool autoPhotos)
|
||||
{
|
||||
var photos = f.NewPhotos(4);
|
||||
var local = await f.LocalAsync(title, photos.Take(3).ToList());
|
||||
var remote = f.Remote.Add(title + " NAS", [photos[0], photos[1], photos[3]]);
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
var row = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
if (state != CopyState.Linked)
|
||||
Assert.True(await f.Sync.Copies.SetStateAsync(row.Id, null, state, new CopyAnswer(null, null, null, autoPhotos, DateTimeOffset.UtcNow), f.Ct));
|
||||
return (local, remote);
|
||||
}
|
||||
|
||||
private static async Task<List<Job>> QueuedAsync(SyncFixture f, JobKind kind) =>
|
||||
[.. (await f.Db.Jobs.ListAsync(JobState.Queued, kind, 50, cancellationToken: f.Ct)).Items];
|
||||
|
||||
[Test]
|
||||
public async Task AnIntegratedPairWithAutomaticPhotosQueuesBothWaysInTheBackground()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var (local, remote) = await PairAsync(f, "Comet", CopyState.Integrated, autoPhotos: true);
|
||||
await using var engine = Queue(f);
|
||||
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
|
||||
var upload = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
|
||||
var download = (await QueuedAsync(f, JobKind.Download)).Single();
|
||||
Assert.Equal((local.Id, JobPriority.Background), (upload.TargetIds.Single(), upload.Priority));
|
||||
Assert.Equal((remote.Id, JobPriority.Background), (download.TargetIds.Single(), download.Priority));
|
||||
Assert.Contains(local.ArchiveId.ToString(), download.ParametersJson ?? string.Empty);
|
||||
Assert.Count(0, agent.PhotoFailures);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APairOnlyLinkedOrWithoutAutomaticPhotosQueuesNothing()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
await PairAsync(f, "Linked", CopyState.Linked, autoPhotos: false);
|
||||
await PairAsync(f, "Manual", CopyState.Integrated, autoPhotos: false);
|
||||
await using var engine = Queue(f);
|
||||
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
|
||||
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
|
||||
Assert.Count(0, await QueuedAsync(f, JobKind.Download));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FailedTransfersCountWaitForTheirBackoffAndThreeInARowAreTold()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos.Take(2));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
var row = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.Sync.Copies.SetStateAsync(row.Id, null, CopyState.Integrated, new CopyAnswer(null, null, null, true, DateTimeOffset.UtcNow), f.Ct);
|
||||
await using var engine = Queue(f);
|
||||
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
|
||||
var told = new List<(Guid, AutoPhotoFailure)>();
|
||||
agent.PhotosNotSent += (id, failure) => told.Add((id, failure));
|
||||
|
||||
for (var attempt = 1; attempt <= SyncAgent.NotSentAfter; attempt++)
|
||||
{
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
var upload = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
|
||||
await f.Db.Jobs.SetStateAsync(upload.Id, JobState.Failed, "Quota reached", f.Ct);
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
Assert.Equal(new AutoPhotoFailure(attempt, "Quota reached"), agent.PhotoFailures[local.Id]);
|
||||
// Within its backoff nothing new is queued.
|
||||
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
|
||||
await Task.Delay(Quick.MaxBackoff + TimeSpan.FromMilliseconds(50), f.Ct);
|
||||
}
|
||||
Assert.Equal((local.Id, SyncAgent.NotSentAfter), (told.Single().Item1, told.Single().Item2.Count));
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
var last = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
|
||||
await f.Db.Jobs.SetStateAsync(last.Id, JobState.Done, null, f.Ct);
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
Assert.Count(0, agent.PhotoFailures);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user