diff --git a/src/Titano.Data/Sync/SyncAgent.cs b/src/Titano.Data/Sync/SyncAgent.cs index 9b3594ec..caa6e992 100644 --- a/src/Titano.Data/Sync/SyncAgent.cs +++ b/src/Titano.Data/Sync/SyncAgent.cs @@ -237,6 +237,9 @@ public sealed class SyncAgent : IAsyncDisposable var job = await _sync.Database.Jobs.GetAsync(jobId, cancellationToken).ConfigureAwait(false); if (job?.State is JobState.Queued or JobState.Running or JobState.Paused) continue; _proxyJobs.Remove(remoteId); + // A download of stand-ins' proxies that did not finish compares the cache with the manifest again (after its + // backoff when it failed): the server's sequence may never change again to ask for it. + if (job?.State != JobState.Done) _standInsCompared.Remove(remoteId); if (job?.State == JobState.Failed) { var failures = _proxyFailures.GetValueOrDefault(remoteId).Failures + 1; @@ -255,6 +258,8 @@ public sealed class SyncAgent : IAsyncDisposable var job = await _sync.EnqueueDownloadAsync(_engine!, Server, link.RemoteId, link.Title, SyncDownloadMode.Proxies, null, JobPriority.Background, cancellationToken).ConfigureAwait(false); _proxyJobs[link.RemoteId] = job.Id; + // Compared at this update time once the download that brings what the server offers is queued. + if (!link.ServerHasProxies) _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; } } @@ -263,7 +268,9 @@ public sealed class SyncAgent : IAsyncDisposable /// of a cache that did not record its kind) or that has no cached file. The sequence's proxy flag cannot say: it waits /// for every frame, a missing or damaged one never gets a proxy, and clients and render nodes send the other frames' /// proxies one by one. The manifest is read once per change of the server's sequence, since a proxy that arrives - /// leaves a new update time (the proxy pass it queues records the sequence's state). + /// leaves a new update time (the proxy pass it queues records the sequence's state). A comparison that offers nothing + /// is recorded here; one that offers proxies is recorded when their download is queued, and dropped when that + /// download does not finish (). /// private async Task OffersProxiesAsync(SyncLink link, CancellationToken cancellationToken) { @@ -280,9 +287,10 @@ public sealed class SyncAgent : IAsyncDisposable Log.Info(LogCategory, $"The proxies of {link.Title} on {Server} could not be compared: {ex.Message}"); return false; } - _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; var cached = _sync.Proxies.Kinds(Server, link.RemoteId); - return manifest.Frames.Any(f => f.HasProxy && cached.GetValueOrDefault(f.Id) != ProxyCache.Proxy); + if (manifest.Frames.Any(f => f.HasProxy && cached.GetValueOrDefault(f.Id) != ProxyCache.Proxy)) return true; + _standInsCompared[link.RemoteId] = link.RemoteUpdatedAt; + return false; } /// Waits for a wake-up or the delay; false when stopping. diff --git a/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs b/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs index 6188f1ae..5c12e86d 100644 --- a/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs +++ b/tests/Titano.Data.Tests/Sync/SyncAgentTests.cs @@ -192,6 +192,41 @@ public sealed class SyncAgentTests(TestContext context) Assert.Count(2, await DownloadsAsync(f)); } + [Test] + public async Task AStandInDownloadThatFailedIsQueuedAgainAfterItsBackoff() + { + await using var f = await SyncFixture.CreateAsync(context); + var remote = f.Remote.Add("Fog", f.NewPhotos(3), status: SequenceStatus.ThumbsBuilt); + remote.Undecodable = true; + var engine = await f.StartEngineAsync(); + await f.RefreshAsync(); + var first = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Fog", SyncDownloadMode.Proxies, null); + await f.Archive.WaitForAsync(first.Id, JobState.Done); + await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(KeepProxiesOffline: true), engine, EventsOnly); + agent.Start(); + await WaitAsync(() => agent.Snapshot is not null, "first comparison"); + + // Two frames' proxies reach the server, but their download fails (the server restarts meanwhile). + foreach (var frame in remote.Frames.Take(2)) f.Remote.SentProxies[frame.Id] = frame.Proxy; + remote.UpdatedAt = DateTimeOffset.UtcNow.AddSeconds(1); + f.Remote.FailAssets = true; + await agent.RefreshAsync(f.Ct); + var failed = Assert.Contains(await DownloadsAsync(f), j => j.Id != first.Id); + await f.Archive.WaitForAsync(failed.Id, JobState.Failed); + + // The server's sequence does not change again: once the backoff has passed, the download is queued anyway. + f.Remote.FailAssets = false; + await WaitAsync(async () => + { + await agent.RefreshAsync(f.Ct); + return (await DownloadsAsync(f)).Count == 3; + }, "the download queued again"); + var retry = Assert.Contains(await DownloadsAsync(f), j => j.Id != first.Id && j.Id != failed.Id); + await f.Archive.WaitForAsync(retry.Id, JobState.Done); + var opened = Assert.NotNull(f.Sync.OpenRemote(f.Server, remote.Id)); + Assert.SequenceEqual([remote.Frames[0].Proxy.Length, remote.Frames[1].Proxy.Length, 64], opened.Files.Select(file => (int)new FileInfo(file!).Length)); + } + private static async Task> DownloadsAsync(SyncFixture f) => (await f.Db.Jobs.ListAsync(null, JobKind.Download, 100, null, f.Ct)).Items;