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

Overhaul core Tracker: refactor statistics module #1228

Closed
Tracked by #1181
josecelano opened this issue Jan 30, 2025 · 3 comments · Fixed by #1229
Closed
Tracked by #1181

Overhaul core Tracker: refactor statistics module #1228

josecelano opened this issue Jan 30, 2025 · 3 comments · Fixed by #1229
Assignees
Labels
- Developer - Torrust Improvement Experience Code Cleanup / Refactoring Tidying and Making Neat

Comments

@josecelano
Copy link
Member

josecelano commented Jan 30, 2025

Parent issue: #1181
Relates to: #753

The recently extracted tracker-core package contains a statistics module:

tree packages/tracker-core/src/statistics/
packages/tracker-core/src/statistics/
├── event
│   ├── handler.rs
│   ├── listener.rs
│   ├── mod.rs
│   └── sender.rs
├── keeper.rs
├── metrics.rs
├── mod.rs
├── repository.rs
├── services.rs
└── setup.rs

2 directories, 10 files

The module was already there (inside the core module) when I extracted the new package. However, now that the code is better organized I've realized that the core tracker doesn't send any stats event and all events are related to the UDP or HTTP trackers. That means the logic belongs to the higher level layer.

The Torrust Tracker application has these layers:

                   Main Torrust Tracker
                              |
              Axum HTTP tracker server (`packages\axum-http-tracker`. This hasn't been extracted yet)
                              |
                 HTTP tracker protocol (`packages\http-protocol`)
                              |
                          Core tracker (`packages\tracker-core`)
  • Axum HTTP tracker server layer: the delivery layer. The public layer the application uses to interact with users.
  • HTTP tracker protocol: logic that could be re-used even if we move to a different HTTP framework in the future or if we decide to have different implementations like for example an ActixWeb HTTP tracker server. In fact, there is a Torrust's fork using ActixWeb. Having these two independent layers will allow us in the future to easily move to new frameworks. Frameworks change faster and more often than BitTorrent protocols.

The UDP tracker should have the same layers:

                   Main Torrust Tracker
                              |
             Custom UDP tracker server (`packages\udp-tracker`. This hasn't been extracted yet)
                              |
                 UDP tracker protocol  (`packages\udp-protocol`. This hasn't been extracted yet)
                              |
                          Core tracker (`packages\tracker-core`)

However, for the UDP tracker, at the level of UDP tracker protocol we are also using the aquatic UDP protocol.

I'm going to move the module back to the main lib. I also see the opportunity to split it into two separate modules:

  • Statistics for UDP tracker
  • Statistics for HTTP tracker

I will basically clone the code and move a copy to each server and finally remove the metrics that don't belong to the module. The final goal is let each type of tracker (UDP or HTTP) handle their or stats.

NOTE: I will not change the API endpoint exposing the metrics. That endpoint will remain the same:

https://tracker.torrust-demo.com/api/v1/stats

{
  "torrents": 331388,
  "seeders": 124488,
  "completed": 17472,
  "leechers": 250249,
  "tcp4_connections_handled": 14,
  "tcp4_announces_handled": 14,
  "tcp4_scrapes_handled": 0,
  "tcp6_connections_handled": 0,
  "tcp6_announces_handled": 0,
  "tcp6_scrapes_handled": 0,
  "udp_requests_aborted": 60222,
  "udp_requests_banned": 7219182,
  "udp_banned_ips_total": 14889,
  "udp_avg_connect_processing_time_ns": 187574,
  "udp_avg_announce_processing_time_ns": 5557430,
  "udp_avg_scrape_processing_time_ns": 188447,
  "udp4_requests": 40682829,
  "udp4_connections_handled": 6475337,
  "udp4_announces_handled": 24841768,
  "udp4_scrapes_handled": 590641,
  "udp4_responses": 33462658,
  "udp4_errors_handled": 1541103,
  "udp6_requests": 0,
  "udp6_connections_handled": 0,
  "udp6_announces_handled": 0,
  "udp6_scrapes_handled": 0,
  "udp6_responses": 0,
  "udp6_errors_handled": 0
}

In the future I will like to change it to this:

{
  "torrent_metrics": {
    "torrents": 331388,
    "seeders": 124488,
    "completed": 17472,
    "leechers": 250249,
  }
  "http_tracker_metrics": {
    "tcp4_connections_handled": 14,
    "tcp4_announces_handled": 14,
    "tcp4_scrapes_handled": 0,
    "tcp6_connections_handled": 0,
    "tcp6_announces_handled": 0,
    "tcp6_scrapes_handled": 0,
  }
  "udp_tracker_metrics": {
    "requests_aborted": 60222,
    "requests_banned": 7219182,
    "banned_ips_total": 14889,
    "avg_connect_processing_time_ns": 187574,
    "avg_announce_processing_time_ns": 5557430,
    "avg_scrape_processing_time_ns": 188447,
    "udp4": {
      "requests": 40682829,
      "connections_handled": 6475337,
      "announces_handled": 24841768,
      "scrapes_handled": 590641,
      "responses": 33462658,
      "errors_handled": 1541103,
    }
    "udp6": {
      "requests": 0,
      "connections_handled": 0,
      "announces_handled": 0,
      "scrapes_handled": 0,
      "responses": 0,
      "errors_handled": 0
    }
}

And also rename the metrics to follow Prometheus recommendations.

Image

cc @da2ce7

@josecelano josecelano added - Developer - Torrust Improvement Experience Code Cleanup / Refactoring Tidying and Making Neat labels Jan 30, 2025
@josecelano josecelano self-assigned this Jan 30, 2025
@josecelano josecelano mentioned this issue Jan 30, 2025
26 tasks
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
This is the frist step in a bigger refactor. We will move statistics out
of the tracker-core package into new packages. Statistics are not
related to the tracker-core or enven handled there. That logic belongs
to upper layers.
@josecelano josecelano linked a pull request Jan 31, 2025 that will close this issue
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
…in lib

The statistics are only used at the higher levels: UDP and HTTP tracker.

We will move them to new packages.
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
…in lib

The statistics are only used at the higher levels: UDP and HTTP tracker.

We will move them to new packages.
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
Parallel change, step 1:

1. [x] Start using HTTP Tracker Core Stats
2. [ ] Start using UDP  Tracker Core Stats
3. [ ] Get metrics from HTTP and UDP Tracker Core Stats
4. [ ] Remove deprecate unified HTTP and UDP stats.
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
Stats have been splited into HTTP and UDP stats.

Parallel change, step 1:

1. [x] Start using HTTP Tracker Core Stats
2. [ ] Start using UDP  Tracker Core Stats
3. [ ] Get metrics from HTTP and UDP Tracker Core Stats
4. [ ] Remove deprecate unified HTTP and UDP stats.
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
Parallel change, step 2:

1. [x] Start using HTTP Tracker Core Stats
2. [x] Start using UDP  Tracker Core Stats
3. [ ] Get metrics from HTTP and UDP Tracker Core Stats
4. [ ] Remove deprecate unified HTTP and UDP stats.
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
…tats

Stats have been splited into HTTP and UDP stats.

Parallel change, step 3:

1. [x] Start using HTTP Tracker Core Stats
2. [x] Start using UDP  Tracker Core Stats
3. [x] Get metrics from HTTP and UDP Tracker Core Stats
4. [ ] Remove deprecated unified HTTP and UDP stats.
josecelano added a commit to josecelano/torrust-tracker that referenced this issue Jan 31, 2025
Stats have been split into HTTP and UDP stats.

Parallel change, step 4:

1. [x] Start using HTTP Tracker Core Stats
2. [x] Start using UDP  Tracker Core Stats
3. [x] Get metrics from HTTP and UDP Tracker Core Stats
4. [x] Remove deprecated unified HTTP and UDP stats.
@josecelano
Copy link
Member Author

josecelano commented Jan 31, 2025

Hi @da2ce7, I've finally introduced a new layer in the app architecture because I didn't fine a good place to put the stats:

HTTP tracker:

                   Main Torrust Tracker
                              |
              Axum HTTP tracker server (`packages\axum-http-tracker`. This hasn't been extracted yet)
                              |
                    HTTP tracker core  (`src\packages\http_tracker_core`)
                              |
                 HTTP tracker protocol (`packages\http-protocol`)
                              |
                          Core tracker (`packages\tracker-core`)
  • Axum HTTP tracker server: the top layer that exposes the user's service with a framework. Basically Axum stuff. It could be another framework in the future.
  • HTTP tracker core: extra logic (like statistics) that might be useful for other implementations regardless what HTTP frameworks you are using.
  • HTTP tracker protocol: mandatory logic needed by any HTTP tracker implementation (official specification)
  • Core tracker: announce and scrape logic without delivery mechanism.

Same for the UDP tracker.

@josecelano
Copy link
Member Author

I've also created a new temporary dir (src/packages) with modules I will extract intro crates (/packages).

@josecelano
Copy link
Member Author

I've also created sub-issues for extracting the packages in #753

josecelano added a commit that referenced this issue Jan 31, 2025
fd8b57a refactor: [#1228] remove deprecated unified HTTP and UDP stats (Jose Celano)
5576938 refactor: [#1228] get metrics from HTTP and UDP Tracker Core Stats (Jose Celano)
f33665d refactor: [#1228] start using the udp tracker stats (Jose Celano)
5f08b2e refactor: [#1228] start using the http tracker stats (Jose Celano)
39cbeda refactor: [#1228] add new UDP and HTTP stats services to AppContainer (Jose Celano)
700c912 docs: update tracker core docs (Jose Celano)
f99534a refactor: [#1228] split statistics mod into UDO and HTTP statistics (Jose Celano)
9318842 refactor: [#1228] move statistics back from tracker-core to main lib (Jose Celano)
0ad88b6 refactor: [#1228] move type from tracker-core to main lib (Jose Celano)

Pull request description:

  Overhaul core Tracker: refactor statistics module.

  Statistics have been split into three parts:

  - HTTP stats
  - UDP stats
  - API stats: the API uses both HTTP metrics and UDP metrics.

  I have created a temporary directory (`src/packages/`) with the new packages. We need to create crates for those packages and move them to the `src/packages/` dir.

  I didn't want to do that in this PR.

  ```
  $ tree src/packages/
  src/packages/
  ├── http_tracker_core
  │   ├── mod.rs
  │   └── statistics
  │       ├── event
  │       │   ├── handler.rs
  │       │   ├── listener.rs
  │       │   ├── mod.rs
  │       │   └── sender.rs
  │       ├── keeper.rs
  │       ├── metrics.rs
  │       ├── mod.rs
  │       ├── repository.rs
  │       ├── services.rs
  │       └── setup.rs
  ├── mod.rs
  ├── tracker_api_core
  │   ├── mod.rs
  │   └── statistics
  │       ├── metrics.rs
  │       ├── mod.rs
  │       └── services.rs
  └── udp_tracker_core
      ├── mod.rs
      └── statistics
          ├── event
          │   ├── handler.rs
          │   ├── listener.rs
          │   ├── mod.rs
          │   └── sender.rs
          ├── keeper.rs
          ├── metrics.rs
          ├── mod.rs
          ├── repository.rs
          ├── services.rs
          └── setup.rs

  9 directories, 27 files
  ```

  The app layers are a little bit different from what I described initially in the issue:

  Initial design:

  ```
                     Main Torrust Tracker
                                |
                Axum HTTP tracker server (`packages\axum-http-tracker`. This hasn't been extracted yet)
                                |
                   HTTP tracker protocol (`packages\http-protocol`)
                                |
                            Core tracker (`packages\tracker-core`)
  ```

  Final design:

  ```
                     Main Torrust Tracker
                                |
                Axum HTTP tracker server (`packages\axum-http-tracker`. This hasn't been extracted yet)
                                |
                      HTTP tracker core  (`src\packages\http_tracker_core`)
                                |
                   HTTP tracker protocol (`packages\http-protocol`)
                                |
                            Core tracker (`packages\tracker-core`)
  ```

  I din't want to put stats in the `packages\http-protocol` because they are no part of the official HTTP tracker protocols. And I didn't want to put it in the axum server because it's code that can be re-used in other server implementations (using other frameworks).

ACKs for top commit:
  josecelano:
    ACK fd8b57a

Tree-SHA512: 75adfaef2bb7f356b2a135183f9eb0306e91fa6dce3e4195ed21dda261139267bcc7cbb7589093dd67c2800ddce5aa172a7ebba19870794436db53890c5a32ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- Developer - Torrust Improvement Experience Code Cleanup / Refactoring Tidying and Making Neat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant