Skip to content

Commit

Permalink
protocols/kad: Add try_finish debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Jun 16, 2020
1 parent e5542e8 commit d87141c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
26 changes: 23 additions & 3 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,17 @@ where
.filter_map(|PeerRecord{ peer, .. }| peer.as_ref())
.cloned()
.collect::<Vec<_>>();
query.try_finish(peers.iter())
let is_finished = query.try_finish(peers.iter());

debug!(
"GetRecord query with id {:?} reached the quorum {} finished \
with recent response from peer {}, counting {} successful \
responses total.",
user_data,
if is_finished { "and is" } else { "but is not yet" },
source,
peers.len(),
);
}
} else if quorum.get() == 1 {
// It is a "standard" Kademlia query, for which the
Expand Down Expand Up @@ -1556,10 +1566,20 @@ where
if let QueryInfo::PutRecord {
phase: PutRecordPhase::PutRecord { success, .. }, quorum, ..
} = &mut query.inner.info {
success.push(source);
success.push(source.clone());
if success.len() >= quorum.get() {
let peers = success.clone();
query.try_finish(peers.iter())
let is_finished = query.try_finish(peers.iter());

debug!(
"PutRecord query with id {:?} reached the quorum {} finished \
with recent response from peer {}, counting {} successful \
responses total.",
user_data,
if is_finished { "and is" } else { "but is not yet" },
source,
peers.len(),
);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions protocols/kad/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,17 @@ impl<TInner> Query<TInner> {
/// A finished query immediately stops yielding new peers to contact and
/// will be reported by [`QueryPool::poll`] via
/// [`QueryPoolState::Finished`].
pub fn try_finish<'a, I>(&mut self, peers: I)
pub fn try_finish<'a, I>(&mut self, peers: I) -> bool
where
I: IntoIterator<Item = &'a PeerId>
{
match &mut self.peer_iter {
QueryPeerIter::Closest(iter) => iter.finish(),
QueryPeerIter::ClosestDisjoint(iter) => iter.finish_paths(peers),
QueryPeerIter::ClosestDisjoint(iter) => return iter.finish_paths(peers),
QueryPeerIter::Fixed(iter) => iter.finish()
}
};

true
}

/// Finishes the query prematurely.
Expand Down
4 changes: 3 additions & 1 deletion protocols/kad/src/query/peers/closest/disjoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl ClosestDisjointPeersIter {
/// Finishes all paths containing one of the given peers.
///
/// See [`crate::query::Query::try_finish`] for details.
pub fn finish_paths<'a, I>(&mut self, peers: I)
pub fn finish_paths<'a, I>(&mut self, peers: I) -> bool
where
I: IntoIterator<Item = &'a PeerId>
{
Expand All @@ -287,6 +287,8 @@ impl ClosestDisjointPeersIter {
self.iters[*initiated_by].finish();
}
}

self.is_finished()
}

/// Immediately transitions the iterator to [`PeersIterState::Finished`].
Expand Down

0 comments on commit d87141c

Please sign in to comment.