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 | |
| 10 | #include "aos/events/logging/logfile_utils.h" |
| 11 | #include "aos/events/logging/logger_generated.h" |
| 12 | #include "aos/events/logging/uuid.h" |
| 13 | #include "flatbuffers/flatbuffers.h" |
| 14 | |
| 15 | namespace aos { |
| 16 | namespace logger { |
| 17 | |
| 18 | // Interface describing how to name, track, and add headers to log file parts. |
| 19 | class LogNamer { |
| 20 | public: |
| 21 | // Constructs a LogNamer with the primary node (ie the one the logger runs on) |
| 22 | // being node. |
| 23 | LogNamer(const Node *node) : node_(node) { nodes_.emplace_back(node_); } |
| 24 | virtual ~LogNamer() {} |
| 25 | |
| 26 | // Writes the header to all log files for a specific node. This function |
| 27 | // needs to be called after all the writers are created. |
| 28 | // |
| 29 | // Modifies header to contain the uuid and part number for each writer as it |
| 30 | // writes it. Since this is done unconditionally, it does not restore the |
| 31 | // previous value at the end. |
| 32 | virtual void WriteHeader( |
| 33 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 34 | const Node *node) = 0; |
| 35 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 36 | // Returns a writer for writing data from messages on this channel (on the |
| 37 | // primary node). |
| 38 | // |
| 39 | // The returned pointer will stay valid across rotations, but the object it |
| 40 | // points to will be assigned to. |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 41 | virtual DetachedBufferWriter *MakeWriter(const Channel *channel) = 0; |
| 42 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 43 | // Returns a writer for writing timestamps from messages on this channel (on |
| 44 | // the primary node). |
| 45 | // |
| 46 | // The returned pointer will stay valid across rotations, but the object it |
| 47 | // points to will be assigned to. |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 48 | virtual DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) = 0; |
| 49 | |
| 50 | // Returns a writer for writing timestamps delivered over the special |
| 51 | // /aos/remote_timestamps/* channels. node is the node that the timestamps |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 52 | // are forwarded back from (to the primary node). |
| 53 | // |
| 54 | // The returned pointer will stay valid across rotations, but the object it |
| 55 | // points to will be assigned to. |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 56 | virtual DetachedBufferWriter *MakeForwardedTimestampWriter( |
| 57 | const Channel *channel, const Node *node) = 0; |
| 58 | |
| 59 | // Rotates all log files for the provided node. The provided header will be |
| 60 | // modified and written per WriteHeader above. |
| 61 | virtual void Rotate( |
| 62 | const Node *node, |
| 63 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header) = 0; |
| 64 | |
| 65 | // Returns all the nodes that data is being written for. |
| 66 | const std::vector<const Node *> &nodes() const { return nodes_; } |
| 67 | |
| 68 | // Returns the node the logger is running on. |
| 69 | const Node *node() const { return node_; } |
| 70 | |
| 71 | protected: |
| 72 | // Modifies the header to have the provided UUID and part id. |
| 73 | void UpdateHeader( |
| 74 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 75 | const UUID &uuid, int part_id) const; |
| 76 | |
| 77 | const Node *const node_; |
| 78 | std::vector<const Node *> nodes_; |
| 79 | }; |
| 80 | |
| 81 | // Local log namer is a simple version which only names things |
| 82 | // "base_name.part#.bfbs" and increments the part number. It doesn't support |
| 83 | // any other log type. |
| 84 | class LocalLogNamer : public LogNamer { |
| 85 | public: |
| 86 | LocalLogNamer(std::string_view base_name, const Node *node) |
| 87 | : LogNamer(node), |
| 88 | base_name_(base_name), |
| 89 | uuid_(UUID::Random()), |
| 90 | data_writer_(OpenDataWriter()) {} |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 91 | ~LocalLogNamer() override = default; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 92 | |
| 93 | void WriteHeader( |
| 94 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 95 | const Node *node) override; |
| 96 | |
| 97 | DetachedBufferWriter *MakeWriter(const Channel *channel) override; |
| 98 | |
| 99 | void Rotate(const Node *node, |
| 100 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header) |
| 101 | override; |
| 102 | |
| 103 | DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) override; |
| 104 | |
| 105 | DetachedBufferWriter *MakeForwardedTimestampWriter( |
| 106 | const Channel * /*channel*/, const Node * /*node*/) override; |
| 107 | |
| 108 | private: |
| 109 | // Creates a new data writer with the new part number. |
| 110 | std::unique_ptr<DetachedBufferWriter> OpenDataWriter() { |
| 111 | return std::make_unique<DetachedBufferWriter>( |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 112 | absl::StrCat(base_name_, ".part", part_number_, ".bfbs"), |
| 113 | std::make_unique<aos::logger::DummyEncoder>()); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | const std::string base_name_; |
| 117 | const UUID uuid_; |
| 118 | size_t part_number_ = 0; |
| 119 | std::unique_ptr<DetachedBufferWriter> data_writer_; |
| 120 | }; |
| 121 | |
| 122 | // Log namer which uses a config and a base name to name a bunch of files. |
| 123 | class MultiNodeLogNamer : public LogNamer { |
| 124 | public: |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 125 | MultiNodeLogNamer(std::string_view base_name, |
| 126 | const Configuration *configuration, const Node *node); |
| 127 | ~MultiNodeLogNamer() override; |
| 128 | |
| 129 | std::string_view base_name() const { return base_name_; } |
| 130 | |
Brian Silverman | 48deab1 | 2020-09-30 18:39:28 -0700 | [diff] [blame] | 131 | // If temp_suffix is set, then this will write files under names beginning |
| 132 | // with the specified suffix, and then rename them to the desired name after |
| 133 | // they are fully written. |
| 134 | // |
| 135 | // This is useful to enable incremental copying of the log files. |
| 136 | // |
| 137 | // Defaults to writing directly to the final filename. |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 138 | void set_temp_suffix(std::string_view temp_suffix) { |
| 139 | temp_suffix_ = temp_suffix; |
| 140 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 141 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 142 | // Sets the function for creating encoders. |
| 143 | // |
| 144 | // Defaults to just creating DummyEncoders. |
| 145 | void set_encoder_factory( |
| 146 | std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory) { |
| 147 | encoder_factory_ = std::move(encoder_factory); |
| 148 | } |
| 149 | |
| 150 | // Sets an additional file extension. |
| 151 | // |
| 152 | // Defaults to nothing. |
| 153 | void set_extension(std::string_view extension) { extension_ = extension; } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 154 | |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 155 | // A list of all the filenames we've written. |
| 156 | // |
| 157 | // This only includes the part after base_name(). |
| 158 | const std::vector<std::string> &all_filenames() const { |
| 159 | return all_filenames_; |
| 160 | } |
| 161 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 162 | void WriteHeader( |
| 163 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 164 | const Node *node) override; |
| 165 | |
| 166 | void Rotate(const Node *node, |
| 167 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header) |
| 168 | override; |
| 169 | |
| 170 | DetachedBufferWriter *MakeWriter(const Channel *channel) override; |
| 171 | |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 172 | DetachedBufferWriter *MakeForwardedTimestampWriter(const Channel *channel, |
| 173 | const Node *node) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 174 | |
| 175 | DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) override; |
| 176 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 177 | // Indicates that at least one file ran out of space. Once this happens, we |
| 178 | // stop trying to open new files, to avoid writing any files with holes from |
| 179 | // previous parts. |
| 180 | // |
| 181 | // Besides this function, this object will silently stop logging data when |
| 182 | // this occurs. If you want to ensure log files are complete, you must call |
| 183 | // this method. |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame^] | 184 | bool ran_out_of_space() const { |
| 185 | return accumulate_data_writers<bool>( |
| 186 | ran_out_of_space_, [](bool x, const DataWriter &data_writer) { |
| 187 | return x || |
| 188 | (data_writer.writer && data_writer.writer->ran_out_of_space()); |
| 189 | }); |
| 190 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 191 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 192 | // Returns the maximum total_bytes() value for all existing |
| 193 | // DetachedBufferWriters. |
| 194 | // |
| 195 | // Returns 0 if no files are open. |
| 196 | size_t maximum_total_bytes() const { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 197 | return accumulate_data_writers<size_t>( |
| 198 | 0, [](size_t x, const DataWriter &data_writer) { |
| 199 | return std::max(x, data_writer.writer->total_bytes()); |
| 200 | }); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 203 | // Closes all existing log files. No more data may be written after this. |
| 204 | // |
| 205 | // This may set ran_out_of_space(). |
| 206 | void Close(); |
| 207 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 208 | // Accessors for various statistics. See the identically-named methods in |
| 209 | // DetachedBufferWriter for documentation. These are aggregated across all |
| 210 | // past and present DetachedBufferWriters. |
| 211 | std::chrono::nanoseconds max_write_time() const { |
| 212 | return accumulate_data_writers( |
| 213 | max_write_time_, |
| 214 | [](std::chrono::nanoseconds x, const DataWriter &data_writer) { |
| 215 | return std::max(x, data_writer.writer->max_write_time()); |
| 216 | }); |
| 217 | } |
| 218 | int max_write_time_bytes() const { |
| 219 | return std::get<0>(accumulate_data_writers( |
| 220 | std::make_tuple(max_write_time_bytes_, max_write_time_), |
| 221 | [](std::tuple<int, std::chrono::nanoseconds> x, |
| 222 | const DataWriter &data_writer) { |
| 223 | if (data_writer.writer->max_write_time() > std::get<1>(x)) { |
| 224 | return std::make_tuple(data_writer.writer->max_write_time_bytes(), |
| 225 | data_writer.writer->max_write_time()); |
| 226 | } |
| 227 | return x; |
| 228 | })); |
| 229 | } |
| 230 | int max_write_time_messages() const { |
| 231 | return std::get<0>(accumulate_data_writers( |
| 232 | std::make_tuple(max_write_time_messages_, max_write_time_), |
| 233 | [](std::tuple<int, std::chrono::nanoseconds> x, |
| 234 | const DataWriter &data_writer) { |
| 235 | if (data_writer.writer->max_write_time() > std::get<1>(x)) { |
| 236 | return std::make_tuple( |
| 237 | data_writer.writer->max_write_time_messages(), |
| 238 | data_writer.writer->max_write_time()); |
| 239 | } |
| 240 | return x; |
| 241 | })); |
| 242 | } |
| 243 | std::chrono::nanoseconds total_write_time() const { |
| 244 | return accumulate_data_writers( |
| 245 | total_write_time_, |
| 246 | [](std::chrono::nanoseconds x, const DataWriter &data_writer) { |
| 247 | return x + data_writer.writer->total_write_time(); |
| 248 | }); |
| 249 | } |
| 250 | int total_write_count() const { |
| 251 | return accumulate_data_writers( |
| 252 | total_write_count_, [](int x, const DataWriter &data_writer) { |
| 253 | return x + data_writer.writer->total_write_count(); |
| 254 | }); |
| 255 | } |
| 256 | int total_write_messages() const { |
| 257 | return accumulate_data_writers( |
| 258 | total_write_messages_, [](int x, const DataWriter &data_writer) { |
| 259 | return x + data_writer.writer->total_write_messages(); |
| 260 | }); |
| 261 | } |
| 262 | int total_write_bytes() const { |
| 263 | return accumulate_data_writers( |
| 264 | total_write_bytes_, [](int x, const DataWriter &data_writer) { |
| 265 | return x + data_writer.writer->total_write_bytes(); |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | void ResetStatistics(); |
| 270 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 271 | private: |
| 272 | // Files to write remote data to. We want one per channel. Maps the channel |
| 273 | // to the writer, Node, and part number. |
| 274 | struct DataWriter { |
| 275 | std::unique_ptr<DetachedBufferWriter> writer = nullptr; |
| 276 | const Node *node; |
| 277 | size_t part_number = 0; |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 278 | const UUID uuid = UUID::Random(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 279 | std::function<void(const Channel *, DataWriter *)> rotate; |
| 280 | }; |
| 281 | |
| 282 | // Opens up a writer for timestamps forwarded back. |
| 283 | void OpenForwardedTimestampWriter(const Channel *channel, |
| 284 | DataWriter *data_writer); |
| 285 | |
| 286 | // Opens up a writer for remote data. |
| 287 | void OpenWriter(const Channel *channel, DataWriter *data_writer); |
| 288 | |
| 289 | // 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] | 290 | void OpenDataWriter(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 291 | |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 292 | void CreateBufferWriter(std::string_view path, |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 293 | std::unique_ptr<DetachedBufferWriter> *destination); |
| 294 | |
Brian Silverman | 48deab1 | 2020-09-30 18:39:28 -0700 | [diff] [blame] | 295 | void RenameTempFile(DetachedBufferWriter *destination); |
| 296 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 297 | void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 298 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 299 | // A version of std::accumulate which operates over all of our DataWriters. |
| 300 | template <typename T, typename BinaryOperation> |
| 301 | T accumulate_data_writers(T t, BinaryOperation op) const { |
| 302 | for (const std::pair<const Channel *const, DataWriter> &data_writer : |
| 303 | data_writers_) { |
| 304 | t = op(std::move(t), data_writer.second); |
| 305 | } |
| 306 | if (data_writer_.writer) { |
| 307 | t = op(std::move(t), data_writer_); |
| 308 | } |
| 309 | return t; |
| 310 | } |
| 311 | |
| 312 | const std::string base_name_; |
| 313 | const Configuration *const configuration_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 314 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 315 | bool ran_out_of_space_ = false; |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 316 | std::vector<std::string> all_filenames_; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 317 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 318 | std::string temp_suffix_; |
| 319 | std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory_ = |
| 320 | []() { return std::make_unique<DummyEncoder>(); }; |
| 321 | std::string extension_; |
| 322 | |
| 323 | // Storage for statistics from previously-rotated DetachedBufferWriters. |
| 324 | std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero(); |
| 325 | int max_write_time_bytes_ = -1; |
| 326 | int max_write_time_messages_ = -1; |
| 327 | std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero(); |
| 328 | int total_write_count_ = 0; |
| 329 | int total_write_messages_ = 0; |
| 330 | int total_write_bytes_ = 0; |
| 331 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 332 | // File to write both delivery timestamps and local data to. |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 333 | DataWriter data_writer_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 334 | |
| 335 | std::map<const Channel *, DataWriter> data_writers_; |
| 336 | }; |
| 337 | |
| 338 | } // namespace logger |
| 339 | } // namespace aos |
| 340 | |
| 341 | #endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_ |