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