mpetazzoni / ttorrent

BitTorrent Java library with tracker and download client
http://mpetazzoni.github.com/ttorrent/
Apache License 2.0
1.38k stars 502 forks source link

List of TrackedTorrent seeders + leechers changing randomly #229

Closed asadsm closed 5 years ago

asadsm commented 5 years ago

I start by creating a torrent, announcing it via the tracker and then creating a seeder for it. I then start two clients to download the data using the torrent. Meanwhile I have a thread which prints out number of seeders and leechers for each TrackedTorrent every 20 seconds. This seems to be changing arbitrarily without me adding/removing clients.

Starting the tracker and adding torrent:

tracker = new Tracker(Tracker.DEFAULT_TRACKER_PORT);
tracker.setAcceptForeignTorrents(true);
tracker.start(true);
System.out.println("Tracker Started...");
tracker.announce(TrackedTorrent.load(new File(torrentFile)));

Starting the initial seeder:

Seeder seed = new Seeder();
File sourceFile = new File(command[2]);
String sourceDir = sourceFile.getAbsoluteFile().getParentFile().getAbsolutePath();
if (sourceFile.isDirectory())
      sourceDir = sourceFile.getAbsoluteFile().getAbsolutePath();

System.out.println("Source dir is " + sourceDir);
seed.start(TORRENT_DIR + sourceFile.getName() + ".torrent", sourceDir);
seederList.add(seed);

Starting the leechers:

String torrentPath = command[0];
String downloadPath = command[1];
File downloadDir = new File(downloadPath);

SimpleClient client = new SimpleClient();
Inet4Address iPv4Address = (Inet4Address) Inet4Address.getLocalHost();
client.downloadTorrent(torrentPath, downloadPath, iPv4Address);

The Seeder class start method is below:

public void start(String torrentPath, String sourceDir) throws IOException {
        communicationManager.addTorrent(torrentPath, sourceDir, FullyPieceStorageFactory.INSTANCE);
    }

Now when I start everything, the leechers are able to download the file/folder and then continue to seed. I use the following to output the status:

tracker.getTrackedTorrents().forEach(trackedTorrent ->
                            System.out.println("Seeders: [" + trackedTorrent.seeders() + "] Leechers: [" + trackedTorrent.leechers() +"]"));

tracker.getTrackedTorrents().forEach(trackedTorrent ->
                            System.out.println("Seeders: [" + trackedTorrent.seeders() + "] Leechers: [" + trackedTorrent.leechers() +"]"));

When I start only the seeder followed by the leechers I see the following:

Seeders: [1] Leechers: [0]
Seeders: [1] Leechers: [0]
Seeders: [1] Leechers: [0]
Seeders: [1] Leechers: [0]
Seeders: [0] Leechers: [1]
Seeders: [0] Leechers: [2]
Seeders: [0] Leechers: [2]
Seeders: [0] Leechers: [2]

This delays the download quite a bit because I don't see any log messages on the leecher side regarding the downloading. It then goes back to Seeders: [1] Leechers: [2] and the download does start but it's weird to see this happening. I can even see this after the download finishes on both leechers (which then continue to seed) as you can see below:

[1009 16:56:59,523 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[1009 16:56:59,524 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@7678a6b1]
Seeders: [1] Leechers: [1]
Seeders: [2] Leechers: [1]
Seeders: [1] Leechers: [1]
[1009 16:57:59,526 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[1009 16:57:59,526 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@7477b12b]
Seeders: [1] Leechers: [1]
Seeders: [0] Leechers: [1]
Seeders: [0] Leechers: [1]
[1009 16:58:59,530 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[1009 16:58:59,530 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@715e3a35]
Seeders: [0] Leechers: [1]
Seeders: [1] Leechers: [1]
Seeders: [1] Leechers: [1]
[1009 16:59:59,533 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[1009 16:59:59,534 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@2854458]

Any idea what I'm doing wrong here?

Dead-off commented 5 years ago

it's bad default values on tracker-side, i've changed default cleanup timeout on tracker side. You can just change it manually (paste this line on tracker starting) tracker.setPeerCollectorExpireTimeout(600);//10 mins

asadsm commented 5 years ago

Thanks, I made that change. However I still see this show up as soon as the first client starts downloading.

Seeders: [1] Leechers: [0]
Seeders: [1] Leechers: [0]
[1011 08:52:30,579 Torrent channels manager thread]  DEBUG - rk.HandshakeReceiver - setup new connection with Peer / for torrent 9C6BCECE81A3E9D151893642F77ECB234DC11CF3
[1011 08:52:30,580 pool-1-thread-2]  DEBUG - client.SharedTorrent - Analyzing local data for QSG with 48 threads...
.
.
.
[1011 08:52:30,633 pool-1-thread-4 handle message for torrent 9C6BCECE81A3E9D151893642F77ECB234DC11CF3 peer: /]  DEBUG - .storage.FileStorage - Opened byte storage file at /home/asmalik/torrent_test/QSG/QG_PF (7011164160+159344640 byte(s)).
Seeders: [1] Leechers: [1]
Seeders: [0] Leechers: [1]

Where it states that there is no seeder although the download completes.

asadsm commented 5 years ago

This is noticeable however in some cases like the following:

Start tracker and seeder on machine A. Start leeching followed by seeding on machine B. Takes approx 70 sec for download complete Start leeching followed by seeding on machine C. Takes approx 65 sec for download complete Start leeching followed by seeding on machine D. Takes approx 112 sec for download complete.

Now wait for all 4 machines to seed. This however doesn't show up in the output which shows Seeders: [2] Leechers: [2]

Once downloaded on all, we should get 4 seeders, A, B, C and D.

Then I start download on machine E. This machine for 2 minutes isn't able to pick up any peers:

[1011 09:01:03,405 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 0 torrents
.
.
.
[1011 09:03:33,578 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents []

After which it only gets 3 peers instead of 4.

[1011 09:03:40,050 main]  DEBUG - nounce.TrackerClient - Announcing  STARTED to tracker with 0U/0D/7170508800L bytes...
[1011 09:03:40,202 main]   INFO - CommunicationManager - Got 3 peer(s) ([Peer /10.87.... for torrent null, Peer /10.87... for torrent null, Peer /10.81.... for torrent null]) for 9C6BCECE81A3E9D151893642F77ECB234DC11CF3 in tracker response

It then completes the download in Download took: 352.635321247 seconds

Dead-off commented 5 years ago

i can't reproduce this problem and also it looks curiously because peer can be removed on tracker in two cases:

  1. peer sent stop event but if you don't remove torrent and don't stop client peer doesn't send stop event
  2. by timeout but again if you specify large timeout by tracker.setPeerCollectorExpireTimeout it should not happen can you check (e.g. using debugger) what removes peers (e.g. show stacktrace)? lines: TrackedTorrent.java:114 TrackedTorrent.java:155 i don't understand how it's possible
asadsm commented 5 years ago

So it doesn't actually remove the initial seeder. It changes its status to STARTED from COMPLETED in the update method in TrackedTorrent on line 217 as the RequestEvent is NONE.

Dead-off commented 5 years ago

i fixed this problem (https://github.com/mpetazzoni/ttorrent/commit/350c88f74fa73d42846c676c99581e380661194e). Thank you for help but i asked about problem with removing peers:)

Then I start download on machine E. This machine for 2 minutes isn't able to pick up any peers:

asadsm commented 5 years ago

Oh so that's my fault. Sorry for not mentioning that. It was due to a firewall which prevented it from discovering peers.