pub struct StorageShardBalancer { /* private fields */ }Expand description
Production-grade consistent-hashing shard balancer.
Virtual nodes for each shard are distributed across a BTreeMap<u64, String>
ring. Content is assigned to shards by walking the ring clockwise from the
FNV-1a hash of the CID, selecting the first replication_factor distinct
healthy shards.
Implementations§
Source§impl StorageShardBalancer
impl StorageShardBalancer
Sourcepub fn new(config: BalancerConfig) -> Self
pub fn new(config: BalancerConfig) -> Self
Create a new balancer with the provided configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a balancer with default configuration.
Sourcepub fn add_shard(&mut self, shard: ShardNode) -> Result<(), BalancerError>
pub fn add_shard(&mut self, shard: ShardNode) -> Result<(), BalancerError>
Register a shard and add its virtual nodes to the ring.
Sourcepub fn remove_shard(
&mut self,
shard_id: &str,
) -> Result<Vec<RebalanceOp>, BalancerError>
pub fn remove_shard( &mut self, shard_id: &str, ) -> Result<Vec<RebalanceOp>, BalancerError>
Remove a shard from the ring and generate MoveContent ops for all
affected assignments.
Sourcepub fn assign(&mut self, cid: &str) -> Result<ShardAssignment, BalancerError>
pub fn assign(&mut self, cid: &str) -> Result<ShardAssignment, BalancerError>
Assign a CID to shards using consistent hashing.
Returns an error when fewer than replication_factor healthy shards
are available.
Sourcepub fn lookup(&self, cid: &str) -> Result<&ShardAssignment, BalancerError>
pub fn lookup(&self, cid: &str) -> Result<&ShardAssignment, BalancerError>
Returns a reference to an existing assignment.
Sourcepub fn record_usage(
&mut self,
shard_id: &str,
delta_bytes: i64,
) -> Result<(), BalancerError>
pub fn record_usage( &mut self, shard_id: &str, delta_bytes: i64, ) -> Result<(), BalancerError>
Update used bytes for a shard (delta can be negative for deletions).
Sourcepub fn set_shard_health(
&mut self,
shard_id: &str,
healthy: bool,
) -> Result<(), BalancerError>
pub fn set_shard_health( &mut self, shard_id: &str, healthy: bool, ) -> Result<(), BalancerError>
Mark a shard healthy or unhealthy.
Sourcepub fn rebalance(&mut self) -> Result<Vec<RebalanceOp>, BalancerError>
pub fn rebalance(&mut self) -> Result<Vec<RebalanceOp>, BalancerError>
Compute rebalance operations if the imbalance ratio exceeds the
configured threshold. Operations are also stored in pending_ops.
Sourcepub fn stats(&self) -> SsbBalancerStats
pub fn stats(&self) -> SsbBalancerStats
Return a snapshot of current balancer statistics.
Sourcepub fn ring_positions(&self) -> Vec<(u64, String)>
pub fn ring_positions(&self) -> Vec<(u64, String)>
Return all virtual node ring positions, sorted ascending.
Sourcepub fn config(&self) -> &BalancerConfig
pub fn config(&self) -> &BalancerConfig
Return the current config.
Sourcepub fn shards(&self) -> impl Iterator<Item = &ShardNode>
pub fn shards(&self) -> impl Iterator<Item = &ShardNode>
Return iterator over all registered shards.
Sourcepub fn assignment_count(&self) -> usize
pub fn assignment_count(&self) -> usize
Return the number of CIDs currently assigned.
Auto Trait Implementations§
impl Freeze for StorageShardBalancer
impl RefUnwindSafe for StorageShardBalancer
impl Send for StorageShardBalancer
impl Sync for StorageShardBalancer
impl Unpin for StorageShardBalancer
impl UnsafeUnpin for StorageShardBalancer
impl UnwindSafe for StorageShardBalancer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more