Skip to content
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

Make snapshot writing into a new file each time it is triggered #1842

Merged
merged 7 commits into from
Nov 1, 2024

Conversation

MichaelOrlov
Copy link
Contributor

@MichaelOrlov MichaelOrlov commented Oct 26, 2024

This PR will make snapshot writing into a new file each time it is triggered.

Note. Snapshot now becomes a blocking call and mutually exclusive with the writer::write(message) method to avoid race conditions, i.e., blocking the same writer_mutex_.
The set_data_ready() method from #1839 is not needed since "snapshot" has now become a blocking call; therefore, there are no race conditions, and we will not dump data from the cyclic buffer twice because data_ready will be settled false after the first dump with message_cache_->notify_data_ready();.

void CircularMessageCache::swap_buffers()
{
std::lock_guard<std::mutex> producer_lock(producer_buffer_mutex_);
// Swap buffers only if data is ready. Data not ready when we are calling flushing on exit and
// we should not dump buffer on exit if snapshot has not been triggered.
if (data_ready_) {
std::lock_guard<std::mutex> consumer_lock(consumer_buffer_mutex_);
consumer_buffer_->clear();
std::swap(producer_buffer_, consumer_buffer_);
data_ready_ = false;
}
}

However, we still need to call split_bagfile_local(true) to trigger callbacks and open a new storage file.

This PR could be backported. There are no API/ABI breaking changes in it.

- Note. Snapshot now became a blocking call and mutually exclusive with
writer::write(message) method to avoid race conditions.
i.e. blocking the same writer_mutex_

Signed-off-by: Michael Orlov <[email protected]>
@MichaelOrlov MichaelOrlov marked this pull request as ready for review October 26, 2024 22:45
@MichaelOrlov MichaelOrlov requested a review from a team as a code owner October 26, 2024 22:45
@MichaelOrlov MichaelOrlov requested review from gbiggs and james-rms and removed request for a team October 26, 2024 22:45
@MichaelOrlov MichaelOrlov requested review from christophebedard and fujitatomoya and removed request for gbiggs and james-rms October 26, 2024 23:12
@MichaelOrlov MichaelOrlov self-assigned this Oct 26, 2024
@MichaelOrlov MichaelOrlov added the enhancement New feature or request label Oct 26, 2024
@cmuehlbacher
Copy link
Contributor

Thank you for all the work on this feature.

Looks very promising. And great news that it can be backported.

As far as I can tell there should be no data races in there although, there will be several calls of write_messages function with an empty array.

There is only one small remark:
The test snapshot_writes_to_new_file_with_file_compression closes the bag file after taking a snapshot while snapshot_writes_to_new_file does not. It would make sense to have both tests behave similarly.

Copy link
Member

@christophebedard christophebedard left a comment

Choose a reason for hiding this comment

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

from what I understand, the tests write messages into the cache, then trigger a single snapshot, and then validate everything.

I believe we should test that this works with more than 1 snapshot. Could we check the metadata too, e.g., starting_time, duration, and message_count too?

@christophebedard
Copy link
Member

Note. Snapshot now becomes a blocking call and mutually exclusive with the writer::write(message) method to avoid race conditions, i.e., blocking the same writer_mutex_.

I think this makes sense.

Copy link
Contributor

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

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

lgtm with green CI, just one minor comment that does not block this PR.

@MichaelOrlov
Copy link
Contributor Author

@christophebedard I added a check for metadata in the test. To cover the case if snapshot could be called twice - I created a separate test case.

@cmuehlbacher As regards

As far as I can tell there should be no data races in there although, there will be several calls of write_messages function with an empty array.

No it is not. I double-checked in debuuger and we have constraints in tests that write messages will be called only once per snapshot.

As regards:

The test snapshot_writes_to_new_file_with_file_compression closes the bag file after taking a snapshot while > snapshot_writes_to_new_file does not. It would make sense to have both tests behave similarly.

The reason why I use writer->close() in the snapshot_writes_to_new_file_with_file_compression is because otherwise without close it will be a race conditions in tests. The close method will wait for the compressor threads to finish.
I don't want to call writer->close() in another snapshot_writes_to_new_file_with_bag_split test because want to check that file will be finalized when calling snapshot, without calling writer->close() method.

Copy link
Member

@christophebedard christophebedard left a comment

Choose a reason for hiding this comment

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

Looks good to me with green CI.

@MichaelOrlov
Copy link
Contributor Author

Pulls: #1842
Gist: https://gist.githubusercontent.com/MichaelOrlov/127b9b542e0ed83d235de01e67be6919/raw/6dec255f6b87621d4194067c15f7fa94ebac1f63/ros2.repos
BUILD args: --packages-above-and-dependencies rosbag2_compression rosbag2_cpp rosbag2_tests
TEST args: --packages-above rosbag2_compression rosbag2_cpp rosbag2_tests
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/14765

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@MichaelOrlov MichaelOrlov merged commit 3f2281f into rolling Nov 1, 2024
12 checks passed
@MichaelOrlov MichaelOrlov deleted the morlov/snapshot_to_new_file branch November 1, 2024 23:32
@MichaelOrlov
Copy link
Contributor Author

https://github.com/Mergifyio backport jazzy humble

Copy link

mergify bot commented Nov 1, 2024

backport jazzy humble

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Nov 1, 2024
* Make snapshot writing into a new file each time when it is triggered

- Note. Snapshot now became a blocking call and mutually exclusive with
writer::write(message) method to avoid race conditions.
i.e. blocking the same writer_mutex_

Signed-off-by: Michael Orlov <[email protected]>

* Add unit test to make sure that snapshot writing in the new file

Co-authored-by: Clemens Mühlbacher <[email protected]>
Signed-off-by: Michael Orlov <[email protected]>

* Add support for snapshot with file compression

Signed-off-by: Michael Orlov <[email protected]>

* Rename newly added tests to avoid misunderstanding

Signed-off-by: Michael Orlov <[email protected]>

* Address review comments in tests

Signed-off-by: Michael Orlov <[email protected]>

* Change order of includes in the test_sequential_compression_writer.cpp

Signed-off-by: Michael Orlov <[email protected]>

* Update metadata_.message_count unconditionally in write_messages(..)

Signed-off-by: Michael Orlov <[email protected]>

---------

Signed-off-by: Michael Orlov <[email protected]>
Co-authored-by: Clemens Mühlbacher <[email protected]>
(cherry picked from commit 3f2281f)
mergify bot pushed a commit that referenced this pull request Nov 1, 2024
* Make snapshot writing into a new file each time when it is triggered

- Note. Snapshot now became a blocking call and mutually exclusive with
writer::write(message) method to avoid race conditions.
i.e. blocking the same writer_mutex_

Signed-off-by: Michael Orlov <[email protected]>

* Add unit test to make sure that snapshot writing in the new file

Co-authored-by: Clemens Mühlbacher <[email protected]>
Signed-off-by: Michael Orlov <[email protected]>

* Add support for snapshot with file compression

Signed-off-by: Michael Orlov <[email protected]>

* Rename newly added tests to avoid misunderstanding

Signed-off-by: Michael Orlov <[email protected]>

* Address review comments in tests

Signed-off-by: Michael Orlov <[email protected]>

* Change order of includes in the test_sequential_compression_writer.cpp

Signed-off-by: Michael Orlov <[email protected]>

* Update metadata_.message_count unconditionally in write_messages(..)

Signed-off-by: Michael Orlov <[email protected]>

---------

Signed-off-by: Michael Orlov <[email protected]>
Co-authored-by: Clemens Mühlbacher <[email protected]>
(cherry picked from commit 3f2281f)
@reinzor
Copy link
Contributor

reinzor commented Nov 2, 2024

Great! Thanks for this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Snapshot should write to a different file every time it is triggered
5 participants