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

Fix bug: number of downloads for a torrent is reset #1356

4 changes: 2 additions & 2 deletions packages/axum-http-tracker-server/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub struct Environment<S> {
impl<S> Environment<S> {
/// Add a torrent to the tracker
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
let () = self
let _number_of_downloads_increased = self
.container
.tracker_core_container
.in_memory_torrent_repository
.upsert_peer(info_hash, peer);
.upsert_peer(info_hash, peer, None);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/axum-rest-tracker-api-server/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ where
{
/// Add a torrent to the tracker
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
let () = self
let _number_of_downloads_increased = self
.container
.tracker_core_container
.in_memory_torrent_repository
.upsert_peer(info_hash, peer);
.upsert_peer(info_hash, peer, None);
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/torrent-repository/benches/helpers/asyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ where

let info_hash = InfoHash::default();

torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER).await;
torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER, None).await;

torrent_repository.get_swarm_metadata(&info_hash).await;
}
Expand All @@ -37,7 +37,7 @@ where
let handles = FuturesUnordered::new();

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

torrent_repository.get_swarm_metadata(&info_hash).await;

Expand All @@ -47,7 +47,7 @@ where
let torrent_repository_clone = torrent_repository.clone();

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

torrent_repository_clone.get_swarm_metadata(&info_hash).await;

Expand Down Expand Up @@ -87,7 +87,7 @@ where
let torrent_repository_clone = torrent_repository.clone();

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

torrent_repository_clone.get_swarm_metadata(&info_hash).await;

Expand Down Expand Up @@ -123,7 +123,7 @@ where

// Add the torrents/peers to the torrent repository
for info_hash in &info_hashes {
torrent_repository.upsert_peer(info_hash, &DEFAULT_PEER).await;
torrent_repository.upsert_peer(info_hash, &DEFAULT_PEER, None).await;
torrent_repository.get_swarm_metadata(info_hash).await;
}

Expand All @@ -133,7 +133,7 @@ where
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER).await;
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None).await;
torrent_repository_clone.get_swarm_metadata(&info_hash).await;

if let Some(sleep_time) = sleep {
Expand Down
12 changes: 6 additions & 6 deletions packages/torrent-repository/benches/helpers/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where

let info_hash = InfoHash::default();

torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER);
torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER, None);

torrent_repository.get_swarm_metadata(&info_hash);
}
Expand All @@ -39,7 +39,7 @@ where
let handles = FuturesUnordered::new();

// Add the torrent/peer to the torrent repository
torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER);
torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER, None);

torrent_repository.get_swarm_metadata(&info_hash);

Expand All @@ -49,7 +49,7 @@ where
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER);
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None);

torrent_repository_clone.get_swarm_metadata(&info_hash);

Expand Down Expand Up @@ -89,7 +89,7 @@ where
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER);
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None);

torrent_repository_clone.get_swarm_metadata(&info_hash);

Expand Down Expand Up @@ -125,7 +125,7 @@ where

// Add the torrents/peers to the torrent repository
for info_hash in &info_hashes {
torrent_repository.upsert_peer(info_hash, &DEFAULT_PEER);
torrent_repository.upsert_peer(info_hash, &DEFAULT_PEER, None);
torrent_repository.get_swarm_metadata(info_hash);
}

Expand All @@ -135,7 +135,7 @@ where
let torrent_repository_clone = torrent_repository.clone();

let handle = runtime.spawn(async move {
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER);
torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None);
torrent_repository_clone.get_swarm_metadata(&info_hash);

