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