Repository navigation
Add get_publication_matched_status and get_subscription_matched_status - #473
Open
johannesschrimpf wants to merge 3 commits into
Open
johannesschrimpf wants to merge 3 commits into
johannesschrimpf wants to merge 3 commits into
Conversation
A DataWriter could not be asked how many DataReaders it is matched with. The PUBLICATION_MATCHED status existed as status events only, and the status channel holds four events and drops the rest when nobody reads them, so a count kept from the events can be wrong with no way to correct it. The getter was a commented-out todo!(). The RTPS Writer now stores its total and current match counts in two atomics that it shares with the DataWriter, at the two places where it sends the PublicationMatched event. get_publication_matched_status() reads them and reports each with its change since the previous call on that DataWriter. It takes no lock, so it can be called before every write, for example to skip serializing a sample that nobody would receive. The status events are unchanged. The specification's last_subscription_handle is left out, like the other handles: the event names the reader. Assisted-by: Claude Fable 5.2
The reader side of the previous commit. The RTPS Reader stores its total and current match counts where it sends the SubscriptionMatched event, and DataReader and SimpleDataReader, with_key and no_key, read them with get_subscription_matched_status(). The counts do not pass through the status channel, so they are right when it has dropped events. ReaderIngredients gets a field for the shared counts, which the test fixtures that build a Reader fill with a default. Assisted-by: Claude Fable 5.2
The matched-status getters loaded the total and the current count one after the other, so a read that raced with a match could return a pair that never existed, such as a total of 0 with a current count of 1. Two threads that read the status of one DataWriter or DataReader could also disturb each other: one that had read older counts could store them as "seen" after the other had stored newer ones. It was then told that the total had gone down, and the next read reported the match again. Keep each pair behind a lock, and hold the lock on the "seen" pair while the counts are read, so that concurrent reads take turns. The getters' documentation said that the call takes no lock. It now says only that it is cheap, and with which QoS a DataWriter may skip a write that nobody would receive. Assisted-by: Claude Fable 5.2 Assisted-by: Astra GPT-6.2
johannesschrimpf
marked this pull request as ready for review
November 19, 2026 16:51
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We are porting a set of rclcpp nodes to ros2-client on an i.MX6, and one thing we kept missing in RustDDS is a way to ask a DataWriter how many readers it is matched with. This PR adds that, and the same for readers.
Some background on why we care. One of our nodes publishes about 1500 samples a second, and most of them go to topics that nobody is subscribed to most of the time. In C++ that was nearly free: rclcpp (with intra-process communication on) does not do the DDS write when there is no subscriber outside the process. It can do that because rmw_fastrtps answers
get_subscription_count()from a set that Fast DDS'son_publication_matchedkeeps per writer.RustDDS reports matches as status events (see #16), and that works well when something follows the stream. What we were missing is a way to simply ask for the current number. The status channel of a DataWriter or DataReader keeps four events and drops further ones while nobody reads them, so a count built from the events is only right as long as the stream is drained. ros2-client's
get_subscription_countis built that way, from the participant's status events.get_publication_matched_statusis already there as a commented-outtodo!(), so this PR fills that in.We ended up polling the discovery database, with
DomainParticipant::discovered_readers()anddiscovered_writers(), which clone every discovered endpoint. That worked, but it cost about 54 MB/s of extra memory reads on this board, and it is still a poll.You will probably not see any of this on a desktop. It shows up on a small CPU with a busy memory bus, several processes, and a lot of topics that are published fast but only subscribed now and then.
This PR adds the missing piece at the source: a DataWriter and a DataReader can simply be asked for their count. The ros2-client side then uses it.
Needed for Atostek/ros2-client#85.
What changes
First commit:
DataWriter::get_publication_matched_status(), forwith_keyandno_key. The RTPS Writer stores its total and current match counts in a small object that it shares with the DataWriter, at the two places where it sends thePublicationMatchedevent. The getter returns aPublicationMatchedStatus { total, current }, each aCountWithChangewhose change is counted since the previous call on that DataWriter, as the specification describes for theget_*_statusoperations (DDS 1.4, 2.2.4.1). The counts do not pass through the status channel, so they are right when it is full.Second commit: the same for readers.
get_subscription_matched_status()onDataReaderandSimpleDataReader,with_keyandno_key, returning aSubscriptionMatchedStatus.Third commit: the two counts are one snapshot. The total and the current count are stored and read together, behind a lock that is otherwise taken only when a match changes, so a getter never returns a pair that did not exist. Several threads may call the getter of one endpoint: they take turns, so none is told that the total went down and each match is reported once.
The specification's
last_subscription_handleandlast_publication_handleare left out, like the other handles; the events name the endpoint. "Matched" means what it means in the events: this side has matched the remote endpoint. That makes this independent of #470, which reports when the remote side has matched too.Compatibility
WriterIngredientsandReaderIngredients(pub(crate)) get a field. The commented-out placeholder forget_publication_matched_statusis gone.ReaderIngredients.WriterIngredients, so whichever of the two is merged second needsmatched_counts: Default::default(),in that fixture. I will add it here on a rebase if Do not wait for acknowledgments from best-effort readers in the heartbeat tick #472 goes first.Tests
cargo test --tests -- --test-threads=1passes with and without--features=security(724 and 734 unit tests, plus the integration tests).cargo fmt -- --checkon nightly andcargo docare clean, and clippy reports nothing in the added lines. The new doc examples were compiled, not run. I have not run the MSRV check on 1.88.0 or the macOS job.Follow-up
The ros2-client side is Atostek/ros2-client#85:
get_subscription_countandget_publisher_countread these getters instead of the maps its spinner keeps from the participant events, andpublish()skips a write that nobody would receive.