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

feat: introduce the advanced publisher and subscriber (backport #368) #469

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
48 changes: 0 additions & 48 deletions rmw_zenoh_cpp/src/detail/graph_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,20 +384,6 @@ void GraphCache::parse_put(
// Otherwise, the entity represents a node that already exists in the graph.
// Update topic info if required below.
update_topic_maps_for_put(node_it->second, entity);

// If the newly added entity is a publisher with transient_local qos durability,
// we trigger any registered querying subscriber callbacks.
if (entity->type() == liveliness::EntityType::Publisher &&
entity->topic_info().has_value() &&
entity->topic_info()->qos_.durability == RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL)
{
auto sub_cbs_it = querying_subs_cbs_.find(entity->topic_info()->topic_keyexpr_);
if (sub_cbs_it != querying_subs_cbs_.end()) {
for (auto sub_it = sub_cbs_it->second.begin(); sub_it != sub_cbs_it->second.end(); ++sub_it) {
sub_it->second(entity->zid());
}
}
}
}

///=============================================================================
Expand Down Expand Up @@ -1259,38 +1245,4 @@ void GraphCache::update_event_counters(
}
}
}

///=============================================================================
void GraphCache::set_querying_subscriber_callback(
const std::string & sub_keyexpr,
const std::size_t sub_keyxpr_hash,
QueryingSubscriberCallback cb)
{
std::unordered_map<
std::string,
std::unordered_map<std::size_t, QueryingSubscriberCallback>
>::iterator cb_it = querying_subs_cbs_.find(sub_keyexpr);
if (cb_it == querying_subs_cbs_.end()) {
querying_subs_cbs_[sub_keyexpr] =
std::unordered_map<std::size_t, QueryingSubscriberCallback>{};
cb_it = querying_subs_cbs_.find(sub_keyexpr);
}
cb_it->second.insert(std::make_pair(sub_keyxpr_hash, std::move(cb)));
}

///=============================================================================
void GraphCache::remove_querying_subscriber_callback(
const std::string & sub_keyexpr,
const std::size_t sub_keyexpr_hash)
{
std::unordered_map<
std::string,
std::unordered_map<std::size_t, QueryingSubscriberCallback>
>::iterator cb_map_it = querying_subs_cbs_.find(sub_keyexpr);
if (cb_map_it == querying_subs_cbs_.end()) {
return;
}
cb_map_it->second.erase(sub_keyexpr_hash);
}

} // namespace rmw_zenoh_cpp
14 changes: 0 additions & 14 deletions rmw_zenoh_cpp/src/detail/graph_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ class GraphCache final
/// @brief Signature for a function that will be invoked by the GraphCache when a QoS
/// event is detected.
using GraphCacheEventCallback = std::function<void (int32_t change)>;
/// Callback to be triggered when a publication cache is detected in the ROS Graph.
using QueryingSubscriberCallback = std::function<void (const std::string & queryable_prefix)>;

/// @brief Constructor
/// @param id The id of the zenoh session that is building the graph cache.
Expand Down Expand Up @@ -189,15 +187,6 @@ class GraphCache final
/// Returns true if the entity is a publisher or client. False otherwise.
static bool is_entity_pub(const liveliness::Entity & entity);

void set_querying_subscriber_callback(
const std::string & sub_keyexpr,
const std::size_t sub_keyexpr_hash,
QueryingSubscriberCallback cb);

void remove_querying_subscriber_callback(
const std::string & sub_keyexpr,
const std::size_t sub_keyexpr_hash);

private:
// Helper function to convert an Entity into a GraphNode.
// Note: this will update bookkeeping variables in GraphCache.
Expand Down Expand Up @@ -287,9 +276,6 @@ class GraphCache final
using GraphEventCallbackMap = std::unordered_map<std::size_t, GraphEventCallbacks>;
// EventCallbackMap for each type of event we support in rmw_zenoh_cpp.
GraphEventCallbackMap event_callbacks_;
// Map key expressions to a map of sub keyexpr_hash and QueryingSubscriberCallback.
std::unordered_map<std::string, std::unordered_map<std::size_t,
QueryingSubscriberCallback>> querying_subs_cbs_;
std::mutex events_mutex_;

