pub struct ShardedColony { /* private fields */ }Expand description
A sharded colony that participates in distributed coordination.
ShardedColony wraps a local Colony instance and adds the machinery needed for distributed operation:
- Consistent hashing for document routing
- Ghost node cache for cross-shard edge references
- Cross-shard edge management for edges spanning shards
- Peer shard address tracking
- Cross-shard edge collection during tick phases
Implementations§
Source§impl ShardedColony
impl ShardedColony
Sourcepub fn new(
shard_id: ShardId,
config: ColonyConfig,
hash_ring: Arc<RwLock<ConsistentHashRing>>,
) -> Self
pub fn new( shard_id: ShardId, config: ColonyConfig, hash_ring: Arc<RwLock<ConsistentHashRing>>, ) -> Self
Create a new sharded colony.
§Arguments
shard_id- Unique identifier for this shardconfig- Colony configuration parametershash_ring- Shared consistent hash ring for routing
§Example
let shard = ShardedColony::new(
ShardId::new(0),
ColonyConfig::default(),
Arc::new(RwLock::new(ConsistentHashRing::new(3))),
);Sourcepub fn with_ghost_cache_size(
shard_id: ShardId,
config: ColonyConfig,
hash_ring: Arc<RwLock<ConsistentHashRing>>,
ghost_cache_size: usize,
) -> Self
pub fn with_ghost_cache_size( shard_id: ShardId, config: ColonyConfig, hash_ring: Arc<RwLock<ConsistentHashRing>>, ghost_cache_size: usize, ) -> Self
Create a new sharded colony with a custom ghost cache size.
§Arguments
shard_id- Unique identifier for this shardconfig- Colony configuration parametershash_ring- Shared consistent hash ring for routingghost_cache_size- Maximum number of ghost nodes to cache
Sourcepub async fn owns_document(&self, doc_id: &DocumentId) -> bool
pub async fn owns_document(&self, doc_id: &DocumentId) -> bool
Check if a document belongs to this shard.
Uses the consistent hash ring to determine ownership.
§Arguments
doc_id- The document ID to check
Sourcepub fn owns_document_sync(
&self,
doc_id: &DocumentId,
ring: &ConsistentHashRing,
) -> bool
pub fn owns_document_sync( &self, doc_id: &DocumentId, ring: &ConsistentHashRing, ) -> bool
Check ownership synchronously (for non-async contexts).
This is a blocking operation and should only be used when async is not available.
Sourcepub async fn ingest_document(
&mut self,
title: &str,
content: &str,
position: Position,
) -> DistributedResult<DocumentId>
pub async fn ingest_document( &mut self, title: &str, content: &str, position: Position, ) -> DistributedResult<DocumentId>
Ingest a document (only if this shard owns it).
Returns an error if the document should be routed to a different shard.
§Arguments
title- Document titlecontent- Document contentposition- Spatial position in the substrate
§Returns
The document ID if successful, or a routing error if this shard doesn’t own the document.
Sourcepub fn ingest_document_direct(
&mut self,
title: &str,
content: &str,
position: Position,
) -> DocumentId
pub fn ingest_document_direct( &mut self, title: &str, content: &str, position: Position, ) -> DocumentId
Ingest a document with a pre-determined ID.
This is useful when routing a document from the coordinator, where the ID has already been assigned.
§Arguments
title- Document titlecontent- Document contentposition- Spatial position in the substrate
Sourcepub fn tick_phase(&mut self, phase: TickPhase) -> PhaseResult
pub fn tick_phase(&mut self, phase: TickPhase) -> PhaseResult
Execute a tick phase.
Each tick is divided into phases that must be synchronized across all shards. The coordinator ensures all shards complete each phase before moving to the next.
§Arguments
phase- The phase to execute
§Returns
A PhaseResult containing statistics and any cross-shard edges
that need resolution.
Sourcepub fn add_peer(&mut self, shard_id: ShardId, address: String)
pub fn add_peer(&mut self, shard_id: ShardId, address: String)
Add a peer shard address.
§Arguments
shard_id- The peer’s shard IDaddress- The peer’s network address (e.g., “127.0.0.1:8081”)
Sourcepub fn remove_peer(&mut self, shard_id: ShardId)
pub fn remove_peer(&mut self, shard_id: ShardId)
Remove a peer shard.
Also invalidates any ghost nodes from that shard.
§Arguments
shard_id- The peer’s shard ID
Sourcepub fn peer_address(&self, shard_id: &ShardId) -> Option<&String>
pub fn peer_address(&self, shard_id: &ShardId) -> Option<&String>
Get a peer’s address.
Sourcepub fn ghost_cache(&self) -> &GhostNodeCache
pub fn ghost_cache(&self) -> &GhostNodeCache
Get ghost node cache.
Sourcepub fn ghost_cache_mut(&mut self) -> &mut GhostNodeCache
pub fn ghost_cache_mut(&mut self) -> &mut GhostNodeCache
Get mutable ghost node cache.
Sourcepub fn edge_manager(&self) -> &CrossShardEdgeManager
pub fn edge_manager(&self) -> &CrossShardEdgeManager
Get the cross-shard edge manager.
Sourcepub fn edge_manager_mut(&mut self) -> &mut CrossShardEdgeManager
pub fn edge_manager_mut(&mut self) -> &mut CrossShardEdgeManager
Get mutable cross-shard edge manager.
Sourcepub fn register_cross_shard_edge(&mut self, edge: CrossShardEdge)
pub fn register_cross_shard_edge(&mut self, edge: CrossShardEdge)
Register an outgoing cross-shard edge with the edge manager.
This registers the edge for tracking and ghost node resolution. The edge is also added to pending_cross_edges for phase synchronization.
§Arguments
edge- The cross-shard edge to register
Sourcepub fn handle_shard_offline(&mut self, shard_id: ShardId) -> (usize, usize)
pub fn handle_shard_offline(&mut self, shard_id: ShardId) -> (usize, usize)
Sourcepub fn decay_cross_shard_edges(
&mut self,
rate: f64,
threshold: f64,
) -> Vec<CrossShardEdge>
pub fn decay_cross_shard_edges( &mut self, rate: f64, threshold: f64, ) -> Vec<CrossShardEdge>
Sourcepub fn cross_shard_edge_stats(&self) -> CrossShardEdgeStats
pub fn cross_shard_edge_stats(&self) -> CrossShardEdgeStats
Get statistics about cross-shard edges.
Sourcepub fn connected_shards(&self) -> Vec<ShardId>
pub fn connected_shards(&self) -> Vec<ShardId>
Get all shards this shard has edges to.
Sourcepub fn take_pending_for_resolution(
&mut self,
) -> HashMap<ShardId, Vec<CrossShardEdge>>
pub fn take_pending_for_resolution( &mut self, ) -> HashMap<ShardId, Vec<CrossShardEdge>>
Resolve pending edges by fetching ghost nodes.
Takes the pending edges from the manager and returns them for processing. After ghost nodes are fetched, the caller should insert them into the ghost cache.
§Returns
Pending edges grouped by target shard for efficient batching.
Sourcepub fn add_pending_cross_edge(&mut self, edge: CrossShardEdge)
pub fn add_pending_cross_edge(&mut self, edge: CrossShardEdge)
Add a cross-shard edge to be synchronized.
Cross-shard edges are collected during the Act phase and sent to the coordinator for resolution during the Exchange phase.
§Arguments
edge- The cross-shard edge to add
Sourcepub fn pending_cross_edges(&self) -> &[CrossShardEdge]
pub fn pending_cross_edges(&self) -> &[CrossShardEdge]
Get pending cross-shard edges.
Sourcepub fn clear_pending_cross_edges(&mut self)
pub fn clear_pending_cross_edges(&mut self)
Clear pending cross-shard edges (after they’ve been processed).
Sourcepub fn hash_ring(&self) -> &Arc<RwLock<ConsistentHashRing>>
pub fn hash_ring(&self) -> &Arc<RwLock<ConsistentHashRing>>
Get the hash ring reference.
Sourcepub fn health(&self) -> ShardHealth
pub fn health(&self) -> ShardHealth
Get shard health information.
Sourcepub fn stats(&self) -> ColonyStats
pub fn stats(&self) -> ColonyStats
Get detailed colony statistics.
Sourcepub fn current_tick(&self) -> Tick
pub fn current_tick(&self) -> Tick
Get the current tick number.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Get the total number of nodes in this shard’s graph.
Sourcepub fn document_count(&self) -> usize
pub fn document_count(&self) -> usize
Get the total number of documents in this shard.
Sourcepub fn shard_info(&self, address: String) -> ShardInfo
pub fn shard_info(&self, address: String) -> ShardInfo
Get shard info for registration with coordinator.