Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

roscpp: handle queue lengths of multiple publishers on the same topic better - #450

Closed
xqms wants to merge 4 commits into
ros:indigo-develfrom
xqms:publisher_queue_length
Closed

xqms wants to merge 4 commits into
ros:indigo-develfrom
xqms:publisher_queue_length

Conversation

@xqms

@xqms xqms commented Jun 26, 2014

Copy link
Copy Markdown
Contributor

Currently, all ros::Publisher on a single topic share a message queue with the length of the queue determined by the first publisher. On advertising, TopicManager tries to find a previous Publication for the advertised topic and only adds the callbacks to it, discarding any other options the caller provided (see here).

There are often cases in which this behavior does not make sense. Each "advertiser" of the topic probably provides a reasonable queue length for his messages. So the internal queue length of Publication should probably be the sum of all requested queue lengths.

This PR currently contains a unit test for that case (which currently fails). If you agree that the current behavior is not desired, I will try to bring in a patch for discussion.

…engths

The first publisher on a topic currently determines the publisher
queue length for all publishers from that node on that topic. For this
reason, the test fails currently.
@ros-pull-request-builder

Copy link
Copy Markdown
Member

Can one of the admins verify this patch?

xqms added a commit to xqms/ros_comm that referenced this pull request Jun 29, 2014
…backs

This fixes ros#450 and makes the unit test pass for me. It involves retaining
the requested queue_size in the SubscriberCallbacks struct and counting
the max_queue_ member of Publication up/down appropriately.
@xqms

xqms commented Jun 29, 2014

Copy link
Copy Markdown
Contributor Author

The second commit (4c0035d) makes the unit test pass for me.

@dirk-thomas

Copy link
Copy Markdown
Member

I am not sure if the sum of all publisher queue length arguments is the final solution for the problem. It definitely make the queue bigger and problems less likely to occur though.

But consider the following simple example:

  • one publisher specifying a queue length of 1 and publishing with 10Hz
  • one publisher specifying a queue length of 10 and publishing with 1 Hz

Using a single queue with a length of 11 will not give you the result both publishers would expect.

@xqms

xqms commented Jul 3, 2014

Copy link
Copy Markdown
Contributor Author

one publisher specifying a queue length of 1 and publishing with 10Hz

What exactly do you expect to happen here? The publishing is done in the PollSet thread, as fast as it can. So your example will probably publish at 11Hz from both Publishers, and I would say that is expected.

The only thing you do with the queue length in my experience is turning it up when the publisher thread can't keep up. You never turn it down to limit the rate, since it can't guarantee that. It's not message frequency, it's more like a "load expectation" for the PollSet thread.

Perhaps we would have to change the fixed queue length to something like checking the message frequency and dropping messages if the frequency is too high, but that is a big API change. In my opinion quite impossible at this point ;-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please avoid any non-crucial changes like moving code around.

@dirk-thomas

Copy link
Copy Markdown
Member

One way or the other: neither the max or sum of queue sizes will make the actual behavior match the publishers expectations.

The first publisher "expects" that he can queue messages if the subscriber is several seconds behind. But exactly that is not going to happen when the second publisher is present.

Increasing the queue size to the sum of all sizes will make it less likely to be a problem though. As long as we agree that this is only a partial workaround everything is good.

…backs

This fixes ros#450 and makes the unit test pass for me. It involves retaining
the requested queue_size in the SubscriberCallbacks struct and counting
the max_queue_ member of Publication up/down appropriately.
@xqms xqms changed the title roscpp: properly handle queue lengths of multiple publishers on the same topic roscpp: handle queue lengths of multiple publishers on the same topic better Jul 3, 2014
@xqms

xqms commented Jul 3, 2014

Copy link
Copy Markdown
Contributor Author

Increasing the queue size to the sum of all sizes will make it less likely to be a problem though. As long as we agree that this is only a partial workaround everything is good.

Yes, that's what I meant in my last comment. The queue size constraint does not mix well with the optimization of placing all messages of a topic in one queue and sending them out together.

