New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use rw_lock to protect mcap metadata lists. #1561
Use rw_lock to protect mcap metadata lists. #1561
Conversation
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fujitatomoya Thanks for trying to fix this issue with race condition.
At first glance, the fix is trivial. However, we can do way better.
- We don't need to use
shared_mutex
because there are no situations in rosbag2 when write() method could be called from multiple threads and unlikely would be because- it is protected with mutex on upper
rosbag2_cpp::writer::write(..)
method for the cases when we are writing without cache. - We are using
SingleThreadedExecutor
which is processing all subscription callbacks one by one sequentially in the one thread. - When double-buffer cache is enabled we are using another method
rosbag2/rosbag2_storage_mcap/src/mcap_storage.cpp
Lines 784 to 790 in b8a9b06
void MCAPStorage::write( const std::vector<std::shared_ptr<const rosbag2_storage::SerializedBagMessage>> & msgs) { for (const auto & msg : msgs) { write(msg); } }
- it is protected with mutex on upper
- And here we can do optimization. It will be more efficient to lock mutex once before we iterate through the vector of messages and call the write method for each message. Other API calls as
create_topic(..)
orremove_topic(..)
have lower priority and could wait until we will finish dumping the vector of messages from our double-buffer cache to the storage. It will be more elegant to create a new private method for writing one message without lock. We can name itwrite_lock_free(msg)
orwrite_locked(msg)
and lock mutex in public write(..) method right before calling newly createdwrite_lock_free(msg)
and in the
rosbag2/rosbag2_storage_mcap/src/mcap_storage.cpp
Lines 784 to 790 in b8a9b06
void MCAPStorage::write( const std::vector<std::shared_ptr<const rosbag2_storage::SerializedBagMessage>> & msgs) { for (const auto & msg : msgs) { write(msg); } }
right before for loop.
@MichaelOrlov thanks!
I see, whole point is
currently true. minor concern was if we support
okay, can do that.
|
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
@MichaelOrlov requesting another look! thanks! |
@fujitatomoya Thanks for the followup. As reagrds.
Yes. Currently using double-buffer cache gives the same performance gain as a multithreaded executor but in two threads and decouples writing to the storage and transport layer. Which is very important due to the possible delays with filesystem or DB operations. rosbag2/rosbag2_cpp/src/rosbag2_cpp/writer.cpp Lines 104 to 108 in edda376
And would need to reconsider and make changes on that level as well. Switching to shared_mutex on the rosbag2_storage_mcap layer if would needed in the future will not cause difficulties even with backporting since the whole changes in the .cpp file only and will be API/ABI compatible.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fujitatomoya Thanks. LGTM with green CI.
Gist: https://gist.githubusercontent.com/MichaelOrlov/a06b14d20610f8f8cc8007db7fc61efe/raw/06daf70cbe74c9b1eb6715c512d655cd101f648f/ros2.repos |
https://github.com/Mergifyio backport iron humble |
✅ Backports have been created
|
* use rw_lock to protect mcap metadata lists. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> * introduce MCAPStorage::write_lock_free private method. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> --------- Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> (cherry picked from commit 90d1da8) # Conflicts: # rosbag2_storage_mcap/src/mcap_storage.cpp
* use rw_lock to protect mcap metadata lists. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> * introduce MCAPStorage::write_lock_free private method. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> --------- Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> (cherry picked from commit 90d1da8) # Conflicts: # rosbag2_storage_mcap/src/mcap_storage.cpp
…#1567) * Use rw_lock to protect mcap metadata lists. (#1561) * use rw_lock to protect mcap metadata lists. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> * introduce MCAPStorage::write_lock_free private method. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> --------- Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> (cherry picked from commit 90d1da8) # Conflicts: # rosbag2_storage_mcap/src/mcap_storage.cpp * Resolve merge conflicts Signed-off-by: Michael Orlov <michael.orlov@apex.ai> --------- Signed-off-by: Michael Orlov <michael.orlov@apex.ai> Co-authored-by: Tomoya Fujita <Tomoya.Fujita@sony.com> Co-authored-by: Michael Orlov <michael.orlov@apex.ai>
…1566) * Use rw_lock to protect mcap metadata lists. (#1561) * use rw_lock to protect mcap metadata lists. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> * introduce MCAPStorage::write_lock_free private method. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> --------- Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> (cherry picked from commit 90d1da8) # Conflicts: # rosbag2_storage_mcap/src/mcap_storage.cpp * Resolve merge conflicts Signed-off-by: Michael Orlov <michael.orlov@apex.ai> * Suppress warning STL4015: The std::iterator class template is deprecated in C++17 Signed-off-by: Michael Orlov <michael.orlov@apex.ai> --------- Signed-off-by: Michael Orlov <michael.orlov@apex.ai> Co-authored-by: Tomoya Fujita <Tomoya.Fujita@sony.com> Co-authored-by: Michael Orlov <michael.orlov@apex.ai>
) (ros2#1567) * Use rw_lock to protect mcap metadata lists. (ros2#1561) * use rw_lock to protect mcap metadata lists. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> * introduce MCAPStorage::write_lock_free private method. Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> --------- Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com> (cherry picked from commit 90d1da8) # Conflicts: # rosbag2_storage_mcap/src/mcap_storage.cpp * Resolve merge conflicts Signed-off-by: Michael Orlov <michael.orlov@apex.ai> --------- Signed-off-by: Michael Orlov <michael.orlov@apex.ai> Co-authored-by: Tomoya Fujita <Tomoya.Fujita@sony.com> Co-authored-by: Michael Orlov <michael.orlov@apex.ai>
This pull request has been mentioned on ROS Discourse. There might be relevant details there: https://discourse.ros.org/t/ros-2-meeting-minutes-2024-02-15/36221/1 |
address #1542