blob: 8842f4e8b57c1a8c2e18971ba195013e275cd04e [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,
Austin Schuh48d10d62022-10-16 22:19:23 -070041 std::function<void(NewDataWriter *)> close,
42 size_t max_message_size);
43
44 void UpdateMaxMessageSize(size_t new_size) {
45 if (new_size > max_message_size_) {
Alexei Strotsbc082d82023-05-03 08:43:42 -070046 CHECK(!header_written_) << ": Tried to update to " << new_size << ", was "
47 << max_message_size_ << " for " << name();
Austin Schuh48d10d62022-10-16 22:19:23 -070048 max_message_size_ = new_size;
49 }
50 }
51 size_t max_message_size() const { return max_message_size_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070052
53 NewDataWriter(NewDataWriter &&other) = default;
54 aos::logger::NewDataWriter &operator=(NewDataWriter &&other) = default;
55 NewDataWriter(const NewDataWriter &) = delete;
56 void operator=(const NewDataWriter &) = delete;
57
Austin Schuh572924a2021-07-30 22:32:12 -070058 ~NewDataWriter();
Austin Schuhb8bca732021-07-30 22:32:00 -070059
Austin Schuh572924a2021-07-30 22:32:12 -070060 // Rotates the log file, delaying writing the new header until data arrives.
61 void Rotate();
Austin Schuhb8bca732021-07-30 22:32:00 -070062
Austin Schuhf5f99f32022-02-07 20:05:37 -080063 // Updates all the metadata in the log file about the remote node which this
64 // message is from.
Austin Schuh72211ae2021-08-05 14:02:30 -070065 void UpdateRemote(size_t remote_node_index, const UUID &remote_node_boot_uuid,
66 monotonic_clock::time_point monotonic_remote_time,
67 monotonic_clock::time_point monotonic_event_time,
Austin Schuhf5f99f32022-02-07 20:05:37 -080068 bool reliable,
69 monotonic_clock::time_point monotonic_timestamp_time =
70 monotonic_clock::min_time);
Austin Schuh48d10d62022-10-16 22:19:23 -070071 // Coppies a message with the provided boot UUID.
72 void CopyMessage(DataEncoder::Copier *coppier,
73 const UUID &source_node_boot_uuid,
74 aos::monotonic_clock::time_point now);
Austin Schuhb8bca732021-07-30 22:32:00 -070075
Austin Schuh5e14d842022-01-21 12:02:15 -080076 // Updates the current boot for the source node. This is useful when you want
77 // to queue a message that may trigger a reboot rotation, but then need to
78 // update the remote timestamps.
79 void UpdateBoot(const UUID &source_node_boot_uuid);
80
Alexei Strotsbc082d82023-05-03 08:43:42 -070081 // Returns the name of the writer. It may be a filename, but assume it is not.
82 std::string_view name() const { return writer ? writer->name() : "(closed)"; }
Austin Schuhb8bca732021-07-30 22:32:00 -070083
Austin Schuh572924a2021-07-30 22:32:12 -070084 void Close();
Austin Schuhb8bca732021-07-30 22:32:00 -070085
86 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
Austin Schuh572924a2021-07-30 22:32:12 -070087
88 size_t node_index() const { return node_index_; }
89 const UUID &parts_uuid() const { return parts_uuid_; }
90 size_t parts_index() const { return parts_index_; }
91 const Node *node() const { return node_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070092
Austin Schuh72211ae2021-08-05 14:02:30 -070093 // Datastructure used to capture all the information about a remote node.
94 struct State {
95 // Boot UUID of the node.
96 UUID boot_uuid = UUID::Zero();
97 // Timestamp on the remote monotonic clock of the oldest message sent to
98 // node_index_.
99 monotonic_clock::time_point oldest_remote_monotonic_timestamp =
100 monotonic_clock::max_time;
101 // Timestamp on the local monotonic clock of the message in
102 // oldest_remote_monotonic_timestamp.
103 monotonic_clock::time_point oldest_local_monotonic_timestamp =
104 monotonic_clock::max_time;
105 // Timestamp on the remote monotonic clock of the oldest message sent to
106 // node_index_, excluding messages forwarded with time_to_live() == 0.
107 monotonic_clock::time_point oldest_remote_unreliable_monotonic_timestamp =
108 monotonic_clock::max_time;
109 // Timestamp on the local monotonic clock of the message in
110 // oldest_local_unreliable_monotonic_timestamp.
111 monotonic_clock::time_point oldest_local_unreliable_monotonic_timestamp =
112 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -0800113
114 // Timestamp on the remote monotonic clock of the oldest message sent to
115 // node_index_, only including messages forwarded with time_to_live() == 0.
116 monotonic_clock::time_point oldest_remote_reliable_monotonic_timestamp =
117 monotonic_clock::max_time;
118 // Timestamp on the local monotonic clock of the message in
119 // oldest_local_reliable_monotonic_timestamp.
120 monotonic_clock::time_point oldest_local_reliable_monotonic_timestamp =
121 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800122
123 // Timestamp on the remote monotonic clock of the oldest message timestamp
124 // sent back to logger_node_index_. The remote here will be the node this
125 // part is from the perspective of, ie node_index_.
126 monotonic_clock::time_point
127 oldest_logger_remote_unreliable_monotonic_timestamp =
128 monotonic_clock::max_time;
129 // The time on the monotonic clock of the logger when this timestamp made it
130 // back to the logger (logger_node_index_).
131 monotonic_clock::time_point
132 oldest_logger_local_unreliable_monotonic_timestamp =
133 monotonic_clock::max_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700134 };
135
Austin Schuhb8bca732021-07-30 22:32:00 -0700136 private:
Austin Schuhe46492f2021-07-31 19:49:41 -0700137 // Signals that a node has rebooted.
Austin Schuh5e14d842022-01-21 12:02:15 -0800138 void Reboot(const UUID &source_node_boot_uuid);
Austin Schuhe46492f2021-07-31 19:49:41 -0700139
Austin Schuh572924a2021-07-30 22:32:12 -0700140 void QueueHeader(
141 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header);
142
Austin Schuhe46492f2021-07-31 19:49:41 -0700143 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader();
144
Austin Schuh58646e22021-08-23 23:51:46 -0700145 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::min_time;
146
Austin Schuh577610e2021-12-08 12:07:19 -0800147 const Node *node_ = nullptr;
148 size_t node_index_ = 0;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800149 size_t logger_node_index_ = 0;
Austin Schuh572924a2021-07-30 22:32:12 -0700150 LogNamer *log_namer_;
151 UUID parts_uuid_ = UUID::Random();
152 size_t parts_index_ = 0;
153
Austin Schuhb8bca732021-07-30 22:32:00 -0700154 std::function<void(NewDataWriter *)> reopen_;
155 std::function<void(NewDataWriter *)> close_;
Austin Schuh572924a2021-07-30 22:32:12 -0700156 bool header_written_ = false;
Austin Schuhe46492f2021-07-31 19:49:41 -0700157
Austin Schuh72211ae2021-08-05 14:02:30 -0700158 std::vector<State> state_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700159
160 size_t max_message_size_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700161};
162
Austin Schuhcb5601b2020-09-10 15:29:59 -0700163// Interface describing how to name, track, and add headers to log file parts.
164class LogNamer {
165 public:
166 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
167 // being node.
Austin Schuh5b728b72021-06-16 14:57:15 -0700168 LogNamer(const aos::Configuration *configuration, EventLoop *event_loop,
169 const aos::Node *node)
Austin Schuha499cea2021-07-31 19:49:53 -0700170 : event_loop_(event_loop),
Austin Schuh5b728b72021-06-16 14:57:15 -0700171 configuration_(configuration),
172 node_(node),
Austin Schuha499cea2021-07-31 19:49:53 -0700173 logger_node_index_(configuration::GetNodeIndex(configuration_, node_)) {
Austin Schuh73340842021-07-30 22:32:06 -0700174 nodes_.emplace_back(node_);
Austin Schuh73340842021-07-30 22:32:06 -0700175 }
Alexei Strots01395492023-03-20 13:59:56 -0700176 virtual ~LogNamer() = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700177
Brian Silverman87ac0402020-09-17 14:47:01 -0700178 // Returns a writer for writing data from messages on this channel (on the
179 // primary node).
180 //
181 // The returned pointer will stay valid across rotations, but the object it
182 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700183 virtual NewDataWriter *MakeWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700184
Brian Silverman87ac0402020-09-17 14:47:01 -0700185 // Returns a writer for writing timestamps from messages on this channel (on
186 // the primary node).
187 //
188 // The returned pointer will stay valid across rotations, but the object it
189 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700190 virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700191
192 // Returns a writer for writing timestamps delivered over the special
193 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -0700194 // are forwarded back from (to the primary node).
195 //
196 // The returned pointer will stay valid across rotations, but the object it
197 // points to will be assigned to.
Austin Schuh73340842021-07-30 22:32:06 -0700198 virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
199 const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700200
Austin Schuh73340842021-07-30 22:32:06 -0700201 // Rotates all log files for the provided node.
202 virtual void Rotate(const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700203
204 // Returns all the nodes that data is being written for.
205 const std::vector<const Node *> &nodes() const { return nodes_; }
206
Austin Schuh08dba8f2023-05-01 08:29:30 -0700207 // Closes all existing log data writers. No more data may be written after
208 // this.
209 virtual WriteCode Close() = 0;
210
Austin Schuhcb5601b2020-09-10 15:29:59 -0700211 // Returns the node the logger is running on.
212 const Node *node() const { return node_; }
Austin Schuhe46492f2021-07-31 19:49:41 -0700213 const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; }
214 size_t logger_node_index() const { return logger_node_index_; }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700215
Austin Schuh8c399962020-12-25 21:51:45 -0800216 // Writes out the nested Configuration object to the config file location.
217 virtual void WriteConfiguration(
218 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
219 std::string_view config_sha256) = 0;
220
Austin Schuh73340842021-07-30 22:32:06 -0700221 void SetHeaderTemplate(
222 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) {
223 header_ = std::move(header);
Austin Schuhe46492f2021-07-31 19:49:41 -0700224 logger_node_boot_uuid_ =
225 UUID::FromString(header_.message().logger_node_boot_uuid());
Austin Schuh73340842021-07-30 22:32:06 -0700226 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700227
Austin Schuh60e77942022-05-16 17:48:24 -0700228 void ClearStartTimes() { node_states_.clear(); }
Austin Schuh58646e22021-08-23 23:51:46 -0700229
230 void SetStartTimes(size_t node_index, const UUID &boot_uuid,
Austin Schuh73340842021-07-30 22:32:06 -0700231 monotonic_clock::time_point monotonic_start_time,
232 realtime_clock::time_point realtime_start_time,
233 monotonic_clock::time_point logger_monotonic_start_time,
234 realtime_clock::time_point logger_realtime_start_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700235 VLOG(1) << "Setting node " << node_index << " to start time "
236 << monotonic_start_time << " rt " << realtime_start_time << " UUID "
237 << boot_uuid;
238 NodeState *node_state = GetNodeState(node_index, boot_uuid);
239 node_state->monotonic_start_time = monotonic_start_time;
240 node_state->realtime_start_time = realtime_start_time;
241 node_state->logger_monotonic_start_time = logger_monotonic_start_time;
242 node_state->logger_realtime_start_time = logger_realtime_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700243 }
244
Austin Schuh58646e22021-08-23 23:51:46 -0700245 monotonic_clock::time_point monotonic_start_time(size_t node_index,
246 const UUID &boot_uuid) {
247 DCHECK_NE(boot_uuid, UUID::Zero());
248
249 NodeState *node_state = GetNodeState(node_index, boot_uuid);
250 return node_state->monotonic_start_time;
Austin Schuh73340842021-07-30 22:32:06 -0700251 }
252
Austin Schuh73340842021-07-30 22:32:06 -0700253 protected:
Austin Schuh73340842021-07-30 22:32:06 -0700254 // Structure with state per node about times and such.
Austin Schuh73340842021-07-30 22:32:06 -0700255 struct NodeState {
256 // Time when this node started logging.
257 monotonic_clock::time_point monotonic_start_time =
258 monotonic_clock::min_time;
259 realtime_clock::time_point realtime_start_time = realtime_clock::min_time;
260
261 // Corresponding time on the logger node when it started logging.
262 monotonic_clock::time_point logger_monotonic_start_time =
263 monotonic_clock::min_time;
264 realtime_clock::time_point logger_realtime_start_time =
265 realtime_clock::min_time;
Austin Schuh73340842021-07-30 22:32:06 -0700266 };
Austin Schuh58646e22021-08-23 23:51:46 -0700267
268 // Creates a new header by copying fields out of the template and combining
269 // them with the arguments provided.
270 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
271 size_t node_index, const std::vector<NewDataWriter::State> &state,
272 const UUID &parts_uuid, int parts_index);
273
274 EventLoop *event_loop_;
275 const Configuration *const configuration_;
276 const Node *const node_;
277 const size_t logger_node_index_;
278 UUID logger_node_boot_uuid_;
279 std::vector<const Node *> nodes_;
280
281 friend NewDataWriter;
282
283 // Returns the start/stop time state structure for a node and boot. We can
284 // have data from multiple boots, and it makes sense to reuse the start/stop
285 // times if we get data from the same boot again.
286 NodeState *GetNodeState(size_t node_index, const UUID &boot_uuid);
287
288 absl::btree_map<std::pair<size_t, UUID>, NodeState> node_states_;
Austin Schuh73340842021-07-30 22:32:06 -0700289
290 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ =
291 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700292};
293
Alexei Strots01395492023-03-20 13:59:56 -0700294// Log namer which uses a config to name a bunch of files.
Austin Schuhcb5601b2020-09-10 15:29:59 -0700295class MultiNodeLogNamer : public LogNamer {
296 public:
Alexei Strotscaf17d32023-04-03 22:31:11 -0700297 MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend,
Alexei Strots01395492023-03-20 13:59:56 -0700298 EventLoop *event_loop);
Alexei Strotscaf17d32023-04-03 22:31:11 -0700299 MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend,
Austin Schuh5b728b72021-06-16 14:57:15 -0700300 const Configuration *configuration, EventLoop *event_loop,
301 const Node *node);
Brian Silvermancb805822020-10-06 17:43:35 -0700302 ~MultiNodeLogNamer() override;
303
Austin Schuh48d10d62022-10-16 22:19:23 -0700304 // Sets the function for creating encoders. The argument is the max message
305 // size (including headers) that will be written into this encoder.
Brian Silvermancb805822020-10-06 17:43:35 -0700306 //
307 // Defaults to just creating DummyEncoders.
308 void set_encoder_factory(
Austin Schuh48d10d62022-10-16 22:19:23 -0700309 std::function<std::unique_ptr<DataEncoder>(size_t)> encoder_factory) {
Brian Silvermancb805822020-10-06 17:43:35 -0700310 encoder_factory_ = std::move(encoder_factory);
311 }
312
313 // Sets an additional file extension.
314 //
315 // Defaults to nothing.
316 void set_extension(std::string_view extension) { extension_ = extension; }
Brian Silverman1f345222020-09-24 21:14:48 -0700317
Brian Silvermana621f522020-09-30 16:52:43 -0700318 // A list of all the filenames we've written.
319 //
320 // This only includes the part after base_name().
321 const std::vector<std::string> &all_filenames() const {
322 return all_filenames_;
323 }
324
Austin Schuh73340842021-07-30 22:32:06 -0700325 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700326
Austin Schuh8c399962020-12-25 21:51:45 -0800327 void WriteConfiguration(
328 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
329 std::string_view config_sha256) override;
330
Austin Schuhb8bca732021-07-30 22:32:00 -0700331 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700332
Austin Schuhb8bca732021-07-30 22:32:00 -0700333 NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
Austin Schuh73340842021-07-30 22:32:06 -0700334 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700335
Austin Schuhb8bca732021-07-30 22:32:00 -0700336 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700337
Brian Silverman0465fcf2020-09-24 00:29:18 -0700338 // Indicates that at least one file ran out of space. Once this happens, we
339 // stop trying to open new files, to avoid writing any files with holes from
340 // previous parts.
341 //
342 // Besides this function, this object will silently stop logging data when
343 // this occurs. If you want to ensure log files are complete, you must call
344 // this method.
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700345 bool ran_out_of_space() const {
346 return accumulate_data_writers<bool>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700347 ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700348 return x ||
349 (data_writer.writer && data_writer.writer->ran_out_of_space());
350 });
351 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700352
Brian Silverman1f345222020-09-24 21:14:48 -0700353 // Returns the maximum total_bytes() value for all existing
354 // DetachedBufferWriters.
355 //
356 // Returns 0 if no files are open.
357 size_t maximum_total_bytes() const {
Brian Silvermancb805822020-10-06 17:43:35 -0700358 return accumulate_data_writers<size_t>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700359 0, [](size_t x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700360 return std::max(x, data_writer.writer->total_bytes());
361 });
Brian Silverman1f345222020-09-24 21:14:48 -0700362 }
363
Brian Silverman0465fcf2020-09-24 00:29:18 -0700364 // Closes all existing log files. No more data may be written after this.
365 //
366 // This may set ran_out_of_space().
Austin Schuh08dba8f2023-05-01 08:29:30 -0700367 WriteCode Close() override;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700368
Brian Silvermancb805822020-10-06 17:43:35 -0700369 // Accessors for various statistics. See the identically-named methods in
370 // DetachedBufferWriter for documentation. These are aggregated across all
371 // past and present DetachedBufferWriters.
372 std::chrono::nanoseconds max_write_time() const {
373 return accumulate_data_writers(
374 max_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700375 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700376 return std::max(
377 x, data_writer.writer->WriteStatistics()->max_write_time());
Brian Silvermancb805822020-10-06 17:43:35 -0700378 });
379 }
380 int max_write_time_bytes() const {
381 return std::get<0>(accumulate_data_writers(
382 std::make_tuple(max_write_time_bytes_, max_write_time_),
383 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700384 const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700385 if (data_writer.writer->WriteStatistics()->max_write_time() >
386 std::get<1>(x)) {
387 return std::make_tuple(
388 data_writer.writer->WriteStatistics()->max_write_time_bytes(),
389 data_writer.writer->WriteStatistics()->max_write_time());
Brian Silvermancb805822020-10-06 17:43:35 -0700390 }
391 return x;
392 }));
393 }
394 int max_write_time_messages() const {
395 return std::get<0>(accumulate_data_writers(
396 std::make_tuple(max_write_time_messages_, max_write_time_),
397 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700398 const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700399 if (data_writer.writer->WriteStatistics()->max_write_time() >
400 std::get<1>(x)) {
Brian Silvermancb805822020-10-06 17:43:35 -0700401 return std::make_tuple(
Alexei Strots01395492023-03-20 13:59:56 -0700402 data_writer.writer->WriteStatistics()
403 ->max_write_time_messages(),
404 data_writer.writer->WriteStatistics()->max_write_time());
Brian Silvermancb805822020-10-06 17:43:35 -0700405 }
406 return x;
407 }));
408 }
409 std::chrono::nanoseconds total_write_time() const {
410 return accumulate_data_writers(
411 total_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700412 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700413 return x + data_writer.writer->WriteStatistics()->total_write_time();
Brian Silvermancb805822020-10-06 17:43:35 -0700414 });
415 }
416 int total_write_count() const {
417 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700418 total_write_count_, [](int x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700419 return x + data_writer.writer->WriteStatistics()->total_write_count();
Brian Silvermancb805822020-10-06 17:43:35 -0700420 });
421 }
422 int total_write_messages() const {
423 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700424 total_write_messages_, [](int x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700425 return x +
426 data_writer.writer->WriteStatistics()->total_write_messages();
Brian Silvermancb805822020-10-06 17:43:35 -0700427 });
428 }
429 int total_write_bytes() const {
430 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700431 total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
Alexei Strots01395492023-03-20 13:59:56 -0700432 return x + data_writer.writer->WriteStatistics()->total_write_bytes();
Brian Silvermancb805822020-10-06 17:43:35 -0700433 });
434 }
435
436 void ResetStatistics();
437
Alexei Strotscaf17d32023-04-03 22:31:11 -0700438 protected:
439 // TODO (Alexei): consider to move ownership of log_namer to concrete sub
440 // class and make log_backend_ raw pointer.
441 LogBackend *log_backend() { return log_backend_.get(); }
442 const LogBackend *log_backend() const { return log_backend_.get(); }
443
Austin Schuhcb5601b2020-09-10 15:29:59 -0700444 private:
Austin Schuhcb5601b2020-09-10 15:29:59 -0700445 // Opens up a writer for timestamps forwarded back.
446 void OpenForwardedTimestampWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700447 NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700448
449 // Opens up a writer for remote data.
Austin Schuhb8bca732021-07-30 22:32:00 -0700450 void OpenWriter(const Channel *channel, NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700451
452 // Opens the main data writer file for this node responsible for data_writer_.
Austin Schuh48d10d62022-10-16 22:19:23 -0700453 void MakeDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700454
Austin Schuh48d10d62022-10-16 22:19:23 -0700455 void CreateBufferWriter(std::string_view path, size_t max_message_size,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700456 std::unique_ptr<DetachedBufferWriter> *destination);
457
Brian Silvermancb805822020-10-06 17:43:35 -0700458 void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700459
Brian Silvermancb805822020-10-06 17:43:35 -0700460 // A version of std::accumulate which operates over all of our DataWriters.
461 template <typename T, typename BinaryOperation>
462 T accumulate_data_writers(T t, BinaryOperation op) const {
Austin Schuhb8bca732021-07-30 22:32:00 -0700463 for (const std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700464 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800465 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700466 t = op(std::move(t), data_writer.second);
467 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700468 if (data_writer_) {
469 t = op(std::move(t), *data_writer_);
Brian Silvermancb805822020-10-06 17:43:35 -0700470 }
471 return t;
472 }
473
Alexei Strotscaf17d32023-04-03 22:31:11 -0700474 std::unique_ptr<LogBackend> log_backend_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700475
Brian Silverman0465fcf2020-09-24 00:29:18 -0700476 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700477 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700478
Austin Schuh8bdfc492023-02-11 12:53:13 -0800479 std::function<std::unique_ptr<DataEncoder>(size_t)> encoder_factory_;
Brian Silvermancb805822020-10-06 17:43:35 -0700480 std::string extension_;
481
482 // Storage for statistics from previously-rotated DetachedBufferWriters.
483 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
484 int max_write_time_bytes_ = -1;
485 int max_write_time_messages_ = -1;
486 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
487 int total_write_count_ = 0;
488 int total_write_messages_ = 0;
489 int total_write_bytes_ = 0;
490
Austin Schuhcb5601b2020-09-10 15:29:59 -0700491 // File to write both delivery timestamps and local data to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700492 std::unique_ptr<NewDataWriter> data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700493
Austin Schuhb8bca732021-07-30 22:32:00 -0700494 std::map<const Channel *, NewDataWriter> data_writers_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700495};
496
Alexei Strots01395492023-03-20 13:59:56 -0700497// This is specialized log namer that deals with directory centric log events.
498class MultiNodeFilesLogNamer : public MultiNodeLogNamer {
499 public:
500 MultiNodeFilesLogNamer(std::string_view base_name, EventLoop *event_loop)
501 : MultiNodeLogNamer(std::make_unique<RenamableFileBackend>(base_name),
502 event_loop) {}
503
504 MultiNodeFilesLogNamer(std::string_view base_name,
505 const Configuration *configuration,
506 EventLoop *event_loop, const Node *node)
507 : MultiNodeLogNamer(std::make_unique<RenamableFileBackend>(base_name),
508 configuration, event_loop, node) {}
509 ~MultiNodeFilesLogNamer() override = default;
Alexei Strotscaf17d32023-04-03 22:31:11 -0700510
511 std::string_view base_name() const {
512 return renamable_file_backend()->base_name();
513 }
514
515 // Rotate should be called at least once in between calls to set_base_name.
516 // Otherwise, temporary files will not be recoverable.
517 // Rotate is called by Logger::RenameLogBase, which is currently the only user
518 // of this method.
519 // Only renaming the folder is supported, not the file base name.
520 void set_base_name(std::string_view base_name) {
521 renamable_file_backend()->RenameLogBase(base_name);
522 }
523
524 // When enabled, this will write files under names beginning
525 // with the .tmp suffix, and then rename them to the desired name after
526 // they are fully written.
527 //
528 // This is useful to enable incremental copying of the log files.
529 //
530 // Defaults to writing directly to the final filename.
531 void EnableTempFiles() { renamable_file_backend()->EnableTempFiles(); }
532
533 private:
534 RenamableFileBackend *renamable_file_backend() {
535 return reinterpret_cast<RenamableFileBackend *>(log_backend());
536 }
537 const RenamableFileBackend *renamable_file_backend() const {
538 return reinterpret_cast<const RenamableFileBackend *>(log_backend());
539 }
Alexei Strots01395492023-03-20 13:59:56 -0700540};
541
Austin Schuhcb5601b2020-09-10 15:29:59 -0700542} // namespace logger
543} // namespace aos
544
545#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_