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