Skip to content

Commit

Permalink
fix: resolve DefaultLeaderElector::elect_leader panicking for party_w…
Browse files Browse the repository at this point in the history
…eights summing to 1
  • Loading branch information
NikitaMasych committed Oct 17, 2024
1 parent 4adb7dd commit 9e4a65f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl DefaultLeaderElector {

/// Hashes the seed to a value within a specified range.
///
/// This method uses the computed seed to generate a value within the range [0, range).
/// This method uses the computed seed to generate a value within the range [0, range].
/// The algorithm ensures uniform distribution of the resulting value, which is crucial
/// for fair leader election.
///
Expand All @@ -83,7 +83,7 @@ impl DefaultLeaderElector {
fn hash_to_range(seed: u64, range: u64) -> u64 {
// Determine the number of bits required to represent the range
let mut k = 64;
while 1u64 << (k - 1) >= range {
while 1u64 << (k - 1) > range {
k -= 1;
}

Expand Down Expand Up @@ -129,7 +129,7 @@ impl<V: Value, VS: ValueSelector<V>> LeaderElector<V, VS> for DefaultLeaderElect
return Err(DefaultLeaderElectorError::ZeroWeightSum.into());
}

// Generate a random number in the range [0, total_weight)
// Generate a random number in the range [0, total_weight]
let random_value = DefaultLeaderElector::hash_to_range(seed, total_weight);

// Use binary search to find the corresponding participant based on the cumulative weight
Expand Down Expand Up @@ -161,6 +161,17 @@ mod tests {
use std::thread;
use std::time::Duration;

#[test]
fn test_default_leader_elector_weight_one() {
let mut party = MockParty::default();
party.cfg.party_weights = vec![0, 1, 0, 0];

let elector = DefaultLeaderElector::new();

let leader = elector.elect_leader(&party).unwrap();
println!("leader: {}", leader);
}

#[test]
fn test_default_leader_elector_determinism() {
let party = MockParty::default();
Expand Down

0 comments on commit 9e4a65f

Please sign in to comment.