blob: c3fc5d498500f85996840973ca03b8192bdaf2cc [file] [log] [blame]
Austin Schuhcb5601b2020-09-10 15:29:59 -07001#ifndef AOS_EVENTS_LOGGING_LOG_NAMER_H_
2#define AOS_EVENTS_LOGGING_LOG_NAMER_H_
3
4#include <functional>
5#include <map>
6#include <memory>
7#include <string_view>
8#include <vector>
9
Austin Schuh58646e22021-08-23 23:51:46 -070010#include "absl/container/btree_map.h"
Austin Schuhcb5601b2020-09-10 15:29:59 -070011#include "aos/events/logging/logfile_utils.h"
12#include "aos/events/logging/logger_generated.h"
Austin Schuh4385b142021-03-14 21:31:13 -070013#include "aos/uuid.h"
Austin Schuhcb5601b2020-09-10 15:29:59 -070014#include "flatbuffers/flatbuffers.h"
15
16namespace aos {
17namespace logger {
18
Austin Schuh572924a2021-07-30 22:32:12 -070019class LogNamer;
20
Austin Schuhb8bca732021-07-30 22:32:00 -070021// TODO(austin): Rename this back to DataWriter once all other callers are of
22// the old DataWriter.
Austin Schuh572924a2021-07-30 22:32:12 -070023//
24// Class to manage writing data to log files. This lets us track which boot the
25// written header has in it, and if the header has been written or not.
Austin Schuh58646e22021-08-23 23:51:46 -070026//
27// The design of this class is that instead of being notified when any of the
28// header data changes, it polls and owns that decision. This makes it much
29// harder to write corrupted data. If that becomes a performance problem, we
30// can DCHECK and take it out of production binaries.
Austin Schuhb8bca732021-07-30 22:32:00 -070031class NewDataWriter {
32 public:
33 // Constructs a NewDataWriter.
Austin Schuh572924a2021-07-30 22:32:12 -070034 // log_namer is the log namer which holds the config and any other data we
35 // need for our header.
36 // node is the node whom's prespective we are logging from.
Austin Schuhb8bca732021-07-30 22:32:00 -070037 // reopen is called whenever a file needs to be reopened.
38 // close is called to close that file and extract any statistics.
Austin Schuh572924a2021-07-30 22:32:12 -070039 NewDataWriter(LogNamer *log_namer, const Node *node,
40 std::function<void(NewDataWriter *)> reopen,
41 std::function<void(NewDataWriter *)> close);
Austin Schuhb8bca732021-07-30 22:32:00 -070042
43 NewDataWriter(NewDataWriter &&other) = default;
44 aos::logger::NewDataWriter &operator=(NewDataWriter &&other) = default;
45 NewDataWriter(const NewDataWriter &) = delete;
46 void operator=(const NewDataWriter &) = delete;
47
Austin Schuh572924a2021-07-30 22:32:12 -070048 ~NewDataWriter();
Austin Schuhb8bca732021-07-30 22:32:00 -070049
Austin Schuh572924a2021-07-30 22:32:12 -070050 // Rotates the log file, delaying writing the new header until data arrives.
51 void Rotate();
Austin Schuhb8bca732021-07-30 22:32:00 -070052
Austin Schuh72211ae2021-08-05 14:02:30 -070053 void UpdateRemote(size_t remote_node_index, const UUID &remote_node_boot_uuid,
54 monotonic_clock::time_point monotonic_remote_time,
55 monotonic_clock::time_point monotonic_event_time,
56 bool reliable);
Austin Schuh572924a2021-07-30 22:32:12 -070057 // Queues up a message with the provided boot UUID.
Austin Schuhe46492f2021-07-31 19:49:41 -070058 void QueueMessage(flatbuffers::FlatBufferBuilder *fbb,
59 const UUID &node_boot_uuid,
60 aos::monotonic_clock::time_point now);
Austin Schuhb8bca732021-07-30 22:32:00 -070061
Austin Schuh5e14d842022-01-21 12:02:15 -080062 // Updates the current boot for the source node. This is useful when you want
63 // to queue a message that may trigger a reboot rotation, but then need to
64 // update the remote timestamps.
65 void UpdateBoot(const UUID &source_node_boot_uuid);
66
Austin Schuh572924a2021-07-30 22:32:12 -070067 // Returns the filename of the writer.
Austin Schuh58646e22021-08-23 23:51:46 -070068 std::string_view filename() const {
69 return writer ? writer->filename() : "(closed)";
70 }
Austin Schuhb8bca732021-07-30 22:32:00 -070071
Austin Schuh572924a2021-07-30 22:32:12 -070072 void Close();
Austin Schuhb8bca732021-07-30 22:32:00 -070073
74 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
Austin Schuh572924a2021-07-30 22:32:12 -070075
76 size_t node_index() const { return node_index_; }
77 const UUID &parts_uuid() const { return parts_uuid_; }
78 size_t parts_index() const { return parts_index_; }
79 const Node *node() const { return node_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070080
Austin Schuh72211ae2021-08-05 14:02:30 -070081 // Datastructure used to capture all the information about a remote node.
82 struct State {
83 // Boot UUID of the node.
84 UUID boot_uuid = UUID::Zero();
85 // Timestamp on the remote monotonic clock of the oldest message sent to
86 // node_index_.
87 monotonic_clock::time_point oldest_remote_monotonic_timestamp =
88 monotonic_clock::max_time;
89 // Timestamp on the local monotonic clock of the message in
90 // oldest_remote_monotonic_timestamp.
91 monotonic_clock::time_point oldest_local_monotonic_timestamp =
92 monotonic_clock::max_time;
93 // Timestamp on the remote monotonic clock of the oldest message sent to
94 // node_index_, excluding messages forwarded with time_to_live() == 0.
95 monotonic_clock::time_point oldest_remote_unreliable_monotonic_timestamp =
96 monotonic_clock::max_time;
97 // Timestamp on the local monotonic clock of the message in
98 // oldest_local_unreliable_monotonic_timestamp.
99 monotonic_clock::time_point oldest_local_unreliable_monotonic_timestamp =
100 monotonic_clock::max_time;
101 };
102
Austin Schuhb8bca732021-07-30 22:32:00 -0700103 private:
Austin Schuhe46492f2021-07-31 19:49:41 -0700104 // Signals that a node has rebooted.
Austin Schuh5e14d842022-01-21 12:02:15 -0800105 void Reboot(const UUID &source_node_boot_uuid);
Austin Schuhe46492f2021-07-31 19:49:41 -0700106
Austin Schuh572924a2021-07-30 22:32:12 -0700107 void QueueHeader(
108 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header);
109
Austin Schuhe46492f2021-07-31 19:49:41 -0700110 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader();
111
Austin Schuh58646e22021-08-23 23:51:46 -0700112 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::min_time;
113
Austin Schuh577610e2021-12-08 12:07:19 -0800114 const Node *node_ = nullptr;
115 size_t node_index_ = 0;
Austin Schuh572924a2021-07-30 22:32:12 -0700116 LogNamer *log_namer_;
117 UUID parts_uuid_ = UUID::Random();
118 size_t parts_index_ = 0;
119
Austin Schuhb8bca732021-07-30 22:32:00 -0700120 std::function<void(NewDataWriter *)> reopen_;
121 std::function<void(NewDataWriter *)> close_;
Austin Schuh572924a2021-07-30 22:32:12 -0700122 bool header_written_ = false;
Austin Schuhe46492f2021-07-31 19:49:41 -0700123
Austin Schuh72211ae2021-08-05 14:02:30 -0700124 std::vector<State> state_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700125};
126
Austin Schuhcb5601b2020-09-10 15:29:59 -0700127// Interface describing how to name, track, and add headers to log file parts.
128class LogNamer {
129 public:
130 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
131 // being node.
Austin Schuh5b728b72021-06-16 14:57:15 -0700132 LogNamer(const aos::Configuration *configuration, EventLoop *event_loop,
133 const aos::Node *node)
Austin Schuha499cea2021-07-31 19:49:53 -0700134 : event_loop_(event_loop),
Austin Schuh5b728b72021-06-16 14:57:15 -0700135 configuration_(configuration),
136 node_(node),
Austin Schuha499cea2021-07-31 19:49:53 -0700137 logger_node_index_(configuration::GetNodeIndex(configuration_, node_)) {
Austin Schuh73340842021-07-30 22:32:06 -0700138 nodes_.emplace_back(node_);
Austin Schuh73340842021-07-30 22:32:06 -0700139 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700140 virtual ~LogNamer() {}
141
Austin Schuh6bb8a822021-03-31 23:04:39 -0700142 virtual std::string_view base_name() const = 0;
143
144 // Rotate should be called at least once in between calls to set_base_name.
145 // Otherwise temporary files will not be recoverable.
146 // Rotate is called by Logger::RenameLogBase, which is currently the only user
147 // of this method.
148 // Only renaming the folder is supported, not the file base name.
149 virtual void set_base_name(std::string_view base_name) = 0;
150
Brian Silverman87ac0402020-09-17 14:47:01 -0700151 // Returns a writer for writing data from messages on this channel (on the
152 // primary node).
153 //
154 // The returned pointer will stay valid across rotations, but the object it
155 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700156 virtual NewDataWriter *MakeWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700157
Brian Silverman87ac0402020-09-17 14:47:01 -0700158 // Returns a writer for writing timestamps from messages on this channel (on
159 // the primary node).
160 //
161 // The returned pointer will stay valid across rotations, but the object it
162 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700163 virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700164
165 // Returns a writer for writing timestamps delivered over the special
166 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -0700167 // are forwarded back from (to the primary node).
168 //
169 // The returned pointer will stay valid across rotations, but the object it
170 // points to will be assigned to.
Austin Schuh73340842021-07-30 22:32:06 -0700171 virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
172 const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700173
Austin Schuh73340842021-07-30 22:32:06 -0700174 // Rotates all log files for the provided node.
175 virtual void Rotate(const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700176
177 // Returns all the nodes that data is being written for.
178 const std::vector<const Node *> &nodes() const { return nodes_; }
179
180 // Returns the node the logger is running on.
181 const Node *node() const { return node_; }
Austin Schuhe46492f2021-07-31 19:49:41 -0700182 const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; }
183 size_t logger_node_index() const { return logger_node_index_; }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700184
Austin Schuh8c399962020-12-25 21:51:45 -0800185 // Writes out the nested Configuration object to the config file location.
186 virtual void WriteConfiguration(
187 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
188 std::string_view config_sha256) = 0;
189
Austin Schuh73340842021-07-30 22:32:06 -0700190 void SetHeaderTemplate(
191 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) {
192 header_ = std::move(header);
Austin Schuhe46492f2021-07-31 19:49:41 -0700193 logger_node_boot_uuid_ =
194 UUID::FromString(header_.message().logger_node_boot_uuid());
Austin Schuh73340842021-07-30 22:32:06 -0700195 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700196
Austin Schuh58646e22021-08-23 23:51:46 -0700197 void ClearStartTimes() {
198 node_states_.clear();
199 }
200
201 void SetStartTimes(size_t node_index, const UUID &boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700202 monotonic_clock::time_point monotonic_start_time,
203 realtime_clock::time_point realtime_start_time,
204 monotonic_clock::time_point logger_monotonic_start_time,
205 realtime_clock::time_point logger_realtime_start_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700206 VLOG(1) << "Setting node " << node_index << " to start time "
207 << monotonic_start_time << " rt " << realtime_start_time << " UUID "
208 << boot_uuid;
209 NodeState *node_state = GetNodeState(node_index, boot_uuid);
210 node_state->monotonic_start_time = monotonic_start_time;
211 node_state->realtime_start_time = realtime_start_time;
212 node_state->logger_monotonic_start_time = logger_monotonic_start_time;
213 node_state->logger_realtime_start_time = logger_realtime_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700214 }
215
Austin Schuh58646e22021-08-23 23:51:46 -0700216 monotonic_clock::time_point monotonic_start_time(size_t node_index,
217 const UUID &boot_uuid) {
218 DCHECK_NE(boot_uuid, UUID::Zero());
219
220 NodeState *node_state = GetNodeState(node_index, boot_uuid);
221 return node_state->monotonic_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700222 }
223
Austin Schuh73340842021-07-30 22:32:06 -0700224 protected:
Austin Schuh73340842021-07-30 22:32:06 -0700225 // Structure with state per node about times and such.
Austin Schuh73340842021-07-30 22:32:06 -0700226 struct NodeState {
227 // Time when this node started logging.
228 monotonic_clock::time_point monotonic_start_time =
229 monotonic_clock::min_time;
230 realtime_clock::time_point realtime_start_time = realtime_clock::min_time;
231
232 // Corresponding time on the logger node when it started logging.
233 monotonic_clock::time_point logger_monotonic_start_time =
234 monotonic_clock::min_time;
235 realtime_clock::time_point logger_realtime_start_time =
236 realtime_clock::min_time;
Austin Schuh73340842021-07-30 22:32:06 -0700237 };
Austin Schuh58646e22021-08-23 23:51:46 -0700238
239 // Creates a new header by copying fields out of the template and combining
240 // them with the arguments provided.
241 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
242 size_t node_index, const std::vector<NewDataWriter::State> &state,
243 const UUID &parts_uuid, int parts_index);
244
245 EventLoop *event_loop_;
246 const Configuration *const configuration_;
247 const Node *const node_;
248 const size_t logger_node_index_;
249 UUID logger_node_boot_uuid_;
250 std::vector<const Node *> nodes_;
251
252 friend NewDataWriter;
253
254 // Returns the start/stop time state structure for a node and boot. We can
255 // have data from multiple boots, and it makes sense to reuse the start/stop
256 // times if we get data from the same boot again.
257 NodeState *GetNodeState(size_t node_index, const UUID &boot_uuid);
258
259 absl::btree_map<std::pair<size_t, UUID>, NodeState> node_states_;
Austin Schuh73340842021-07-30 22:32:06 -0700260
261 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ =
262 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700263};
264
265// Local log namer is a simple version which only names things
266// "base_name.part#.bfbs" and increments the part number. It doesn't support
267// any other log type.
268class LocalLogNamer : public LogNamer {
269 public:
Austin Schuh5b728b72021-06-16 14:57:15 -0700270 LocalLogNamer(std::string_view base_name, aos::EventLoop *event_loop,
271 const aos::Node *node)
272 : LogNamer(event_loop->configuration(), event_loop, node),
Austin Schuhcb5601b2020-09-10 15:29:59 -0700273 base_name_(base_name),
Austin Schuh5b728b72021-06-16 14:57:15 -0700274 data_writer_(this, node,
Austin Schuh572924a2021-07-30 22:32:12 -0700275 [this](NewDataWriter *writer) {
276 writer->writer = std::make_unique<DetachedBufferWriter>(
277 absl::StrCat(base_name_, ".part",
278 writer->parts_index(), ".bfbs"),
279 std::make_unique<aos::logger::DummyEncoder>());
280 },
281 [](NewDataWriter * /*writer*/) {}) {}
Austin Schuhb8bca732021-07-30 22:32:00 -0700282
283 LocalLogNamer(const LocalLogNamer &) = delete;
284 LocalLogNamer(LocalLogNamer &&) = delete;
285 LocalLogNamer &operator=(const LocalLogNamer &) = delete;
286 LocalLogNamer &operator=(LocalLogNamer &&) = delete;
287
Brian Silverman0465fcf2020-09-24 00:29:18 -0700288 ~LocalLogNamer() override = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700289
Austin Schuh6bb8a822021-03-31 23:04:39 -0700290 std::string_view base_name() const final { return base_name_; }
291
292 void set_base_name(std::string_view base_name) final {
293 base_name_ = base_name;
294 }
295
Austin Schuhb8bca732021-07-30 22:32:00 -0700296 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700297
Austin Schuh73340842021-07-30 22:32:06 -0700298 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700299
Austin Schuhb8bca732021-07-30 22:32:00 -0700300 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700301
Austin Schuh73340842021-07-30 22:32:06 -0700302 NewDataWriter *MakeForwardedTimestampWriter(const Channel * /*channel*/,
303 const Node * /*node*/) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700304
Austin Schuh8c399962020-12-25 21:51:45 -0800305 void WriteConfiguration(
306 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
307 std::string_view config_sha256) override;
308
Austin Schuhcb5601b2020-09-10 15:29:59 -0700309 private:
Austin Schuh6bb8a822021-03-31 23:04:39 -0700310 std::string base_name_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700311
312 NewDataWriter data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700313};
314
315// Log namer which uses a config and a base name to name a bunch of files.
316class MultiNodeLogNamer : public LogNamer {
317 public:
Austin Schuha499cea2021-07-31 19:49:53 -0700318 MultiNodeLogNamer(std::string_view base_name, EventLoop *event_loop);
Austin Schuh5b728b72021-06-16 14:57:15 -0700319 MultiNodeLogNamer(std::string_view base_name,
320 const Configuration *configuration, EventLoop *event_loop,
321 const Node *node);
Brian Silvermancb805822020-10-06 17:43:35 -0700322 ~MultiNodeLogNamer() override;
323
Austin Schuh6bb8a822021-03-31 23:04:39 -0700324 std::string_view base_name() const final { return base_name_; }
325
326 void set_base_name(std::string_view base_name) final {
327 old_base_name_ = base_name_;
328 base_name_ = base_name;
329 }
Brian Silvermancb805822020-10-06 17:43:35 -0700330
Brian Silverman48deab12020-09-30 18:39:28 -0700331 // If temp_suffix is set, then this will write files under names beginning
332 // with the specified suffix, and then rename them to the desired name after
333 // they are fully written.
334 //
335 // This is useful to enable incremental copying of the log files.
336 //
337 // Defaults to writing directly to the final filename.
Brian Silvermancb805822020-10-06 17:43:35 -0700338 void set_temp_suffix(std::string_view temp_suffix) {
339 temp_suffix_ = temp_suffix;
340 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700341
Brian Silvermancb805822020-10-06 17:43:35 -0700342 // Sets the function for creating encoders.
343 //
344 // Defaults to just creating DummyEncoders.
345 void set_encoder_factory(
346 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory) {
347 encoder_factory_ = std::move(encoder_factory);
348 }
349
350 // Sets an additional file extension.
351 //
352 // Defaults to nothing.
353 void set_extension(std::string_view extension) { extension_ = extension; }
Brian Silverman1f345222020-09-24 21:14:48 -0700354
Brian Silvermana621f522020-09-30 16:52:43 -0700355 // A list of all the filenames we've written.
356 //
357 // This only includes the part after base_name().
358 const std::vector<std::string> &all_filenames() const {
359 return all_filenames_;
360 }
361
Austin Schuh73340842021-07-30 22:32:06 -0700362 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700363
Austin Schuh8c399962020-12-25 21:51:45 -0800364 void WriteConfiguration(
365 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
366 std::string_view config_sha256) override;
367
Austin Schuhb8bca732021-07-30 22:32:00 -0700368 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700369
Austin Schuhb8bca732021-07-30 22:32:00 -0700370 NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
Austin Schuh73340842021-07-30 22:32:06 -0700371 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700372
Austin Schuhb8bca732021-07-30 22:32:00 -0700373 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700374
Brian Silverman0465fcf2020-09-24 00:29:18 -0700375 // Indicates that at least one file ran out of space. Once this happens, we
376 // stop trying to open new files, to avoid writing any files with holes from
377 // previous parts.
378 //
379 // Besides this function, this object will silently stop logging data when
380 // this occurs. If you want to ensure log files are complete, you must call
381 // this method.
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700382 bool ran_out_of_space() const {
383 return accumulate_data_writers<bool>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700384 ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700385 return x ||
386 (data_writer.writer && data_writer.writer->ran_out_of_space());
387 });
388 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700389
Brian Silverman1f345222020-09-24 21:14:48 -0700390 // Returns the maximum total_bytes() value for all existing
391 // DetachedBufferWriters.
392 //
393 // Returns 0 if no files are open.
394 size_t maximum_total_bytes() const {
Brian Silvermancb805822020-10-06 17:43:35 -0700395 return accumulate_data_writers<size_t>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700396 0, [](size_t x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700397 return std::max(x, data_writer.writer->total_bytes());
398 });
Brian Silverman1f345222020-09-24 21:14:48 -0700399 }
400
Brian Silverman0465fcf2020-09-24 00:29:18 -0700401 // Closes all existing log files. No more data may be written after this.
402 //
403 // This may set ran_out_of_space().
404 void Close();
405
Brian Silvermancb805822020-10-06 17:43:35 -0700406 // Accessors for various statistics. See the identically-named methods in
407 // DetachedBufferWriter for documentation. These are aggregated across all
408 // past and present DetachedBufferWriters.
409 std::chrono::nanoseconds max_write_time() const {
410 return accumulate_data_writers(
411 max_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700412 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700413 return std::max(x, data_writer.writer->max_write_time());
414 });
415 }
416 int max_write_time_bytes() const {
417 return std::get<0>(accumulate_data_writers(
418 std::make_tuple(max_write_time_bytes_, max_write_time_),
419 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700420 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700421 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
422 return std::make_tuple(data_writer.writer->max_write_time_bytes(),
423 data_writer.writer->max_write_time());
424 }
425 return x;
426 }));
427 }
428 int max_write_time_messages() const {
429 return std::get<0>(accumulate_data_writers(
430 std::make_tuple(max_write_time_messages_, max_write_time_),
431 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700432 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700433 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
434 return std::make_tuple(
435 data_writer.writer->max_write_time_messages(),
436 data_writer.writer->max_write_time());
437 }
438 return x;
439 }));
440 }
441 std::chrono::nanoseconds total_write_time() const {
442 return accumulate_data_writers(
443 total_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700444 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700445 return x + data_writer.writer->total_write_time();
446 });
447 }
448 int total_write_count() const {
449 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700450 total_write_count_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700451 return x + data_writer.writer->total_write_count();
452 });
453 }
454 int total_write_messages() const {
455 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700456 total_write_messages_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700457 return x + data_writer.writer->total_write_messages();
458 });
459 }
460 int total_write_bytes() const {
461 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700462 total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700463 return x + data_writer.writer->total_write_bytes();
464 });
465 }
466
467 void ResetStatistics();
468
Austin Schuhcb5601b2020-09-10 15:29:59 -0700469 private:
Austin Schuhcb5601b2020-09-10 15:29:59 -0700470 // Opens up a writer for timestamps forwarded back.
471 void OpenForwardedTimestampWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700472 NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700473
474 // Opens up a writer for remote data.
Austin Schuhb8bca732021-07-30 22:32:00 -0700475 void OpenWriter(const Channel *channel, NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700476
477 // Opens the main data writer file for this node responsible for data_writer_.
Brian Silvermana621f522020-09-30 16:52:43 -0700478 void OpenDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700479
Brian Silvermana621f522020-09-30 16:52:43 -0700480 void CreateBufferWriter(std::string_view path,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700481 std::unique_ptr<DetachedBufferWriter> *destination);
482
Brian Silverman48deab12020-09-30 18:39:28 -0700483 void RenameTempFile(DetachedBufferWriter *destination);
484
Brian Silvermancb805822020-10-06 17:43:35 -0700485 void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700486
Brian Silvermancb805822020-10-06 17:43:35 -0700487 // A version of std::accumulate which operates over all of our DataWriters.
488 template <typename T, typename BinaryOperation>
489 T accumulate_data_writers(T t, BinaryOperation op) const {
Austin Schuhb8bca732021-07-30 22:32:00 -0700490 for (const std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700491 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800492 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700493 t = op(std::move(t), data_writer.second);
494 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700495 if (data_writer_) {
496 t = op(std::move(t), *data_writer_);
Brian Silvermancb805822020-10-06 17:43:35 -0700497 }
498 return t;
499 }
500
Austin Schuh6bb8a822021-03-31 23:04:39 -0700501 std::string base_name_;
502 std::string old_base_name_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700503
Brian Silverman0465fcf2020-09-24 00:29:18 -0700504 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700505 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700506
Brian Silvermancb805822020-10-06 17:43:35 -0700507 std::string temp_suffix_;
508 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory_ =
509 []() { return std::make_unique<DummyEncoder>(); };
510 std::string extension_;
511
512 // Storage for statistics from previously-rotated DetachedBufferWriters.
513 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
514 int max_write_time_bytes_ = -1;
515 int max_write_time_messages_ = -1;
516 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
517 int total_write_count_ = 0;
518 int total_write_messages_ = 0;
519 int total_write_bytes_ = 0;
520
Austin Schuhcb5601b2020-09-10 15:29:59 -0700521 // File to write both delivery timestamps and local data to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700522 std::unique_ptr<NewDataWriter> data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700523
Austin Schuhb8bca732021-07-30 22:32:00 -0700524 std::map<const Channel *, NewDataWriter> data_writers_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700525};
526
527} // namespace logger
528} // namespace aos
529
530#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_