blob: c8d2e706ae945e9d0bed088e1cdbfcaa78f20406 [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#ifndef AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
2#define AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_
3
4#include <sys/uio.h>
5
Austin Schuh97789fc2020-08-01 14:42:45 -07006#include <chrono>
Austin Schuh05b70472020-01-01 17:11:17 -08007#include <deque>
Austin Schuh97789fc2020-08-01 14:42:45 -07008#include <limits>
9#include <memory>
Austin Schuh05b70472020-01-01 17:11:17 -080010#include <optional>
Austin Schuhfa895892020-01-07 20:07:41 -080011#include <string>
Austin Schuha36c8902019-12-30 18:07:15 -080012#include <string_view>
Brian Silverman98360e22020-04-28 16:51:20 -070013#include <tuple>
Austin Schuh97789fc2020-08-01 14:42:45 -070014#include <utility>
Austin Schuha36c8902019-12-30 18:07:15 -080015#include <vector>
16
Austin Schuh4b5c22a2020-11-30 22:58:43 -080017#include "absl/container/btree_set.h"
Austin Schuh05b70472020-01-01 17:11:17 -080018#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070019#include "flatbuffers/flatbuffers.h"
20
Brian Silvermanf51499a2020-09-21 12:49:08 -070021#include "aos/containers/resizeable_buffer.h"
Austin Schuha36c8902019-12-30 18:07:15 -080022#include "aos/events/event_loop.h"
Austin Schuh2dc8c7d2021-07-01 17:41:28 -070023#include "aos/events/logging/boot_timestamp.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070024#include "aos/events/logging/buffer_encoder.h"
Alexei Strots01395492023-03-20 13:59:56 -070025#include "aos/events/logging/log_backend.h"
Austin Schuhc41603c2020-10-11 16:17:37 -070026#include "aos/events/logging/logfile_sorting.h"
Austin Schuha36c8902019-12-30 18:07:15 -080027#include "aos/events/logging/logger_generated.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -070028#include "aos/flatbuffers.h"
Austin Schuhf2d0e682022-10-16 14:20:58 -070029#include "aos/network/remote_message_generated.h"
Austin Schuha36c8902019-12-30 18:07:15 -080030
Brian Silvermanf51499a2020-09-21 12:49:08 -070031namespace aos::logger {
Austin Schuha36c8902019-12-30 18:07:15 -080032
33enum class LogType : uint8_t {
34 // The message originated on this node and should be logged here.
35 kLogMessage,
36 // The message originated on another node, but only the delivery times are
37 // logged here.
38 kLogDeliveryTimeOnly,
39 // The message originated on another node. Log it and the delivery times
40 // together. The message_gateway is responsible for logging any messages
41 // which didn't get delivered.
Austin Schuh6f3babe2020-01-26 20:34:50 -080042 kLogMessageAndDeliveryTime,
43 // The message originated on the other node and should be logged on this node.
44 kLogRemoteMessage
Austin Schuha36c8902019-12-30 18:07:15 -080045};
46
Austin Schuha36c8902019-12-30 18:07:15 -080047// This class manages efficiently writing a sequence of detached buffers to a
Brian Silvermanf51499a2020-09-21 12:49:08 -070048// file. It encodes them, queues them up, and batches the write operation.
Alexei Strots01395492023-03-20 13:59:56 -070049
Austin Schuha36c8902019-12-30 18:07:15 -080050class DetachedBufferWriter {
51 public:
Brian Silvermana9f2ec92020-10-06 18:00:53 -070052 // Marker struct for one of our constructor overloads.
53 struct already_out_of_space_t {};
54
Alexei Strotsbc082d82023-05-03 08:43:42 -070055 DetachedBufferWriter(std::unique_ptr<LogSink> log_sink,
Austin Schuh48d10d62022-10-16 22:19:23 -070056 std::unique_ptr<DataEncoder> encoder);
Brian Silvermana9f2ec92020-10-06 18:00:53 -070057 // Creates a dummy instance which won't even open a file. It will act as if
58 // opening the file ran out of space immediately.
Philipp Schrader10397952023-06-15 11:43:07 -070059 DetachedBufferWriter(already_out_of_space_t);
Austin Schuh2f8fd752020-09-01 22:38:28 -070060 DetachedBufferWriter(DetachedBufferWriter &&other);
61 DetachedBufferWriter(const DetachedBufferWriter &) = delete;
62
Philipp Schrader10397952023-06-15 11:43:07 -070063 virtual ~DetachedBufferWriter();
Austin Schuha36c8902019-12-30 18:07:15 -080064
Austin Schuh2f8fd752020-09-01 22:38:28 -070065 DetachedBufferWriter &operator=(DetachedBufferWriter &&other);
Brian Silverman98360e22020-04-28 16:51:20 -070066 DetachedBufferWriter &operator=(const DetachedBufferWriter &) = delete;
67
Alexei Strotsbc082d82023-05-03 08:43:42 -070068 std::string_view name() const { return log_sink_->name(); }
Austin Schuh6f3babe2020-01-26 20:34:50 -080069
Brian Silvermana9f2ec92020-10-06 18:00:53 -070070 // This will be true until Close() is called, unless the file couldn't be
71 // created due to running out of space.
Alexei Strotsbc082d82023-05-03 08:43:42 -070072 bool is_open() const { return log_sink_->is_open(); }
Brian Silvermana9f2ec92020-10-06 18:00:53 -070073
Brian Silvermanf51499a2020-09-21 12:49:08 -070074 // Queues up a finished FlatBufferBuilder to be encoded and written.
75 //
76 // Triggers a flush if there's enough data queued up.
77 //
78 // Steals the detached buffer from it.
Austin Schuh48d10d62022-10-16 22:19:23 -070079 void CopyMessage(DataEncoder::Copier *coppier,
80 aos::monotonic_clock::time_point now);
Austin Schuha36c8902019-12-30 18:07:15 -080081
Brian Silverman0465fcf2020-09-24 00:29:18 -070082 // Indicates we got ENOSPC when trying to write. After this returns true, no
83 // further data is written.
84 bool ran_out_of_space() const { return ran_out_of_space_; }
85
86 // To avoid silently failing to write logfiles, you must call this before
87 // destruction if ran_out_of_space() is true and the situation has been
88 // handled.
89 void acknowledge_out_of_space() {
90 CHECK(ran_out_of_space_);
91 acknowledge_ran_out_of_space_ = true;
92 }
93
94 // Fully flushes and closes the underlying file now. No additional data may be
95 // enqueued after calling this.
96 //
97 // This will be performed in the destructor automatically.
98 //
99 // Note that this may set ran_out_of_space().
100 void Close();
101
Brian Silvermanf51499a2020-09-21 12:49:08 -0700102 // Returns the total number of bytes written and currently queued.
Austin Schuha426f1f2021-03-31 22:27:41 -0700103 size_t total_bytes() const {
104 if (!encoder_) {
105 return 0;
106 }
107 return encoder_->total_bytes();
108 }
Austin Schuha36c8902019-12-30 18:07:15 -0800109
Alexei Strotsbc082d82023-05-03 08:43:42 -0700110 WriteStats *WriteStatistics() const { return log_sink_->WriteStatistics(); }
Brian Silverman98360e22020-04-28 16:51:20 -0700111
Austin Schuha36c8902019-12-30 18:07:15 -0800112 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700113 // Performs a single writev call with as much of the data we have queued up as
Austin Schuh8bdfc492023-02-11 12:53:13 -0800114 // possible. now is the time we flushed at, to be recorded in
115 // last_flush_time_.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700116 //
117 // This will normally take all of the data we have queued up, unless an
118 // encoder has spit out a big enough chunk all at once that we can't manage
119 // all of it.
Austin Schuh8bdfc492023-02-11 12:53:13 -0800120 void Flush(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700121
Brian Silvermanf51499a2020-09-21 12:49:08 -0700122 // Flushes data if we've reached the threshold to do that as part of normal
Austin Schuhbd06ae42021-03-31 22:48:21 -0700123 // operation either due to the outstanding queued data, or because we have
124 // passed our flush period. now is the current time to save some CPU grabbing
125 // the current time. It just needs to be close.
126 void FlushAtThreshold(aos::monotonic_clock::time_point now);
Brian Silvermanf51499a2020-09-21 12:49:08 -0700127
Alexei Strotsbc082d82023-05-03 08:43:42 -0700128 std::unique_ptr<LogSink> log_sink_;
Austin Schuh48d10d62022-10-16 22:19:23 -0700129 std::unique_ptr<DataEncoder> encoder_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800130
Brian Silverman0465fcf2020-09-24 00:29:18 -0700131 bool ran_out_of_space_ = false;
132 bool acknowledge_ran_out_of_space_ = false;
Austin Schuha36c8902019-12-30 18:07:15 -0800133
Austin Schuhbd06ae42021-03-31 22:48:21 -0700134 aos::monotonic_clock::time_point last_flush_time_ =
135 aos::monotonic_clock::min_time;
Austin Schuha36c8902019-12-30 18:07:15 -0800136};
137
Austin Schuhf2d0e682022-10-16 14:20:58 -0700138// Repacks the provided RemoteMessage into fbb.
139flatbuffers::Offset<MessageHeader> PackRemoteMessage(
140 flatbuffers::FlatBufferBuilder *fbb,
141 const message_bridge::RemoteMessage *msg, int channel_index,
142 const aos::monotonic_clock::time_point monotonic_timestamp_time);
143
144constexpr flatbuffers::uoffset_t PackRemoteMessageSize() { return 96u; }
145size_t PackRemoteMessageInline(
146 uint8_t *data, const message_bridge::RemoteMessage *msg, int channel_index,
Austin Schuh71a40d42023-02-04 21:22:22 -0800147 const aos::monotonic_clock::time_point monotonic_timestamp_time,
148 size_t start_byte, size_t end_byte);
Austin Schuhf2d0e682022-10-16 14:20:58 -0700149
Austin Schuha36c8902019-12-30 18:07:15 -0800150// Packes a message pointed to by the context into a MessageHeader.
151flatbuffers::Offset<MessageHeader> PackMessage(
152 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
153 int channel_index, LogType log_type);
154
Austin Schuhfa30c352022-10-16 11:12:02 -0700155// Returns the size that the packed message from PackMessage or
156// PackMessageInline will be.
Austin Schuh48d10d62022-10-16 22:19:23 -0700157flatbuffers::uoffset_t PackMessageSize(LogType log_type, size_t data_size);
Austin Schuhfa30c352022-10-16 11:12:02 -0700158
159// Packs the provided message pointed to by context into the provided buffer.
160// This is equivalent to PackMessage, but doesn't require allocating a
161// FlatBufferBuilder underneath.
162size_t PackMessageInline(uint8_t *data, const Context &contex,
Austin Schuh71a40d42023-02-04 21:22:22 -0800163 int channel_index, LogType log_type, size_t start_byte,
164 size_t end_byte);
Austin Schuhfa30c352022-10-16 11:12:02 -0700165
Austin Schuh05b70472020-01-01 17:11:17 -0800166// Class to read chunks out of a log file.
167class SpanReader {
168 public:
Alexei Strotscee7b372023-04-21 11:57:54 -0700169 // It creates a reader and makes proper decoder based on information encoded
170 // in the filename.
Austin Schuhcd368422021-11-22 21:23:29 -0800171 SpanReader(std::string_view filename, bool quiet = false);
Austin Schuha36c8902019-12-30 18:07:15 -0800172
Alexei Strotscee7b372023-04-21 11:57:54 -0700173 // Opens new reader from provided decoder.
174 SpanReader(std::string_view filename, std::unique_ptr<DataDecoder> decoder);
175
Austin Schuh6f3babe2020-01-26 20:34:50 -0800176 std::string_view filename() const { return filename_; }
177
Brian Smarttea913d42021-12-10 15:02:38 -0800178 size_t TotalRead() const { return total_read_; }
179 size_t TotalConsumed() const { return total_consumed_; }
Austin Schuh60e77942022-05-16 17:48:24 -0700180 bool IsIncomplete() const {
181 return is_finished_ && total_consumed_ < total_read_;
182 }
Brian Smarttea913d42021-12-10 15:02:38 -0800183
Austin Schuhcf5f6442021-07-06 10:43:28 -0700184 // Returns a span with the data for the next message from the log file,
185 // including the size. The result is only guarenteed to be valid until
186 // ReadMessage() or PeekMessage() is called again.
Austin Schuh05b70472020-01-01 17:11:17 -0800187 absl::Span<const uint8_t> ReadMessage();
188
Austin Schuhcf5f6442021-07-06 10:43:28 -0700189 // Returns a span with the data for the next message without consuming it.
190 // Multiple calls to PeekMessage return the same data. ReadMessage or
191 // ConsumeMessage must be called to get the next message.
192 absl::Span<const uint8_t> PeekMessage();
193 // Consumes the message so the next call to ReadMessage or PeekMessage returns
194 // new data. This does not invalidate the data.
195 void ConsumeMessage();
196
Austin Schuh05b70472020-01-01 17:11:17 -0800197 private:
198 // TODO(austin): Optimization:
199 // Allocate the 256k blocks like we do today. But, refcount them with
200 // shared_ptr pointed to by the messageheader that is returned. This avoids
201 // the copy. Need to do more benchmarking.
Brian Silvermanf51499a2020-09-21 12:49:08 -0700202 // And (Brian): Consider just mmapping the file and handing out refcounted
203 // pointers into that too.
Austin Schuh05b70472020-01-01 17:11:17 -0800204
205 // Reads a chunk of data into data_. Returns false if no data was read.
206 bool ReadBlock();
207
Austin Schuhc41603c2020-10-11 16:17:37 -0700208 std::string filename_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800209
Brian Silvermanf51499a2020-09-21 12:49:08 -0700210 // File reader and data decoder.
211 std::unique_ptr<DataDecoder> decoder_;
Austin Schuh05b70472020-01-01 17:11:17 -0800212
Brian Silvermanf51499a2020-09-21 12:49:08 -0700213 // Vector to read into.
214 ResizeableBuffer data_;
Austin Schuh05b70472020-01-01 17:11:17 -0800215
216 // Amount of data consumed already in data_.
217 size_t consumed_data_ = 0;
Brian Smarttea913d42021-12-10 15:02:38 -0800218
219 // Accumulates the total volume of bytes read from filename_
220 size_t total_read_ = 0;
221
222 // Accumulates the total volume of read bytes that were 'consumed' into
223 // messages. May be less than total_read_, if the last message (span) is
224 // either truncated or somehow corrupt.
225 size_t total_consumed_ = 0;
226
227 // Reached the end, no more readable messages.
228 bool is_finished_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800229};
230
Alexei Strotsa3194712023-04-21 23:30:50 -0700231// Class to borrow log readers from pool based on their ids. This is used as a
232// factory and helps with performance when construction or descrution of
233// decoders are not free. For instance,, S3 fetchers are slow to destroy.
234class ReadersPool {
235 public:
236 virtual ~ReadersPool() = default;
237
238 // Borrow reader from pool based on the id.
239 virtual SpanReader *BorrowReader(std::string_view id) = 0;
240};
241
242class LogReadersPool : public ReadersPool {
243 public:
244 explicit LogReadersPool(const LogSource *log_source = nullptr,
245 size_t pool_size = 50);
246
247 SpanReader *BorrowReader(std::string_view id) override;
248
249 private:
250 const LogSource *log_source_;
251 std::vector<SpanReader> part_readers_;
252 const size_t pool_size_;
253};
254
Brian Silvermanfee16972021-09-14 12:06:38 -0700255// Reads the last header from a log file. This handles any duplicate headers
256// that were written.
257std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
258 SpanReader *span_reader);
259std::optional<SizePrefixedFlatbufferVector<LogFileHeader>> ReadHeader(
260 std::string_view filename);
261// Reads the Nth message from a log file, excluding the header. Note: this
262// doesn't handle duplicate headers.
263std::optional<SizePrefixedFlatbufferVector<MessageHeader>> ReadNthMessage(
264 std::string_view filename, size_t n);
265
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700266class UnpackedMessageHeader;
267
Austin Schuh05b70472020-01-01 17:11:17 -0800268// Class which handles reading the header and messages from the log file. This
269// handles any per-file state left before merging below.
270class MessageReader {
271 public:
Alexei Strots58017402023-05-03 22:05:06 -0700272 // TODO (Alexei): it's deprecated and needs to be removed.
273 explicit MessageReader(std::string_view filename)
274 : MessageReader(SpanReader(filename)) {}
275
276 explicit MessageReader(SpanReader span_reader);
Austin Schuh05b70472020-01-01 17:11:17 -0800277
Austin Schuh6f3babe2020-01-26 20:34:50 -0800278 std::string_view filename() const { return span_reader_.filename(); }
279
Austin Schuh05b70472020-01-01 17:11:17 -0800280 // Returns the header from the log file.
281 const LogFileHeader *log_file_header() const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700282 return &raw_log_file_header_.message();
283 }
284
285 // Returns the raw data of the header from the log file.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800286 const SizePrefixedFlatbufferVector<LogFileHeader> &raw_log_file_header()
287 const {
Austin Schuh97789fc2020-08-01 14:42:45 -0700288 return raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800289 }
290
Mithun Bharadwaja5cb8e02023-08-02 16:10:40 -0700291 // Returns the minimum amount of data needed to queue up for sorting before
292 // we're guarenteed to not see data out of order.
Austin Schuh05b70472020-01-01 17:11:17 -0800293 std::chrono::nanoseconds max_out_of_order_duration() const {
294 return max_out_of_order_duration_;
295 }
296
Austin Schuhcde938c2020-02-02 17:30:07 -0800297 // Returns the newest timestamp read out of the log file.
Austin Schuh05b70472020-01-01 17:11:17 -0800298 monotonic_clock::time_point newest_timestamp() const {
299 return newest_timestamp_;
300 }
301
302 // Returns the next message if there is one.
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700303 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800304
305 // The time at which we need to read another chunk from the logfile.
306 monotonic_clock::time_point queue_data_time() const {
307 return newest_timestamp() - max_out_of_order_duration();
308 }
309
Brian Smarttea913d42021-12-10 15:02:38 -0800310 // Flag value setters for testing
311 void set_crash_on_corrupt_message_flag(bool b) {
312 crash_on_corrupt_message_flag_ = b;
313 }
314 void set_ignore_corrupt_messages_flag(bool b) {
315 ignore_corrupt_messages_flag_ = b;
316 }
317
Austin Schuh05b70472020-01-01 17:11:17 -0800318 private:
319 // Log chunk reader.
320 SpanReader span_reader_;
321
Austin Schuh97789fc2020-08-01 14:42:45 -0700322 // Vector holding the raw data for the log file header.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800323 SizePrefixedFlatbufferVector<LogFileHeader> raw_log_file_header_;
Austin Schuh05b70472020-01-01 17:11:17 -0800324
325 // Minimum amount of data to queue up for sorting before we are guarenteed
326 // to not see data out of order.
327 std::chrono::nanoseconds max_out_of_order_duration_;
328
329 // Timestamp of the newest message in a channel queue.
330 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Brian Smarttea913d42021-12-10 15:02:38 -0800331
332 // Total volume of verifiable messages from the beginning of the file.
333 // TODO - are message counts also useful?
334 size_t total_verified_before_ = 0;
335
336 // Total volume of messages with corrupted flatbuffer formatting, if any.
337 // Excludes corrupted message content.
338 // TODO - if the layout included something as simple as a CRC (relatively
339 // fast and robust enough) for each span, then corrupted content could be
340 // included in this check.
341 size_t total_corrupted_ = 0;
342
343 // Total volume of verifiable messages intermixed with corrupted messages,
344 // if any. Will be == 0 if total_corrupted_ == 0.
345 size_t total_verified_during_ = 0;
346
347 // Total volume of verifiable messages found after the last corrupted one,
348 // if any. Will be == 0 if total_corrupted_ == 0.
349 size_t total_verified_after_ = 0;
350
351 bool is_corrupted() const { return total_corrupted_ > 0; }
352
353 bool crash_on_corrupt_message_flag_ = true;
354 bool ignore_corrupt_messages_flag_ = false;
Austin Schuh05b70472020-01-01 17:11:17 -0800355};
356
Austin Schuhc41603c2020-10-11 16:17:37 -0700357// A class to seamlessly read messages from a list of part files.
358class PartsMessageReader {
359 public:
Alexei Strots58017402023-05-03 22:05:06 -0700360 // TODO (Alexei): it's deprecated, need to removed.
361 explicit PartsMessageReader(LogParts log_parts)
362 : PartsMessageReader(LogPartsAccess(std::nullopt, std::move(log_parts))) {
363 }
364
365 explicit PartsMessageReader(LogPartsAccess log_parts_access);
Austin Schuhc41603c2020-10-11 16:17:37 -0700366
367 std::string_view filename() const { return message_reader_.filename(); }
368
Austin Schuhd2f96102020-12-01 20:27:29 -0800369 // Returns the LogParts that holds the filenames we are reading.
Alexei Strots58017402023-05-03 22:05:06 -0700370 const LogParts &parts() const { return log_parts_access_.parts(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800371
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800372 const LogFileHeader *log_file_header() const {
373 return message_reader_.log_file_header();
374 }
375
Austin Schuhc41603c2020-10-11 16:17:37 -0700376 // Returns the minimum amount of data needed to queue up for sorting before
377 // we are guarenteed to not see data out of order.
378 std::chrono::nanoseconds max_out_of_order_duration() const {
Mithun Bharadwaja5cb8e02023-08-02 16:10:40 -0700379 return max_out_of_order_duration_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700380 }
381
382 // Returns the newest timestamp read out of the log file.
383 monotonic_clock::time_point newest_timestamp() const {
384 return newest_timestamp_;
385 }
386
387 // Returns the next message if there is one, or nullopt if we have reached the
388 // end of all the files.
389 // Note: reading the next message may change the max_out_of_order_duration().
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700390 std::shared_ptr<UnpackedMessageHeader> ReadMessage();
Austin Schuhc41603c2020-10-11 16:17:37 -0700391
Austin Schuh48507722021-07-17 17:29:24 -0700392 // Returns the boot count for the requested node, or std::nullopt if we don't
393 // know.
394 std::optional<size_t> boot_count(size_t node_index) const {
395 CHECK_GE(node_index, 0u);
396 CHECK_LT(node_index, boot_counts_.size());
397 return boot_counts_[node_index];
398 }
399
Austin Schuhc41603c2020-10-11 16:17:37 -0700400 private:
Alexei Strots58017402023-05-03 22:05:06 -0700401 static SpanReader MakeSpanReader(const LogPartsAccess &log_parts_access,
402 size_t part_number);
403
Austin Schuhc41603c2020-10-11 16:17:37 -0700404 // Opens the next log and updates message_reader_. Sets done_ if there is
405 // nothing more to do.
406 void NextLog();
Austin Schuh48507722021-07-17 17:29:24 -0700407 void ComputeBootCounts();
Austin Schuhc41603c2020-10-11 16:17:37 -0700408
Alexei Strots58017402023-05-03 22:05:06 -0700409 const LogPartsAccess log_parts_access_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700410 size_t next_part_index_ = 1u;
411 bool done_ = false;
Alexei Strots036d84e2023-05-03 16:05:12 -0700412
Austin Schuhc41603c2020-10-11 16:17:37 -0700413 MessageReader message_reader_;
Brian Silvermanfee16972021-09-14 12:06:38 -0700414 // We instantiate the next one early, to allow implementations to prefetch.
415 // TODO(Brian): To get optimal performance when downloading, this needs more
416 // communication with the implementation to prioritize the next part and add
417 // more parallelism when it helps. Maybe some kind of a queue of parts in
418 // order, and the implementation gets to pull however many make sense off the
419 // front?
420 std::optional<MessageReader> next_message_reader_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700421
Austin Schuh315b96b2020-12-11 21:21:12 -0800422 // True after we have seen a message after the start of the log. The
423 // guarentees on logging essentially are that all data from before the
424 // starting time of the log may be arbitrarily out of order, but once we get
425 // max_out_of_order_duration past the start, everything will remain within
426 // max_out_of_order_duration. We shouldn't see anything before the start
427 // after we've seen a message that is at least max_out_of_order_duration after
428 // the start.
429 bool after_start_ = false;
430
Austin Schuhc41603c2020-10-11 16:17:37 -0700431 monotonic_clock::time_point newest_timestamp_ = monotonic_clock::min_time;
Austin Schuh48507722021-07-17 17:29:24 -0700432
433 // Per node boot counts.
434 std::vector<std::optional<size_t>> boot_counts_;
Mithun Bharadwaja5cb8e02023-08-02 16:10:40 -0700435
436 const std::chrono::nanoseconds max_out_of_order_duration_;
Austin Schuhc41603c2020-10-11 16:17:37 -0700437};
438
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700439// Stores MessageHeader as a flat header and inline, aligned block of data.
440class UnpackedMessageHeader {
441 public:
James Kuszmaul9776b392023-01-14 14:08:08 -0800442 UnpackedMessageHeader(
443 uint32_t channel_index, monotonic_clock::time_point monotonic_sent_time,
444 realtime_clock::time_point realtime_sent_time, uint32_t queue_index,
445 std::optional<monotonic_clock::time_point> monotonic_remote_time,
446 std::optional<realtime_clock::time_point> realtime_remote_time,
447 std::optional<uint32_t> remote_queue_index,
448 monotonic_clock::time_point monotonic_timestamp_time,
449 bool has_monotonic_timestamp_time, absl::Span<const uint8_t> span)
450 : channel_index(channel_index),
451 monotonic_sent_time(monotonic_sent_time),
452 realtime_sent_time(realtime_sent_time),
453 queue_index(queue_index),
454 monotonic_remote_time(monotonic_remote_time),
455 realtime_remote_time(realtime_remote_time),
456 remote_queue_index(remote_queue_index),
457 monotonic_timestamp_time(monotonic_timestamp_time),
458 has_monotonic_timestamp_time(has_monotonic_timestamp_time),
459 span(span) {}
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700460 UnpackedMessageHeader(const UnpackedMessageHeader &) = delete;
461 UnpackedMessageHeader &operator=(const UnpackedMessageHeader &) = delete;
462
463 // The channel.
464 uint32_t channel_index = 0xffffffff;
465
466 monotonic_clock::time_point monotonic_sent_time;
467 realtime_clock::time_point realtime_sent_time;
468
469 // The local queue index.
470 uint32_t queue_index = 0xffffffff;
471
Austin Schuh826e6ce2021-11-18 20:33:10 -0800472 std::optional<aos::monotonic_clock::time_point> monotonic_remote_time;
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700473
474 std::optional<realtime_clock::time_point> realtime_remote_time;
475 std::optional<uint32_t> remote_queue_index;
476
477 // This field is defaulted in the flatbuffer, so we need to store both the
478 // possibly defaulted value and whether it is defaulted.
479 monotonic_clock::time_point monotonic_timestamp_time;
480 bool has_monotonic_timestamp_time;
481
482 static std::shared_ptr<UnpackedMessageHeader> MakeMessage(
483 const MessageHeader &message);
484
485 // Note: we are storing a span here because we need something to put in the
486 // SharedSpan pointer that RawSender takes. We are using the aliasing
487 // constructor of shared_ptr to avoid the allocation, and it needs a nice
488 // pointer to track.
489 absl::Span<const uint8_t> span;
490
Eric Schmiedebergae00e732023-04-12 15:53:17 -0600491 // Used to be able to mutate the data in the span. This is only used for
492 // mutating the message inside of LogReader for the Before Send Callback. It
493 // is safe in this case since there is only one caller to Send, and the data
494 // is not mutated after Send is called.
495 uint8_t *mutable_data() { return const_cast<uint8_t *>(span.data()); }
496
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700497 char actual_data[];
498
499 private:
500 ~UnpackedMessageHeader() {}
501
502 static void DestroyAndFree(UnpackedMessageHeader *p) {
503 p->~UnpackedMessageHeader();
504 free(p);
505 }
506};
507
508std::ostream &operator<<(std::ostream &os,
509 const UnpackedMessageHeader &message);
510
Austin Schuh1be0ce42020-11-29 22:43:26 -0800511// Struct to hold a message as it gets sorted on a single node.
512struct Message {
513 // The channel.
514 uint32_t channel_index = 0xffffffff;
515 // The local queue index.
Austin Schuh58646e22021-08-23 23:51:46 -0700516 // TODO(austin): Technically the boot inside queue_index is redundant with
517 // timestamp. In practice, it is less error-prone to duplicate it. Maybe a
518 // function to return the combined struct?
519 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700520 // The local timestamp.
521 BootTimestamp timestamp;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700522
Austin Schuh48507722021-07-17 17:29:24 -0700523 // Remote boot when this is a timestamp.
524 size_t monotonic_remote_boot = 0xffffff;
525
526 size_t monotonic_timestamp_boot = 0xffffff;
527
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700528 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800529
530 bool operator<(const Message &m2) const;
531 bool operator>=(const Message &m2) const;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800532 bool operator==(const Message &m2) const;
Austin Schuh1be0ce42020-11-29 22:43:26 -0800533};
534
535std::ostream &operator<<(std::ostream &os, const Message &m);
536
Austin Schuhd2f96102020-12-01 20:27:29 -0800537// Structure to hold a full message and all the timestamps, which may or may not
538// have been sent from a remote node. The remote_queue_index will be invalid if
539// this message is from the point of view of the node which sent it.
540struct TimestampedMessage {
541 uint32_t channel_index = 0xffffffff;
542
Austin Schuh58646e22021-08-23 23:51:46 -0700543 BootQueueIndex queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700544 BootTimestamp monotonic_event_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800545 realtime_clock::time_point realtime_event_time = realtime_clock::min_time;
546
Austin Schuh58646e22021-08-23 23:51:46 -0700547 BootQueueIndex remote_queue_index;
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700548 BootTimestamp monotonic_remote_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800549 realtime_clock::time_point realtime_remote_time = realtime_clock::min_time;
550
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700551 BootTimestamp monotonic_timestamp_time;
Austin Schuh8bf1e632021-01-02 22:41:04 -0800552
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700553 std::shared_ptr<UnpackedMessageHeader> data;
Austin Schuhd2f96102020-12-01 20:27:29 -0800554};
555
556std::ostream &operator<<(std::ostream &os, const TimestampedMessage &m);
557
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800558// Class to sort the resulting messages from a PartsMessageReader.
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700559class MessageSorter {
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800560 public:
Alexei Strots58017402023-05-03 22:05:06 -0700561 // TODO (Alexei): it's deperecated and need to be removed.
562 explicit MessageSorter(LogParts log_parts)
563 : MessageSorter(LogPartsAccess(std::nullopt, std::move(log_parts))) {}
564
565 explicit MessageSorter(const LogPartsAccess log_parts_access);
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800566
Austin Schuh0ca51f32020-12-25 21:51:45 -0800567 // Returns the parts that this is sorting messages from.
568 const LogParts &parts() const { return parts_message_reader_.parts(); }
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800569
Austin Schuhd2f96102020-12-01 20:27:29 -0800570 monotonic_clock::time_point monotonic_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800571 return parts().monotonic_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800572 }
573 realtime_clock::time_point realtime_start_time() const {
Austin Schuh0ca51f32020-12-25 21:51:45 -0800574 return parts().realtime_start_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800575 }
576
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800577 // The time this data is sorted until.
578 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
579
580 // Returns the next sorted message from the log file. It is safe to call
581 // std::move() on the result to move the data flatbuffer from it.
582 Message *Front();
583 // Pops the front message. This should only be called after a call to
584 // Front().
585 void PopFront();
586
587 // Returns a debug string representing the contents of this sorter.
588 std::string DebugString() const;
589
590 private:
591 // Log parts reader we are wrapping.
592 PartsMessageReader parts_message_reader_;
593 // Cache of the time we are sorted until.
594 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
595
Austin Schuhb000de62020-12-03 22:00:40 -0800596 // Timestamp of the last message returned. Used to make sure nothing goes
597 // backwards.
598 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
599
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800600 // Set used for efficient sorting of messages. We can benchmark and evaluate
601 // other data structures if this proves to be the bottleneck.
602 absl::btree_set<Message> messages_;
Austin Schuh48507722021-07-17 17:29:24 -0700603
604 // Mapping from channel to source node.
605 // TODO(austin): Should we put this in Boots so it can be cached for everyone?
606 std::vector<size_t> source_node_index_;
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800607};
608
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700609// Class to run merge sort on the messages associated with specific node and
610// boot.
611class PartsMerger {
Austin Schuh8f52ed52020-11-30 23:12:39 -0800612 public:
Alexei Strots1f51ac72023-05-15 10:14:54 -0700613 PartsMerger(std::string_view node_name, size_t boot_count,
614 const LogFilesContainer &log_files);
Austin Schuhd2f96102020-12-01 20:27:29 -0800615
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700616 // Copying and moving will mess up the internal raw pointers. Just don't do
617 // it.
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700618 PartsMerger(PartsMerger const &) = delete;
619 PartsMerger(PartsMerger &&) = delete;
620 void operator=(PartsMerger const &) = delete;
621 void operator=(PartsMerger &&) = delete;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700622
Austin Schuhd2f96102020-12-01 20:27:29 -0800623 // Node index in the configuration of this node.
624 int node() const { return node_; }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800625
Austin Schuh0ca51f32020-12-25 21:51:45 -0800626 // List of parts being sorted together.
627 std::vector<const LogParts *> Parts() const;
628
629 const Configuration *configuration() const {
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700630 return message_sorters_[0].parts().config.get();
Austin Schuhd2f96102020-12-01 20:27:29 -0800631 }
632
633 monotonic_clock::time_point monotonic_start_time() const {
634 return monotonic_start_time_;
635 }
636 realtime_clock::time_point realtime_start_time() const {
637 return realtime_start_time_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800638 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800639 monotonic_clock::time_point monotonic_oldest_time() const {
640 return monotonic_oldest_time_;
641 }
Austin Schuh8f52ed52020-11-30 23:12:39 -0800642
643 // The time this data is sorted until.
644 monotonic_clock::time_point sorted_until() const { return sorted_until_; }
645
646 // Returns the next sorted message from the set of log files. It is safe to
647 // call std::move() on the result to move the data flatbuffer from it.
648 Message *Front();
649 // Pops the front message. This should only be called after a call to
650 // Front().
651 void PopFront();
652
653 private:
654 // Unsorted list of all parts sorters.
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700655 std::vector<MessageSorter> message_sorters_;
Alexei Strots58017402023-05-03 22:05:06 -0700656
Austin Schuh8f52ed52020-11-30 23:12:39 -0800657 // Pointer to the parts sorter holding the current Front message if one
658 // exists, or nullptr if a new one needs to be found.
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700659 MessageSorter *current_ = nullptr;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800660 // Cached sorted_until value.
661 aos::monotonic_clock::time_point sorted_until_ = monotonic_clock::min_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800662
663 // Cached node.
664 int node_;
665
Austin Schuhb000de62020-12-03 22:00:40 -0800666 // Timestamp of the last message returned. Used to make sure nothing goes
667 // backwards.
668 monotonic_clock::time_point last_message_time_ = monotonic_clock::min_time;
669
Austin Schuhd2f96102020-12-01 20:27:29 -0800670 realtime_clock::time_point realtime_start_time_ = realtime_clock::max_time;
671 monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::max_time;
Austin Schuh60e77942022-05-16 17:48:24 -0700672 monotonic_clock::time_point monotonic_oldest_time_ =
673 monotonic_clock::max_time;
Austin Schuhd2f96102020-12-01 20:27:29 -0800674};
675
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700676// Class to concatenate multiple boots worth of logs into a single per-node
677// stream.
678class BootMerger {
679 public:
Alexei Strots1f51ac72023-05-15 10:14:54 -0700680 BootMerger(std::string_view node_name, const LogFilesContainer &log_files);
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700681
682 // Copying and moving will mess up the internal raw pointers. Just don't do
683 // it.
684 BootMerger(BootMerger const &) = delete;
685 BootMerger(BootMerger &&) = delete;
686 void operator=(BootMerger const &) = delete;
687 void operator=(BootMerger &&) = delete;
688
689 // Node index in the configuration of this node.
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700690 int node() const { return parts_mergers_[0]->node(); }
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700691
692 // List of parts being sorted together.
693 std::vector<const LogParts *> Parts() const;
694
695 const Configuration *configuration() const {
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700696 return parts_mergers_[0]->configuration();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700697 }
698
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700699 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700700 CHECK_LT(boot, parts_mergers_.size());
701 return parts_mergers_[boot]->monotonic_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700702 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700703 realtime_clock::time_point realtime_start_time(size_t boot) const {
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700704 CHECK_LT(boot, parts_mergers_.size());
705 return parts_mergers_[boot]->realtime_start_time();
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700706 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800707 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700708 CHECK_LT(boot, parts_mergers_.size());
709 return parts_mergers_[boot]->monotonic_oldest_time();
Austin Schuh5dd22842021-11-17 16:09:39 -0800710 }
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700711
712 bool started() const {
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700713 return parts_mergers_[index_]->sorted_until() !=
714 monotonic_clock::min_time ||
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700715 index_ != 0;
716 }
717
718 // Returns the next sorted message from the set of log files. It is safe to
719 // call std::move() on the result to move the data flatbuffer from it.
720 Message *Front();
721 // Pops the front message. This should only be called after a call to
722 // Front().
723 void PopFront();
724
725 private:
726 int index_ = 0;
727
728 // TODO(austin): Sanjay points out this is pretty inefficient. Don't keep so
729 // many things open.
Alexei Strotsa8dadd12023-04-28 15:19:23 -0700730 std::vector<std::unique_ptr<PartsMerger>> parts_mergers_;
Austin Schuhf16ef6a2021-06-30 21:48:17 -0700731};
732
Austin Schuhd2f96102020-12-01 20:27:29 -0800733// Class to match timestamps with the corresponding data from other nodes.
Austin Schuh79b30942021-01-24 22:32:21 -0800734//
735// This class also buffers data for the node it represents, and supports
736// notifying when new data is queued as well as queueing until a point in time.
Austin Schuhd2f96102020-12-01 20:27:29 -0800737class TimestampMapper {
738 public:
Alexei Strots1f51ac72023-05-15 10:14:54 -0700739 TimestampMapper(std::string_view node_name,
740 const LogFilesContainer &log_files);
Austin Schuhd2f96102020-12-01 20:27:29 -0800741
742 // Copying and moving will mess up the internal raw pointers. Just don't do
743 // it.
744 TimestampMapper(TimestampMapper const &) = delete;
745 TimestampMapper(TimestampMapper &&) = delete;
746 void operator=(TimestampMapper const &) = delete;
747 void operator=(TimestampMapper &&) = delete;
748
749 // TODO(austin): It would be super helpful to provide a way to queue up to
750 // time X without matching timestamps, and to then be able to pull the
751 // timestamps out of this queue. This lets us bootstrap time estimation
752 // without exploding memory usage worst case.
753
Austin Schuh0ca51f32020-12-25 21:51:45 -0800754 const Configuration *configuration() const { return configuration_.get(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800755
756 // Returns which node this is sorting for.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700757 size_t node() const { return boot_merger_.node(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800758
759 // The start time of this log.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700760 monotonic_clock::time_point monotonic_start_time(size_t boot) const {
761 return boot_merger_.monotonic_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800762 }
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700763 realtime_clock::time_point realtime_start_time(size_t boot) const {
764 return boot_merger_.realtime_start_time(boot);
Austin Schuhd2f96102020-12-01 20:27:29 -0800765 }
Austin Schuh5dd22842021-11-17 16:09:39 -0800766 // Returns the oldest timestamp on a message on this boot.
767 monotonic_clock::time_point monotonic_oldest_time(size_t boot) const {
768 return boot_merger_.monotonic_oldest_time(boot);
769 }
Austin Schuhd2f96102020-12-01 20:27:29 -0800770
771 // Uses timestamp_mapper as the peer for its node. Only one mapper may be set
772 // for each node. Peers are used to look up the data for timestamps on this
773 // node.
774 void AddPeer(TimestampMapper *timestamp_mapper);
775
Austin Schuh24bf4972021-06-29 22:09:08 -0700776 // Returns true if anything has been queued up.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700777 bool started() const { return boot_merger_.started(); }
Austin Schuhd2f96102020-12-01 20:27:29 -0800778
779 // Returns the next message for this node.
780 TimestampedMessage *Front();
781 // Pops the next message. Front must be called first.
782 void PopFront();
783
784 // Returns debug information about this node.
785 std::string DebugString() const;
786
Austin Schuh79b30942021-01-24 22:32:21 -0800787 // Queues data the provided time.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700788 void QueueUntil(BootTimestamp queue_time);
Austin Schuhe639ea12021-01-25 13:00:22 -0800789 // Queues until we have time_estimation_buffer of data in the queue.
790 void QueueFor(std::chrono::nanoseconds time_estimation_buffer);
Austin Schuh79b30942021-01-24 22:32:21 -0800791
Austin Schuh06601222021-01-26 17:02:50 -0800792 // Queues until the condition is met.
793 template <typename T>
794 void QueueUntilCondition(T fn) {
795 while (true) {
796 if (fn()) {
797 break;
798 }
799 if (!QueueMatched()) {
800 break;
801 }
802 }
803 }
804
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700805 // Sets the callback that can be used to skip messages.
806 void set_replay_channels_callback(
807 std::function<bool(const TimestampedMessage &)> fn) {
808 replay_channels_callback_ = fn;
809 }
810
Austin Schuh79b30942021-01-24 22:32:21 -0800811 // Sets a callback to be called whenever a full message is queued.
812 void set_timestamp_callback(std::function<void(TimestampedMessage *)> fn) {
813 timestamp_callback_ = fn;
814 }
815
Austin Schuhd2f96102020-12-01 20:27:29 -0800816 private:
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700817 // Result of MaybeQueueMatched
818 enum class MatchResult : uint8_t {
819 kEndOfFile, // End of the log file being read
820 kQueued, // Message was queued
821 kSkipped // Message was skipped over
822 };
823
Austin Schuhd2f96102020-12-01 20:27:29 -0800824 // The state for a remote node. This holds the data that needs to be matched
825 // with the remote node's timestamps.
826 struct NodeData {
827 // True if we should save data here. This should be true if any of the
828 // bools in delivered below are true.
829 bool any_delivered = false;
830
Austin Schuh36c00932021-07-19 18:13:21 -0700831 // True if we have a peer and therefore should be saving data for it.
832 bool save_for_peer = false;
833
Austin Schuhd2f96102020-12-01 20:27:29 -0800834 // Peer pointer. This node is only to be considered if a peer is set.
835 TimestampMapper *peer = nullptr;
836
837 struct ChannelData {
838 // Deque per channel. This contains the data from the outside
839 // TimestampMapper node which is relevant for the node this NodeData
840 // points to.
841 std::deque<Message> messages;
842 // Bool tracking per channel if a message is delivered to the node this
843 // NodeData represents.
844 bool delivered = false;
Austin Schuh6a7358f2021-11-18 22:40:40 -0800845 // The TTL for delivery.
846 std::chrono::nanoseconds time_to_live = std::chrono::nanoseconds(0);
Austin Schuhd2f96102020-12-01 20:27:29 -0800847 };
848
849 // Vector with per channel data.
850 std::vector<ChannelData> channels;
851 };
852
853 // Returns (and forgets about) the data for the provided timestamp message
854 // showing when it was delivered to this node.
855 Message MatchingMessageFor(const Message &message);
856
857 // Queues up a single message into our message queue, and any nodes that this
858 // message is delivered to. Returns true if one was available, false
859 // otherwise.
860 bool Queue();
861
Austin Schuh79b30942021-01-24 22:32:21 -0800862 // Queues up a single matched message into our matched message queue. Returns
863 // true if one was queued, and false otherwise.
864 bool QueueMatched();
865
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700866 // Queues a message if the replay_channels_callback is passed and the end of
867 // the log file has not been reached.
868 MatchResult MaybeQueueMatched();
869
Austin Schuhd2f96102020-12-01 20:27:29 -0800870 // Queues up data until we have at least one message >= to time t.
871 // Useful for triggering a remote node to read enough data to have the
872 // timestamp you care about available.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700873 void QueueUnmatchedUntil(BootTimestamp t);
Austin Schuhd2f96102020-12-01 20:27:29 -0800874
Austin Schuh79b30942021-01-24 22:32:21 -0800875 // Queues m into matched_messages_.
876 void QueueMessage(Message *m);
Austin Schuhd2f96102020-12-01 20:27:29 -0800877
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700878 // If a replay_channels_callback was set and the callback returns false, a
879 // matched message is popped and true is returned. Otherwise false is
880 // returned.
881 bool CheckReplayChannelsAndMaybePop(const TimestampedMessage &message);
882
Austin Schuh58646e22021-08-23 23:51:46 -0700883 // Returns the name of the node this class is sorting for.
884 std::string_view node_name() const {
885 return configuration_->has_nodes() ? configuration_->nodes()
886 ->Get(boot_merger_.node())
887 ->name()
888 ->string_view()
889 : "(single node)";
890 }
891
Austin Schuhd2f96102020-12-01 20:27:29 -0800892 // The node merger to source messages from.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700893 BootMerger boot_merger_;
Austin Schuh0ca51f32020-12-25 21:51:45 -0800894
895 std::shared_ptr<const Configuration> configuration_;
896
Austin Schuhd2f96102020-12-01 20:27:29 -0800897 // The buffer of messages for this node. These are not matched with any
898 // remote data.
899 std::deque<Message> messages_;
900 // The node index for the source node for each channel.
901 std::vector<size_t> source_node_;
902
903 // Vector per node. Not all nodes will have anything.
904 std::vector<NodeData> nodes_data_;
905
906 // Latest message to return.
Austin Schuh79b30942021-01-24 22:32:21 -0800907 std::deque<TimestampedMessage> matched_messages_;
Austin Schuhd2f96102020-12-01 20:27:29 -0800908
Austin Schuh79b30942021-01-24 22:32:21 -0800909 // Tracks the state of the first message in matched_messages_. Do we need to
910 // update it, is it valid, or should we return nullptr?
Austin Schuhd2f96102020-12-01 20:27:29 -0800911 enum class FirstMessage {
912 kNeedsUpdate,
913 kInMessage,
914 kNullptr,
915 };
916 FirstMessage first_message_ = FirstMessage::kNeedsUpdate;
917
918 // Timestamp of the last message returned. Used to make sure nothing goes
919 // backwards.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700920 BootTimestamp last_message_time_ = BootTimestamp::min_time();
Austin Schuh6a7358f2021-11-18 22:40:40 -0800921 BootTimestamp last_popped_message_time_ = BootTimestamp::min_time();
Austin Schuhd2f96102020-12-01 20:27:29 -0800922 // Time this node is queued up until. Used for caching.
Austin Schuh2dc8c7d2021-07-01 17:41:28 -0700923 BootTimestamp queued_until_ = BootTimestamp::min_time();
Austin Schuh79b30942021-01-24 22:32:21 -0800924
925 std::function<void(TimestampedMessage *)> timestamp_callback_;
Eric Schmiedebergb38477e2022-12-02 16:08:04 -0700926 std::function<bool(TimestampedMessage &)> replay_channels_callback_;
Austin Schuh8f52ed52020-11-30 23:12:39 -0800927};
928
Alexei Strots036d84e2023-05-03 16:05:12 -0700929// Returns the node name, or an empty string if we are a single node.
930inline std::string_view MaybeNodeName(const Node *node) {
931 if (node != nullptr) {
932 return node->name()->string_view();
933 }
934 return "";
935}
Austin Schuhee711052020-08-24 16:06:09 -0700936
Austin Schuh71a40d42023-02-04 21:22:22 -0800937// Class to copy a RemoteMessage into the provided buffer.
938class RemoteMessageCopier : public DataEncoder::Copier {
939 public:
940 RemoteMessageCopier(const message_bridge::RemoteMessage *message,
941 int channel_index,
942 aos::monotonic_clock::time_point monotonic_timestamp_time,
943 EventLoop *event_loop)
944 : DataEncoder::Copier(PackRemoteMessageSize()),
945 message_(message),
946 channel_index_(channel_index),
947 monotonic_timestamp_time_(monotonic_timestamp_time),
948 event_loop_(event_loop) {}
949
950 monotonic_clock::time_point end_time() const { return end_time_; }
951
952 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
953 size_t result = PackRemoteMessageInline(data, message_, channel_index_,
954 monotonic_timestamp_time_,
955 start_byte, end_byte);
956 end_time_ = event_loop_->monotonic_now();
957 return result;
958 }
959
960 private:
961 const message_bridge::RemoteMessage *message_;
962 int channel_index_;
963 aos::monotonic_clock::time_point monotonic_timestamp_time_;
964 EventLoop *event_loop_;
965 monotonic_clock::time_point end_time_;
966};
967
968// Class to copy a context into the provided buffer.
969class ContextDataCopier : public DataEncoder::Copier {
970 public:
971 ContextDataCopier(const Context &context, int channel_index, LogType log_type,
972 EventLoop *event_loop)
973 : DataEncoder::Copier(PackMessageSize(log_type, context.size)),
974 context_(context),
975 channel_index_(channel_index),
976 log_type_(log_type),
977 event_loop_(event_loop) {}
978
979 monotonic_clock::time_point end_time() const { return end_time_; }
980
981 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
982 size_t result = PackMessageInline(data, context_, channel_index_, log_type_,
983 start_byte, end_byte);
984 end_time_ = event_loop_->monotonic_now();
985 return result;
986 }
987
988 private:
989 const Context &context_;
990 const int channel_index_;
991 const LogType log_type_;
992 EventLoop *event_loop_;
993 monotonic_clock::time_point end_time_;
994};
995
Brian Silvermanf51499a2020-09-21 12:49:08 -0700996} // namespace aos::logger
Austin Schuha36c8902019-12-30 18:07:15 -0800997
998#endif // AOS_EVENTS_LOGGING_LOGFILE_UTILS_H_