// Mutex to lock before modifying the members above.
Expand Down
72 changes: 33 additions & 39 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,40 +104,35 @@ std::shared_ptr<PublisherData> PublisherData::make(
return nullptr;
}

zenoh::ZResult result;
std::optional<zenoh::ext::PublicationCache> pub_cache;
zenoh::KeyExpr pub_ke(entity->topic_info()->topic_keyexpr_);
// Create a Publication Cache if durability is transient_local.
if (adapted_qos_profile.durability == RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL) {
zenoh::ext::SessionExt::PublicationCacheOptions pub_cache_opts =
zenoh::ext::SessionExt::PublicationCacheOptions::create_default();

pub_cache_opts.history = adapted_qos_profile.depth;
pub_cache_opts.queryable_complete = true;
using AdvancedPublisherOptions = zenoh::ext::SessionExt::AdvancedPublisherOptions;
auto adv_pub_opts = AdvancedPublisherOptions::create_default();

std::string queryable_prefix = entity->zid();
pub_cache_opts.queryable_prefix = zenoh::KeyExpr(queryable_prefix);

pub_cache = session->ext().declare_publication_cache(
pub_ke, std::move(pub_cache_opts), &result);

if (result != Z_OK) {
RMW_SET_ERROR_MSG("unable to create zenoh publisher cache");
return nullptr;
}
if (adapted_qos_profile.durability == RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL) {
// Allow this publisher to be detected through liveliness.
adv_pub_opts.publisher_detection = true;
adv_pub_opts.cache = AdvancedPublisherOptions::CacheOptions::create_default();
adv_pub_opts.cache->max_samples = adapted_qos_profile.depth;
}

zenoh::KeyExpr pub_ke(entity->topic_info()->topic_keyexpr_);
// Set congestion_control to BLOCK if appropriate.
zenoh::Session::PublisherOptions opts = zenoh::Session::PublisherOptions::create_default();
opts.congestion_control = Z_CONGESTION_CONTROL_DROP;
auto pub_opts = zenoh::Session::PublisherOptions::create_default();
pub_opts.congestion_control = Z_CONGESTION_CONTROL_DROP;
if (adapted_qos_profile.reliability == RMW_QOS_POLICY_RELIABILITY_RELIABLE) {
opts.reliability = Z_RELIABILITY_RELIABLE;

pub_opts.reliability = Z_RELIABILITY_RELIABLE;
if (adapted_qos_profile.history == RMW_QOS_POLICY_HISTORY_KEEP_ALL) {
opts.congestion_control = Z_CONGESTION_CONTROL_BLOCK;
pub_opts.congestion_control = Z_CONGESTION_CONTROL_BLOCK;
}
}
auto pub = session->declare_publisher(pub_ke, std::move(opts), &result);
adv_pub_opts.publisher_options = pub_opts;

zenoh::ZResult result;
auto adv_pub = session->ext().declare_advanced_publisher(
pub_ke, std::move(adv_pub_opts), &result);
if (result != Z_OK) {
RMW_SET_ERROR_MSG("unable to create zenoh publisher cache");
return nullptr;
}

if (result != Z_OK) {
RMW_SET_ERROR_MSG("Unable to create Zenoh publisher.");
Expand All @@ -161,8 +156,7 @@ std::shared_ptr<PublisherData> PublisherData::make(
node,
std::move(entity),
std::move(session),
std::move(pub),
std::move(pub_cache),
std::move(adv_pub),
std::move(token),
type_support->data,
std::move(message_type_support)
Expand All @@ -174,16 +168,14 @@ PublisherData::PublisherData(
const rmw_node_t * rmw_node,
std::shared_ptr<liveliness::Entity> entity,
std::shared_ptr<zenoh::Session> sess,
zenoh::Publisher pub,
std::optional<zenoh::ext::PublicationCache> pub_cache,
zenoh::ext::AdvancedPublisher pub,
zenoh::LivelinessToken token,
const void * type_support_impl,
std::unique_ptr<MessageTypeSupport> type_support)
: rmw_node_(rmw_node),
entity_(std::move(entity)),
sess_(std::move(sess)),
pub_(std::move(pub)),
pub_cache_(std::move(pub_cache)),
token_(std::move(token)),
type_support_impl_(type_support_impl),
type_support_(std::move(type_support)),
Expand Down Expand Up @@ -246,18 +238,18 @@ rmw_ret_t PublisherData::publish(
// session use different encoding formats. In our case, all key expressions
// will be encoded with CDR so it does not really matter.
zenoh::ZResult result;
auto options = zenoh::Publisher::PutOptions::create_default();
options.attachment = create_map_and_set_sequence_num(
sequence_number_++,
entity_->copy_gid());
int64_t source_timestamp = rmw_zenoh_cpp::get_system_time_in_ns();
auto opts = zenoh::ext::AdvancedPublisher::PutOptions::create_default();
opts.put_options.attachment = rmw_zenoh_cpp::AttachmentData(
sequence_number_++, source_timestamp, entity_->copy_gid()).serialize_to_zbytes();
Copy link
Member

Choose a reason for hiding this comment

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

It looks like we're switching from create_map_and_set_sequence_num() to AttachmentData in this PR which was not part of #368.
Do we still need create_map_and_set_sequence_num?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's best if we do a backport of #294 but make modifications to drop the tracetools dependency. That should ensure we're consistent with our usage of AttachmentData instead of create_map_and_set_sequence_num. Please hold off on merging this and the humble backport in the meanwhile.


// TODO(ahcorde): shmbuf
std::vector<uint8_t> raw_data(
reinterpret_cast<const uint8_t *>(msg_bytes),
reinterpret_cast<const uint8_t *>(msg_bytes) + data_length);
zenoh::Bytes payload(std::move(raw_data));

pub_.put(std::move(payload), std::move(options), &result);
pub_.put(std::move(payload), std::move(opts), &result);
if (result != Z_OK) {
if (result == Z_ESESSION_CLOSED) {
RMW_ZENOH_LOG_WARN_NAMED(
Expand Down Expand Up @@ -292,15 +284,17 @@ rmw_ret_t PublisherData::publish_serialized_message(
// session use different encoding formats. In our case, all key expressions
// will be encoded with CDR so it does not really matter.
zenoh::ZResult result;
auto options = zenoh::Publisher::PutOptions::create_default();
options.attachment = create_map_and_set_sequence_num(sequence_number_++, entity_->copy_gid());
int64_t source_timestamp = rmw_zenoh_cpp::get_system_time_in_ns();
auto opts = zenoh::ext::AdvancedPublisher::PutOptions::create_default();
opts.put_options.attachment = rmw_zenoh_cpp::AttachmentData(
sequence_number_++, source_timestamp, entity_->copy_gid()).serialize_to_zbytes();

std::vector<uint8_t> raw_data(
serialized_message->buffer,
serialized_message->buffer + data_length);
zenoh::Bytes payload(std::move(raw_data));

pub_.put(std::move(payload), std::move(options), &result);
pub_.put(std::move(payload), std::move(opts), &result);
if (result != Z_OK) {
if (result == Z_ESESSION_CLOSED) {
RMW_ZENOH_LOG_WARN_NAMED(
Expand Down
9 changes: 3 additions & 6 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ class PublisherData final
const rmw_node_t * rmw_node,
std::shared_ptr<liveliness::Entity> entity,
std::shared_ptr<zenoh::Session> session,
zenoh::Publisher pub,
std::optional<zenoh::ext::PublicationCache> pub_cache,
zenoh::ext::AdvancedPublisher pub,
zenoh::LivelinessToken token,
const void * type_support_impl,
std::unique_ptr<MessageTypeSupport> type_support);
Expand All @@ -107,10 +106,8 @@ class PublisherData final
std::shared_ptr<liveliness::Entity> entity_;
// A shared session.
std::shared_ptr<zenoh::Session> sess_;
// An owned publisher.
zenoh::Publisher pub_;
// Optional publication cache when durability is transient_local.
std::optional<zenoh::ext::PublicationCache> pub_cache_;
// An owned AdvancedPublisher.
zenoh::ext::AdvancedPublisher pub_;
// Liveliness token for the publisher.
std::optional<zenoh::LivelinessToken> token_;
// Type support fields
Expand Down
Loading