Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LOG_READER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LOG_READER_H_ |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 3 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 4 | #include <chrono> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 5 | #include <deque> |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 6 | #include <string_view> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 7 | #include <tuple> |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 8 | #include <vector> |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 9 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop.h" |
Austin Schuh | f6f9bf3 | 2020-10-11 14:37:43 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 12 | #include "aos/events/logging/logfile_utils.h" |
James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 13 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 9254752 | 2019-12-28 14:33:43 -0800 | [diff] [blame] | 14 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 15 | #include "aos/network/message_bridge_server_generated.h" |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 16 | #include "aos/network/multinode_timestamp_filter.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 17 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 18 | #include "aos/network/timestamp_filter.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 19 | #include "aos/time/time.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame^] | 20 | #include "aos/uuid.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 21 | #include "flatbuffers/flatbuffers.h" |
| 22 | |
| 23 | namespace aos { |
| 24 | namespace logger { |
| 25 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 26 | // We end up with one of the following 3 log file types. |
| 27 | // |
| 28 | // Single node logged as the source node. |
| 29 | // -> Replayed just on the source node. |
| 30 | // |
| 31 | // Forwarding timestamps only logged from the perspective of the destination |
| 32 | // node. |
| 33 | // -> Matched with data on source node and logged. |
| 34 | // |
| 35 | // Forwarding timestamps with data logged as the destination node. |
| 36 | // -> Replayed just as the destination |
| 37 | // -> Replayed as the source (Much harder, ordering is not defined) |
| 38 | // |
| 39 | // Duplicate data logged. -> CHECK that it matches and explode otherwise. |
| 40 | // |
| 41 | // This can be boiled down to a set of constraints and tools. |
| 42 | // |
| 43 | // 1) Forwarding timestamps and data need to be logged separately. |
| 44 | // 2) Any forwarded data logged on the destination node needs to be logged |
| 45 | // separately such that it can be sorted. |
| 46 | // |
| 47 | // 1) Log reader needs to be able to sort a list of log files. |
| 48 | // 2) Log reader needs to be able to merge sorted lists of log files. |
| 49 | // 3) Log reader needs to be able to match timestamps with messages. |
| 50 | // |
| 51 | // We also need to be able to generate multiple views of a log file depending on |
| 52 | // the target. |
| 53 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 54 | // Replays all the channels in the logfile to the event loop. |
| 55 | class LogReader { |
| 56 | public: |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 57 | // If you want to supply a new configuration that will be used for replay |
| 58 | // (e.g., to change message rates, or to populate an updated schema), then |
| 59 | // pass it in here. It must provide all the channels that the original logged |
| 60 | // config did. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 61 | // |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 62 | // The single file constructor calls SortParts internally. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 63 | LogReader(std::string_view filename, |
| 64 | const Configuration *replay_configuration = nullptr); |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 65 | LogReader(std::vector<LogFile> log_files, |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 66 | const Configuration *replay_configuration = nullptr); |
James Kuszmaul | 7daef36 | 2019-12-31 18:28:17 -0800 | [diff] [blame] | 67 | ~LogReader(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 69 | // Registers all the callbacks to send the log file data out on an event loop |
| 70 | // created in event_loop_factory. This also updates time to be at the start |
| 71 | // of the log file by running until the log file starts. |
| 72 | // Note: the configuration used in the factory should be configuration() |
| 73 | // below, but can be anything as long as the locations needed to send |
| 74 | // everything are available. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 75 | void Register(SimulatedEventLoopFactory *event_loop_factory); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 76 | // Creates an SimulatedEventLoopFactory accessible via event_loop_factory(), |
| 77 | // and then calls Register. |
| 78 | void Register(); |
| 79 | // Registers callbacks for all the events after the log file starts. This is |
| 80 | // only useful when replaying live. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 81 | void Register(EventLoop *event_loop); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 82 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 83 | // Unregisters the senders. You only need to call this if you separately |
| 84 | // supplied an event loop or event loop factory and the lifetimes are such |
| 85 | // that they need to be explicitly destroyed before the LogReader destructor |
| 86 | // gets called. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 87 | void Deregister(); |
| 88 | |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 89 | // Returns the configuration being used for replay from the log file. |
| 90 | // Note that this may be different from the configuration actually used for |
| 91 | // handling events. You should generally only use this to create a |
| 92 | // SimulatedEventLoopFactory, and then get the configuration from there for |
| 93 | // everything else. |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 94 | const Configuration *logged_configuration() const; |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 95 | // Returns the configuration being used for replay from the log file. |
| 96 | // Note that this may be different from the configuration actually used for |
| 97 | // handling events. You should generally only use this to create a |
| 98 | // SimulatedEventLoopFactory, and then get the configuration from there for |
| 99 | // everything else. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 100 | // The pointer is invalidated whenever RemapLoggedChannel is called. |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 101 | const Configuration *configuration() const; |
| 102 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 103 | // Returns the nodes that this log file was created on. This is a list of |
Austin Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 104 | // pointers to a node in the nodes() list inside logged_configuration(). |
| 105 | std::vector<const Node *> LoggedNodes() const; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 106 | |
| 107 | // Returns the starting timestamp for the log file. |
Austin Schuh | 11d4373 | 2020-09-21 17:28:30 -0700 | [diff] [blame] | 108 | monotonic_clock::time_point monotonic_start_time( |
| 109 | const Node *node = nullptr) const; |
| 110 | realtime_clock::time_point realtime_start_time( |
| 111 | const Node *node = nullptr) const; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 112 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 113 | // Causes the logger to publish the provided channel on a different name so |
| 114 | // that replayed applications can publish on the proper channel name without |
| 115 | // interference. This operates on raw channel names, without any node or |
| 116 | // application specific mappings. |
| 117 | void RemapLoggedChannel(std::string_view name, std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 118 | std::string_view add_prefix = "/original", |
| 119 | std::string_view new_type = ""); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 120 | template <typename T> |
| 121 | void RemapLoggedChannel(std::string_view name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 122 | std::string_view add_prefix = "/original", |
| 123 | std::string_view new_type = "") { |
| 124 | RemapLoggedChannel(name, T::GetFullyQualifiedName(), add_prefix, new_type); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 127 | // Remaps the provided channel, though this respects node mappings, and |
| 128 | // preserves them too. This makes it so if /aos -> /pi1/aos on one node, |
| 129 | // /original/aos -> /original/pi1/aos on the same node after renaming, just |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 130 | // like you would hope. If new_type is not empty, the new channel will use |
| 131 | // the provided type instead. This allows for renaming messages. |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 132 | // |
| 133 | // TODO(austin): If you have 2 nodes remapping something to the same channel, |
| 134 | // this doesn't handle that. No use cases exist yet for that, so it isn't |
| 135 | // being done yet. |
| 136 | void RemapLoggedChannel(std::string_view name, std::string_view type, |
| 137 | const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 138 | std::string_view add_prefix = "/original", |
| 139 | std::string_view new_type = ""); |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 140 | template <typename T> |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 141 | void RemapLoggedChannel(std::string_view name, const Node *node, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 142 | std::string_view add_prefix = "/original", |
| 143 | std::string_view new_type = "") { |
| 144 | RemapLoggedChannel(name, T::GetFullyQualifiedName(), node, add_prefix, |
| 145 | new_type); |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | template <typename T> |
| 149 | bool HasChannel(std::string_view name, const Node *node = nullptr) { |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 150 | return configuration::GetChannel(logged_configuration(), name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 151 | T::GetFullyQualifiedName(), "", node, |
| 152 | true) != nullptr; |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 153 | } |
| 154 | |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 155 | // Returns true if the channel exists on the node and was logged. |
| 156 | template <typename T> |
| 157 | bool HasLoggedChannel(std::string_view name, const Node *node = nullptr) { |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 158 | const Channel *channel = |
| 159 | configuration::GetChannel(logged_configuration(), name, |
| 160 | T::GetFullyQualifiedName(), "", node, true); |
James Kuszmaul | 4f106fb | 2021-01-05 20:53:02 -0800 | [diff] [blame] | 161 | if (channel == nullptr) return false; |
| 162 | return channel->logger() != LoggerConfig::NOT_LOGGED; |
| 163 | } |
| 164 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 165 | SimulatedEventLoopFactory *event_loop_factory() { |
| 166 | return event_loop_factory_; |
| 167 | } |
| 168 | |
Austin Schuh | 0ca51f3 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 169 | std::string_view name() const { return log_files_[0].name; } |
Austin Schuh | 0c29701 | 2020-09-16 18:41:59 -0700 | [diff] [blame] | 170 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 171 | // Set whether to exit the SimulatedEventLoopFactory when we finish reading |
| 172 | // the logfile. |
| 173 | void set_exit_on_finish(bool exit_on_finish) { |
| 174 | exit_on_finish_ = exit_on_finish; |
| 175 | } |
| 176 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 177 | private: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 178 | const Channel *RemapChannel(const EventLoop *event_loop, |
| 179 | const Channel *channel); |
| 180 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 181 | // Queues at least max_out_of_order_duration_ messages into channels_. |
| 182 | void QueueMessages(); |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 183 | // Handle constructing a configuration with all the additional remapped |
| 184 | // channels from calls to RemapLoggedChannel. |
| 185 | void MakeRemappedConfig(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 186 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 187 | // Returns the number of nodes. |
| 188 | size_t nodes_count() const { |
| 189 | return !configuration::MultiNode(logged_configuration()) |
| 190 | ? 1u |
| 191 | : logged_configuration()->nodes()->size(); |
| 192 | } |
| 193 | |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 194 | const std::vector<LogFile> log_files_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 195 | |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 196 | // Class to manage sending RemoteMessages on the provided node after the |
| 197 | // correct delay. |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 198 | class RemoteMessageSender { |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 199 | public: |
| 200 | RemoteMessageSender(aos::Sender<message_bridge::RemoteMessage> sender, |
| 201 | EventLoop *event_loop); |
| 202 | RemoteMessageSender(RemoteMessageSender const &) = delete; |
| 203 | RemoteMessageSender &operator=(RemoteMessageSender const &) = delete; |
| 204 | |
| 205 | // Sends the provided message. If monotonic_timestamp_time is min_time, |
| 206 | // send it immediately. |
| 207 | void Send( |
| 208 | FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message, |
| 209 | monotonic_clock::time_point monotonic_timestamp_time); |
| 210 | |
| 211 | private: |
| 212 | // Handles actually sending the timestamp if we were delayed. |
| 213 | void SendTimestamp(); |
| 214 | // Handles scheduling the timer to send at the correct time. |
| 215 | void ScheduleTimestamp(); |
| 216 | |
| 217 | EventLoop *event_loop_; |
| 218 | aos::Sender<message_bridge::RemoteMessage> sender_; |
| 219 | aos::TimerHandler *timer_; |
| 220 | |
| 221 | // Time we are scheduled for, or min_time if we aren't scheduled. |
| 222 | monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time; |
| 223 | |
| 224 | struct Timestamp { |
| 225 | Timestamp(FlatbufferDetachedBuffer<message_bridge::RemoteMessage> |
| 226 | new_remote_message, |
| 227 | monotonic_clock::time_point new_monotonic_timestamp_time) |
| 228 | : remote_message(std::move(new_remote_message)), |
| 229 | monotonic_timestamp_time(new_monotonic_timestamp_time) {} |
| 230 | FlatbufferDetachedBuffer<message_bridge::RemoteMessage> remote_message; |
| 231 | monotonic_clock::time_point monotonic_timestamp_time; |
| 232 | }; |
| 233 | |
| 234 | // List of messages to send. The timer works through them and then disables |
| 235 | // itself automatically. |
| 236 | std::deque<Timestamp> remote_timestamps_; |
| 237 | }; |
| 238 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 239 | // State per node. |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 240 | class State { |
| 241 | public: |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 242 | State(std::unique_ptr<TimestampMapper> timestamp_mapper); |
| 243 | |
| 244 | // Connects up the timestamp mappers. |
| 245 | void AddPeer(State *peer); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 246 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 247 | TimestampMapper *timestamp_mapper() { return timestamp_mapper_.get(); } |
| 248 | |
Austin Schuh | dda74ec | 2021-01-03 19:30:37 -0800 | [diff] [blame] | 249 | // Returns the next sorted message with all the timestamps extracted and |
| 250 | // matched. |
| 251 | TimestampedMessage PopOldest(); |
Austin Schuh | 188eabe | 2020-12-29 23:41:13 -0800 | [diff] [blame] | 252 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 253 | // Returns the monotonic time of the oldest message. |
| 254 | monotonic_clock::time_point OldestMessageTime() const; |
| 255 | |
| 256 | // Primes the queues inside State. Should be called before calling |
| 257 | // OldestMessageTime. |
| 258 | void SeedSortedMessages(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 259 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 260 | // Returns the starting time for this node. |
| 261 | monotonic_clock::time_point monotonic_start_time() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 262 | return timestamp_mapper_ ? timestamp_mapper_->monotonic_start_time() |
| 263 | : monotonic_clock::min_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 264 | } |
| 265 | realtime_clock::time_point realtime_start_time() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 266 | return timestamp_mapper_ ? timestamp_mapper_->realtime_start_time() |
| 267 | : realtime_clock::min_time; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // Sets the node event loop factory for replaying into a |
| 271 | // SimulatedEventLoopFactory. Returns the EventLoop to use. |
| 272 | EventLoop *SetNodeEventLoopFactory( |
| 273 | NodeEventLoopFactory *node_event_loop_factory); |
| 274 | |
| 275 | // Sets and gets the event loop to use. |
| 276 | void set_event_loop(EventLoop *event_loop) { event_loop_ = event_loop; } |
| 277 | EventLoop *event_loop() { return event_loop_; } |
| 278 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 279 | // Sets the current realtime offset from the monotonic clock for this node |
| 280 | // (if we are on a simulated event loop). |
| 281 | void SetRealtimeOffset(monotonic_clock::time_point monotonic_time, |
| 282 | realtime_clock::time_point realtime_time) { |
| 283 | if (node_event_loop_factory_ != nullptr) { |
| 284 | node_event_loop_factory_->SetRealtimeOffset(monotonic_time, |
| 285 | realtime_time); |
| 286 | } |
| 287 | } |
| 288 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 289 | // Returns the MessageHeader sender to log delivery timestamps to for the |
| 290 | // provided remote node. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 291 | RemoteMessageSender *RemoteTimestampSender(const Channel *channel, |
| 292 | const Connection *connection); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 293 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 294 | // Converts a timestamp from the monotonic clock on this node to the |
| 295 | // distributed clock. |
| 296 | distributed_clock::time_point ToDistributedClock( |
| 297 | monotonic_clock::time_point time) { |
| 298 | return node_event_loop_factory_->ToDistributedClock(time); |
| 299 | } |
| 300 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 301 | // Returns the current time on the remote node which sends messages on |
| 302 | // channel_index. |
| 303 | monotonic_clock::time_point monotonic_remote_now(size_t channel_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 304 | return channel_source_state_[channel_index] |
| 305 | ->node_event_loop_factory_->monotonic_now(); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Austin Schuh | 5ee5687 | 2021-01-30 16:53:34 -0800 | [diff] [blame] | 308 | // Returns the start time of the remote for the provided channel. |
| 309 | monotonic_clock::time_point monotonic_remote_start_time( |
| 310 | size_t channel_index) { |
| 311 | return channel_source_state_[channel_index]->monotonic_start_time(); |
| 312 | } |
| 313 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 314 | distributed_clock::time_point RemoteToDistributedClock( |
| 315 | size_t channel_index, monotonic_clock::time_point time) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 316 | return channel_source_state_[channel_index] |
| 317 | ->node_event_loop_factory_->ToDistributedClock(time); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | const Node *remote_node(size_t channel_index) { |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 321 | return channel_source_state_[channel_index] |
| 322 | ->node_event_loop_factory_->node(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | monotonic_clock::time_point monotonic_now() { |
| 326 | return node_event_loop_factory_->monotonic_now(); |
| 327 | } |
| 328 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 329 | // Sets the number of channels. |
| 330 | void SetChannelCount(size_t count); |
| 331 | |
| 332 | // Sets the sender, filter, and target factory for a channel. |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 333 | void SetChannel(size_t logged_channel_index, size_t factory_channel_index, |
| 334 | std::unique_ptr<RawSender> sender, |
| 335 | message_bridge::NoncausalOffsetEstimator *filter, |
| 336 | RemoteMessageSender *remote_timestamp_sender, |
| 337 | State *source_state); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 338 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 339 | // Unregisters everything so we can destory the event loop. |
| 340 | void Deregister(); |
| 341 | |
| 342 | // Sets the current TimerHandle for the replay callback. |
| 343 | void set_timer_handler(TimerHandler *timer_handler) { |
| 344 | timer_handler_ = timer_handler; |
| 345 | } |
| 346 | |
| 347 | // Sets the next wakeup time on the replay callback. |
| 348 | void Setup(monotonic_clock::time_point next_time) { |
| 349 | timer_handler_->Setup(next_time); |
| 350 | } |
| 351 | |
| 352 | // Sends a buffer on the provided channel index. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 353 | bool Send(const TimestampedMessage ×tamped_message); |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 354 | |
| 355 | // Returns a debug string for the channel merger. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 356 | std::string DebugString() const { |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 357 | if (!timestamp_mapper_) { |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 358 | return ""; |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 359 | } |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 360 | return timestamp_mapper_->DebugString(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 361 | } |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 362 | |
| 363 | private: |
| 364 | // Log file. |
Austin Schuh | 287d43d | 2020-12-04 20:19:33 -0800 | [diff] [blame] | 365 | std::unique_ptr<TimestampMapper> timestamp_mapper_; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 366 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 367 | // Senders. |
| 368 | std::vector<std::unique_ptr<RawSender>> channels_; |
Austin Schuh | 969cd60 | 2021-01-03 00:09:45 -0800 | [diff] [blame] | 369 | std::vector<RemoteMessageSender *> remote_timestamp_senders_; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 370 | // The mapping from logged channel index to sent channel index. Needed for |
| 371 | // sending out MessageHeaders. |
| 372 | std::vector<int> factory_channel_index_; |
| 373 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 374 | struct ContiguousSentTimestamp { |
| 375 | // Most timestamps make it through the network, so it saves a ton of |
| 376 | // memory and CPU to store the start and end, and search for valid ranges. |
| 377 | // For one of the logs I looked at, we had 2 ranges for 4 days. |
| 378 | // |
| 379 | // Save monotonic times as well to help if a queue index ever wraps. Odds |
| 380 | // are very low, but doesn't hurt. |
| 381 | // |
| 382 | // The starting time and matching queue index. |
| 383 | monotonic_clock::time_point starting_monotonic_event_time = |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 384 | monotonic_clock::min_time; |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 385 | uint32_t starting_queue_index = 0xffffffff; |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 386 | |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 387 | // Ending time and queue index. |
| 388 | monotonic_clock::time_point ending_monotonic_event_time = |
| 389 | monotonic_clock::max_time; |
| 390 | uint32_t ending_queue_index = 0xffffffff; |
| 391 | |
| 392 | // The queue index that the first message was *actually* sent with. The |
| 393 | // queue indices are assumed to be contiguous through this range. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 394 | uint32_t actual_queue_index = 0xffffffff; |
| 395 | }; |
| 396 | |
| 397 | // Stores all the timestamps that have been sent on this channel. This is |
| 398 | // only done for channels which are forwarded and on the node which |
Austin Schuh | 9942bae | 2021-01-07 22:06:44 -0800 | [diff] [blame] | 399 | // initially sends the message. Compress using ranges and offsets. |
| 400 | std::vector<std::unique_ptr<std::vector<ContiguousSentTimestamp>>> |
| 401 | queue_index_map_; |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 402 | |
| 403 | // Factory (if we are in sim) that this loop was created on. |
| 404 | NodeEventLoopFactory *node_event_loop_factory_ = nullptr; |
| 405 | std::unique_ptr<EventLoop> event_loop_unique_ptr_; |
| 406 | // Event loop. |
| 407 | EventLoop *event_loop_ = nullptr; |
| 408 | // And timer used to send messages. |
| 409 | TimerHandler *timer_handler_; |
| 410 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 411 | // Filters (or nullptr if it isn't a forwarded channel) for each channel. |
| 412 | // This corresponds to the object which is shared among all the channels |
| 413 | // going between 2 nodes. The second element in the tuple indicates if this |
| 414 | // is the primary direction or not. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 415 | std::vector<message_bridge::NoncausalOffsetEstimator *> filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 416 | |
| 417 | // List of NodeEventLoopFactorys (or nullptr if it isn't a forwarded |
| 418 | // channel) which correspond to the originating node. |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 419 | std::vector<State *> channel_source_state_; |
| 420 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 421 | // This is a cache for channel, connection mapping to the corresponding |
| 422 | // sender. |
| 423 | absl::btree_map<std::pair<const Channel *, const Connection *>, |
| 424 | std::shared_ptr<RemoteMessageSender>> |
| 425 | channel_timestamp_loggers_; |
| 426 | |
| 427 | // Mapping from resolved RemoteMessage channel to RemoteMessage sender. This |
| 428 | // is the channel that timestamps are published to. |
| 429 | absl::btree_map<const Channel *, std::shared_ptr<RemoteMessageSender>> |
| 430 | timestamp_loggers_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 431 | }; |
| 432 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 433 | // Node index -> State. |
| 434 | std::vector<std::unique_ptr<State>> states_; |
| 435 | |
| 436 | // Creates the requested filter if it doesn't exist, regardless of whether |
| 437 | // these nodes can actually communicate directly. The second return value |
| 438 | // reports if this is the primary direction or not. |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 439 | message_bridge::NoncausalOffsetEstimator *GetFilter(const Node *node_a, |
| 440 | const Node *node_b); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 441 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 442 | // List of filters for a connection. The pointer to the first node will be |
| 443 | // less than the second node. |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 444 | std::unique_ptr<message_bridge::MultiNodeNoncausalOffsetEstimator> filters_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 445 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 446 | std::unique_ptr<FlatbufferDetachedBuffer<Configuration>> |
| 447 | remapped_configuration_buffer_; |
| 448 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 449 | std::unique_ptr<SimulatedEventLoopFactory> event_loop_factory_unique_ptr_; |
| 450 | SimulatedEventLoopFactory *event_loop_factory_ = nullptr; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 451 | |
| 452 | // Map of channel indices to new name. The channel index will be an index into |
| 453 | // logged_configuration(), and the string key will be the name of the channel |
| 454 | // to send on instead of the logged channel name. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 455 | struct RemappedChannel { |
| 456 | std::string remapped_name; |
| 457 | std::string new_type; |
| 458 | }; |
| 459 | std::map<size_t, RemappedChannel> remapped_channels_; |
Austin Schuh | 01b4c35 | 2020-09-21 23:09:39 -0700 | [diff] [blame] | 460 | std::vector<MapT> maps_; |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 461 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 462 | // Number of nodes which still have data to send. This is used to figure out |
| 463 | // when to exit. |
| 464 | size_t live_nodes_ = 0; |
| 465 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 466 | const Configuration *remapped_configuration_ = nullptr; |
| 467 | const Configuration *replay_configuration_ = nullptr; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 468 | |
| 469 | // If true, the replay timer will ignore any missing data. This is used |
| 470 | // during startup when we are bootstrapping everything and trying to get to |
| 471 | // the start of all the log files. |
| 472 | bool ignore_missing_data_ = false; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 473 | |
| 474 | // Whether to exit the SimulatedEventLoop when we finish reading the logs. |
| 475 | bool exit_on_finish_ = true; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | } // namespace logger |
| 479 | } // namespace aos |
| 480 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 481 | #endif // AOS_EVENTS_LOGGING_LOG_READER_H_ |