From 3514465db58c4e8f4d587a3775e12899e3d96b4f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 14 Jun 2026 13:17:50 +0100 Subject: [PATCH] Fan out Python snapshot sync --- packages/cultcache-py/PARITY.md | 7 +- .../cultcache-py/src/cultmesh_py/client.py | 85 +++++++++++++++++-- packages/cultcache-py/tests/test_cultcache.py | 59 +++++++++++++ .../Contracts/cultnet-distributed-database.md | 4 +- 4 files changed, 142 insertions(+), 13 deletions(-) diff --git a/packages/cultcache-py/PARITY.md b/packages/cultcache-py/PARITY.md index 59802693..2b3c720a 100644 --- a/packages/cultcache-py/PARITY.md +++ b/packages/cultcache-py/PARITY.md @@ -79,9 +79,10 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti. CultNet frames. It can also parse `cultnet://host:port` endpoints, discover from endpoint lists, de-duplicate discovery targets, fan out one-shot discovery from endpoints already advertised in local Verse and peer catalogs, - fan out simulation observations to peer-card endpoints, and upsert results - into local catalogs with the same count-returning ergonomics as the C# - discovery clients. + fan out raw snapshot fetches and simulation observations to peer-card + endpoints, apply peer snapshots through a caller-owned database, and upsert + results into local catalogs with the same count-returning ergonomics as the + C# discovery clients. - Python CultMesh facade: Exposes C#-matching client entrypoints for Verse discovery, peer exchange, raw CultNet client construction, local nodes, catalogs, sessions, streams, diff --git a/packages/cultcache-py/src/cultmesh_py/client.py b/packages/cultcache-py/src/cultmesh_py/client.py index e5e8e365..56adb1a7 100644 --- a/packages/cultcache-py/src/cultmesh_py/client.py +++ b/packages/cultcache-py/src/cultmesh_py/client.py @@ -4,7 +4,7 @@ from dataclasses import dataclass from typing import Any, Callable from urllib.parse import urlparse -from cultnet_py import CultNetRawClient +from cultnet_py import CultNetRawClient, CultNetRawSnapshotResponse from .wire import ( PEER_EXCHANGE_RESPONSE, @@ -208,15 +208,8 @@ class CultMeshDiscoveryClient: roles: list[str] | None = None, on_error: Callable[[str, Exception], None] | None = None, ) -> list[dict[str, Any]]: - requested_roles = set(roles or []) - endpoints = [ - endpoint - for peer in catalog.find(verse_id) - if not requested_roles or requested_roles.intersection(peer.roles) - for endpoint in peer.endpoints - ] candidates = [] - for endpoint in _distinct_non_empty(endpoints): + for endpoint in self._fanout_endpoints(catalog, verse_id=verse_id, roles=roles): try: client = type(self).from_endpoint(endpoint, timeout_seconds=self.timeout_seconds) candidates.append(client.submit_simulation_observation(message)) @@ -226,6 +219,80 @@ class CultMeshDiscoveryClient: on_error(endpoint, error) return candidates + def fanout_snapshot_responses( + self, + catalog: CultMeshPeerCatalog, + *, + verse_id: str, + roles: list[str] | None = None, + schema_ids: list[str] | None = None, + record_keys: list[str] | None = None, + shard_id: str | None = None, + shard_epoch: int | None = None, + on_error: Callable[[str, Exception], None] | None = None, + ) -> list[CultNetRawSnapshotResponse]: + responses: list[CultNetRawSnapshotResponse] = [] + for endpoint in self._fanout_endpoints(catalog, verse_id=verse_id, roles=roles): + try: + client = type(self).from_endpoint(endpoint, timeout_seconds=self.timeout_seconds) + raw_client = CultNetRawClient(client.host, client.port, client.timeout_seconds) + responses.append( + raw_client.fetch_snapshot_response( + schema_ids=schema_ids, + record_keys=record_keys, + shard_id=shard_id, + shard_epoch=shard_epoch, + ) + ) + except Exception as error: + if on_error is None: + raise + on_error(endpoint, error) + return responses + + def sync_snapshots( + self, + database: Any, + catalog: CultMeshPeerCatalog, + *, + verse_id: str, + roles: list[str] | None = None, + schema_ids: list[str] | None = None, + record_keys: list[str] | None = None, + shard_id: str | None = None, + shard_epoch: int | None = None, + on_error: Callable[[str, Exception], None] | None = None, + ) -> list[Any]: + applied = [] + for response in self.fanout_snapshot_responses( + catalog, + verse_id=verse_id, + roles=roles, + schema_ids=schema_ids, + record_keys=record_keys, + shard_id=shard_id, + shard_epoch=shard_epoch, + on_error=on_error, + ): + applied.extend(database.apply_snapshot_response(response)) + return applied + + def _fanout_endpoints( + self, + catalog: CultMeshPeerCatalog, + *, + verse_id: str, + roles: list[str] | None = None, + ) -> list[str]: + requested_roles = set(roles or []) + endpoints = [ + endpoint + for peer in catalog.find(verse_id) + if not requested_roles or requested_roles.intersection(peer.roles) + for endpoint in peer.endpoints + ] + return _distinct_non_empty(endpoints) + @classmethod def from_endpoint(cls, endpoint: str, *, timeout_seconds: float = 4.0) -> "CultMeshDiscoveryClient": host, port = _parse_endpoint(endpoint) diff --git a/packages/cultcache-py/tests/test_cultcache.py b/packages/cultcache-py/tests/test_cultcache.py index ff7865db..b4f36d85 100644 --- a/packages/cultcache-py/tests/test_cultcache.py +++ b/packages/cultcache-py/tests/test_cultcache.py @@ -2554,6 +2554,65 @@ class CultCacheTests(unittest.TestCase): self.assertEqual({candidate["messageId"] for candidate in candidates}, {"fanout-observation"}) self.assertEqual({candidate["hasQuorum"] for candidate in candidates}, {True}) + def test_cultmesh_discovery_client_fans_out_snapshots_to_peers(self) -> None: + document = define_database_entry_type( + "mesh.snapshot_fanout_note", + [("body", 0)], + schema_id="mesh.snapshot_fanout_note.v1", + ) + first = CultMesh.create_node(runtime_id="snapshot-peer-a") + first.database.register_document(document) + first.database.put_raw_message(document, "note:a", {"body": "from-a"}, shard_id="notes", shard_epoch=1) + second = CultMesh.create_node(runtime_id="snapshot-peer-b") + second.database.register_document(document) + second.database.put_raw_message(document, "note:b", {"body": "from-b"}, shard_id="notes", shard_epoch=1) + target = CultMesh.create_node(runtime_id="snapshot-target") + target.database.register_document(document) + first_server = CultMesh.serve_node(first) + second_server = CultMesh.serve_node(second) + try: + peers = CultMeshPeerCatalog() + peers.upsert(CultMeshPeerCard( + peer_id="snapshot-peer-a", + verse_id="mesh", + endpoints=(f"cultnet://127.0.0.1:{first_server.port}", f"cultnet://127.0.0.1:{first_server.port}"), + roles=("read-replica",), + )) + peers.upsert(CultMeshPeerCard( + peer_id="snapshot-peer-b", + verse_id="mesh", + endpoints=(f"cultnet://127.0.0.1:{second_server.port}",), + roles=("read-replica",), + )) + + client = CultMeshDiscoveryClient("127.0.0.1", first_server.port, timeout_seconds=2.0) + responses = client.fanout_snapshot_responses( + peers, + verse_id="mesh", + roles=["read-replica"], + schema_ids=["mesh.snapshot_fanout_note.v1"], + ) + applied = client.sync_snapshots( + target.database, + peers, + verse_id="mesh", + roles=["read-replica"], + schema_ids=["mesh.snapshot_fanout_note.v1"], + ) + finally: + first_server.stop() + second_server.stop() + + self.assertEqual(len(responses), 2) + self.assertTrue(all(isinstance(response, CultNetRawSnapshotResponse) for response in responses)) + self.assertEqual( + sorted(record.record_key for response in responses for record in response.documents), + ["note:a", "note:b"], + ) + self.assertEqual(sorted(record.record_key for record in applied), ["note:a", "note:b"]) + self.assertEqual(target.database.get_required(document, "note:a")["body"], "from-a") + self.assertEqual(target.database.get_required(document, "note:b")["body"], "from-b") + def test_cultmesh_local_server_serves_node_and_catalogs_over_clients(self) -> None: document = define_database_entry_type( "mesh.server_note", diff --git a/src/GameCult.Networking/Contracts/cultnet-distributed-database.md b/src/GameCult.Networking/Contracts/cultnet-distributed-database.md index 4b93bd01..90955e61 100644 --- a/src/GameCult.Networking/Contracts/cultnet-distributed-database.md +++ b/src/GameCult.Networking/Contracts/cultnet-distributed-database.md @@ -94,7 +94,7 @@ Not implemented yet: - membership/failure detection - leader election or automatic shard failover -- peer-to-peer snapshot fanout and throttling +- scheduled peer-to-peer snapshot fanout beyond Python's one-shot client helper - scheduled peer-to-peer fanout for observation gossip and candidate propagation - cross-runtime rollback/resimulation helpers for simulation frames - declared CRDT merge policies @@ -148,6 +148,8 @@ Add discovery fanout and operational polish: package-local Python HMAC verifier - standardize cross-runtime simulation-frame rollback/resimulation helpers beyond Python's local pending-prediction rollback surface +- add scheduled peer-to-peer snapshot fanout beyond Python's one-shot client + helper - add scheduled peer-to-peer fanout for observation gossip and candidate propagation beyond Python's one-shot client helper