blob: 57f29bb1c511f81fbfaadf6a6c8f8ed62f03d9f8 [file] [log] [blame]
Austin Schuhb06f03b2021-02-17 22:00:37 -08001#ifndef AOS_EVENTS_LOGGING_LOG_READER_H_
2#define AOS_EVENTS_LOGGING_LOG_READER_H_
Austin Schuhe309d2a2019-11-29 13:25:21 -08003
Austin Schuh8bd96322020-02-13 21:18:22 -08004#include <chrono>
Austin Schuhe309d2a2019-11-29 13:25:21 -08005#include <deque>
Austin Schuh05b70472020-01-01 17:11:17 -08006#include <string_view>
Austin Schuh2f8fd752020-09-01 22:38:28 -07007#include <tuple>
Austin Schuh6f3babe2020-01-26 20:34:50 -08008#include <vector>
Austin Schuhe309d2a2019-11-29 13:25:21 -08009
Austin Schuhe309d2a2019-11-29 13:25:21 -080010#include "aos/events/event_loop.h"
Austin Schuhf6f9bf32020-10-11 14:37:43 -070011#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080012#include "aos/events/logging/logfile_utils.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080013#include "aos/events/logging/logger_generated.h"
Austin Schuh92547522019-12-28 14:33:43 -080014#include "aos/events/simulated_event_loop.h"
Austin Schuh2f8fd752020-09-01 22:38:28 -070015#include "aos/network/message_bridge_server_generated.h"
Austin Schuh0ca1fd32020-12-18 22:53:05 -080016#include "aos/network/multinode_timestamp_filter.h"
Austin Schuh0de30f32020-12-06 12:44:28 -080017#include "aos/network/remote_message_generated.h"
Austin Schuh8bd96322020-02-13 21:18:22 -080018#include "aos/network/timestamp_filter.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080019#include "aos/time/time.h"
Austin Schuh4385b142021-03-14 21:31:13 -070020#include "aos/uuid.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080021#include "flatbuffers/flatbuffers.h"
22
23namespace aos {
24namespace logger {
25
Austin Schuh6f3babe2020-01-26 20:34:50 -080026// We end up with one of the following 3 log file types.
27//
28// Single node logged as the source node.
29// -> Replayed just on the source node.
30//
31// Forwarding timestamps only logged from the perspective of the destination
32// node.
33// -> Matched with data on source node and logged.
34//
35// Forwarding timestamps with data logged as the destination node.
36// -> Replayed just as the destination
37// -> Replayed as the source (Much harder, ordering is not defined)
38//
39// Duplicate data logged. -> CHECK that it matches and explode otherwise.
40//
41// This can be boiled down to a set of constraints and tools.
42//
43// 1) Forwarding timestamps and data need to be logged separately.
44// 2) Any forwarded data logged on the destination node needs to be logged
45// separately such that it can be sorted.
46//
47// 1) Log reader needs to be able to sort a list of log files.
48// 2) Log reader needs to be able to merge sorted lists of log files.
49// 3) Log reader needs to be able to match timestamps with messages.
50//
51// We also need to be able to generate multiple views of a log file depending on
52// the target.
53
Austin Schuhe309d2a2019-11-29 13:25:21 -080054// Replays all the channels in the logfile to the event loop.
55class LogReader {
56 public:
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080057 // If you want to supply a new configuration that will be used for replay
58 // (e.g., to change message rates, or to populate an updated schema), then
59 // pass it in here. It must provide all the channels that the original logged
60 // config did.
Austin Schuh6f3babe2020-01-26 20:34:50 -080061 //
Austin Schuh287d43d2020-12-04 20:19:33 -080062 // The single file constructor calls SortParts internally.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080063 LogReader(std::string_view filename,
64 const Configuration *replay_configuration = nullptr);
Austin Schuh287d43d2020-12-04 20:19:33 -080065 LogReader(std::vector<LogFile> log_files,
Austin Schuh11d43732020-09-21 17:28:30 -070066 const Configuration *replay_configuration = nullptr);
James Kuszmaul7daef362019-12-31 18:28:17 -080067 ~LogReader();
Austin Schuhe309d2a2019-11-29 13:25:21 -080068
Austin Schuh6331ef92020-01-07 18:28:09 -080069 // Registers all the callbacks to send the log file data out on an event loop
70 // created in event_loop_factory. This also updates time to be at the start
71 // of the log file by running until the log file starts.
72 // Note: the configuration used in the factory should be configuration()
73 // below, but can be anything as long as the locations needed to send
74 // everything are available.
James Kuszmaul84ff3e52020-01-03 19:48:53 -080075 void Register(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuh58646e22021-08-23 23:51:46 -070076 // Registers all the callbacks to send the log file data out to an event loop
77 // factory. This does not start replaying or change the current distributed
78 // time of the factory. It does change the monotonic clocks to be right.
79 void RegisterWithoutStarting(SimulatedEventLoopFactory *event_loop_factory);
Austin Schuh6331ef92020-01-07 18:28:09 -080080 // Creates an SimulatedEventLoopFactory accessible via event_loop_factory(),
81 // and then calls Register.
82 void Register();
83 // Registers callbacks for all the events after the log file starts. This is
84 // only useful when replaying live.
Austin Schuhe309d2a2019-11-29 13:25:21 -080085 void Register(EventLoop *event_loop);
Austin Schuh6331ef92020-01-07 18:28:09 -080086
Austin Schuh58646e22021-08-23 23:51:46 -070087 // Called whenever a log file starts for a node.
88 void OnStart(std::function<void()> fn);
89 void OnStart(const Node *node, std::function<void()> fn);
90 // Called whenever a log file ends for a node.
91 void OnEnd(std::function<void()> fn);
92 void OnEnd(const Node *node, std::function<void()> fn);
93
James Kuszmaul84ff3e52020-01-03 19:48:53 -080094 // Unregisters the senders. You only need to call this if you separately
95 // supplied an event loop or event loop factory and the lifetimes are such
96 // that they need to be explicitly destroyed before the LogReader destructor
97 // gets called.
Austin Schuhe309d2a2019-11-29 13:25:21 -080098 void Deregister();
99
Austin Schuh0c297012020-09-16 18:41:59 -0700100 // Returns the configuration being used for replay from the log file.
101 // Note that this may be different from the configuration actually used for
102 // handling events. You should generally only use this to create a
103 // SimulatedEventLoopFactory, and then get the configuration from there for
104 // everything else.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800105 const Configuration *logged_configuration() const;
Austin Schuh11d43732020-09-21 17:28:30 -0700106 // Returns the configuration being used for replay from the log file.
107 // Note that this may be different from the configuration actually used for
108 // handling events. You should generally only use this to create a
109 // SimulatedEventLoopFactory, and then get the configuration from there for
110 // everything else.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800111 // The pointer is invalidated whenever RemapLoggedChannel is called.
Austin Schuh15649d62019-12-28 16:36:38 -0800112 const Configuration *configuration() const;
113
Austin Schuh6f3babe2020-01-26 20:34:50 -0800114 // Returns the nodes that this log file was created on. This is a list of
Austin Schuh07676622021-01-21 18:59:17 -0800115 // pointers to a node in the nodes() list inside logged_configuration().
116 std::vector<const Node *> LoggedNodes() const;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800117
118 // Returns the starting timestamp for the log file.
Austin Schuh11d43732020-09-21 17:28:30 -0700119 monotonic_clock::time_point monotonic_start_time(
120 const Node *node = nullptr) const;
121 realtime_clock::time_point realtime_start_time(
122 const Node *node = nullptr) const;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800123
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800124 // Causes the logger to publish the provided channel on a different name so
125 // that replayed applications can publish on the proper channel name without
126 // interference. This operates on raw channel names, without any node or
127 // application specific mappings.
128 void RemapLoggedChannel(std::string_view name, std::string_view type,
Austin Schuh0de30f32020-12-06 12:44:28 -0800129 std::string_view add_prefix = "/original",
130 std::string_view new_type = "");
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800131 template <typename T>
132 void RemapLoggedChannel(std::string_view name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800133 std::string_view add_prefix = "/original",
134 std::string_view new_type = "") {
135 RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix, new_type);
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800136 }
137
Austin Schuh01b4c352020-09-21 23:09:39 -0700138 // Remaps the provided channel, though this respects node mappings, and
139 // preserves them too. This makes it so if /aos -> /pi1/aos on one node,
140 // /original/aos -> /original/pi1/aos on the same node after renaming, just
Austin Schuh0de30f32020-12-06 12:44:28 -0800141 // like you would hope. If new_type is not empty, the new channel will use
142 // the provided type instead. This allows for renaming messages.
Austin Schuh01b4c352020-09-21 23:09:39 -0700143 //
144 // TODO(austin): If you have 2 nodes remapping something to the same channel,
145 // this doesn't handle that. No use cases exist yet for that, so it isn't
146 // being done yet.
147 void RemapLoggedChannel(std::string_view name, std::string_view type,
148 const Node *node,
Austin Schuh0de30f32020-12-06 12:44:28 -0800149 std::string_view add_prefix = "/original",
150 std::string_view new_type = "");
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700151 template <typename T>
Austin Schuh01b4c352020-09-21 23:09:39 -0700152 void RemapLoggedChannel(std::string_view name, const Node *node,
Austin Schuh0de30f32020-12-06 12:44:28 -0800153 std::string_view add_prefix = "/original",
154 std::string_view new_type = "") {
155 RemapLoggedChannel(name, T::GetFullyQualifiedName(), node, add_prefix,
156 new_type);
Austin Schuh01b4c352020-09-21 23:09:39 -0700157 }
158
159 template <typename T>
160 bool HasChannel(std::string_view name, const Node *node = nullptr) {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800161 return configuration::GetChannel(logged_configuration(), name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800162 T::GetFullyQualifiedName(), "", node,
163 true) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700164 }
165
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800166 // Returns true if the channel exists on the node and was logged.
167 template <typename T>
168 bool HasLoggedChannel(std::string_view name, const Node *node = nullptr) {
Austin Schuh5ee56872021-01-30 16:53:34 -0800169 const Channel *channel =
170 configuration::GetChannel(logged_configuration(), name,
171 T::GetFullyQualifiedName(), "", node, true);
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800172 if (channel == nullptr) return false;
173 return channel->logger() != LoggerConfig::NOT_LOGGED;
174 }
175
Austin Schuh1c227352021-09-17 12:53:54 -0700176 // Returns a list of all the original channels from remapping.
177 std::vector<const Channel *> RemappedChannels() const;
178
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800179 SimulatedEventLoopFactory *event_loop_factory() {
180 return event_loop_factory_;
181 }
182
Austin Schuh0ca51f32020-12-25 21:51:45 -0800183 std::string_view name() const { return log_files_[0].name; }
Austin Schuh0c297012020-09-16 18:41:59 -0700184
James Kuszmaul71a81932020-12-15 21:08:01 -0800185 // Set whether to exit the SimulatedEventLoopFactory when we finish reading
186 // the logfile.
187 void set_exit_on_finish(bool exit_on_finish) {
188 exit_on_finish_ = exit_on_finish;
189 }
190
Austin Schuhe309d2a2019-11-29 13:25:21 -0800191 private:
Austin Schuh58646e22021-08-23 23:51:46 -0700192 void Register(EventLoop *event_loop, const Node *node);
193
194 void RegisterDuringStartup(EventLoop *event_loop, const Node *node);
195
196 const Channel *RemapChannel(const EventLoop *event_loop, const Node *node,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800197 const Channel *channel);
198
Austin Schuhe309d2a2019-11-29 13:25:21 -0800199 // Queues at least max_out_of_order_duration_ messages into channels_.
200 void QueueMessages();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800201 // Handle constructing a configuration with all the additional remapped
202 // channels from calls to RemapLoggedChannel.
203 void MakeRemappedConfig();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800204
Austin Schuh2f8fd752020-09-01 22:38:28 -0700205 // Returns the number of nodes.
206 size_t nodes_count() const {
207 return !configuration::MultiNode(logged_configuration())
208 ? 1u
209 : logged_configuration()->nodes()->size();
210 }
211
Austin Schuh287d43d2020-12-04 20:19:33 -0800212 const std::vector<LogFile> log_files_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800213
Austin Schuh969cd602021-01-03 00:09:45 -0800214 // Class to manage sending RemoteMessages on the provided node after the
215 // correct delay.
Austin Schuh5ee56872021-01-30 16:53:34 -0800216 class RemoteMessageSender {
Austin Schuh969cd602021-01-03 00:09:45 -0800217 public:
218 RemoteMessageSender(aos::Sender<message_bridge::RemoteMessage> sender,
219 EventLoop *event_loop);
220 RemoteMessageSender(RemoteMessageSender const &) = delete;
221 RemoteMessageSender &operator=(RemoteMessageSender const &) = delete;
222
223 // Sends the provided message. If monotonic_timestamp_time is min_time,
224 // send it immediately.
225 void Send(
226 FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message,
Austin Schuh58646e22021-08-23 23:51:46 -0700227 BootTimestamp monotonic_timestamp_time, size_t source_boot_count);
Austin Schuh969cd602021-01-03 00:09:45 -0800228
229 private:
230 // Handles actually sending the timestamp if we were delayed.
231 void SendTimestamp();
232 // Handles scheduling the timer to send at the correct time.
233 void ScheduleTimestamp();
234
235 EventLoop *event_loop_;
236 aos::Sender<message_bridge::RemoteMessage> sender_;
237 aos::TimerHandler *timer_;
238
239 // Time we are scheduled for, or min_time if we aren't scheduled.
240 monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time;
241
242 struct Timestamp {
243 Timestamp(FlatbufferDetachedBuffer<message_bridge::RemoteMessage>
244 new_remote_message,
245 monotonic_clock::time_point new_monotonic_timestamp_time)
246 : remote_message(std::move(new_remote_message)),
247 monotonic_timestamp_time(new_monotonic_timestamp_time) {}
248 FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message;
249 monotonic_clock::time_point monotonic_timestamp_time;
250 };
251
252 // List of messages to send. The timer works through them and then disables
253 // itself automatically.
254 std::deque<Timestamp> remote_timestamps_;
255 };
256
Austin Schuh6f3babe2020-01-26 20:34:50 -0800257 // State per node.
Austin Schuh858c9f32020-08-31 16:56:12 -0700258 class State {
259 public:
Austin Schuh58646e22021-08-23 23:51:46 -0700260 State(std::unique_ptr<TimestampMapper> timestamp_mapper, const Node *node);
Austin Schuh287d43d2020-12-04 20:19:33 -0800261
262 // Connects up the timestamp mappers.
263 void AddPeer(State *peer);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800264
Austin Schuhe639ea12021-01-25 13:00:22 -0800265 TimestampMapper *timestamp_mapper() { return timestamp_mapper_.get(); }
266
Austin Schuhdda74ec2021-01-03 19:30:37 -0800267 // Returns the next sorted message with all the timestamps extracted and
268 // matched.
269 TimestampedMessage PopOldest();
Austin Schuh188eabe2020-12-29 23:41:13 -0800270
Austin Schuh858c9f32020-08-31 16:56:12 -0700271 // Returns the monotonic time of the oldest message.
Austin Schuh58646e22021-08-23 23:51:46 -0700272 BootTimestamp OldestMessageTime() const;
273
274 size_t boot_count() const {
275 // If we are replaying directly into an event loop, we can't reboot. So
276 // we will stay stuck on the 0th boot.
277 if (!node_event_loop_factory_) return 0u;
278 return node_event_loop_factory_->boot_count();
279 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700280
281 // Primes the queues inside State. Should be called before calling
282 // OldestMessageTime.
283 void SeedSortedMessages();
Austin Schuh8bd96322020-02-13 21:18:22 -0800284
Austin Schuh58646e22021-08-23 23:51:46 -0700285 void SetupStartupTimer() {
286 const monotonic_clock::time_point start_time =
287 monotonic_start_time(boot_count());
288 if (start_time == monotonic_clock::min_time) {
289 LOG(ERROR)
290 << "No start time, skipping, please figure out when this happens";
291 RunOnStart();
292 return;
293 }
James Kuszmaul57d39742021-10-15 20:07:34 -0700294 CHECK_GE(start_time, event_loop_->monotonic_now());
Austin Schuh58646e22021-08-23 23:51:46 -0700295 startup_timer_->Setup(start_time);
296 }
297
298 void set_startup_timer(TimerHandler *timer_handler) {
299 startup_timer_ = timer_handler;
300 if (startup_timer_) {
301 if (event_loop_->node() != nullptr) {
302 startup_timer_->set_name(absl::StrCat(
303 event_loop_->node()->name()->string_view(), "_startup"));
304 } else {
305 startup_timer_->set_name("startup");
306 }
307 }
308 }
309
Austin Schuh858c9f32020-08-31 16:56:12 -0700310 // Returns the starting time for this node.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700311 monotonic_clock::time_point monotonic_start_time(size_t boot_count) const {
312 return timestamp_mapper_
313 ? timestamp_mapper_->monotonic_start_time(boot_count)
314 : monotonic_clock::min_time;
Austin Schuh858c9f32020-08-31 16:56:12 -0700315 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700316 realtime_clock::time_point realtime_start_time(size_t boot_count) const {
317 return timestamp_mapper_
318 ? timestamp_mapper_->realtime_start_time(boot_count)
319 : realtime_clock::min_time;
Austin Schuh858c9f32020-08-31 16:56:12 -0700320 }
321
322 // Sets the node event loop factory for replaying into a
323 // SimulatedEventLoopFactory. Returns the EventLoop to use.
Austin Schuh58646e22021-08-23 23:51:46 -0700324 void SetNodeEventLoopFactory(NodeEventLoopFactory *node_event_loop_factory);
Austin Schuh858c9f32020-08-31 16:56:12 -0700325
326 // Sets and gets the event loop to use.
327 void set_event_loop(EventLoop *event_loop) { event_loop_ = event_loop; }
328 EventLoop *event_loop() { return event_loop_; }
329
Austin Schuh58646e22021-08-23 23:51:46 -0700330 const Node *node() const { return node_; }
331
332 void Register(EventLoop *event_loop);
333
334 void OnStart(std::function<void()> fn);
335 void OnEnd(std::function<void()> fn);
336
Austin Schuh858c9f32020-08-31 16:56:12 -0700337 // Sets the current realtime offset from the monotonic clock for this node
338 // (if we are on a simulated event loop).
339 void SetRealtimeOffset(monotonic_clock::time_point monotonic_time,
340 realtime_clock::time_point realtime_time) {
341 if (node_event_loop_factory_ != nullptr) {
342 node_event_loop_factory_->SetRealtimeOffset(monotonic_time,
343 realtime_time);
344 }
345 }
346
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700347 // Returns the MessageHeader sender to log delivery timestamps to for the
348 // provided remote node.
Austin Schuh61e973f2021-02-21 21:43:56 -0800349 RemoteMessageSender *RemoteTimestampSender(const Channel *channel,
350 const Connection *connection);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700351
Austin Schuh858c9f32020-08-31 16:56:12 -0700352 // Converts a timestamp from the monotonic clock on this node to the
353 // distributed clock.
354 distributed_clock::time_point ToDistributedClock(
355 monotonic_clock::time_point time) {
356 return node_event_loop_factory_->ToDistributedClock(time);
357 }
358
Austin Schuh858c9f32020-08-31 16:56:12 -0700359 // Returns the current time on the remote node which sends messages on
360 // channel_index.
Austin Schuh58646e22021-08-23 23:51:46 -0700361 BootTimestamp monotonic_remote_now(size_t channel_index) {
362 State *s = channel_source_state_[channel_index];
363 return BootTimestamp{
364 .boot = s->boot_count(),
365 .time = s->node_event_loop_factory_->monotonic_now()};
Austin Schuh858c9f32020-08-31 16:56:12 -0700366 }
367
Austin Schuh5ee56872021-01-30 16:53:34 -0800368 // Returns the start time of the remote for the provided channel.
369 monotonic_clock::time_point monotonic_remote_start_time(
Austin Schuh58646e22021-08-23 23:51:46 -0700370 size_t boot_count, size_t channel_index) {
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700371 return channel_source_state_[channel_index]->monotonic_start_time(
372 boot_count);
Austin Schuh5ee56872021-01-30 16:53:34 -0800373 }
374
Austin Schuh58646e22021-08-23 23:51:46 -0700375 void DestroyEventLoop() { event_loop_unique_ptr_.reset(); }
376
377 EventLoop *MakeEventLoop() {
378 CHECK(!event_loop_unique_ptr_);
379 event_loop_unique_ptr_ =
380 node_event_loop_factory_->MakeEventLoop("log_reader");
381 return event_loop_unique_ptr_.get();
382 }
383
Austin Schuh2f8fd752020-09-01 22:38:28 -0700384 distributed_clock::time_point RemoteToDistributedClock(
385 size_t channel_index, monotonic_clock::time_point time) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700386 return channel_source_state_[channel_index]
387 ->node_event_loop_factory_->ToDistributedClock(time);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700388 }
389
390 const Node *remote_node(size_t channel_index) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700391 return channel_source_state_[channel_index]
392 ->node_event_loop_factory_->node();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700393 }
394
395 monotonic_clock::time_point monotonic_now() {
396 return node_event_loop_factory_->monotonic_now();
397 }
398
Austin Schuh858c9f32020-08-31 16:56:12 -0700399 // Sets the number of channels.
400 void SetChannelCount(size_t count);
401
402 // Sets the sender, filter, and target factory for a channel.
Austin Schuh969cd602021-01-03 00:09:45 -0800403 void SetChannel(size_t logged_channel_index, size_t factory_channel_index,
404 std::unique_ptr<RawSender> sender,
405 message_bridge::NoncausalOffsetEstimator *filter,
Austin Schuh58646e22021-08-23 23:51:46 -0700406 bool is_forwarded, State *source_state);
407
408 void SetRemoteTimestampSender(size_t logged_channel_index,
409 RemoteMessageSender *remote_timestamp_sender);
410
411 void RunOnStart();
412 void RunOnEnd();
Austin Schuh858c9f32020-08-31 16:56:12 -0700413
Austin Schuh858c9f32020-08-31 16:56:12 -0700414 // Unregisters everything so we can destory the event loop.
Austin Schuh58646e22021-08-23 23:51:46 -0700415 // TODO(austin): Is this needed? OnShutdown should be able to serve this
416 // need.
Austin Schuh858c9f32020-08-31 16:56:12 -0700417 void Deregister();
418
419 // Sets the current TimerHandle for the replay callback.
420 void set_timer_handler(TimerHandler *timer_handler) {
421 timer_handler_ = timer_handler;
Austin Schuh58646e22021-08-23 23:51:46 -0700422 if (timer_handler_) {
423 if (event_loop_->node() != nullptr) {
424 timer_handler_->set_name(absl::StrCat(
425 event_loop_->node()->name()->string_view(), "_main"));
426 } else {
427 timer_handler_->set_name("main");
428 }
429 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700430 }
431
432 // Sets the next wakeup time on the replay callback.
433 void Setup(monotonic_clock::time_point next_time) {
434 timer_handler_->Setup(next_time);
435 }
436
437 // Sends a buffer on the provided channel index.
Austin Schuh287d43d2020-12-04 20:19:33 -0800438 bool Send(const TimestampedMessage &timestamped_message);
Austin Schuh858c9f32020-08-31 16:56:12 -0700439
440 // Returns a debug string for the channel merger.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700441 std::string DebugString() const {
Austin Schuh287d43d2020-12-04 20:19:33 -0800442 if (!timestamp_mapper_) {
Austin Schuhe639ea12021-01-25 13:00:22 -0800443 return "";
Austin Schuh287d43d2020-12-04 20:19:33 -0800444 }
Austin Schuhe639ea12021-01-25 13:00:22 -0800445 return timestamp_mapper_->DebugString();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700446 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700447
Austin Schuh58646e22021-08-23 23:51:46 -0700448 void ClearRemoteTimestampSenders() {
449 channel_timestamp_loggers_.clear();
450 timestamp_loggers_.clear();
451 }
452
Austin Schuh858c9f32020-08-31 16:56:12 -0700453 private:
454 // Log file.
Austin Schuh287d43d2020-12-04 20:19:33 -0800455 std::unique_ptr<TimestampMapper> timestamp_mapper_;
Austin Schuh858c9f32020-08-31 16:56:12 -0700456
Austin Schuh858c9f32020-08-31 16:56:12 -0700457 // Senders.
458 std::vector<std::unique_ptr<RawSender>> channels_;
Austin Schuh969cd602021-01-03 00:09:45 -0800459 std::vector<RemoteMessageSender *> remote_timestamp_senders_;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700460 // The mapping from logged channel index to sent channel index. Needed for
461 // sending out MessageHeaders.
462 std::vector<int> factory_channel_index_;
463
Austin Schuh9942bae2021-01-07 22:06:44 -0800464 struct ContiguousSentTimestamp {
465 // Most timestamps make it through the network, so it saves a ton of
466 // memory and CPU to store the start and end, and search for valid ranges.
467 // For one of the logs I looked at, we had 2 ranges for 4 days.
468 //
469 // Save monotonic times as well to help if a queue index ever wraps. Odds
470 // are very low, but doesn't hurt.
471 //
472 // The starting time and matching queue index.
473 monotonic_clock::time_point starting_monotonic_event_time =
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700474 monotonic_clock::min_time;
Austin Schuh9942bae2021-01-07 22:06:44 -0800475 uint32_t starting_queue_index = 0xffffffff;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700476
Austin Schuh9942bae2021-01-07 22:06:44 -0800477 // Ending time and queue index.
478 monotonic_clock::time_point ending_monotonic_event_time =
479 monotonic_clock::max_time;
480 uint32_t ending_queue_index = 0xffffffff;
481
482 // The queue index that the first message was *actually* sent with. The
483 // queue indices are assumed to be contiguous through this range.
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700484 uint32_t actual_queue_index = 0xffffffff;
485 };
486
487 // Stores all the timestamps that have been sent on this channel. This is
488 // only done for channels which are forwarded and on the node which
Austin Schuh9942bae2021-01-07 22:06:44 -0800489 // initially sends the message. Compress using ranges and offsets.
490 std::vector<std::unique_ptr<std::vector<ContiguousSentTimestamp>>>
491 queue_index_map_;
Austin Schuh858c9f32020-08-31 16:56:12 -0700492
493 // Factory (if we are in sim) that this loop was created on.
494 NodeEventLoopFactory *node_event_loop_factory_ = nullptr;
495 std::unique_ptr<EventLoop> event_loop_unique_ptr_;
496 // Event loop.
Austin Schuh58646e22021-08-23 23:51:46 -0700497 const Node *node_ = nullptr;
Austin Schuh858c9f32020-08-31 16:56:12 -0700498 EventLoop *event_loop_ = nullptr;
499 // And timer used to send messages.
Austin Schuh58646e22021-08-23 23:51:46 -0700500 TimerHandler *timer_handler_ = nullptr;
501 TimerHandler *startup_timer_ = nullptr;
Austin Schuh858c9f32020-08-31 16:56:12 -0700502
Austin Schuh8bd96322020-02-13 21:18:22 -0800503 // Filters (or nullptr if it isn't a forwarded channel) for each channel.
504 // This corresponds to the object which is shared among all the channels
505 // going between 2 nodes. The second element in the tuple indicates if this
506 // is the primary direction or not.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700507 std::vector<message_bridge::NoncausalOffsetEstimator *> filters_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800508
509 // List of NodeEventLoopFactorys (or nullptr if it isn't a forwarded
510 // channel) which correspond to the originating node.
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700511 std::vector<State *> channel_source_state_;
512
Austin Schuh61e973f2021-02-21 21:43:56 -0800513 // This is a cache for channel, connection mapping to the corresponding
514 // sender.
515 absl::btree_map<std::pair<const Channel *, const Connection *>,
516 std::shared_ptr<RemoteMessageSender>>
517 channel_timestamp_loggers_;
518
519 // Mapping from resolved RemoteMessage channel to RemoteMessage sender. This
520 // is the channel that timestamps are published to.
521 absl::btree_map<const Channel *, std::shared_ptr<RemoteMessageSender>>
522 timestamp_loggers_;
Austin Schuh58646e22021-08-23 23:51:46 -0700523
524 std::vector<std::function<void()>> on_starts_;
525 std::vector<std::function<void()>> on_ends_;
526
527 bool stopped_ = false;
528 bool started_ = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800529 };
530
Austin Schuh8bd96322020-02-13 21:18:22 -0800531 // Node index -> State.
532 std::vector<std::unique_ptr<State>> states_;
533
534 // Creates the requested filter if it doesn't exist, regardless of whether
535 // these nodes can actually communicate directly. The second return value
536 // reports if this is the primary direction or not.
Austin Schuh2f8fd752020-09-01 22:38:28 -0700537 message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a,
538 const Node *node_b);
Austin Schuh8bd96322020-02-13 21:18:22 -0800539
Austin Schuh8bd96322020-02-13 21:18:22 -0800540 // List of filters for a connection. The pointer to the first node will be
541 // less than the second node.
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800542 std::unique_ptr<message_bridge::MultiNodeNoncausalOffsetEstimator> filters_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800543
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800544 std::unique_ptr<FlatbufferDetachedBuffer<Configuration>>
545 remapped_configuration_buffer_;
546
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800547 std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_;
548 SimulatedEventLoopFactory *event_loop_factory_ = nullptr;
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800549
550 // Map of channel indices to new name. The channel index will be an index into
551 // logged_configuration(), and the string key will be the name of the channel
552 // to send on instead of the logged channel name.
Austin Schuh0de30f32020-12-06 12:44:28 -0800553 struct RemappedChannel {
554 std::string remapped_name;
555 std::string new_type;
556 };
557 std::map<size_t, RemappedChannel> remapped_channels_;
Austin Schuh01b4c352020-09-21 23:09:39 -0700558 std::vector<MapT> maps_;
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800559
Austin Schuh6f3babe2020-01-26 20:34:50 -0800560 // Number of nodes which still have data to send. This is used to figure out
561 // when to exit.
562 size_t live_nodes_ = 0;
563
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800564 const Configuration *remapped_configuration_ = nullptr;
565 const Configuration *replay_configuration_ = nullptr;
Austin Schuhcde938c2020-02-02 17:30:07 -0800566
567 // If true, the replay timer will ignore any missing data. This is used
568 // during startup when we are bootstrapping everything and trying to get to
569 // the start of all the log files.
570 bool ignore_missing_data_ = false;
James Kuszmaul71a81932020-12-15 21:08:01 -0800571
572 // Whether to exit the SimulatedEventLoop when we finish reading the logs.
573 bool exit_on_finish_ = true;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800574};
575
576} // namespace logger
577} // namespace aos
578
Austin Schuhb06f03b2021-02-17 22:00:37 -0800579#endif // AOS_EVENTS_LOGGING_LOG_READER_H_