I still can't think of a better solution if we do not want to get rid of this optimization and keep separate queues (and connections!) for each publisher. But I have removed the claim of doing it 'properly' from the PR title ;-)

@dirk-thomas

Copy link
Copy Markdown
Member

I will wait for our CI server to catch up and when it passes all tests it can be merged.

@dirk-thomas

Copy link
Copy Markdown
Member

@ros-pull-request-builder

Copy link
Copy Markdown
Member

@dirk-thomas

Copy link
Copy Markdown
Member

@xqms One case this patch seems to not address is that the queue_size might be zero which indicates an unlimited queue. Can you pleae update the PR to address that?

@dirk-thomas

Copy link
Copy Markdown
Member

@jonbinney @mikeferguson FYI Can you imagine any cases where this patch might have negative side effects (especially for high level packages)?

@wjwwood

wjwwood commented Jul 10, 2014

Copy link
Copy Markdown
Member

I see the benefit to doing a sum of the queue sizes over the current behavior or doing a max of the queue sizes. However, I am weary of this this change causing increased resource usage in very particular scenarios. I think we should bring in some outside review on this one.

I am +0 to this since the result is still a flawed concept at the expense of a considerable behavior change. This is a good example of why we need to break API with ROS 2.0.

@jonbinney

Copy link
Copy Markdown

@dirk-thomas this could do funny things if people rely on the current behavior (of the queue-size being set with the first advertise). Consider the case where I have 10 different filter classes, each of which take an image and publish a filtered image. Then in some main loop of my node i might have:

switch (some_case) {
case 1:
   filter_class_1.publish_filtered_image(img)
   break;
case 2:
   filter_class_2.publish_filtered_image(img)
   break
. . ..
case 10:
   filter_class_10.publish_filtered_image(img)
}

To make things easy, someone may create a separate publisher in each filter class, but for the same topic and with the same queue_size. If that queue_size is 100, this PR would make the total queue size 1000, which could be a LOT of memory. That said, I don't know of any specific existing ROS package which follows this pattern.

