fix: keep render filing retryable and writers out of a folder another index moved

Third fix round of owner review 2, phase 4a (Reorganize), cross-cutting items.

- Server render filing now runs under the sequence lease, so it can meet a busy
  sequence or an offline archive. Both completions (client render uploads and
  render-node results) used to answer 500 and leave the upload stuck in
  Committing until a restart. They now answer 503 sequence_busy or
  archive_offline, which clients retry. The upload goes back to receiving with
  its part file and the node keeps its job (UploadService.ReceiveAgainAsync).
- Save both: when this PC's copy cannot be written (another index moved the
  folder), the server's version is no longer applied, even if this index's scan
  relocates the row before the apply would run. A copy that has to be dropped is
  deleted from where the folder is now, as a best effort.
- Export rebase on a folder move (scan and Reorganize): the sequence's own
  export follows the folder unless its file is still at the old path and not at
  the new one. A file written into a re-created old folder therefore keeps a row
  that finds it, so the file is never stranded.
- A render job refuses to start when its folder is not where the index has it,
  which also stops it from re-creating the old folder. Renders fetched by sync
  are now filed under the sequence lease into the current folder, or not at all
  (new string Errors.Archive.FolderMoved, en and it).
- The proxy pass holds the thumbnail lease one batch at a time (a quarter of
  the lease wait), then lets it go for longer than a waiter's poll and reads
  the folder and sidecar again. Before, another process's Reorganize, sequence
  deletion or pack compaction could wait out its lease wait and fail. Updated
  the stale comments on that lease and ARCHITECTURE.md to match.

Tests cover each fix, and each new test fails with its fix reverted. Also
covered: proxy store and pass in a moved folder, RenderStore with the folder
gone, Take server with the folder moved, and an editor save with the folder
moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 811509d32a118ed50ad10d47d4b002e534da9d77)
This commit is contained in:
Alby96 committed 2026-10-27 19:16:28 +01:00
1 parent 0377040a19
commit c3756d03c9
26 files changed
+666 -134

No files matched your search

@@ -2,9 +2,13 @@ using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Core.Archiving;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Protocol;
using Chronos.Protocol.Client;
using Chronos.Server.Jobs;
using Chronos.Server.Storage;
using Chronos.Testing;
namespace Chronos.Server.Tests.Jobs;
@@ -120,6 +124,85 @@ public sealed class RenderNodeTests(TestContext context)
Assert.Equal(render.Id, (await renders.ReadAsync(ProtocolJson.Default.ListRenderDto)).Single().Id);
}
[Test]
public async Task ARenderThatCannotBeFiledForNowIsCompletedAgain()
{
// The archive engine with a short lease wait: a sequence another writer holds refuses the filing at once.
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = 64 * 1024, s => s.AddSingleton(sp =>
new ArchiveService(sp.GetRequiredService<ChronosDatabase>(), new ArchiveServiceOptions { Owner = ArchiveModule.Owner, LeaseWait = TimeSpan.FromMilliseconds(200) })));
var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true);
var frame = await Uploads.UploadAsync(editor, Uploads.Photo(5000, 54), "IMG_0001.CR3");
var job = await JobApiTests.CreateAsync(editor, "render", frame.SequenceId, HevcJob);
var node = await RegisterAsync(editor, "studio-pc", "hevc", 8192);
using (var claim = await editor.PostAsync($"/api/v1/nodes/{node.Id}/claim", null)) await claim.ExpectAsync(HttpStatusCode.OK);
// A render node's output, and a render a client sends of the same sequence, both fully received.
var output = Uploads.Photo(3000, 55);
var result = new RenderResultRequest("Roof - 4K HEVC.mp4", output.Length, Uploads.Sha(output), "4K HEVC", "hevc", 3840, 2160, 25, 120);
Guid fromNode;
using (var started = await editor.PostAsJsonAsync($"/api/v1/nodes/{node.Id}/jobs/{job.Id}/result", result, ProtocolJson.Default.RenderResultRequest))
{
await started.ExpectAsync(HttpStatusCode.Created);
fromNode = Assert.NotNull((await started.ReadAsync(ProtocolJson.Default.StartUploadResponse)).UploadId);
}
using (var put = await Uploads.PutChunkAsync(editor, fromNode, 0, output)) await put.ExpectAsync(HttpStatusCode.OK);
var sent = Uploads.Photo(2000, 56);
var offered = new RenderUploadRequest(Guid.CreateVersion7(), "Roof.mp4", sent.Length, Uploads.Sha(sent), "1080p", "h264", 1920, 1080, 30, 10, null);
Guid fromClient;
using (var started = await editor.PostAsJsonAsync($"/api/v1/sequences/{frame.SequenceId}/renders", offered, ProtocolJson.Default.RenderUploadRequest))
{
await started.ExpectAsync(HttpStatusCode.Created);
fromClient = Assert.NotNull((await started.ReadAsync(ProtocolJson.Default.StartUploadResponse)).UploadId);
}
using (var put = await Uploads.PutChunkAsync(editor, fromClient, 0, sent)) await put.ExpectAsync(HttpStatusCode.OK);
var completions = new[] { $"/api/v1/nodes/{node.Id}/jobs/{job.Id}/result/complete", $"/api/v1/sequences/{frame.SequenceId}/renders/{fromClient}/complete" };
var paths = new ArchivePaths(server.Options.ArchivePath);
async Task RefusedAsync(string code)
{
foreach (var complete in completions)
{
using var refused = await editor.PostAsync(complete, null);
await refused.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, code);
}
// Nothing filed, nothing staged: both uploads receive again with their chunks, and the node keeps its job.
foreach (var upload in new[] { fromNode, fromClient })
{
Assert.Equal(Chronos.Data.Repositories.UploadState.Receiving, (await server.Database.Uploads.GetAsync(upload))!.State);
Assert.True(server.Services.GetRequiredService<UploadStore>().Exists(upload));
}
Assert.False(Directory.Exists(server.Options.StagingPath) && Directory.EnumerateFiles(server.Options.StagingPath).Any());
Assert.Equal(0, (await server.Database.Sequences.GetAsync(frame.SequenceId))!.RenderedCount);
var waiting = await JobApiTests.GetAsync(editor, job.Id);
Assert.Equal(("running", node.Id.ToString()), (waiting.State, waiting.NodeId));
}
// Another writer holds the sequence past the lease wait (a Reorganize step, a photo deletion).
using (Assert.NotNull(LeaseFile.TryAcquire(paths.LocksDirectory, $"seq-{frame.SequenceId:N}", "tests")))
await RefusedAsync("sequence_busy");
// The archive drive is gone.
File.Move(paths.ManifestPath, paths.ManifestPath + ".away");
try
{
await RefusedAsync("archive_offline");
}
finally
{
File.Move(paths.ManifestPath + ".away", paths.ManifestPath);
}
foreach (var complete in completions)
{
using var filed = await editor.PostAsync(complete, null);
await filed.ExpectAsync(HttpStatusCode.OK);
}
Assert.Equal("done", (await JobApiTests.GetAsync(editor, job.Id)).State);
var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!;
Assert.Equal(2, sequence.RenderedCount);
await Http.BytesEqualAsync(output, File.ReadAllBytesAsync(Path.Combine(server.Options.ArchivePath, sequence.Folder, "exports", "Roof - 4K HEVC.mp4")));
await Http.BytesEqualAsync(sent, File.ReadAllBytesAsync(Path.Combine(server.Options.ArchivePath, sequence.Folder, "exports", "Roof.mp4")));
}
[Test]
public async Task NodesReportFailuresAndNeedTheRight()
{