if let Some(sleep_time) = sleep {
Expand Down
8 changes: 5 additions & 3 deletions packages/torrent-repository/src/entry/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Entry for EntrySingle {
}

fn upsert_peer(&mut self, peer: &peer::Peer) -> bool {
let mut downloaded_stats_updated: bool = false;
let mut number_of_downloads_increased: bool = false;

match peer::ReadInfo::get_event(peer) {
AnnounceEvent::Stopped => {
Expand All @@ -62,15 +62,17 @@ impl Entry for EntrySingle {
// Don't count if peer was not previously known and not already completed.
if previous.is_some_and(|p| p.event != AnnounceEvent::Completed) {
self.downloaded += 1;
downloaded_stats_updated = true;
number_of_downloads_increased = true;
}
}
_ => {
// `Started` event (first announced event) or
// `None` event (announcements done at regular intervals).
drop(self.swarm.upsert(Arc::new(*peer)));
}
}

downloaded_stats_updated
number_of_downloads_increased
}

fn remove_inactive_peers(&mut self, current_cutoff: DurationSinceUnixEpoch) {
Expand Down
12 changes: 8 additions & 4 deletions packages/torrent-repository/src/repository/dash_map_mutex_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::Repository;
use crate::entry::peer_list::PeerList;
Expand All @@ -23,13 +23,17 @@ where
EntryMutexStd: EntrySync,
EntrySingle: Entry,
{
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<PersistentTorrent>) -> bool {
// todo: load persistent torrent data if provided

if let Some(entry) = self.torrents.get(info_hash) {
entry.upsert_peer(peer);
entry.upsert_peer(peer)
} else {
let _unused = self.torrents.insert(*info_hash, Arc::default());
if let Some(entry) = self.torrents.get(info_hash) {
entry.upsert_peer(peer);
entry.upsert_peer(peer)
} else {
false
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions packages/torrent-repository/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

pub mod dash_map_mutex_std;
pub mod rw_lock_std;
Expand All @@ -24,7 +24,7 @@ pub trait Repository<T>: Debug + Default + Sized + 'static {
fn remove(&self, key: &InfoHash) -> Option<T>;
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch);
fn remove_peerless_torrents(&self, policy: &TrackerPolicy);
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer);
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, opt_persistent_torrent: Option<PersistentTorrent>) -> bool;
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata>;
}

Expand All @@ -37,6 +37,11 @@ pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
fn remove(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) -> impl std::future::Future<Output = ()> + Send;
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) -> impl std::future::Future<Output = ()> + Send;
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> impl std::future::Future<Output = ()> + Send;
fn upsert_peer(
&self,
info_hash: &InfoHash,
peer: &peer::Peer,
opt_persistent_torrent: Option<PersistentTorrent>,
) -> impl std::future::Future<Output = bool> + Send;
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> impl std::future::Future<Output = Option<SwarmMetadata>> + Send;
}
8 changes: 5 additions & 3 deletions packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::Repository;
use crate::entry::peer_list::PeerList;
Expand Down Expand Up @@ -46,12 +46,14 @@ impl Repository<EntrySingle> for TorrentsRwLockStd
where
EntrySingle: Entry,
{
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<PersistentTorrent>) -> bool {
// todo: load persistent torrent data if provided

let mut db = self.get_torrents_mut();

let entry = db.entry(*info_hash).or_insert(EntrySingle::default());

entry.upsert_peer(peer);
entry.upsert_peer(peer)
}

fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::Repository;
use crate::entry::peer_list::PeerList;
Expand Down Expand Up @@ -33,7 +33,9 @@ where
EntryMutexStd: EntrySync,
EntrySingle: Entry,
{
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer, _opt_persistent_torrent: Option<PersistentTorrent>) -> bool {
// todo: load persistent torrent data if provided

let maybe_entry = self.get_torrents().get(info_hash).cloned();

let entry = if let Some(entry) = maybe_entry {
Expand All @@ -44,7 +46,7 @@ where
entry.clone()
};

entry.upsert_peer(peer);
entry.upsert_peer(peer)
}

fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::peer_list::PeerList;
Expand Down Expand Up @@ -37,7 +37,14 @@ where
EntryMutexTokio: EntryAsync,
EntrySingle: Entry,
{
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
async fn upsert_peer(
&self,
info_hash: &InfoHash,
peer: &peer::Peer,
_opt_persistent_torrent: Option<PersistentTorrent>,
) -> bool {
// todo: load persistent torrent data if provided

let maybe_entry = self.get_torrents().get(info_hash).cloned();

let entry = if let Some(entry) = maybe_entry {
Expand All @@ -48,7 +55,7 @@ where
entry.clone()
};

entry.upsert_peer(peer).await;
entry.upsert_peer(peer).await
}

async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {
Expand Down
13 changes: 10 additions & 3 deletions packages/torrent-repository/src/repository/rw_lock_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::peer_list::PeerList;
Expand Down Expand Up @@ -47,12 +47,19 @@ impl RepositoryAsync<EntrySingle> for TorrentsRwLockTokio
where
EntrySingle: Entry,
{
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
async fn upsert_peer(
&self,
info_hash: &InfoHash,
peer: &peer::Peer,
_opt_persistent_torrent: Option<PersistentTorrent>,
) -> bool {
// todo: load persistent torrent data if provided

let mut db = self.get_torrents_mut().await;

let entry = db.entry(*info_hash).or_insert(EntrySingle::default());

entry.upsert_peer(peer);
entry.upsert_peer(peer)
}

async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::pagination::Pagination;
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::peer_list::PeerList;
Expand Down Expand Up @@ -35,7 +35,14 @@ where
EntryMutexStd: EntrySync,
EntrySingle: Entry,
{
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
async fn upsert_peer(
&self,
info_hash: &InfoHash,
peer: &peer::Peer,
_opt_persistent_torrent: Option<PersistentTorrent>,
) -> bool {
// todo: load persistent torrent data if provided

let maybe_entry = self.get_torrents().await.get(info_hash).cloned();

let entry = if let Some(entry) = maybe_entry {
Expand All @@ -46,7 +53,7 @@ where
entry.clone()
};

entry.upsert_peer(peer);
entry.upsert_peer(peer)
}

async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {
Expand Down
Loading