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