Skip to main content

Coordinator

Struct Coordinator 

Source
pub struct Coordinator { /* private fields */ }
Expand description

The distributed coordinator.

The coordinator manages the distributed colony by:

  • Maintaining a registry of all active shards
  • Routing documents to shards using consistent hashing
  • Synchronizing tick phases across all shards
  • Aggregating global statistics for TF-IDF computation

§Thread Safety

The coordinator is designed for concurrent access. It uses interior mutability with RwLock for the registry and hash ring, and atomics for the tick counter.

§Example

use phago_distributed::Coordinator;
use phago_distributed::types::{ShardInfo, NodeAddress};

let coordinator = Coordinator::new(3);

// Register a shard
let info = ShardInfo::new(NodeAddress::new("127.0.0.1", 8080));
let shard_id = coordinator.register_shard(info).await?;

// Route a document
let doc_id = DocumentId::new();
let target_shard = coordinator.route_document(&doc_id).await;

Implementations§

Source§

impl Coordinator

Source

pub fn new(num_shards: u32) -> Self

Create a new coordinator with the specified number of shards.

The coordinator will initialize the hash ring with the given number of shards, but actual shards must be registered before they can receive documents.

Source

pub fn with_config(config: DistributedConfig) -> Self

Create a coordinator with custom configuration.

Source

pub async fn register_shard( &self, info: ShardInfo, ) -> DistributedResult<ShardId>

Register a shard with the coordinator.

The shard will be assigned a unique ID, added to the registry, and included in the hash ring for document routing.

§Arguments
  • info - Information about the shard to register
§Returns

The assigned shard ID.

Source

pub async fn deregister_shard(&self, shard_id: ShardId) -> DistributedResult<()>

Deregister a shard from the coordinator.

The shard will be removed from the registry and hash ring. Documents previously assigned to this shard will be redistributed.

Source

pub async fn route_document(&self, doc_id: &DocumentId) -> ShardId

Route a document to the appropriate shard.

Uses consistent hashing to determine which shard should store the document. The same document will always route to the same shard (unless the cluster topology changes).

Source

pub async fn get_replica_shards(&self, doc_id: &DocumentId) -> Vec<ShardId>

Get replica shards for a document.

Returns the primary shard plus additional replica shards based on the configured replication factor.

Source

pub async fn phase_complete( &self, shard_id: ShardId, phase: TickPhase, tick: Tick, ) -> DistributedResult<()>

Signal that a shard has completed a phase.

This is called by each shard when it finishes a phase of the tick. The coordinator tracks progress and releases the barrier when all shards have completed.

Source

pub async fn wait_for_phase( &self, phase: TickPhase, tick: Tick, ) -> DistributedResult<()>

Wait for all shards to complete a phase.

Blocks until all registered shards have signaled completion of the specified phase.

Source

pub async fn advance_tick(&self) -> Tick

Advance to the next tick.

This should be called after all phases of the current tick are complete. Returns the new tick number.

Source

pub fn current_tick(&self) -> Tick

Get the current tick number.

Source

pub fn aggregate_global_df( &self, local_dfs: Vec<HashMap<String, u64>>, ) -> HashMap<String, u64>

Aggregate global document frequencies from all shards.

This is used for computing global TF-IDF scores. Each shard provides its local document frequencies, and the coordinator sums them to produce global counts.

§Arguments
  • local_dfs - Vector of term->count maps from each shard
§Returns

A map of term->global_count across all shards.

Source

pub async fn all_shards(&self) -> Vec<ShardInfo>

Get all registered shards.

Source

pub async fn online_shards(&self) -> Vec<ShardInfo>

Get all online shards.

Source

pub async fn get_shard(&self, shard_id: ShardId) -> Option<ShardInfo>

Get a specific shard’s information.

Source

pub async fn shard_heartbeat(&self, shard_id: ShardId)

Update heartbeat for a shard.

Called periodically by shards to indicate they are still alive.

Source

pub async fn check_shard_health(&self) -> Vec<ShardId>

Check for dead shards and mark them offline.

Returns the IDs of shards that were marked offline.

Source

pub async fn update_shard_metrics( &self, shard_id: ShardId, document_count: usize, memory_bytes: u64, )

Update shard metrics.

Source

pub async fn total_documents(&self) -> u64

Get the total number of documents across all shards.

Source

pub async fn cluster_stats(&self) -> ClusterStats

Get cluster statistics.

Source

pub fn config(&self) -> &DistributedConfig

Get the configuration.

Source

pub async fn shard_count(&self) -> u32

Get the shard count from the hash ring.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more