Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/message_bus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ readme = "../../../README.md"
[dependencies]
iggy_common = { workspace = true }
rand = { workspace = true }

[lints.clippy]
enum_glob_use = "deny"
pedantic = "deny"
nursery = "deny"
20 changes: 9 additions & 11 deletions core/message_bus/src/cache/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ pub trait ShardedState {
/// Least-loaded allocation strategy for connections
#[derive(Debug)]
pub struct LeastLoadedStrategy {
total_shards: usize,
total_shards: u16,
connections_per_shard: RefCell<Vec<(u16, usize)>>,
replica_to_shards: RefCell<HashMap<u8, HashSet<u16>>>,
rng_seed: u64,
}

impl LeastLoadedStrategy {
pub fn new(total_shards: usize, seed: u64) -> Self {
pub fn new(total_shards: u16, seed: u64) -> Self {
Self {
total_shards,
connections_per_shard: RefCell::new((0..total_shards).map(|s| (s as u16, 0)).collect()),
connections_per_shard: RefCell::new((0..total_shards).map(|s| (s, 0)).collect()),
replica_to_shards: RefCell::new(HashMap::new()),
rng_seed: seed,
}
Expand All @@ -58,11 +58,11 @@ impl LeastLoadedStrategy {
replica: u8,
mut conn_shards: Vec<u16>,
) {
for shard in &conn_shards {
for &shard in &conn_shards {
mappings.push(ShardAssignment {
replica,
shard: *shard,
conn_shard: *shard,
shard,
conn_shard: shard,
});
}

Expand All @@ -71,7 +71,6 @@ impl LeastLoadedStrategy {

let mut j = 0;
for shard in 0..self.total_shards {
let shard = shard as u16;
if conn_shards.contains(&shard) {
continue;
}
Expand Down Expand Up @@ -123,7 +122,7 @@ impl AllocationStrategy<ConnectionCache> for LeastLoadedStrategy {

let mut connections = Vec::new();
let mut mappings = Vec::new();
let connections_needed = self.total_shards.min(MAX_CONNECTIONS_PER_REPLICA);
let connections_needed = usize::from(self.total_shards).min(MAX_CONNECTIONS_PER_REPLICA);

let mut rng = StdRng::seed_from_u64(self.rng_seed);
self.connections_per_shard.borrow_mut().shuffle(&mut rng);
Expand Down Expand Up @@ -182,7 +181,6 @@ impl AllocationStrategy<ConnectionCache> for LeastLoadedStrategy {
}

for shard in 0..self.total_shards {
let shard = shard as u16;
mappings.push(ConnectionAssignment { replica, shard });
}

Expand All @@ -209,7 +207,7 @@ where
SS: ShardedState,
A: AllocationStrategy<SS>,
{
pub fn new(strategy: A) -> Self {
pub const fn new(strategy: A) -> Self {
Self {
strategy,
_ss: std::marker::PhantomData,
Expand Down Expand Up @@ -271,7 +269,7 @@ pub struct ConnectionCache {

impl ConnectionCache {
pub fn get_connection(&self, replica: u8) -> Option<Rc<TcpSender>> {
self.connections.get(&replica).and_then(|opt| opt.clone())
self.connections.get(&replica).and_then(Clone::clone)
}

pub fn get_mapped_shard(&self, replica: u8) -> Option<u16> {
Expand Down
2 changes: 1 addition & 1 deletion core/message_bus/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ where
fn deallocate(&self, entry: SS::Entry) -> Option<SS::Delta>;
}

pub(crate) mod connection;
pub mod connection;
7 changes: 5 additions & 2 deletions core/message_bus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub struct IggyMessageBus {
}

impl IggyMessageBus {
pub fn new(total_shards: usize, shard_id: u16, seed: u64) -> Self {
#[must_use]
pub fn new(total_shards: u16, shard_id: u16, seed: u64) -> Self {
Self {
clients: HashMap::new(),
replicas: ShardedConnections {
Expand All @@ -81,6 +82,7 @@ impl IggyMessageBus {
}
}

#[allow(clippy::future_not_send)] // Single-threaded runtime (compio), Rc/RefCell by design
impl MessageBus for IggyMessageBus {
type Client = u128;
type Replica = u8;
Expand Down Expand Up @@ -112,6 +114,7 @@ impl MessageBus for IggyMessageBus {
client_id: Self::Client,
_message: Self::Data,
) -> Result<(), IggyError> {
#[allow(clippy::cast_possible_truncation)] // IggyError::ClientNotFound takes u32
let _sender = self
.clients
.get(&client_id)
Expand All @@ -127,7 +130,7 @@ impl MessageBus for IggyMessageBus {
// TODO: Handle lazily creating the connection.
let _connection = self
.get_replica_connection(replica)
.ok_or(IggyError::ResourceNotFound(format!("Replica {}", replica)))?;
.ok_or(IggyError::ResourceNotFound(format!("Replica {replica}")))?;
Ok(())
}
}
Loading