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