blob: f30649204cbee11f2bc9a6a764e30f4100831a6f [file] [log] [blame]
Austin Schuhb06f03b2021-02-17 22:00:37 -08001#include "aos/events/logging/log_reader.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08002
3#include <fcntl.h>
4#include <sys/stat.h>
5#include <sys/types.h>
6#include <sys/uio.h>
Brian Silverman8ff74aa2021-02-05 16:37:15 -08007
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008#include <climits>
Austin Schuhe309d2a2019-11-29 13:25:21 -08009#include <vector>
10
Austin Schuh2f8fd752020-09-01 22:38:28 -070011#include "absl/strings/escaping.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080012#include "absl/types/span.h"
13#include "aos/events/event_loop.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070014#include "aos/events/logging/boot_timestamp.h"
Austin Schuhf6f9bf32020-10-11 14:37:43 -070015#include "aos/events/logging/logfile_sorting.h"
James Kuszmaul38735e82019-12-07 16:42:06 -080016#include "aos/events/logging/logger_generated.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080017#include "aos/flatbuffer_merge.h"
Austin Schuh0ca1fd32020-12-18 22:53:05 -080018#include "aos/network/multinode_timestamp_filter.h"
Austin Schuh0de30f32020-12-06 12:44:28 -080019#include "aos/network/remote_message_generated.h"
20#include "aos/network/remote_message_schema.h"
Austin Schuh288479d2019-12-18 19:47:52 -080021#include "aos/network/team_number.h"
Austin Schuh61e973f2021-02-21 21:43:56 -080022#include "aos/network/timestamp_channel.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080023#include "aos/time/time.h"
Brian Silvermanae7c0332020-09-30 16:58:23 -070024#include "aos/util/file.h"
Austin Schuh4385b142021-03-14 21:31:13 -070025#include "aos/uuid.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080026#include "flatbuffers/flatbuffers.h"
Austin Schuh8c399962020-12-25 21:51:45 -080027#include "openssl/sha.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -080028
Austin Schuh15649d62019-12-28 16:36:38 -080029DEFINE_bool(skip_missing_forwarding_entries, false,
30 "If true, drop any forwarding entries with missing data. If "
31 "false, CHECK.");
Austin Schuhe309d2a2019-11-29 13:25:21 -080032
Austin Schuh0ca1fd32020-12-18 22:53:05 -080033DECLARE_bool(timestamps_to_csv);
Austin Schuh8bd96322020-02-13 21:18:22 -080034
Austin Schuh2f8fd752020-09-01 22:38:28 -070035DEFINE_bool(skip_order_validation, false,
36 "If true, ignore any out of orderness in replay");
37
Austin Schuhf0688662020-12-19 15:37:45 -080038DEFINE_double(
39 time_estimation_buffer_seconds, 2.0,
40 "The time to buffer ahead in the log file to accurately reconstruct time.");
41
Austin Schuhe309d2a2019-11-29 13:25:21 -080042namespace aos {
Austin Schuh006a9f52021-04-07 16:24:18 -070043namespace configuration {
44// We don't really want to expose this publicly, but log reader doesn't really
45// want to re-implement it.
46void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
47 std::string *name, std::string_view type, const Node *node);
Tyler Chatowbf0609c2021-07-31 16:13:27 -070048} // namespace configuration
Austin Schuhe309d2a2019-11-29 13:25:21 -080049namespace logger {
Austin Schuh0afc4d12020-10-19 11:42:04 -070050namespace {
Austin Schuh8c399962020-12-25 21:51:45 -080051
Austin Schuh0de30f32020-12-06 12:44:28 -080052// Copies the channel, removing the schema as we go. If new_name is provided,
53// it is used instead of the name inside the channel. If new_type is provided,
54// it is used instead of the type in the channel.
55flatbuffers::Offset<Channel> CopyChannel(const Channel *c,
56 std::string_view new_name,
57 std::string_view new_type,
58 flatbuffers::FlatBufferBuilder *fbb) {
59 flatbuffers::Offset<flatbuffers::String> name_offset =
60 fbb->CreateSharedString(new_name.empty() ? c->name()->string_view()
61 : new_name);
62 flatbuffers::Offset<flatbuffers::String> type_offset =
63 fbb->CreateSharedString(new_type.empty() ? c->type()->str() : new_type);
64 flatbuffers::Offset<flatbuffers::String> source_node_offset =
65 c->has_source_node() ? fbb->CreateSharedString(c->source_node()->str())
66 : 0;
67
68 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>>
69 destination_nodes_offset =
70 aos::RecursiveCopyVectorTable(c->destination_nodes(), fbb);
71
72 flatbuffers::Offset<
73 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
74 logger_nodes_offset = aos::CopyVectorSharedString(c->logger_nodes(), fbb);
75
76 Channel::Builder channel_builder(*fbb);
77 channel_builder.add_name(name_offset);
78 channel_builder.add_type(type_offset);
79 if (c->has_frequency()) {
80 channel_builder.add_frequency(c->frequency());
81 }
82 if (c->has_max_size()) {
83 channel_builder.add_max_size(c->max_size());
84 }
85 if (c->has_num_senders()) {
86 channel_builder.add_num_senders(c->num_senders());
87 }
88 if (c->has_num_watchers()) {
89 channel_builder.add_num_watchers(c->num_watchers());
90 }
91 if (!source_node_offset.IsNull()) {
92 channel_builder.add_source_node(source_node_offset);
93 }
94 if (!destination_nodes_offset.IsNull()) {
95 channel_builder.add_destination_nodes(destination_nodes_offset);
96 }
97 if (c->has_logger()) {
98 channel_builder.add_logger(c->logger());
99 }
100 if (!logger_nodes_offset.IsNull()) {
101 channel_builder.add_logger_nodes(logger_nodes_offset);
102 }
103 if (c->has_read_method()) {
104 channel_builder.add_read_method(c->read_method());
105 }
106 if (c->has_num_readers()) {
107 channel_builder.add_num_readers(c->num_readers());
108 }
109 return channel_builder.Finish();
110}
111
Austin Schuhe309d2a2019-11-29 13:25:21 -0800112namespace chrono = std::chrono;
Austin Schuh0de30f32020-12-06 12:44:28 -0800113using message_bridge::RemoteMessage;
Austin Schuh0afc4d12020-10-19 11:42:04 -0700114} // namespace
Austin Schuhe309d2a2019-11-29 13:25:21 -0800115
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800116LogReader::LogReader(std::string_view filename,
117 const Configuration *replay_configuration)
Austin Schuh287d43d2020-12-04 20:19:33 -0800118 : LogReader(SortParts({std::string(filename)}), replay_configuration) {}
Austin Schuhfa895892020-01-07 20:07:41 -0800119
Austin Schuh287d43d2020-12-04 20:19:33 -0800120LogReader::LogReader(std::vector<LogFile> log_files,
Austin Schuhfa895892020-01-07 20:07:41 -0800121 const Configuration *replay_configuration)
Austin Schuh287d43d2020-12-04 20:19:33 -0800122 : log_files_(std::move(log_files)),
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800123 replay_configuration_(replay_configuration) {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800124 CHECK_GT(log_files_.size(), 0u);
125 {
126 // Validate that we have the same config everwhere. This will be true if
127 // all the parts were sorted together and the configs match.
128 const Configuration *config = nullptr;
Austin Schuh297d2352021-01-21 19:02:17 -0800129 for (const LogFile &log_file : log_files_) {
130 if (log_file.config.get() == nullptr) {
131 LOG(FATAL) << "Couldn't find a config in " << log_file;
132 }
Austin Schuh0ca51f32020-12-25 21:51:45 -0800133 if (config == nullptr) {
134 config = log_file.config.get();
135 } else {
136 CHECK_EQ(config, log_file.config.get());
137 }
138 }
139 }
Austin Schuhdda74ec2021-01-03 19:30:37 -0800140
Austin Schuh6331ef92020-01-07 18:28:09 -0800141 MakeRemappedConfig();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800142
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700143 // Remap all existing remote timestamp channels. They will be recreated, and
144 // the data logged isn't relevant anymore.
Austin Schuh3c5dae52020-10-06 18:55:18 -0700145 for (const Node *node : configuration::GetNodes(logged_configuration())) {
Austin Schuh61e973f2021-02-21 21:43:56 -0800146 message_bridge::ChannelTimestampFinder finder(logged_configuration(),
147 "log_reader", node);
148
149 absl::btree_set<std::string_view> remote_nodes;
150
151 for (const Channel *channel : *logged_configuration()->channels()) {
152 if (!configuration::ChannelIsSendableOnNode(channel, node)) {
153 continue;
154 }
155 if (!channel->has_destination_nodes()) {
156 continue;
157 }
158 for (const Connection *connection : *channel->destination_nodes()) {
159 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
160 node)) {
161 // Start by seeing if the split timestamp channels are being used for
162 // this message. If so, remap them.
163 const Channel *timestamp_channel = configuration::GetChannel(
164 logged_configuration(),
165 finder.SplitChannelName(channel, connection),
166 RemoteMessage::GetFullyQualifiedName(), "", node, true);
167
168 if (timestamp_channel != nullptr) {
169 if (timestamp_channel->logger() != LoggerConfig::NOT_LOGGED) {
170 RemapLoggedChannel<RemoteMessage>(
171 timestamp_channel->name()->string_view(), node);
172 }
173 continue;
174 }
175
176 // Otherwise collect this one up as a node to look for a combined
177 // channel from. It is more efficient to compare nodes than channels.
178 remote_nodes.insert(connection->name()->string_view());
179 }
180 }
181 }
182
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700183 std::vector<const Node *> timestamp_logger_nodes =
184 configuration::TimestampNodes(logged_configuration(), node);
Austin Schuh61e973f2021-02-21 21:43:56 -0800185 for (const std::string_view remote_node : remote_nodes) {
186 const std::string channel = finder.CombinedChannelName(remote_node);
187
Austin Schuh0de30f32020-12-06 12:44:28 -0800188 // See if the log file is an old log with MessageHeader channels in it, or
189 // a newer log with RemoteMessage. If we find an older log, rename the
190 // type too along with the name.
191 if (HasChannel<MessageHeader>(channel, node)) {
192 CHECK(!HasChannel<RemoteMessage>(channel, node))
193 << ": Can't have both a MessageHeader and RemoteMessage remote "
194 "timestamp channel.";
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800195 // In theory, we should check NOT_LOGGED like RemoteMessage and be more
196 // careful about updating the config, but there are fewer and fewer logs
197 // with MessageHeader remote messages, so it isn't worth the effort.
Austin Schuh0de30f32020-12-06 12:44:28 -0800198 RemapLoggedChannel<MessageHeader>(channel, node, "/original",
199 "aos.message_bridge.RemoteMessage");
200 } else {
201 CHECK(HasChannel<RemoteMessage>(channel, node))
202 << ": Failed to find {\"name\": \"" << channel << "\", \"type\": \""
203 << RemoteMessage::GetFullyQualifiedName() << "\"} for node "
204 << node->name()->string_view();
James Kuszmaul4f106fb2021-01-05 20:53:02 -0800205 // Only bother to remap if there's something on the channel. We can
206 // tell if the channel was marked NOT_LOGGED or not. This makes the
207 // config not change un-necesarily when we replay a log with NOT_LOGGED
208 // messages.
209 if (HasLoggedChannel<RemoteMessage>(channel, node)) {
210 RemapLoggedChannel<RemoteMessage>(channel, node);
211 }
Austin Schuh0de30f32020-12-06 12:44:28 -0800212 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700213 }
214 }
215
Austin Schuh6aa77be2020-02-22 21:06:40 -0800216 if (replay_configuration) {
217 CHECK_EQ(configuration::MultiNode(configuration()),
218 configuration::MultiNode(replay_configuration))
Austin Schuh2f8fd752020-09-01 22:38:28 -0700219 << ": Log file and replay config need to both be multi or single "
220 "node.";
Austin Schuh6aa77be2020-02-22 21:06:40 -0800221 }
222
Austin Schuh6f3babe2020-01-26 20:34:50 -0800223 if (!configuration::MultiNode(configuration())) {
Austin Schuh287d43d2020-12-04 20:19:33 -0800224 states_.emplace_back(std::make_unique<State>(
Austin Schuh58646e22021-08-23 23:51:46 -0700225 std::make_unique<TimestampMapper>(FilterPartsForNode(log_files_, "")),
226 nullptr));
Austin Schuh8bd96322020-02-13 21:18:22 -0800227 } else {
Austin Schuh6aa77be2020-02-22 21:06:40 -0800228 if (replay_configuration) {
James Kuszmaul46d82582020-05-09 19:50:09 -0700229 CHECK_EQ(logged_configuration()->nodes()->size(),
Austin Schuh6aa77be2020-02-22 21:06:40 -0800230 replay_configuration->nodes()->size())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700231 << ": Log file and replay config need to have matching nodes "
232 "lists.";
James Kuszmaul46d82582020-05-09 19:50:09 -0700233 for (const Node *node : *logged_configuration()->nodes()) {
234 if (configuration::GetNode(replay_configuration, node) == nullptr) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700235 LOG(FATAL) << "Found node " << FlatbufferToJson(node)
236 << " in logged config that is not present in the replay "
237 "config.";
James Kuszmaul46d82582020-05-09 19:50:09 -0700238 }
239 }
Austin Schuh6aa77be2020-02-22 21:06:40 -0800240 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800241 states_.resize(configuration()->nodes()->size());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800242 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800243}
244
Austin Schuh6aa77be2020-02-22 21:06:40 -0800245LogReader::~LogReader() {
Austin Schuh39580f12020-08-01 14:44:08 -0700246 if (event_loop_factory_unique_ptr_) {
247 Deregister();
248 } else if (event_loop_factory_ != nullptr) {
249 LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory "
250 "is destroyed";
251 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700252 // Zero out some buffers. It's easy to do use-after-frees on these, so make
253 // it more obvious.
Austin Schuh39580f12020-08-01 14:44:08 -0700254 if (remapped_configuration_buffer_) {
255 remapped_configuration_buffer_->Wipe();
256 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800257}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800258
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800259const Configuration *LogReader::logged_configuration() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800260 return log_files_[0].config.get();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800261}
262
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800263const Configuration *LogReader::configuration() const {
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800264 return remapped_configuration_;
265}
266
Austin Schuh07676622021-01-21 18:59:17 -0800267std::vector<const Node *> LogReader::LoggedNodes() const {
268 return configuration::GetNodes(logged_configuration());
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800269}
Austin Schuh15649d62019-12-28 16:36:38 -0800270
Austin Schuh11d43732020-09-21 17:28:30 -0700271monotonic_clock::time_point LogReader::monotonic_start_time(
272 const Node *node) const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800273 State *state =
274 states_[configuration::GetNodeIndex(configuration(), node)].get();
275 CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node);
276
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700277 // TODO(austin): Un-hard-code the 0 boot count.
278 return state->monotonic_start_time(0);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800279}
280
Austin Schuh11d43732020-09-21 17:28:30 -0700281realtime_clock::time_point LogReader::realtime_start_time(
282 const Node *node) const {
Austin Schuh8bd96322020-02-13 21:18:22 -0800283 State *state =
284 states_[configuration::GetNodeIndex(configuration(), node)].get();
285 CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node);
286
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700287 // TODO(austin): Un-hard-code the 0 boot count.
288 return state->realtime_start_time(0);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800289}
290
Austin Schuh58646e22021-08-23 23:51:46 -0700291void LogReader::OnStart(std::function<void()> fn) {
292 CHECK(!configuration::MultiNode(configuration()));
293 OnStart(nullptr, std::move(fn));
294}
295
296void LogReader::OnStart(const Node *node, std::function<void()> fn) {
297 const int node_index = configuration::GetNodeIndex(configuration(), node);
298 CHECK_GE(node_index, 0);
299 CHECK_LT(node_index, static_cast<int>(states_.size()));
300 State *state = states_[node_index].get();
301 CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node);
302
303 state->OnStart(std::move(fn));
304}
305
306void LogReader::State::OnStart(std::function<void()> fn) {
307 on_starts_.emplace_back(std::move(fn));
308}
309
310void LogReader::State::RunOnStart() {
311 SetRealtimeOffset(monotonic_start_time(boot_count()),
312 realtime_start_time(boot_count()));
313
314 VLOG(1) << "Starting " << MaybeNodeName(node()) << "at time "
315 << monotonic_start_time(boot_count());
316 for (size_t i = 0; i < on_starts_.size(); ++i) {
317 on_starts_[i]();
318 }
319 stopped_ = false;
320 started_ = true;
321}
322
323void LogReader::OnEnd(std::function<void()> fn) {
324 CHECK(!configuration::MultiNode(configuration()));
325 OnEnd(nullptr, std::move(fn));
326}
327
328void LogReader::OnEnd(const Node *node, std::function<void()> fn) {
329 const int node_index = configuration::GetNodeIndex(configuration(), node);
330 CHECK_GE(node_index, 0);
331 CHECK_LT(node_index, static_cast<int>(states_.size()));
332 State *state = states_[node_index].get();
333 CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node);
334
335 state->OnEnd(std::move(fn));
336}
337
338void LogReader::State::OnEnd(std::function<void()> fn) {
339 on_ends_.emplace_back(std::move(fn));
340}
341
342void LogReader::State::RunOnEnd() {
343 VLOG(1) << "Ending " << MaybeNodeName(node()) << "at time "
344 << monotonic_start_time(boot_count());
345 for (size_t i = 0; i < on_ends_.size(); ++i) {
346 on_ends_[i]();
347 }
348
349 stopped_ = true;
350 started_ = false;
351}
352
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800353void LogReader::Register() {
354 event_loop_factory_unique_ptr_ =
Austin Schuhac0771c2020-01-07 18:36:30 -0800355 std::make_unique<SimulatedEventLoopFactory>(configuration());
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800356 Register(event_loop_factory_unique_ptr_.get());
357}
358
Austin Schuh58646e22021-08-23 23:51:46 -0700359void LogReader::RegisterWithoutStarting(
360 SimulatedEventLoopFactory *event_loop_factory) {
Austin Schuh92547522019-12-28 14:33:43 -0800361 event_loop_factory_ = event_loop_factory;
Austin Schuhe5bbd9e2020-09-21 17:29:20 -0700362 remapped_configuration_ = event_loop_factory_->configuration();
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800363 filters_ =
364 std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>(
Austin Schuhba20ea72021-01-21 16:47:01 -0800365 event_loop_factory_->configuration(), logged_configuration(),
Austin Schuh58646e22021-08-23 23:51:46 -0700366 log_files_[0].boots, FLAGS_skip_order_validation,
Austin Schuhfe3fb342021-01-16 18:50:37 -0800367 chrono::duration_cast<chrono::nanoseconds>(
368 chrono::duration<double>(FLAGS_time_estimation_buffer_seconds)));
Austin Schuh92547522019-12-28 14:33:43 -0800369
Austin Schuhe639ea12021-01-25 13:00:22 -0800370 std::vector<TimestampMapper *> timestamp_mappers;
Brian Silvermand90905f2020-09-23 14:42:56 -0700371 for (const Node *node : configuration::GetNodes(configuration())) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800372 const size_t node_index =
373 configuration::GetNodeIndex(configuration(), node);
Austin Schuh287d43d2020-12-04 20:19:33 -0800374 std::vector<LogParts> filtered_parts = FilterPartsForNode(
375 log_files_, node != nullptr ? node->name()->string_view() : "");
Austin Schuh315b96b2020-12-11 21:21:12 -0800376
Austin Schuh287d43d2020-12-04 20:19:33 -0800377 states_[node_index] = std::make_unique<State>(
378 filtered_parts.size() == 0u
379 ? nullptr
Austin Schuh58646e22021-08-23 23:51:46 -0700380 : std::make_unique<TimestampMapper>(std::move(filtered_parts)),
381 node);
Austin Schuh8bd96322020-02-13 21:18:22 -0800382 State *state = states_[node_index].get();
Austin Schuh58646e22021-08-23 23:51:46 -0700383 state->SetNodeEventLoopFactory(
384 event_loop_factory_->GetNodeEventLoopFactory(node));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700385
386 state->SetChannelCount(logged_configuration()->channels()->size());
Austin Schuhe639ea12021-01-25 13:00:22 -0800387 timestamp_mappers.emplace_back(state->timestamp_mapper());
Austin Schuhcde938c2020-02-02 17:30:07 -0800388 }
Austin Schuhe639ea12021-01-25 13:00:22 -0800389 filters_->SetTimestampMappers(std::move(timestamp_mappers));
390
391 // Note: this needs to be set before any times are pulled, or we won't observe
392 // the timestamps.
Austin Schuh87dd3832021-01-01 23:07:31 -0800393 event_loop_factory_->SetTimeConverter(filters_.get());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700394
Austin Schuh287d43d2020-12-04 20:19:33 -0800395 for (const Node *node : configuration::GetNodes(configuration())) {
396 const size_t node_index =
397 configuration::GetNodeIndex(configuration(), node);
398 State *state = states_[node_index].get();
399 for (const Node *other_node : configuration::GetNodes(configuration())) {
400 const size_t other_node_index =
401 configuration::GetNodeIndex(configuration(), other_node);
402 State *other_state = states_[other_node_index].get();
403 if (other_state != state) {
404 state->AddPeer(other_state);
405 }
406 }
407 }
408
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700409 // Register after making all the State objects so we can build references
410 // between them.
411 for (const Node *node : configuration::GetNodes(configuration())) {
412 const size_t node_index =
413 configuration::GetNodeIndex(configuration(), node);
414 State *state = states_[node_index].get();
415
Austin Schuh58646e22021-08-23 23:51:46 -0700416 // If we didn't find any log files with data in them, we won't ever get a
417 // callback or be live. So skip the rest of the setup.
418 if (state->OldestMessageTime() == BootTimestamp::max_time()) {
419 continue;
420 }
421 ++live_nodes_;
422
423 NodeEventLoopFactory *node_factory =
424 event_loop_factory_->GetNodeEventLoopFactory(node);
425 node_factory->OnStartup([this, state, node]() {
426 RegisterDuringStartup(state->MakeEventLoop(), node);
427 });
428 node_factory->OnShutdown([this, state, node]() {
429 RegisterDuringStartup(nullptr, node);
430 state->DestroyEventLoop();
431 });
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700432 }
433
James Kuszmaul46d82582020-05-09 19:50:09 -0700434 if (live_nodes_ == 0) {
435 LOG(FATAL)
436 << "Don't have logs from any of the nodes in the replay config--are "
437 "you sure that the replay config matches the original config?";
438 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800439
Austin Schuh87dd3832021-01-01 23:07:31 -0800440 filters_->CheckGraph();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800441
Austin Schuh858c9f32020-08-31 16:56:12 -0700442 for (std::unique_ptr<State> &state : states_) {
443 state->SeedSortedMessages();
444 }
445
Austin Schuh6f3babe2020-01-26 20:34:50 -0800446 // Forwarding is tracked per channel. If it is enabled, we want to turn it
447 // off. Otherwise messages replayed will get forwarded across to the other
Austin Schuh2f8fd752020-09-01 22:38:28 -0700448 // nodes, and also replayed on the other nodes. This may not satisfy all
449 // our users, but it'll start the discussion.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800450 if (configuration::MultiNode(event_loop_factory_->configuration())) {
451 for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) {
452 const Channel *channel = logged_configuration()->channels()->Get(i);
453 const Node *node = configuration::GetNode(
454 configuration(), channel->source_node()->string_view());
455
Austin Schuh8bd96322020-02-13 21:18:22 -0800456 State *state =
457 states_[configuration::GetNodeIndex(configuration(), node)].get();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800458
459 const Channel *remapped_channel =
Austin Schuh58646e22021-08-23 23:51:46 -0700460 RemapChannel(state->event_loop(), node, channel);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800461
462 event_loop_factory_->DisableForwarding(remapped_channel);
463 }
Austin Schuh4c3b9702020-08-30 11:34:55 -0700464
465 // If we are replaying a log, we don't want a bunch of redundant messages
466 // from both the real message bridge and simulated message bridge.
467 event_loop_factory_->DisableStatistics();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800468 }
Austin Schuh58646e22021-08-23 23:51:46 -0700469}
470
471void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) {
472 RegisterWithoutStarting(event_loop_factory);
473 // We want to start the log file at the last start time of the log files
474 // from all the nodes. Compute how long each node's simulation needs to run
475 // to move time to this point.
476 distributed_clock::time_point start_time = distributed_clock::min_time;
477
478 // TODO(austin): We want an "OnStart" callback for each node rather than
479 // running until the last node.
480
481 for (std::unique_ptr<State> &state : states_) {
482 VLOG(1) << "Start time is " << state->monotonic_start_time(0)
483 << " for node " << MaybeNodeName(state->node()) << "now "
484 << state->monotonic_now();
485 if (state->monotonic_start_time(0) == monotonic_clock::min_time) {
486 continue;
487 }
488 // And start computing the start time on the distributed clock now that
489 // that works.
490 start_time = std::max(
491 start_time, state->ToDistributedClock(state->monotonic_start_time(0)));
492 }
493
494 // TODO(austin): If a node doesn't have a start time, we might not queue
495 // enough. If this happens, we'll explode with a frozen error eventually.
496
497 CHECK_GE(start_time, distributed_clock::epoch())
498 << ": Hmm, we have a node starting before the start of time. Offset "
499 "everything.";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800500
Austin Schuhcde938c2020-02-02 17:30:07 -0800501 // While we are starting the system up, we might be relying on matching data
502 // to timestamps on log files where the timestamp log file starts before the
503 // data. In this case, it is reasonable to expect missing data.
Austin Schuhdda74ec2021-01-03 19:30:37 -0800504 {
505 const bool prior_ignore_missing_data = ignore_missing_data_;
506 ignore_missing_data_ = true;
507 VLOG(1) << "Running until " << start_time << " in Register";
508 event_loop_factory_->RunFor(start_time.time_since_epoch());
509 VLOG(1) << "At start time";
510 // Now that we are running for real, missing data means that the log file is
511 // corrupted or went wrong.
512 ignore_missing_data_ = prior_ignore_missing_data;
513 }
Austin Schuh92547522019-12-28 14:33:43 -0800514
Austin Schuh8bd96322020-02-13 21:18:22 -0800515 for (std::unique_ptr<State> &state : states_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700516 // Make the RT clock be correct before handing it to the user.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700517 if (state->realtime_start_time(0) != realtime_clock::min_time) {
518 state->SetRealtimeOffset(state->monotonic_start_time(0),
519 state->realtime_start_time(0));
Austin Schuh2f8fd752020-09-01 22:38:28 -0700520 }
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700521 VLOG(1) << "Start time is " << state->monotonic_start_time(0)
522 << " for node " << MaybeNodeName(state->event_loop()->node())
523 << "now " << state->monotonic_now();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700524 }
525
526 if (FLAGS_timestamps_to_csv) {
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800527 filters_->Start(event_loop_factory);
Austin Schuh8bd96322020-02-13 21:18:22 -0800528 }
529}
530
Austin Schuh2f8fd752020-09-01 22:38:28 -0700531message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter(
Austin Schuh8bd96322020-02-13 21:18:22 -0800532 const Node *node_a, const Node *node_b) {
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800533 if (filters_) {
534 return filters_->GetFilter(node_a, node_b);
Austin Schuh8bd96322020-02-13 21:18:22 -0800535 }
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800536 return nullptr;
Austin Schuh8bd96322020-02-13 21:18:22 -0800537}
538
Austin Schuhe309d2a2019-11-29 13:25:21 -0800539void LogReader::Register(EventLoop *event_loop) {
Austin Schuh58646e22021-08-23 23:51:46 -0700540 Register(event_loop, event_loop->node());
541}
542
543void LogReader::Register(EventLoop *event_loop, const Node *node) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800544 State *state =
Austin Schuh58646e22021-08-23 23:51:46 -0700545 states_[configuration::GetNodeIndex(configuration(), node)].get();
546
547 // If we didn't find any log files with data in them, we won't ever get a
548 // callback or be live. So skip the rest of the setup.
549 if (state->OldestMessageTime() == BootTimestamp::max_time()) {
550 return;
551 }
552 ++live_nodes_;
553
554 if (event_loop_factory_ != nullptr) {
555 event_loop_factory_->GetNodeEventLoopFactory(node)->OnStartup(
556 [this, event_loop, node]() {
557 RegisterDuringStartup(event_loop, node);
558 });
559 } else {
560 RegisterDuringStartup(event_loop, node);
561 }
562}
563
564void LogReader::RegisterDuringStartup(EventLoop *event_loop, const Node *node) {
565 if (event_loop) {
566 CHECK(event_loop->configuration() == configuration());
567 }
568
569 State *state =
570 states_[configuration::GetNodeIndex(configuration(), node)].get();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800571
Austin Schuh858c9f32020-08-31 16:56:12 -0700572 state->set_event_loop(event_loop);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800573
Tyler Chatow67ddb032020-01-12 14:30:04 -0800574 // We don't run timing reports when trying to print out logged data, because
575 // otherwise we would end up printing out the timing reports themselves...
576 // This is only really relevant when we are replaying into a simulation.
Austin Schuh58646e22021-08-23 23:51:46 -0700577 if (event_loop) {
578 event_loop->SkipTimingReport();
579 event_loop->SkipAosLog();
580 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800581
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700582 for (size_t logged_channel_index = 0;
583 logged_channel_index < logged_configuration()->channels()->size();
584 ++logged_channel_index) {
585 const Channel *channel = RemapChannel(
Austin Schuh58646e22021-08-23 23:51:46 -0700586 event_loop, node,
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700587 logged_configuration()->channels()->Get(logged_channel_index));
Austin Schuh8bd96322020-02-13 21:18:22 -0800588
Austin Schuh532656d2021-01-11 10:17:18 -0800589 if (channel->logger() == LoggerConfig::NOT_LOGGED) {
590 continue;
591 }
592
Austin Schuh2f8fd752020-09-01 22:38:28 -0700593 message_bridge::NoncausalOffsetEstimator *filter = nullptr;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700594
595 State *source_state = nullptr;
Austin Schuh58646e22021-08-23 23:51:46 -0700596 if (!configuration::ChannelIsSendableOnNode(channel, node) &&
597 configuration::ChannelIsReadableOnNode(channel, node)) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700598 const Node *source_node = configuration::GetNode(
Austin Schuh58646e22021-08-23 23:51:46 -0700599 configuration(), channel->source_node()->string_view());
Austin Schuh8bd96322020-02-13 21:18:22 -0800600
Austin Schuh58646e22021-08-23 23:51:46 -0700601 // We've got a message which is being forwarded to this node.
602 filter = GetFilter(node, source_node);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700603
604 source_state =
605 states_[configuration::GetNodeIndex(configuration(), source_node)]
606 .get();
Austin Schuh8bd96322020-02-13 21:18:22 -0800607 }
Austin Schuh858c9f32020-08-31 16:56:12 -0700608
Austin Schuh58646e22021-08-23 23:51:46 -0700609 // We are the source, and it is forwarded.
610 const bool is_forwarded =
611 configuration::ChannelIsSendableOnNode(channel, node) &&
612 configuration::ConnectionCount(channel);
613
614 state->SetChannel(logged_channel_index,
615 configuration::ChannelIndex(configuration(), channel),
616 event_loop ? event_loop->MakeRawSender(channel) : nullptr,
617 filter, is_forwarded, source_state);
618
619 if (is_forwarded) {
620 const Node *source_node = configuration::GetNode(
621 configuration(), channel->source_node()->string_view());
622
623 for (const Connection *connection : *channel->destination_nodes()) {
624 const bool delivery_time_is_logged =
625 configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
626 source_node);
627
628 if (delivery_time_is_logged) {
629 State *destination_state =
630 states_[configuration::GetNodeIndex(
631 configuration(), connection->name()->string_view())]
632 .get();
633 destination_state->SetRemoteTimestampSender(
634 logged_channel_index,
635 event_loop ? state->RemoteTimestampSender(channel, connection)
636 : nullptr);
637 }
638 }
639 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800640 }
641
Austin Schuh58646e22021-08-23 23:51:46 -0700642 if (!event_loop) {
643 state->ClearRemoteTimestampSenders();
644 state->set_timer_handler(nullptr);
645 state->set_startup_timer(nullptr);
Austin Schuh6aa77be2020-02-22 21:06:40 -0800646 return;
647 }
648
Austin Schuh858c9f32020-08-31 16:56:12 -0700649 state->set_timer_handler(event_loop->AddTimer([this, state]() {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700650 VLOG(1) << "Starting sending " << MaybeNodeName(state->event_loop()->node())
651 << "at " << state->event_loop()->context().monotonic_event_time
652 << " now " << state->monotonic_now();
Austin Schuh58646e22021-08-23 23:51:46 -0700653 if (state->OldestMessageTime() == BootTimestamp::max_time()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800654 --live_nodes_;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700655 VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!";
James Kuszmaul71a81932020-12-15 21:08:01 -0800656 if (exit_on_finish_ && live_nodes_ == 0) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800657 event_loop_factory_->Exit();
658 }
James Kuszmaul314f1672020-01-03 20:02:08 -0800659 return;
660 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700661
Austin Schuhdda74ec2021-01-03 19:30:37 -0800662 TimestampedMessage timestamped_message = state->PopOldest();
Austin Schuh58646e22021-08-23 23:51:46 -0700663
664 CHECK_EQ(timestamped_message.monotonic_event_time.boot,
665 state->boot_count());
Austin Schuh05b70472020-01-01 17:11:17 -0800666
Austin Schuhe309d2a2019-11-29 13:25:21 -0800667 const monotonic_clock::time_point monotonic_now =
Austin Schuh858c9f32020-08-31 16:56:12 -0700668 state->event_loop()->context().monotonic_event_time;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700669 if (!FLAGS_skip_order_validation) {
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700670 CHECK(monotonic_now == timestamped_message.monotonic_event_time.time)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700671 << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now "
672 << monotonic_now << " trying to send "
Austin Schuh287d43d2020-12-04 20:19:33 -0800673 << timestamped_message.monotonic_event_time << " failure "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700674 << state->DebugString();
Austin Schuh58646e22021-08-23 23:51:46 -0700675 } else if (BootTimestamp{.boot = state->boot_count(),
676 .time = monotonic_now} !=
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700677 timestamped_message.monotonic_event_time) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700678 LOG(WARNING) << "Check failed: monotonic_now == "
Austin Schuh287d43d2020-12-04 20:19:33 -0800679 "timestamped_message.monotonic_event_time) ("
Austin Schuh2f8fd752020-09-01 22:38:28 -0700680 << monotonic_now << " vs. "
Austin Schuh287d43d2020-12-04 20:19:33 -0800681 << timestamped_message.monotonic_event_time
Austin Schuh2f8fd752020-09-01 22:38:28 -0700682 << "): " << FlatbufferToJson(state->event_loop()->node())
683 << " Now " << monotonic_now << " trying to send "
Austin Schuh287d43d2020-12-04 20:19:33 -0800684 << timestamped_message.monotonic_event_time << " failure "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700685 << state->DebugString();
686 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800687
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700688 if (timestamped_message.monotonic_event_time.time >
689 state->monotonic_start_time(
690 timestamped_message.monotonic_event_time.boot) ||
Austin Schuh15649d62019-12-28 16:36:38 -0800691 event_loop_factory_ != nullptr) {
Austin Schuhdda74ec2021-01-03 19:30:37 -0800692 if (timestamped_message.data.span().size() != 0u) {
693 if (timestamped_message.monotonic_remote_time !=
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700694 BootTimestamp::min_time()) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800695 // Confirm that the message was sent on the sending node before the
696 // destination node (this node). As a proxy, do this by making sure
697 // that time on the source node is past when the message was sent.
Austin Schuh87dd3832021-01-01 23:07:31 -0800698 //
699 // TODO(austin): <= means that the cause message (which we know) could
700 // happen after the effect even though we know they are at the same
701 // time. I doubt anyone will notice for a bit, but we should really
702 // fix that.
Austin Schuh58646e22021-08-23 23:51:46 -0700703 BootTimestamp monotonic_remote_now =
704 state->monotonic_remote_now(timestamped_message.channel_index);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700705 if (!FLAGS_skip_order_validation) {
Austin Schuh58646e22021-08-23 23:51:46 -0700706 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
707 monotonic_remote_now.boot);
708 CHECK_LE(timestamped_message.monotonic_remote_time,
709 monotonic_remote_now)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700710 << state->event_loop()->node()->name()->string_view() << " to "
Austin Schuh287d43d2020-12-04 20:19:33 -0800711 << state->remote_node(timestamped_message.channel_index)
712 ->name()
713 ->string_view()
Austin Schuh315b96b2020-12-11 21:21:12 -0800714 << " while trying to send a message on "
715 << configuration::CleanedChannelToString(
716 logged_configuration()->channels()->Get(
717 timestamped_message.channel_index))
Austin Schuh2f8fd752020-09-01 22:38:28 -0700718 << " " << state->DebugString();
Austin Schuh58646e22021-08-23 23:51:46 -0700719 } else if (monotonic_remote_now.boot !=
720 timestamped_message.monotonic_remote_time.boot) {
721 LOG(WARNING) << "Missmatched boots, " << monotonic_remote_now.boot
722 << " vs "
723 << timestamped_message.monotonic_remote_time.boot;
724 } else if (timestamped_message.monotonic_remote_time >
725 monotonic_remote_now) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700726 LOG(WARNING)
Austin Schuh287d43d2020-12-04 20:19:33 -0800727 << "Check failed: timestamped_message.monotonic_remote_time < "
728 "state->monotonic_remote_now(timestamped_message.channel_"
729 "index) ("
730 << timestamped_message.monotonic_remote_time << " vs. "
731 << state->monotonic_remote_now(
732 timestamped_message.channel_index)
733 << ") " << state->event_loop()->node()->name()->string_view()
734 << " to "
735 << state->remote_node(timestamped_message.channel_index)
736 ->name()
737 ->string_view()
738 << " currently " << timestamped_message.monotonic_event_time
Austin Schuh2f8fd752020-09-01 22:38:28 -0700739 << " ("
740 << state->ToDistributedClock(
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700741 timestamped_message.monotonic_event_time.time)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700742 << ") remote event time "
Austin Schuh287d43d2020-12-04 20:19:33 -0800743 << timestamped_message.monotonic_remote_time << " ("
Austin Schuh2f8fd752020-09-01 22:38:28 -0700744 << state->RemoteToDistributedClock(
Austin Schuh287d43d2020-12-04 20:19:33 -0800745 timestamped_message.channel_index,
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700746 timestamped_message.monotonic_remote_time.time)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700747 << ") " << state->DebugString();
748 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800749 }
750
Austin Schuh15649d62019-12-28 16:36:38 -0800751 // If we have access to the factory, use it to fix the realtime time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700752 state->SetRealtimeOffset(timestamped_message.monotonic_event_time.time,
Austin Schuh287d43d2020-12-04 20:19:33 -0800753 timestamped_message.realtime_event_time);
Austin Schuh15649d62019-12-28 16:36:38 -0800754
Austin Schuh2f8fd752020-09-01 22:38:28 -0700755 VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending "
Austin Schuh287d43d2020-12-04 20:19:33 -0800756 << timestamped_message.monotonic_event_time;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700757 // TODO(austin): std::move channel_data in and make that efficient in
758 // simulation.
Austin Schuh287d43d2020-12-04 20:19:33 -0800759 state->Send(std::move(timestamped_message));
Austin Schuhdda74ec2021-01-03 19:30:37 -0800760 } else if (!ignore_missing_data_ &&
Austin Schuh5ee56872021-01-30 16:53:34 -0800761 // When starting up, we can have data which was sent before the
762 // log starts, but the timestamp was after the log starts. This
763 // is unreasonable to avoid, so ignore the missing data.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700764 timestamped_message.monotonic_remote_time.time >=
Austin Schuh5ee56872021-01-30 16:53:34 -0800765 state->monotonic_remote_start_time(
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700766 timestamped_message.monotonic_remote_time.boot,
Austin Schuh5ee56872021-01-30 16:53:34 -0800767 timestamped_message.channel_index) &&
Austin Schuhdda74ec2021-01-03 19:30:37 -0800768 !FLAGS_skip_missing_forwarding_entries) {
Austin Schuh5ee56872021-01-30 16:53:34 -0800769 // We've found a timestamp without data that we expect to have data for.
770 // This likely means that we are at the end of the log file. Record it
771 // and CHECK that in the rest of the log file, we don't find any more
772 // data on that channel. Not all channels will end at the same point in
773 // time since they can be in different files.
Austin Schuhdda74ec2021-01-03 19:30:37 -0800774 VLOG(1) << "Found the last message on channel "
Austin Schuh94526302021-04-28 22:46:34 -0700775 << timestamped_message.channel_index << ", "
776 << configuration::CleanedChannelToString(
777 logged_configuration()->channels()->Get(
Austin Schuh58646e22021-08-23 23:51:46 -0700778 timestamped_message.channel_index))
779 << " " << timestamped_message;
Austin Schuhdda74ec2021-01-03 19:30:37 -0800780
Austin Schuh2bb80e02021-03-20 21:46:17 -0700781 // The user might be working with log files from 1 node but forgot to
782 // configure the infrastructure to log data for a remote channel on that
783 // node. That can be very hard to debug, even though the log reader is
784 // doing the right thing. At least log a warning in that case and tell
785 // the user what is happening so they can either update their config to
786 // log the channel or can find a log with the data.
787 {
788 const std::vector<std::string> logger_nodes =
789 FindLoggerNodes(log_files_);
790 if (logger_nodes.size()) {
791 // We have old logs which don't have the logger nodes logged. In
792 // that case, we can't be helpful :(
793 bool data_logged = false;
794 const Channel *channel = logged_configuration()->channels()->Get(
795 timestamped_message.channel_index);
796 for (const std::string &node : logger_nodes) {
797 data_logged |=
798 configuration::ChannelMessageIsLoggedOnNode(channel, node);
799 }
800 if (!data_logged) {
801 LOG(WARNING) << "Got a timestamp without any logfiles which "
802 "could contain data for channel "
803 << configuration::CleanedChannelToString(channel);
804 LOG(WARNING) << "Only have logs logged on ["
805 << absl::StrJoin(logger_nodes, ", ") << "]";
806 LOG(WARNING)
807 << "Dropping the rest of the data on "
808 << state->event_loop()->node()->name()->string_view();
809 LOG(WARNING)
810 << "Consider using --skip_missing_forwarding_entries to "
811 "bypass this, update your config to log it, or add data "
812 "from one of the nodes it is logged on.";
813 }
814 }
815 }
816
Austin Schuhdda74ec2021-01-03 19:30:37 -0800817 // Vector storing if we've seen a nullptr message or not per channel.
818 std::vector<bool> last_message;
819 last_message.resize(logged_configuration()->channels()->size(), false);
820
821 last_message[timestamped_message.channel_index] = true;
822
823 // Now that we found the end of one channel, artificially stop the
824 // rest. It is confusing when part of your data gets replayed but not
Austin Schuh5ee56872021-01-30 16:53:34 -0800825 // all. Read the rest of the messages and drop them on the floor while
826 // doing some basic validation.
Austin Schuh58646e22021-08-23 23:51:46 -0700827 while (state->OldestMessageTime() != BootTimestamp::max_time()) {
Austin Schuhb08891b2021-09-02 18:52:31 -0700828 // TODO(austin): This force queues up the rest of the log file for all
829 // the other nodes. We should do this through the timer instead to
830 // keep memory usage down.
Austin Schuhdda74ec2021-01-03 19:30:37 -0800831 TimestampedMessage next = state->PopOldest();
832 // Make sure that once we have seen the last message on a channel,
833 // data doesn't start back up again. If the user wants to play
834 // through events like this, they can set
835 // --skip_missing_forwarding_entries or ignore_missing_data_.
836 CHECK_LT(next.channel_index, last_message.size());
837 if (next.data.span().size() == 0u) {
838 last_message[next.channel_index] = true;
839 } else {
840 if (last_message[next.channel_index]) {
841 LOG(FATAL)
842 << "Found missing data in the middle of the log file on "
843 "channel "
Austin Schuh6e014b82021-09-14 17:46:33 -0700844 << next.channel_index << " "
845 << configuration::StrippedChannelToString(
846 logged_configuration()->channels()->Get(
847 next.channel_index))
848 << " " << next << " " << state->DebugString();
Austin Schuhdda74ec2021-01-03 19:30:37 -0800849 }
850 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800851 }
Austin Schuh92547522019-12-28 14:33:43 -0800852 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800853 } else {
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700854 LOG(WARNING) << "Not sending data from before the start of the log file. "
855 << timestamped_message.monotonic_event_time.time
856 .time_since_epoch()
857 .count()
858 << " start "
859 << monotonic_start_time().time_since_epoch().count() << " "
860 << FlatbufferToJson(
861 timestamped_message.data,
862 {.multi_line = false, .max_vector_size = 100});
Austin Schuhe309d2a2019-11-29 13:25:21 -0800863 }
864
Austin Schuh58646e22021-08-23 23:51:46 -0700865 const BootTimestamp next_time = state->OldestMessageTime();
866 if (next_time != BootTimestamp::max_time()) {
867 if (next_time.boot != state->boot_count()) {
868 VLOG(1) << "Next message for "
869 << MaybeNodeName(state->event_loop()->node())
870 << "is on the next boot, " << next_time << " now is "
871 << state->monotonic_now();
872 CHECK(event_loop_factory_);
873 state->RunOnEnd();
874 return;
875 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700876 VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node())
Austin Schuh58646e22021-08-23 23:51:46 -0700877 << "wakeup for " << next_time.time << "("
878 << state->ToDistributedClock(next_time.time)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700879 << " distributed), now is " << state->monotonic_now();
Austin Schuh58646e22021-08-23 23:51:46 -0700880 state->Setup(next_time.time);
James Kuszmaul314f1672020-01-03 20:02:08 -0800881 } else {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700882 VLOG(1) << MaybeNodeName(state->event_loop()->node())
883 << "No next message, scheduling shutdown";
Austin Schuh58646e22021-08-23 23:51:46 -0700884 state->RunOnEnd();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700885 // Set a timer up immediately after now to die. If we don't do this,
886 // then the senders waiting on the message we just read will never get
887 // called.
Austin Schuheecb9282020-01-08 17:43:30 -0800888 if (event_loop_factory_ != nullptr) {
Austin Schuh858c9f32020-08-31 16:56:12 -0700889 state->Setup(monotonic_now + event_loop_factory_->send_delay() +
890 std::chrono::nanoseconds(1));
Austin Schuheecb9282020-01-08 17:43:30 -0800891 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800892 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800893
Austin Schuh2f8fd752020-09-01 22:38:28 -0700894 VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at "
895 << state->event_loop()->context().monotonic_event_time << " now "
896 << state->monotonic_now();
Austin Schuh858c9f32020-08-31 16:56:12 -0700897 }));
Austin Schuhe309d2a2019-11-29 13:25:21 -0800898
Austin Schuh58646e22021-08-23 23:51:46 -0700899 if (state->OldestMessageTime() != BootTimestamp::max_time()) {
900 state->set_startup_timer(
901 event_loop->AddTimer([state]() { state->RunOnStart(); }));
902 event_loop->OnRun([state]() {
903 BootTimestamp next_time = state->OldestMessageTime();
904 CHECK_EQ(next_time.boot, state->boot_count());
905 state->Setup(next_time.time);
906 state->SetupStartupTimer();
907 });
Austin Schuhe309d2a2019-11-29 13:25:21 -0800908 }
909}
910
911void LogReader::Deregister() {
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800912 // Make sure that things get destroyed in the correct order, rather than
913 // relying on getting the order correct in the class definition.
Austin Schuh8bd96322020-02-13 21:18:22 -0800914 for (std::unique_ptr<State> &state : states_) {
Austin Schuh858c9f32020-08-31 16:56:12 -0700915 state->Deregister();
Austin Schuhe309d2a2019-11-29 13:25:21 -0800916 }
Austin Schuh92547522019-12-28 14:33:43 -0800917
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800918 event_loop_factory_unique_ptr_.reset();
919 event_loop_factory_ = nullptr;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800920}
921
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800922void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type,
Austin Schuh0de30f32020-12-06 12:44:28 -0800923 std::string_view add_prefix,
924 std::string_view new_type) {
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800925 for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) {
926 const Channel *const channel = logged_configuration()->channels()->Get(ii);
927 if (channel->name()->str() == name &&
928 channel->type()->string_view() == type) {
929 CHECK_EQ(0u, remapped_channels_.count(ii))
930 << "Already remapped channel "
931 << configuration::CleanedChannelToString(channel);
Austin Schuh0de30f32020-12-06 12:44:28 -0800932 RemappedChannel remapped_channel;
933 remapped_channel.remapped_name =
934 std::string(add_prefix) + std::string(name);
935 remapped_channel.new_type = new_type;
936 remapped_channels_[ii] = std::move(remapped_channel);
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800937 VLOG(1) << "Remapping channel "
938 << configuration::CleanedChannelToString(channel)
Austin Schuh0de30f32020-12-06 12:44:28 -0800939 << " to have name " << remapped_channels_[ii].remapped_name;
Austin Schuh6331ef92020-01-07 18:28:09 -0800940 MakeRemappedConfig();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800941 return;
942 }
943 }
944 LOG(FATAL) << "Unabled to locate channel with name " << name << " and type "
945 << type;
946}
947
Austin Schuh01b4c352020-09-21 23:09:39 -0700948void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type,
949 const Node *node,
Austin Schuh0de30f32020-12-06 12:44:28 -0800950 std::string_view add_prefix,
951 std::string_view new_type) {
Austin Schuh01b4c352020-09-21 23:09:39 -0700952 VLOG(1) << "Node is " << aos::FlatbufferToJson(node);
953 const Channel *remapped_channel =
954 configuration::GetChannel(logged_configuration(), name, type, "", node);
955 CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name
956 << "\", \"type\": \"" << type << "\"}";
957 VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type
958 << "\"}";
959 VLOG(1) << "Remapped "
960 << aos::configuration::StrippedChannelToString(remapped_channel);
961
962 // We want to make /spray on node 0 go to /0/spray by snooping the maps. And
963 // we want it to degrade if the heuristics fail to just work.
964 //
965 // The easiest way to do this is going to be incredibly specific and verbose.
966 // Look up /spray, to /0/spray. Then, prefix the result with /original to get
967 // /original/0/spray. Then, create a map from /original/spray to
968 // /original/0/spray for just the type we were asked for.
969 if (name != remapped_channel->name()->string_view()) {
970 MapT new_map;
971 new_map.match = std::make_unique<ChannelT>();
972 new_map.match->name = absl::StrCat(add_prefix, name);
973 new_map.match->type = type;
974 if (node != nullptr) {
975 new_map.match->source_node = node->name()->str();
976 }
977 new_map.rename = std::make_unique<ChannelT>();
978 new_map.rename->name =
979 absl::StrCat(add_prefix, remapped_channel->name()->string_view());
980 maps_.emplace_back(std::move(new_map));
981 }
982
983 const size_t channel_index =
984 configuration::ChannelIndex(logged_configuration(), remapped_channel);
985 CHECK_EQ(0u, remapped_channels_.count(channel_index))
986 << "Already remapped channel "
987 << configuration::CleanedChannelToString(remapped_channel);
Austin Schuh0de30f32020-12-06 12:44:28 -0800988
989 RemappedChannel remapped_channel_struct;
990 remapped_channel_struct.remapped_name =
991 std::string(add_prefix) +
992 std::string(remapped_channel->name()->string_view());
993 remapped_channel_struct.new_type = new_type;
994 remapped_channels_[channel_index] = std::move(remapped_channel_struct);
Austin Schuh01b4c352020-09-21 23:09:39 -0700995 MakeRemappedConfig();
996}
997
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -0800998void LogReader::MakeRemappedConfig() {
Austin Schuh8bd96322020-02-13 21:18:22 -0800999 for (std::unique_ptr<State> &state : states_) {
Austin Schuh6aa77be2020-02-22 21:06:40 -08001000 if (state) {
Austin Schuh858c9f32020-08-31 16:56:12 -07001001 CHECK(!state->event_loop())
Austin Schuh6aa77be2020-02-22 21:06:40 -08001002 << ": Can't change the mapping after the events are scheduled.";
1003 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001004 }
Austin Schuhac0771c2020-01-07 18:36:30 -08001005
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001006 // If no remapping occurred and we are using the original config, then there
1007 // is nothing interesting to do here.
1008 if (remapped_channels_.empty() && replay_configuration_ == nullptr) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001009 remapped_configuration_ = logged_configuration();
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001010 return;
1011 }
1012 // Config to copy Channel definitions from. Use the specified
1013 // replay_configuration_ if it has been provided.
1014 const Configuration *const base_config = replay_configuration_ == nullptr
1015 ? logged_configuration()
1016 : replay_configuration_;
Austin Schuh0de30f32020-12-06 12:44:28 -08001017
1018 // Create a config with all the channels, but un-sorted/merged. Collect up
1019 // the schemas while we do this. Call MergeConfiguration to sort everything,
1020 // and then merge it all in together.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001021
1022 // This is the builder that we use for the config containing all the new
1023 // channels.
Austin Schuh0de30f32020-12-06 12:44:28 -08001024 flatbuffers::FlatBufferBuilder fbb;
1025 fbb.ForceDefaults(true);
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001026 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
Austin Schuh0de30f32020-12-06 12:44:28 -08001027
1028 CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u)
1029 << ": Merging logic needs to be updated when the number of channel "
1030 "fields changes.";
1031
1032 // List of schemas.
1033 std::map<std::string_view, FlatbufferVector<reflection::Schema>> schema_map;
1034 // Make sure our new RemoteMessage schema is in there for old logs without it.
1035 schema_map.insert(std::make_pair(
1036 RemoteMessage::GetFullyQualifiedName(),
1037 FlatbufferVector<reflection::Schema>(FlatbufferSpan<reflection::Schema>(
1038 message_bridge::RemoteMessageSchema()))));
1039
1040 // Reconstruct the remapped channels.
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001041 for (auto &pair : remapped_channels_) {
Austin Schuh0de30f32020-12-06 12:44:28 -08001042 const Channel *const c = CHECK_NOTNULL(configuration::GetChannel(
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001043 base_config, logged_configuration()->channels()->Get(pair.first), "",
1044 nullptr));
Austin Schuh0de30f32020-12-06 12:44:28 -08001045 channel_offsets.emplace_back(
1046 CopyChannel(c, pair.second.remapped_name, "", &fbb));
Austin Schuh006a9f52021-04-07 16:24:18 -07001047
1048 if (c->has_destination_nodes()) {
1049 for (const Connection *connection : *c->destination_nodes()) {
1050 switch (connection->timestamp_logger()) {
1051 case LoggerConfig::LOCAL_LOGGER:
1052 case LoggerConfig::NOT_LOGGED:
1053 // There is no timestamp channel associated with this, so ignore it.
1054 break;
1055
1056 case LoggerConfig::REMOTE_LOGGER:
1057 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
1058 // We want to make a split timestamp channel regardless of what type
1059 // of log this used to be. No sense propagating the single
1060 // timestamp channel.
1061
1062 CHECK(connection->has_timestamp_logger_nodes());
1063 for (const flatbuffers::String *timestamp_logger_node :
1064 *connection->timestamp_logger_nodes()) {
1065 const Node *node = configuration::GetNode(
1066 logged_configuration(), timestamp_logger_node->string_view());
1067 message_bridge::ChannelTimestampFinder finder(
1068 logged_configuration(), "log_reader", node);
1069
1070 // We are assuming here that all the maps are setup correctly to
1071 // handle arbitrary timestamps. Apply the maps for this node to
1072 // see what name this ends up with.
1073 std::string name = finder.SplitChannelName(
1074 pair.second.remapped_name, c->type()->str(), connection);
1075 std::string unmapped_name = name;
1076 configuration::HandleMaps(logged_configuration()->maps(), &name,
1077 "aos.message_bridge.RemoteMessage",
1078 node);
1079 CHECK_NE(name, unmapped_name)
1080 << ": Remote timestamp channel was not remapped, this is "
1081 "very fishy";
1082 flatbuffers::Offset<flatbuffers::String> channel_name_offset =
1083 fbb.CreateString(name);
1084 flatbuffers::Offset<flatbuffers::String> channel_type_offset =
1085 fbb.CreateString("aos.message_bridge.RemoteMessage");
1086 flatbuffers::Offset<flatbuffers::String> source_node_offset =
1087 fbb.CreateString(timestamp_logger_node->string_view());
1088
1089 // Now, build a channel. Don't log it, 2 senders, and match the
1090 // source frequency.
1091 Channel::Builder channel_builder(fbb);
1092 channel_builder.add_name(channel_name_offset);
1093 channel_builder.add_type(channel_type_offset);
1094 channel_builder.add_source_node(source_node_offset);
1095 channel_builder.add_logger(LoggerConfig::NOT_LOGGED);
1096 channel_builder.add_num_senders(2);
1097 if (c->has_frequency()) {
1098 channel_builder.add_frequency(c->frequency());
1099 }
1100 channel_offsets.emplace_back(channel_builder.Finish());
1101 }
1102 break;
1103 }
1104 }
1105 }
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001106 }
Austin Schuh01b4c352020-09-21 23:09:39 -07001107
Austin Schuh0de30f32020-12-06 12:44:28 -08001108 // Now reconstruct the original channels, translating types as needed
1109 for (const Channel *c : *base_config->channels()) {
1110 // Search for a mapping channel.
1111 std::string_view new_type = "";
1112 for (auto &pair : remapped_channels_) {
1113 const Channel *const remapped_channel =
1114 logged_configuration()->channels()->Get(pair.first);
1115 if (remapped_channel->name()->string_view() == c->name()->string_view() &&
1116 remapped_channel->type()->string_view() == c->type()->string_view()) {
1117 new_type = pair.second.new_type;
1118 break;
1119 }
1120 }
1121
1122 // Copy everything over.
1123 channel_offsets.emplace_back(CopyChannel(c, "", new_type, &fbb));
1124
1125 // Add the schema if it doesn't exist.
1126 if (schema_map.find(c->type()->string_view()) == schema_map.end()) {
1127 CHECK(c->has_schema());
1128 schema_map.insert(std::make_pair(c->type()->string_view(),
1129 RecursiveCopyFlatBuffer(c->schema())));
1130 }
1131 }
1132
1133 // The MergeConfiguration API takes a vector, not a map. Convert.
1134 std::vector<FlatbufferVector<reflection::Schema>> schemas;
1135 while (!schema_map.empty()) {
1136 schemas.emplace_back(std::move(schema_map.begin()->second));
1137 schema_map.erase(schema_map.begin());
1138 }
1139
1140 // Create the Configuration containing the new channels that we want to add.
1141 const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
1142 channels_offset =
1143 channel_offsets.empty() ? 0 : fbb.CreateVector(channel_offsets);
1144
1145 // Copy over the old maps.
Austin Schuh01b4c352020-09-21 23:09:39 -07001146 std::vector<flatbuffers::Offset<Map>> map_offsets;
Austin Schuh0de30f32020-12-06 12:44:28 -08001147 if (base_config->maps()) {
1148 for (const Map *map : *base_config->maps()) {
1149 map_offsets.emplace_back(aos::RecursiveCopyFlatBuffer(map, &fbb));
1150 }
1151 }
1152
1153 // Now create the new maps. These are second so they take effect first.
Austin Schuh01b4c352020-09-21 23:09:39 -07001154 for (const MapT &map : maps_) {
1155 const flatbuffers::Offset<flatbuffers::String> match_name_offset =
Austin Schuh0de30f32020-12-06 12:44:28 -08001156 fbb.CreateString(map.match->name);
Austin Schuh01b4c352020-09-21 23:09:39 -07001157 const flatbuffers::Offset<flatbuffers::String> match_type_offset =
Austin Schuh0de30f32020-12-06 12:44:28 -08001158 fbb.CreateString(map.match->type);
Austin Schuh01b4c352020-09-21 23:09:39 -07001159 const flatbuffers::Offset<flatbuffers::String> rename_name_offset =
Austin Schuh0de30f32020-12-06 12:44:28 -08001160 fbb.CreateString(map.rename->name);
Austin Schuh01b4c352020-09-21 23:09:39 -07001161 flatbuffers::Offset<flatbuffers::String> match_source_node_offset;
1162 if (!map.match->source_node.empty()) {
Austin Schuh0de30f32020-12-06 12:44:28 -08001163 match_source_node_offset = fbb.CreateString(map.match->source_node);
Austin Schuh01b4c352020-09-21 23:09:39 -07001164 }
Austin Schuh0de30f32020-12-06 12:44:28 -08001165 Channel::Builder match_builder(fbb);
Austin Schuh01b4c352020-09-21 23:09:39 -07001166 match_builder.add_name(match_name_offset);
1167 match_builder.add_type(match_type_offset);
1168 if (!map.match->source_node.empty()) {
1169 match_builder.add_source_node(match_source_node_offset);
1170 }
1171 const flatbuffers::Offset<Channel> match_offset = match_builder.Finish();
1172
Austin Schuh0de30f32020-12-06 12:44:28 -08001173 Channel::Builder rename_builder(fbb);
Austin Schuh01b4c352020-09-21 23:09:39 -07001174 rename_builder.add_name(rename_name_offset);
1175 const flatbuffers::Offset<Channel> rename_offset = rename_builder.Finish();
1176
Austin Schuh0de30f32020-12-06 12:44:28 -08001177 Map::Builder map_builder(fbb);
Austin Schuh01b4c352020-09-21 23:09:39 -07001178 map_builder.add_match(match_offset);
1179 map_builder.add_rename(rename_offset);
1180 map_offsets.emplace_back(map_builder.Finish());
1181 }
1182
Austin Schuh0de30f32020-12-06 12:44:28 -08001183 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
1184 maps_offsets = map_offsets.empty() ? 0 : fbb.CreateVector(map_offsets);
Austin Schuh01b4c352020-09-21 23:09:39 -07001185
Austin Schuh0de30f32020-12-06 12:44:28 -08001186 // And copy everything else over.
1187 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
1188 nodes_offset = aos::RecursiveCopyVectorTable(base_config->nodes(), &fbb);
1189
1190 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
1191 applications_offset =
1192 aos::RecursiveCopyVectorTable(base_config->applications(), &fbb);
1193
1194 // Now insert everything else in unmodified.
1195 ConfigurationBuilder configuration_builder(fbb);
1196 if (!channels_offset.IsNull()) {
1197 configuration_builder.add_channels(channels_offset);
1198 }
1199 if (!maps_offsets.IsNull()) {
1200 configuration_builder.add_maps(maps_offsets);
1201 }
1202 if (!nodes_offset.IsNull()) {
1203 configuration_builder.add_nodes(nodes_offset);
1204 }
1205 if (!applications_offset.IsNull()) {
1206 configuration_builder.add_applications(applications_offset);
1207 }
1208
1209 if (base_config->has_channel_storage_duration()) {
1210 configuration_builder.add_channel_storage_duration(
1211 base_config->channel_storage_duration());
1212 }
1213
1214 CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u)
1215 << ": Merging logic needs to be updated when the number of configuration "
1216 "fields changes.";
1217
1218 fbb.Finish(configuration_builder.Finish());
1219
1220 // Clean it up and return it! By using MergeConfiguration here, we'll
1221 // actually get a deduplicated config for free too.
1222 FlatbufferDetachedBuffer<Configuration> new_merged_config =
1223 configuration::MergeConfiguration(
1224 FlatbufferDetachedBuffer<Configuration>(fbb.Release()));
1225
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001226 remapped_configuration_buffer_ =
1227 std::make_unique<FlatbufferDetachedBuffer<Configuration>>(
Austin Schuh0de30f32020-12-06 12:44:28 -08001228 configuration::MergeConfiguration(new_merged_config, schemas));
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001229
1230 remapped_configuration_ = &remapped_configuration_buffer_->message();
Austin Schuh0de30f32020-12-06 12:44:28 -08001231
1232 // TODO(austin): Lazily re-build to save CPU?
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -08001233}
1234
Austin Schuh6f3babe2020-01-26 20:34:50 -08001235const Channel *LogReader::RemapChannel(const EventLoop *event_loop,
Austin Schuh58646e22021-08-23 23:51:46 -07001236 const Node *node,
Austin Schuh6f3babe2020-01-26 20:34:50 -08001237 const Channel *channel) {
1238 std::string_view channel_name = channel->name()->string_view();
1239 std::string_view channel_type = channel->type()->string_view();
1240 const int channel_index =
1241 configuration::ChannelIndex(logged_configuration(), channel);
1242 // If the channel is remapped, find the correct channel name to use.
1243 if (remapped_channels_.count(channel_index) > 0) {
Austin Schuhee711052020-08-24 16:06:09 -07001244 VLOG(3) << "Got remapped channel on "
Austin Schuh6f3babe2020-01-26 20:34:50 -08001245 << configuration::CleanedChannelToString(channel);
Austin Schuh0de30f32020-12-06 12:44:28 -08001246 channel_name = remapped_channels_[channel_index].remapped_name;
Austin Schuh6f3babe2020-01-26 20:34:50 -08001247 }
1248
Austin Schuhee711052020-08-24 16:06:09 -07001249 VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type;
Austin Schuh6f3babe2020-01-26 20:34:50 -08001250 const Channel *remapped_channel = configuration::GetChannel(
Austin Schuh58646e22021-08-23 23:51:46 -07001251 configuration(), channel_name, channel_type,
1252 event_loop ? event_loop->name() : "log_reader", node);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001253
1254 CHECK(remapped_channel != nullptr)
1255 << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \""
1256 << channel_type << "\"} because it is not in the provided configuration.";
1257
1258 return remapped_channel;
1259}
1260
Austin Schuh58646e22021-08-23 23:51:46 -07001261LogReader::State::State(std::unique_ptr<TimestampMapper> timestamp_mapper,
1262 const Node *node)
1263 : timestamp_mapper_(std::move(timestamp_mapper)), node_(node) {}
Austin Schuh287d43d2020-12-04 20:19:33 -08001264
1265void LogReader::State::AddPeer(State *peer) {
1266 if (timestamp_mapper_ && peer->timestamp_mapper_) {
1267 timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get());
1268 }
1269}
Austin Schuh858c9f32020-08-31 16:56:12 -07001270
Austin Schuh58646e22021-08-23 23:51:46 -07001271void LogReader::State::SetNodeEventLoopFactory(
Austin Schuh858c9f32020-08-31 16:56:12 -07001272 NodeEventLoopFactory *node_event_loop_factory) {
1273 node_event_loop_factory_ = node_event_loop_factory;
Austin Schuh858c9f32020-08-31 16:56:12 -07001274}
1275
1276void LogReader::State::SetChannelCount(size_t count) {
1277 channels_.resize(count);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001278 remote_timestamp_senders_.resize(count);
Austin Schuh858c9f32020-08-31 16:56:12 -07001279 filters_.resize(count);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001280 channel_source_state_.resize(count);
1281 factory_channel_index_.resize(count);
1282 queue_index_map_.resize(count);
Austin Schuh858c9f32020-08-31 16:56:12 -07001283}
1284
Austin Schuh58646e22021-08-23 23:51:46 -07001285void LogReader::State::SetRemoteTimestampSender(
1286 size_t logged_channel_index, RemoteMessageSender *remote_timestamp_sender) {
1287 remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender;
1288}
1289
Austin Schuh858c9f32020-08-31 16:56:12 -07001290void LogReader::State::SetChannel(
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001291 size_t logged_channel_index, size_t factory_channel_index,
1292 std::unique_ptr<RawSender> sender,
Austin Schuh58646e22021-08-23 23:51:46 -07001293 message_bridge::NoncausalOffsetEstimator *filter, bool is_forwarded,
1294 State *source_state) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001295 channels_[logged_channel_index] = std::move(sender);
1296 filters_[logged_channel_index] = filter;
Austin Schuh58646e22021-08-23 23:51:46 -07001297 channel_source_state_[logged_channel_index] = source_state;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001298
Austin Schuh58646e22021-08-23 23:51:46 -07001299 if (is_forwarded) {
1300 queue_index_map_[logged_channel_index] =
1301 std::make_unique<std::vector<State::ContiguousSentTimestamp>>();
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001302 }
1303
1304 factory_channel_index_[logged_channel_index] = factory_channel_index;
1305}
1306
Austin Schuh287d43d2020-12-04 20:19:33 -08001307bool LogReader::State::Send(const TimestampedMessage &timestamped_message) {
1308 aos::RawSender *sender = channels_[timestamped_message.channel_index].get();
Austin Schuh58646e22021-08-23 23:51:46 -07001309 CHECK(sender);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001310 uint32_t remote_queue_index = 0xffffffff;
1311
Austin Schuh287d43d2020-12-04 20:19:33 -08001312 if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001313 State *source_state =
1314 CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]);
Austin Schuh9942bae2021-01-07 22:06:44 -08001315 std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL(
Austin Schuh58646e22021-08-23 23:51:46 -07001316 source_state->queue_index_map_[timestamped_message.channel_index]
Austin Schuh287d43d2020-12-04 20:19:33 -08001317 .get());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001318
Austin Schuh9942bae2021-01-07 22:06:44 -08001319 struct SentTimestamp {
1320 monotonic_clock::time_point monotonic_event_time;
1321 uint32_t queue_index;
1322 } search;
1323
Austin Schuh58646e22021-08-23 23:51:46 -07001324 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1325 source_state->boot_count());
Tyler Chatowbf0609c2021-07-31 16:13:27 -07001326 search.monotonic_event_time =
1327 timestamped_message.monotonic_remote_time.time;
Austin Schuh58646e22021-08-23 23:51:46 -07001328 search.queue_index = timestamped_message.remote_queue_index.index;
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001329
1330 // Find the sent time if available.
1331 auto element = std::lower_bound(
1332 queue_index_map->begin(), queue_index_map->end(), search,
Austin Schuh9942bae2021-01-07 22:06:44 -08001333 [](ContiguousSentTimestamp a, SentTimestamp b) {
1334 if (a.ending_monotonic_event_time < b.monotonic_event_time) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001335 return true;
1336 }
Austin Schuh9942bae2021-01-07 22:06:44 -08001337 if (a.starting_monotonic_event_time > b.monotonic_event_time) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001338 return false;
1339 }
Austin Schuh9942bae2021-01-07 22:06:44 -08001340
1341 if (a.ending_queue_index < b.queue_index) {
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001342 return true;
1343 }
Austin Schuh9942bae2021-01-07 22:06:44 -08001344 if (a.starting_queue_index >= b.queue_index) {
1345 return false;
1346 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001347
Austin Schuh9942bae2021-01-07 22:06:44 -08001348 // If it isn't clearly below or above, it is below. Since we return
1349 // the last element <, this will return a match.
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001350 return false;
1351 });
1352
1353 // TODO(austin): Be a bit more principled here, but we will want to do that
1354 // after the logger rewrite. We hit this when one node finishes, but the
1355 // other node isn't done yet. So there is no send time, but there is a
1356 // receive time.
1357 if (element != queue_index_map->end()) {
Austin Schuh58646e22021-08-23 23:51:46 -07001358 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1359 source_state->boot_count());
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001360
1361 CHECK_GE(timestamped_message.monotonic_remote_time.time,
Austin Schuh9942bae2021-01-07 22:06:44 -08001362 element->starting_monotonic_event_time);
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001363 CHECK_LE(timestamped_message.monotonic_remote_time.time,
Austin Schuh9942bae2021-01-07 22:06:44 -08001364 element->ending_monotonic_event_time);
Austin Schuh58646e22021-08-23 23:51:46 -07001365 CHECK_GE(timestamped_message.remote_queue_index.index,
Austin Schuh9942bae2021-01-07 22:06:44 -08001366 element->starting_queue_index);
Austin Schuh58646e22021-08-23 23:51:46 -07001367 CHECK_LE(timestamped_message.remote_queue_index.index,
Austin Schuh9942bae2021-01-07 22:06:44 -08001368 element->ending_queue_index);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001369
Austin Schuh58646e22021-08-23 23:51:46 -07001370 remote_queue_index = timestamped_message.remote_queue_index.index +
Austin Schuh9942bae2021-01-07 22:06:44 -08001371 element->actual_queue_index -
1372 element->starting_queue_index;
1373 } else {
1374 VLOG(1) << "No timestamp match in the map.";
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001375 }
Austin Schuh58646e22021-08-23 23:51:46 -07001376 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1377 source_state->boot_count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001378 }
1379
1380 // Send! Use the replayed queue index here instead of the logged queue index
1381 // for the remote queue index. This makes re-logging work.
Austin Schuh287d43d2020-12-04 20:19:33 -08001382 const bool sent = sender->Send(
1383 timestamped_message.data.message().data()->Data(),
1384 timestamped_message.data.message().data()->size(),
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001385 timestamped_message.monotonic_remote_time.time,
Austin Schuh8902fa52021-03-14 22:39:24 -07001386 timestamped_message.realtime_remote_time, remote_queue_index,
1387 (channel_source_state_[timestamped_message.channel_index] != nullptr
1388 ? CHECK_NOTNULL(
1389 channel_source_state_[timestamped_message.channel_index])
1390 ->event_loop_->boot_uuid()
1391 : event_loop_->boot_uuid()));
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001392 if (!sent) return false;
1393
Austin Schuh287d43d2020-12-04 20:19:33 -08001394 if (queue_index_map_[timestamped_message.channel_index]) {
Austin Schuh58646e22021-08-23 23:51:46 -07001395 CHECK_EQ(timestamped_message.monotonic_event_time.boot, boot_count());
Austin Schuh9942bae2021-01-07 22:06:44 -08001396 if (queue_index_map_[timestamped_message.channel_index]->empty()) {
1397 // Nothing here, start a range with 0 length.
1398 ContiguousSentTimestamp timestamp;
1399 timestamp.starting_monotonic_event_time =
1400 timestamp.ending_monotonic_event_time =
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001401 timestamped_message.monotonic_event_time.time;
Austin Schuh9942bae2021-01-07 22:06:44 -08001402 timestamp.starting_queue_index = timestamp.ending_queue_index =
Austin Schuh58646e22021-08-23 23:51:46 -07001403 timestamped_message.queue_index.index;
Austin Schuh9942bae2021-01-07 22:06:44 -08001404 timestamp.actual_queue_index = sender->sent_queue_index();
1405 queue_index_map_[timestamped_message.channel_index]->emplace_back(
1406 timestamp);
1407 } else {
1408 // We've got something. See if the next timestamp is still contiguous. If
1409 // so, grow it.
1410 ContiguousSentTimestamp *back =
1411 &queue_index_map_[timestamped_message.channel_index]->back();
1412 if ((back->starting_queue_index - back->actual_queue_index) ==
Austin Schuh58646e22021-08-23 23:51:46 -07001413 (timestamped_message.queue_index.index - sender->sent_queue_index())) {
1414 back->ending_queue_index = timestamped_message.queue_index.index;
Austin Schuh9942bae2021-01-07 22:06:44 -08001415 back->ending_monotonic_event_time =
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001416 timestamped_message.monotonic_event_time.time;
Austin Schuh9942bae2021-01-07 22:06:44 -08001417 } else {
1418 // Otherwise, make a new one.
1419 ContiguousSentTimestamp timestamp;
1420 timestamp.starting_monotonic_event_time =
1421 timestamp.ending_monotonic_event_time =
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001422 timestamped_message.monotonic_event_time.time;
Austin Schuh9942bae2021-01-07 22:06:44 -08001423 timestamp.starting_queue_index = timestamp.ending_queue_index =
Austin Schuh58646e22021-08-23 23:51:46 -07001424 timestamped_message.queue_index.index;
Austin Schuh9942bae2021-01-07 22:06:44 -08001425 timestamp.actual_queue_index = sender->sent_queue_index();
1426 queue_index_map_[timestamped_message.channel_index]->emplace_back(
1427 timestamp);
1428 }
1429 }
1430
1431 // TODO(austin): Should we prune the map? On a many day log, I only saw the
1432 // queue index diverge a couple of elements, which would be a very small
1433 // map.
Austin Schuh287d43d2020-12-04 20:19:33 -08001434 } else if (remote_timestamp_senders_[timestamped_message.channel_index] !=
1435 nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001436 State *source_state =
1437 CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]);
1438
Austin Schuh969cd602021-01-03 00:09:45 -08001439 flatbuffers::FlatBufferBuilder fbb;
1440 fbb.ForceDefaults(true);
Austin Schuhcdd90272021-03-15 12:46:16 -07001441 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> boot_uuid_offset =
1442 event_loop_->boot_uuid().PackVector(&fbb);
Austin Schuh315b96b2020-12-11 21:21:12 -08001443
Austin Schuh969cd602021-01-03 00:09:45 -08001444 RemoteMessage::Builder message_header_builder(fbb);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001445
1446 message_header_builder.add_channel_index(
Austin Schuh287d43d2020-12-04 20:19:33 -08001447 factory_channel_index_[timestamped_message.channel_index]);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001448
1449 // Swap the remote and sent metrics. They are from the sender's
1450 // perspective, not the receiver's perspective.
1451 message_header_builder.add_monotonic_sent_time(
1452 sender->monotonic_sent_time().time_since_epoch().count());
1453 message_header_builder.add_realtime_sent_time(
1454 sender->realtime_sent_time().time_since_epoch().count());
1455 message_header_builder.add_queue_index(sender->sent_queue_index());
1456
Austin Schuh58646e22021-08-23 23:51:46 -07001457 CHECK_EQ(timestamped_message.monotonic_remote_time.boot,
1458 source_state->boot_count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001459 message_header_builder.add_monotonic_remote_time(
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001460 timestamped_message.monotonic_remote_time.time.time_since_epoch()
1461 .count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001462 message_header_builder.add_realtime_remote_time(
Austin Schuh287d43d2020-12-04 20:19:33 -08001463 timestamped_message.realtime_remote_time.time_since_epoch().count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001464
1465 message_header_builder.add_remote_queue_index(remote_queue_index);
Austin Schuh315b96b2020-12-11 21:21:12 -08001466 message_header_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001467
Austin Schuh969cd602021-01-03 00:09:45 -08001468 fbb.Finish(message_header_builder.Finish());
1469
1470 remote_timestamp_senders_[timestamped_message.channel_index]->Send(
1471 FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()),
Austin Schuh58646e22021-08-23 23:51:46 -07001472 timestamped_message.monotonic_timestamp_time,
1473 source_state->boot_count());
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001474 }
1475
1476 return true;
1477}
1478
Austin Schuh969cd602021-01-03 00:09:45 -08001479LogReader::RemoteMessageSender::RemoteMessageSender(
1480 aos::Sender<message_bridge::RemoteMessage> sender, EventLoop *event_loop)
1481 : event_loop_(event_loop),
1482 sender_(std::move(sender)),
1483 timer_(event_loop->AddTimer([this]() { SendTimestamp(); })) {}
1484
1485void LogReader::RemoteMessageSender::ScheduleTimestamp() {
1486 if (remote_timestamps_.empty()) {
1487 CHECK_NOTNULL(timer_);
1488 timer_->Disable();
1489 scheduled_time_ = monotonic_clock::min_time;
1490 return;
1491 }
1492
1493 if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) {
1494 CHECK_NOTNULL(timer_);
Austin Schuh816e5d62021-01-05 23:42:20 -08001495 timer_->Setup(remote_timestamps_.front().monotonic_timestamp_time);
Austin Schuh969cd602021-01-03 00:09:45 -08001496 scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time;
Austin Schuh3d94be02021-02-12 23:15:20 -08001497 CHECK_GE(scheduled_time_, event_loop_->monotonic_now())
1498 << event_loop_->node()->name()->string_view();
Austin Schuh969cd602021-01-03 00:09:45 -08001499 }
1500}
1501
1502void LogReader::RemoteMessageSender::Send(
1503 FlatbufferDetachedBuffer<RemoteMessage> remote_message,
Austin Schuh58646e22021-08-23 23:51:46 -07001504 BootTimestamp monotonic_timestamp_time, size_t source_boot_count) {
Austin Schuhc41d6a82021-07-16 14:49:23 -07001505 // There are 2 variants of logs.
1506 // 1) Logs without monotonic_timestamp_time
1507 // 2) Logs with monotonic_timestamp_time
1508 //
1509 // As of Jan 2021, we shouldn't have any more logs without
1510 // monotonic_timestamp_time. We don't have data locked up in those logs worth
1511 // the effort of saving.
1512 //
1513 // This gives us 3 cases, 2 of which are undistinguishable.
1514 // 1) Old log without monotonic_timestamp_time.
1515 // 2) New log with monotonic_timestamp_time where the timestamp was logged
1516 // remotely so we actually have monotonic_timestamp_time.
1517 // 3) New log, but the timestamp was logged on the node receiving the message
1518 // so there is no monotonic_timestamp_time.
1519 //
1520 // Our goal when replaying is to accurately reproduce the state of the world
1521 // present when logging. If a timestamp wasn't sent back across the network,
1522 // we shouldn't replay one back across the network.
1523 //
1524 // Given that we don't really care about 1, we can use the presence of the
1525 // timestamp to distinguish 2 and 3, and ignore 1. If we don't have a
1526 // monotonic_timestamp_time, this means the message was logged locally and
1527 // remote timestamps can be ignored.
Austin Schuh58646e22021-08-23 23:51:46 -07001528 if (monotonic_timestamp_time == BootTimestamp::min_time()) {
Austin Schuhc41d6a82021-07-16 14:49:23 -07001529 return;
Austin Schuh969cd602021-01-03 00:09:45 -08001530 }
Austin Schuhc41d6a82021-07-16 14:49:23 -07001531
Austin Schuh58646e22021-08-23 23:51:46 -07001532 CHECK_EQ(monotonic_timestamp_time.boot, source_boot_count);
1533
Austin Schuhc41d6a82021-07-16 14:49:23 -07001534 remote_timestamps_.emplace(
1535 std::upper_bound(
1536 remote_timestamps_.begin(), remote_timestamps_.end(),
Austin Schuh58646e22021-08-23 23:51:46 -07001537 monotonic_timestamp_time.time,
Austin Schuhc41d6a82021-07-16 14:49:23 -07001538 [](const aos::monotonic_clock::time_point monotonic_timestamp_time,
1539 const Timestamp &timestamp) {
1540 return monotonic_timestamp_time <
1541 timestamp.monotonic_timestamp_time;
1542 }),
Austin Schuh58646e22021-08-23 23:51:46 -07001543 std::move(remote_message), monotonic_timestamp_time.time);
Austin Schuhc41d6a82021-07-16 14:49:23 -07001544 ScheduleTimestamp();
Austin Schuh969cd602021-01-03 00:09:45 -08001545}
1546
1547void LogReader::RemoteMessageSender::SendTimestamp() {
Austin Schuh3d94be02021-02-12 23:15:20 -08001548 CHECK_EQ(event_loop_->context().monotonic_event_time, scheduled_time_)
1549 << event_loop_->node()->name()->string_view();
Austin Schuh969cd602021-01-03 00:09:45 -08001550 CHECK(!remote_timestamps_.empty());
1551
1552 // Send out all timestamps at the currently scheduled time.
1553 while (remote_timestamps_.front().monotonic_timestamp_time ==
1554 scheduled_time_) {
1555 sender_.Send(std::move(remote_timestamps_.front().remote_message));
1556 remote_timestamps_.pop_front();
1557 if (remote_timestamps_.empty()) {
1558 break;
1559 }
1560 }
1561 scheduled_time_ = monotonic_clock::min_time;
1562
1563 ScheduleTimestamp();
1564}
1565
1566LogReader::RemoteMessageSender *LogReader::State::RemoteTimestampSender(
Austin Schuh61e973f2021-02-21 21:43:56 -08001567 const Channel *channel, const Connection *connection) {
1568 message_bridge::ChannelTimestampFinder finder(event_loop_);
1569 // Look at any pre-created channel/connection pairs.
1570 {
1571 auto it =
1572 channel_timestamp_loggers_.find(std::make_pair(channel, connection));
1573 if (it != channel_timestamp_loggers_.end()) {
1574 return it->second.get();
1575 }
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001576 }
1577
Austin Schuh61e973f2021-02-21 21:43:56 -08001578 // That failed, so resolve the RemoteMessage channel timestamps will be logged
1579 // to.
1580 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
1581
1582 {
1583 // See if that has been created before. If so, cache it in
1584 // channel_timestamp_loggers_ and return.
1585 auto it = timestamp_loggers_.find(timestamp_channel);
1586 if (it != timestamp_loggers_.end()) {
1587 CHECK(channel_timestamp_loggers_
1588 .try_emplace(std::make_pair(channel, connection), it->second)
1589 .second);
1590 return it->second.get();
1591 }
1592 }
1593
1594 // Otherwise, make a sender, save it, and cache it.
1595 auto result = channel_timestamp_loggers_.try_emplace(
1596 std::make_pair(channel, connection),
1597 std::make_shared<RemoteMessageSender>(
1598 event_loop()->MakeSender<RemoteMessage>(
1599 timestamp_channel->name()->string_view()),
1600 event_loop()));
1601
1602 CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second)
1603 .second);
1604 return result.first->second.get();
Austin Schuh858c9f32020-08-31 16:56:12 -07001605}
1606
Austin Schuhdda74ec2021-01-03 19:30:37 -08001607TimestampedMessage LogReader::State::PopOldest() {
Austin Schuhe639ea12021-01-25 13:00:22 -08001608 CHECK(timestamp_mapper_ != nullptr);
1609 TimestampedMessage *result_ptr = timestamp_mapper_->Front();
1610 CHECK(result_ptr != nullptr);
Austin Schuh858c9f32020-08-31 16:56:12 -07001611
Austin Schuhe639ea12021-01-25 13:00:22 -08001612 TimestampedMessage result = std::move(*result_ptr);
1613
Austin Schuh2f8fd752020-09-01 22:38:28 -07001614 VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping "
Austin Schuhe639ea12021-01-25 13:00:22 -08001615 << result.monotonic_event_time;
1616 timestamp_mapper_->PopFront();
Austin Schuh858c9f32020-08-31 16:56:12 -07001617 SeedSortedMessages();
1618
Austin Schuh58646e22021-08-23 23:51:46 -07001619 CHECK_EQ(result.monotonic_event_time.boot, boot_count());
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001620
Austin Schuh5ee56872021-01-30 16:53:34 -08001621 VLOG(1) << "Popped " << result
1622 << configuration::CleanedChannelToString(
1623 event_loop_->configuration()->channels()->Get(
1624 factory_channel_index_[result.channel_index]));
Austin Schuhe639ea12021-01-25 13:00:22 -08001625 return result;
Austin Schuh858c9f32020-08-31 16:56:12 -07001626}
1627
Austin Schuh58646e22021-08-23 23:51:46 -07001628BootTimestamp LogReader::State::OldestMessageTime() const {
Austin Schuhe639ea12021-01-25 13:00:22 -08001629 if (timestamp_mapper_ == nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001630 return BootTimestamp::max_time();
Austin Schuh287d43d2020-12-04 20:19:33 -08001631 }
Austin Schuhe639ea12021-01-25 13:00:22 -08001632 TimestampedMessage *result_ptr = timestamp_mapper_->Front();
1633 if (result_ptr == nullptr) {
Austin Schuh58646e22021-08-23 23:51:46 -07001634 return BootTimestamp::max_time();
Austin Schuhe639ea12021-01-25 13:00:22 -08001635 }
1636 VLOG(2) << MaybeNodeName(event_loop_->node()) << "oldest message at "
Austin Schuh2dc8c7d2021-07-01 17:41:28 -07001637 << result_ptr->monotonic_event_time.time;
Austin Schuh58646e22021-08-23 23:51:46 -07001638 return result_ptr->monotonic_event_time;
Austin Schuh858c9f32020-08-31 16:56:12 -07001639}
1640
1641void LogReader::State::SeedSortedMessages() {
Austin Schuh287d43d2020-12-04 20:19:33 -08001642 if (!timestamp_mapper_) return;
Austin Schuh858c9f32020-08-31 16:56:12 -07001643
Austin Schuhe639ea12021-01-25 13:00:22 -08001644 timestamp_mapper_->QueueFor(chrono::duration_cast<chrono::seconds>(
1645 chrono::duration<double>(FLAGS_time_estimation_buffer_seconds)));
Austin Schuh858c9f32020-08-31 16:56:12 -07001646}
1647
1648void LogReader::State::Deregister() {
Austin Schuh58646e22021-08-23 23:51:46 -07001649 if (started_ && !stopped_) {
1650 RunOnEnd();
1651 }
Austin Schuh858c9f32020-08-31 16:56:12 -07001652 for (size_t i = 0; i < channels_.size(); ++i) {
1653 channels_[i].reset();
1654 }
Austin Schuh61e973f2021-02-21 21:43:56 -08001655 channel_timestamp_loggers_.clear();
1656 timestamp_loggers_.clear();
Austin Schuh858c9f32020-08-31 16:56:12 -07001657 event_loop_unique_ptr_.reset();
1658 event_loop_ = nullptr;
1659 timer_handler_ = nullptr;
1660 node_event_loop_factory_ = nullptr;
1661}
1662
Austin Schuhe309d2a2019-11-29 13:25:21 -08001663} // namespace logger
1664} // namespace aos