Skip to content

Overhaul stats: Improve events package (part 3) - #1486

Merged
josecelano merged 9 commits into
torrust:developfrom
josecelano:1485-overhaul-stats-extract-events-package-part-3
Apr 29, 2025
Merged

josecelano merged 9 commits into
torrust:developfrom
josecelano:1485-overhaul-stats-extract-events-package-part-3

Conversation

@josecelano

@josecelano josecelano commented Apr 28, 2025 •

Copy link
Copy Markdown
Member

Continuation of #1483

Subtasks

  • Receiver trait.
  • Decouple Sender and Receiver traits from the tokio broadcast implementation. Wrapper for SendError and RecvError.
  • Return an Option<Arc<...>> instead of an Arc<Option<...>> in the EventBus implementation.
  • Replace Arc<Box<dyn Sender>> with Arc<dyn Sender> to avoid double allocations.
  • Add unit tests.

@josecelano josecelano linked an issue Apr 28, 2025 that may be closed by this pull request
@josecelano josecelano self-assigned this Apr 28, 2025
@josecelano josecelano added Code Cleanup / Refactoring Tidying and Making Neat - Developer - Torrust Improvement Experience labels Apr 28, 2025
@josecelano
josecelano requested a review from da2ce7 April 28, 2025 18:08
@codecov

codecov Bot commented Apr 28, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 90.75145% with 16 lines in your changes missing coverage. Please review.

Project coverage is 84.32%. Comparing base (b6ed3f3) to head (d98d93f).
Report is 10 commits behind head on develop.

Files with missing lines Patch % Lines
packages/events/src/receiver.rs 0.00% 6 Missing ⚠️
packages/events/src/broadcaster.rs 94.91% 1 Missing and 2 partials ⚠️
packages/events/src/sender.rs 0.00% 3 Missing ⚠️
packages/events/src/bus.rs 97.91% 0 Missing and 1 partial ⚠️
...http-tracker-core/src/statistics/event/listener.rs 66.66% 1 Missing ⚠️
.../udp-tracker-core/src/statistics/event/listener.rs 66.66% 1 Missing ⚠️
...dp-tracker-server/src/statistics/event/listener.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1486      +/-   ##
===========================================
- Coverage    84.36%   84.32%   -0.04%     
===========================================
  Files          250      251       +1     
  Lines        19719    19821     +102     
  Branches     19719    19821     +102     
===========================================
+ Hits         16636    16715      +79     
- Misses        2779     2798      +19     
- Partials       304      308       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@josecelano
josecelano force-pushed the 1485-overhaul-stats-extract-events-package-part-3 branch from 5cded47 to 8f5b57e Compare April 29, 2025 07:43
@josecelano
josecelano force-pushed the 1485-overhaul-stats-extract-events-package-part-3 branch from 4e8d733 to d98d93f Compare April 29, 2025 10:32
@josecelano
josecelano marked this pull request as ready for review April 29, 2025 10:32
@josecelano

Copy link
Copy Markdown
Member Author

ACK d98d93f

@josecelano
josecelano merged commit ef6c91d into torrust:develop Apr 29, 2025

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a post-merge review of the change from #1486 as it stands at a94c3ced8 on the default branch.

What still runs: the Receiver trait and the crate-owned SendError and RecvError types in packages/events, the broadcast-channel implementations of Sender and Receiver with the conversions from the tokio errors, EventBus::sender() returning Option<Arc<dyn Sender>>, EventBus::receiver() returning Box<dyn Receiver>, and the event::{sender, receiver, bus} aliases in http-core, udp-core and udp-server. Every statistics listener and the UDP banning listener consume events through this trait, so its contract reaches ban handling.

