Add an explicit tracing span to each address book.
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
This commit is contained in:
parent
e0e17a4719
commit
2f3292759f
|
|
@ -11,6 +11,7 @@ use std::{
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use futures::channel::mpsc;
|
use futures::channel::mpsc;
|
||||||
use tokio::prelude::*;
|
use tokio::prelude::*;
|
||||||
|
use tracing::Span;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
constants,
|
constants,
|
||||||
|
|
@ -19,13 +20,23 @@ use crate::{
|
||||||
|
|
||||||
/// A database of peers, their advertised services, and information on when they
|
/// A database of peers, their advertised services, and information on when they
|
||||||
/// were last seen.
|
/// were last seen.
|
||||||
#[derive(Default, Debug)]
|
#[derive(Debug)]
|
||||||
pub struct AddressBook {
|
pub struct AddressBook {
|
||||||
by_addr: HashMap<SocketAddr, (DateTime<Utc>, PeerServices)>,
|
by_addr: HashMap<SocketAddr, (DateTime<Utc>, PeerServices)>,
|
||||||
by_time: BTreeSet<MetaAddr>,
|
by_time: BTreeSet<MetaAddr>,
|
||||||
|
span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AddressBook {
|
impl AddressBook {
|
||||||
|
/// Construct an `AddressBook` with the given [`tracing::Span`].
|
||||||
|
pub fn new(span: Span) -> AddressBook {
|
||||||
|
AddressBook {
|
||||||
|
by_addr: HashMap::default(),
|
||||||
|
by_time: BTreeSet::default(),
|
||||||
|
span,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Check consistency of the address book invariants or panic, doing work
|
/// Check consistency of the address book invariants or panic, doing work
|
||||||
/// quadratic in the address book size.
|
/// quadratic in the address book size.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -41,11 +52,13 @@ impl AddressBook {
|
||||||
|
|
||||||
/// Returns true if the address book has an entry for `addr`.
|
/// Returns true if the address book has an entry for `addr`.
|
||||||
pub fn contains_addr(&self, addr: &SocketAddr) -> bool {
|
pub fn contains_addr(&self, addr: &SocketAddr) -> bool {
|
||||||
|
let _guard = self.span.enter();
|
||||||
self.by_addr.contains_key(addr)
|
self.by_addr.contains_key(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the entry corresponding to `addr`, or `None` if it does not exist.
|
/// Returns the entry corresponding to `addr`, or `None` if it does not exist.
|
||||||
pub fn get_by_addr(&self, addr: SocketAddr) -> Option<MetaAddr> {
|
pub fn get_by_addr(&self, addr: SocketAddr) -> Option<MetaAddr> {
|
||||||
|
let _guard = self.span.enter();
|
||||||
let (last_seen, services) = self.by_addr.get(&addr).cloned()?;
|
let (last_seen, services) = self.by_addr.get(&addr).cloned()?;
|
||||||
Some(MetaAddr {
|
Some(MetaAddr {
|
||||||
addr,
|
addr,
|
||||||
|
|
@ -57,6 +70,7 @@ impl AddressBook {
|
||||||
/// Add `new` to the address book, updating the previous entry if `new` is
|
/// Add `new` to the address book, updating the previous entry if `new` is
|
||||||
/// more recent or discarding `new` if it is stale.
|
/// more recent or discarding `new` if it is stale.
|
||||||
pub fn update(&mut self, new: MetaAddr) {
|
pub fn update(&mut self, new: MetaAddr) {
|
||||||
|
let _guard = self.span.enter();
|
||||||
trace!(
|
trace!(
|
||||||
?new,
|
?new,
|
||||||
data.total = self.by_time.len(),
|
data.total = self.by_time.len(),
|
||||||
|
|
@ -102,6 +116,7 @@ impl AddressBook {
|
||||||
/// Returns true if the given [`SocketAddr`] could potentially be connected
|
/// Returns true if the given [`SocketAddr`] could potentially be connected
|
||||||
/// to a node feeding timestamps into this address book.
|
/// to a node feeding timestamps into this address book.
|
||||||
pub fn is_potentially_connected(&self, addr: &SocketAddr) -> bool {
|
pub fn is_potentially_connected(&self, addr: &SocketAddr) -> bool {
|
||||||
|
let _guard = self.span.enter();
|
||||||
match self.by_addr.get(addr) {
|
match self.by_addr.get(addr) {
|
||||||
None => return false,
|
None => return false,
|
||||||
Some((ref last_seen, _)) => last_seen > &AddressBook::cutoff_time(),
|
Some((ref last_seen, _)) => last_seen > &AddressBook::cutoff_time(),
|
||||||
|
|
@ -111,12 +126,14 @@ impl AddressBook {
|
||||||
/// Return an iterator over all peers, ordered from most recently seen to
|
/// Return an iterator over all peers, ordered from most recently seen to
|
||||||
/// least recently seen.
|
/// least recently seen.
|
||||||
pub fn peers<'a>(&'a self) -> impl Iterator<Item = MetaAddr> + 'a {
|
pub fn peers<'a>(&'a self) -> impl Iterator<Item = MetaAddr> + 'a {
|
||||||
|
let _guard = self.span.enter();
|
||||||
self.by_time.iter().rev().cloned()
|
self.by_time.iter().rev().cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return an iterator over peers known to be disconnected, ordered from most
|
/// Return an iterator over peers known to be disconnected, ordered from most
|
||||||
/// recently seen to least recently seen.
|
/// recently seen to least recently seen.
|
||||||
pub fn disconnected_peers<'a>(&'a self) -> impl Iterator<Item = MetaAddr> + 'a {
|
pub fn disconnected_peers<'a>(&'a self) -> impl Iterator<Item = MetaAddr> + 'a {
|
||||||
|
let _guard = self.span.enter();
|
||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::ops::Bound::{Excluded, Unbounded};
|
use std::ops::Bound::{Excluded, Unbounded};
|
||||||
let cutoff_meta = MetaAddr {
|
let cutoff_meta = MetaAddr {
|
||||||
|
|
|
||||||
|
|
@ -277,10 +277,11 @@ where
|
||||||
S: Service<Request, Response = Response, Error = BoxedStdError>,
|
S: Service<Request, Response = Response, Error = BoxedStdError>,
|
||||||
S::Future: Send + 'static,
|
S::Future: Send + 'static,
|
||||||
{
|
{
|
||||||
|
use tracing::Level;
|
||||||
let mut candidates = CandidateSet {
|
let mut candidates = CandidateSet {
|
||||||
disconnected: AddressBook::default(),
|
disconnected: AddressBook::new(span!(Level::TRACE, "disconnected peers")),
|
||||||
gossiped: AddressBook::default(),
|
gossiped: AddressBook::new(span!(Level::TRACE, "gossiped peers")),
|
||||||
failed: AddressBook::default(),
|
failed: AddressBook::new(span!(Level::TRACE, "failed peers")),
|
||||||
peer_set: peer_set_address_book.clone(),
|
peer_set: peer_set_address_book.clone(),
|
||||||
peer_service: peer_set_service,
|
peer_service: peer_set_service,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,13 @@ impl TimestampCollector {
|
||||||
/// transmission channel for timestamp events and for the [`AddressBook`] it
|
/// transmission channel for timestamp events and for the [`AddressBook`] it
|
||||||
/// updates.
|
/// updates.
|
||||||
pub fn spawn() -> (Arc<Mutex<AddressBook>>, mpsc::Sender<MetaAddr>) {
|
pub fn spawn() -> (Arc<Mutex<AddressBook>>, mpsc::Sender<MetaAddr>) {
|
||||||
|
use tracing::Level;
|
||||||
const TIMESTAMP_WORKER_BUFFER_SIZE: usize = 100;
|
const TIMESTAMP_WORKER_BUFFER_SIZE: usize = 100;
|
||||||
let (worker_tx, mut worker_rx) = mpsc::channel(TIMESTAMP_WORKER_BUFFER_SIZE);
|
let (worker_tx, mut worker_rx) = mpsc::channel(TIMESTAMP_WORKER_BUFFER_SIZE);
|
||||||
let address_book = Arc::new(Mutex::new(AddressBook::default()));
|
let address_book = Arc::new(Mutex::new(AddressBook::new(span!(
|
||||||
|
Level::TRACE,
|
||||||
|
"timestamp collector"
|
||||||
|
))));
|
||||||
let worker_address_book = address_book.clone();
|
let worker_address_book = address_book.clone();
|
||||||
|
|
||||||
let worker = async move {
|
let worker = async move {
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,8 @@ impl ConnectCmd {
|
||||||
addr_reqs.push(peer_set.call(Request::GetPeers));
|
addr_reqs.push(peer_set.call(Request::GetPeers));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut all_addrs = AddressBook::default();
|
use tracing::Level;
|
||||||
|
let mut all_addrs = AddressBook::new(span!(Level::TRACE, "connect stub addressbook"));
|
||||||
while let Some(Ok(Response::Peers(addrs))) = addr_reqs.next().await {
|
while let Some(Ok(Response::Peers(addrs))) = addr_reqs.next().await {
|
||||||
info!(addrs.len = addrs.len(), "got address response");
|
info!(addrs.len = addrs.len(), "got address response");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue