rqbit/crates/librqbit/src/torrent_state.rs

444 lines
14 KiB
Rust
Raw Normal View History

use std::{
collections::{HashMap, HashSet},
fs::File,
net::SocketAddr,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
time::{Duration, Instant},
};
use futures::{stream::FuturesUnordered, StreamExt};
2021-06-30 18:42:16 +01:00
use log::{debug, trace, warn};
use parking_lot::{Mutex, RwLock};
2021-06-29 00:17:10 +01:00
use tokio::sync::mpsc::{channel, Sender};
use crate::{
chunk_tracker::ChunkTracker,
file_ops::FileOps,
2021-06-30 00:00:44 +01:00
lengths::{Lengths, ValidPieceIndex},
peer_binary_protocol::{Handshake, Message},
2021-06-29 00:17:10 +01:00
peer_connection::{PeerConnection, WriterRequest},
2021-06-28 14:23:28 +01:00
peer_state::{LivePeerState, PeerState},
2021-07-01 19:17:44 +01:00
spawn_utils::{spawn, BlockingSpawner},
torrent_metainfo::TorrentMetaV1Owned,
2021-07-01 19:17:44 +01:00
type_aliases::{PeerHandle, Sha1, BF},
};
2021-06-30 00:02:25 +01:00
pub struct InflightPiece {
pub peer: PeerHandle,
pub started: Instant,
}
#[derive(Default)]
pub struct PeerStates {
states: HashMap<PeerHandle, PeerState>,
2021-06-28 22:48:14 +01:00
seen: HashSet<SocketAddr>,
2021-06-30 00:02:25 +01:00
inflight_pieces: HashMap<ValidPieceIndex, InflightPiece>,
tx: HashMap<PeerHandle, Arc<tokio::sync::mpsc::Sender<WriterRequest>>>,
}
#[derive(Debug, Default)]
pub struct AggregatePeerStats {
pub connecting: usize,
pub live: usize,
}
impl PeerStates {
pub fn stats(&self) -> AggregatePeerStats {
self.states
.values()
.fold(AggregatePeerStats::default(), |mut s, p| {
match p {
PeerState::Connecting(_) => s.connecting += 1,
PeerState::Live(_) => s.live += 1,
};
s
})
}
pub fn add_if_not_seen(
&mut self,
addr: SocketAddr,
tx: tokio::sync::mpsc::Sender<WriterRequest>,
) -> Option<PeerHandle> {
2021-06-28 22:48:14 +01:00
if self.seen.contains(&addr) {
return None;
}
let handle = self.add(addr, tx)?;
2021-06-28 22:48:14 +01:00
self.seen.insert(addr);
Some(handle)
}
2021-06-28 22:48:14 +01:00
pub fn seen(&self) -> &HashSet<SocketAddr> {
&self.seen
}
pub fn get_live(&self, handle: PeerHandle) -> Option<&LivePeerState> {
if let PeerState::Live(ref l) = self.states.get(&handle)? {
return Some(l);
}
None
}
pub fn get_live_mut(&mut self, handle: PeerHandle) -> Option<&mut LivePeerState> {
if let PeerState::Live(ref mut l) = self.states.get_mut(&handle)? {
return Some(l);
}
None
}
pub fn try_get_live_mut(&mut self, handle: PeerHandle) -> anyhow::Result<&mut LivePeerState> {
self.get_live_mut(handle)
.ok_or_else(|| anyhow::anyhow!("peer dropped"))
}
pub fn add(
&mut self,
addr: SocketAddr,
tx: tokio::sync::mpsc::Sender<WriterRequest>,
) -> Option<PeerHandle> {
let handle = addr;
if self.states.contains_key(&addr) {
return None;
}
self.states.insert(handle, PeerState::Connecting(addr));
self.tx.insert(handle, Arc::new(tx));
Some(handle)
}
pub fn drop_peer(&mut self, handle: PeerHandle) -> Option<PeerState> {
let result = self.states.remove(&handle);
self.tx.remove(&handle);
result
}
pub fn mark_i_am_choked(&mut self, handle: PeerHandle, is_choked: bool) -> Option<bool> {
2021-06-28 14:58:53 +01:00
let live = self.get_live_mut(handle)?;
let prev = live.i_am_choked;
live.i_am_choked = is_choked;
Some(prev)
}
2021-06-28 16:55:50 +01:00
pub fn mark_peer_interested(
&mut self,
handle: PeerHandle,
is_interested: bool,
) -> Option<bool> {
let live = self.get_live_mut(handle)?;
let prev = live.peer_interested;
live.peer_interested = is_interested;
Some(prev)
}
pub fn update_bitfield_from_vec(
&mut self,
handle: PeerHandle,
bitfield: Vec<u8>,
) -> Option<Option<BF>> {
2021-06-28 14:58:53 +01:00
let live = self.get_live_mut(handle)?;
let bitfield = BF::from_vec(bitfield);
let prev = live.bitfield.take();
live.bitfield = Some(bitfield);
Some(prev)
}
pub fn clone_tx(&self, handle: PeerHandle) -> Option<Arc<Sender<WriterRequest>>> {
Some(self.tx.get(&handle)?.clone())
}
2021-06-30 00:02:25 +01:00
pub fn remove_inflight_piece(&mut self, piece: ValidPieceIndex) -> Option<InflightPiece> {
self.inflight_pieces.remove(&piece)
}
}
pub struct TorrentStateLocked {
pub peers: PeerStates,
pub chunks: ChunkTracker,
}
2021-06-30 10:31:30 +01:00
#[derive(Default, Debug)]
pub struct AtomicStats {
pub have: AtomicU64,
pub downloaded_and_checked: AtomicU64,
pub uploaded: AtomicU64,
pub fetched_bytes: AtomicU64,
pub downloaded_pieces: AtomicU64,
pub total_piece_download_ms: AtomicU64,
}
impl AtomicStats {
pub fn average_piece_download_time(&self) -> Option<Duration> {
let d = self.downloaded_pieces.load(Ordering::Relaxed);
let t = self.total_piece_download_ms.load(Ordering::Relaxed);
if d == 0 {
return None;
}
Some(Duration::from_secs_f64(t as f64 / d as f64 / 1000f64))
}
}
2021-06-30 23:26:22 +01:00
#[derive(Debug)]
pub struct StatsSnapshot {
pub have_bytes: u64,
pub downloaded_and_checked_bytes: u64,
pub downloaded_and_checked_pieces: u64,
pub fetched_bytes: u64,
pub uploaded_bytes: u64,
pub initially_needed_bytes: u64,
pub remaining_bytes: u64,
pub live_peers: u32,
pub seen_peers: u32,
pub connecting_peers: u32,
pub time: Instant,
}
pub struct TorrentState {
pub torrent: TorrentMetaV1Owned,
pub locked: Arc<RwLock<TorrentStateLocked>>,
pub files: Vec<Arc<Mutex<File>>>,
pub info_hash: [u8; 20],
pub peer_id: [u8; 20],
pub lengths: Lengths,
pub needed: u64,
pub stats: AtomicStats,
2021-07-01 19:17:44 +01:00
pub spawner: BlockingSpawner,
}
impl TorrentState {
2021-07-01 19:17:44 +01:00
pub fn file_ops(&self) -> FileOps<'_, Sha1> {
FileOps::new(&self.torrent, &self.files, &self.lengths)
}
pub fn get_next_needed_piece(&self, peer_handle: PeerHandle) -> Option<ValidPieceIndex> {
let g = self.locked.read();
2021-06-28 14:58:53 +01:00
let bf = g.peers.get_live(peer_handle)?.bitfield.as_ref()?;
for n in g.chunks.get_needed_pieces().iter_ones() {
if bf.get(n).map(|v| *v) == Some(true) {
// in theory it should be safe without validation, but whatever.
return self.lengths.validate_piece_index(n as u32);
}
}
None
}
pub fn am_i_choked(&self, peer_handle: PeerHandle) -> Option<bool> {
self.locked
.read()
.peers
2021-06-28 14:58:53 +01:00
.get_live(peer_handle)
.map(|l| l.i_am_choked)
}
pub fn reserve_next_needed_piece(&self, peer_handle: PeerHandle) -> Option<ValidPieceIndex> {
if self.am_i_choked(peer_handle)? {
warn!("we are choked by {}, can't reserve next piece", peer_handle);
return None;
}
let mut g = self.locked.write();
let n = {
let mut n_opt = None;
let bf = g.peers.get_live(peer_handle)?.bitfield.as_ref()?;
for n in g.chunks.get_needed_pieces().iter_ones() {
if bf.get(n).map(|v| *v) == Some(true) {
n_opt = Some(n);
break;
}
}
self.lengths.validate_piece_index(n_opt? as u32)?
};
2021-06-30 00:02:25 +01:00
g.peers.inflight_pieces.insert(
n,
InflightPiece {
peer: peer_handle,
started: Instant::now(),
},
);
g.chunks.reserve_needed_piece(n);
Some(n)
}
pub fn am_i_interested_in_peer(&self, handle: PeerHandle) -> bool {
self.get_next_needed_piece(handle).is_some()
}
pub fn try_steal_old_slow_piece(&self, handle: PeerHandle) -> Option<ValidPieceIndex> {
let total = self.stats.downloaded_pieces.load(Ordering::Relaxed);
// heuristic for not enough precision in average time
if total < 20 {
return None;
}
let avg_time = self.stats.average_piece_download_time()?;
let mut g = self.locked.write();
let (idx, elapsed, piece_req) = g
.peers
.inflight_pieces
.iter_mut()
// don't steal from myself
.filter(|(_, r)| r.peer != handle)
.map(|(p, r)| (p, r.started.elapsed(), r))
.max_by_key(|(_, e, _)| *e)?;
// heuristic for "too slow peer"
if elapsed > avg_time * 10 {
debug!(
"{} will steal piece {} from {}: elapsed time {:?}, avg piece time: {:?}",
handle, idx, piece_req.peer, elapsed, avg_time
);
piece_req.peer = handle;
piece_req.started = Instant::now();
return Some(*idx);
}
None
}
pub fn try_steal_piece(&self, handle: PeerHandle) -> Option<ValidPieceIndex> {
let mut rng = rand::thread_rng();
use rand::seq::IteratorRandom;
let g = self.locked.read();
let pl = g.peers.get_live(handle)?;
g.peers
.inflight_pieces
2021-06-30 00:02:25 +01:00
.keys()
.filter(|p| !pl.inflight_requests.iter().any(|req| req.piece == **p))
.choose(&mut rng)
.copied()
}
pub fn set_peer_live(&self, handle: PeerHandle, h: Handshake) {
let mut g = self.locked.write();
match g.peers.states.get_mut(&handle) {
Some(s @ &mut PeerState::Connecting(_)) => {
2021-06-28 14:42:19 +01:00
*s = PeerState::Live(LivePeerState::new(h.peer_id));
}
_ => {
warn!("peer {} was in wrong state", handle);
}
}
}
pub fn drop_peer(&self, handle: PeerHandle) -> bool {
let mut g = self.locked.write();
let peer = match g.peers.drop_peer(handle) {
Some(peer) => peer,
None => return false,
};
match peer {
PeerState::Connecting(_) => {}
PeerState::Live(l) => {
for req in l.inflight_requests {
g.chunks.mark_chunk_request_cancelled(req.piece, req.chunk);
}
}
}
true
}
pub fn get_uploaded(&self) -> u64 {
self.stats.uploaded.load(Ordering::Relaxed)
}
pub fn get_downloaded(&self) -> u64 {
self.stats.downloaded_and_checked.load(Ordering::Relaxed)
}
pub fn get_left_to_download(&self) -> u64 {
self.needed - self.get_downloaded()
}
2021-06-28 16:55:50 +01:00
pub fn maybe_transmit_haves(&self, index: ValidPieceIndex) {
let mut futures = Vec::new();
2021-06-28 16:55:50 +01:00
let g = self.locked.read();
for (handle, peer_state) in g.peers.states.iter() {
match peer_state {
PeerState::Live(live) => {
if !live.peer_interested {
continue;
}
if live
.bitfield
.as_ref()
.and_then(|b| b.get(index.get() as usize).map(|v| *v))
.unwrap_or(false)
{
2021-06-28 16:55:50 +01:00
continue;
}
2021-06-28 16:55:50 +01:00
let tx = match g.peers.tx.get(handle) {
Some(tx) => tx,
None => continue,
};
let tx = Arc::downgrade(tx);
futures.push(async move {
2021-06-28 16:55:50 +01:00
if let Some(tx) = tx.upgrade() {
if tx
.send(WriterRequest::Message(Message::Have(index.get())))
.await
.is_err()
{
// whatever
}
}
});
}
2021-06-28 16:55:50 +01:00
_ => continue,
}
}
if futures.is_empty() {
2021-06-28 16:55:50 +01:00
trace!("no peers to transmit Have={} to, saving some work", index);
return;
}
let mut unordered: FuturesUnordered<_> = futures.into_iter().collect();
2021-06-28 16:55:50 +01:00
spawn(
format!("transmit_haves(piece={}, count={})", index, unordered.len()),
async move {
while unordered.next().await.is_some() {}
Ok(())
},
);
}
2021-06-29 00:17:10 +01:00
2021-06-30 10:14:33 +01:00
pub fn add_peer_if_not_seen(self: &Arc<Self>, addr: SocketAddr) -> bool {
2021-06-29 00:17:10 +01:00
let (out_tx, out_rx) = channel::<WriterRequest>(1);
let handle = match self.locked.write().peers.add_if_not_seen(addr, out_tx) {
Some(handle) => handle,
2021-06-30 10:14:33 +01:00
None => return false,
2021-06-29 00:17:10 +01:00
};
2021-07-01 19:17:44 +01:00
let peer_connection = PeerConnection::new(self.clone(), self.spawner.clone());
2021-06-29 00:17:10 +01:00
spawn(format!("manage_peer({})", handle), async move {
if let Err(e) = peer_connection.manage_peer(addr, handle, out_rx).await {
debug!("error managing peer {}: {:#}", handle, e)
};
peer_connection.into_state().drop_peer(handle);
Ok::<_, anyhow::Error>(())
});
2021-06-30 10:14:33 +01:00
true
2021-06-29 00:17:10 +01:00
}
2021-06-30 23:26:22 +01:00
pub fn stats_snapshot(&self) -> StatsSnapshot {
let g = self.locked.read();
use Ordering::*;
let (live, connecting) =
g.peers
.states
.values()
.fold((0u32, 0u32), |(live, connecting), p| match p {
PeerState::Connecting(_) => (live, connecting + 1),
PeerState::Live(_) => (live + 1, connecting),
});
let downloaded = self.stats.downloaded_and_checked.load(Relaxed);
let remaining = self.needed - downloaded;
StatsSnapshot {
have_bytes: self.stats.have.load(Relaxed),
downloaded_and_checked_bytes: downloaded,
downloaded_and_checked_pieces: self.stats.downloaded_pieces.load(Relaxed),
fetched_bytes: self.stats.fetched_bytes.load(Relaxed),
uploaded_bytes: self.stats.fetched_bytes.load(Relaxed),
live_peers: live,
seen_peers: g.peers.seen.len() as u32,
connecting_peers: connecting,
time: Instant::now(),
initially_needed_bytes: self.needed,
remaining_bytes: remaining,
}
}
}