Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change maintainer to ping nodes while removal #141

Merged
merged 1 commit into from
Apr 9, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Change 'need_bootstrappers' to have dinamically threshold [#135]
- Change `BinaryID::from_nonce` to return result [#136]
- Change maintainer to ping nodes while removal [#138]

## [0.6.0] - 2023-11-01

Expand Down Expand Up @@ -137,6 +138,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#123]: https://github.com/dusk-network/kadcast/issues/123
[#135]: https://github.com/dusk-network/kadcast/issues/135
[#136]: https://github.com/dusk-network/kadcast/issues/136
[#138]: https://github.com/dusk-network/kadcast/issues/138

<!-- Releases -->

Expand Down
6 changes: 5 additions & 1 deletion src/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<V> Tree<V> {
})
}

//pick at most Alpha nodes for each idle bucket
// pick at most Alpha nodes for each idle bucket
pub(crate) fn idle_buckets(
&self,
) -> impl Iterator<Item = (BucketHeight, impl Iterator<Item = &Node<V>>)>
Expand Down Expand Up @@ -134,6 +134,10 @@ impl<V> Tree<V> {
})
}

pub(crate) fn idle_nodes(&self) -> impl Iterator<Item = &Node<V>> {
self.buckets.iter().flat_map(|(_, b)| b.idle_nodes())
}

pub(crate) fn remove_idle_nodes(&mut self) {
self.buckets
.iter_mut()
Expand Down
6 changes: 6 additions & 0 deletions src/kbucket/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ impl<V> Bucket<V> {
self.nodes.iter().filter(move |&n| n.is_alive(ttl))
}

/// Get idle nodes from the bucket.
pub(crate) fn idle_nodes(&self) -> impl Iterator<Item = &Node<V>> {
let ttl = self.bucket_config.node_ttl;
self.nodes.iter().filter(move |n| n.is_alive(ttl))
}

/// Checks if the bucket contains a node with the given peer key.
pub(crate) fn has_node(&self, peer: &BinaryKey) -> bool {
self.nodes.iter().any(|n| n.id().as_binary() == peer)
Expand Down
22 changes: 18 additions & 4 deletions src/maintainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl TableMaintainer {

/// This is the main function of this utility class. It's responsible to:
/// 1. Contact bootstrappers (if needed)
/// 2. Ping idle buckets
/// 2. Find new node for idle buckets
/// 3. Remove idles nodes from buckets
async fn monitor_buckets(&self, idle_time: Duration, min_peers: usize) {
info!("TableMaintainer::monitor_buckets started");
Expand All @@ -102,16 +102,30 @@ impl TableMaintainer {
tokio::time::sleep(idle_time).await;

info!("TableMaintainer::monitor_buckets woke up");
self.ping_idle_buckets().await;
self.find_new_nodes().await;

info!("TableMaintainer::monitor_buckets removing idle nodes");
self.ktable.write().await.remove_idle_nodes();
self.ping_and_remove_idles().await;
}
}

/// Search for idle buckets (no message received) and try to contact some of
/// the belonging nodes
async fn ping_idle_buckets(&self) {
async fn ping_and_remove_idles(&self) {
let idles = self
.ktable
.read()
.await
.idle_nodes()
.map(|n| *n.value().address())
.collect();
self.send((Message::Ping(self.header), idles)).await;
herr-seppia marked this conversation as resolved.
Show resolved Hide resolved
self.ktable.write().await.remove_idle_nodes();
}

/// Search for idle buckets (no message received) and try to contact some of
/// the belonging nodes
async fn find_new_nodes(&self) {
let table_lock_read = self.ktable.read().await;

let find_node_messages = table_lock_read
Expand Down