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