Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1 | #include "aos/events/simulated_network_bridge.h" |
| 2 | |
| 3 | #include "aos/events/event_loop.h" |
| 4 | #include "aos/events/simulated_event_loop.h" |
| 5 | |
| 6 | namespace aos { |
| 7 | namespace message_bridge { |
| 8 | |
| 9 | // This class delays messages forwarded between two factories. |
| 10 | // |
| 11 | // The basic design is that we need to use the distributed_clock to convert |
| 12 | // monotonic times from the source to the destination node. We also use a |
| 13 | // fetcher to manage the queue of data, and a timer to schedule the sends. |
| 14 | class RawMessageDelayer { |
| 15 | public: |
| 16 | RawMessageDelayer(aos::NodeEventLoopFactory *fetch_node_factory, |
| 17 | aos::NodeEventLoopFactory *send_node_factory, |
| 18 | aos::EventLoop *send_event_loop, |
| 19 | std::unique_ptr<aos::RawFetcher> fetcher, |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 20 | std::unique_ptr<aos::RawSender> sender, |
| 21 | ServerConnection *server_connection, int client_index, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 22 | MessageBridgeClientStatus *client_status, |
| 23 | size_t channel_index, |
| 24 | aos::Sender<logger::MessageHeader> *timestamp_logger) |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 25 | : fetch_node_factory_(fetch_node_factory), |
| 26 | send_node_factory_(send_node_factory), |
| 27 | send_event_loop_(send_event_loop), |
| 28 | fetcher_(std::move(fetcher)), |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 29 | sender_(std::move(sender)), |
| 30 | server_connection_(server_connection), |
| 31 | client_status_(client_status), |
| 32 | client_index_(client_index), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 33 | client_connection_(client_status_->GetClientConnection(client_index)), |
| 34 | channel_index_(channel_index), |
| 35 | timestamp_logger_(timestamp_logger) { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 36 | timer_ = send_event_loop_->AddTimer([this]() { Send(); }); |
| 37 | |
| 38 | Schedule(); |
| 39 | } |
| 40 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 41 | const Channel *channel() const { return fetcher_->channel(); } |
| 42 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 43 | // Kicks us to re-fetch and schedule the timer. |
| 44 | void Schedule() { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 45 | // Keep pulling messages out of the fetcher until we find one in the future. |
| 46 | while (true) { |
| 47 | if (fetcher_->context().data == nullptr || sent_) { |
| 48 | sent_ = !fetcher_->FetchNext(); |
| 49 | } |
| 50 | if (sent_) { |
| 51 | break; |
| 52 | } |
| 53 | if (fetcher_->context().monotonic_event_time + |
| 54 | send_node_factory_->network_delay() + |
| 55 | send_node_factory_->send_delay() > |
| 56 | fetch_node_factory_->monotonic_now()) { |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | // TODO(austin): Not cool. We want to actually forward these. This means |
| 61 | // we need a more sophisticated concept of what is running. |
| 62 | LOG(WARNING) << "Not forwarding message on " |
| 63 | << configuration::CleanedChannelToString(fetcher_->channel()) |
| 64 | << " because we aren't running. Set at " |
| 65 | << fetcher_->context().monotonic_event_time << " now is " |
| 66 | << fetch_node_factory_->monotonic_now(); |
| 67 | sent_ = true; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 68 | server_connection_->mutate_dropped_packets( |
| 69 | server_connection_->dropped_packets() + 1); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (fetcher_->context().data == nullptr) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (sent_) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Compute the time to publish this message. |
| 81 | const monotonic_clock::time_point monotonic_delivered_time = |
| 82 | DeliveredTime(fetcher_->context()); |
| 83 | |
| 84 | CHECK_GE(monotonic_delivered_time, send_node_factory_->monotonic_now()) |
| 85 | << ": Trying to deliver message in the past..."; |
| 86 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 87 | server_connection_->mutate_sent_packets(server_connection_->sent_packets() + |
| 88 | 1); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 89 | timer_->Setup(monotonic_delivered_time); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | // Acutally sends the message, and reschedules. |
| 94 | void Send() { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 95 | // Fill out the send times. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 96 | sender_->Send(fetcher_->context().data, fetcher_->context().size, |
| 97 | fetcher_->context().monotonic_event_time, |
| 98 | fetcher_->context().realtime_event_time, |
| 99 | fetcher_->context().queue_index); |
| 100 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 101 | // And simulate message_bridge's offset recovery. |
| 102 | client_status_->SampleFilter(client_index_, |
| 103 | fetcher_->context().monotonic_event_time, |
| 104 | sender_->monotonic_sent_time()); |
| 105 | |
| 106 | client_connection_->mutate_received_packets( |
| 107 | client_connection_->received_packets() + 1); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 108 | |
| 109 | if (timestamp_logger_) { |
| 110 | aos::Sender<logger::MessageHeader>::Builder builder = |
| 111 | timestamp_logger_->MakeBuilder(); |
| 112 | |
| 113 | logger::MessageHeader::Builder message_header_builder = |
| 114 | builder.MakeBuilder<logger::MessageHeader>(); |
| 115 | |
| 116 | message_header_builder.add_channel_index(channel_index_); |
| 117 | |
| 118 | // Swap the remote and sent metrics. They are from the sender's |
| 119 | // perspective, not the receiver's perspective. |
| 120 | message_header_builder.add_monotonic_remote_time( |
| 121 | fetcher_->context().monotonic_event_time.time_since_epoch().count()); |
| 122 | message_header_builder.add_realtime_remote_time( |
| 123 | fetcher_->context().realtime_event_time.time_since_epoch().count()); |
| 124 | message_header_builder.add_remote_queue_index( |
| 125 | fetcher_->context().queue_index); |
| 126 | |
| 127 | message_header_builder.add_monotonic_sent_time( |
| 128 | sender_->monotonic_sent_time().time_since_epoch().count()); |
| 129 | message_header_builder.add_realtime_sent_time( |
| 130 | sender_->realtime_sent_time().time_since_epoch().count()); |
| 131 | message_header_builder.add_queue_index(sender_->sent_queue_index()); |
| 132 | |
| 133 | builder.Send(message_header_builder.Finish()); |
| 134 | } |
| 135 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 136 | sent_ = true; |
| 137 | Schedule(); |
| 138 | } |
| 139 | |
| 140 | // Converts from time on the sending node to time on the receiving node. |
| 141 | monotonic_clock::time_point DeliveredTime(const Context &context) const { |
| 142 | const distributed_clock::time_point distributed_sent_time = |
| 143 | fetch_node_factory_->ToDistributedClock(context.monotonic_event_time); |
| 144 | |
| 145 | return aos::monotonic_clock::epoch() + |
| 146 | (distributed_sent_time - send_node_factory_->ToDistributedClock( |
| 147 | aos::monotonic_clock::epoch())) + |
| 148 | send_node_factory_->network_delay() + |
| 149 | send_node_factory_->send_delay(); |
| 150 | } |
| 151 | |
| 152 | // Factories used for time conversion. |
| 153 | aos::NodeEventLoopFactory *fetch_node_factory_; |
| 154 | aos::NodeEventLoopFactory *send_node_factory_; |
| 155 | |
| 156 | // Event loop which sending is scheduled on. |
| 157 | aos::EventLoop *send_event_loop_; |
| 158 | // Timer used to send. |
| 159 | aos::TimerHandler *timer_; |
| 160 | // Fetcher used to receive messages. |
| 161 | std::unique_ptr<aos::RawFetcher> fetcher_; |
| 162 | // Sender to send them back out. |
| 163 | std::unique_ptr<aos::RawSender> sender_; |
| 164 | // True if we have sent the message in the fetcher. |
| 165 | bool sent_ = false; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 166 | |
| 167 | ServerConnection *server_connection_ = nullptr; |
| 168 | MessageBridgeClientStatus *client_status_ = nullptr; |
| 169 | int client_index_; |
| 170 | ClientConnection *client_connection_ = nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 171 | |
| 172 | size_t channel_index_; |
| 173 | aos::Sender<logger::MessageHeader> *timestamp_logger_ = nullptr; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | SimulatedMessageBridge::SimulatedMessageBridge( |
| 177 | SimulatedEventLoopFactory *simulated_event_loop_factory) { |
| 178 | CHECK( |
| 179 | configuration::MultiNode(simulated_event_loop_factory->configuration())); |
| 180 | |
| 181 | // Pre-build up event loops for every node. They are pretty cheap anyways. |
| 182 | for (const Node *node : simulated_event_loop_factory->nodes()) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 183 | auto it = event_loop_map_.emplace(std::make_pair( |
| 184 | node, |
| 185 | simulated_event_loop_factory->MakeEventLoop("message_bridge", node))); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 186 | |
| 187 | CHECK(it.second); |
| 188 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 189 | it.first->second.event_loop->SkipTimingReport(); |
| 190 | it.first->second.event_loop->SkipAosLog(); |
| 191 | |
| 192 | for (ServerConnection *connection : |
| 193 | it.first->second.server_status.server_connection()) { |
| 194 | if (connection == nullptr) continue; |
| 195 | |
| 196 | connection->mutate_state(message_bridge::State::CONNECTED); |
| 197 | } |
| 198 | |
| 199 | for (size_t i = 0; |
| 200 | i < it.first->second.client_status.mutable_client_statistics() |
| 201 | ->mutable_connections() |
| 202 | ->size(); |
| 203 | ++i) { |
| 204 | ClientConnection *connection = |
| 205 | it.first->second.client_status.mutable_client_statistics() |
| 206 | ->mutable_connections() |
| 207 | ->GetMutableObject(i); |
| 208 | if (connection == nullptr) continue; |
| 209 | |
| 210 | connection->mutate_state(message_bridge::State::CONNECTED); |
| 211 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | for (const Channel *channel : |
| 215 | *simulated_event_loop_factory->configuration()->channels()) { |
| 216 | if (!channel->has_destination_nodes()) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | // Find the sending node. |
| 221 | const Node *node = |
| 222 | configuration::GetNode(simulated_event_loop_factory->configuration(), |
| 223 | channel->source_node()->string_view()); |
| 224 | auto source_event_loop = event_loop_map_.find(node); |
| 225 | CHECK(source_event_loop != event_loop_map_.end()); |
| 226 | |
| 227 | std::unique_ptr<DelayersVector> delayers = |
| 228 | std::make_unique<DelayersVector>(); |
| 229 | |
| 230 | // And then build up a RawMessageDelayer for each destination. |
| 231 | for (const Connection *connection : *channel->destination_nodes()) { |
| 232 | const Node *destination_node = |
| 233 | configuration::GetNode(simulated_event_loop_factory->configuration(), |
| 234 | connection->name()->string_view()); |
| 235 | auto destination_event_loop = event_loop_map_.find(destination_node); |
| 236 | CHECK(destination_event_loop != event_loop_map_.end()); |
| 237 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 238 | ServerConnection *server_connection = |
| 239 | source_event_loop->second.server_status.FindServerConnection( |
| 240 | connection->name()->string_view()); |
| 241 | |
| 242 | int client_index = |
| 243 | destination_event_loop->second.client_status.FindClientIndex( |
| 244 | channel->source_node()->string_view()); |
| 245 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 246 | const size_t destination_node_index = configuration::GetNodeIndex( |
| 247 | simulated_event_loop_factory->configuration(), destination_node); |
| 248 | |
| 249 | const bool delivery_time_is_logged = |
| 250 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 251 | connection, source_event_loop->second.event_loop->node()); |
| 252 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 253 | delayers->emplace_back(std::make_unique<RawMessageDelayer>( |
| 254 | simulated_event_loop_factory->GetNodeEventLoopFactory(node), |
| 255 | simulated_event_loop_factory->GetNodeEventLoopFactory( |
| 256 | destination_node), |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 257 | destination_event_loop->second.event_loop.get(), |
| 258 | source_event_loop->second.event_loop->MakeRawFetcher(channel), |
| 259 | destination_event_loop->second.event_loop->MakeRawSender(channel), |
| 260 | server_connection, client_index, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 261 | &destination_event_loop->second.client_status, |
| 262 | configuration::ChannelIndex( |
| 263 | source_event_loop->second.event_loop->configuration(), channel), |
| 264 | delivery_time_is_logged |
| 265 | ? &source_event_loop->second |
| 266 | .timestamp_loggers[destination_node_index] |
| 267 | : nullptr)); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 270 | const Channel *const timestamp_channel = configuration::GetChannel( |
| 271 | simulated_event_loop_factory->configuration(), "/aos", |
| 272 | Timestamp::GetFullyQualifiedName(), |
| 273 | source_event_loop->second.event_loop->name(), node); |
| 274 | |
| 275 | if (channel == timestamp_channel) { |
| 276 | source_event_loop->second.server_status.set_send_data( |
| 277 | [captured_delayers = delayers.get()](const Context &) { |
| 278 | for (std::unique_ptr<RawMessageDelayer> &delayer : |
| 279 | *captured_delayers) { |
| 280 | delayer->Schedule(); |
| 281 | } |
| 282 | }); |
| 283 | } else { |
| 284 | // And register every delayer to be poked when a new message shows up. |
| 285 | source_event_loop->second.event_loop->MakeRawNoArgWatcher( |
| 286 | channel, [captured_delayers = delayers.get()](const Context &) { |
| 287 | for (std::unique_ptr<RawMessageDelayer> &delayer : |
| 288 | *captured_delayers) { |
| 289 | delayer->Schedule(); |
| 290 | } |
| 291 | }); |
| 292 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 293 | delayers_list_.emplace_back(std::move(delayers)); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | SimulatedMessageBridge::~SimulatedMessageBridge() {} |
| 298 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 299 | void SimulatedMessageBridge::DisableForwarding(const Channel *channel) { |
| 300 | for (std::unique_ptr<std::vector<std::unique_ptr<RawMessageDelayer>>> |
| 301 | &delayers : delayers_list_) { |
| 302 | if (delayers->size() > 0) { |
| 303 | if ((*delayers)[0]->channel() == channel) { |
| 304 | for (std::unique_ptr<RawMessageDelayer> &delayer : *delayers) { |
| 305 | CHECK(delayer->channel() == channel); |
| 306 | } |
| 307 | |
| 308 | // If we clear the delayers list, nothing will be scheduled. Which is a |
| 309 | // success! |
| 310 | delayers->clear(); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 316 | void SimulatedMessageBridge::DisableStatistics() { |
| 317 | for (std::pair<const Node *const, State> &state : event_loop_map_) { |
| 318 | state.second.server_status.DisableStatistics(); |
| 319 | state.second.client_status.DisableStatistics(); |
| 320 | } |
| 321 | } |
| 322 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 323 | SimulatedMessageBridge::State::State( |
| 324 | std::unique_ptr<aos::EventLoop> &&new_event_loop) |
| 325 | : event_loop(std::move(new_event_loop)), |
| 326 | server_status(event_loop.get()), |
| 327 | client_status(event_loop.get()) { |
| 328 | timestamp_loggers.resize(event_loop->configuration()->nodes()->size()); |
| 329 | |
| 330 | // Find all nodes which log timestamps back to us (from us). |
| 331 | for (const Channel *channel : *event_loop->configuration()->channels()) { |
| 332 | CHECK(channel->has_source_node()); |
| 333 | |
| 334 | // Sent by us. |
| 335 | if (configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 336 | channel->has_destination_nodes()) { |
| 337 | for (const Connection *connection : *channel->destination_nodes()) { |
| 338 | const bool delivery_time_is_logged = |
| 339 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 340 | connection, event_loop->node()); |
| 341 | |
| 342 | // And the timestamps are then logged back by us again. |
| 343 | if (!delivery_time_is_logged) { |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | // (And only construct the sender if it hasn't been constructed) |
| 348 | const Node *other_node = configuration::GetNode( |
| 349 | event_loop->configuration(), connection->name()->string_view()); |
| 350 | const size_t other_node_index = configuration::GetNodeIndex( |
| 351 | event_loop->configuration(), other_node); |
| 352 | |
| 353 | if (!timestamp_loggers[other_node_index]) { |
| 354 | timestamp_loggers[other_node_index] = |
| 355 | event_loop->MakeSender<logger::MessageHeader>( |
| 356 | absl::StrCat("/aos/remote_timestamps/", |
| 357 | connection->name()->string_view())); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 364 | } // namespace message_bridge |
| 365 | } // namespace aos |