Skip to content

Overhaul stats: Handle lagging in event listeners channels #1449

Closed

Description

@josecelano

Relates to: #1385

When I run the tracker in the demo server, stats stop working after a while, and I get this error in logs:

tracker  | 2025-04-14T08:43:30.045862Z ERROR torrust_udp_tracker_server::statistics::event::listener: Error receiving udp tracker server event: Lagged(9)

The problem was produced when I switched to Tokio broadcast channels to allow multiple event listeners.

Lagging is not handled in the event listener. For example, the UDP tracker server event listener:

use tokio::sync::broadcast;
use torrust_tracker_clock::clock::Time;

use super::handler::handle_event;
use crate::event::Event;
use crate::statistics::repository::Repository;
use crate::CurrentClock;

pub async fn dispatch_events(mut receiver: broadcast::Receiver<Event>, stats_repository: Repository) {
    loop {
        match receiver.recv().await {
            Ok(event) => handle_event(event, &stats_repository, CurrentClock::now()).await,
            Err(e) => {
                tracing::error!("Error receiving udp tracker server event: {:?}", e);
                break;
            }
        }
    }
}

The channel capacity is 1024, so when unhandled events reach the channel capacity, we receive an error in the loop that breaks the loop, stopping the listener.

Since:

  • We only have one listener now to update metrics.
  • I want metrics to be exact.

As a temporary solution, I will increase the channel capacity to have the same effect as when we used a mspc channel.

I will also continue the loop when the listener receives that error.

IMPORTANT: The metrics will NOT be exact if there are lagged events.

cc @da2ce7

Alternative solution 1

Restore the mspc channel for metrics.

Pros:

  • Precise metrics

Cons:

  • Slower request processing? The request has to wait until the metrics are updated.
  • We will need two channels: one mpsc channel for metrics and a broadcast channel for generic listeners.

NOTE: The main reason I wanted to have multiple listeners is that I want third-party software to be able to build their own metrics from tracker events (by registering their event listeners).

Activity

  1. added
    Bug
    on Apr 14, 2025
  2. self-assigned this
    on Apr 14, 2025
  3. added theissue type on Apr 14, 2025
  4. added 2 commits that reference this issue on Apr 14, 2025
  5. josecelano commented on Apr 14, 2025

    @josecelano
    MemberAuthor
  6. added a commit that references this issue on Apr 14, 2025
  7. josecelano commented on Apr 14, 2025

    @josecelano
    MemberAuthor

    The temporary solution is working, at least for the demo tracker. The metrics are working more than 2 hours after the deployment:

    Image

    And there is no error in logs related to the broadcast channel.

  8. changed the title [-]Overhaul stats: handle lagging in event listeners channels[/-] [+]Overhaul stats: Handle lagging in event listeners channels[/+] on Apr 15, 2025
  9. added
    Needs Feedback
    on Apr 15, 2025
  10. josecelano commented on May 5, 2025

    @josecelano
    MemberAuthor

    In a meeting today, @da2ce7 and I decided to leave it as it is. If we see a performance problem, we can switch to the crossbeam channel in the future. We think losing some events in a high load scenario is not a big problem. You would have other bigger problems, like timeouts for the requests, and in general, metrics are used to make sure the tracker is running fine. Even private indexes that have more advanced requirements in terms of tracker metrics would accept some missing requests in the counters when the tracker is running overloaded.

    On the other hand, we don't expect more than one listener. The idea of having multiple listeners is just to allow third-party users of our crates to build their own metrics, not to add many listeners.

  11. josecelano commented on May 5, 2025

    @josecelano
    MemberAuthor

    In a meeting today, @da2ce7 and I decided to leave it as it is. If we see a performance problem, we can switch to the crossbeam channel in the future. We think losing some events in a high load scenario is not a big problem. You would have other bigger problems, like timeouts for the requests, and in general, metrics are used to make sure the tracker is running fine. Even private indexes that have more advanced requirements in terms of tracker metrics would accept some missing requests in the counters when the tracker is running overloaded.

    On the other hand, we don't expect more than one listener. The idea of having multiple listeners is just to allow third-party users of our crates to build their own metrics, not to add many listeners.

    Decided not to change it for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugNeeds Feedback

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions