Skip to content

Commit

Permalink
chore: replace bootstrap with query specific kbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and joshuef committed Nov 23, 2023
1 parent e23114a commit 27ec468
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
39 changes: 15 additions & 24 deletions sn_networking/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const BOOTSTRAP_CONNECTED_PEERS_STEP: u32 = 5;
/// process. This is to make sure we don't flood the network with `FindNode` msgs.
const LAST_PEER_ADDED_TIME_LIMIT: Duration = Duration::from_secs(180);

/// A minumn interval to prevent bootstrap got triggered too often
const LAST_BOOTSTRAP_TRIGGERED_TIME_LIMIT: Duration = Duration::from_secs(30);

/// The bootstrap interval to use if we haven't added any new peers in a while.
const NO_PEER_ADDED_SLOWDOWN_INTERVAL: Duration = Duration::from_secs(300);

Expand All @@ -34,7 +37,7 @@ impl SwarmDriver {
.should_we_bootstrap(self.connected_peers as u32, current_bootstrap_interval)
.await;
if should_bootstrap {
self.initiate_bootstrap();
self.trigger_network_discovery();
}
if let Some(new_interval) = &new_interval {
debug!(
Expand All @@ -45,19 +48,6 @@ impl SwarmDriver {
new_interval
}

/// Helper to initiate the Kademlia bootstrap process.
pub(crate) fn initiate_bootstrap(&mut self) {
match self.swarm.behaviour_mut().kademlia.bootstrap() {
Ok(query_id) => {
debug!("Initiated kad bootstrap process with query id {query_id:?}");
self.bootstrap.initiated();
}
Err(err) => {
error!("Failed to initiate kad bootstrap with error: {err:?}");
}
};
}

pub(crate) fn trigger_network_discovery(&mut self) {
// The query is just to trigger the network discovery,
// hence no need to wait for a result.
Expand All @@ -68,30 +58,31 @@ impl SwarmDriver {
.kademlia
.get_closest_peers(addr.as_bytes());
}
self.bootstrap.initiated();
}
}

/// Tracks and helps with the continuous kad::bootstrapping process
pub(crate) struct ContinuousBootstrap {
is_ongoing: bool,
initial_bootstrap_done: bool,
stop_bootstrapping: bool,
last_peer_added_instant: Instant,
last_bootstrap_triggered: Option<Instant>,
}

impl ContinuousBootstrap {
pub(crate) fn new() -> Self {
Self {
is_ongoing: false,
initial_bootstrap_done: false,
last_peer_added_instant: Instant::now(),
stop_bootstrapping: false,
last_peer_added_instant: Instant::now(),
last_bootstrap_triggered: None,
}
}

/// The Kademlia Bootstrap request has been sent successfully.
pub(crate) fn initiated(&mut self) {
self.is_ongoing = true;
self.last_bootstrap_triggered = Some(Instant::now());
}

/// Notify about a newly added peer to the RT. This will help with slowing down the bootstrap process.
Expand All @@ -108,11 +99,6 @@ impl ContinuousBootstrap {
}
}

/// A previous Kademlia Bootstrap process has been completed. Now a new bootstrap process can start.
pub(crate) fn completed(&mut self) {
self.is_ongoing = false;
}

/// Set the flag to stop any further re-bootstrapping.
pub(crate) fn stop_bootstrapping(&mut self) {
self.stop_bootstrapping = true;
Expand All @@ -134,7 +120,12 @@ impl ContinuousBootstrap {
}

// kad bootstrap process needs at least one peer in the RT be carried out.
let should_bootstrap = !self.is_ongoing && peers_in_rt >= 1;
let is_ongoing = if let Some(last_bootstrap_triggered) = self.last_bootstrap_triggered {
last_bootstrap_triggered.elapsed() < LAST_BOOTSTRAP_TRIGGERED_TIME_LIMIT
} else {
false
};
let should_bootstrap = !is_ongoing && peers_in_rt >= 1;

// if it has been a while (LAST_PEER_ADDED_TIME_LIMIT) since we have added a new peer to our RT, then, slowdown
// the bootstrapping process.
Expand Down
4 changes: 0 additions & 4 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,6 @@ impl SwarmDriver {
if let Some(new_interval) = self.run_bootstrap_continuously(bootstrap_interval.period()).await {
bootstrap_interval = new_interval;
}
// execute extra `kbucket targeted query` to make RT filled up more accurately
let start = std::time::Instant::now();
self.query_specific_kbucket();
trace!("Query specific kbuckets handled in {:?}", start.elapsed());
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions sn_networking/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ impl SwarmDriver {
.map_err(|_| Error::InternalMsgChannelDropped)?;
}
}
// Shall no longer receive this event
kad::Event::OutboundQueryProgressed {
id,
result: QueryResult::Bootstrap(bootstrap_result),
Expand All @@ -867,10 +868,6 @@ impl SwarmDriver {
// here BootstrapOk::num_remaining refers to the remaining random peer IDs to query, one per
// bucket that still needs refreshing.
trace!("Kademlia Bootstrap with {id:?} progressed with {bootstrap_result:?} and step {step:?}");
if step.last {
// inform the bootstrap process about the completion.
self.bootstrap.completed();
}
}
kad::Event::RoutingUpdated {
peer,
Expand All @@ -887,7 +884,7 @@ impl SwarmDriver {

if self.bootstrap.notify_new_peer() {
info!("Performing the first bootstrap");
self.initiate_bootstrap();
self.trigger_network_discovery();
}
self.send_event(NetworkEvent::PeerAdded(peer, self.connected_peers));
}
Expand Down

0 comments on commit 27ec468

Please sign in to comment.