Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LOG_NAMER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LOG_NAMER_H_ |
| 3 | |
| 4 | #include <functional> |
| 5 | #include <map> |
| 6 | #include <memory> |
| 7 | #include <string_view> |
| 8 | #include <vector> |
| 9 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 10 | #include "absl/container/btree_map.h" |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 11 | #include "aos/events/logging/logfile_utils.h" |
| 12 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 13 | #include "aos/uuid.h" |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 14 | #include "flatbuffers/flatbuffers.h" |
| 15 | |
| 16 | namespace aos { |
| 17 | namespace logger { |
| 18 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 19 | class LogNamer; |
| 20 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 21 | // TODO(austin): Rename this back to DataWriter once all other callers are of |
| 22 | // the old DataWriter. |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 23 | // |
| 24 | // Class to manage writing data to log files. This lets us track which boot the |
| 25 | // written header has in it, and if the header has been written or not. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 26 | // |
| 27 | // The design of this class is that instead of being notified when any of the |
| 28 | // header data changes, it polls and owns that decision. This makes it much |
| 29 | // harder to write corrupted data. If that becomes a performance problem, we |
| 30 | // can DCHECK and take it out of production binaries. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 31 | class NewDataWriter { |
| 32 | public: |
| 33 | // Constructs a NewDataWriter. |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 34 | // log_namer is the log namer which holds the config and any other data we |
| 35 | // need for our header. |
| 36 | // node is the node whom's prespective we are logging from. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 37 | // reopen is called whenever a file needs to be reopened. |
| 38 | // close is called to close that file and extract any statistics. |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 39 | NewDataWriter(LogNamer *log_namer, const Node *node, const Node *logger_node, |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 40 | std::function<void(NewDataWriter *)> reopen, |
| 41 | std::function<void(NewDataWriter *)> close); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 42 | |
| 43 | NewDataWriter(NewDataWriter &&other) = default; |
| 44 | aos::logger::NewDataWriter &operator=(NewDataWriter &&other) = default; |
| 45 | NewDataWriter(const NewDataWriter &) = delete; |
| 46 | void operator=(const NewDataWriter &) = delete; |
| 47 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 48 | ~NewDataWriter(); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 49 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 50 | // Rotates the log file, delaying writing the new header until data arrives. |
| 51 | void Rotate(); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 52 | |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 53 | // Updates all the metadata in the log file about the remote node which this |
| 54 | // message is from. |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 55 | void UpdateRemote(size_t remote_node_index, const UUID &remote_node_boot_uuid, |
| 56 | monotonic_clock::time_point monotonic_remote_time, |
| 57 | monotonic_clock::time_point monotonic_event_time, |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 58 | bool reliable, |
| 59 | monotonic_clock::time_point monotonic_timestamp_time = |
| 60 | monotonic_clock::min_time); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 61 | // Queues up a message with the provided boot UUID. |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 62 | void QueueMessage(flatbuffers::FlatBufferBuilder *fbb, |
| 63 | const UUID &node_boot_uuid, |
| 64 | aos::monotonic_clock::time_point now); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 65 | |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 66 | // Updates the current boot for the source node. This is useful when you want |
| 67 | // to queue a message that may trigger a reboot rotation, but then need to |
| 68 | // update the remote timestamps. |
| 69 | void UpdateBoot(const UUID &source_node_boot_uuid); |
| 70 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 71 | // Returns the filename of the writer. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 72 | std::string_view filename() const { |
| 73 | return writer ? writer->filename() : "(closed)"; |
| 74 | } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 75 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 76 | void Close(); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 77 | |
| 78 | std::unique_ptr<DetachedBufferWriter> writer = nullptr; |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 79 | |
| 80 | size_t node_index() const { return node_index_; } |
| 81 | const UUID &parts_uuid() const { return parts_uuid_; } |
| 82 | size_t parts_index() const { return parts_index_; } |
| 83 | const Node *node() const { return node_; } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 84 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 85 | // Datastructure used to capture all the information about a remote node. |
| 86 | struct State { |
| 87 | // Boot UUID of the node. |
| 88 | UUID boot_uuid = UUID::Zero(); |
| 89 | // Timestamp on the remote monotonic clock of the oldest message sent to |
| 90 | // node_index_. |
| 91 | monotonic_clock::time_point oldest_remote_monotonic_timestamp = |
| 92 | monotonic_clock::max_time; |
| 93 | // Timestamp on the local monotonic clock of the message in |
| 94 | // oldest_remote_monotonic_timestamp. |
| 95 | monotonic_clock::time_point oldest_local_monotonic_timestamp = |
| 96 | monotonic_clock::max_time; |
| 97 | // Timestamp on the remote monotonic clock of the oldest message sent to |
| 98 | // node_index_, excluding messages forwarded with time_to_live() == 0. |
| 99 | monotonic_clock::time_point oldest_remote_unreliable_monotonic_timestamp = |
| 100 | monotonic_clock::max_time; |
| 101 | // Timestamp on the local monotonic clock of the message in |
| 102 | // oldest_local_unreliable_monotonic_timestamp. |
| 103 | monotonic_clock::time_point oldest_local_unreliable_monotonic_timestamp = |
| 104 | monotonic_clock::max_time; |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 105 | |
| 106 | // Timestamp on the remote monotonic clock of the oldest message sent to |
| 107 | // node_index_, only including messages forwarded with time_to_live() == 0. |
| 108 | monotonic_clock::time_point oldest_remote_reliable_monotonic_timestamp = |
| 109 | monotonic_clock::max_time; |
| 110 | // Timestamp on the local monotonic clock of the message in |
| 111 | // oldest_local_reliable_monotonic_timestamp. |
| 112 | monotonic_clock::time_point oldest_local_reliable_monotonic_timestamp = |
| 113 | monotonic_clock::max_time; |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 114 | |
| 115 | // Timestamp on the remote monotonic clock of the oldest message timestamp |
| 116 | // sent back to logger_node_index_. The remote here will be the node this |
| 117 | // part is from the perspective of, ie node_index_. |
| 118 | monotonic_clock::time_point |
| 119 | oldest_logger_remote_unreliable_monotonic_timestamp = |
| 120 | monotonic_clock::max_time; |
| 121 | // The time on the monotonic clock of the logger when this timestamp made it |
| 122 | // back to the logger (logger_node_index_). |
| 123 | monotonic_clock::time_point |
| 124 | oldest_logger_local_unreliable_monotonic_timestamp = |
| 125 | monotonic_clock::max_time; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 128 | private: |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 129 | // Signals that a node has rebooted. |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 130 | void Reboot(const UUID &source_node_boot_uuid); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 131 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 132 | void QueueHeader( |
| 133 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header); |
| 134 | |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 135 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(); |
| 136 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 137 | monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::min_time; |
| 138 | |
Austin Schuh | 577610e | 2021-12-08 12:07:19 -0800 | [diff] [blame] | 139 | const Node *node_ = nullptr; |
| 140 | size_t node_index_ = 0; |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 141 | size_t logger_node_index_ = 0; |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 142 | LogNamer *log_namer_; |
| 143 | UUID parts_uuid_ = UUID::Random(); |
| 144 | size_t parts_index_ = 0; |
| 145 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 146 | std::function<void(NewDataWriter *)> reopen_; |
| 147 | std::function<void(NewDataWriter *)> close_; |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 148 | bool header_written_ = false; |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 149 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 150 | std::vector<State> state_; |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 153 | // Interface describing how to name, track, and add headers to log file parts. |
| 154 | class LogNamer { |
| 155 | public: |
| 156 | // Constructs a LogNamer with the primary node (ie the one the logger runs on) |
| 157 | // being node. |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 158 | LogNamer(const aos::Configuration *configuration, EventLoop *event_loop, |
| 159 | const aos::Node *node) |
Austin Schuh | a499cea | 2021-07-31 19:49:53 -0700 | [diff] [blame] | 160 | : event_loop_(event_loop), |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 161 | configuration_(configuration), |
| 162 | node_(node), |
Austin Schuh | a499cea | 2021-07-31 19:49:53 -0700 | [diff] [blame] | 163 | logger_node_index_(configuration::GetNodeIndex(configuration_, node_)) { |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 164 | nodes_.emplace_back(node_); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 165 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 166 | virtual ~LogNamer() {} |
| 167 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 168 | virtual std::string_view base_name() const = 0; |
| 169 | |
| 170 | // Rotate should be called at least once in between calls to set_base_name. |
| 171 | // Otherwise temporary files will not be recoverable. |
| 172 | // Rotate is called by Logger::RenameLogBase, which is currently the only user |
| 173 | // of this method. |
| 174 | // Only renaming the folder is supported, not the file base name. |
| 175 | virtual void set_base_name(std::string_view base_name) = 0; |
| 176 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 177 | // Returns a writer for writing data from messages on this channel (on the |
| 178 | // primary node). |
| 179 | // |
| 180 | // The returned pointer will stay valid across rotations, but the object it |
| 181 | // points to will be assigned to. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 182 | virtual NewDataWriter *MakeWriter(const Channel *channel) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 183 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 184 | // Returns a writer for writing timestamps from messages on this channel (on |
| 185 | // the primary node). |
| 186 | // |
| 187 | // The returned pointer will stay valid across rotations, but the object it |
| 188 | // points to will be assigned to. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 189 | virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 190 | |
| 191 | // Returns a writer for writing timestamps delivered over the special |
| 192 | // /aos/remote_timestamps/* channels. node is the node that the timestamps |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 193 | // are forwarded back from (to the primary node). |
| 194 | // |
| 195 | // The returned pointer will stay valid across rotations, but the object it |
| 196 | // points to will be assigned to. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 197 | virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel, |
| 198 | const Node *node) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 199 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 200 | // Rotates all log files for the provided node. |
| 201 | virtual void Rotate(const Node *node) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 202 | |
| 203 | // Returns all the nodes that data is being written for. |
| 204 | const std::vector<const Node *> &nodes() const { return nodes_; } |
| 205 | |
| 206 | // Returns the node the logger is running on. |
| 207 | const Node *node() const { return node_; } |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 208 | const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; } |
| 209 | size_t logger_node_index() const { return logger_node_index_; } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 210 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 211 | // Writes out the nested Configuration object to the config file location. |
| 212 | virtual void WriteConfiguration( |
| 213 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 214 | std::string_view config_sha256) = 0; |
| 215 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 216 | void SetHeaderTemplate( |
| 217 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) { |
| 218 | header_ = std::move(header); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 219 | logger_node_boot_uuid_ = |
| 220 | UUID::FromString(header_.message().logger_node_boot_uuid()); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 221 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 222 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 223 | void ClearStartTimes() { |
| 224 | node_states_.clear(); |
| 225 | } |
| 226 | |
| 227 | void SetStartTimes(size_t node_index, const UUID &boot_uuid, |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 228 | monotonic_clock::time_point monotonic_start_time, |
| 229 | realtime_clock::time_point realtime_start_time, |
| 230 | monotonic_clock::time_point logger_monotonic_start_time, |
| 231 | realtime_clock::time_point logger_realtime_start_time) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 232 | VLOG(1) << "Setting node " << node_index << " to start time " |
| 233 | << monotonic_start_time << " rt " << realtime_start_time << " UUID " |
| 234 | << boot_uuid; |
| 235 | NodeState *node_state = GetNodeState(node_index, boot_uuid); |
| 236 | node_state->monotonic_start_time = monotonic_start_time; |
| 237 | node_state->realtime_start_time = realtime_start_time; |
| 238 | node_state->logger_monotonic_start_time = logger_monotonic_start_time; |
| 239 | node_state->logger_realtime_start_time = logger_realtime_start_time; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 242 | monotonic_clock::time_point monotonic_start_time(size_t node_index, |
| 243 | const UUID &boot_uuid) { |
| 244 | DCHECK_NE(boot_uuid, UUID::Zero()); |
| 245 | |
| 246 | NodeState *node_state = GetNodeState(node_index, boot_uuid); |
| 247 | return node_state->monotonic_start_time; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 250 | protected: |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 251 | // Structure with state per node about times and such. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 252 | struct NodeState { |
| 253 | // Time when this node started logging. |
| 254 | monotonic_clock::time_point monotonic_start_time = |
| 255 | monotonic_clock::min_time; |
| 256 | realtime_clock::time_point realtime_start_time = realtime_clock::min_time; |
| 257 | |
| 258 | // Corresponding time on the logger node when it started logging. |
| 259 | monotonic_clock::time_point logger_monotonic_start_time = |
| 260 | monotonic_clock::min_time; |
| 261 | realtime_clock::time_point logger_realtime_start_time = |
| 262 | realtime_clock::min_time; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 263 | }; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 264 | |
| 265 | // Creates a new header by copying fields out of the template and combining |
| 266 | // them with the arguments provided. |
| 267 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader( |
| 268 | size_t node_index, const std::vector<NewDataWriter::State> &state, |
| 269 | const UUID &parts_uuid, int parts_index); |
| 270 | |
| 271 | EventLoop *event_loop_; |
| 272 | const Configuration *const configuration_; |
| 273 | const Node *const node_; |
| 274 | const size_t logger_node_index_; |
| 275 | UUID logger_node_boot_uuid_; |
| 276 | std::vector<const Node *> nodes_; |
| 277 | |
| 278 | friend NewDataWriter; |
| 279 | |
| 280 | // Returns the start/stop time state structure for a node and boot. We can |
| 281 | // have data from multiple boots, and it makes sense to reuse the start/stop |
| 282 | // times if we get data from the same boot again. |
| 283 | NodeState *GetNodeState(size_t node_index, const UUID &boot_uuid); |
| 284 | |
| 285 | absl::btree_map<std::pair<size_t, UUID>, NodeState> node_states_; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 286 | |
| 287 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ = |
| 288 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | // Local log namer is a simple version which only names things |
| 292 | // "base_name.part#.bfbs" and increments the part number. It doesn't support |
| 293 | // any other log type. |
| 294 | class LocalLogNamer : public LogNamer { |
| 295 | public: |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 296 | LocalLogNamer(std::string_view base_name, aos::EventLoop *event_loop, |
| 297 | const aos::Node *node) |
| 298 | : LogNamer(event_loop->configuration(), event_loop, node), |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 299 | base_name_(base_name), |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 300 | data_writer_( |
| 301 | this, node, event_loop->node(), |
| 302 | [this](NewDataWriter *writer) { |
| 303 | writer->writer = std::make_unique<DetachedBufferWriter>( |
| 304 | absl::StrCat(base_name_, ".part", writer->parts_index(), |
| 305 | ".bfbs"), |
| 306 | std::make_unique<aos::logger::DummyEncoder>()); |
| 307 | }, |
| 308 | [](NewDataWriter * /*writer*/) {}) {} |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 309 | |
| 310 | LocalLogNamer(const LocalLogNamer &) = delete; |
| 311 | LocalLogNamer(LocalLogNamer &&) = delete; |
| 312 | LocalLogNamer &operator=(const LocalLogNamer &) = delete; |
| 313 | LocalLogNamer &operator=(LocalLogNamer &&) = delete; |
| 314 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 315 | ~LocalLogNamer() override = default; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 316 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 317 | std::string_view base_name() const final { return base_name_; } |
| 318 | |
| 319 | void set_base_name(std::string_view base_name) final { |
| 320 | base_name_ = base_name; |
| 321 | } |
| 322 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 323 | NewDataWriter *MakeWriter(const Channel *channel) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 324 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 325 | void Rotate(const Node *node) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 326 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 327 | NewDataWriter *MakeTimestampWriter(const Channel *channel) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 328 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 329 | NewDataWriter *MakeForwardedTimestampWriter(const Channel * /*channel*/, |
| 330 | const Node * /*node*/) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 331 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 332 | void WriteConfiguration( |
| 333 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 334 | std::string_view config_sha256) override; |
| 335 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 336 | private: |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 337 | std::string base_name_; |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 338 | |
| 339 | NewDataWriter data_writer_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | // Log namer which uses a config and a base name to name a bunch of files. |
| 343 | class MultiNodeLogNamer : public LogNamer { |
| 344 | public: |
Austin Schuh | a499cea | 2021-07-31 19:49:53 -0700 | [diff] [blame] | 345 | MultiNodeLogNamer(std::string_view base_name, EventLoop *event_loop); |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 346 | MultiNodeLogNamer(std::string_view base_name, |
| 347 | const Configuration *configuration, EventLoop *event_loop, |
| 348 | const Node *node); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 349 | ~MultiNodeLogNamer() override; |
| 350 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 351 | std::string_view base_name() const final { return base_name_; } |
| 352 | |
| 353 | void set_base_name(std::string_view base_name) final { |
| 354 | old_base_name_ = base_name_; |
| 355 | base_name_ = base_name; |
| 356 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 357 | |
Brian Silverman | 48deab1 | 2020-09-30 18:39:28 -0700 | [diff] [blame] | 358 | // If temp_suffix is set, then this will write files under names beginning |
| 359 | // with the specified suffix, and then rename them to the desired name after |
| 360 | // they are fully written. |
| 361 | // |
| 362 | // This is useful to enable incremental copying of the log files. |
| 363 | // |
| 364 | // Defaults to writing directly to the final filename. |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 365 | void set_temp_suffix(std::string_view temp_suffix) { |
| 366 | temp_suffix_ = temp_suffix; |
| 367 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 368 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 369 | // Sets the function for creating encoders. |
| 370 | // |
| 371 | // Defaults to just creating DummyEncoders. |
| 372 | void set_encoder_factory( |
| 373 | std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory) { |
| 374 | encoder_factory_ = std::move(encoder_factory); |
| 375 | } |
| 376 | |
| 377 | // Sets an additional file extension. |
| 378 | // |
| 379 | // Defaults to nothing. |
| 380 | void set_extension(std::string_view extension) { extension_ = extension; } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 381 | |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 382 | // A list of all the filenames we've written. |
| 383 | // |
| 384 | // This only includes the part after base_name(). |
| 385 | const std::vector<std::string> &all_filenames() const { |
| 386 | return all_filenames_; |
| 387 | } |
| 388 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 389 | void Rotate(const Node *node) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 390 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 391 | void WriteConfiguration( |
| 392 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 393 | std::string_view config_sha256) override; |
| 394 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 395 | NewDataWriter *MakeWriter(const Channel *channel) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 396 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 397 | NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel, |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 398 | const Node *node) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 399 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 400 | NewDataWriter *MakeTimestampWriter(const Channel *channel) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 401 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 402 | // Indicates that at least one file ran out of space. Once this happens, we |
| 403 | // stop trying to open new files, to avoid writing any files with holes from |
| 404 | // previous parts. |
| 405 | // |
| 406 | // Besides this function, this object will silently stop logging data when |
| 407 | // this occurs. If you want to ensure log files are complete, you must call |
| 408 | // this method. |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 409 | bool ran_out_of_space() const { |
| 410 | return accumulate_data_writers<bool>( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 411 | ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 412 | return x || |
| 413 | (data_writer.writer && data_writer.writer->ran_out_of_space()); |
| 414 | }); |
| 415 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 416 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 417 | // Returns the maximum total_bytes() value for all existing |
| 418 | // DetachedBufferWriters. |
| 419 | // |
| 420 | // Returns 0 if no files are open. |
| 421 | size_t maximum_total_bytes() const { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 422 | return accumulate_data_writers<size_t>( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 423 | 0, [](size_t x, const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 424 | return std::max(x, data_writer.writer->total_bytes()); |
| 425 | }); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 428 | // Closes all existing log files. No more data may be written after this. |
| 429 | // |
| 430 | // This may set ran_out_of_space(). |
| 431 | void Close(); |
| 432 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 433 | // Accessors for various statistics. See the identically-named methods in |
| 434 | // DetachedBufferWriter for documentation. These are aggregated across all |
| 435 | // past and present DetachedBufferWriters. |
| 436 | std::chrono::nanoseconds max_write_time() const { |
| 437 | return accumulate_data_writers( |
| 438 | max_write_time_, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 439 | [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 440 | return std::max(x, data_writer.writer->max_write_time()); |
| 441 | }); |
| 442 | } |
| 443 | int max_write_time_bytes() const { |
| 444 | return std::get<0>(accumulate_data_writers( |
| 445 | std::make_tuple(max_write_time_bytes_, max_write_time_), |
| 446 | [](std::tuple<int, std::chrono::nanoseconds> x, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 447 | const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 448 | if (data_writer.writer->max_write_time() > std::get<1>(x)) { |
| 449 | return std::make_tuple(data_writer.writer->max_write_time_bytes(), |
| 450 | data_writer.writer->max_write_time()); |
| 451 | } |
| 452 | return x; |
| 453 | })); |
| 454 | } |
| 455 | int max_write_time_messages() const { |
| 456 | return std::get<0>(accumulate_data_writers( |
| 457 | std::make_tuple(max_write_time_messages_, max_write_time_), |
| 458 | [](std::tuple<int, std::chrono::nanoseconds> x, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 459 | const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 460 | if (data_writer.writer->max_write_time() > std::get<1>(x)) { |
| 461 | return std::make_tuple( |
| 462 | data_writer.writer->max_write_time_messages(), |
| 463 | data_writer.writer->max_write_time()); |
| 464 | } |
| 465 | return x; |
| 466 | })); |
| 467 | } |
| 468 | std::chrono::nanoseconds total_write_time() const { |
| 469 | return accumulate_data_writers( |
| 470 | total_write_time_, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 471 | [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 472 | return x + data_writer.writer->total_write_time(); |
| 473 | }); |
| 474 | } |
| 475 | int total_write_count() const { |
| 476 | return accumulate_data_writers( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 477 | total_write_count_, [](int x, const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 478 | return x + data_writer.writer->total_write_count(); |
| 479 | }); |
| 480 | } |
| 481 | int total_write_messages() const { |
| 482 | return accumulate_data_writers( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 483 | total_write_messages_, [](int x, const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 484 | return x + data_writer.writer->total_write_messages(); |
| 485 | }); |
| 486 | } |
| 487 | int total_write_bytes() const { |
| 488 | return accumulate_data_writers( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 489 | total_write_bytes_, [](int x, const NewDataWriter &data_writer) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 490 | return x + data_writer.writer->total_write_bytes(); |
| 491 | }); |
| 492 | } |
| 493 | |
| 494 | void ResetStatistics(); |
| 495 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 496 | private: |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 497 | // Opens up a writer for timestamps forwarded back. |
| 498 | void OpenForwardedTimestampWriter(const Channel *channel, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 499 | NewDataWriter *data_writer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 500 | |
| 501 | // Opens up a writer for remote data. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 502 | void OpenWriter(const Channel *channel, NewDataWriter *data_writer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 503 | |
| 504 | // Opens the main data writer file for this node responsible for data_writer_. |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 505 | void OpenDataWriter(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 506 | |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 507 | void CreateBufferWriter(std::string_view path, |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 508 | std::unique_ptr<DetachedBufferWriter> *destination); |
| 509 | |
Brian Silverman | 48deab1 | 2020-09-30 18:39:28 -0700 | [diff] [blame] | 510 | void RenameTempFile(DetachedBufferWriter *destination); |
| 511 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 512 | void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 513 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 514 | // A version of std::accumulate which operates over all of our DataWriters. |
| 515 | template <typename T, typename BinaryOperation> |
| 516 | T accumulate_data_writers(T t, BinaryOperation op) const { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 517 | for (const std::pair<const Channel *const, NewDataWriter> &data_writer : |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 518 | data_writers_) { |
Austin Schuh | ad0cfc3 | 2020-12-21 12:34:26 -0800 | [diff] [blame] | 519 | if (!data_writer.second.writer) continue; |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 520 | t = op(std::move(t), data_writer.second); |
| 521 | } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 522 | if (data_writer_) { |
| 523 | t = op(std::move(t), *data_writer_); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 524 | } |
| 525 | return t; |
| 526 | } |
| 527 | |
Austin Schuh | 6bb8a82 | 2021-03-31 23:04:39 -0700 | [diff] [blame] | 528 | std::string base_name_; |
| 529 | std::string old_base_name_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 530 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 531 | bool ran_out_of_space_ = false; |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 532 | std::vector<std::string> all_filenames_; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 533 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 534 | std::string temp_suffix_; |
| 535 | std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory_ = |
| 536 | []() { return std::make_unique<DummyEncoder>(); }; |
| 537 | std::string extension_; |
| 538 | |
| 539 | // Storage for statistics from previously-rotated DetachedBufferWriters. |
| 540 | std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero(); |
| 541 | int max_write_time_bytes_ = -1; |
| 542 | int max_write_time_messages_ = -1; |
| 543 | std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero(); |
| 544 | int total_write_count_ = 0; |
| 545 | int total_write_messages_ = 0; |
| 546 | int total_write_bytes_ = 0; |
| 547 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 548 | // File to write both delivery timestamps and local data to. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 549 | std::unique_ptr<NewDataWriter> data_writer_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 550 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 551 | std::map<const Channel *, NewDataWriter> data_writers_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | } // namespace logger |
| 555 | } // namespace aos |
| 556 | |
| 557 | #endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_ |