The desktop keyed every server by the address it was reached at, so one
NAS reached by name and by IP, or after an address change, looked like two
servers, and all servers shared one workspace and one cache. Each server
now announces a stable id, and the desktop's folders are sorted into one
home per server, so that every server can have its own archive of
downloaded originals (or5 answers Q1).
- The server makes a Guid v7 once, keeps it as the app setting server.id
(so it survives restarts and restores, on SQLite and Postgres) and
announces it as ServerInfo.ServerId. Older clients ignore the field, and
older servers read as null.
- ServerIdentity keys a server as id:<N> when it announces an id, and by
its address otherwise. Address keys are kept as aliases.
- HomeFolders names the homes: local, servers/<id> or
servers/addr-<hash16>. The hash equals the 1.0 proxy cache folder name.
It also names the per-server originals archive root.
- WorkspaceHomes splits the workspace into one format-1 store per home
under a format-2 parent manifest, which 1.0 refuses. A 1.0 workspace
stays one store until it is sorted.
- HomeLayoutMigration plans as a dry run and applies through a journal:
- only same-folder renames, each checked by inventory;
- a clash keeps the copy saved last and sends the other to the trash;
- it resumes after a crash at any step;
- it skips leased projects;
- it defers an offline workspace or one being moved, while the cache
part still runs.
- RekeyAsync moves an alias home to the id home: whole folders, or
project-by-project merges. It also rewrites working states under their
lease, and moves sync_links (the newer row wins a clash).
- ProxyCache, the job root resolver, WorkspaceMover and WorkspaceCleaner
understand the split layout. The new layout is opt-in through
SyncOptions.ServersDirectory, so the app sees no change until it is
wired.
Refs #66
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
298 lines
17 KiB
C#
298 lines
17 KiB
C#
using Chronos.Core.IO;
|
|
using Chronos.Core.Project;
|
|
using Chronos.Data.Homes;
|
|
using Chronos.Data.Sync;
|
|
using Chronos.Data.Workspaces;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Homes;
|
|
|
|
/// <summary>The one-time sorting of a 1.0 workspace and cache into homes (owner review 5, #66).</summary>
|
|
[Category("homes")]
|
|
public sealed class HomeLayoutMigrationTests(TestContext context)
|
|
{
|
|
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
|
|
|
|
/// <summary>A 1.0 data folder: a workspace with local, server and damaged projects and a trash, and a cache with proxies, render-node files and a staged import.</summary>
|
|
private sealed class Legacy
|
|
{
|
|
public required string Data { get; init; }
|
|
public required string Workspace { get; init; }
|
|
public required string Cache { get; init; }
|
|
public required Guid Local { get; init; }
|
|
public required Guid OnNas { get; init; }
|
|
public required Guid OnOther { get; init; }
|
|
public required Guid Damaged { get; init; }
|
|
public required Guid NasRemote { get; init; }
|
|
|
|
public HomeLayoutMigration Migration(ServerIdentity? identity = null, Action<string, string>? cut = null) =>
|
|
new(new HomeLayoutOptions
|
|
{
|
|
DataDirectory = Data,
|
|
WorkspaceRoot = Workspace,
|
|
CacheRoot = Cache,
|
|
Identity = identity ?? ServerIdentity.None,
|
|
CurrentServer = HomeFixtures.Nas,
|
|
})
|
|
{ Cut = cut };
|
|
}
|
|
|
|
private Legacy Make()
|
|
{
|
|
var data = context.CreateTempDirectory("data");
|
|
var workspace = HomeFixtures.LegacyWorkspace(data);
|
|
var local = HomeFixtures.Project(workspace, HomeFixtures.Local());
|
|
var onNas = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Nas));
|
|
var onOther = HomeFixtures.Project(workspace, HomeFixtures.Server(HomeFixtures.Other));
|
|
var damaged = Guid.CreateVersion7();
|
|
Directory.CreateDirectory(Path.Combine(workspace, damaged.ToString("N")));
|
|
File.WriteAllText(Path.Combine(workspace, damaged.ToString("N"), WorkspaceProject.BundleFile), "{ not json");
|
|
HomeFixtures.TrashEntry(workspace, onNas, HomeFixtures.Server(HomeFixtures.Nas));
|
|
File.WriteAllText(Path.Combine(workspace, WorkspaceTrash.FolderName, "loose.txt"), "kept");
|
|
var cache = Path.Combine(data, "cache");
|
|
var nasRemote = Guid.CreateVersion7();
|
|
HomeFixtures.LegacyProxies(cache, HomeFixtures.Nas, nasRemote);
|
|
HomeFixtures.LegacyProxies(cache, HomeFixtures.Other, Guid.CreateVersion7());
|
|
Directory.CreateDirectory(Path.Combine(cache, "render-node", "job1"));
|
|
File.WriteAllText(Path.Combine(cache, "render-node", "job1", "frame.jpg"), "rn");
|
|
Directory.CreateDirectory(Path.Combine(cache, "server-import"));
|
|
File.WriteAllText(Path.Combine(cache, "server-import", "card.part"), "import");
|
|
return new Legacy
|
|
{
|
|
Data = data,
|
|
Workspace = workspace,
|
|
Cache = cache,
|
|
Local = local,
|
|
OnNas = onNas,
|
|
OnOther = onOther,
|
|
Damaged = damaged,
|
|
NasRemote = nasRemote,
|
|
};
|
|
}
|
|
|
|
/// <summary>Every file and folder under <paramref name="root"/> with its size and time: what a dry run must leave as it was.</summary>
|
|
private static List<string> Snapshot(string root) =>
|
|
[.. new DirectoryInfo(root).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
|
|
.Select(e => Path.GetRelativePath(root, e.FullName) + "|" + (e is FileInfo f ? f.Length + "|" + f.LastWriteTimeUtc.Ticks : "dir"))
|
|
.Order(StringComparer.Ordinal)];
|
|
|
|
private static string Home(string workspace, string key) => Path.Combine(workspace, "servers", HomeFolders.ServerFolder(key));
|
|
|
|
private static void AssertSorted(Legacy legacy, string nasFolder)
|
|
{
|
|
var ws = legacy.Workspace;
|
|
Assert.True(WorkspaceHomes.IsSplitRoot(ws));
|
|
Assert.FileExists(Path.Combine(ws, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile));
|
|
Assert.FileExists(Path.Combine(ws, "local", legacy.Damaged.ToString("N"), WorkspaceProject.BundleFile));
|
|
Assert.FileExists(Path.Combine(ws, "servers", nasFolder, legacy.OnNas.ToString("N"), WorkspaceProject.BundleFile));
|
|
Assert.FileExists(Path.Combine(Home(ws, HomeFixtures.Other), legacy.OnOther.ToString("N"), WorkspaceProject.BundleFile));
|
|
Assert.FileExists(Path.Combine(ws, "local", WorkspaceTrash.FolderName, "loose.txt"));
|
|
Assert.Count(1, Directory.EnumerateDirectories(Path.Combine(ws, "servers", nasFolder, WorkspaceTrash.FolderName)));
|
|
Assert.False(Directory.EnumerateDirectories(ws).Any(d => Guid.TryParseExact(Path.GetFileName(d), "N", out _)), "no project is left in the parent");
|
|
Assert.False(Directory.Exists(Path.Combine(ws, WorkspaceTrash.FolderName)), "the parent's emptied trash went");
|
|
Assert.Equal("Studio NAS", Assert.NotNull(ServerHomeRecord.Read(Path.Combine(ws, "servers", nasFolder))).Name);
|
|
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "proxies", legacy.NasRemote.ToString("N"), "remote.json"));
|
|
Assert.True(Directory.Exists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Other), "proxies")));
|
|
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "render-node", "job1", "frame.jpg"));
|
|
Assert.FileExists(Path.Combine(legacy.Cache, "servers", nasFolder, "import", "card.part"));
|
|
Assert.False(Directory.Exists(Path.Combine(legacy.Cache, "render-node")));
|
|
Assert.FileMissing(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
|
var homes = new WorkspaceHomes(ws);
|
|
Assert.True(homes.IsSplit);
|
|
Assert.Equal(4, homes.List().Count);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ThePlanWritesNothing()
|
|
{
|
|
var legacy = Make();
|
|
var before = Snapshot(legacy.Data);
|
|
var plan = await legacy.Migration().PlanAsync(context.Cancellation);
|
|
Assert.SequenceEqual(before, Snapshot(legacy.Data));
|
|
Assert.True(plan.SplitsWorkspace);
|
|
Assert.Null(plan.Deferred);
|
|
Assert.Equal(4, plan.Projects);
|
|
Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Trash));
|
|
Assert.Equal(2, plan.Entries.Count(e => e.Kind == HomeEntryKind.Proxies));
|
|
Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.RenderNode));
|
|
Assert.Equal(1, plan.Entries.Count(e => e.Kind == HomeEntryKind.Import));
|
|
var damaged = plan.Entries.Single(e => e.Source.EndsWith(legacy.Damaged.ToString("N"), StringComparison.Ordinal));
|
|
Assert.Equal(Path.Combine(legacy.Workspace, "local", legacy.Damaged.ToString("N")), damaged.Target);
|
|
Assert.Equal(plan.Fingerprint, (await legacy.Migration().PlanAsync(context.Cancellation)).Fingerprint);
|
|
}
|
|
|
|
[Test]
|
|
public async Task SortingMovesEveryHomeAndRenamesOnly()
|
|
{
|
|
var legacy = Make();
|
|
var before = HomeFixtures.Manifest(legacy.Workspace);
|
|
var cacheBefore = HomeFixtures.Manifest(legacy.Cache);
|
|
var result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.Equal((4, 0, false, true), (result.ProjectsMoved, result.InUse, result.Resumed, result.Split));
|
|
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
|
// Every file is there with the same name relative to its project and the same bytes: the sorting only renamed.
|
|
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace));
|
|
Assert.SequenceEqual(cacheBefore.Values.Order(), HomeFixtures.Manifest(legacy.Cache).Values.Order());
|
|
|
|
// A second run does nothing.
|
|
var again = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.Equal(HomeLayoutResult.Nothing, again);
|
|
Assert.True((await legacy.Migration().PlanAsync(context.Cancellation)).IsEmpty);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AServerKnownByIdGetsItsIdHome()
|
|
{
|
|
var legacy = Make();
|
|
await legacy.Migration(new ServerIdentity([new KnownServerKey(Id, [HomeFixtures.Nas])])).ApplyAsync(context.Cancellation);
|
|
AssertSorted(legacy, Id.ToString("N"));
|
|
Assert.Equal(ServerIdentity.IdKey(Id), Assert.NotNull(ServerHomeRecord.Read(Path.Combine(legacy.Workspace, "servers", Id.ToString("N")))).Key);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ACrashAtAnyStepIsTakenUpToTheSameEnd()
|
|
{
|
|
var entries = (await Make().Migration().PlanAsync(context.Cancellation)).Entries.Count;
|
|
var cuts = new List<(string Step, int Occurrence)> { ("journal", 1), ("commit", 1) };
|
|
for (var i = 1; i <= entries; i++) cuts.AddRange([("rename", i), ("renamed", i)]);
|
|
foreach (var (step, occurrence) in cuts)
|
|
{
|
|
var legacy = Make();
|
|
var before = HomeFixtures.Manifest(legacy.Workspace);
|
|
var seen = 0;
|
|
var crashing = legacy.Migration(cut: (name, _) =>
|
|
{
|
|
if (name == step && ++seen == occurrence) throw new IOException("cut at " + step);
|
|
});
|
|
await Assert.ThrowsAsync<IOException>(() => crashing.ApplyAsync(context.Cancellation));
|
|
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
|
var resumed = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.True(resumed.Resumed, $"{step} #{occurrence}");
|
|
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
|
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace), $"{step} #{occurrence}");
|
|
Assert.Equal(HomeLayoutResult.Nothing, await legacy.Migration().ApplyAsync(context.Cancellation));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task AProjectInUseIsLeftAndMovedAtALaterStart()
|
|
{
|
|
var legacy = Make();
|
|
var held = HomeFixtures.Hold(Path.Combine(legacy.Workspace, legacy.Local.ToString("N")));
|
|
HomeLayoutResult result;
|
|
using (held)
|
|
{
|
|
var plan = await legacy.Migration().PlanAsync(context.Cancellation);
|
|
Assert.SequenceEqual(new[] { legacy.Local }, plan.InUse);
|
|
result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.Equal((3, 1, true), (result.ProjectsMoved, result.InUse, result.Split));
|
|
Assert.True(WorkspaceHomes.IsSplitRoot(legacy.Workspace));
|
|
var homes = new WorkspaceHomes(legacy.Workspace);
|
|
Assert.Equal(homes.Root, Path.GetDirectoryName(Assert.NotNull(homes.Find(legacy.Local)).Folder));
|
|
Assert.Equal(4, homes.List().Count);
|
|
}
|
|
result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.Equal((1, 0), (result.ProjectsMoved, result.InUse));
|
|
Assert.FileExists(Path.Combine(legacy.Workspace, "local", legacy.Local.ToString("N"), WorkspaceProject.BundleFile));
|
|
Assert.Null(new WorkspaceHomes(legacy.Workspace).Homes().FirstOrDefault(h => h.IsLegacy));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnUnfinishedMoveDefersTheWorkspaceWhileTheCacheIsSorted()
|
|
{
|
|
var legacy = Make();
|
|
File.WriteAllText(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile), "{}");
|
|
var before = Snapshot(legacy.Workspace);
|
|
var result = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Moving, result.Deferred);
|
|
Assert.False(result.Split);
|
|
Assert.SequenceEqual(before, Snapshot(legacy.Workspace));
|
|
Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "proxies", legacy.NasRemote.ToString("N"), "remote.json"));
|
|
|
|
File.Delete(Path.Combine(legacy.Workspace, WorkspaceMover.JournalFile));
|
|
await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AWorkspaceThatWentOfflineWaitsWithItsJournal()
|
|
{
|
|
var legacy = Make();
|
|
var before = HomeFixtures.Manifest(legacy.Workspace);
|
|
await Assert.ThrowsAsync<IOException>(() => legacy.Migration(cut: (name, _) =>
|
|
{
|
|
if (name == "journal") throw new IOException("cut");
|
|
}).ApplyAsync(context.Cancellation));
|
|
// The drive goes before the next start.
|
|
var away = legacy.Workspace + "-unplugged";
|
|
Directory.Move(legacy.Workspace, away);
|
|
var waiting = await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
Assert.Equal((HomeLayoutDeferral?)HomeLayoutDeferral.Offline, waiting.Deferred);
|
|
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
|
Assert.FileExists(Path.Combine(legacy.Cache, "servers", HomeFolders.ServerFolder(HomeFixtures.Nas), "import", "card.part"));
|
|
// Back: the workspace is sorted, the cache left as it is now.
|
|
Directory.Move(away, legacy.Workspace);
|
|
await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
|
Assert.SequenceEqual(before, HomeFixtures.Manifest(legacy.Workspace));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AProjectInBothPlacesKeepsTheCopySavedLastAndTrashesTheOther()
|
|
{
|
|
var data = context.CreateTempDirectory("data");
|
|
var workspace = HomeFixtures.LegacyWorkspace(data);
|
|
var id = Guid.CreateVersion7();
|
|
var home = HomeFixtures.Local();
|
|
HomeFixtures.Project(workspace, home, "newer", DateTimeOffset.UtcNow, id);
|
|
HomeFixtures.Project(Path.Combine(workspace, "local"), home, "older", DateTimeOffset.UtcNow.AddDays(-1), id);
|
|
var files = Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories).Count(f => Path.GetFileName(f) != WorkspaceStore.ManifestFile);
|
|
var result = await new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = workspace }).ApplyAsync(context.Cancellation);
|
|
Assert.Equal(1, result.Clashes);
|
|
var kept = ProjectBundle.Load(Path.Combine(workspace, "local", id.ToString("N"), WorkspaceProject.BundleFile));
|
|
Assert.Equal("newer", kept.Workspace!.Name);
|
|
var trashed = Assert.NotNull(Directory.EnumerateDirectories(Path.Combine(workspace, "local", WorkspaceTrash.FolderName)).SingleOrDefault());
|
|
Assert.Equal("older", ProjectBundle.Load(Path.Combine(trashed, WorkspaceProject.BundleFile)).Workspace!.Name);
|
|
Assert.Equal(files, Directory.EnumerateFiles(workspace, "*", SearchOption.AllDirectories)
|
|
.Count(f => Path.GetFileName(f) is not (WorkspaceStore.ManifestFile or "server.json")), "nothing was deleted");
|
|
}
|
|
|
|
[Test]
|
|
public async Task ARenameThatDoesNotCheckStopsWithItsJournal()
|
|
{
|
|
var legacy = Make();
|
|
var tampered = false;
|
|
var migration = legacy.Migration(cut: (name, path) =>
|
|
{
|
|
if (name != "renamed" || tampered || !Directory.Exists(path)) return;
|
|
tampered = true;
|
|
File.WriteAllText(Path.Combine(path, "stray.txt"), "written meanwhile");
|
|
});
|
|
await Assert.ThrowsAsync<HomeLayoutException>(() => migration.ApplyAsync(context.Cancellation));
|
|
Assert.FileExists(Path.Combine(legacy.Data, HomeLayoutMigration.JournalFile));
|
|
// The next start takes it up; the entry found renamed is taken as moved.
|
|
await legacy.Migration().ApplyAsync(context.Cancellation);
|
|
AssertSorted(legacy, HomeFolders.ServerFolder(HomeFixtures.Nas));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AWorkspaceNeverMadeOrAlreadySortedHasNothingToDo()
|
|
{
|
|
var data = context.CreateTempDirectory("data");
|
|
var options = new HomeLayoutOptions { DataDirectory = data, WorkspaceRoot = Path.Combine(data, "workspace"), CacheRoot = Path.Combine(data, "cache") };
|
|
Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty);
|
|
Assert.Equal(HomeLayoutResult.Nothing, await new HomeLayoutMigration(options).ApplyAsync(context.Cancellation));
|
|
Assert.False(Directory.Exists(options.WorkspaceRoot));
|
|
|
|
_ = new WorkspaceHomes(options.WorkspaceRoot);
|
|
Assert.True((await new HomeLayoutMigration(options).PlanAsync(context.Cancellation)).IsEmpty);
|
|
|
|
// An empty 1.0 workspace is split by its manifest alone.
|
|
var empty = HomeFixtures.LegacyWorkspace(context.CreateTempDirectory("empty"));
|
|
var split = await new HomeLayoutMigration(options with { WorkspaceRoot = empty }).ApplyAsync(context.Cancellation);
|
|
Assert.True(split.Split);
|
|
Assert.True(WorkspaceHomes.IsSplitRoot(empty));
|
|
Assert.Equal(WorkspaceStore.FormatVersion, new WorkspaceStore(Path.Combine(empty, "local")).Manifest.FormatVersion);
|
|
}
|
|
}
|