Skip to content

Add get_publication_matched_status and get_subscription_matched_status - #473

Open
johannesschrimpf wants to merge 3 commits into
Atostek:masterfrom
johannesschrimpf:feat/matched-status-getters
Open

johannesschrimpf wants to merge 3 commits into
Atostek:masterfrom
johannesschrimpf:feat/matched-status-getters

Conversation

@johannesschrimpf

@johannesschrimpf johannesschrimpf commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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's on_publication_matched keeps 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_count is built that way, from the participant's status events. get_publication_matched_status is already there as a commented-out todo!(), so this PR fills that in.

We ended up polling the discovery database, with DomainParticipant::discovered_readers() and discovered_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(), for with_key and no_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 the PublicationMatched event. The getter returns a PublicationMatchedStatus { total, current }, each a CountWithChange whose change is counted since the previous call on that DataWriter, as the specification describes for the get_*_status operations (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() on DataReader and SimpleDataReader, with_key and no_key, returning a SubscriptionMatchedStatus.

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_handle and last_publication_handle are 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

Tests

  • Unit tests for the count bookkeeping, and for the Writer and the Reader: an endpoint is counted once however often it is announced, one with incompatible QoS is not counted, a lost endpoint or participant lowers the current count and leaves the total, and the counts are right for more matches than the status channel holds.
  • An integration test with two participants takes both getters through a match and the loss of the reader without reading a status event. It fails when the DataWriter and the Writer do not share the counts.

cargo test --tests -- --test-threads=1 passes with and without --features=security (724 and 734 unit tests, plus the integration tests). cargo fmt -- --check on nightly and cargo doc are 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_count and get_publisher_count read these getters instead of the maps its spinner keeps from the participant events, and publish() skips a write that nobody would receive.

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
johannesschrimpf marked this pull request as ready for review November 19, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant