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

Fixes for services playback per review 2 #6

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
bc4d118
Make service_event_ts_lib as private member again
MichaelOrlov Mar 31, 2024
5f324c1
Cleanup in PlayerServiceClient::async_send_request(ser_message)
MichaelOrlov Mar 31, 2024
6397df6
Refactoring. Do full deserialization and only once
MichaelOrlov Mar 31, 2024
94d418f
Specify service request from which introspection message and fix uncr…
Barry-Xu-2018 Apr 7, 2024
00bff3e
Revert uncrustify changes from previous commit.
MichaelOrlov Apr 9, 2024
05a3d1d
Rename service_request_from to the service_requests_source
MichaelOrlov Apr 9, 2024
9d98b04
Add Player::wait_for_sent_service_requests_to_finish() API
MichaelOrlov Apr 10, 2024
b7116b9
Mitigate potential issues related to the operations reordering on ARM
MichaelOrlov Apr 10, 2024
b87c944
Make tests play_service_requests_from_service(client) deterministic
MichaelOrlov Apr 10, 2024
bede854
Misc findings and improvements 1
MichaelOrlov Apr 10, 2024
c5f6c13
Rename get_services_clients() to the get_service_clients()
MichaelOrlov Apr 10, 2024
a623a23
Add a new CLI parameter "--publish-service-requests" for Player
Barry-Xu-2018 Apr 8, 2024
091620c
Fix an issue on filtering topic when prepare publishers
MichaelOrlov Apr 10, 2024
3c19873
Cleanup in play_without_publish_service_requests
MichaelOrlov Apr 10, 2024
98c14ef
Wrap code which can throw with try-catch in the publish_message(..)
MichaelOrlov Apr 11, 2024
f270ac1
Delete some part of the code which became absolute and shall not be used
MichaelOrlov Apr 11, 2024
974d687
Update test codes
Barry-Xu-2018 Apr 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions rosbag2_transport/include/rosbag2_transport/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ class Player : public rclcpp::Node
ROSBAG2_TRANSPORT_PUBLIC
void delete_on_play_message_callback(const callback_handle_t & handle);

/// Wait until sent service requests will receive responses from service servers.
/// \param service_name - Name of the service or service event from what to wait responses.
/// \note is service_name is empty the function will wait until all service requests sent to all
/// service servers will finish. Timeout in this cases will be used for each service name.
/// \param timeout - Timeout in fraction of seconds to wait for.
/// \return true if service requests successfully finished, otherwise false.
ROSBAG2_TRANSPORT_PUBLIC
bool wait_for_sent_service_requests_to_finish(
const std::string & service_name,
std::chrono::duration<double> timeout = std::chrono::seconds(5));

protected:
/// \brief Getter for publishers corresponding to each topic
/// \return Hashtable representing topic to publisher map excluding inner clock_publisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class PlayerServiceClient final

void async_send_request(const std::shared_ptr<uint8_t[]> type_erased_service_event);

/// Wait until sent service requests will receive responses from service servers.
/// \param service_name - Name of the service or service event from what to wait responses.
/// \param timeout - Timeout in fraction of seconds to wait for.
/// \return true if service requests successfully finished, otherwise false.
bool wait_for_sent_requests_to_finish(
std::chrono::duration<double> timeout = std::chrono::seconds(5));

void async_send_request(const rcl_serialized_message_t & message);

std::shared_ptr<rclcpp::GenericClient> generic_client()
Expand Down Expand Up @@ -103,6 +110,8 @@ class PlayerServiceClientManager final
rclcpp::GenericClient::FutureAndRequestId & request_future,
std::weak_ptr<rclcpp::GenericClient> client);

bool wait_for_all_futures(std::chrono::duration<double> timeout = std::chrono::seconds(5));

private:
using time_point = std::chrono::steady_clock::time_point;
using ptr_future_and_request_id = std::unique_ptr<rclcpp::GenericClient::FutureAndRequestId>;
Expand Down
37 changes: 37 additions & 0 deletions rosbag2_transport/src/rosbag2_transport/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ class PlayerImpl
/// #add_on_play_message_post_callback
void delete_on_play_message_callback(const callback_handle_t & handle);

/// TBD
/// \param service_name
/// \param timeout
/// \return
bool wait_for_sent_service_requests_to_finish(
const std::string & service_name,
std::chrono::duration<double> timeout = std::chrono::seconds(5));

/// \brief Getter for publishers corresponding to each topic
/// \return Hashtable representing topic to publisher map excluding inner clock_publisher
std::unordered_map<std::string, std::shared_ptr<rclcpp::GenericPublisher>> get_publishers();
Expand Down Expand Up @@ -787,6 +795,29 @@ void PlayerImpl::delete_on_play_message_callback(const callback_handle_t & handl
});
}

