The topology module has two responsibilities:
- Topology policy for outbound connection decisions.
- Topology state dissemination for eventually consistent cluster-wide adjacency knowledge.
Policy keeps networking strategy pluggable. Topology state lets every node publish and merge direct-neighbor declarations learned via peer-to-peer gossip.
models.py- Defines immutable policy data models:
TopologyPeer(node identifier and advertised endpoint).TopologyContext(known peers, connected peer IDs, bootstrap peers).
- Defines immutable policy data models:
policy.py- Defines the abstract
TopologyPolicycontract:- target resolution (
resolve_connection_target), - connect selection (
select_peers_to_connect), - reserved extension hooks for disconnect and under-connected handling.
- target resolution (
- Defines the abstract
full_mesh.py- Implements
FullMeshTopologyPolicy, the default policy:- selects all non-connected peers from known and bootstrap sets,
- deduplicates by node ID,
- rewrites wildcard host (
0.0.0.0) to node ID for connectability, - keeps disconnect/remediation hooks as no-op.
- Implements
resolver.py- Maps configured policy names to concrete
TopologyPolicyinstances and validates allowed values.
- Maps configured policy names to concrete
state.py- Defines reusable disseminated topology state:
TopologyEntrywith{node_id, direct_neighbors, updated_at_ms}.TopologyStateStorethat keeps local declaration + merged global view.- LWW merge per node entry with deterministic tie-break on equal version.
- Defines reusable disseminated topology state:
__init__.py- Exposes the module public surface (
TopologyContext,TopologyPeer,TopologyPolicy,FullMeshTopologyPolicy, resolver function,TopologyEntry,TopologyStateStore).
- Exposes the module public surface (
- Internal:
utils.config.TopologyPolicyName- Provides the canonical configuration enum used to validate and resolve policy selection.
- Internal:
protocol.contracts.NetworkConstant- Provides the wildcard host constant used during outbound target normalization in full-mesh mode.
- Internal consumer:
runtime.networking- Builds
TopologyContext, invokes policy selection/resolution, and applies decisions to TCP peer registration.
- Builds
- Internal consumers:
gossip.publisher,gossip.handlers,runtime.networking,runtime.heartbeat- Publish and merge topology entries through
GOSSIP_STATE.
- Publish and merge topology entries through
- External (standard library):
dataclasses,abc- Support immutable data models and abstract policy interfaces.
-
Core responsibilities
- Define a stable policy contract for topology decisions.
- Represent topology decision inputs as immutable snapshots.
- Provide a default full-mesh policy consistent with current runtime behavior.
- Centralize policy instantiation from configuration.
-
Main data flow
- Runtime collects membership-discovered peers and configured bootstrap peers.
- Runtime builds
TopologyContextand callsselect_peers_to_connect. - For each selected peer, runtime calls
resolve_connection_target. - Runtime registers resolved targets in the outbound TCP client.
- Disconnect and under-connected hooks are currently invoked as extension points but return empty results in full-mesh mode.
- Runtime also updates local topology declaration on connectivity changes (connect, reconnect, unavailable).
- Gossip rounds include
state.topology.entries. - Receivers merge entries with LWW per node entry:
- newer
updated_at_mswins, - equal timestamp resolves deterministically by sorted neighbor tuple comparison.
- newer
- Any component can read
TopologyStateStore.topology_snapshot()orget_adjacency_map()without coupling to UI.
-
Interactions with other modules
- Input side: receives peer information originating from membership/discovery and bootstrap configuration (via runtime).
- Output side: provides connect candidates and resolved endpoints consumed by networking transport assembly.
- Configuration boundary: policy choice is controlled by configuration parsing (
TopologyPolicyName) and resolved throughresolver.py.