Initialization progress reporting

This commit is contained in:
Igor Katson 2023-11-24 15:04:36 +00:00
parent b79a21179f
commit 876afbf41b
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
9 changed files with 109 additions and 40 deletions

View file

@ -1,12 +1,11 @@
use std::{
fs::{File, OpenOptions},
sync::Arc,
sync::{atomic::AtomicU64, Arc},
time::Instant,
};
use anyhow::Context;
use librqbit_core::{lengths::Lengths, torrent_metainfo::TorrentMetaV1Info};
use parking_lot::Mutex;
use sha1w::Sha1;
@ -17,13 +16,6 @@ use crate::{chunk_tracker::ChunkTracker, file_ops::FileOps};
use super::{paused::TorrentStatePaused, ManagedTorrentInfo};
fn make_lengths<ByteBuf: AsRef<[u8]>>(
torrent: &TorrentMetaV1Info<ByteBuf>,
) -> anyhow::Result<Lengths> {
let total_length = torrent.iter_file_lengths()?.sum();
Lengths::new(total_length, torrent.piece_length, None)
}
fn ensure_file_length(file: &File, length: u64) -> anyhow::Result<()> {
Ok(file.set_len(length)?)
}
@ -31,11 +23,16 @@ fn ensure_file_length(file: &File, length: u64) -> anyhow::Result<()> {
pub struct TorrentStateInitializing {
pub(crate) meta: Arc<ManagedTorrentInfo>,
pub(crate) only_files: Option<Vec<usize>>,
pub(crate) checked_bytes: AtomicU64,
}
impl TorrentStateInitializing {
pub fn new(meta: Arc<ManagedTorrentInfo>, only_files: Option<Vec<usize>>) -> Self {
Self { meta, only_files }
Self {
meta,
only_files,
checked_bytes: AtomicU64::new(0),
}
}
pub async fn check(&self) -> anyhow::Result<TorrentStatePaused> {
@ -72,14 +69,12 @@ impl TorrentStateInitializing {
(files, filenames)
};
let lengths =
make_lengths(&self.meta.info).context("unable to compute Lengths from torrent")?;
debug!("computed lengths: {:?}", &lengths);
debug!("computed lengths: {:?}", &self.meta.lengths);
info!("Doing initial checksum validation, this might take a while...");
let initial_check_results = self.meta.spawner.spawn_block_in_place(|| {
FileOps::<Sha1>::new(&self.meta.info, &files, &lengths)
.initial_check(self.only_files.as_deref())
FileOps::<Sha1>::new(&self.meta.info, &files, &self.meta.lengths)
.initial_check(self.only_files.as_deref(), &self.checked_bytes)
})?;
info!(
@ -122,7 +117,7 @@ impl TorrentStateInitializing {
let chunk_tracker = ChunkTracker::new(
initial_check_results.needed_pieces,
initial_check_results.have_pieces,
lengths,
self.meta.lengths,
);
let paused = TorrentStatePaused {
@ -131,7 +126,6 @@ impl TorrentStateInitializing {
filenames,
chunk_tracker,
have_bytes: initial_check_results.have_bytes,
needed_bytes: initial_check_results.needed_bytes,
};
Ok(paused)
}

View file

@ -163,7 +163,7 @@ impl TorrentStateLive {
let speed_estimator = SpeedEstimator::new(5);
let have_bytes = paused.have_bytes;
let needed_bytes = paused.needed_bytes;
let needed_bytes = paused.info.lengths.total_length() - have_bytes;
let lengths = *paused.chunk_tracker.get_lengths();
let state = Arc::new(TorrentStateLive {
@ -533,7 +533,6 @@ impl TorrentStateLive {
fetched_bytes: self.stats.fetched_bytes.load(Relaxed),
uploaded_bytes: self.stats.uploaded_bytes.load(Relaxed),
total_bytes: self.have_plus_needed_bytes,
time: Instant::now(),
initially_needed_bytes: self.needed_bytes,
remaining_bytes: remaining,
total_piece_download_ms: self.stats.total_piece_download_ms.load(Relaxed),

View file

@ -4,7 +4,7 @@ use serde::Serialize;
use crate::torrent_state::live::peers::stats::snapshot::AggregatePeerStats;
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Default)]
pub struct StatsSnapshot {
pub have_bytes: u64,
pub downloaded_and_checked_bytes: u64,
@ -14,8 +14,8 @@ pub struct StatsSnapshot {
pub initially_needed_bytes: u64,
pub remaining_bytes: u64,
pub total_bytes: u64,
#[serde(skip)]
pub time: Instant,
// #[serde(skip)]
// pub time: Instant,
pub total_piece_download_ms: u64,
pub peer_stats: AggregatePeerStats,
}

View file

@ -13,6 +13,7 @@ use anyhow::bail;
use anyhow::Context;
use buffers::ByteString;
use librqbit_core::id20::Id20;
use librqbit_core::lengths::Lengths;
use librqbit_core::peer_id::generate_peer_id;
use librqbit_core::torrent_metainfo::TorrentMetaV1Info;
@ -73,6 +74,7 @@ pub struct ManagedTorrentInfo {
pub spawner: BlockingSpawner,
pub trackers: Vec<Url>,
pub peer_id: Id20,
pub lengths: Lengths,
pub(crate) options: ManagedTorrentOptions,
}
@ -286,7 +288,8 @@ impl ManagedTorrentBuilder {
self
}
pub(crate) fn build(self) -> ManagedTorrentHandle {
pub(crate) fn build(self) -> anyhow::Result<ManagedTorrentHandle> {
let lengths = Lengths::from_torrent(&self.info)?;
let info = Arc::new(ManagedTorrentInfo {
info: self.info,
info_hash: self.info_hash,
@ -294,6 +297,7 @@ impl ManagedTorrentBuilder {
trackers: self.trackers.into_iter().collect(),
spawner: self.spawner.unwrap_or_default(),
peer_id: self.peer_id.unwrap_or_else(generate_peer_id),
lengths,
options: ManagedTorrentOptions {
force_tracker_interval: self.force_tracker_interval,
peer_connect_timeout: self.peer_connect_timeout,
@ -305,13 +309,13 @@ impl ManagedTorrentBuilder {
info.clone(),
self.only_files.clone(),
));
Arc::new(ManagedTorrent {
Ok(Arc::new(ManagedTorrent {
only_files: self.only_files,
locked: RwLock::new(ManagedTorrentLocked {
state: ManagedTorrentState::Initializing(initializing),
}),
info,
})
}))
}
}

View file

@ -12,7 +12,6 @@ pub struct TorrentStatePaused {
pub(crate) filenames: Vec<PathBuf>,
pub(crate) chunk_tracker: ChunkTracker,
pub(crate) have_bytes: u64,
pub(crate) needed_bytes: u64,
}
// impl TorrentStatePaused {