Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/log_reader.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/uio.h> |
Brian Silverman | 8ff74aa | 2021-02-05 16:37:15 -0800 | [diff] [blame] | 7 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 8 | #include <climits> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 11 | #include "absl/strings/escaping.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 12 | #include "absl/types/span.h" |
| 13 | #include "aos/events/event_loop.h" |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 14 | #include "aos/events/logging/boot_timestamp.h" |
Austin Schuh | f6f9bf3 | 2020-10-11 14:37:43 -0700 | [diff] [blame] | 15 | #include "aos/events/logging/logfile_sorting.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 16 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 17 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 18 | #include "aos/network/multinode_timestamp_filter.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 19 | #include "aos/network/remote_message_generated.h" |
| 20 | #include "aos/network/remote_message_schema.h" |
Austin Schuh | 288479d | 2019-12-18 19:47:52 -0800 | [diff] [blame] | 21 | #include "aos/network/team_number.h" |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 22 | #include "aos/network/timestamp_channel.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 23 | #include "aos/time/time.h" |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 24 | #include "aos/util/file.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 25 | #include "aos/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 26 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 27 | #include "openssl/sha.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 28 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 29 | DEFINE_bool(skip_missing_forwarding_entries, false, |
| 30 | "If true, drop any forwarding entries with missing data. If " |
| 31 | "false, CHECK."); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 33 | DECLARE_bool(timestamps_to_csv); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 34 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 35 | DEFINE_bool(skip_order_validation, false, |
| 36 | "If true, ignore any out of orderness in replay"); |
| 37 | |
Austin Schuh | f068866 | 2020-12-19 15:37:45 -0800 | [diff] [blame] | 38 | DEFINE_double( |
| 39 | time_estimation_buffer_seconds, 2.0, |
| 40 | "The time to buffer ahead in the log file to accurately reconstruct time."); |
| 41 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 42 | namespace aos { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 43 | namespace configuration { |
| 44 | // We don't really want to expose this publicly, but log reader doesn't really |
| 45 | // want to re-implement it. |
| 46 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 47 | std::string *name, std::string_view type, const Node *node); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 48 | } // namespace configuration |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 49 | namespace logger { |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 50 | namespace { |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 1c22735 | 2021-09-17 12:53:54 -0700 | [diff] [blame] | 52 | bool CompareChannels(const Channel *c, |
| 53 | ::std::pair<std::string_view, std::string_view> p) { |
| 54 | int name_compare = c->name()->string_view().compare(p.first); |
| 55 | if (name_compare == 0) { |
| 56 | return c->type()->string_view() < p.second; |
| 57 | } else if (name_compare < 0) { |
| 58 | return true; |
| 59 | } else { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | bool EqualsChannels(const Channel *c, |
| 65 | ::std::pair<std::string_view, std::string_view> p) { |
| 66 | return c->name()->string_view() == p.first && |
| 67 | c->type()->string_view() == p.second; |
| 68 | } |
| 69 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 70 | // Copies the channel, removing the schema as we go. If new_name is provided, |
| 71 | // it is used instead of the name inside the channel. If new_type is provided, |
| 72 | // it is used instead of the type in the channel. |
| 73 | flatbuffers::Offset<Channel> CopyChannel(const Channel *c, |
| 74 | std::string_view new_name, |
| 75 | std::string_view new_type, |
| 76 | flatbuffers::FlatBufferBuilder *fbb) { |
| 77 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 78 | fbb->CreateSharedString(new_name.empty() ? c->name()->string_view() |
| 79 | : new_name); |
| 80 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 81 | fbb->CreateSharedString(new_type.empty() ? c->type()->str() : new_type); |
| 82 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 83 | c->has_source_node() ? fbb->CreateSharedString(c->source_node()->str()) |
| 84 | : 0; |
| 85 | |
| 86 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 87 | destination_nodes_offset = |
| 88 | aos::RecursiveCopyVectorTable(c->destination_nodes(), fbb); |
| 89 | |
| 90 | flatbuffers::Offset< |
| 91 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 92 | logger_nodes_offset = aos::CopyVectorSharedString(c->logger_nodes(), fbb); |
| 93 | |
| 94 | Channel::Builder channel_builder(*fbb); |
| 95 | channel_builder.add_name(name_offset); |
| 96 | channel_builder.add_type(type_offset); |
| 97 | if (c->has_frequency()) { |
| 98 | channel_builder.add_frequency(c->frequency()); |
| 99 | } |
| 100 | if (c->has_max_size()) { |
| 101 | channel_builder.add_max_size(c->max_size()); |
| 102 | } |
| 103 | if (c->has_num_senders()) { |
| 104 | channel_builder.add_num_senders(c->num_senders()); |
| 105 | } |
| 106 | if (c->has_num_watchers()) { |
| 107 | channel_builder.add_num_watchers(c->num_watchers()); |
| 108 | } |
| 109 | if (!source_node_offset.IsNull()) { |
| 110 | channel_builder.add_source_node(source_node_offset); |
| 111 | } |
| 112 | if (!destination_nodes_offset.IsNull()) { |
| 113 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 114 | } |
| 115 | if (c->has_logger()) { |
| 116 | channel_builder.add_logger(c->logger()); |
| 117 | } |
| 118 | if (!logger_nodes_offset.IsNull()) { |
| 119 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 120 | } |
| 121 | if (c->has_read_method()) { |
| 122 | channel_builder.add_read_method(c->read_method()); |
| 123 | } |
| 124 | if (c->has_num_readers()) { |
| 125 | channel_builder.add_num_readers(c->num_readers()); |
| 126 | } |
| 127 | return channel_builder.Finish(); |
| 128 | } |
| 129 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 130 | namespace chrono = std::chrono; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 131 | using message_bridge::RemoteMessage; |
Austin Schuh | 0afc4d1 | 2020-10-19 11:42:04 -0700 | [diff] [blame] | 132 | } // namespace |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 133 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 134 | LogReader::LogReader(std::string_view filename, |
| 135 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 136 | : LogReader(SortParts({std::string(filename)}), replay_configuration) {} |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 137 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 138 | LogReader::LogReader(std::vector<LogFile> log_files, |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 139 | const Configuration *replay_configuration) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 140 | : log_files_(std::move(log_files)), |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 141 | replay_configuration_(replay_configuration) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 142 | CHECK_GT(log_files_.size(), 0u); |
| 143 | { |
| 144 | // Validate that we have the same config everwhere. This will be true if |
| 145 | // all the parts were sorted together and the configs match. |
| 146 | const Configuration *config = nullptr; |
Austin Schuh | 297d235 | 2021-01-21 19:02:17 -0800 | [diff] [blame] | 147 | for (const LogFile &log_file : log_files_) { |
| 148 | if (log_file.config.get() == nullptr) { |
| 149 | LOG(FATAL) << "Couldn't find a config in " << log_file; |
| 150 | } |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 151 | if (config == nullptr) { |
| 152 | config = log_file.config.get(); |
| 153 | } else { |
| 154 | CHECK_EQ(config, log_file.config.get()); |
| 155 | } |
| 156 | } |
| 157 | } |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 158 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 159 | MakeRemappedConfig(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 160 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 161 | // Remap all existing remote timestamp channels. They will be recreated, and |
| 162 | // the data logged isn't relevant anymore. |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 163 | for (const Node *node : configuration::GetNodes(logged_configuration())) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 164 | message_bridge::ChannelTimestampFinder finder(logged_configuration(), |
| 165 | "log_reader", node); |
| 166 | |
| 167 | absl::btree_set<std::string_view> remote_nodes; |
| 168 | |
| 169 | for (const Channel *channel : *logged_configuration()->channels()) { |
| 170 | if (!configuration::ChannelIsSendableOnNode(channel, node)) { |
| 171 | continue; |
| 172 | } |
| 173 | if (!channel->has_destination_nodes()) { |
| 174 | continue; |
| 175 | } |
| 176 | for (const Connection *connection : *channel->destination_nodes()) { |
| 177 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 178 | node)) { |
| 179 | // Start by seeing if the split timestamp channels are being used for |
| 180 | // this message. If so, remap them. |
| 181 | const Channel *timestamp_channel = configuration::GetChannel( |
| 182 | logged_configuration(), |
| 183 | finder.SplitChannelName(channel, connection), |
| 184 | RemoteMessage::GetFullyQualifiedName(), "", node, true); |
| 185 | |
| 186 | if (timestamp_channel != nullptr) { |
| 187 | if (timestamp_channel->logger() != LoggerConfig::NOT_LOGGED) { |
| 188 | RemapLoggedChannel<RemoteMessage>( |
| 189 | timestamp_channel->name()->string_view(), node); |
| 190 | } |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | // Otherwise collect this one up as a node to look for a combined |
| 195 | // channel from. It is more efficient to compare nodes than channels. |
| 196 | remote_nodes.insert(connection->name()->string_view()); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 201 | std::vector<const Node *> timestamp_logger_nodes = |
| 202 | configuration::TimestampNodes(logged_configuration(), node); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 203 | for (const std::string_view remote_node : remote_nodes) { |
| 204 | const std::string channel = finder.CombinedChannelName(remote_node); |
| 205 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 206 | // See if the log file is an old log with MessageHeader channels in it, or |
| 207 | // a newer log with RemoteMessage. If we find an older log, rename the |
| 208 | // type too along with the name. |
| 209 | if (HasChannel<MessageHeader>(channel, node)) { |
| 210 | CHECK(!HasChannel<RemoteMessage>(channel, node)) |
| 211 | << ": Can't have both a MessageHeader and RemoteMessage remote " |
| 212 | "timestamp channel."; |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 213 | // In theory, we should check NOT_LOGGED like RemoteMessage and be more |
| 214 | // careful about updating the config, but there are fewer and fewer logs |
| 215 | // with MessageHeader remote messages, so it isn't worth the effort. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 216 | RemapLoggedChannel<MessageHeader>(channel, node, "/original", |
| 217 | "aos.message_bridge.RemoteMessage"); |
| 218 | } else { |
| 219 | CHECK(HasChannel<RemoteMessage>(channel, node)) |
| 220 | << ": Failed to find {\"name\": \"" << channel << "\", \"type\": \"" |
| 221 | << RemoteMessage::GetFullyQualifiedName() << "\"} for node " |
| 222 | << node->name()->string_view(); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 223 | // Only bother to remap if there's something on the channel. We can |
| 224 | // tell if the channel was marked NOT_LOGGED or not. This makes the |
| 225 | // config not change un-necesarily when we replay a log with NOT_LOGGED |
| 226 | // messages. |
| 227 | if (HasLoggedChannel<RemoteMessage>(channel, node)) { |
| 228 | RemapLoggedChannel<RemoteMessage>(channel, node); |
| 229 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 230 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 234 | if (replay_configuration) { |
| 235 | CHECK_EQ(configuration::MultiNode(configuration()), |
| 236 | configuration::MultiNode(replay_configuration)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 237 | << ": Log file and replay config need to both be multi or single " |
| 238 | "node."; |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 241 | if (!configuration::MultiNode(configuration())) { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 242 | states_.emplace_back(std::make_unique<State>( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 243 | std::make_unique<TimestampMapper>(FilterPartsForNode(log_files_, "")), |
| 244 | nullptr)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 245 | } else { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 246 | if (replay_configuration) { |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 247 | CHECK_EQ(logged_configuration()->nodes()->size(), |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 248 | replay_configuration->nodes()->size()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 249 | << ": Log file and replay config need to have matching nodes " |
| 250 | "lists."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 251 | for (const Node *node : *logged_configuration()->nodes()) { |
| 252 | if (configuration::GetNode(replay_configuration, node) == nullptr) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 253 | LOG(FATAL) << "Found node " << FlatbufferToJson(node) |
| 254 | << " in logged config that is not present in the replay " |
| 255 | "config."; |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 258 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 259 | states_.resize(configuration()->nodes()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 260 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 263 | LogReader::~LogReader() { |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 264 | if (event_loop_factory_unique_ptr_) { |
| 265 | Deregister(); |
| 266 | } else if (event_loop_factory_ != nullptr) { |
| 267 | LOG(FATAL) << "Must call Deregister before the SimulatedEventLoopFactory " |
| 268 | "is destroyed"; |
| 269 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 270 | // Zero out some buffers. It's easy to do use-after-frees on these, so make |
| 271 | // it more obvious. |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 272 | if (remapped_configuration_buffer_) { |
| 273 | remapped_configuration_buffer_->Wipe(); |
| 274 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 275 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 276 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 277 | const Configuration *LogReader::logged_configuration() const { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 278 | return log_files_[0].config.get(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 279 | } |
| 280 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 281 | const Configuration *LogReader::configuration() const { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 282 | return remapped_configuration_; |
| 283 | } |
| 284 | |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 285 | std::vector<const Node *> LogReader::LoggedNodes() const { |
| 286 | return configuration::GetNodes(logged_configuration()); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 287 | } |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 288 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 289 | monotonic_clock::time_point LogReader::monotonic_start_time( |
| 290 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 291 | State *state = |
| 292 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 293 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 294 | |
Austin Schuh | f665eb4 | 2022-02-03 18:26:25 -0800 | [diff] [blame^] | 295 | return state->monotonic_start_time(state->boot_count()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 298 | realtime_clock::time_point LogReader::realtime_start_time( |
| 299 | const Node *node) const { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 300 | State *state = |
| 301 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 302 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 303 | |
Austin Schuh | f665eb4 | 2022-02-03 18:26:25 -0800 | [diff] [blame^] | 304 | return state->realtime_start_time(state->boot_count()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 307 | void LogReader::OnStart(std::function<void()> fn) { |
| 308 | CHECK(!configuration::MultiNode(configuration())); |
| 309 | OnStart(nullptr, std::move(fn)); |
| 310 | } |
| 311 | |
| 312 | void LogReader::OnStart(const Node *node, std::function<void()> fn) { |
| 313 | const int node_index = configuration::GetNodeIndex(configuration(), node); |
| 314 | CHECK_GE(node_index, 0); |
| 315 | CHECK_LT(node_index, static_cast<int>(states_.size())); |
| 316 | State *state = states_[node_index].get(); |
| 317 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 318 | |
| 319 | state->OnStart(std::move(fn)); |
| 320 | } |
| 321 | |
| 322 | void LogReader::State::OnStart(std::function<void()> fn) { |
| 323 | on_starts_.emplace_back(std::move(fn)); |
| 324 | } |
| 325 | |
| 326 | void LogReader::State::RunOnStart() { |
| 327 | SetRealtimeOffset(monotonic_start_time(boot_count()), |
| 328 | realtime_start_time(boot_count())); |
| 329 | |
| 330 | VLOG(1) << "Starting " << MaybeNodeName(node()) << "at time " |
| 331 | << monotonic_start_time(boot_count()); |
| 332 | for (size_t i = 0; i < on_starts_.size(); ++i) { |
| 333 | on_starts_[i](); |
| 334 | } |
| 335 | stopped_ = false; |
| 336 | started_ = true; |
| 337 | } |
| 338 | |
| 339 | void LogReader::OnEnd(std::function<void()> fn) { |
| 340 | CHECK(!configuration::MultiNode(configuration())); |
| 341 | OnEnd(nullptr, std::move(fn)); |
| 342 | } |
| 343 | |
| 344 | void LogReader::OnEnd(const Node *node, std::function<void()> fn) { |
| 345 | const int node_index = configuration::GetNodeIndex(configuration(), node); |
| 346 | CHECK_GE(node_index, 0); |
| 347 | CHECK_LT(node_index, static_cast<int>(states_.size())); |
| 348 | State *state = states_[node_index].get(); |
| 349 | CHECK(state != nullptr) << ": Unknown node " << FlatbufferToJson(node); |
| 350 | |
| 351 | state->OnEnd(std::move(fn)); |
| 352 | } |
| 353 | |
| 354 | void LogReader::State::OnEnd(std::function<void()> fn) { |
| 355 | on_ends_.emplace_back(std::move(fn)); |
| 356 | } |
| 357 | |
| 358 | void LogReader::State::RunOnEnd() { |
| 359 | VLOG(1) << "Ending " << MaybeNodeName(node()) << "at time " |
| 360 | << monotonic_start_time(boot_count()); |
| 361 | for (size_t i = 0; i < on_ends_.size(); ++i) { |
| 362 | on_ends_[i](); |
| 363 | } |
| 364 | |
| 365 | stopped_ = true; |
| 366 | started_ = false; |
| 367 | } |
| 368 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 369 | void LogReader::Register() { |
| 370 | event_loop_factory_unique_ptr_ = |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 371 | std::make_unique<SimulatedEventLoopFactory>(configuration()); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 372 | Register(event_loop_factory_unique_ptr_.get()); |
| 373 | } |
| 374 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 375 | void LogReader::RegisterWithoutStarting( |
| 376 | SimulatedEventLoopFactory *event_loop_factory) { |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 377 | event_loop_factory_ = event_loop_factory; |
Austin Schuh | e5bbd9e | 2020-09-21 17:29:20 -0700 | [diff] [blame] | 378 | remapped_configuration_ = event_loop_factory_->configuration(); |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 379 | filters_ = |
| 380 | std::make_unique<message_bridge::MultiNodeNoncausalOffsetEstimator>( |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 381 | event_loop_factory_->configuration(), logged_configuration(), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 382 | log_files_[0].boots, FLAGS_skip_order_validation, |
Austin Schuh | fe3fb34 | 2021-01-16 18:50:37 -0800 | [diff] [blame] | 383 | chrono::duration_cast<chrono::nanoseconds>( |
| 384 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 385 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 386 | std::vector<TimestampMapper *> timestamp_mappers; |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 387 | for (const Node *node : configuration::GetNodes(configuration())) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 388 | const size_t node_index = |
| 389 | configuration::GetNodeIndex(configuration(), node); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 390 | std::vector<LogParts> filtered_parts = FilterPartsForNode( |
| 391 | log_files_, node != nullptr ? node->name()->string_view() : ""); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 392 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 393 | states_[node_index] = std::make_unique<State>( |
| 394 | filtered_parts.size() == 0u |
| 395 | ? nullptr |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 396 | : std::make_unique<TimestampMapper>(std::move(filtered_parts)), |
| 397 | node); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 398 | State *state = states_[node_index].get(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 399 | state->SetNodeEventLoopFactory( |
| 400 | event_loop_factory_->GetNodeEventLoopFactory(node)); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 401 | |
| 402 | state->SetChannelCount(logged_configuration()->channels()->size()); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 403 | timestamp_mappers.emplace_back(state->timestamp_mapper()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 404 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 405 | filters_->SetTimestampMappers(std::move(timestamp_mappers)); |
| 406 | |
| 407 | // Note: this needs to be set before any times are pulled, or we won't observe |
| 408 | // the timestamps. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 409 | event_loop_factory_->SetTimeConverter(filters_.get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 410 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 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 | for (const Node *other_node : configuration::GetNodes(configuration())) { |
| 416 | const size_t other_node_index = |
| 417 | configuration::GetNodeIndex(configuration(), other_node); |
| 418 | State *other_state = states_[other_node_index].get(); |
| 419 | if (other_state != state) { |
| 420 | state->AddPeer(other_state); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 425 | // Register after making all the State objects so we can build references |
| 426 | // between them. |
| 427 | for (const Node *node : configuration::GetNodes(configuration())) { |
| 428 | const size_t node_index = |
| 429 | configuration::GetNodeIndex(configuration(), node); |
| 430 | State *state = states_[node_index].get(); |
| 431 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 432 | // If we didn't find any log files with data in them, we won't ever get a |
| 433 | // callback or be live. So skip the rest of the setup. |
| 434 | if (state->OldestMessageTime() == BootTimestamp::max_time()) { |
| 435 | continue; |
| 436 | } |
| 437 | ++live_nodes_; |
| 438 | |
| 439 | NodeEventLoopFactory *node_factory = |
| 440 | event_loop_factory_->GetNodeEventLoopFactory(node); |
| 441 | node_factory->OnStartup([this, state, node]() { |
| 442 | RegisterDuringStartup(state->MakeEventLoop(), node); |
| 443 | }); |
| 444 | node_factory->OnShutdown([this, state, node]() { |
| 445 | RegisterDuringStartup(nullptr, node); |
| 446 | state->DestroyEventLoop(); |
| 447 | }); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 448 | } |
| 449 | |
James Kuszmaul | 46d8258 | 2020-05-09 19:50:09 -0700 | [diff] [blame] | 450 | if (live_nodes_ == 0) { |
| 451 | LOG(FATAL) |
| 452 | << "Don't have logs from any of the nodes in the replay config--are " |
| 453 | "you sure that the replay config matches the original config?"; |
| 454 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 455 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 456 | filters_->CheckGraph(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 457 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 458 | for (std::unique_ptr<State> &state : states_) { |
| 459 | state->SeedSortedMessages(); |
| 460 | } |
| 461 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 462 | // Forwarding is tracked per channel. If it is enabled, we want to turn it |
| 463 | // off. Otherwise messages replayed will get forwarded across to the other |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 464 | // nodes, and also replayed on the other nodes. This may not satisfy all |
| 465 | // our users, but it'll start the discussion. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 466 | if (configuration::MultiNode(event_loop_factory_->configuration())) { |
| 467 | for (size_t i = 0; i < logged_configuration()->channels()->size(); ++i) { |
| 468 | const Channel *channel = logged_configuration()->channels()->Get(i); |
| 469 | const Node *node = configuration::GetNode( |
| 470 | configuration(), channel->source_node()->string_view()); |
| 471 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 472 | State *state = |
| 473 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 474 | |
| 475 | const Channel *remapped_channel = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 476 | RemapChannel(state->event_loop(), node, channel); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 477 | |
| 478 | event_loop_factory_->DisableForwarding(remapped_channel); |
| 479 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 480 | |
| 481 | // If we are replaying a log, we don't want a bunch of redundant messages |
| 482 | // from both the real message bridge and simulated message bridge. |
| 483 | event_loop_factory_->DisableStatistics(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 484 | } |
Austin Schuh | 891214d | 2021-11-11 20:35:02 -0800 | [diff] [blame] | 485 | |
| 486 | // Write pseudo start times out to file now that we are all setup. |
| 487 | filters_->Start(event_loop_factory_); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void LogReader::Register(SimulatedEventLoopFactory *event_loop_factory) { |
| 491 | RegisterWithoutStarting(event_loop_factory); |
| 492 | // We want to start the log file at the last start time of the log files |
| 493 | // from all the nodes. Compute how long each node's simulation needs to run |
| 494 | // to move time to this point. |
| 495 | distributed_clock::time_point start_time = distributed_clock::min_time; |
| 496 | |
| 497 | // TODO(austin): We want an "OnStart" callback for each node rather than |
| 498 | // running until the last node. |
| 499 | |
| 500 | for (std::unique_ptr<State> &state : states_) { |
| 501 | VLOG(1) << "Start time is " << state->monotonic_start_time(0) |
| 502 | << " for node " << MaybeNodeName(state->node()) << "now " |
| 503 | << state->monotonic_now(); |
| 504 | if (state->monotonic_start_time(0) == monotonic_clock::min_time) { |
| 505 | continue; |
| 506 | } |
| 507 | // And start computing the start time on the distributed clock now that |
| 508 | // that works. |
| 509 | start_time = std::max( |
| 510 | start_time, state->ToDistributedClock(state->monotonic_start_time(0))); |
| 511 | } |
| 512 | |
| 513 | // TODO(austin): If a node doesn't have a start time, we might not queue |
| 514 | // enough. If this happens, we'll explode with a frozen error eventually. |
| 515 | |
| 516 | CHECK_GE(start_time, distributed_clock::epoch()) |
| 517 | << ": Hmm, we have a node starting before the start of time. Offset " |
| 518 | "everything."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 519 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 520 | // While we are starting the system up, we might be relying on matching data |
| 521 | // to timestamps on log files where the timestamp log file starts before the |
| 522 | // data. In this case, it is reasonable to expect missing data. |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 523 | { |
| 524 | const bool prior_ignore_missing_data = ignore_missing_data_; |
| 525 | ignore_missing_data_ = true; |
| 526 | VLOG(1) << "Running until " << start_time << " in Register"; |
| 527 | event_loop_factory_->RunFor(start_time.time_since_epoch()); |
| 528 | VLOG(1) << "At start time"; |
| 529 | // Now that we are running for real, missing data means that the log file is |
| 530 | // corrupted or went wrong. |
| 531 | ignore_missing_data_ = prior_ignore_missing_data; |
| 532 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 533 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 534 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 535 | // Make the RT clock be correct before handing it to the user. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 536 | if (state->realtime_start_time(0) != realtime_clock::min_time) { |
| 537 | state->SetRealtimeOffset(state->monotonic_start_time(0), |
| 538 | state->realtime_start_time(0)); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 539 | } |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 540 | VLOG(1) << "Start time is " << state->monotonic_start_time(0) |
| 541 | << " for node " << MaybeNodeName(state->event_loop()->node()) |
| 542 | << "now " << state->monotonic_now(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | if (FLAGS_timestamps_to_csv) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 546 | filters_->Start(event_loop_factory); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 550 | message_bridge::NoncausalOffsetEstimator *LogReader::GetFilter( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 551 | const Node *node_a, const Node *node_b) { |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 552 | if (filters_) { |
| 553 | return filters_->GetFilter(node_a, node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 554 | } |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 555 | return nullptr; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 558 | void LogReader::Register(EventLoop *event_loop) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 559 | Register(event_loop, event_loop->node()); |
| 560 | } |
| 561 | |
| 562 | void LogReader::Register(EventLoop *event_loop, const Node *node) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 563 | State *state = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 564 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
| 565 | |
| 566 | // If we didn't find any log files with data in them, we won't ever get a |
| 567 | // callback or be live. So skip the rest of the setup. |
| 568 | if (state->OldestMessageTime() == BootTimestamp::max_time()) { |
| 569 | return; |
| 570 | } |
| 571 | ++live_nodes_; |
| 572 | |
| 573 | if (event_loop_factory_ != nullptr) { |
| 574 | event_loop_factory_->GetNodeEventLoopFactory(node)->OnStartup( |
| 575 | [this, event_loop, node]() { |
| 576 | RegisterDuringStartup(event_loop, node); |
| 577 | }); |
| 578 | } else { |
| 579 | RegisterDuringStartup(event_loop, node); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void LogReader::RegisterDuringStartup(EventLoop *event_loop, const Node *node) { |
| 584 | if (event_loop) { |
| 585 | CHECK(event_loop->configuration() == configuration()); |
| 586 | } |
| 587 | |
| 588 | State *state = |
| 589 | states_[configuration::GetNodeIndex(configuration(), node)].get(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 590 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 591 | state->set_event_loop(event_loop); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 592 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 593 | // We don't run timing reports when trying to print out logged data, because |
| 594 | // otherwise we would end up printing out the timing reports themselves... |
| 595 | // This is only really relevant when we are replaying into a simulation. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 596 | if (event_loop) { |
| 597 | event_loop->SkipTimingReport(); |
| 598 | event_loop->SkipAosLog(); |
| 599 | } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 600 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 601 | for (size_t logged_channel_index = 0; |
| 602 | logged_channel_index < logged_configuration()->channels()->size(); |
| 603 | ++logged_channel_index) { |
| 604 | const Channel *channel = RemapChannel( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 605 | event_loop, node, |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 606 | logged_configuration()->channels()->Get(logged_channel_index)); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 607 | |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 608 | const bool logged = channel->logger() != LoggerConfig::NOT_LOGGED; |
Austin Schuh | 532656d | 2021-01-11 10:17:18 -0800 | [diff] [blame] | 609 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 610 | message_bridge::NoncausalOffsetEstimator *filter = nullptr; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 611 | |
| 612 | State *source_state = nullptr; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 613 | if (!configuration::ChannelIsSendableOnNode(channel, node) && |
| 614 | configuration::ChannelIsReadableOnNode(channel, node)) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 615 | const Node *source_node = configuration::GetNode( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 616 | configuration(), channel->source_node()->string_view()); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 617 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 618 | // We've got a message which is being forwarded to this node. |
| 619 | filter = GetFilter(node, source_node); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 620 | |
| 621 | source_state = |
| 622 | states_[configuration::GetNodeIndex(configuration(), source_node)] |
| 623 | .get(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 624 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 625 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 626 | // We are the source, and it is forwarded. |
| 627 | const bool is_forwarded = |
| 628 | configuration::ChannelIsSendableOnNode(channel, node) && |
| 629 | configuration::ConnectionCount(channel); |
| 630 | |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 631 | state->SetChannel( |
| 632 | logged_channel_index, |
| 633 | configuration::ChannelIndex(configuration(), channel), |
| 634 | event_loop && logged ? event_loop->MakeRawSender(channel) : nullptr, |
| 635 | filter, is_forwarded, source_state); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 636 | |
Austin Schuh | c0b6c4f | 2021-10-11 18:28:38 -0700 | [diff] [blame] | 637 | if (is_forwarded && logged) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 638 | const Node *source_node = configuration::GetNode( |
| 639 | configuration(), channel->source_node()->string_view()); |
| 640 | |
| 641 | for (const Connection *connection : *channel->destination_nodes()) { |
| 642 | const bool delivery_time_is_logged = |
| 643 | configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 644 | source_node); |
| 645 | |
| 646 | if (delivery_time_is_logged) { |
| 647 | State *destination_state = |
| 648 | states_[configuration::GetNodeIndex( |
| 649 | configuration(), connection->name()->string_view())] |
| 650 | .get(); |
| 651 | destination_state->SetRemoteTimestampSender( |
| 652 | logged_channel_index, |
| 653 | event_loop ? state->RemoteTimestampSender(channel, connection) |
| 654 | : nullptr); |
| 655 | } |
| 656 | } |
| 657 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 660 | if (!event_loop) { |
| 661 | state->ClearRemoteTimestampSenders(); |
| 662 | state->set_timer_handler(nullptr); |
| 663 | state->set_startup_timer(nullptr); |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 664 | return; |
| 665 | } |
| 666 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 667 | state->set_timer_handler(event_loop->AddTimer([this, state]() { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 668 | VLOG(1) << "Starting sending " << MaybeNodeName(state->event_loop()->node()) |
| 669 | << "at " << state->event_loop()->context().monotonic_event_time |
| 670 | << " now " << state->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 671 | if (state->OldestMessageTime() == BootTimestamp::max_time()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 672 | --live_nodes_; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 673 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Node down!"; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 674 | if (exit_on_finish_ && live_nodes_ == 0) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 675 | event_loop_factory_->Exit(); |
| 676 | } |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 677 | return; |
| 678 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 679 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 680 | TimestampedMessage timestamped_message = state->PopOldest(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 681 | |
| 682 | CHECK_EQ(timestamped_message.monotonic_event_time.boot, |
| 683 | state->boot_count()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 684 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 685 | const monotonic_clock::time_point monotonic_now = |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 686 | state->event_loop()->context().monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 687 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 688 | CHECK(monotonic_now == timestamped_message.monotonic_event_time.time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 689 | << ": " << FlatbufferToJson(state->event_loop()->node()) << " Now " |
| 690 | << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 691 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 692 | << state->DebugString(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 693 | } else if (BootTimestamp{.boot = state->boot_count(), |
| 694 | .time = monotonic_now} != |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 695 | timestamped_message.monotonic_event_time) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 696 | LOG(WARNING) << "Check failed: monotonic_now == " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 697 | "timestamped_message.monotonic_event_time) (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 698 | << monotonic_now << " vs. " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 699 | << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 700 | << "): " << FlatbufferToJson(state->event_loop()->node()) |
| 701 | << " Now " << monotonic_now << " trying to send " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 702 | << timestamped_message.monotonic_event_time << " failure " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 703 | << state->DebugString(); |
| 704 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 705 | |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 706 | if (timestamped_message.monotonic_event_time.time > |
| 707 | state->monotonic_start_time( |
| 708 | timestamped_message.monotonic_event_time.boot) || |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 709 | event_loop_factory_ != nullptr) { |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 710 | if (timestamped_message.data != nullptr && !state->found_last_message()) { |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 711 | if (timestamped_message.monotonic_remote_time != |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 712 | BootTimestamp::min_time()) { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 713 | // Confirm that the message was sent on the sending node before the |
| 714 | // destination node (this node). As a proxy, do this by making sure |
| 715 | // that time on the source node is past when the message was sent. |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 716 | // |
| 717 | // TODO(austin): <= means that the cause message (which we know) could |
| 718 | // happen after the effect even though we know they are at the same |
| 719 | // time. I doubt anyone will notice for a bit, but we should really |
| 720 | // fix that. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 721 | BootTimestamp monotonic_remote_now = |
| 722 | state->monotonic_remote_now(timestamped_message.channel_index); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 723 | if (!FLAGS_skip_order_validation) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 724 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
Austin Schuh | 3e20c69 | 2021-11-16 20:43:16 -0800 | [diff] [blame] | 725 | monotonic_remote_now.boot) |
| 726 | << state->event_loop()->node()->name()->string_view() << " to " |
| 727 | << state->remote_node(timestamped_message.channel_index) |
| 728 | ->name() |
| 729 | ->string_view() |
| 730 | << " while trying to send a message on " |
| 731 | << configuration::CleanedChannelToString( |
| 732 | logged_configuration()->channels()->Get( |
| 733 | timestamped_message.channel_index)) |
| 734 | << " " << timestamped_message << " " << state->DebugString(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 735 | CHECK_LE(timestamped_message.monotonic_remote_time, |
| 736 | monotonic_remote_now) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 737 | << state->event_loop()->node()->name()->string_view() << " to " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 738 | << state->remote_node(timestamped_message.channel_index) |
| 739 | ->name() |
| 740 | ->string_view() |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 741 | << " while trying to send a message on " |
| 742 | << configuration::CleanedChannelToString( |
| 743 | logged_configuration()->channels()->Get( |
| 744 | timestamped_message.channel_index)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 745 | << " " << state->DebugString(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 746 | } else if (monotonic_remote_now.boot != |
| 747 | timestamped_message.monotonic_remote_time.boot) { |
| 748 | LOG(WARNING) << "Missmatched boots, " << monotonic_remote_now.boot |
| 749 | << " vs " |
| 750 | << timestamped_message.monotonic_remote_time.boot; |
| 751 | } else if (timestamped_message.monotonic_remote_time > |
| 752 | monotonic_remote_now) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 753 | LOG(WARNING) |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 754 | << "Check failed: timestamped_message.monotonic_remote_time < " |
| 755 | "state->monotonic_remote_now(timestamped_message.channel_" |
| 756 | "index) (" |
| 757 | << timestamped_message.monotonic_remote_time << " vs. " |
| 758 | << state->monotonic_remote_now( |
| 759 | timestamped_message.channel_index) |
| 760 | << ") " << state->event_loop()->node()->name()->string_view() |
| 761 | << " to " |
| 762 | << state->remote_node(timestamped_message.channel_index) |
| 763 | ->name() |
| 764 | ->string_view() |
| 765 | << " currently " << timestamped_message.monotonic_event_time |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 766 | << " (" |
| 767 | << state->ToDistributedClock( |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 768 | timestamped_message.monotonic_event_time.time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 769 | << ") remote event time " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 770 | << timestamped_message.monotonic_remote_time << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 771 | << state->RemoteToDistributedClock( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 772 | timestamped_message.channel_index, |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 773 | timestamped_message.monotonic_remote_time.time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 774 | << ") " << state->DebugString(); |
| 775 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 778 | // If we have access to the factory, use it to fix the realtime time. |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 779 | state->SetRealtimeOffset(timestamped_message.monotonic_event_time.time, |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 780 | timestamped_message.realtime_event_time); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 781 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 782 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Sending " |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 783 | << timestamped_message.monotonic_event_time; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 784 | // TODO(austin): std::move channel_data in and make that efficient in |
| 785 | // simulation. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 786 | state->Send(std::move(timestamped_message)); |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 787 | } else if (state->found_last_message() || |
| 788 | (!ignore_missing_data_ && |
| 789 | // When starting up, we can have data which was sent before |
| 790 | // the log starts, but the timestamp was after the log |
| 791 | // starts. This is unreasonable to avoid, so ignore the |
| 792 | // missing data. |
| 793 | timestamped_message.monotonic_remote_time.time >= |
| 794 | state->monotonic_remote_start_time( |
| 795 | timestamped_message.monotonic_remote_time.boot, |
| 796 | timestamped_message.channel_index) && |
| 797 | !FLAGS_skip_missing_forwarding_entries)) { |
| 798 | if (!state->found_last_message()) { |
| 799 | // We've found a timestamp without data that we expect to have data |
| 800 | // for. This likely means that we are at the end of the log file. |
| 801 | // Record it and CHECK that in the rest of the log file, we don't find |
| 802 | // any more data on that channel. Not all channels will end at the |
| 803 | // same point in time since they can be in different files. |
| 804 | VLOG(1) << "Found the last message on channel " |
| 805 | << timestamped_message.channel_index << ", " |
| 806 | << configuration::CleanedChannelToString( |
| 807 | logged_configuration()->channels()->Get( |
| 808 | timestamped_message.channel_index)) |
| 809 | << " on node " << MaybeNodeName(state->event_loop()->node()) |
| 810 | << timestamped_message; |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 811 | |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 812 | // The user might be working with log files from 1 node but forgot to |
| 813 | // configure the infrastructure to log data for a remote channel on |
| 814 | // that node. That can be very hard to debug, even though the log |
| 815 | // reader is doing the right thing. At least log a warning in that |
| 816 | // case and tell the user what is happening so they can either update |
| 817 | // their config to log the channel or can find a log with the data. |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 818 | const std::vector<std::string> logger_nodes = |
| 819 | FindLoggerNodes(log_files_); |
| 820 | if (logger_nodes.size()) { |
| 821 | // We have old logs which don't have the logger nodes logged. In |
| 822 | // that case, we can't be helpful :( |
| 823 | bool data_logged = false; |
| 824 | const Channel *channel = logged_configuration()->channels()->Get( |
| 825 | timestamped_message.channel_index); |
| 826 | for (const std::string &node : logger_nodes) { |
| 827 | data_logged |= |
| 828 | configuration::ChannelMessageIsLoggedOnNode(channel, node); |
| 829 | } |
| 830 | if (!data_logged) { |
| 831 | LOG(WARNING) << "Got a timestamp without any logfiles which " |
| 832 | "could contain data for channel " |
| 833 | << configuration::CleanedChannelToString(channel); |
| 834 | LOG(WARNING) << "Only have logs logged on [" |
| 835 | << absl::StrJoin(logger_nodes, ", ") << "]"; |
| 836 | LOG(WARNING) |
| 837 | << "Dropping the rest of the data on " |
| 838 | << state->event_loop()->node()->name()->string_view(); |
| 839 | LOG(WARNING) |
| 840 | << "Consider using --skip_missing_forwarding_entries to " |
| 841 | "bypass this, update your config to log it, or add data " |
| 842 | "from one of the nodes it is logged on."; |
| 843 | } |
| 844 | } |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 845 | // Now that we found the end of one channel, artificially stop the |
| 846 | // rest by setting the found_last_message bit. It is confusing when |
| 847 | // part of your data gets replayed but not all. The rest of them will |
| 848 | // get dropped as they are replayed to keep memory usage down. |
| 849 | state->SetFoundLastMessage(true); |
| 850 | |
| 851 | // Vector storing if we've seen a nullptr message or not per channel. |
| 852 | state->set_last_message(timestamped_message.channel_index); |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Austin Schuh | bd5f74a | 2021-11-11 20:55:38 -0800 | [diff] [blame] | 855 | // Make sure that once we have seen the last message on a channel, |
| 856 | // data doesn't start back up again. If the user wants to play |
| 857 | // through events like this, they can set |
| 858 | // --skip_missing_forwarding_entries or ignore_missing_data_. |
| 859 | if (timestamped_message.data == nullptr) { |
| 860 | state->set_last_message(timestamped_message.channel_index); |
| 861 | } else { |
| 862 | if (state->last_message(timestamped_message.channel_index)) { |
| 863 | LOG(FATAL) << "Found missing data in the middle of the log file on " |
| 864 | "channel " |
| 865 | << timestamped_message.channel_index << " " |
| 866 | << configuration::StrippedChannelToString( |
| 867 | logged_configuration()->channels()->Get( |
| 868 | timestamped_message.channel_index)) |
| 869 | << " " << timestamped_message << " " |
| 870 | << state->DebugString(); |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 871 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 872 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 873 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 874 | } else { |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 875 | LOG(WARNING) << "Not sending data from before the start of the log file. " |
| 876 | << timestamped_message.monotonic_event_time.time |
| 877 | .time_since_epoch() |
| 878 | .count() |
| 879 | << " start " |
| 880 | << monotonic_start_time().time_since_epoch().count() << " " |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 881 | << *timestamped_message.data; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 884 | const BootTimestamp next_time = state->OldestMessageTime(); |
| 885 | if (next_time != BootTimestamp::max_time()) { |
| 886 | if (next_time.boot != state->boot_count()) { |
| 887 | VLOG(1) << "Next message for " |
| 888 | << MaybeNodeName(state->event_loop()->node()) |
| 889 | << "is on the next boot, " << next_time << " now is " |
| 890 | << state->monotonic_now(); |
| 891 | CHECK(event_loop_factory_); |
| 892 | state->RunOnEnd(); |
| 893 | return; |
| 894 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 895 | VLOG(1) << "Scheduling " << MaybeNodeName(state->event_loop()->node()) |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 896 | << "wakeup for " << next_time.time << "(" |
| 897 | << state->ToDistributedClock(next_time.time) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 898 | << " distributed), now is " << state->monotonic_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 899 | state->Setup(next_time.time); |
James Kuszmaul | 314f167 | 2020-01-03 20:02:08 -0800 | [diff] [blame] | 900 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 901 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) |
| 902 | << "No next message, scheduling shutdown"; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 903 | state->RunOnEnd(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 904 | // Set a timer up immediately after now to die. If we don't do this, |
| 905 | // then the senders waiting on the message we just read will never get |
| 906 | // called. |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 907 | if (event_loop_factory_ != nullptr) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 908 | state->Setup(monotonic_now + event_loop_factory_->send_delay() + |
| 909 | std::chrono::nanoseconds(1)); |
Austin Schuh | eecb928 | 2020-01-08 17:43:30 -0800 | [diff] [blame] | 910 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 911 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 912 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 913 | VLOG(1) << MaybeNodeName(state->event_loop()->node()) << "Done sending at " |
| 914 | << state->event_loop()->context().monotonic_event_time << " now " |
| 915 | << state->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 916 | })); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 917 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 918 | if (state->OldestMessageTime() != BootTimestamp::max_time()) { |
| 919 | state->set_startup_timer( |
| 920 | event_loop->AddTimer([state]() { state->RunOnStart(); })); |
| 921 | event_loop->OnRun([state]() { |
| 922 | BootTimestamp next_time = state->OldestMessageTime(); |
| 923 | CHECK_EQ(next_time.boot, state->boot_count()); |
| 924 | state->Setup(next_time.time); |
| 925 | state->SetupStartupTimer(); |
| 926 | }); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
| 930 | void LogReader::Deregister() { |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 931 | // Make sure that things get destroyed in the correct order, rather than |
| 932 | // relying on getting the order correct in the class definition. |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 933 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 934 | state->Deregister(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 935 | } |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 936 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 937 | event_loop_factory_unique_ptr_.reset(); |
| 938 | event_loop_factory_ = nullptr; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 939 | } |
| 940 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 941 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 942 | std::string_view add_prefix, |
| 943 | std::string_view new_type) { |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 944 | for (size_t ii = 0; ii < logged_configuration()->channels()->size(); ++ii) { |
| 945 | const Channel *const channel = logged_configuration()->channels()->Get(ii); |
| 946 | if (channel->name()->str() == name && |
| 947 | channel->type()->string_view() == type) { |
| 948 | CHECK_EQ(0u, remapped_channels_.count(ii)) |
| 949 | << "Already remapped channel " |
| 950 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 951 | RemappedChannel remapped_channel; |
| 952 | remapped_channel.remapped_name = |
| 953 | std::string(add_prefix) + std::string(name); |
| 954 | remapped_channel.new_type = new_type; |
| 955 | remapped_channels_[ii] = std::move(remapped_channel); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 956 | VLOG(1) << "Remapping channel " |
| 957 | << configuration::CleanedChannelToString(channel) |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 958 | << " to have name " << remapped_channels_[ii].remapped_name; |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 959 | MakeRemappedConfig(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 960 | return; |
| 961 | } |
| 962 | } |
| 963 | LOG(FATAL) << "Unabled to locate channel with name " << name << " and type " |
| 964 | << type; |
| 965 | } |
| 966 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 967 | void LogReader::RemapLoggedChannel(std::string_view name, std::string_view type, |
| 968 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 969 | std::string_view add_prefix, |
| 970 | std::string_view new_type) { |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 971 | VLOG(1) << "Node is " << aos::FlatbufferToJson(node); |
| 972 | const Channel *remapped_channel = |
| 973 | configuration::GetChannel(logged_configuration(), name, type, "", node); |
| 974 | CHECK(remapped_channel != nullptr) << ": Failed to find {\"name\": \"" << name |
| 975 | << "\", \"type\": \"" << type << "\"}"; |
| 976 | VLOG(1) << "Original {\"name\": \"" << name << "\", \"type\": \"" << type |
| 977 | << "\"}"; |
| 978 | VLOG(1) << "Remapped " |
| 979 | << aos::configuration::StrippedChannelToString(remapped_channel); |
| 980 | |
| 981 | // We want to make /spray on node 0 go to /0/spray by snooping the maps. And |
| 982 | // we want it to degrade if the heuristics fail to just work. |
| 983 | // |
| 984 | // The easiest way to do this is going to be incredibly specific and verbose. |
| 985 | // Look up /spray, to /0/spray. Then, prefix the result with /original to get |
| 986 | // /original/0/spray. Then, create a map from /original/spray to |
| 987 | // /original/0/spray for just the type we were asked for. |
| 988 | if (name != remapped_channel->name()->string_view()) { |
| 989 | MapT new_map; |
| 990 | new_map.match = std::make_unique<ChannelT>(); |
| 991 | new_map.match->name = absl::StrCat(add_prefix, name); |
| 992 | new_map.match->type = type; |
| 993 | if (node != nullptr) { |
| 994 | new_map.match->source_node = node->name()->str(); |
| 995 | } |
| 996 | new_map.rename = std::make_unique<ChannelT>(); |
| 997 | new_map.rename->name = |
| 998 | absl::StrCat(add_prefix, remapped_channel->name()->string_view()); |
| 999 | maps_.emplace_back(std::move(new_map)); |
| 1000 | } |
| 1001 | |
| 1002 | const size_t channel_index = |
| 1003 | configuration::ChannelIndex(logged_configuration(), remapped_channel); |
| 1004 | CHECK_EQ(0u, remapped_channels_.count(channel_index)) |
| 1005 | << "Already remapped channel " |
| 1006 | << configuration::CleanedChannelToString(remapped_channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1007 | |
| 1008 | RemappedChannel remapped_channel_struct; |
| 1009 | remapped_channel_struct.remapped_name = |
| 1010 | std::string(add_prefix) + |
| 1011 | std::string(remapped_channel->name()->string_view()); |
| 1012 | remapped_channel_struct.new_type = new_type; |
| 1013 | remapped_channels_[channel_index] = std::move(remapped_channel_struct); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1014 | MakeRemappedConfig(); |
| 1015 | } |
| 1016 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1017 | void LogReader::MakeRemappedConfig() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1018 | for (std::unique_ptr<State> &state : states_) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1019 | if (state) { |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1020 | CHECK(!state->event_loop()) |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 1021 | << ": Can't change the mapping after the events are scheduled."; |
| 1022 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1023 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1024 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1025 | // If no remapping occurred and we are using the original config, then there |
| 1026 | // is nothing interesting to do here. |
| 1027 | if (remapped_channels_.empty() && replay_configuration_ == nullptr) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1028 | remapped_configuration_ = logged_configuration(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | // Config to copy Channel definitions from. Use the specified |
| 1032 | // replay_configuration_ if it has been provided. |
| 1033 | const Configuration *const base_config = replay_configuration_ == nullptr |
| 1034 | ? logged_configuration() |
| 1035 | : replay_configuration_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1036 | |
| 1037 | // Create a config with all the channels, but un-sorted/merged. Collect up |
| 1038 | // the schemas while we do this. Call MergeConfiguration to sort everything, |
| 1039 | // and then merge it all in together. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1040 | |
| 1041 | // This is the builder that we use for the config containing all the new |
| 1042 | // channels. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1043 | flatbuffers::FlatBufferBuilder fbb; |
| 1044 | fbb.ForceDefaults(true); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1045 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1046 | |
| 1047 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u) |
| 1048 | << ": Merging logic needs to be updated when the number of channel " |
| 1049 | "fields changes."; |
| 1050 | |
| 1051 | // List of schemas. |
| 1052 | std::map<std::string_view, FlatbufferVector<reflection::Schema>> schema_map; |
| 1053 | // Make sure our new RemoteMessage schema is in there for old logs without it. |
| 1054 | schema_map.insert(std::make_pair( |
| 1055 | RemoteMessage::GetFullyQualifiedName(), |
| 1056 | FlatbufferVector<reflection::Schema>(FlatbufferSpan<reflection::Schema>( |
| 1057 | message_bridge::RemoteMessageSchema())))); |
| 1058 | |
| 1059 | // Reconstruct the remapped channels. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1060 | for (auto &pair : remapped_channels_) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1061 | const Channel *const c = CHECK_NOTNULL(configuration::GetChannel( |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1062 | base_config, logged_configuration()->channels()->Get(pair.first), "", |
| 1063 | nullptr)); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1064 | channel_offsets.emplace_back( |
| 1065 | CopyChannel(c, pair.second.remapped_name, "", &fbb)); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 1066 | |
| 1067 | if (c->has_destination_nodes()) { |
| 1068 | for (const Connection *connection : *c->destination_nodes()) { |
| 1069 | switch (connection->timestamp_logger()) { |
| 1070 | case LoggerConfig::LOCAL_LOGGER: |
| 1071 | case LoggerConfig::NOT_LOGGED: |
| 1072 | // There is no timestamp channel associated with this, so ignore it. |
| 1073 | break; |
| 1074 | |
| 1075 | case LoggerConfig::REMOTE_LOGGER: |
| 1076 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 1077 | // We want to make a split timestamp channel regardless of what type |
| 1078 | // of log this used to be. No sense propagating the single |
| 1079 | // timestamp channel. |
| 1080 | |
| 1081 | CHECK(connection->has_timestamp_logger_nodes()); |
| 1082 | for (const flatbuffers::String *timestamp_logger_node : |
| 1083 | *connection->timestamp_logger_nodes()) { |
| 1084 | const Node *node = configuration::GetNode( |
| 1085 | logged_configuration(), timestamp_logger_node->string_view()); |
| 1086 | message_bridge::ChannelTimestampFinder finder( |
| 1087 | logged_configuration(), "log_reader", node); |
| 1088 | |
| 1089 | // We are assuming here that all the maps are setup correctly to |
| 1090 | // handle arbitrary timestamps. Apply the maps for this node to |
| 1091 | // see what name this ends up with. |
| 1092 | std::string name = finder.SplitChannelName( |
| 1093 | pair.second.remapped_name, c->type()->str(), connection); |
| 1094 | std::string unmapped_name = name; |
| 1095 | configuration::HandleMaps(logged_configuration()->maps(), &name, |
| 1096 | "aos.message_bridge.RemoteMessage", |
| 1097 | node); |
| 1098 | CHECK_NE(name, unmapped_name) |
| 1099 | << ": Remote timestamp channel was not remapped, this is " |
| 1100 | "very fishy"; |
| 1101 | flatbuffers::Offset<flatbuffers::String> channel_name_offset = |
| 1102 | fbb.CreateString(name); |
| 1103 | flatbuffers::Offset<flatbuffers::String> channel_type_offset = |
| 1104 | fbb.CreateString("aos.message_bridge.RemoteMessage"); |
| 1105 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
| 1106 | fbb.CreateString(timestamp_logger_node->string_view()); |
| 1107 | |
| 1108 | // Now, build a channel. Don't log it, 2 senders, and match the |
| 1109 | // source frequency. |
| 1110 | Channel::Builder channel_builder(fbb); |
| 1111 | channel_builder.add_name(channel_name_offset); |
| 1112 | channel_builder.add_type(channel_type_offset); |
| 1113 | channel_builder.add_source_node(source_node_offset); |
| 1114 | channel_builder.add_logger(LoggerConfig::NOT_LOGGED); |
| 1115 | channel_builder.add_num_senders(2); |
| 1116 | if (c->has_frequency()) { |
| 1117 | channel_builder.add_frequency(c->frequency()); |
| 1118 | } |
| 1119 | channel_offsets.emplace_back(channel_builder.Finish()); |
| 1120 | } |
| 1121 | break; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1125 | } |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1126 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1127 | // Now reconstruct the original channels, translating types as needed |
| 1128 | for (const Channel *c : *base_config->channels()) { |
| 1129 | // Search for a mapping channel. |
| 1130 | std::string_view new_type = ""; |
| 1131 | for (auto &pair : remapped_channels_) { |
| 1132 | const Channel *const remapped_channel = |
| 1133 | logged_configuration()->channels()->Get(pair.first); |
| 1134 | if (remapped_channel->name()->string_view() == c->name()->string_view() && |
| 1135 | remapped_channel->type()->string_view() == c->type()->string_view()) { |
| 1136 | new_type = pair.second.new_type; |
| 1137 | break; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | // Copy everything over. |
| 1142 | channel_offsets.emplace_back(CopyChannel(c, "", new_type, &fbb)); |
| 1143 | |
| 1144 | // Add the schema if it doesn't exist. |
| 1145 | if (schema_map.find(c->type()->string_view()) == schema_map.end()) { |
| 1146 | CHECK(c->has_schema()); |
| 1147 | schema_map.insert(std::make_pair(c->type()->string_view(), |
| 1148 | RecursiveCopyFlatBuffer(c->schema()))); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | // The MergeConfiguration API takes a vector, not a map. Convert. |
| 1153 | std::vector<FlatbufferVector<reflection::Schema>> schemas; |
| 1154 | while (!schema_map.empty()) { |
| 1155 | schemas.emplace_back(std::move(schema_map.begin()->second)); |
| 1156 | schema_map.erase(schema_map.begin()); |
| 1157 | } |
| 1158 | |
| 1159 | // Create the Configuration containing the new channels that we want to add. |
| 1160 | const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 1161 | channels_offset = |
| 1162 | channel_offsets.empty() ? 0 : fbb.CreateVector(channel_offsets); |
| 1163 | |
| 1164 | // Copy over the old maps. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1165 | std::vector<flatbuffers::Offset<Map>> map_offsets; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1166 | if (base_config->maps()) { |
| 1167 | for (const Map *map : *base_config->maps()) { |
| 1168 | map_offsets.emplace_back(aos::RecursiveCopyFlatBuffer(map, &fbb)); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | // Now create the new maps. These are second so they take effect first. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1173 | for (const MapT &map : maps_) { |
| 1174 | const flatbuffers::Offset<flatbuffers::String> match_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1175 | fbb.CreateString(map.match->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1176 | const flatbuffers::Offset<flatbuffers::String> match_type_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1177 | fbb.CreateString(map.match->type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1178 | const flatbuffers::Offset<flatbuffers::String> rename_name_offset = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1179 | fbb.CreateString(map.rename->name); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1180 | flatbuffers::Offset<flatbuffers::String> match_source_node_offset; |
| 1181 | if (!map.match->source_node.empty()) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1182 | match_source_node_offset = fbb.CreateString(map.match->source_node); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1183 | } |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1184 | Channel::Builder match_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1185 | match_builder.add_name(match_name_offset); |
| 1186 | match_builder.add_type(match_type_offset); |
| 1187 | if (!map.match->source_node.empty()) { |
| 1188 | match_builder.add_source_node(match_source_node_offset); |
| 1189 | } |
| 1190 | const flatbuffers::Offset<Channel> match_offset = match_builder.Finish(); |
| 1191 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1192 | Channel::Builder rename_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1193 | rename_builder.add_name(rename_name_offset); |
| 1194 | const flatbuffers::Offset<Channel> rename_offset = rename_builder.Finish(); |
| 1195 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1196 | Map::Builder map_builder(fbb); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1197 | map_builder.add_match(match_offset); |
| 1198 | map_builder.add_rename(rename_offset); |
| 1199 | map_offsets.emplace_back(map_builder.Finish()); |
| 1200 | } |
| 1201 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1202 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 1203 | maps_offsets = map_offsets.empty() ? 0 : fbb.CreateVector(map_offsets); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 1204 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1205 | // And copy everything else over. |
| 1206 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 1207 | nodes_offset = aos::RecursiveCopyVectorTable(base_config->nodes(), &fbb); |
| 1208 | |
| 1209 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 1210 | applications_offset = |
| 1211 | aos::RecursiveCopyVectorTable(base_config->applications(), &fbb); |
| 1212 | |
| 1213 | // Now insert everything else in unmodified. |
| 1214 | ConfigurationBuilder configuration_builder(fbb); |
| 1215 | if (!channels_offset.IsNull()) { |
| 1216 | configuration_builder.add_channels(channels_offset); |
| 1217 | } |
| 1218 | if (!maps_offsets.IsNull()) { |
| 1219 | configuration_builder.add_maps(maps_offsets); |
| 1220 | } |
| 1221 | if (!nodes_offset.IsNull()) { |
| 1222 | configuration_builder.add_nodes(nodes_offset); |
| 1223 | } |
| 1224 | if (!applications_offset.IsNull()) { |
| 1225 | configuration_builder.add_applications(applications_offset); |
| 1226 | } |
| 1227 | |
| 1228 | if (base_config->has_channel_storage_duration()) { |
| 1229 | configuration_builder.add_channel_storage_duration( |
| 1230 | base_config->channel_storage_duration()); |
| 1231 | } |
| 1232 | |
| 1233 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 1234 | << ": Merging logic needs to be updated when the number of configuration " |
| 1235 | "fields changes."; |
| 1236 | |
| 1237 | fbb.Finish(configuration_builder.Finish()); |
| 1238 | |
| 1239 | // Clean it up and return it! By using MergeConfiguration here, we'll |
| 1240 | // actually get a deduplicated config for free too. |
| 1241 | FlatbufferDetachedBuffer<Configuration> new_merged_config = |
| 1242 | configuration::MergeConfiguration( |
| 1243 | FlatbufferDetachedBuffer<Configuration>(fbb.Release())); |
| 1244 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1245 | remapped_configuration_buffer_ = |
| 1246 | std::make_unique<FlatbufferDetachedBuffer<Configuration>>( |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1247 | configuration::MergeConfiguration(new_merged_config, schemas)); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1248 | |
| 1249 | remapped_configuration_ = &remapped_configuration_buffer_->message(); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1250 | |
| 1251 | // TODO(austin): Lazily re-build to save CPU? |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Austin Schuh | 1c22735 | 2021-09-17 12:53:54 -0700 | [diff] [blame] | 1254 | std::vector<const Channel *> LogReader::RemappedChannels() const { |
| 1255 | std::vector<const Channel *> result; |
| 1256 | result.reserve(remapped_channels_.size()); |
| 1257 | for (auto &pair : remapped_channels_) { |
| 1258 | const Channel *const logged_channel = |
| 1259 | CHECK_NOTNULL(logged_configuration()->channels()->Get(pair.first)); |
| 1260 | |
| 1261 | auto channel_iterator = std::lower_bound( |
| 1262 | remapped_configuration_->channels()->cbegin(), |
| 1263 | remapped_configuration_->channels()->cend(), |
| 1264 | std::make_pair(std::string_view(pair.second.remapped_name), |
| 1265 | logged_channel->type()->string_view()), |
| 1266 | CompareChannels); |
| 1267 | |
| 1268 | CHECK(channel_iterator != remapped_configuration_->channels()->cend()); |
| 1269 | CHECK(EqualsChannels( |
| 1270 | *channel_iterator, |
| 1271 | std::make_pair(std::string_view(pair.second.remapped_name), |
| 1272 | logged_channel->type()->string_view()))); |
| 1273 | result.push_back(*channel_iterator); |
| 1274 | } |
| 1275 | return result; |
| 1276 | } |
| 1277 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1278 | const Channel *LogReader::RemapChannel(const EventLoop *event_loop, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1279 | const Node *node, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1280 | const Channel *channel) { |
| 1281 | std::string_view channel_name = channel->name()->string_view(); |
| 1282 | std::string_view channel_type = channel->type()->string_view(); |
| 1283 | const int channel_index = |
| 1284 | configuration::ChannelIndex(logged_configuration(), channel); |
| 1285 | // If the channel is remapped, find the correct channel name to use. |
| 1286 | if (remapped_channels_.count(channel_index) > 0) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1287 | VLOG(3) << "Got remapped channel on " |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1288 | << configuration::CleanedChannelToString(channel); |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 1289 | channel_name = remapped_channels_[channel_index].remapped_name; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1292 | VLOG(2) << "Going to remap channel " << channel_name << " " << channel_type; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1293 | const Channel *remapped_channel = configuration::GetChannel( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1294 | configuration(), channel_name, channel_type, |
| 1295 | event_loop ? event_loop->name() : "log_reader", node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1296 | |
| 1297 | CHECK(remapped_channel != nullptr) |
| 1298 | << ": Unable to send {\"name\": \"" << channel_name << "\", \"type\": \"" |
| 1299 | << channel_type << "\"} because it is not in the provided configuration."; |
| 1300 | |
| 1301 | return remapped_channel; |
| 1302 | } |
| 1303 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1304 | LogReader::State::State(std::unique_ptr<TimestampMapper> timestamp_mapper, |
| 1305 | const Node *node) |
| 1306 | : timestamp_mapper_(std::move(timestamp_mapper)), node_(node) {} |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1307 | |
| 1308 | void LogReader::State::AddPeer(State *peer) { |
| 1309 | if (timestamp_mapper_ && peer->timestamp_mapper_) { |
| 1310 | timestamp_mapper_->AddPeer(peer->timestamp_mapper_.get()); |
| 1311 | } |
| 1312 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1313 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1314 | void LogReader::State::SetNodeEventLoopFactory( |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1315 | NodeEventLoopFactory *node_event_loop_factory) { |
| 1316 | node_event_loop_factory_ = node_event_loop_factory; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | void LogReader::State::SetChannelCount(size_t count) { |
| 1320 | channels_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1321 | remote_timestamp_senders_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1322 | filters_.resize(count); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1323 | channel_source_state_.resize(count); |
| 1324 | factory_channel_index_.resize(count); |
| 1325 | queue_index_map_.resize(count); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1328 | void LogReader::State::SetRemoteTimestampSender( |
| 1329 | size_t logged_channel_index, RemoteMessageSender *remote_timestamp_sender) { |
| 1330 | remote_timestamp_senders_[logged_channel_index] = remote_timestamp_sender; |
| 1331 | } |
| 1332 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1333 | void LogReader::State::SetChannel( |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1334 | size_t logged_channel_index, size_t factory_channel_index, |
| 1335 | std::unique_ptr<RawSender> sender, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1336 | message_bridge::NoncausalOffsetEstimator *filter, bool is_forwarded, |
| 1337 | State *source_state) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1338 | channels_[logged_channel_index] = std::move(sender); |
| 1339 | filters_[logged_channel_index] = filter; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1340 | channel_source_state_[logged_channel_index] = source_state; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1341 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1342 | if (is_forwarded) { |
| 1343 | queue_index_map_[logged_channel_index] = |
| 1344 | std::make_unique<std::vector<State::ContiguousSentTimestamp>>(); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | factory_channel_index_[logged_channel_index] = factory_channel_index; |
| 1348 | } |
| 1349 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1350 | bool LogReader::State::Send(const TimestampedMessage ×tamped_message) { |
| 1351 | aos::RawSender *sender = channels_[timestamped_message.channel_index].get(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1352 | CHECK(sender); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1353 | uint32_t remote_queue_index = 0xffffffff; |
| 1354 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1355 | if (remote_timestamp_senders_[timestamped_message.channel_index] != nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1356 | State *source_state = |
| 1357 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]); |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1358 | std::vector<ContiguousSentTimestamp> *queue_index_map = CHECK_NOTNULL( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1359 | source_state->queue_index_map_[timestamped_message.channel_index] |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1360 | .get()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1361 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1362 | struct SentTimestamp { |
| 1363 | monotonic_clock::time_point monotonic_event_time; |
| 1364 | uint32_t queue_index; |
| 1365 | } search; |
| 1366 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1367 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1368 | source_state->boot_count()); |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 1369 | search.monotonic_event_time = |
| 1370 | timestamped_message.monotonic_remote_time.time; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1371 | search.queue_index = timestamped_message.remote_queue_index.index; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1372 | |
| 1373 | // Find the sent time if available. |
| 1374 | auto element = std::lower_bound( |
| 1375 | queue_index_map->begin(), queue_index_map->end(), search, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1376 | [](ContiguousSentTimestamp a, SentTimestamp b) { |
| 1377 | if (a.ending_monotonic_event_time < b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1378 | return true; |
| 1379 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1380 | if (a.starting_monotonic_event_time > b.monotonic_event_time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1381 | return false; |
| 1382 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1383 | |
| 1384 | if (a.ending_queue_index < b.queue_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1385 | return true; |
| 1386 | } |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1387 | if (a.starting_queue_index >= b.queue_index) { |
| 1388 | return false; |
| 1389 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1390 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1391 | // If it isn't clearly below or above, it is below. Since we return |
| 1392 | // the last element <, this will return a match. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1393 | return false; |
| 1394 | }); |
| 1395 | |
| 1396 | // TODO(austin): Be a bit more principled here, but we will want to do that |
| 1397 | // after the logger rewrite. We hit this when one node finishes, but the |
| 1398 | // other node isn't done yet. So there is no send time, but there is a |
| 1399 | // receive time. |
| 1400 | if (element != queue_index_map->end()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1401 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1402 | source_state->boot_count()); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1403 | |
| 1404 | CHECK_GE(timestamped_message.monotonic_remote_time.time, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1405 | element->starting_monotonic_event_time); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1406 | CHECK_LE(timestamped_message.monotonic_remote_time.time, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1407 | element->ending_monotonic_event_time); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1408 | CHECK_GE(timestamped_message.remote_queue_index.index, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1409 | element->starting_queue_index); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1410 | CHECK_LE(timestamped_message.remote_queue_index.index, |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1411 | element->ending_queue_index); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1412 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1413 | remote_queue_index = timestamped_message.remote_queue_index.index + |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1414 | element->actual_queue_index - |
| 1415 | element->starting_queue_index; |
| 1416 | } else { |
| 1417 | VLOG(1) << "No timestamp match in the map."; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1418 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1419 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1420 | source_state->boot_count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | // Send! Use the replayed queue index here instead of the logged queue index |
| 1424 | // for the remote queue index. This makes re-logging work. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1425 | const auto err = sender->Send( |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 1426 | RawSender::SharedSpan(timestamped_message.data, |
| 1427 | ×tamped_message.data->span), |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1428 | timestamped_message.monotonic_remote_time.time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 1429 | timestamped_message.realtime_remote_time, remote_queue_index, |
| 1430 | (channel_source_state_[timestamped_message.channel_index] != nullptr |
| 1431 | ? CHECK_NOTNULL( |
| 1432 | channel_source_state_[timestamped_message.channel_index]) |
| 1433 | ->event_loop_->boot_uuid() |
| 1434 | : event_loop_->boot_uuid())); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1435 | if (err != RawSender::Error::kOk) return false; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1436 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1437 | if (queue_index_map_[timestamped_message.channel_index]) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1438 | CHECK_EQ(timestamped_message.monotonic_event_time.boot, boot_count()); |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1439 | if (queue_index_map_[timestamped_message.channel_index]->empty()) { |
| 1440 | // Nothing here, start a range with 0 length. |
| 1441 | ContiguousSentTimestamp timestamp; |
| 1442 | timestamp.starting_monotonic_event_time = |
| 1443 | timestamp.ending_monotonic_event_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1444 | timestamped_message.monotonic_event_time.time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1445 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1446 | timestamped_message.queue_index.index; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1447 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 1448 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 1449 | timestamp); |
| 1450 | } else { |
| 1451 | // We've got something. See if the next timestamp is still contiguous. If |
| 1452 | // so, grow it. |
| 1453 | ContiguousSentTimestamp *back = |
| 1454 | &queue_index_map_[timestamped_message.channel_index]->back(); |
| 1455 | if ((back->starting_queue_index - back->actual_queue_index) == |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1456 | (timestamped_message.queue_index.index - |
| 1457 | sender->sent_queue_index())) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1458 | back->ending_queue_index = timestamped_message.queue_index.index; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1459 | back->ending_monotonic_event_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1460 | timestamped_message.monotonic_event_time.time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1461 | } else { |
| 1462 | // Otherwise, make a new one. |
| 1463 | ContiguousSentTimestamp timestamp; |
| 1464 | timestamp.starting_monotonic_event_time = |
| 1465 | timestamp.ending_monotonic_event_time = |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1466 | timestamped_message.monotonic_event_time.time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1467 | timestamp.starting_queue_index = timestamp.ending_queue_index = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1468 | timestamped_message.queue_index.index; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 1469 | timestamp.actual_queue_index = sender->sent_queue_index(); |
| 1470 | queue_index_map_[timestamped_message.channel_index]->emplace_back( |
| 1471 | timestamp); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | // TODO(austin): Should we prune the map? On a many day log, I only saw the |
| 1476 | // queue index diverge a couple of elements, which would be a very small |
| 1477 | // map. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1478 | } else if (remote_timestamp_senders_[timestamped_message.channel_index] != |
| 1479 | nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1480 | State *source_state = |
| 1481 | CHECK_NOTNULL(channel_source_state_[timestamped_message.channel_index]); |
| 1482 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1483 | flatbuffers::FlatBufferBuilder fbb; |
| 1484 | fbb.ForceDefaults(true); |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 1485 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> boot_uuid_offset = |
| 1486 | event_loop_->boot_uuid().PackVector(&fbb); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1487 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1488 | RemoteMessage::Builder message_header_builder(fbb); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1489 | |
| 1490 | message_header_builder.add_channel_index( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1491 | factory_channel_index_[timestamped_message.channel_index]); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1492 | |
| 1493 | // Swap the remote and sent metrics. They are from the sender's |
| 1494 | // perspective, not the receiver's perspective. |
| 1495 | message_header_builder.add_monotonic_sent_time( |
| 1496 | sender->monotonic_sent_time().time_since_epoch().count()); |
| 1497 | message_header_builder.add_realtime_sent_time( |
| 1498 | sender->realtime_sent_time().time_since_epoch().count()); |
| 1499 | message_header_builder.add_queue_index(sender->sent_queue_index()); |
| 1500 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1501 | CHECK_EQ(timestamped_message.monotonic_remote_time.boot, |
| 1502 | source_state->boot_count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1503 | message_header_builder.add_monotonic_remote_time( |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1504 | timestamped_message.monotonic_remote_time.time.time_since_epoch() |
| 1505 | .count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1506 | message_header_builder.add_realtime_remote_time( |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1507 | timestamped_message.realtime_remote_time.time_since_epoch().count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1508 | |
| 1509 | message_header_builder.add_remote_queue_index(remote_queue_index); |
Austin Schuh | 315b96b | 2020-12-11 21:21:12 -0800 | [diff] [blame] | 1510 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1511 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1512 | fbb.Finish(message_header_builder.Finish()); |
| 1513 | |
| 1514 | remote_timestamp_senders_[timestamped_message.channel_index]->Send( |
| 1515 | FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1516 | timestamped_message.monotonic_timestamp_time, |
| 1517 | source_state->boot_count()); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1523 | LogReader::RemoteMessageSender::RemoteMessageSender( |
| 1524 | aos::Sender<message_bridge::RemoteMessage> sender, EventLoop *event_loop) |
| 1525 | : event_loop_(event_loop), |
| 1526 | sender_(std::move(sender)), |
| 1527 | timer_(event_loop->AddTimer([this]() { SendTimestamp(); })) {} |
| 1528 | |
| 1529 | void LogReader::RemoteMessageSender::ScheduleTimestamp() { |
| 1530 | if (remote_timestamps_.empty()) { |
| 1531 | CHECK_NOTNULL(timer_); |
| 1532 | timer_->Disable(); |
| 1533 | scheduled_time_ = monotonic_clock::min_time; |
| 1534 | return; |
| 1535 | } |
| 1536 | |
| 1537 | if (scheduled_time_ != remote_timestamps_.front().monotonic_timestamp_time) { |
| 1538 | CHECK_NOTNULL(timer_); |
Austin Schuh | 816e5d6 | 2021-01-05 23:42:20 -0800 | [diff] [blame] | 1539 | timer_->Setup(remote_timestamps_.front().monotonic_timestamp_time); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1540 | scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time; |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 1541 | CHECK_GE(scheduled_time_, event_loop_->monotonic_now()) |
| 1542 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | void LogReader::RemoteMessageSender::Send( |
| 1547 | FlatbufferDetachedBuffer<RemoteMessage> remote_message, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1548 | BootTimestamp monotonic_timestamp_time, size_t source_boot_count) { |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 1549 | // There are 2 variants of logs. |
| 1550 | // 1) Logs without monotonic_timestamp_time |
| 1551 | // 2) Logs with monotonic_timestamp_time |
| 1552 | // |
| 1553 | // As of Jan 2021, we shouldn't have any more logs without |
| 1554 | // monotonic_timestamp_time. We don't have data locked up in those logs worth |
| 1555 | // the effort of saving. |
| 1556 | // |
| 1557 | // This gives us 3 cases, 2 of which are undistinguishable. |
| 1558 | // 1) Old log without monotonic_timestamp_time. |
| 1559 | // 2) New log with monotonic_timestamp_time where the timestamp was logged |
| 1560 | // remotely so we actually have monotonic_timestamp_time. |
| 1561 | // 3) New log, but the timestamp was logged on the node receiving the message |
| 1562 | // so there is no monotonic_timestamp_time. |
| 1563 | // |
| 1564 | // Our goal when replaying is to accurately reproduce the state of the world |
| 1565 | // present when logging. If a timestamp wasn't sent back across the network, |
| 1566 | // we shouldn't replay one back across the network. |
| 1567 | // |
| 1568 | // Given that we don't really care about 1, we can use the presence of the |
| 1569 | // timestamp to distinguish 2 and 3, and ignore 1. If we don't have a |
| 1570 | // monotonic_timestamp_time, this means the message was logged locally and |
| 1571 | // remote timestamps can be ignored. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1572 | if (monotonic_timestamp_time == BootTimestamp::min_time()) { |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 1573 | return; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1574 | } |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 1575 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1576 | CHECK_EQ(monotonic_timestamp_time.boot, source_boot_count); |
| 1577 | |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 1578 | remote_timestamps_.emplace( |
| 1579 | std::upper_bound( |
| 1580 | remote_timestamps_.begin(), remote_timestamps_.end(), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1581 | monotonic_timestamp_time.time, |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 1582 | [](const aos::monotonic_clock::time_point monotonic_timestamp_time, |
| 1583 | const Timestamp ×tamp) { |
| 1584 | return monotonic_timestamp_time < |
| 1585 | timestamp.monotonic_timestamp_time; |
| 1586 | }), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1587 | std::move(remote_message), monotonic_timestamp_time.time); |
Austin Schuh | c41d6a8 | 2021-07-16 14:49:23 -0700 | [diff] [blame] | 1588 | ScheduleTimestamp(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | void LogReader::RemoteMessageSender::SendTimestamp() { |
Austin Schuh | 3d94be0 | 2021-02-12 23:15:20 -0800 | [diff] [blame] | 1592 | CHECK_EQ(event_loop_->context().monotonic_event_time, scheduled_time_) |
| 1593 | << event_loop_->node()->name()->string_view(); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1594 | CHECK(!remote_timestamps_.empty()); |
| 1595 | |
| 1596 | // Send out all timestamps at the currently scheduled time. |
| 1597 | while (remote_timestamps_.front().monotonic_timestamp_time == |
| 1598 | scheduled_time_) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 1599 | CHECK_EQ(sender_.Send(std::move(remote_timestamps_.front().remote_message)), |
| 1600 | RawSender::Error::kOk); |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 1601 | remote_timestamps_.pop_front(); |
| 1602 | if (remote_timestamps_.empty()) { |
| 1603 | break; |
| 1604 | } |
| 1605 | } |
| 1606 | scheduled_time_ = monotonic_clock::min_time; |
| 1607 | |
| 1608 | ScheduleTimestamp(); |
| 1609 | } |
| 1610 | |
| 1611 | LogReader::RemoteMessageSender *LogReader::State::RemoteTimestampSender( |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1612 | const Channel *channel, const Connection *connection) { |
| 1613 | message_bridge::ChannelTimestampFinder finder(event_loop_); |
| 1614 | // Look at any pre-created channel/connection pairs. |
| 1615 | { |
| 1616 | auto it = |
| 1617 | channel_timestamp_loggers_.find(std::make_pair(channel, connection)); |
| 1618 | if (it != channel_timestamp_loggers_.end()) { |
| 1619 | return it->second.get(); |
| 1620 | } |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1623 | // That failed, so resolve the RemoteMessage channel timestamps will be logged |
| 1624 | // to. |
| 1625 | const Channel *timestamp_channel = finder.ForChannel(channel, connection); |
| 1626 | |
| 1627 | { |
| 1628 | // See if that has been created before. If so, cache it in |
| 1629 | // channel_timestamp_loggers_ and return. |
| 1630 | auto it = timestamp_loggers_.find(timestamp_channel); |
| 1631 | if (it != timestamp_loggers_.end()) { |
| 1632 | CHECK(channel_timestamp_loggers_ |
| 1633 | .try_emplace(std::make_pair(channel, connection), it->second) |
| 1634 | .second); |
| 1635 | return it->second.get(); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | // Otherwise, make a sender, save it, and cache it. |
| 1640 | auto result = channel_timestamp_loggers_.try_emplace( |
| 1641 | std::make_pair(channel, connection), |
| 1642 | std::make_shared<RemoteMessageSender>( |
| 1643 | event_loop()->MakeSender<RemoteMessage>( |
| 1644 | timestamp_channel->name()->string_view()), |
| 1645 | event_loop())); |
| 1646 | |
| 1647 | CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second) |
| 1648 | .second); |
| 1649 | return result.first->second.get(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 1652 | TimestampedMessage LogReader::State::PopOldest() { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1653 | CHECK(timestamp_mapper_ != nullptr); |
| 1654 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 1655 | CHECK(result_ptr != nullptr); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1656 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1657 | TimestampedMessage result = std::move(*result_ptr); |
| 1658 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1659 | VLOG(2) << MaybeNodeName(event_loop_->node()) << "PopOldest Popping " |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1660 | << result.monotonic_event_time; |
| 1661 | timestamp_mapper_->PopFront(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1662 | SeedSortedMessages(); |
| 1663 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1664 | CHECK_EQ(result.monotonic_event_time.boot, boot_count()); |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1665 | |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 1666 | VLOG(1) << "Popped " << result |
| 1667 | << configuration::CleanedChannelToString( |
| 1668 | event_loop_->configuration()->channels()->Get( |
| 1669 | factory_channel_index_[result.channel_index])); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1670 | return result; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1673 | BootTimestamp LogReader::State::OldestMessageTime() const { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1674 | if (timestamp_mapper_ == nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1675 | return BootTimestamp::max_time(); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1676 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1677 | TimestampedMessage *result_ptr = timestamp_mapper_->Front(); |
| 1678 | if (result_ptr == nullptr) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1679 | return BootTimestamp::max_time(); |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1680 | } |
Austin Schuh | 0b8a550 | 2021-11-18 15:34:12 -0800 | [diff] [blame] | 1681 | VLOG(2) << MaybeNodeName(node()) << "oldest message at " |
Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1682 | << result_ptr->monotonic_event_time.time; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1683 | return result_ptr->monotonic_event_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | void LogReader::State::SeedSortedMessages() { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 1687 | if (!timestamp_mapper_) return; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1688 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 1689 | timestamp_mapper_->QueueFor(chrono::duration_cast<chrono::seconds>( |
| 1690 | chrono::duration<double>(FLAGS_time_estimation_buffer_seconds))); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | void LogReader::State::Deregister() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 1694 | if (started_ && !stopped_) { |
| 1695 | RunOnEnd(); |
| 1696 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1697 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 1698 | channels_[i].reset(); |
| 1699 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 1700 | channel_timestamp_loggers_.clear(); |
| 1701 | timestamp_loggers_.clear(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1702 | event_loop_unique_ptr_.reset(); |
| 1703 | event_loop_ = nullptr; |
| 1704 | timer_handler_ = nullptr; |
| 1705 | node_event_loop_factory_ = nullptr; |
| 1706 | } |
| 1707 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 1708 | } // namespace logger |
| 1709 | } // namespace aos |