[Minor] The documented outcomes of Sender::send do not match the only implementation. packages/events/src/sender.rs:19-22 says None means the event was not sent and gives no active receivers as an example, but Broadcaster::send (packages/events/src/broadcaster.rs:33-35) always returns Some, and with no subscribed receiver it returns Some(Err(SendError(event))). The change's own test it_should_fail_when_trying_tos_send_with_no_subscribers (packages/events/src/broadcaster.rs:110-119) asserts exactly that. Concretely, EventBus::default().sender().unwrap().send(e).await before any receiver() call yields Some(Err(..)), and its Display (packages/events/src/sender.rs:33-37) reads channel closed although the bus and its sender are alive. A consumer that matches None to detect that nobody is listening never takes that branch. The disabled case is already expressed one level up, by EventBus::sender() returning None (packages/events/src/bus.rs:56-61). Smallest fix: document Some(Err(SendError(event))) as no receiver subscribed, with the event handed back; document None as reserved for implementations that drop the event on purpose, which the broadcaster never does; and have the Display text say no active receivers.

[Minor] Receiver::recv (packages/events/src/receiver.rs:12) does not say it must be cancel safe, and every listener depends on that. packages/udp-server/src/banning/event/listener.rs:93, packages/udp-server/src/statistics/event/listener.rs:81, packages/udp-core/src/statistics/event/listener.rs:80, packages/http-core/src/statistics/event/listener.rs:83, packages/tracker-core/src/statistics/event/listener.rs:113 and :147, and packages/swarm-coordination-registry/src/statistics/event/listener.rs:65 all race receiver.recv() against cancellation_token.cancelled() in a biased tokio::select!. Each loop iteration creates the recv future and drops it whenever cancellation wins, and the listener then drains with try_recv. An implementation that takes the next event before its returned future completes, either in the synchronous part of recv or before a later await point, loses that event on cancellation, and the drain never sees it. On the banning path that is one uncounted connection-cookie error. The broadcast implementation (packages/events/src/broadcaster.rs:41-43) is cancel safe because tokio documents broadcast::Receiver::recv as cancel safe, so nothing is lost at this head. The test double at packages/udp-server/src/banning/event/listener.rs:190-191 already has a comment working the rule out again. Smallest fix: add a # Cancel safety section to Receiver::recv saying that dropping the returned future before it completes must not consume an event.

[Nit] The description says the traits were decoupled from the tokio broadcast implementation, but Broadcaster::subscribe (packages/events/src/broadcaster.rs:25) still returns tokio::sync::broadcast::Receiver<Event>, and EventBus::broadcaster is a public field (packages/events/src/bus.rs:34). Any package can therefore call bus.broadcaster.subscribe() and bypass the Receiver trait. At this head only tests do so (packages/udp-server/src/handlers/scrape.rs:403, packages/udp-server/src/handlers/error.rs:254). Smallest fix: return Box<dyn Receiver<Event = Event>> from subscribe, as EventBus::receiver does, or make it pub(crate), and make the field private.

Checked and found sound:

  • The From conversions map Closed and Lagged(n) one to one (packages/events/src/broadcaster.rs:54-77).
  • self.recv() inside the trait's recv (packages/events/src/broadcaster.rs:42) calls tokio's inherent method, not itself, because inherent methods take precedence over trait methods.
  • Option<Arc<dyn Sender>> removes the double allocation the description promised.
  • Every listener stops on Closed and warns and continues on Lagged.
  • The UDP-server listeners subscribe when bootstrap builds their runners (src/app.rs:477-481), before the UDP instances start, and the UDP instances stop before those listeners at shutdown. So ignoring the result of send at the call sites, for example packages/udp-server/src/server/launcher.rs:465, loses no event while publishers run.
  • At merge, only tests needed the time feature this change added to the normal tokio dependency. It is now used in production by packages/events/src/drain.rs:16.

This branch was previously deployed

1 inactive deployment
coverage — d98d93f3 Deployed Apr 29, 2025 by josecelano
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 this pull request may close these issues.

Overhaul stats: Extract events package (part 3)

2 participants