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>
73 lines
3.6 KiB
C#
73 lines
3.6 KiB
C#
using Chronos.Data.Sync;
|
|
using Chronos.Data.Tests.Infrastructure;
|
|
using Chronos.Data.Tests.Repositories;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Sync;
|
|
|
|
/// <summary>A server's links move from its address key to its id key (owner review 5, #66), the newer row winning a clash.</summary>
|
|
[Category("sync")]
|
|
public abstract class SyncLinkRekeyTests(TestContext context) : RepositoryTest(context)
|
|
{
|
|
private const string Address = "https://nas.local:8443";
|
|
private static readonly string IdKey = ServerIdentity.IdKey(Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"));
|
|
|
|
private static SyncLink Link(string server, Guid remoteId, Guid? localId, DateTimeOffset? synced, string title = "Harbour") => new()
|
|
{
|
|
Server = server,
|
|
RemoteId = remoteId,
|
|
LocalId = localId,
|
|
Title = title,
|
|
RemoteUpdatedAt = TestData.Now,
|
|
SyncedAt = synced,
|
|
};
|
|
|
|
[Test]
|
|
public async Task TheLinksOfAnAddressMoveToTheIdAndTheNewerRowWinsAClash()
|
|
{
|
|
var db = await OpenChronosAsync();
|
|
var archive = TestData.Archive();
|
|
await db.Archives.UpsertAsync(archive, Ct);
|
|
var (a, b, c) = (TestData.Sequence(archive.Id), TestData.Sequence(archive.Id), TestData.Sequence(archive.Id));
|
|
foreach (var sequence in new[] { a, b, c }) await db.Sequences.UpsertAsync(sequence, Ct);
|
|
var store = new SyncLinkStore(db);
|
|
var (plain, sameRemote, sameLocal, stale) = (Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7(), Guid.CreateVersion7());
|
|
// Only under the address: it moves.
|
|
await store.UpsertAsync(Link(Address, plain, a.Id, TestData.Now), Ct);
|
|
// Under both keys for one server sequence: the address's row is newer, so it replaces the id's.
|
|
await store.UpsertAsync(Link(Address, sameRemote, null, TestData.Now.AddHours(2), "newer"), Ct);
|
|
await store.UpsertAsync(Link(IdKey, sameRemote, null, TestData.Now.AddHours(1), "older"), Ct);
|
|
// Both keys link local copy b to different server sequences: the id's row is newer, so the address's goes.
|
|
await store.UpsertAsync(Link(Address, sameLocal, b.Id, TestData.Now), Ct);
|
|
await store.UpsertAsync(Link(IdKey, Guid.CreateVersion7(), b.Id, TestData.Now.AddDays(1)), Ct);
|
|
// Never compared under the address, compared under the id: the id's stays.
|
|
await store.UpsertAsync(Link(Address, stale, c.Id, null), Ct);
|
|
await store.UpsertAsync(Link(IdKey, stale, null, TestData.Now), Ct);
|
|
|
|
var (moved, dropped) = await store.RekeyAsync(Address, IdKey, Ct);
|
|
Assert.Equal((2, 3), (moved, dropped));
|
|
Assert.Empty(await store.ListAsync(Address, Ct));
|
|
var links = (await store.ListAsync(IdKey, Ct)).ToDictionary(l => l.RemoteId);
|
|
Assert.Equal(4, links.Count);
|
|
Assert.Equal(a.Id, links[plain].LocalId);
|
|
Assert.Equal("newer", links[sameRemote].Title);
|
|
Assert.False(links.ContainsKey(sameLocal));
|
|
Assert.Null(links[stale].LocalId);
|
|
Assert.Equal(b.Id, Assert.NotNull(await store.FindByLocalAsync(IdKey, b.Id, Ct)).LocalId);
|
|
|
|
// Again: nothing left to move; and a key onto itself does nothing.
|
|
Assert.Equal((0, 0), await store.RekeyAsync(Address, IdKey, Ct));
|
|
Assert.Equal((0, 0), await store.RekeyAsync(IdKey, IdKey, Ct));
|
|
}
|
|
}
|
|
|
|
public sealed class SqliteSyncLinkRekeyTests(TestContext context) : SyncLinkRekeyTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.SqliteMemory;
|
|
}
|
|
|
|
public sealed class PostgresSyncLinkRekeyTests(TestContext context) : SyncLinkRekeyTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.Postgres;
|
|
}
|