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