From da62f722532f2f4326451d8a3276361d8115edaa Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 17:50:02 +0100 Subject: [PATCH] Sync replica snapshots through CultMesh endpoint surface --- Aetheria.State/AetheriaVerseReplica.cs | 84 ++++++++++++++++---------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/Aetheria.State/AetheriaVerseReplica.cs b/Aetheria.State/AetheriaVerseReplica.cs index 7bfe3ca8b..4188e7d4d 100644 --- a/Aetheria.State/AetheriaVerseReplica.cs +++ b/Aetheria.State/AetheriaVerseReplica.cs @@ -39,13 +39,17 @@ public static class AetheriaVerseReplica endpoint, runtimeId, pullOnOpen: false).ConfigureAwait(false); - var applied = await CultMesh.ApplySnapshotAsync( - node, + var result = await CreateSnapshotEndpoint( endpoint, - CreateSnapshotOptions(runtimeId, schemaIds, recordKeys, connectTimeout, responseTimeout)) + runtimeId, + schemaIds, + recordKeys, + connectTimeout, + responseTimeout) + .SyncSnapshotAsync(node) .ConfigureAwait(false); await node.FlushAsync().ConfigureAwait(false); - return applied.Count; + return result.AppliedCount; } public static async Task FetchScopedSnapshotAsync( @@ -55,9 +59,14 @@ public static class AetheriaVerseReplica TimeSpan? connectTimeout = null, TimeSpan? responseTimeout = null) { - return await CultMesh.FetchSnapshotAsync( + return await CreateSnapshotEndpoint( endpoint, - CreateSnapshotOptions("aetheria-verse-replica", schemaIds, recordKeys, connectTimeout, responseTimeout)) + "aetheria-verse-replica", + schemaIds, + recordKeys, + connectTimeout, + responseTimeout) + .FetchSnapshotAsync() .ConfigureAwait(false); } @@ -79,6 +88,32 @@ public static class AetheriaVerseReplica return await surface.FetchDocumentsAsync(recordKeys, schemaIds).ConfigureAwait(false); } + public static async Task> SyncScopedDocumentsAsync( + string replicaStatePath, + string endpoint, + string runtimeId = "aetheria-verse-replica", + IReadOnlyList? schemaIds = null, + IReadOnlyList? recordKeys = null, + TimeSpan? connectTimeout = null, + TimeSpan? responseTimeout = null) + where T : class + { + using var node = await OpenReplicaNodeAsync( + replicaStatePath, + endpoint, + runtimeId, + pullOnOpen: false).ConfigureAwait(false); + return await CreateSnapshotEndpoint( + endpoint, + runtimeId, + schemaIds, + recordKeys, + connectTimeout, + responseTimeout) + .SyncDocumentsAsync(node, recordKeys, schemaIds, flush: true) + .ConfigureAwait(false); + } + public static async Task RunReplicaAsync( string replicaStatePath, string endpoint, @@ -158,42 +193,25 @@ public static class AetheriaVerseReplica }; var sequence = 0L; + var surface = CreateSnapshotEndpoint( + endpoint, + runtimeId, + schemaIds: null, + recordKeys: null, + connectTimeout: null, + responseTimeout: TimeSpan.FromSeconds(15)); foreach (var recordKey in recordKeys) { - var applied = await SyncScopedSnapshotAsync( + var result = await surface.SyncSnapshotAsync( node, - endpoint, - runtimeId, schemaIds: null, - recordKeys: new[] { recordKey }, - responseTimeout: TimeSpan.FromSeconds(15)).ConfigureAwait(false); - sequence = Math.Max(sequence, SnapshotSequence(applied)); + recordKeys: new[] { recordKey }).ConfigureAwait(false); + sequence = Math.Max(sequence, result.ShardLogSequence); } return sequence; } - private static async Task SyncScopedSnapshotAsync( - CultMeshNode node, - string endpoint, - string runtimeId, - IReadOnlyList? schemaIds, - IReadOnlyList? recordKeys, - TimeSpan responseTimeout) - { - var snapshot = await CultMesh.FetchSnapshotAsync( - endpoint, - CreateSnapshotOptions(runtimeId, schemaIds, recordKeys, null, responseTimeout)) - .ConfigureAwait(false); - await node.Database.Documents.ApplyRawSnapshotResponseAsync(node.Cache, snapshot).ConfigureAwait(false); - return snapshot; - } - - private static long SnapshotSequence(CultNetSnapshotResponseRawMessage snapshot) - { - return snapshot.ShardLogSequence ?? 0L; - } - private static CultMeshSnapshotRequestOptions CreateSnapshotOptions( string runtimeId, IReadOnlyList? schemaIds,