Skip to content

Commit

Permalink
Merge pull request #1108 from muzarski/remove-needless-clone-in-host-…
Browse files Browse the repository at this point in the history
…resolution

node: tiny optimizations and code refactors
  • Loading branch information
wprzytula authored Oct 26, 2024
2 parents 0e0b754 + 81d9576 commit 7e73e25
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
8 changes: 2 additions & 6 deletions scylla/src/transport/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ impl PoolRefiller {
// `last_error` must not be `None` if there is a possibility of the pool
// being empty.
fn update_shared_conns(&mut self, last_error: Option<ConnectionError>) {
let new_conns = if !self.has_connections() {
let new_conns = if self.is_empty() {
Arc::new(MaybePoolConnections::Broken(last_error.unwrap()))
} else {
let new_conns = if let Some(sharder) = self.sharder.as_ref() {
Expand Down Expand Up @@ -1046,7 +1046,7 @@ impl PoolRefiller {
self.conns[shard_id].len(),
self.active_connection_count(),
);
if !self.has_connections() {
if self.is_empty() {
let _ = self.pool_empty_notifier.send(());
}
self.update_shared_conns(Some(last_error));
Expand Down Expand Up @@ -1152,10 +1152,6 @@ impl PoolRefiller {
);
}

fn has_connections(&self) -> bool {
self.conns.iter().any(|v| !v.is_empty())
}

fn active_connection_count(&self) -> usize {
self.conns.iter().map(Vec::len).sum::<usize>()
}
Expand Down
57 changes: 28 additions & 29 deletions scylla/src/transport/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use itertools::Itertools;
use tokio::net::lookup_host;
use tracing::warn;
use uuid::Uuid;
Expand Down Expand Up @@ -270,27 +271,23 @@ pub(crate) struct ResolvedContactPoint {
// The resolution may return multiple IPs and the function returns one of them.
// It prefers to return IPv4s first, and only if there are none, IPv6s.
pub(crate) async fn resolve_hostname(hostname: &str) -> Result<SocketAddr, io::Error> {
let mut ret = None;
let addrs: Vec<SocketAddr> = match lookup_host(hostname).await {
Ok(addrs) => addrs.collect(),
let addrs = match lookup_host(hostname).await {
Ok(addrs) => itertools::Either::Left(addrs),
// Use a default port in case of error, but propagate the original error on failure
Err(e) => lookup_host((hostname, 9042)).await.or(Err(e))?.collect(),
};
for a in addrs {
match a {
SocketAddr::V4(_) => return Ok(a),
_ => {
ret = Some(a);
}
Err(e) => {
let addrs = lookup_host((hostname, 9042)).await.or(Err(e))?;
itertools::Either::Right(addrs)
}
}
};

ret.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
format!("Empty address list returned by DNS for {}", hostname),
)
})
addrs
.find_or_last(|addr| matches!(addr, SocketAddr::V4(_)))
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
format!("Empty address list returned by DNS for {}", hostname),
)
})
}

/// Transforms the given [`InternalKnownNode`]s into [`ContactPoint`]s.
Expand Down Expand Up @@ -323,18 +320,20 @@ pub(crate) async fn resolve_contact_points(
}) => to_resolve.push((hostname, Some(datacenter.clone()))),
};
}
let resolve_futures = to_resolve.iter().map(|(hostname, datacenter)| async move {
match resolve_hostname(hostname).await {
Ok(address) => Some(ResolvedContactPoint {
address,
datacenter: datacenter.clone(),
}),
Err(e) => {
warn!("Hostname resolution failed for {}: {}", hostname, &e);
None
let resolve_futures = to_resolve
.into_iter()
.map(|(hostname, datacenter)| async move {
match resolve_hostname(hostname).await {
Ok(address) => Some(ResolvedContactPoint {
address,
datacenter,
}),
Err(e) => {
warn!("Hostname resolution failed for {}: {}", hostname, &e);
None
}
}
}
});
});
let resolved: Vec<_> = futures::future::join_all(resolve_futures).await;
initial_peers.extend(resolved.into_iter().flatten());

Expand Down

0 comments on commit 7e73e25

Please sign in to comment.