pub struct ShardBalancer { /* private fields */ }Expand description
Tracks per-shard load and makes placement / rebalancing decisions.
All load counters use AtomicUsize so concurrent updates from multiple
async tasks require no mutex.
Implementations§
Source§impl ShardBalancer
impl ShardBalancer
Sourcepub fn new(config: ShardConfig) -> Self
pub fn new(config: ShardConfig) -> Self
Sourcepub fn assign_vector(&self, vector_id: u64) -> usize
pub fn assign_vector(&self, vector_id: u64) -> usize
Return the shard that should store the vector with the given id.
Uses Knuth multiplicative hashing for a uniform distribution.
Sourcepub fn least_loaded_shard(&self) -> usize
pub fn least_loaded_shard(&self) -> usize
Return the shard index with the lowest current load.
In the case of a tie the shard with the smaller index wins, giving stable, deterministic behaviour in tests.
Sourcepub fn increment_shard_load(&self, shard_id: usize)
pub fn increment_shard_load(&self, shard_id: usize)
Atomically increment the vector count for the given shard.
Sourcepub fn decrement_shard_load(&self, shard_id: usize)
pub fn decrement_shard_load(&self, shard_id: usize)
Atomically decrement the vector count for the given shard.
Saturates at zero to avoid underflow.
Sourcepub fn shard_loads_snapshot(&self) -> Vec<usize>
pub fn shard_loads_snapshot(&self) -> Vec<usize>
Return a point-in-time snapshot of all shard load counts.
Sourcepub fn rebalance_needed(&self) -> bool
pub fn rebalance_needed(&self) -> bool
Return true when the ratio of the most-loaded shard to the
least-loaded shard exceeds 2.0 (ignoring empty shards with zero load).
Sourcepub fn hotspot_shards(&self) -> Vec<usize>
pub fn hotspot_shards(&self) -> Vec<usize>
Return the indices of shards whose load exceeds the average load.
Sourcepub fn config(&self) -> &ShardConfig
pub fn config(&self) -> &ShardConfig
Expose configuration for inspection.
Auto Trait Implementations§
impl Freeze for ShardBalancer
impl RefUnwindSafe for ShardBalancer
impl Send for ShardBalancer
impl Sync for ShardBalancer
impl Unpin for ShardBalancer
impl UnsafeUnpin for ShardBalancer
impl UnwindSafe for ShardBalancer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.