blob: 8c62559c9ac3fa6a4e1c8ac6561cd7fb4d284c99 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "flatbuffers/flatbuffers.h"
12
Austin Schuhcb5601b2020-09-10 15:29:59 -070013#include "aos/events/logging/logfile_utils.h"
14#include "aos/events/logging/logger_generated.h"
Austin Schuh4385b142021-03-14 21:31:13 -070015#include "aos/uuid.h"
Austin Schuhcb5601b2020-09-10 15:29:59 -070016
17namespace aos {
18namespace logger {
19
Austin Schuh572924a2021-07-30 22:32:12 -070020class LogNamer;
21
Austin Schuhb8bca732021-07-30 22:32:00 -070022// TODO(austin): Rename this back to DataWriter once all other callers are of
23// the old DataWriter.
Austin Schuh572924a2021-07-30 22:32:12 -070024//
25// Class to manage writing data to log files. This lets us track which boot the
26// written header has in it, and if the header has been written or not.
Austin Schuh58646e22021-08-23 23:51:46 -070027//
28// The design of this class is that instead of being notified when any of the
29// header data changes, it polls and owns that decision. This makes it much
30// harder to write corrupted data. If that becomes a performance problem, we
31// can DCHECK and take it out of production binaries.
Austin Schuhb8bca732021-07-30 22:32:00 -070032class NewDataWriter {
33 public:
34 // Constructs a NewDataWriter.
Austin Schuh572924a2021-07-30 22:32:12 -070035 // log_namer is the log namer which holds the config and any other data we
36 // need for our header.
37 // node is the node whom's prespective we are logging from.
Austin Schuhb8bca732021-07-30 22:32:00 -070038 // reopen is called whenever a file needs to be reopened.
39 // close is called to close that file and extract any statistics.
Austin Schuhf5f99f32022-02-07 20:05:37 -080040 NewDataWriter(LogNamer *log_namer, const Node *node, const Node *logger_node,
Austin Schuh572924a2021-07-30 22:32:12 -070041 std::function<void(NewDataWriter *)> reopen,
Austin Schuh48d10d62022-10-16 22:19:23 -070042 std::function<void(NewDataWriter *)> close,
43 size_t max_message_size);
44
45 void UpdateMaxMessageSize(size_t new_size) {
46 if (new_size > max_message_size_) {
Alexei Strotsbc082d82023-05-03 08:43:42 -070047 CHECK(!header_written_) << ": Tried to update to " << new_size << ", was "
48 << max_message_size_ << " for " << name();
Austin Schuh48d10d62022-10-16 22:19:23 -070049 max_message_size_ = new_size;
50 }
51 }
52 size_t max_message_size() const { return max_message_size_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070053
54 NewDataWriter(NewDataWriter &&other) = default;
55 aos::logger::NewDataWriter &operator=(NewDataWriter &&other) = default;
56 NewDataWriter(const NewDataWriter &) = delete;
57 void operator=(const NewDataWriter &) = delete;
58
Austin Schuh572924a2021-07-30 22:32:12 -070059 ~NewDataWriter();
Austin Schuhb8bca732021-07-30 22:32:00 -070060
Austin Schuh572924a2021-07-30 22:32:12 -070061 // Rotates the log file, delaying writing the new header until data arrives.
62 void Rotate();
Austin Schuhb8bca732021-07-30 22:32:00 -070063
Austin Schuhf5f99f32022-02-07 20:05:37 -080064 // Updates all the metadata in the log file about the remote node which this
65 // message is from.
Austin Schuh72211ae2021-08-05 14:02:30 -070066 void UpdateRemote(size_t remote_node_index, const UUID &remote_node_boot_uuid,
67 monotonic_clock::time_point monotonic_remote_time,
68 monotonic_clock::time_point monotonic_event_time,
Austin Schuhf5f99f32022-02-07 20:05:37 -080069 bool reliable,
70 monotonic_clock::time_point monotonic_timestamp_time =
71 monotonic_clock::min_time);
Austin Schuh48d10d62022-10-16 22:19:23 -070072 // Coppies a message with the provided boot UUID.
73 void CopyMessage(DataEncoder::Copier *coppier,
74 const UUID &source_node_boot_uuid,
75 aos::monotonic_clock::time_point now);
Austin Schuhb8bca732021-07-30 22:32:00 -070076
Austin Schuh5e14d842022-01-21 12:02:15 -080077 // Updates the current boot for the source node. This is useful when you want
78 // to queue a message that may trigger a reboot rotation, but then need to
79 // update the remote timestamps.
80 void UpdateBoot(const UUID &source_node_boot_uuid);
81
Alexei Strotsbc082d82023-05-03 08:43:42 -070082 // Returns the name of the writer. It may be a filename, but assume it is not.
83 std::string_view name() const { return writer ? writer->name() : "(closed)"; }
Austin Schuhb8bca732021-07-30 22:32:00 -070084
Austin Schuh572924a2021-07-30 22:32:12 -070085 void Close();
Austin Schuhb8bca732021-07-30 22:32:00 -070086
87 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
Austin Schuh572924a2021-07-30 22:32:12 -070088
89 size_t node_index() const { return node_index_; }
90 const UUID &parts_uuid() const { return parts_uuid_; }
91 size_t parts_index() const { return parts_index_; }
92 const Node *node() const { return node_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070093
Austin Schuh72211ae2021-08-05 14:02:30 -070094 // Datastructure used to capture all the information about a remote node.
95 struct State {
96 // Boot UUID of the node.
97 UUID boot_uuid = UUID::Zero();
98 // Timestamp on the remote monotonic clock of the oldest message sent to
99 // node_index_.
100 monotonic_clock::time_point oldest_remote_monotonic_timestamp =
101 monotonic_clock::max_time;
102 // Timestamp on the local monotonic clock of the message in
103 // oldest_remote_monotonic_timestamp.
104 monotonic_clock::time_point oldest_local_monotonic_timestamp =
105 monotonic_clock::max_time;
106 // Timestamp on the remote monotonic clock of the oldest message sent to
107 // node_index_, excluding messages forwarded with time_to_live() == 0.
108 monotonic_clock::time_point oldest_remote_unreliable_monotonic_timestamp =
109 monotonic_clock::max_time;
110 // Timestamp on the local monotonic clock of the message in
111 // oldest_local_unreliable_monotonic_timestamp.
112 monotonic_clock::time_point oldest_local_unreliable_monotonic_timestamp =
113 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -0800114
115 // Timestamp on the remote monotonic clock of the oldest message sent to
116 // node_index_, only including messages forwarded with time_to_live() == 0.
117 monotonic_clock::time_point oldest_remote_reliable_monotonic_timestamp =
118 monotonic_clock::max_time;
119 // Timestamp on the local monotonic clock of the message in
120 // oldest_local_reliable_monotonic_timestamp.
121 monotonic_clock::time_point oldest_local_reliable_monotonic_timestamp =
122 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800123
124 // Timestamp on the remote monotonic clock of the oldest message timestamp
125 // sent back to logger_node_index_. The remote here will be the node this
126 // part is from the perspective of, ie node_index_.
127 monotonic_clock::time_point
128 oldest_logger_remote_unreliable_monotonic_timestamp =
129 monotonic_clock::max_time;
130 // The time on the monotonic clock of the logger when this timestamp made it
131 // back to the logger (logger_node_index_).
132 monotonic_clock::time_point
133 oldest_logger_local_unreliable_monotonic_timestamp =
134 monotonic_clock::max_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700135 };
136
Austin Schuhb8bca732021-07-30 22:32:00 -0700137 private:
Austin Schuhe46492f2021-07-31 19:49:41 -0700138 // Signals that a node has rebooted.
Austin Schuh5e14d842022-01-21 12:02:15 -0800139 void Reboot(const UUID &source_node_boot_uuid);
Austin Schuhe46492f2021-07-31 19:49:41 -0700140
Austin Schuh572924a2021-07-30 22:32:12 -0700141 void QueueHeader(
142 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header);
143
Austin Schuhe46492f2021-07-31 19:49:41 -0700144 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader();
145
Austin Schuh58646e22021-08-23 23:51:46 -0700146 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::min_time;
147
Austin Schuh577610e2021-12-08 12:07:19 -0800148 const Node *node_ = nullptr;
149 size_t node_index_ = 0;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800150 size_t logger_node_index_ = 0;
Austin Schuh572924a2021-07-30 22:32:12 -0700151 LogNamer *log_namer_;
152 UUID parts_uuid_ = UUID::Random();
153 size_t parts_index_ = 0;
154
Austin Schuhb8bca732021-07-30 22:32:00 -0700155 std::function<void(NewDataWriter *)> reopen_;
156 std::function<void(NewDataWriter *)> close_;
Austin Schuh572924a2021-07-30 22:32:12 -0700157 bool header_written_ = false;
Austin Schuhe46492f2021-07-31 19:49:41 -0700158
Austin Schuh72211ae2021-08-05 14:02:30 -0700159 std::vector<State> state_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700160
161 size_t max_message_size_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700162};
163
Austin Schuhcb5601b2020-09-10 15:29:59 -0700164// Interface describing how to name, track, and add headers to log file parts.
165class LogNamer {
166 public:
167 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
168 // being node.
Austin Schuh5b728b72021-06-16 14:57:15 -0700169 LogNamer(const aos::Configuration *configuration, EventLoop *event_loop,
170 const aos::Node *node)
Austin Schuha499cea2021-07-31 19:49:53 -0700171 : event_loop_(event_loop),
Austin Schuh5b728b72021-06-16 14:57:15 -0700172 configuration_(configuration),
173 node_(node),
Austin Schuha499cea2021-07-31 19:49:53 -0700174 logger_node_index_(configuration::GetNodeIndex(configuration_, node_)) {
Austin Schuh73340842021-07-30 22:32:06 -0700175 nodes_.emplace_back(node_);
Austin Schuh73340842021-07-30 22:32:06 -0700176 }
Alexei Strots01395492023-03-20 13:59:56 -0700177 virtual ~LogNamer() = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700178
Brian Silverman87ac0402020-09-17 14:47:01 -0700179 // Returns a writer for writing data from messages on this channel (on the
180 // primary node).
181 //
182 // The returned pointer will stay valid across rotations, but the object it
183 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700184 virtual NewDataWriter *MakeWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700185
Brian Silverman87ac0402020-09-17 14:47:01 -0700186 // Returns a writer for writing timestamps from messages on this channel (on
187 // the primary node).
188 //
189 // The returned pointer will stay valid across rotations, but the object it
190 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700191 virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700192
193 // Returns a writer for writing timestamps delivered over the special
194 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -0700195 // are forwarded back from (to the primary node).
196 //
197 // The returned pointer will stay valid across rotations, but the object it
198 // points to will be assigned to.
Austin Schuh73340842021-07-30 22:32:06 -0700199 virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
200 const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700201
Austin Schuh73340842021-07-30 22:32:06 -0700202 // Rotates all log files for the provided node.
203 virtual void Rotate(const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700204
205 // Returns all the nodes that data is being written for.
206 const std::vector<const Node *> &nodes() const { return nodes_; }
207
Austin Schuh08dba8f2023-05-01 08:29:30 -0700208 // Closes all existing log data writers. No more data may be written after
209 // this.
210 virtual WriteCode Close() = 0;
211
Austin Schuhcb5601b2020-09-10 15:29:59 -0700212 // Returns the node the logger is running on.
213 const Node *node() const { return node_; }
Austin Schuhe46492f2021-07-31 19:49:41 -0700214 const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; }
215 size_t logger_node_index() const { return logger_node_index_; }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700216
Austin Schuh8c399962020-12-25 21:51:45 -0800217 // Writes out the nested Configuration object to the config file location.
218 virtual void WriteConfiguration(
219 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
220 std::string_view config_sha256) = 0;
221
Austin Schuh73340842021-07-30 22:32:06 -0700222 void SetHeaderTemplate(
223 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) {
224 header_ = std::move(header);
Austin Schuhe46492f2021-07-31 19:49:41 -0700225 logger_node_boot_uuid_ =
226 UUID::FromString(header_.message().logger_node_boot_uuid());
Austin Schuh73340842021-07-30 22:32:06 -0700227 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700228
Austin Schuh60e77942022-05-16 17:48:24 -0700229 void ClearStartTimes() { node_states_.clear(); }
Austin Schuh58646e22021-08-23 23:51:46 -0700230
231 void SetStartTimes(size_t node_index, const UUID &boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700232 monotonic_clock::time_point monotonic_start_time,
233 realtime_clock::time_point realtime_start_time,
234 monotonic_clock::time_point logger_monotonic_start_time,
235 realtime_clock::time_point logger_realtime_start_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700236 VLOG(1) << "Setting node " << node_index << " to start time "
237 << monotonic_start_time << " rt " << realtime_start_time << " UUID "
238 << boot_uuid;
239 NodeState *node_state = GetNodeState(node_index, boot_uuid);
240 node_state->monotonic_start_time = monotonic_start_time;
241 node_state->realtime_start_time = realtime_start_time;
242 node_state->logger_monotonic_start_time = logger_monotonic_start_time;
243 node_state->logger_realtime_start_time = logger_realtime_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700244 }
245
Austin Schuh58646e22021-08-23 23:51:46 -0700246 monotonic_clock::time_point monotonic_start_time(size_t node_index,
247 const UUID &boot_uuid) {
248 DCHECK_NE(boot_uuid, UUID::Zero());
249
250 NodeState *node_state = GetNodeState(node_index, boot_uuid);
251 return node_state->monotonic_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700252 }
253
Austin Schuh73340842021-07-30 22:32:06 -0700254 protected:
Austin Schuh73340842021-07-30 22:32:06 -0700255 // Structure with state per node about times and such.
Austin Schuh73340842021-07-30 22:32:06 -0700256 struct NodeState {
257 // Time when this node started logging.
258 monotonic_clock::time_point monotonic_start_time =
259 monotonic_clock::min_time;
260 realtime_clock::time_point realtime_start_time = realtime_clock::min_time;
261
262 // Corresponding time on the logger node when it started logging.
263 monotonic_clock::time_point logger_monotonic_start_time =
264 monotonic_clock::min_time;
265 realtime_clock::time_point logger_realtime_start_time =
266 realtime_clock::min_time;
Austin Schuh73340842021-07-30 22:32:06 -0700267 };
Austin Schuh58646e22021-08-23 23:51:46 -0700268
269 // Creates a new header by copying fields out of the template and combining
270 // them with the arguments provided.
271 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
272 size_t node_index, const std::vector<NewDataWriter::State> &state,
273 const UUID &parts_uuid, int parts_index);
274
275 EventLoop *event_loop_;
276 const Configuration *const configuration_;
277 const Node *const node_;
278 const size_t logger_node_index_;
279 UUID logger_node_boot_uuid_;
280 std::vector<const Node *> nodes_;
281
282 friend NewDataWriter;
283
284 // Returns the start/stop time state structure for a node and boot. We can
285 // have data from multiple boots, and it makes sense to reuse the start/stop
286 // times if we get data from the same boot again.
287 NodeState *GetNodeState(size_t node_index, const UUID &boot_uuid);
288
289 absl::btree_map<std::pair<size_t, UUID>, NodeState> node_states_;
Austin Schuh73340842021-07-30 22:32:06 -0700290
291 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ =
292 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700293};
294
Alexei Strots01395492023-03-20 13:59:56 -0700295// Log namer which uses a config to name a bunch of files.
Austin Schuhcb5601b2020-09-10 15:29:59 -0700296class MultiNodeLogNamer : public LogNamer {
297 public:
Alexei Strotscaf17d32023-04-03 22:31:11 -0700298 MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend,
Alexei Strots01395492023-03-20 13:59:56 -0700299 EventLoop *event_loop);
Alexei Strotscaf17d32023-04-03 22:31:11 -0700300 MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend,
Austin Schuh5b728b72021-06-16 14:57:15 -0700301 const Configuration *configuration, EventLoop *event_loop,
302 const Node *node);
Brian Silvermancb805822020-10-06 17:43:35 -0700303 ~MultiNodeLogNamer() override;
304
Austin Schuh48d10d62022-10-16 22:19:23 -0700305 // Sets the function for creating encoders. The argument is the max message
306 // size (including headers) that will be written into this encoder.
Brian Silvermancb805822020-10-06 17:43:35 -0700307 //
308 // Defaults to just creating DummyEncoders.
309 void set_encoder_factory(
Austin Schuh48d10d62022-10-16 22:19:23 -0700310 std::function<std::unique_ptr<DataEncoder>(size_t)> encoder_factory) {
Brian Silvermancb805822020-10-06 17:43:35 -0700311 encoder_factory_ = std::move(encoder_factory);
312 }
313
314 // Sets an additional file extension.
315 //
316 // Defaults to nothing.
317 void set_extension(std::string_view extension) { extension_ = extension; }
Brian Silverman1f345222020-09-24 21:14:48 -0700318
Brian Silvermana621f522020-09-30 16:52:43 -0700319 // A list of all the filenames we've written.
320 //
321 // This only includes the part after base_name().
322 const std::vector<std::string> &all_filenames() const {
323 return all_filenames_;
324 }
325
Austin Schuh73340842021-07-30 22:32:06 -0700326 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700327
Austin Schuh8c399962020-12-25 21:51:45 -0800328 void WriteConfiguration(
329 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
330 std::string_view config_sha256) override;
331
Austin Schuhb8bca732021-07-30 22:32:00 -0700332 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700333
Austin Schuhb8bca732021-07-30 22:32:00 -0700334 NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
Austin Schuh73340842021-07-30 22:32:06 -0700335 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700336
Austin Schuhb8bca732021-07-30 22:32:00 -0700337 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700338
Brian Silverman0465fcf2020-09-24 00:29:18 -0700339 // Indicates that at least one file ran out of space. Once this happens, we
340 // stop trying to open new files, to avoid writing any files with holes from
341 // previous parts.
342 //
343 // Besides this function, this object will silently stop logging data when
344 // this occurs. If you want to ensure log files are complete, you must call
345 // this method.
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700346 bool ran_out_of_space() const {
347 return accumulate_data_writers<bool>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700348 ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700349 return x ||
350 (data_writer.writer && data_writer.writer->ran_out_of_space());
351 });
352 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700353
Brian Silverman1f345222020-09-24 21:14:48 -0700354 // Returns the maximum total_bytes() value for all existing
355 // DetachedBufferWriters.
356 //
357 // Returns 0 if no files are open.
358 size_t maximum_total_bytes() const {
Brian Silvermancb805822020-10-06 17:43:35 -0700359 return accumulate_data_writers<size_t>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700360 0, [](size_t x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700361 return std::max(x, data_writer.writer->total_bytes());
362 });
Brian Silverman1f345222020-09-24 21:14:48 -0700363 }
364
Brian Silverman0465fcf2020-09-24 00:29:18 -0700365 // Closes all existing log files. No more data may be written after this.
366 //
367 // This may set ran_out_of_space().
Austin Schuh08dba8f2023-05-01 08:29:30 -0700368 WriteCode Close() override;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700369
Brian Silvermancb805822020-10-06 17:43:35 -0700370 // Accessors for various statistics. See the identically-named methods in
371 // DetachedBufferWriter for documentation. These are aggregated across all
372 // past and present DetachedBufferWriters.
373 std::chrono::nanoseconds max_write_time() const {
374 return accumulate_data_writers(
375 max_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700376 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700377 return std::max(
378 x, data_writer.writer->WriteStatistics()->max_write_time());
Brian Silvermancb805822020-10-06 17:43:35 -0700379 });
380 }
381 int max_write_time_bytes() const {
382 return std::get<0>(accumulate_data_writers(
383 std::make_tuple(max_write_time_bytes_, max_write_time_),
384 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700385 const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700386 if (data_writer.writer->WriteStatistics()->max_write_time() >
387 std::get<1>(x)) {
388 return std::make_tuple(
389 data_writer.writer->WriteStatistics()->max_write_time_bytes(),
390 data_writer.writer->WriteStatistics()->max_write_time());
Brian Silvermancb805822020-10-06 17:43:35 -0700391 }
392 return x;
393 }));
394 }
395 int max_write_time_messages() const {
396 return std::get<0>(accumulate_data_writers(
397 std::make_tuple(max_write_time_messages_, max_write_time_),
398 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700399 const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700400 if (data_writer.writer->WriteStatistics()->max_write_time() >
401 std::get<1>(x)) {
Brian Silvermancb805822020-10-06 17:43:35 -0700402 return std::make_tuple(
Alexei Strots01395492023-03-20 13:59:56 -0700403 data_writer.writer->WriteStatistics()
404 ->max_write_time_messages(),
405 data_writer.writer->WriteStatistics()->max_write_time());
Brian Silvermancb805822020-10-06 17:43:35 -0700406 }
407 return x;
408 }));
409 }
410 std::chrono::nanoseconds total_write_time() const {
411 return accumulate_data_writers(
412 total_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700413 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700414 return x + data_writer.writer->WriteStatistics()->total_write_time();
Brian Silvermancb805822020-10-06 17:43:35 -0700415 });
416 }
417 int total_write_count() const {
418 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700419 total_write_count_, [](int x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700420 return x + data_writer.writer->WriteStatistics()->total_write_count();
Brian Silvermancb805822020-10-06 17:43:35 -0700421 });
422 }
423 int total_write_messages() const {
424 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700425 total_write_messages_, [](int x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700426 return x +
427 data_writer.writer->WriteStatistics()->total_write_messages();
Brian Silvermancb805822020-10-06 17:43:35 -0700428 });
429 }
430 int total_write_bytes() const {
431 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700432 total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700433 return x + data_writer.writer->WriteStatistics()->total_write_bytes();
Brian Silvermancb805822020-10-06 17:43:35 -0700434 });
435 }
436
437 void ResetStatistics();
438
Alexei Strotscaf17d32023-04-03 22:31:11 -0700439 protected:
440 // TODO (Alexei): consider to move ownership of log_namer to concrete sub
441 // class and make log_backend_ raw pointer.
442 LogBackend *log_backend() { return log_backend_.get(); }
443 const LogBackend *log_backend() const { return log_backend_.get(); }
444
Austin Schuhcb5601b2020-09-10 15:29:59 -0700445 private:
Austin Schuhcb5601b2020-09-10 15:29:59 -0700446 // Opens up a writer for timestamps forwarded back.
447 void OpenForwardedTimestampWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700448 NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700449
450 // Opens up a writer for remote data.
Austin Schuhb8bca732021-07-30 22:32:00 -0700451 void OpenWriter(const Channel *channel, NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700452
453 // Opens the main data writer file for this node responsible for data_writer_.
Austin Schuh48d10d62022-10-16 22:19:23 -0700454 void MakeDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700455
Austin Schuh48d10d62022-10-16 22:19:23 -0700456 void CreateBufferWriter(std::string_view path, size_t max_message_size,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700457 std::unique_ptr<DetachedBufferWriter> *destination);
458
Brian Silvermancb805822020-10-06 17:43:35 -0700459 void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700460
Brian Silvermancb805822020-10-06 17:43:35 -0700461 // A version of std::accumulate which operates over all of our DataWriters.
462 template <typename T, typename BinaryOperation>
463 T accumulate_data_writers(T t, BinaryOperation op) const {
Austin Schuhb8bca732021-07-30 22:32:00 -0700464 for (const std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700465 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800466 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700467 t = op(std::move(t), data_writer.second);
468 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700469 if (data_writer_) {
470 t = op(std::move(t), *data_writer_);
Brian Silvermancb805822020-10-06 17:43:35 -0700471 }
472 return t;
473 }
474
Alexei Strotscaf17d32023-04-03 22:31:11 -0700475 std::unique_ptr<LogBackend> log_backend_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700476
Brian Silverman0465fcf2020-09-24 00:29:18 -0700477 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700478 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700479
Austin Schuh8bdfc492023-02-11 12:53:13 -0800480 std::function<std::unique_ptr<DataEncoder>(size_t)> encoder_factory_;
Brian Silvermancb805822020-10-06 17:43:35 -0700481 std::string extension_;
482
483 // Storage for statistics from previously-rotated DetachedBufferWriters.
484 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
485 int max_write_time_bytes_ = -1;
486 int max_write_time_messages_ = -1;
487 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
488 int total_write_count_ = 0;
489 int total_write_messages_ = 0;
490 int total_write_bytes_ = 0;
491
Austin Schuhcb5601b2020-09-10 15:29:59 -0700492 // File to write both delivery timestamps and local data to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700493 std::unique_ptr<NewDataWriter> data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700494
Austin Schuhb8bca732021-07-30 22:32:00 -0700495 std::map<const Channel *, NewDataWriter> data_writers_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700496};
497
Alexei Strots01395492023-03-20 13:59:56 -0700498// This is specialized log namer that deals with directory centric log events.
499class MultiNodeFilesLogNamer : public MultiNodeLogNamer {
500 public:
501 MultiNodeFilesLogNamer(std::string_view base_name, EventLoop *event_loop)
colleen61276dc2023-06-01 09:23:29 -0700502 : MultiNodeLogNamer(
503 std::make_unique<RenamableFileBackend>(base_name, false),
504 event_loop) {}
Alexei Strots01395492023-03-20 13:59:56 -0700505
506 MultiNodeFilesLogNamer(std::string_view base_name,
507 const Configuration *configuration,
508 EventLoop *event_loop, const Node *node)
colleen61276dc2023-06-01 09:23:29 -0700509 : MultiNodeLogNamer(
510 std::make_unique<RenamableFileBackend>(base_name, false),
511 configuration, event_loop, node) {}
512
513 MultiNodeFilesLogNamer(EventLoop *event_loop,
514 std::unique_ptr<RenamableFileBackend> backend)
515 : MultiNodeLogNamer(std::move(backend), event_loop) {}
516
Alexei Strots01395492023-03-20 13:59:56 -0700517 ~MultiNodeFilesLogNamer() override = default;
Alexei Strotscaf17d32023-04-03 22:31:11 -0700518
519 std::string_view base_name() const {
520 return renamable_file_backend()->base_name();
521 }
522
523 // Rotate should be called at least once in between calls to set_base_name.
524 // Otherwise, temporary files will not be recoverable.
525 // Rotate is called by Logger::RenameLogBase, which is currently the only user
526 // of this method.
527 // Only renaming the folder is supported, not the file base name.
528 void set_base_name(std::string_view base_name) {
529 renamable_file_backend()->RenameLogBase(base_name);
530 }
531
532 // When enabled, this will write files under names beginning
533 // with the .tmp suffix, and then rename them to the desired name after
534 // they are fully written.
535 //
536 // This is useful to enable incremental copying of the log files.
537 //
538 // Defaults to writing directly to the final filename.
539 void EnableTempFiles() { renamable_file_backend()->EnableTempFiles(); }
540
541 private:
542 RenamableFileBackend *renamable_file_backend() {
543 return reinterpret_cast<RenamableFileBackend *>(log_backend());
544 }
545 const RenamableFileBackend *renamable_file_backend() const {
546 return reinterpret_cast<const RenamableFileBackend *>(log_backend());
547 }
Alexei Strots01395492023-03-20 13:59:56 -0700548};
549
Austin Schuhcb5601b2020-09-10 15:29:59 -0700550} // namespace logger
551} // namespace aos
552
553#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_