Skip to content

Commit

Permalink
feat(Gossip KV): Make member random suffix length configurable. (#1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitkulshreshtha authored Nov 8, 2024
1 parent 0bd3a2d commit 1d35639
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion datastores/gossip_kv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "Apache-2.0"
publish = false

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
clap = { version = "4.5.4", features = ["derive", "env"] }
config = "0.14.0"
governor = "0.7.0"
hostname = "0.4.0"
Expand Down
5 changes: 4 additions & 1 deletion datastores/gossip_kv/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct Opts {
/// The duration (in seconds) between gossip rounds.
#[clap(short, long, default_value = "5", value_parser = clap_duration_from_secs)]
gossip_frequency: Duration,

#[clap(env = "GOSSIP_MEMBER_SUFFIX_LEN", default_value = "4")]
member_suffix_len: usize,
}

/// Parse duration from float string for clap args.
Expand Down Expand Up @@ -133,7 +136,7 @@ async fn main() {
let gossip_protocol_address =
SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), opts.gossip_port);

let member_data = MemberDataBuilder::new(member_name().clone())
let member_data = MemberDataBuilder::new(member_name(opts.member_suffix_len).clone())
.add_protocol(Protocol::new("gossip".into(), gossip_protocol_address))
.add_protocol(Protocol::new("client".into(), client_protocol_address))
.build();
Expand Down
49 changes: 23 additions & 26 deletions datastores/gossip_kv/server/membership.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
use std::sync::OnceLock;

use gossip_kv::membership::MemberId;
// use rand::distributions::Distribution;
// use rand::{Rng};
use rand::distributions::Distribution;
use rand::{thread_rng, Rng};

// /// This is a simple distribution that generates a random lower-case alphanumeric
// struct LowercaseAlphanumeric;
//
// impl Distribution<char> for LowercaseAlphanumeric {
// fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
// let choices = b"abcdefghijklmnopqrstuvwxyz0123456789";
// choices[rng.gen_range(0..choices.len())] as char
// }
// }
/// This is a simple distribution that generates a random lower-case alphanumeric
struct LowercaseAlphanumeric;

impl Distribution<char> for LowercaseAlphanumeric {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
let choices = b"abcdefghijklmnopqrstuvwxyz0123456789";
choices[rng.gen_range(0..choices.len())] as char
}
}

/// Gets a name for the current process.
pub fn member_name() -> &'static MemberId {
pub fn member_name(random_suffix_len: usize) -> &'static MemberId {
static MEMBER_NAME: OnceLock<MemberId> = OnceLock::new();
MEMBER_NAME.get_or_init(|| {
// Generate a lower-case alphanumeric suffix of length 4

// TODO: Random suffixes are good, but make benchmarking a pain. For now, we'll just use
// the hostname as-is.

// let suffix: String = thread_rng()
// .sample_iter(&LowercaseAlphanumeric)
// .take(4)
// .map(char::from)
// .collect();

// Retrieve hostname
hostname::get().unwrap().to_str().unwrap().to_string()
let hostname = hostname::get().unwrap().to_str().unwrap().to_string();

// format!("{}-{}", hostname, suffix)
if random_suffix_len > 0 {
let suffix: String = thread_rng()
.sample_iter(&LowercaseAlphanumeric)
.take(4)
.map(char::from)
.collect();
format!("{}-{}", hostname, suffix)
} else {
hostname
}
})
}

0 comments on commit 1d35639

Please sign in to comment.