Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: performance optimization in tracker announce #751

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions packages/torrent-repository-benchmarks/src/benches/asyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

let start_time = std::time::Instant::now();

torrent_repository
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
.await;
torrent_repository.announce(&info_hash, &DEFAULT_PEER).await;

Check warning on line 22 in packages/torrent-repository-benchmarks/src/benches/asyn.rs

View check run for this annotation

Codecov / codecov/patch

packages/torrent-repository-benchmarks/src/benches/asyn.rs#L22

Added line #L22 was not covered by tests

let result = start_time.elapsed();

Expand All @@ -45,19 +43,15 @@
let handles = FuturesUnordered::new();

// Add the torrent/peer to the torrent repository
torrent_repository
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
.await;
torrent_repository.announce(info_hash, &DEFAULT_PEER).await;

Check warning on line 46 in packages/torrent-repository-benchmarks/src/benches/asyn.rs

View check run for this annotation

Codecov / codecov/patch

packages/torrent-repository-benchmarks/src/benches/asyn.rs#L46

Added line #L46 was not covered by tests

let start_time = std::time::Instant::now();

for _ in 0..10_000 {
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
.await;
torrent_repository_clone.announce(info_hash, &DEFAULT_PEER).await;

Check warning on line 54 in packages/torrent-repository-benchmarks/src/benches/asyn.rs

View check run for this annotation

Codecov / codecov/patch

packages/torrent-repository-benchmarks/src/benches/asyn.rs#L54

Added line #L54 was not covered by tests

if let Some(sleep_time) = args.sleep {
let start_time = std::time::Instant::now();
Expand Down Expand Up @@ -99,9 +93,7 @@
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
.await;
torrent_repository_clone.announce(&info_hash, &DEFAULT_PEER).await;

Check warning on line 96 in packages/torrent-repository-benchmarks/src/benches/asyn.rs

View check run for this annotation

Codecov / codecov/patch

packages/torrent-repository-benchmarks/src/benches/asyn.rs#L96

Added line #L96 was not covered by tests

if let Some(sleep_time) = args.sleep {
let start_time = std::time::Instant::now();
Expand Down Expand Up @@ -139,9 +131,7 @@

// Add the torrents/peers to the torrent repository
for info_hash in &info_hashes {
torrent_repository
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
.await;
torrent_repository.announce(info_hash, &DEFAULT_PEER).await;

Check warning on line 134 in packages/torrent-repository-benchmarks/src/benches/asyn.rs

View check run for this annotation

Codecov / codecov/patch

packages/torrent-repository-benchmarks/src/benches/asyn.rs#L134

Added line #L134 was not covered by tests
}

let start_time = std::time::Instant::now();
Expand All @@ -150,9 +140,7 @@
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
.await;
torrent_repository_clone.announce(&info_hash, &DEFAULT_PEER).await;

Check warning on line 143 in packages/torrent-repository-benchmarks/src/benches/asyn.rs

View check run for this annotation

Codecov / codecov/patch

packages/torrent-repository-benchmarks/src/benches/asyn.rs#L143

Added line #L143 was not covered by tests

if let Some(sleep_time) = args.sleep {
let start_time = std::time::Instant::now();
Expand Down
41 changes: 9 additions & 32 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,7 @@ impl Tracker {
peer.change_ip(&assign_ip_address_to_peer(remote_client_ip, self.external_ip));
debug!("After: {peer:?}");

// we should update the torrent and get the stats before we get the peer list.
let stats = self.update_torrent_with_peer_and_get_stats(info_hash, peer).await;

let peers = self.get_torrent_peers_for_peer(info_hash, peer).await;
let (stats, peers) = self.inner_announce(info_hash, peer).await;

AnnounceData {
peers,
Expand Down Expand Up @@ -722,19 +719,6 @@ impl Tracker {
Ok(())
}

async fn get_torrent_peers_for_peer(&self, info_hash: &InfoHash, peer: &Peer) -> Vec<peer::Peer> {
let read_lock = self.torrents.get_torrents().await;

match read_lock.get(info_hash) {
None => vec![],
Some(entry) => entry
.get_peers_for_peer(peer, TORRENT_PEERS_LIMIT)
.into_iter()
.copied()
.collect(),
}
}

/// # Context: Tracker
///
/// Get all torrent peers for a given torrent
Expand All @@ -752,11 +736,8 @@ impl Tracker {
/// needed for a `announce` request response.
///
/// # Context: Tracker
pub async fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> torrent::SwarmStats {
// code-review: consider splitting the function in two (command and query segregation).
// `update_torrent_with_peer` and `get_stats`

let (stats, stats_updated) = self.torrents.update_torrent_with_peer_and_get_stats(info_hash, peer).await;
pub async fn inner_announce(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (torrent::SwarmStats, Vec<Peer>) {
let (stats, stats_updated, peers) = self.torrents.announce(info_hash, peer).await;

if self.policy.persistent_torrent_completed_stat && stats_updated {
let completed = stats.downloaded;
Expand All @@ -765,7 +746,7 @@ impl Tracker {
drop(self.database.save_persistent_torrent(&info_hash, completed).await);
}

stats
(stats, peers)
}

/// It calculates and returns the general `Tracker`
Expand Down Expand Up @@ -1228,7 +1209,7 @@ mod tests {
let info_hash = sample_info_hash();
let peer = sample_peer();

tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;
tracker.inner_announce(&info_hash, &peer).await;

let peers = tracker.get_torrent_peers(&info_hash).await;

Expand All @@ -1242,9 +1223,7 @@ mod tests {
let info_hash = sample_info_hash();
let peer = sample_peer();

tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;

let peers = tracker.get_torrent_peers_for_peer(&info_hash, &peer).await;
let (_stats, peers) = tracker.inner_announce(&info_hash, &peer).await;

assert_eq!(peers, vec![]);
}
Expand All @@ -1253,9 +1232,7 @@ mod tests {
async fn it_should_return_the_torrent_metrics() {
let tracker = public_tracker();

tracker
.update_torrent_with_peer_and_get_stats(&sample_info_hash(), &leecher())
.await;
tracker.inner_announce(&sample_info_hash(), &leecher()).await;

let torrent_metrics = tracker.get_torrents_metrics().await;

Expand Down Expand Up @@ -1764,11 +1741,11 @@ mod tests {
let mut peer = sample_peer();

peer.event = AnnounceEvent::Started;
let swarm_stats = tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;
let (swarm_stats, _peers) = tracker.inner_announce(&info_hash, &peer).await;
assert_eq!(swarm_stats.downloaded, 0);

peer.event = AnnounceEvent::Completed;
let swarm_stats = tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;
let (swarm_stats, _peers) = tracker.inner_announce(&info_hash, &peer).await;
assert_eq!(swarm_stats.downloaded, 1);

// Remove the newly updated torrent from memory
Expand Down
32 changes: 8 additions & 24 deletions src/core/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ mod tests {

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
tracker
.update_torrent_with_peer_and_get_stats(&info_hash, &sample_peer())
.await;
tracker.inner_announce(&info_hash, &sample_peer()).await;

let torrent_info = get_torrent_info(tracker.clone(), &info_hash).await.unwrap();

Expand Down Expand Up @@ -265,9 +263,7 @@ mod tests {
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();

tracker
.update_torrent_with_peer_and_get_stats(&info_hash, &sample_peer())
.await;
tracker.inner_announce(&info_hash, &sample_peer()).await;

let torrents = get_torrents_page(tracker.clone(), &Pagination::default()).await;

Expand All @@ -291,12 +287,8 @@ mod tests {
let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();

tracker
.update_torrent_with_peer_and_get_stats(&info_hash1, &sample_peer())
.await;
tracker
.update_torrent_with_peer_and_get_stats(&info_hash2, &sample_peer())
.await;
tracker.inner_announce(&info_hash1, &sample_peer()).await;
tracker.inner_announce(&info_hash2, &sample_peer()).await;

let offset = 0;
let limit = 1;
Expand All @@ -315,12 +307,8 @@ mod tests {
let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();

tracker
.update_torrent_with_peer_and_get_stats(&info_hash1, &sample_peer())
.await;
tracker
.update_torrent_with_peer_and_get_stats(&info_hash2, &sample_peer())
.await;
tracker.inner_announce(&info_hash1, &sample_peer()).await;
tracker.inner_announce(&info_hash2, &sample_peer()).await;

let offset = 1;
let limit = 4000;
Expand All @@ -345,15 +333,11 @@ mod tests {

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
tracker
.update_torrent_with_peer_and_get_stats(&info_hash1, &sample_peer())
.await;
tracker.inner_announce(&info_hash1, &sample_peer()).await;

let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();
tracker
.update_torrent_with_peer_and_get_stats(&info_hash2, &sample_peer())
.await;
tracker.inner_announce(&info_hash2, &sample_peer()).await;

let torrents = get_torrents_page(tracker.clone(), &Pagination::default()).await;

Expand Down
Loading
Loading