As has been pointed out above, having a separate queue for each publisher seems to be the "Right" thing to do. This would mean that if I have two classes which publish on the same topic, they will behave the same whether i start them in separate roscpp nodes, or if i combine them into a single running roscpp node. (I realize there's an implementation effort with that)

I would consider switching to either sum of queue sizes or switching to separate queues to be pretty big changes, not just quick workarounds.

@dirk-thomas

Copy link
Copy Markdown
Member

@jonbinney This PR is exactly proposing this: using the sum of queue sizes. I am not sure if I understand you correctly - do you consider that to be a quick workaround which you don't like to see?

The increased memory usage is indeed a consequence. I think the increases memory usage is an acceptable trade off compared to a better behavior for a use case like this:

  • publisher A has a queue size of 1 and publishes messages with 1 Hz
  • publisher B has a queue size of 1000 and publishes messages with 1 kHz

Both work well when being used independently but when used together they don't work at all - at least when A is registered before B.

Having separate queues would indeed be the clean solution but due to the necessary refactoring that will very likely be out of scope.

@jonbinney

Copy link
Copy Markdown

My point was just that the increase in memory usage when switching to the sum of queue sizes could be extreme. In my example, switching to sum of queue sizes would multiply memory usage by 10.

@jonbinney

Copy link
Copy Markdown

(factor of 10)

@wjwwood

wjwwood commented Jul 10, 2014

Copy link
Copy Markdown
Member

The other half of the behavior which isn't clear is that if the Publisher's are destroyed then the queue size will go back down, (@dirk-thomas will have to confirm that) so it might have fluctuating queue size which may also be bad if the queue is staying full.

I agree that changing the implementation to have a queue for each publisher is not in the scope of this issue nor something we could do for Indigo.

@jonbinney

Copy link
Copy Markdown

Agreed, we should not try to do the split queues for indigo. My hunch is that it is best to stick with the current behavior for indigo, since even switching to sum of queue sizes could break things (cause lots of memory usage) and since sum of queue sizes isn't the "right" fix anyway.

Edit: "not try"

@dirk-thomas

Copy link
Copy Markdown
Member

The queue size should go down again when publishers are removed: https://github.com/ros/ros_comm/pull/450/files#diff-7e9728328ffc5c67a41abae998836bb2R126

The pending question is to handle queue_size=0 correctly - see my previous comment for this (#450 (comment)).

@dirk-thomas

Copy link
Copy Markdown
Member

Whenever the queue size is reduced I think the rest of the code base won't correctly reduce the size of the queue: e.g. TransportSubscriberLink::enqueueMessage assumes to only remove one message before adding a new one while it will now need to remove old messages until the length of the queue is smaller then the updated max size.

@xqms

xqms commented Jul 11, 2014

Copy link
Copy Markdown
Contributor Author

I seem to have messed up the commit anyway, sorry. Please wait a moment while I fix this mess.

@xqms

xqms commented Jul 11, 2014

Copy link
Copy Markdown
Contributor Author

Okay, now it's ready for review. Sorry for the noise.

@dirk-thomas: I integrated your suggestion with the local variable.

Whenever the queue size is reduced I think the rest of the code base won't correctly reduce the size of the queue: e.g. TransportSubscriberLink::enqueueMessage assumes to only remove one message before adding a new one while it will now need to remove old messages until the length of the queue is smaller then the updated max size.

Do you think throwing away old messages until the queue has the correct size is okay? I was kind of wondering about this and decided not to do it since I wasn't sure. Of course, the 'correct' solution is to throw away any messages published by this publisher, but that's not possible without separate queues.

The old behavior of TransportSubscriberLink::enqueueMessage() was to
remove one message to make room for a new message. Since the queue size
is variable now, change that.
@xqms

xqms commented Jul 11, 2014

Copy link
Copy Markdown
Contributor Author

Here's a commit for dropping messages until the queue size is okay again, in case we decide that is the better approach.

@dirk-thomas

Copy link
Copy Markdown
Member

@ros-pull-request-builder

Copy link
Copy Markdown
Member

@dirk-thomas

Copy link
Copy Markdown
Member

We have discussed this intensively and do see a big chance for regressions. The problem is that the API is designed to allow the Publisher being used as a kind of singleton (per topic) so different code parts access the same instance - sometime just to get access to the publishers properties.

This patch would change the behavior of the API dramatically and will likely cause hard-to-detect regressions in several higher level packages. Also the Python client library would need be updated to provide similar behavior. Due to this uncertainty and big impact the PR won't be merged for the upcoming release of Indigo.

I do understand that the answer is not great for you since it means the current behavior (which is far from being good) will stick around in ROS for longer. But as the maintainer and having to guarantee stability of these core functionalities I am too uncomfortable to merge this change into Indigo.

I am not sure how the upgrade path can look like. We could consider to apply it for the next ROS distribution (j-turtle). Since the development cycle for it is very long it would give us the chance to identify problems and try to address them.

@xqms

xqms commented Jul 18, 2014

Copy link
Copy Markdown
Contributor Author

I see, I understand your point about the regressions.

Is the consensus still that the best solution would be separate queues for each publisher? Maybe we can make that happen in time for j-turtle?

@dirk-thomas

Copy link
Copy Markdown
Member

Even the separate queue approach would break the currently documented behavior and how packages use the API. We could go for it but I am not sure if it is worth the effort.

For the new ROS 2 related development this is being addresses already so we might not spend a lot of time implementing this change for ROS 1.

@xqms

xqms commented Jul 18, 2014

Copy link
Copy Markdown
Contributor Author

Alright, then I'm fine with closing this PR. The issue is easy to work around once you understand what is going on. And we can wait with a proper solution until ROS 2.

@dirk-thomas

Copy link
Copy Markdown
Member

Thank you for your effort anyway.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants