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