blob: 45bc6e7b64e760fc16f06c88dfb8daa1e9ec89c2 [file] [log] [blame]
Austin Schuh898f4972020-01-11 17:21:25 -08001#include "aos/events/simulated_network_bridge.h"
2
Austin Schuh0de30f32020-12-06 12:44:28 -08003#include "absl/strings/str_cat.h"
4#include "aos/configuration.h"
Austin Schuh898f4972020-01-11 17:21:25 -08005#include "aos/events/event_loop.h"
6#include "aos/events/simulated_event_loop.h"
Austin Schuh0de30f32020-12-06 12:44:28 -08007#include "aos/network/remote_message_generated.h"
Austin Schuh898f4972020-01-11 17:21:25 -08008
9namespace aos {
10namespace message_bridge {
11
12// This class delays messages forwarded between two factories.
13//
14// The basic design is that we need to use the distributed_clock to convert
15// monotonic times from the source to the destination node. We also use a
16// fetcher to manage the queue of data, and a timer to schedule the sends.
17class RawMessageDelayer {
18 public:
19 RawMessageDelayer(aos::NodeEventLoopFactory *fetch_node_factory,
20 aos::NodeEventLoopFactory *send_node_factory,
21 aos::EventLoop *send_event_loop,
22 std::unique_ptr<aos::RawFetcher> fetcher,
Austin Schuh4c3b9702020-08-30 11:34:55 -070023 std::unique_ptr<aos::RawSender> sender,
Austin Schuh20ac95d2020-12-05 17:24:19 -080024 MessageBridgeServerStatus *server_status,
25 size_t destination_node_index,
Austin Schuh4c3b9702020-08-30 11:34:55 -070026 ServerConnection *server_connection, int client_index,
Austin Schuh2f8fd752020-09-01 22:38:28 -070027 MessageBridgeClientStatus *client_status,
28 size_t channel_index,
Austin Schuh0de30f32020-12-06 12:44:28 -080029 aos::Sender<RemoteMessage> *timestamp_logger)
Austin Schuh898f4972020-01-11 17:21:25 -080030 : fetch_node_factory_(fetch_node_factory),
31 send_node_factory_(send_node_factory),
32 send_event_loop_(send_event_loop),
33 fetcher_(std::move(fetcher)),
Austin Schuh4c3b9702020-08-30 11:34:55 -070034 sender_(std::move(sender)),
Austin Schuh20ac95d2020-12-05 17:24:19 -080035 server_status_(server_status),
36 destination_node_index_(destination_node_index),
Austin Schuh4c3b9702020-08-30 11:34:55 -070037 server_connection_(server_connection),
38 client_status_(client_status),
39 client_index_(client_index),
Austin Schuh2f8fd752020-09-01 22:38:28 -070040 client_connection_(client_status_->GetClientConnection(client_index)),
41 channel_index_(channel_index),
42 timestamp_logger_(timestamp_logger) {
Austin Schuh898f4972020-01-11 17:21:25 -080043 timer_ = send_event_loop_->AddTimer([this]() { Send(); });
44
45 Schedule();
46 }
47
Austin Schuh6f3babe2020-01-26 20:34:50 -080048 const Channel *channel() const { return fetcher_->channel(); }
49
Austin Schuh4c570ea2020-11-19 23:13:24 -080050 uint32_t time_to_live() {
51 return configuration::ConnectionToNode(sender_->channel(),
52 send_node_factory_->node())
53 ->time_to_live();
54 }
55
Austin Schuh898f4972020-01-11 17:21:25 -080056 // Kicks us to re-fetch and schedule the timer.
57 void Schedule() {
Austin Schuh6aa77be2020-02-22 21:06:40 -080058 // Keep pulling messages out of the fetcher until we find one in the future.
59 while (true) {
60 if (fetcher_->context().data == nullptr || sent_) {
61 sent_ = !fetcher_->FetchNext();
62 }
63 if (sent_) {
64 break;
65 }
66 if (fetcher_->context().monotonic_event_time +
67 send_node_factory_->network_delay() +
68 send_node_factory_->send_delay() >
69 fetch_node_factory_->monotonic_now()) {
70 break;
71 }
72
73 // TODO(austin): Not cool. We want to actually forward these. This means
74 // we need a more sophisticated concept of what is running.
75 LOG(WARNING) << "Not forwarding message on "
76 << configuration::CleanedChannelToString(fetcher_->channel())
77 << " because we aren't running. Set at "
78 << fetcher_->context().monotonic_event_time << " now is "
79 << fetch_node_factory_->monotonic_now();
80 sent_ = true;
Austin Schuh4c3b9702020-08-30 11:34:55 -070081 server_connection_->mutate_dropped_packets(
82 server_connection_->dropped_packets() + 1);
Austin Schuh898f4972020-01-11 17:21:25 -080083 }
84
85 if (fetcher_->context().data == nullptr) {
86 return;
87 }
88
89 if (sent_) {
90 return;
91 }
92
93 // Compute the time to publish this message.
94 const monotonic_clock::time_point monotonic_delivered_time =
95 DeliveredTime(fetcher_->context());
96
97 CHECK_GE(monotonic_delivered_time, send_node_factory_->monotonic_now())
Austin Schuh2febf0d2020-09-21 22:24:30 -070098 << ": Trying to deliver message in the past on channel "
99 << configuration::StrippedChannelToString(fetcher_->channel())
100 << " to node " << send_event_loop_->node()->name()->string_view()
101 << " sent from " << fetcher_->channel()->source_node()->string_view()
102 << " at " << fetch_node_factory_->monotonic_now();
Austin Schuh898f4972020-01-11 17:21:25 -0800103
Austin Schuh4c3b9702020-08-30 11:34:55 -0700104 server_connection_->mutate_sent_packets(server_connection_->sent_packets() +
105 1);
Austin Schuh898f4972020-01-11 17:21:25 -0800106 timer_->Setup(monotonic_delivered_time);
107 }
108
109 private:
110 // Acutally sends the message, and reschedules.
111 void Send() {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700112 // Fill out the send times.
Austin Schuh898f4972020-01-11 17:21:25 -0800113 sender_->Send(fetcher_->context().data, fetcher_->context().size,
114 fetcher_->context().monotonic_event_time,
115 fetcher_->context().realtime_event_time,
116 fetcher_->context().queue_index);
117
Austin Schuh4c3b9702020-08-30 11:34:55 -0700118 // And simulate message_bridge's offset recovery.
119 client_status_->SampleFilter(client_index_,
120 fetcher_->context().monotonic_event_time,
121 sender_->monotonic_sent_time());
122
123 client_connection_->mutate_received_packets(
124 client_connection_->received_packets() + 1);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700125
126 if (timestamp_logger_) {
Austin Schuh0de30f32020-12-06 12:44:28 -0800127 aos::Sender<RemoteMessage>::Builder builder =
Austin Schuh2f8fd752020-09-01 22:38:28 -0700128 timestamp_logger_->MakeBuilder();
129
Austin Schuh20ac95d2020-12-05 17:24:19 -0800130 // Reset the filter every time the UUID changes. There's probably a more
131 // clever way to do this, but that means a better concept of rebooting.
132 if (server_status_->BootUUID(destination_node_index_) !=
133 send_node_factory_->boot_uuid().string_view()) {
134 server_status_->ResetFilter(destination_node_index_);
135 server_status_->SetBootUUID(
136 destination_node_index_,
137 send_node_factory_->boot_uuid().string_view());
138 }
139
140 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
141 builder.fbb()->CreateString(
142 send_node_factory_->boot_uuid().string_view());
143
Austin Schuh0de30f32020-12-06 12:44:28 -0800144 RemoteMessage::Builder message_header_builder =
145 builder.MakeBuilder<RemoteMessage>();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700146
147 message_header_builder.add_channel_index(channel_index_);
148
149 // Swap the remote and sent metrics. They are from the sender's
150 // perspective, not the receiver's perspective.
151 message_header_builder.add_monotonic_remote_time(
152 fetcher_->context().monotonic_event_time.time_since_epoch().count());
153 message_header_builder.add_realtime_remote_time(
154 fetcher_->context().realtime_event_time.time_since_epoch().count());
155 message_header_builder.add_remote_queue_index(
156 fetcher_->context().queue_index);
157
158 message_header_builder.add_monotonic_sent_time(
159 sender_->monotonic_sent_time().time_since_epoch().count());
160 message_header_builder.add_realtime_sent_time(
161 sender_->realtime_sent_time().time_since_epoch().count());
162 message_header_builder.add_queue_index(sender_->sent_queue_index());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800163 message_header_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700164
165 builder.Send(message_header_builder.Finish());
166 }
167
Austin Schuh898f4972020-01-11 17:21:25 -0800168 sent_ = true;
169 Schedule();
170 }
171
172 // Converts from time on the sending node to time on the receiving node.
173 monotonic_clock::time_point DeliveredTime(const Context &context) const {
174 const distributed_clock::time_point distributed_sent_time =
175 fetch_node_factory_->ToDistributedClock(context.monotonic_event_time);
176
Austin Schuh2febf0d2020-09-21 22:24:30 -0700177 return send_node_factory_->FromDistributedClock(
178 distributed_sent_time + send_node_factory_->network_delay() +
179 send_node_factory_->send_delay());
Austin Schuh898f4972020-01-11 17:21:25 -0800180 }
181
182 // Factories used for time conversion.
183 aos::NodeEventLoopFactory *fetch_node_factory_;
184 aos::NodeEventLoopFactory *send_node_factory_;
185
186 // Event loop which sending is scheduled on.
187 aos::EventLoop *send_event_loop_;
188 // Timer used to send.
189 aos::TimerHandler *timer_;
190 // Fetcher used to receive messages.
191 std::unique_ptr<aos::RawFetcher> fetcher_;
192 // Sender to send them back out.
193 std::unique_ptr<aos::RawSender> sender_;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800194
195 MessageBridgeServerStatus *server_status_;
196 const size_t destination_node_index_;
Austin Schuh898f4972020-01-11 17:21:25 -0800197 // True if we have sent the message in the fetcher.
198 bool sent_ = false;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700199
200 ServerConnection *server_connection_ = nullptr;
201 MessageBridgeClientStatus *client_status_ = nullptr;
202 int client_index_;
203 ClientConnection *client_connection_ = nullptr;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700204
205 size_t channel_index_;
Austin Schuh0de30f32020-12-06 12:44:28 -0800206 aos::Sender<RemoteMessage> *timestamp_logger_ = nullptr;
Austin Schuh898f4972020-01-11 17:21:25 -0800207};
208
209SimulatedMessageBridge::SimulatedMessageBridge(
210 SimulatedEventLoopFactory *simulated_event_loop_factory) {
211 CHECK(
212 configuration::MultiNode(simulated_event_loop_factory->configuration()));
213
214 // Pre-build up event loops for every node. They are pretty cheap anyways.
215 for (const Node *node : simulated_event_loop_factory->nodes()) {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700216 auto it = event_loop_map_.emplace(std::make_pair(
217 node,
218 simulated_event_loop_factory->MakeEventLoop("message_bridge", node)));
Austin Schuhcde938c2020-02-02 17:30:07 -0800219
220 CHECK(it.second);
221
Austin Schuh4c3b9702020-08-30 11:34:55 -0700222 it.first->second.event_loop->SkipTimingReport();
223 it.first->second.event_loop->SkipAosLog();
224
225 for (ServerConnection *connection :
226 it.first->second.server_status.server_connection()) {
227 if (connection == nullptr) continue;
228
229 connection->mutate_state(message_bridge::State::CONNECTED);
230 }
231
232 for (size_t i = 0;
233 i < it.first->second.client_status.mutable_client_statistics()
234 ->mutable_connections()
235 ->size();
236 ++i) {
237 ClientConnection *connection =
238 it.first->second.client_status.mutable_client_statistics()
239 ->mutable_connections()
240 ->GetMutableObject(i);
241 if (connection == nullptr) continue;
242
243 connection->mutate_state(message_bridge::State::CONNECTED);
244 }
Austin Schuh898f4972020-01-11 17:21:25 -0800245 }
246
Austin Schuh20ac95d2020-12-05 17:24:19 -0800247 for (const Node *node : simulated_event_loop_factory->nodes()) {
248 auto it = event_loop_map_.find(node);
249
250 CHECK(it != event_loop_map_.end());
251
252 size_t node_index = 0;
253 for (ServerConnection *connection :
254 it->second.server_status.server_connection()) {
255 if (connection != nullptr) {
256 const Node *client_node =
257 simulated_event_loop_factory->configuration()->nodes()->Get(
258 node_index);
259 auto client_event_loop = event_loop_map_.find(client_node);
260 it->second.server_status.ResetFilter(node_index);
261 it->second.server_status.SetBootUUID(
262 node_index,
263 client_event_loop->second.event_loop->boot_uuid().string_view());
264 }
265 ++node_index;
266 }
267 }
268
Austin Schuh898f4972020-01-11 17:21:25 -0800269 for (const Channel *channel :
270 *simulated_event_loop_factory->configuration()->channels()) {
271 if (!channel->has_destination_nodes()) {
272 continue;
273 }
274
275 // Find the sending node.
276 const Node *node =
277 configuration::GetNode(simulated_event_loop_factory->configuration(),
278 channel->source_node()->string_view());
279 auto source_event_loop = event_loop_map_.find(node);
280 CHECK(source_event_loop != event_loop_map_.end());
281
282 std::unique_ptr<DelayersVector> delayers =
283 std::make_unique<DelayersVector>();
284
285 // And then build up a RawMessageDelayer for each destination.
286 for (const Connection *connection : *channel->destination_nodes()) {
287 const Node *destination_node =
288 configuration::GetNode(simulated_event_loop_factory->configuration(),
289 connection->name()->string_view());
290 auto destination_event_loop = event_loop_map_.find(destination_node);
291 CHECK(destination_event_loop != event_loop_map_.end());
292
Austin Schuh4c3b9702020-08-30 11:34:55 -0700293 ServerConnection *server_connection =
294 source_event_loop->second.server_status.FindServerConnection(
295 connection->name()->string_view());
296
297 int client_index =
298 destination_event_loop->second.client_status.FindClientIndex(
299 channel->source_node()->string_view());
300
Austin Schuh2f8fd752020-09-01 22:38:28 -0700301 const size_t destination_node_index = configuration::GetNodeIndex(
302 simulated_event_loop_factory->configuration(), destination_node);
303
304 const bool delivery_time_is_logged =
305 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
306 connection, source_event_loop->second.event_loop->node());
307
Austin Schuh898f4972020-01-11 17:21:25 -0800308 delayers->emplace_back(std::make_unique<RawMessageDelayer>(
309 simulated_event_loop_factory->GetNodeEventLoopFactory(node),
310 simulated_event_loop_factory->GetNodeEventLoopFactory(
311 destination_node),
Austin Schuh4c3b9702020-08-30 11:34:55 -0700312 destination_event_loop->second.event_loop.get(),
313 source_event_loop->second.event_loop->MakeRawFetcher(channel),
314 destination_event_loop->second.event_loop->MakeRawSender(channel),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800315 &source_event_loop->second.server_status, destination_node_index,
Austin Schuh4c3b9702020-08-30 11:34:55 -0700316 server_connection, client_index,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700317 &destination_event_loop->second.client_status,
318 configuration::ChannelIndex(
319 source_event_loop->second.event_loop->configuration(), channel),
320 delivery_time_is_logged
321 ? &source_event_loop->second
322 .timestamp_loggers[destination_node_index]
323 : nullptr));
Austin Schuh898f4972020-01-11 17:21:25 -0800324 }
325
Austin Schuh4c3b9702020-08-30 11:34:55 -0700326 const Channel *const timestamp_channel = configuration::GetChannel(
327 simulated_event_loop_factory->configuration(), "/aos",
328 Timestamp::GetFullyQualifiedName(),
329 source_event_loop->second.event_loop->name(), node);
330
331 if (channel == timestamp_channel) {
332 source_event_loop->second.server_status.set_send_data(
333 [captured_delayers = delayers.get()](const Context &) {
334 for (std::unique_ptr<RawMessageDelayer> &delayer :
335 *captured_delayers) {
336 delayer->Schedule();
337 }
338 });
339 } else {
340 // And register every delayer to be poked when a new message shows up.
Austin Schuh4c570ea2020-11-19 23:13:24 -0800341
342 source_event_loop->second.event_loop->OnRun([captured_delayers =
343 delayers.get()]() {
344 // Poke all the reliable delayers so they send any queued messages.
345 for (std::unique_ptr<RawMessageDelayer> &delayer : *captured_delayers) {
346 if (delayer->time_to_live() == 0) {
347 delayer->Schedule();
348 }
349 }
350 });
Austin Schuh4c3b9702020-08-30 11:34:55 -0700351 source_event_loop->second.event_loop->MakeRawNoArgWatcher(
352 channel, [captured_delayers = delayers.get()](const Context &) {
353 for (std::unique_ptr<RawMessageDelayer> &delayer :
354 *captured_delayers) {
355 delayer->Schedule();
356 }
357 });
358 }
Austin Schuh898f4972020-01-11 17:21:25 -0800359 delayers_list_.emplace_back(std::move(delayers));
360 }
361}
362
363SimulatedMessageBridge::~SimulatedMessageBridge() {}
364
Austin Schuh6f3babe2020-01-26 20:34:50 -0800365void SimulatedMessageBridge::DisableForwarding(const Channel *channel) {
366 for (std::unique_ptr<std::vector<std::unique_ptr<RawMessageDelayer>>>
367 &delayers : delayers_list_) {
368 if (delayers->size() > 0) {
369 if ((*delayers)[0]->channel() == channel) {
370 for (std::unique_ptr<RawMessageDelayer> &delayer : *delayers) {
371 CHECK(delayer->channel() == channel);
372 }
373
374 // If we clear the delayers list, nothing will be scheduled. Which is a
375 // success!
376 delayers->clear();
377 }
378 }
379 }
380}
381
Austin Schuh4c3b9702020-08-30 11:34:55 -0700382void SimulatedMessageBridge::DisableStatistics() {
383 for (std::pair<const Node *const, State> &state : event_loop_map_) {
384 state.second.server_status.DisableStatistics();
385 state.second.client_status.DisableStatistics();
386 }
387}
388
Austin Schuh2f8fd752020-09-01 22:38:28 -0700389SimulatedMessageBridge::State::State(
390 std::unique_ptr<aos::EventLoop> &&new_event_loop)
391 : event_loop(std::move(new_event_loop)),
392 server_status(event_loop.get()),
393 client_status(event_loop.get()) {
394 timestamp_loggers.resize(event_loop->configuration()->nodes()->size());
395
396 // Find all nodes which log timestamps back to us (from us).
397 for (const Channel *channel : *event_loop->configuration()->channels()) {
398 CHECK(channel->has_source_node());
399
400 // Sent by us.
401 if (configuration::ChannelIsSendableOnNode(channel, event_loop->node()) &&
402 channel->has_destination_nodes()) {
403 for (const Connection *connection : *channel->destination_nodes()) {
404 const bool delivery_time_is_logged =
405 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
406 connection, event_loop->node());
407
408 // And the timestamps are then logged back by us again.
409 if (!delivery_time_is_logged) {
410 continue;
411 }
412
413 // (And only construct the sender if it hasn't been constructed)
414 const Node *other_node = configuration::GetNode(
415 event_loop->configuration(), connection->name()->string_view());
416 const size_t other_node_index = configuration::GetNodeIndex(
417 event_loop->configuration(), other_node);
418
419 if (!timestamp_loggers[other_node_index]) {
420 timestamp_loggers[other_node_index] =
Austin Schuh0de30f32020-12-06 12:44:28 -0800421 event_loop->MakeSender<RemoteMessage>(
Austin Schuh2f8fd752020-09-01 22:38:28 -0700422 absl::StrCat("/aos/remote_timestamps/",
423 connection->name()->string_view()));
424 }
425 }
426 }
427 }
428}
429
Austin Schuh898f4972020-01-11 17:21:25 -0800430} // namespace message_bridge
431} // namespace aos