Overhaul stats: Emit events from the torrent-repository
package
#1358
Labels
- Developer -
Torrust Improvement Experience
Code Cleanup / Refactoring
Tidying and Making Neat
Enhancement / Feature Request
Something New
Context
I've been working on this issue #1264 recently.
The issue was a bug with the number of downloads for a given torrent in stats.
If you enable it in the configuration the number of downloads for all torrents is persisted into the database.
The tracker must:
Torrent are removed from the in-memory torrent repository when:
Problem
Internally all torrent repository implementations keep torrent metrics like:
complete
: number of seeders.incomplete
: number of leechers.downloaded
: the number of peers that have ever completed downloading.The following is the one we are using in production:
As you can see the
upsert_peer
method return a value that indicates if the number of downloads was increased.That boolean returned value is used by the tracker core announce handler to decide whether the downloads counter has to be increased or not.
From my point of view there is an smell there. Ideally the
upsert_peer
command should not return anything (Command–query separation principle).However, we need to communicate the the upper layers that something happened. What happened is a peer announce that completed a download (after announcing it had started).
Solution add domain events to the torrent-repository package
Instead of communicate with upper layers via the returning value we could emit an event (focused on the type of the announce event) from the
torrent-repository
:Other alternative for events (focused on changes on the peer list) could be:
Maybe we can emit more than one type.
NOTICE: the event includes the peer data that can be useful for listeners (in-process listeners or external services, like the Index, if we emit the event as an integration event in the future).
The event is the same we have at the delivery layer but with different information.
We can trigger the event in the
Entry::upsert_peer
orRepository<EntryRwLockParkingLot>::upsert_peer
. I would do it in the repo.The
Entry::upsert_peer
can return the event instead of the boolean that is returning currently:The tracker core could listen to these events and update the downloads counter in the database.
Pros:
AnnounceHandler
.Secondary refactors enabled by this feature
AnnounceHandler
The new tracker core
AnnounceHandler
would be simpler and be something like:The torrent repo keeps tracks of these metrics:
Notice the number of downloads value is used in the API:
We can change it to avoid loading the data from the database (when persistence is enable), meaning we have to revert this PR.
downloaded
metrics would count only downloads since the torrents was first announced.The API can take this value
I suggested this here.
IMPORTANT: it would track not the total number of downloads since the tracker started but since the torrent was added to the repo the last time (torrents can be added and removed when they don't have peers).
How to enable torrent stats persistence
The option
persistent_torrent_completed_stat
must betrue
.For example, you can force that option overriding it when you run the tracker.
TORRUST_TRACKER_CONFIG_OVERRIDE_CORE__TRACKER_POLICY__PERSISTENT_TORRENT_COMPLETED_STAT=true cargo run
And this is where the config option is located (path) in the configuration.
TODO
If this issue is implemented, open new issues for the suggested secondary refactors:
AnnounceHandler
.cc @da2ce7
The text was updated successfully, but these errors were encountered: