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>
85 lines
4.3 KiB
C#
85 lines
4.3 KiB
C#
using Chronos.Core.IO;
|
|
using Chronos.Data.Projects;
|
|
using Chronos.Data.Sync;
|
|
using Chronos.Protocol;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Sync;
|
|
|
|
/// <summary>How the desktop names a server (owner review 5, #66): by its announced id, its address keys kept as aliases.</summary>
|
|
[Category("sync")]
|
|
public sealed class ServerIdentityTests
|
|
{
|
|
private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01");
|
|
|
|
private static ServerInfo Info(Guid? id) => new("Studio NAS", "1.1.0", 1, [], null, [], new ServerLimits(1, 1), null, null, id);
|
|
|
|
[Test]
|
|
public void AServerThatAnnouncesAnIdIsKeyedByIt()
|
|
{
|
|
var key = ServerIdentity.Key(Info(Id), new Uri("https://NAS.local:8443/"));
|
|
Assert.Equal("id:" + Id.ToString("N"), key);
|
|
Assert.True(ServerIdentity.IsIdKey(key));
|
|
Assert.Equal(Id, ServerIdentity.IdOf(key));
|
|
// Another address of the same server: the same key.
|
|
Assert.Equal(key, ServerIdentity.Key(Info(Id), new Uri("https://192.168.1.5:8443")));
|
|
}
|
|
|
|
[Test]
|
|
public void AnOlderServerKeepsItsAddressKey()
|
|
{
|
|
var address = new Uri("https://NAS.local:8443/");
|
|
Assert.Equal("https://nas.local:8443", ServerIdentity.Key(Info(null), address));
|
|
Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key((ServerInfo?)null, address));
|
|
Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key(Guid.Empty, address));
|
|
Assert.False(ServerIdentity.IsIdKey(SyncService.ServerKey(address)));
|
|
}
|
|
|
|
[Test]
|
|
public void AliasesNameTheServerOfTheirId()
|
|
{
|
|
var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443", "https://192.168.1.5:8443"])]);
|
|
var key = ServerIdentity.IdKey(Id);
|
|
Assert.Equal(key, identity.Canonical("https://NAS.local:8443"));
|
|
Assert.Equal(key, identity.Canonical(key.ToUpperInvariant()));
|
|
Assert.Equal("https://other:1", identity.Canonical("https://other:1"));
|
|
Assert.True(identity.Same("https://nas.local:8443", "https://192.168.1.5:8443"));
|
|
Assert.True(identity.Same(key, "https://192.168.1.5:8443"));
|
|
Assert.False(identity.Same(key, "https://other:1"));
|
|
Assert.True(identity.Same(null, null));
|
|
Assert.False(identity.Same(key, null));
|
|
Assert.Equal(Id.ToString("N"), identity.FolderName("https://192.168.1.5:8443"));
|
|
Assert.Equal(HomeFolders.ServerFolder("https://other:1"), identity.FolderName("https://other:1"));
|
|
Assert.SequenceEqual(new[] { key, "https://nas.local:8443", "https://192.168.1.5:8443" }, identity.KeysOf("https://192.168.1.5:8443"));
|
|
Assert.SequenceEqual(new[] { "https://other:1" }, identity.KeysOf("https://other:1"));
|
|
Assert.Equal(2, identity.AliasesOf(Id).Count);
|
|
|
|
// Without the table every key is its own.
|
|
Assert.False(ServerIdentity.None.Same(key, "https://nas.local:8443"));
|
|
}
|
|
|
|
[Test]
|
|
public void AnAddressChangeKeepsTheKeyOnceTheAliasIsKnown()
|
|
{
|
|
var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443"])]);
|
|
var widened = identity.With(Id, "https://nas.local:9443");
|
|
Assert.True(widened.Same("https://nas.local:8443", "https://nas.local:9443"));
|
|
Assert.Same(widened, widened.With(Id, "HTTPS://nas.local:9443"));
|
|
Assert.False(identity.Same("https://nas.local:8443", "https://nas.local:9443"), "the table it came from is unchanged");
|
|
var other = Guid.CreateVersion7();
|
|
Assert.Equal(ServerIdentity.IdKey(other), widened.With(other, "https://second:1").Canonical("https://second:1"));
|
|
// An address two ids claim stays with the first one listed.
|
|
var clash = new ServerIdentity([new KnownServerKey(Id, ["https://x"]), new KnownServerKey(other, ["https://x"])]);
|
|
Assert.Equal(ServerIdentity.IdKey(Id), clash.Canonical("https://x"));
|
|
}
|
|
|
|
[Test]
|
|
public void TheArchiveIdOfAnyAliasIsTheServers()
|
|
{
|
|
string[] keys = [ServerIdentity.IdKey(Id), "https://nas.local:8443"];
|
|
Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://nas.local:8443"), keys));
|
|
Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf(keys[0]), keys));
|
|
Assert.False(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://other:1"), keys));
|
|
}
|
|
}
|