blob: 4a1e74f787c0cad1c352a1339f65b69219838fb3 [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 Schuhf5f99f32022-02-07 20:05:37 -080039 NewDataWriter(LogNamer *log_namer, const Node *node, const Node *logger_node,
Austin Schuh572924a2021-07-30 22:32:12 -070040 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 Schuhf5f99f32022-02-07 20:05:37 -080053 // Updates all the metadata in the log file about the remote node which this
54 // message is from.
Austin Schuh72211ae2021-08-05 14:02:30 -070055 void UpdateRemote(size_t remote_node_index, const UUID &remote_node_boot_uuid,
56 monotonic_clock::time_point monotonic_remote_time,
57 monotonic_clock::time_point monotonic_event_time,
Austin Schuhf5f99f32022-02-07 20:05:37 -080058 bool reliable,
59 monotonic_clock::time_point monotonic_timestamp_time =
60 monotonic_clock::min_time);
Austin Schuh572924a2021-07-30 22:32:12 -070061 // Queues up a message with the provided boot UUID.
Austin Schuhe46492f2021-07-31 19:49:41 -070062 void QueueMessage(flatbuffers::FlatBufferBuilder *fbb,
63 const UUID &node_boot_uuid,
64 aos::monotonic_clock::time_point now);
Austin Schuhb8bca732021-07-30 22:32:00 -070065
Austin Schuh5e14d842022-01-21 12:02:15 -080066 // Updates the current boot for the source node. This is useful when you want
67 // to queue a message that may trigger a reboot rotation, but then need to
68 // update the remote timestamps.
69 void UpdateBoot(const UUID &source_node_boot_uuid);
70
Austin Schuh572924a2021-07-30 22:32:12 -070071 // Returns the filename of the writer.
Austin Schuh58646e22021-08-23 23:51:46 -070072 std::string_view filename() const {
73 return writer ? writer->filename() : "(closed)";
74 }
Austin Schuhb8bca732021-07-30 22:32:00 -070075
Austin Schuh572924a2021-07-30 22:32:12 -070076 void Close();
Austin Schuhb8bca732021-07-30 22:32:00 -070077
78 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
Austin Schuh572924a2021-07-30 22:32:12 -070079
80 size_t node_index() const { return node_index_; }
81 const UUID &parts_uuid() const { return parts_uuid_; }
82 size_t parts_index() const { return parts_index_; }
83 const Node *node() const { return node_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070084
Austin Schuh72211ae2021-08-05 14:02:30 -070085 // Datastructure used to capture all the information about a remote node.
86 struct State {
87 // Boot UUID of the node.
88 UUID boot_uuid = UUID::Zero();
89 // Timestamp on the remote monotonic clock of the oldest message sent to
90 // node_index_.
91 monotonic_clock::time_point oldest_remote_monotonic_timestamp =
92 monotonic_clock::max_time;
93 // Timestamp on the local monotonic clock of the message in
94 // oldest_remote_monotonic_timestamp.
95 monotonic_clock::time_point oldest_local_monotonic_timestamp =
96 monotonic_clock::max_time;
97 // Timestamp on the remote monotonic clock of the oldest message sent to
98 // node_index_, excluding messages forwarded with time_to_live() == 0.
99 monotonic_clock::time_point oldest_remote_unreliable_monotonic_timestamp =
100 monotonic_clock::max_time;
101 // Timestamp on the local monotonic clock of the message in
102 // oldest_local_unreliable_monotonic_timestamp.
103 monotonic_clock::time_point oldest_local_unreliable_monotonic_timestamp =
104 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -0800105
106 // Timestamp on the remote monotonic clock of the oldest message sent to
107 // node_index_, only including messages forwarded with time_to_live() == 0.
108 monotonic_clock::time_point oldest_remote_reliable_monotonic_timestamp =
109 monotonic_clock::max_time;
110 // Timestamp on the local monotonic clock of the message in
111 // oldest_local_reliable_monotonic_timestamp.
112 monotonic_clock::time_point oldest_local_reliable_monotonic_timestamp =
113 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800114
115 // Timestamp on the remote monotonic clock of the oldest message timestamp
116 // sent back to logger_node_index_. The remote here will be the node this
117 // part is from the perspective of, ie node_index_.
118 monotonic_clock::time_point
119 oldest_logger_remote_unreliable_monotonic_timestamp =
120 monotonic_clock::max_time;
121 // The time on the monotonic clock of the logger when this timestamp made it
122 // back to the logger (logger_node_index_).
123 monotonic_clock::time_point
124 oldest_logger_local_unreliable_monotonic_timestamp =
125 monotonic_clock::max_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700126 };
127
Austin Schuhb8bca732021-07-30 22:32:00 -0700128 private:
Austin Schuhe46492f2021-07-31 19:49:41 -0700129 // Signals that a node has rebooted.
Austin Schuh5e14d842022-01-21 12:02:15 -0800130 void Reboot(const UUID &source_node_boot_uuid);
Austin Schuhe46492f2021-07-31 19:49:41 -0700131
Austin Schuh572924a2021-07-30 22:32:12 -0700132 void QueueHeader(
133 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header);
134
Austin Schuhe46492f2021-07-31 19:49:41 -0700135 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader();
136
Austin Schuh58646e22021-08-23 23:51:46 -0700137 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::min_time;
138
Austin Schuh577610e2021-12-08 12:07:19 -0800139 const Node *node_ = nullptr;
140 size_t node_index_ = 0;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800141 size_t logger_node_index_ = 0;
Austin Schuh572924a2021-07-30 22:32:12 -0700142 LogNamer *log_namer_;
143 UUID parts_uuid_ = UUID::Random();
144 size_t parts_index_ = 0;
145
Austin Schuhb8bca732021-07-30 22:32:00 -0700146 std::function<void(NewDataWriter *)> reopen_;
147 std::function<void(NewDataWriter *)> close_;
Austin Schuh572924a2021-07-30 22:32:12 -0700148 bool header_written_ = false;
Austin Schuhe46492f2021-07-31 19:49:41 -0700149
Austin Schuh72211ae2021-08-05 14:02:30 -0700150 std::vector<State> state_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700151};
152
Austin Schuhcb5601b2020-09-10 15:29:59 -0700153// Interface describing how to name, track, and add headers to log file parts.
154class LogNamer {
155 public:
156 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
157 // being node.
Austin Schuh5b728b72021-06-16 14:57:15 -0700158 LogNamer(const aos::Configuration *configuration, EventLoop *event_loop,
159 const aos::Node *node)
Austin Schuha499cea2021-07-31 19:49:53 -0700160 : event_loop_(event_loop),
Austin Schuh5b728b72021-06-16 14:57:15 -0700161 configuration_(configuration),
162 node_(node),
Austin Schuha499cea2021-07-31 19:49:53 -0700163 logger_node_index_(configuration::GetNodeIndex(configuration_, node_)) {
Austin Schuh73340842021-07-30 22:32:06 -0700164 nodes_.emplace_back(node_);
Austin Schuh73340842021-07-30 22:32:06 -0700165 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700166 virtual ~LogNamer() {}
167
Austin Schuh6bb8a822021-03-31 23:04:39 -0700168 virtual std::string_view base_name() const = 0;
169
170 // Rotate should be called at least once in between calls to set_base_name.
171 // Otherwise temporary files will not be recoverable.
172 // Rotate is called by Logger::RenameLogBase, which is currently the only user
173 // of this method.
174 // Only renaming the folder is supported, not the file base name.
175 virtual void set_base_name(std::string_view base_name) = 0;
176
Brian Silverman87ac0402020-09-17 14:47:01 -0700177 // Returns a writer for writing data from messages on this channel (on the
178 // primary node).
179 //
180 // The returned pointer will stay valid across rotations, but the object it
181 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700182 virtual NewDataWriter *MakeWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700183
Brian Silverman87ac0402020-09-17 14:47:01 -0700184 // Returns a writer for writing timestamps from messages on this channel (on
185 // the primary node).
186 //
187 // The returned pointer will stay valid across rotations, but the object it
188 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700189 virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700190
191 // Returns a writer for writing timestamps delivered over the special
192 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -0700193 // are forwarded back from (to the primary node).
194 //
195 // The returned pointer will stay valid across rotations, but the object it
196 // points to will be assigned to.
Austin Schuh73340842021-07-30 22:32:06 -0700197 virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
198 const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700199
Austin Schuh73340842021-07-30 22:32:06 -0700200 // Rotates all log files for the provided node.
201 virtual void Rotate(const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700202
203 // Returns all the nodes that data is being written for.
204 const std::vector<const Node *> &nodes() const { return nodes_; }
205
206 // Returns the node the logger is running on.
207 const Node *node() const { return node_; }
Austin Schuhe46492f2021-07-31 19:49:41 -0700208 const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; }
209 size_t logger_node_index() const { return logger_node_index_; }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700210
Austin Schuh8c399962020-12-25 21:51:45 -0800211 // Writes out the nested Configuration object to the config file location.
212 virtual void WriteConfiguration(
213 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
214 std::string_view config_sha256) = 0;
215
Austin Schuh73340842021-07-30 22:32:06 -0700216 void SetHeaderTemplate(
217 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) {
218 header_ = std::move(header);
Austin Schuhe46492f2021-07-31 19:49:41 -0700219 logger_node_boot_uuid_ =
220 UUID::FromString(header_.message().logger_node_boot_uuid());
Austin Schuh73340842021-07-30 22:32:06 -0700221 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700222
Austin Schuh58646e22021-08-23 23:51:46 -0700223 void ClearStartTimes() {
224 node_states_.clear();
225 }
226
227 void SetStartTimes(size_t node_index, const UUID &boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700228 monotonic_clock::time_point monotonic_start_time,
229 realtime_clock::time_point realtime_start_time,
230 monotonic_clock::time_point logger_monotonic_start_time,
231 realtime_clock::time_point logger_realtime_start_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700232 VLOG(1) << "Setting node " << node_index << " to start time "
233 << monotonic_start_time << " rt " << realtime_start_time << " UUID "
234 << boot_uuid;
235 NodeState *node_state = GetNodeState(node_index, boot_uuid);
236 node_state->monotonic_start_time = monotonic_start_time;
237 node_state->realtime_start_time = realtime_start_time;
238 node_state->logger_monotonic_start_time = logger_monotonic_start_time;
239 node_state->logger_realtime_start_time = logger_realtime_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700240 }
241
Austin Schuh58646e22021-08-23 23:51:46 -0700242 monotonic_clock::time_point monotonic_start_time(size_t node_index,
243 const UUID &boot_uuid) {
244 DCHECK_NE(boot_uuid, UUID::Zero());
245
246 NodeState *node_state = GetNodeState(node_index, boot_uuid);
247 return node_state->monotonic_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700248 }
249
Austin Schuh73340842021-07-30 22:32:06 -0700250 protected:
Austin Schuh73340842021-07-30 22:32:06 -0700251 // Structure with state per node about times and such.
Austin Schuh73340842021-07-30 22:32:06 -0700252 struct NodeState {
253 // Time when this node started logging.
254 monotonic_clock::time_point monotonic_start_time =
255 monotonic_clock::min_time;
256 realtime_clock::time_point realtime_start_time = realtime_clock::min_time;
257
258 // Corresponding time on the logger node when it started logging.
259 monotonic_clock::time_point logger_monotonic_start_time =
260 monotonic_clock::min_time;
261 realtime_clock::time_point logger_realtime_start_time =
262 realtime_clock::min_time;
Austin Schuh73340842021-07-30 22:32:06 -0700263 };
Austin Schuh58646e22021-08-23 23:51:46 -0700264
265 // Creates a new header by copying fields out of the template and combining
266 // them with the arguments provided.
267 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
268 size_t node_index, const std::vector<NewDataWriter::State> &state,
269 const UUID &parts_uuid, int parts_index);
270
271 EventLoop *event_loop_;
272 const Configuration *const configuration_;
273 const Node *const node_;
274 const size_t logger_node_index_;
275 UUID logger_node_boot_uuid_;
276 std::vector<const Node *> nodes_;
277
278 friend NewDataWriter;
279
280 // Returns the start/stop time state structure for a node and boot. We can
281 // have data from multiple boots, and it makes sense to reuse the start/stop
282 // times if we get data from the same boot again.
283 NodeState *GetNodeState(size_t node_index, const UUID &boot_uuid);
284
285 absl::btree_map<std::pair<size_t, UUID>, NodeState> node_states_;
Austin Schuh73340842021-07-30 22:32:06 -0700286
287 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ =
288 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700289};
290
291// Local log namer is a simple version which only names things
292// "base_name.part#.bfbs" and increments the part number. It doesn't support
293// any other log type.
294class LocalLogNamer : public LogNamer {
295 public:
Austin Schuh5b728b72021-06-16 14:57:15 -0700296 LocalLogNamer(std::string_view base_name, aos::EventLoop *event_loop,
297 const aos::Node *node)
298 : LogNamer(event_loop->configuration(), event_loop, node),
Austin Schuhcb5601b2020-09-10 15:29:59 -0700299 base_name_(base_name),
Austin Schuhf5f99f32022-02-07 20:05:37 -0800300 data_writer_(
301 this, node, event_loop->node(),
302 [this](NewDataWriter *writer) {
303 writer->writer = std::make_unique<DetachedBufferWriter>(
304 absl::StrCat(base_name_, ".part", writer->parts_index(),
305 ".bfbs"),
306 std::make_unique<aos::logger::DummyEncoder>());
307 },
308 [](NewDataWriter * /*writer*/) {}) {}
Austin Schuhb8bca732021-07-30 22:32:00 -0700309
310 LocalLogNamer(const LocalLogNamer &) = delete;
311 LocalLogNamer(LocalLogNamer &&) = delete;
312 LocalLogNamer &operator=(const LocalLogNamer &) = delete;
313 LocalLogNamer &operator=(LocalLogNamer &&) = delete;
314
Brian Silverman0465fcf2020-09-24 00:29:18 -0700315 ~LocalLogNamer() override = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700316
Austin Schuh6bb8a822021-03-31 23:04:39 -0700317 std::string_view base_name() const final { return base_name_; }
318
319 void set_base_name(std::string_view base_name) final {
320 base_name_ = base_name;
321 }
322
Austin Schuhb8bca732021-07-30 22:32:00 -0700323 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700324
Austin Schuh73340842021-07-30 22:32:06 -0700325 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700326
Austin Schuhb8bca732021-07-30 22:32:00 -0700327 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700328
Austin Schuh73340842021-07-30 22:32:06 -0700329 NewDataWriter *MakeForwardedTimestampWriter(const Channel * /*channel*/,
330 const Node * /*node*/) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700331
Austin Schuh8c399962020-12-25 21:51:45 -0800332 void WriteConfiguration(
333 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
334 std::string_view config_sha256) override;
335
Austin Schuhcb5601b2020-09-10 15:29:59 -0700336 private:
Austin Schuh6bb8a822021-03-31 23:04:39 -0700337 std::string base_name_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700338
339 NewDataWriter data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700340};
341
342// Log namer which uses a config and a base name to name a bunch of files.
343class MultiNodeLogNamer : public LogNamer {
344 public:
Austin Schuha499cea2021-07-31 19:49:53 -0700345 MultiNodeLogNamer(std::string_view base_name, EventLoop *event_loop);
Austin Schuh5b728b72021-06-16 14:57:15 -0700346 MultiNodeLogNamer(std::string_view base_name,
347 const Configuration *configuration, EventLoop *event_loop,
348 const Node *node);
Brian Silvermancb805822020-10-06 17:43:35 -0700349 ~MultiNodeLogNamer() override;
350
Austin Schuh6bb8a822021-03-31 23:04:39 -0700351 std::string_view base_name() const final { return base_name_; }
352
353 void set_base_name(std::string_view base_name) final {
354 old_base_name_ = base_name_;
355 base_name_ = base_name;
356 }
Brian Silvermancb805822020-10-06 17:43:35 -0700357
Brian Silverman48deab12020-09-30 18:39:28 -0700358 // If temp_suffix is set, then this will write files under names beginning
359 // with the specified suffix, and then rename them to the desired name after
360 // they are fully written.
361 //
362 // This is useful to enable incremental copying of the log files.
363 //
364 // Defaults to writing directly to the final filename.
Brian Silvermancb805822020-10-06 17:43:35 -0700365 void set_temp_suffix(std::string_view temp_suffix) {
366 temp_suffix_ = temp_suffix;
367 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700368
Brian Silvermancb805822020-10-06 17:43:35 -0700369 // Sets the function for creating encoders.
370 //
371 // Defaults to just creating DummyEncoders.
372 void set_encoder_factory(
373 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory) {
374 encoder_factory_ = std::move(encoder_factory);
375 }
376
377 // Sets an additional file extension.
378 //
379 // Defaults to nothing.
380 void set_extension(std::string_view extension) { extension_ = extension; }
Brian Silverman1f345222020-09-24 21:14:48 -0700381
Brian Silvermana621f522020-09-30 16:52:43 -0700382 // A list of all the filenames we've written.
383 //
384 // This only includes the part after base_name().
385 const std::vector<std::string> &all_filenames() const {
386 return all_filenames_;
387 }
388
Austin Schuh73340842021-07-30 22:32:06 -0700389 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700390
Austin Schuh8c399962020-12-25 21:51:45 -0800391 void WriteConfiguration(
392 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
393 std::string_view config_sha256) override;
394
Austin Schuhb8bca732021-07-30 22:32:00 -0700395 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700396
Austin Schuhb8bca732021-07-30 22:32:00 -0700397 NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
Austin Schuh73340842021-07-30 22:32:06 -0700398 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700399
Austin Schuhb8bca732021-07-30 22:32:00 -0700400 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700401
Brian Silverman0465fcf2020-09-24 00:29:18 -0700402 // Indicates that at least one file ran out of space. Once this happens, we
403 // stop trying to open new files, to avoid writing any files with holes from
404 // previous parts.
405 //
406 // Besides this function, this object will silently stop logging data when
407 // this occurs. If you want to ensure log files are complete, you must call
408 // this method.
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700409 bool ran_out_of_space() const {
410 return accumulate_data_writers<bool>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700411 ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700412 return x ||
413 (data_writer.writer && data_writer.writer->ran_out_of_space());
414 });
415 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700416
Brian Silverman1f345222020-09-24 21:14:48 -0700417 // Returns the maximum total_bytes() value for all existing
418 // DetachedBufferWriters.
419 //
420 // Returns 0 if no files are open.
421 size_t maximum_total_bytes() const {
Brian Silvermancb805822020-10-06 17:43:35 -0700422 return accumulate_data_writers<size_t>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700423 0, [](size_t x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700424 return std::max(x, data_writer.writer->total_bytes());
425 });
Brian Silverman1f345222020-09-24 21:14:48 -0700426 }
427
Brian Silverman0465fcf2020-09-24 00:29:18 -0700428 // Closes all existing log files. No more data may be written after this.
429 //
430 // This may set ran_out_of_space().
431 void Close();
432
Brian Silvermancb805822020-10-06 17:43:35 -0700433 // Accessors for various statistics. See the identically-named methods in
434 // DetachedBufferWriter for documentation. These are aggregated across all
435 // past and present DetachedBufferWriters.
436 std::chrono::nanoseconds max_write_time() const {
437 return accumulate_data_writers(
438 max_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700439 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700440 return std::max(x, data_writer.writer->max_write_time());
441 });
442 }
443 int max_write_time_bytes() const {
444 return std::get<0>(accumulate_data_writers(
445 std::make_tuple(max_write_time_bytes_, max_write_time_),
446 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700447 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700448 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
449 return std::make_tuple(data_writer.writer->max_write_time_bytes(),
450 data_writer.writer->max_write_time());
451 }
452 return x;
453 }));
454 }
455 int max_write_time_messages() const {
456 return std::get<0>(accumulate_data_writers(
457 std::make_tuple(max_write_time_messages_, max_write_time_),
458 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700459 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700460 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
461 return std::make_tuple(
462 data_writer.writer->max_write_time_messages(),
463 data_writer.writer->max_write_time());
464 }
465 return x;
466 }));
467 }
468 std::chrono::nanoseconds total_write_time() const {
469 return accumulate_data_writers(
470 total_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700471 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700472 return x + data_writer.writer->total_write_time();
473 });
474 }
475 int total_write_count() const {
476 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700477 total_write_count_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700478 return x + data_writer.writer->total_write_count();
479 });
480 }
481 int total_write_messages() const {
482 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700483 total_write_messages_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700484 return x + data_writer.writer->total_write_messages();
485 });
486 }
487 int total_write_bytes() const {
488 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700489 total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700490 return x + data_writer.writer->total_write_bytes();
491 });
492 }
493
494 void ResetStatistics();
495
Austin Schuhcb5601b2020-09-10 15:29:59 -0700496 private:
Austin Schuhcb5601b2020-09-10 15:29:59 -0700497 // Opens up a writer for timestamps forwarded back.
498 void OpenForwardedTimestampWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700499 NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700500
501 // Opens up a writer for remote data.
Austin Schuhb8bca732021-07-30 22:32:00 -0700502 void OpenWriter(const Channel *channel, NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700503
504 // Opens the main data writer file for this node responsible for data_writer_.
Brian Silvermana621f522020-09-30 16:52:43 -0700505 void OpenDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700506
Brian Silvermana621f522020-09-30 16:52:43 -0700507 void CreateBufferWriter(std::string_view path,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700508 std::unique_ptr<DetachedBufferWriter> *destination);
509
Brian Silverman48deab12020-09-30 18:39:28 -0700510 void RenameTempFile(DetachedBufferWriter *destination);
511
Brian Silvermancb805822020-10-06 17:43:35 -0700512 void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700513
Brian Silvermancb805822020-10-06 17:43:35 -0700514 // A version of std::accumulate which operates over all of our DataWriters.
515 template <typename T, typename BinaryOperation>
516 T accumulate_data_writers(T t, BinaryOperation op) const {
Austin Schuhb8bca732021-07-30 22:32:00 -0700517 for (const std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700518 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800519 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700520 t = op(std::move(t), data_writer.second);
521 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700522 if (data_writer_) {
523 t = op(std::move(t), *data_writer_);
Brian Silvermancb805822020-10-06 17:43:35 -0700524 }
525 return t;
526 }
527
Austin Schuh6bb8a822021-03-31 23:04:39 -0700528 std::string base_name_;
529 std::string old_base_name_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700530
Brian Silverman0465fcf2020-09-24 00:29:18 -0700531 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700532 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700533
Brian Silvermancb805822020-10-06 17:43:35 -0700534 std::string temp_suffix_;
535 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory_ =
536 []() { return std::make_unique<DummyEncoder>(); };
537 std::string extension_;
538
539 // Storage for statistics from previously-rotated DetachedBufferWriters.
540 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
541 int max_write_time_bytes_ = -1;
542 int max_write_time_messages_ = -1;
543 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
544 int total_write_count_ = 0;
545 int total_write_messages_ = 0;
546 int total_write_bytes_ = 0;
547
Austin Schuhcb5601b2020-09-10 15:29:59 -0700548 // File to write both delivery timestamps and local data to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700549 std::unique_ptr<NewDataWriter> data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700550
Austin Schuhb8bca732021-07-30 22:32:00 -0700551 std::map<const Channel *, NewDataWriter> data_writers_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700552};
553
554} // namespace logger
555} // namespace aos
556
557#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_