bool PlayerImpl::wait_for_sent_service_requests_to_finish(
const std::string & service_name,
std::chrono::duration<double> timeout)
{
bool is_requests_complete = true;
if (!service_name.empty()) {
auto service_event_name = rosbag2_cpp::service_name_to_service_event_topic_name(service_name);
auto client_iter = service_clients_.find(service_event_name);
if (client_iter != service_clients_.end()) {
is_requests_complete = client_iter->second->wait_for_sent_requests_to_finish(timeout);
Copy link
Owner

Choose a reason for hiding this comment

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

Finally, it also will call PlayerServiceClientManager::wait_for_all_futures().
So this operation does not wait for the future of the client of the specified service to complete, but waits for all futures to complete.

Copy link
Author

Choose a reason for hiding this comment

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

@Barry-Xu-2018 In this particular case it will wait for all futures for specific client.
However if service_name is empty it will wait for all futures from all clients, - does it make sense?

Copy link
Owner

Choose a reason for hiding this comment

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

@MichaelOrlov

However if service_name is empty it will wait for all futures from all clients, - does it make sense?

Yes. It makes sense.

In this particular case it will wait for all futures for specific client.

Although the code retrieves the corresponding client based on the service name, client->wait_for_sent_requests_to_finish() actually calls PlayerServiceClientManager::wait_for_all_futures(). This function does not wait for all futures of a specific client; instead, it waits for all futures of all registered clients.

} else {
is_requests_complete = false;
}
} else {
for (const auto & [service_event_name, client] : service_clients_) {
if (!client->wait_for_sent_requests_to_finish(timeout)) {
return false;
}
}
}
return is_requests_complete;
}

std::unordered_map<std::string,
std::shared_ptr<rclcpp::GenericPublisher>> PlayerImpl::get_publishers()
{
Expand Down Expand Up @@ -1630,6 +1661,12 @@ void Player::delete_on_play_message_callback(const Player::callback_handle_t & h
pimpl_->delete_on_play_message_callback(handle);
}

bool Player::wait_for_sent_service_requests_to_finish(
const std::string & service_name, std::chrono::duration<double> timeout)
{
return pimpl_->wait_for_sent_service_requests_to_finish(service_name, timeout);
}

std::unordered_map<std::string, std::shared_ptr<rclcpp::GenericPublisher>> Player::get_publishers()
{
return pimpl_->get_publishers();
Expand Down
40 changes: 40 additions & 0 deletions rosbag2_transport/src/rosbag2_transport/player_service_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "logging.hpp"

using namespace std::chrono_literals;

namespace rosbag2_transport
{

Expand Down Expand Up @@ -168,6 +170,11 @@ void PlayerServiceClient::async_send_request(
} // else { /* No service request in the service event. Do nothing, just skip it. */ }
}

bool PlayerServiceClient::wait_for_sent_requests_to_finish(std::chrono::duration<double> timeout)
{
return player_service_client_manager_->wait_for_all_futures(timeout);
}

void PlayerServiceClient::async_send_request(const rcl_serialized_message_t & message)
{
if (!client_->service_is_ready()) {
Expand Down Expand Up @@ -258,6 +265,39 @@ bool PlayerServiceClientManager::register_request_future(
return false;
}

bool PlayerServiceClientManager::wait_for_all_futures(std::chrono::duration<double> timeout)
{
auto is_all_futures_ready = [&]() {
bool is_ready = true;
for (auto & [timestamp, future_request_id_and_client] : request_futures_list_) {
if (!future_request_id_and_client.first->future.valid()) {
std::stringstream ss;
ss << "request's " << future_request_id_and_client.first->request_id <<
" future is not valid!\n";
throw std::runtime_error(ss.str());
}
if (future_request_id_and_client.first->wait_for(0s) != std::future_status::ready) {
return false;
}
}
return is_ready;
};

auto sleep_time = std::chrono::milliseconds(10);
if (timeout < std::chrono::seconds(1)) {
sleep_time = std::chrono::duration_cast<std::chrono::milliseconds>(timeout);
}
using clock = std::chrono::system_clock;
auto start = clock::now();

std::lock_guard<std::mutex> lock(request_futures_list_mutex_);
while (!is_all_futures_ready() && (clock::now() - start) < timeout) {
std::this_thread::sleep_for(sleep_time);
}

return is_all_futures_ready();
}

void PlayerServiceClientManager::remove_complete_request_future()
{
std::vector<time_point> remove_keys;
Expand Down