blob: 472580182f817346f63b2a6b3491a19dd2974138 [file] [log] [blame]
Austin Schuha36c8902019-12-30 18:07:15 -08001#include "aos/events/logging/logfile_utils.h"
2
3#include <fcntl.h>
4#include <limits.h>
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <sys/uio.h>
8
9#include <vector>
10
Austin Schuhe4fca832020-03-07 16:58:53 -080011#include "absl/strings/escaping.h"
Austin Schuh05b70472020-01-01 17:11:17 -080012#include "aos/configuration.h"
Austin Schuha36c8902019-12-30 18:07:15 -080013#include "aos/events/logging/logger_generated.h"
Austin Schuhfa895892020-01-07 20:07:41 -080014#include "aos/flatbuffer_merge.h"
Austin Schuh6f3babe2020-01-26 20:34:50 -080015#include "aos/util/file.h"
Austin Schuha36c8902019-12-30 18:07:15 -080016#include "flatbuffers/flatbuffers.h"
Austin Schuh05b70472020-01-01 17:11:17 -080017#include "gflags/gflags.h"
18#include "glog/logging.h"
Austin Schuha36c8902019-12-30 18:07:15 -080019
Austin Schuh7fbf5a72020-09-21 16:28:13 -070020DEFINE_int32(flush_size, 128000,
Austin Schuha36c8902019-12-30 18:07:15 -080021 "Number of outstanding bytes to allow before flushing to disk.");
22
23namespace aos {
24namespace logger {
25
Austin Schuh05b70472020-01-01 17:11:17 -080026namespace chrono = std::chrono;
27
Austin Schuha36c8902019-12-30 18:07:15 -080028DetachedBufferWriter::DetachedBufferWriter(std::string_view filename)
Austin Schuh6f3babe2020-01-26 20:34:50 -080029 : filename_(filename) {
30 util::MkdirP(filename, 0777);
31 fd_ = open(std::string(filename).c_str(),
32 O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 0774);
33 VLOG(1) << "Opened " << filename << " for writing";
34 PCHECK(fd_ != -1) << ": Failed to open " << filename << " for writing";
Austin Schuha36c8902019-12-30 18:07:15 -080035}
36
37DetachedBufferWriter::~DetachedBufferWriter() {
38 Flush();
39 PLOG_IF(ERROR, close(fd_) == -1) << " Failed to close logfile";
Austin Schuh2f8fd752020-09-01 22:38:28 -070040 VLOG(1) << "Closed " << filename_;
41}
42
43DetachedBufferWriter::DetachedBufferWriter(
44 DetachedBufferWriter &&other) {
45 *this = std::move(other);
46}
47
Brian Silverman87ac0402020-09-17 14:47:01 -070048// When other is destroyed "soon" (which it should be because we're getting an
49// rvalue reference to it), it will flush etc all the data we have queued up
50// (because that data will then be its data).
Austin Schuh2f8fd752020-09-01 22:38:28 -070051DetachedBufferWriter &DetachedBufferWriter::operator=(
52 DetachedBufferWriter &&other) {
Austin Schuh2f8fd752020-09-01 22:38:28 -070053 std::swap(filename_, other.filename_);
54 std::swap(fd_, other.fd_);
55 std::swap(queued_size_, other.queued_size_);
56 std::swap(written_size_, other.written_size_);
57 std::swap(queue_, other.queue_);
58 std::swap(iovec_, other.iovec_);
59 return *this;
Austin Schuha36c8902019-12-30 18:07:15 -080060}
61
62void DetachedBufferWriter::QueueSizedFlatbuffer(
63 flatbuffers::FlatBufferBuilder *fbb) {
64 QueueSizedFlatbuffer(fbb->Release());
65}
66
Austin Schuhde031b72020-01-10 19:34:41 -080067void DetachedBufferWriter::WriteSizedFlatbuffer(
68 absl::Span<const uint8_t> span) {
69 // Cheat aggressively... Write out the queued up data, and then write this
70 // data once without buffering. It is hard to make a DetachedBuffer out of
71 // this data, and we don't want to worry about lifetimes.
72 Flush();
73 iovec_.clear();
74 iovec_.reserve(1);
75
76 struct iovec n;
77 n.iov_base = const_cast<uint8_t *>(span.data());
78 n.iov_len = span.size();
79 iovec_.emplace_back(n);
80
81 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
82
83 PCHECK(written == static_cast<ssize_t>(n.iov_len))
84 << ": Wrote " << written << " expected " << n.iov_len;
Brian Silverman98360e22020-04-28 16:51:20 -070085 written_size_ += written;
Austin Schuhde031b72020-01-10 19:34:41 -080086}
87
Austin Schuha36c8902019-12-30 18:07:15 -080088void DetachedBufferWriter::QueueSizedFlatbuffer(
89 flatbuffers::DetachedBuffer &&buffer) {
90 queued_size_ += buffer.size();
91 queue_.emplace_back(std::move(buffer));
92
93 // Flush if we are at the max number of iovs per writev, or have written
94 // enough data. Otherwise writev will fail with an invalid argument.
95 if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) ||
96 queue_.size() == IOV_MAX) {
97 Flush();
98 }
99}
100
101void DetachedBufferWriter::Flush() {
102 if (queue_.size() == 0u) {
103 return;
104 }
105 iovec_.clear();
106 iovec_.reserve(queue_.size());
107 size_t counted_size = 0;
108 for (size_t i = 0; i < queue_.size(); ++i) {
109 struct iovec n;
110 n.iov_base = queue_[i].data();
111 n.iov_len = queue_[i].size();
112 counted_size += n.iov_len;
113 iovec_.emplace_back(std::move(n));
114 }
115 CHECK_EQ(counted_size, queued_size_);
116 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
117
118 PCHECK(written == static_cast<ssize_t>(queued_size_))
119 << ": Wrote " << written << " expected " << queued_size_;
Brian Silverman98360e22020-04-28 16:51:20 -0700120 written_size_ += written;
Austin Schuha36c8902019-12-30 18:07:15 -0800121
122 queued_size_ = 0;
123 queue_.clear();
124 // TODO(austin): Handle partial writes in some way other than crashing...
125}
126
127flatbuffers::Offset<MessageHeader> PackMessage(
128 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
129 int channel_index, LogType log_type) {
130 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset;
131
132 switch (log_type) {
133 case LogType::kLogMessage:
134 case LogType::kLogMessageAndDeliveryTime:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800135 case LogType::kLogRemoteMessage:
Brian Silvermaneaa41d62020-07-08 19:47:35 -0700136 data_offset = fbb->CreateVector(
137 static_cast<const uint8_t *>(context.data), context.size);
Austin Schuha36c8902019-12-30 18:07:15 -0800138 break;
139
140 case LogType::kLogDeliveryTimeOnly:
141 break;
142 }
143
144 MessageHeader::Builder message_header_builder(*fbb);
145 message_header_builder.add_channel_index(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800146
147 switch (log_type) {
148 case LogType::kLogRemoteMessage:
149 message_header_builder.add_queue_index(context.remote_queue_index);
150 message_header_builder.add_monotonic_sent_time(
151 context.monotonic_remote_time.time_since_epoch().count());
152 message_header_builder.add_realtime_sent_time(
153 context.realtime_remote_time.time_since_epoch().count());
154 break;
155
156 case LogType::kLogMessage:
157 case LogType::kLogMessageAndDeliveryTime:
158 case LogType::kLogDeliveryTimeOnly:
159 message_header_builder.add_queue_index(context.queue_index);
160 message_header_builder.add_monotonic_sent_time(
161 context.monotonic_event_time.time_since_epoch().count());
162 message_header_builder.add_realtime_sent_time(
163 context.realtime_event_time.time_since_epoch().count());
164 break;
165 }
Austin Schuha36c8902019-12-30 18:07:15 -0800166
167 switch (log_type) {
168 case LogType::kLogMessage:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800169 case LogType::kLogRemoteMessage:
Austin Schuha36c8902019-12-30 18:07:15 -0800170 message_header_builder.add_data(data_offset);
171 break;
172
173 case LogType::kLogMessageAndDeliveryTime:
174 message_header_builder.add_data(data_offset);
175 [[fallthrough]];
176
177 case LogType::kLogDeliveryTimeOnly:
178 message_header_builder.add_monotonic_remote_time(
179 context.monotonic_remote_time.time_since_epoch().count());
180 message_header_builder.add_realtime_remote_time(
181 context.realtime_remote_time.time_since_epoch().count());
182 message_header_builder.add_remote_queue_index(context.remote_queue_index);
183 break;
184 }
185
186 return message_header_builder.Finish();
187}
188
Austin Schuh05b70472020-01-01 17:11:17 -0800189SpanReader::SpanReader(std::string_view filename)
Austin Schuh6f3babe2020-01-26 20:34:50 -0800190 : filename_(filename),
191 fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) {
Austin Schuh05b70472020-01-01 17:11:17 -0800192 PCHECK(fd_ != -1) << ": Failed to open " << filename;
193}
194
195absl::Span<const uint8_t> SpanReader::ReadMessage() {
196 // Make sure we have enough for the size.
197 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
198 if (!ReadBlock()) {
199 return absl::Span<const uint8_t>();
200 }
201 }
202
203 // Now make sure we have enough for the message.
204 const size_t data_size =
205 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
206 sizeof(flatbuffers::uoffset_t);
Austin Schuhe4fca832020-03-07 16:58:53 -0800207 if (data_size == sizeof(flatbuffers::uoffset_t)) {
208 LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping.";
209 LOG(ERROR) << " Rest of log file is "
210 << absl::BytesToHexString(std::string_view(
211 reinterpret_cast<const char *>(data_.data() +
212 consumed_data_),
213 data_.size() - consumed_data_));
214 return absl::Span<const uint8_t>();
215 }
Austin Schuh05b70472020-01-01 17:11:17 -0800216 while (data_.size() < consumed_data_ + data_size) {
217 if (!ReadBlock()) {
218 return absl::Span<const uint8_t>();
219 }
220 }
221
222 // And return it, consuming the data.
223 const uint8_t *data_ptr = data_.data() + consumed_data_;
224
225 consumed_data_ += data_size;
226
227 return absl::Span<const uint8_t>(data_ptr, data_size);
228}
229
230bool SpanReader::MessageAvailable() {
231 // Are we big enough to read the size?
232 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
233 return false;
234 }
235
236 // Then, are we big enough to read the full message?
237 const size_t data_size =
238 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
239 sizeof(flatbuffers::uoffset_t);
240 if (data_.size() < consumed_data_ + data_size) {
241 return false;
242 }
243
244 return true;
245}
246
247bool SpanReader::ReadBlock() {
248 if (end_of_file_) {
249 return false;
250 }
251
252 // Appends 256k. This is enough that the read call is efficient. We don't
253 // want to spend too much time reading small chunks because the syscalls for
254 // that will be expensive.
255 constexpr size_t kReadSize = 256 * 1024;
256
257 // Strip off any unused data at the front.
258 if (consumed_data_ != 0) {
259 data_.erase(data_.begin(), data_.begin() + consumed_data_);
260 consumed_data_ = 0;
261 }
262
263 const size_t starting_size = data_.size();
264
265 // This should automatically grow the backing store. It won't shrink if we
266 // get a small chunk later. This reduces allocations when we want to append
267 // more data.
268 data_.resize(data_.size() + kReadSize);
269
270 ssize_t count = read(fd_, &data_[starting_size], kReadSize);
271 data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0)));
272 if (count == 0) {
273 end_of_file_ = true;
274 return false;
275 }
276 PCHECK(count > 0);
277
278 return true;
279}
280
Austin Schuh6f3babe2020-01-26 20:34:50 -0800281FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename) {
282 SpanReader span_reader(filename);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800283 absl::Span<const uint8_t> config_data = span_reader.ReadMessage();
284
285 // Make sure something was read.
Austin Schuh97789fc2020-08-01 14:42:45 -0700286 CHECK(config_data != absl::Span<const uint8_t>())
287 << ": Failed to read header from: " << filename;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800288
Austin Schuh5212cad2020-09-09 23:12:09 -0700289 // And copy the config so we have it forever, removing the size prefix.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800290 std::vector<uint8_t> data(
291 config_data.begin() + sizeof(flatbuffers::uoffset_t), config_data.end());
292 return FlatbufferVector<LogFileHeader>(std::move(data));
293}
294
Austin Schuh5212cad2020-09-09 23:12:09 -0700295FlatbufferVector<MessageHeader> ReadNthMessage(std::string_view filename,
296 size_t n) {
297 SpanReader span_reader(filename);
298 absl::Span<const uint8_t> data_span = span_reader.ReadMessage();
299 for (size_t i = 0; i < n + 1; ++i) {
300 data_span = span_reader.ReadMessage();
301
302 // Make sure something was read.
303 CHECK(data_span != absl::Span<const uint8_t>())
304 << ": Failed to read data from: " << filename;
305 }
306
307 // And copy the data so we have it forever.
308 std::vector<uint8_t> data(data_span.begin() + sizeof(flatbuffers::uoffset_t),
309 data_span.end());
310 return FlatbufferVector<MessageHeader>(std::move(data));
311}
312
Austin Schuh05b70472020-01-01 17:11:17 -0800313MessageReader::MessageReader(std::string_view filename)
Austin Schuh97789fc2020-08-01 14:42:45 -0700314 : span_reader_(filename),
315 raw_log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) {
Austin Schuh05b70472020-01-01 17:11:17 -0800316 // Make sure we have enough to read the size.
Austin Schuh97789fc2020-08-01 14:42:45 -0700317 absl::Span<const uint8_t> header_data = span_reader_.ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800318
319 // Make sure something was read.
Austin Schuh97789fc2020-08-01 14:42:45 -0700320 CHECK(header_data != absl::Span<const uint8_t>())
321 << ": Failed to read header from: " << filename;
Austin Schuh05b70472020-01-01 17:11:17 -0800322
Austin Schuh97789fc2020-08-01 14:42:45 -0700323 // And copy the header data so we have it forever.
324 std::vector<uint8_t> header_data_copy(
325 header_data.begin() + sizeof(flatbuffers::uoffset_t), header_data.end());
326 raw_log_file_header_ =
327 FlatbufferVector<LogFileHeader>(std::move(header_data_copy));
Austin Schuh05b70472020-01-01 17:11:17 -0800328
Austin Schuhcde938c2020-02-02 17:30:07 -0800329 max_out_of_order_duration_ =
Austin Schuh2f8fd752020-09-01 22:38:28 -0700330 chrono::nanoseconds(log_file_header()->max_out_of_order_duration());
Austin Schuhcde938c2020-02-02 17:30:07 -0800331
332 VLOG(1) << "Opened " << filename << " as node "
333 << FlatbufferToJson(log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800334}
335
336std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() {
337 absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage();
338 if (msg_data == absl::Span<const uint8_t>()) {
339 return std::nullopt;
340 }
341
342 FlatbufferVector<MessageHeader> result{std::vector<uint8_t>(
343 msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())};
344
345 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
346 chrono::nanoseconds(result.message().monotonic_sent_time()));
347
348 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
Austin Schuh8bd96322020-02-13 21:18:22 -0800349 VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800350 return std::move(result);
Austin Schuh05b70472020-01-01 17:11:17 -0800351}
352
Austin Schuh6f3babe2020-01-26 20:34:50 -0800353SplitMessageReader::SplitMessageReader(
Austin Schuhfa895892020-01-07 20:07:41 -0800354 const std::vector<std::string> &filenames)
355 : filenames_(filenames),
Austin Schuh97789fc2020-08-01 14:42:45 -0700356 log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) {
Austin Schuhfa895892020-01-07 20:07:41 -0800357 CHECK(NextLogFile()) << ": filenames is empty. Need files to read.";
358
Austin Schuh6f3babe2020-01-26 20:34:50 -0800359 // Grab any log file header. They should all match (and we will check as we
360 // open more of them).
Austin Schuh97789fc2020-08-01 14:42:45 -0700361 log_file_header_ = message_reader_->raw_log_file_header();
Austin Schuhfa895892020-01-07 20:07:41 -0800362
Austin Schuh2f8fd752020-09-01 22:38:28 -0700363 for (size_t i = 1; i < filenames_.size(); ++i) {
364 MessageReader message_reader(filenames_[i]);
365
366 const monotonic_clock::time_point new_monotonic_start_time(
367 chrono::nanoseconds(
368 message_reader.log_file_header()->monotonic_start_time()));
369 const realtime_clock::time_point new_realtime_start_time(
370 chrono::nanoseconds(
371 message_reader.log_file_header()->realtime_start_time()));
372
373 // There are 2 types of part files. Part files from before time estimation
374 // has started, and part files after. We don't declare a log file "started"
375 // until time estimation is up. And once a log file starts, it should never
376 // stop again, and should remain constant.
377 // To compare both types of headers, we mutate our saved copy of the header
378 // to match the next chunk by updating time if we detect a stopped ->
379 // started transition.
380 if (monotonic_start_time() == monotonic_clock::min_time) {
381 CHECK_EQ(realtime_start_time(), realtime_clock::min_time);
382 // We should only be missing the monotonic start time when logging data
Brian Silverman87ac0402020-09-17 14:47:01 -0700383 // for remote nodes. We don't have a good way to determine the remote
Austin Schuh2f8fd752020-09-01 22:38:28 -0700384 // realtime offset, so it shouldn't be filled out.
385 // TODO(austin): If we have a good way, feel free to fill it out. It
386 // probably won't be better than we could do in post though with the same
387 // data.
388 CHECK(!log_file_header_.mutable_message()->has_realtime_start_time());
389 if (new_monotonic_start_time != monotonic_clock::min_time) {
390 // If we finally found our start time, update the header. Do this once
391 // because it should never change again.
392 log_file_header_.mutable_message()->mutate_monotonic_start_time(
393 new_monotonic_start_time.time_since_epoch().count());
394 log_file_header_.mutable_message()->mutate_realtime_start_time(
395 new_realtime_start_time.time_since_epoch().count());
396 }
397 }
398
Austin Schuh64fab802020-09-09 22:47:47 -0700399 // We don't have a good way to set the realtime start time on remote nodes.
400 // Confirm it remains consistent.
401 CHECK_EQ(log_file_header_.mutable_message()->has_realtime_start_time(),
402 message_reader.log_file_header()->has_realtime_start_time());
403
404 // Parts index will *not* match unless we set them to match. We only want
405 // to accept the start time and parts mismatching, so set them.
406 log_file_header_.mutable_message()->mutate_parts_index(
407 message_reader.log_file_header()->parts_index());
408
Austin Schuh2f8fd752020-09-01 22:38:28 -0700409 // Now compare that the headers match.
Austin Schuh64fab802020-09-09 22:47:47 -0700410 if (!CompareFlatBuffer(message_reader.raw_log_file_header(),
411 log_file_header_)) {
412 if (message_reader.log_file_header()->has_logger_uuid() &&
413 log_file_header_.message().has_logger_uuid() &&
414 message_reader.log_file_header()->logger_uuid()->string_view() !=
415 log_file_header_.message().logger_uuid()->string_view()) {
416 LOG(FATAL) << "Logger UUIDs don't match between log file chunks "
417 << filenames_[0] << " and " << filenames_[i]
418 << ", this is not supported.";
419 }
420 if (message_reader.log_file_header()->has_parts_uuid() &&
421 log_file_header_.message().has_parts_uuid() &&
422 message_reader.log_file_header()->parts_uuid()->string_view() !=
423 log_file_header_.message().parts_uuid()->string_view()) {
424 LOG(FATAL) << "Parts UUIDs don't match between log file chunks "
425 << filenames_[0] << " and " << filenames_[i]
426 << ", this is not supported.";
427 }
428
429 LOG(FATAL) << "Header is different between log file chunks "
430 << filenames_[0] << " and " << filenames_[i]
431 << ", this is not supported.";
432 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700433 }
Austin Schuh64fab802020-09-09 22:47:47 -0700434 // Put the parts index back to the first log file chunk.
435 log_file_header_.mutable_message()->mutate_parts_index(
436 message_reader_->log_file_header()->parts_index());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700437
Austin Schuh6f3babe2020-01-26 20:34:50 -0800438 // Setup per channel state.
Austin Schuh05b70472020-01-01 17:11:17 -0800439 channels_.resize(configuration()->channels()->size());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800440 for (ChannelData &channel_data : channels_) {
441 channel_data.data.split_reader = this;
442 // Build up the timestamp list.
443 if (configuration::MultiNode(configuration())) {
444 channel_data.timestamps.resize(configuration()->nodes()->size());
445 for (MessageHeaderQueue &queue : channel_data.timestamps) {
446 queue.timestamps = true;
447 queue.split_reader = this;
448 }
449 }
450 }
Austin Schuh05b70472020-01-01 17:11:17 -0800451
Austin Schuh6f3babe2020-01-26 20:34:50 -0800452 // Build up channels_to_write_ as an optimization to make it fast to figure
453 // out which datastructure to place any new data from a channel on.
454 for (const Channel *channel : *configuration()->channels()) {
455 // This is the main case. We will only see data on this node.
456 if (configuration::ChannelIsSendableOnNode(channel, node())) {
457 channels_to_write_.emplace_back(
458 &channels_[channels_to_write_.size()].data);
459 } else
460 // If we can't send, but can receive, we should be able to see
461 // timestamps here.
462 if (configuration::ChannelIsReadableOnNode(channel, node())) {
463 channels_to_write_.emplace_back(
464 &(channels_[channels_to_write_.size()]
465 .timestamps[configuration::GetNodeIndex(configuration(),
466 node())]));
467 } else {
468 channels_to_write_.emplace_back(nullptr);
469 }
470 }
Austin Schuh05b70472020-01-01 17:11:17 -0800471}
472
Austin Schuh6f3babe2020-01-26 20:34:50 -0800473bool SplitMessageReader::NextLogFile() {
Austin Schuhfa895892020-01-07 20:07:41 -0800474 if (next_filename_index_ == filenames_.size()) {
475 return false;
476 }
477 message_reader_ =
478 std::make_unique<MessageReader>(filenames_[next_filename_index_]);
479
480 // We can't support the config diverging between two log file headers. See if
481 // they are the same.
482 if (next_filename_index_ != 0) {
Austin Schuh64fab802020-09-09 22:47:47 -0700483 // In order for the headers to identically compare, they need to have the
484 // same parts_index. Rewrite the saved header with the new parts_index,
485 // compare, and then restore.
486 const int32_t original_parts_index =
487 log_file_header_.message().parts_index();
488 log_file_header_.mutable_message()->mutate_parts_index(
489 message_reader_->log_file_header()->parts_index());
490
Austin Schuh97789fc2020-08-01 14:42:45 -0700491 CHECK(CompareFlatBuffer(message_reader_->raw_log_file_header(),
492 log_file_header_))
Austin Schuhfa895892020-01-07 20:07:41 -0800493 << ": Header is different between log file chunks "
494 << filenames_[next_filename_index_] << " and "
495 << filenames_[next_filename_index_ - 1] << ", this is not supported.";
Austin Schuh64fab802020-09-09 22:47:47 -0700496
497 log_file_header_.mutable_message()->mutate_parts_index(
498 original_parts_index);
Austin Schuhfa895892020-01-07 20:07:41 -0800499 }
500
501 ++next_filename_index_;
502 return true;
503}
504
Austin Schuh6f3babe2020-01-26 20:34:50 -0800505bool SplitMessageReader::QueueMessages(
Austin Schuhcde938c2020-02-02 17:30:07 -0800506 monotonic_clock::time_point last_dequeued_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800507 // TODO(austin): Once we are happy that everything works, read a 256kb chunk
508 // to reduce the need to re-heap down below.
Austin Schuhcde938c2020-02-02 17:30:07 -0800509
510 // Special case no more data. Otherwise we blow up on the CHECK statement
511 // confirming that we have enough data queued.
512 if (at_end_) {
513 return false;
514 }
515
516 // If this isn't the first time around, confirm that we had enough data queued
517 // to follow the contract.
518 if (time_to_queue_ != monotonic_clock::min_time) {
519 CHECK_LE(last_dequeued_time,
520 newest_timestamp() - max_out_of_order_duration())
521 << " node " << FlatbufferToJson(node()) << " on " << this;
522
523 // Bail if there is enough data already queued.
524 if (last_dequeued_time < time_to_queue_) {
Austin Schuhee711052020-08-24 16:06:09 -0700525 VLOG(1) << MaybeNodeName(target_node_) << "All up to date on " << this
526 << ", dequeued " << last_dequeued_time << " queue time "
527 << time_to_queue_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800528 return true;
529 }
530 } else {
531 // Startup takes a special dance. We want to queue up until the start time,
532 // but we then want to find the next message to read. The conservative
533 // answer is to immediately trigger a second requeue to get things moving.
534 time_to_queue_ = monotonic_start_time();
535 QueueMessages(time_to_queue_);
536 }
537
538 // If we are asked to queue, queue for at least max_out_of_order_duration past
539 // the last known time in the log file (ie the newest timestep read). As long
540 // as we requeue exactly when time_to_queue_ is dequeued and go no further, we
541 // are safe. And since we pop in order, that works.
542 //
543 // Special case the start of the log file. There should be at most 1 message
544 // from each channel at the start of the log file. So always force the start
545 // of the log file to just be read.
546 time_to_queue_ = std::max(time_to_queue_, newest_timestamp());
Austin Schuhee711052020-08-24 16:06:09 -0700547 VLOG(1) << MaybeNodeName(target_node_) << "Queueing, going until "
548 << time_to_queue_ << " " << filename();
Austin Schuhcde938c2020-02-02 17:30:07 -0800549
550 bool was_emplaced = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800551 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800552 // Stop if we have enough.
Brian Silverman98360e22020-04-28 16:51:20 -0700553 if (newest_timestamp() > time_to_queue_ + max_out_of_order_duration() &&
Austin Schuhcde938c2020-02-02 17:30:07 -0800554 was_emplaced) {
Austin Schuhee711052020-08-24 16:06:09 -0700555 VLOG(1) << MaybeNodeName(target_node_) << "Done queueing on " << this
556 << ", queued to " << newest_timestamp() << " with requeue time "
557 << time_to_queue_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800558 return true;
559 }
Austin Schuh05b70472020-01-01 17:11:17 -0800560
Austin Schuh6f3babe2020-01-26 20:34:50 -0800561 if (std::optional<FlatbufferVector<MessageHeader>> msg =
562 message_reader_->ReadMessage()) {
563 const MessageHeader &header = msg.value().message();
564
Austin Schuhcde938c2020-02-02 17:30:07 -0800565 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
566 chrono::nanoseconds(header.monotonic_sent_time()));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800567
Austin Schuh0b5fd032020-03-28 17:36:49 -0700568 if (VLOG_IS_ON(2)) {
Austin Schuhee711052020-08-24 16:06:09 -0700569 LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this
570 << " " << filename() << " ttq: " << time_to_queue_ << " now "
571 << newest_timestamp() << " start time "
572 << monotonic_start_time() << " " << FlatbufferToJson(&header);
Austin Schuh0b5fd032020-03-28 17:36:49 -0700573 } else if (VLOG_IS_ON(1)) {
574 FlatbufferVector<MessageHeader> copy = msg.value();
575 copy.mutable_message()->clear_data();
Austin Schuhee711052020-08-24 16:06:09 -0700576 LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " "
577 << filename() << " ttq: " << time_to_queue_ << " now "
578 << newest_timestamp() << " start time "
579 << monotonic_start_time() << " " << FlatbufferToJson(copy);
Austin Schuh0b5fd032020-03-28 17:36:49 -0700580 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800581
582 const int channel_index = header.channel_index();
583 was_emplaced = channels_to_write_[channel_index]->emplace_back(
584 std::move(msg.value()));
585 if (was_emplaced) {
586 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
587 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800588 } else {
589 if (!NextLogFile()) {
Austin Schuhee711052020-08-24 16:06:09 -0700590 VLOG(1) << MaybeNodeName(target_node_) << "No more files, last was "
591 << filenames_.back();
Austin Schuhcde938c2020-02-02 17:30:07 -0800592 at_end_ = true;
Austin Schuh8bd96322020-02-13 21:18:22 -0800593 for (MessageHeaderQueue *queue : channels_to_write_) {
594 if (queue == nullptr || queue->timestamp_merger == nullptr) {
595 continue;
596 }
597 queue->timestamp_merger->NoticeAtEnd();
598 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800599 return false;
600 }
601 }
Austin Schuh05b70472020-01-01 17:11:17 -0800602 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800603}
604
605void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger,
606 int channel_index,
607 const Node *target_node) {
608 const Node *reinterpreted_target_node =
609 configuration::GetNodeOrDie(configuration(), target_node);
Austin Schuhee711052020-08-24 16:06:09 -0700610 target_node_ = reinterpreted_target_node;
611
Austin Schuh6f3babe2020-01-26 20:34:50 -0800612 const Channel *const channel =
613 configuration()->channels()->Get(channel_index);
614
Austin Schuhcde938c2020-02-02 17:30:07 -0800615 VLOG(1) << " Configuring merger " << this << " for channel " << channel_index
616 << " "
617 << configuration::CleanedChannelToString(
618 configuration()->channels()->Get(channel_index));
619
Austin Schuh6f3babe2020-01-26 20:34:50 -0800620 MessageHeaderQueue *message_header_queue = nullptr;
621
622 // Figure out if this log file is from our point of view, or the other node's
623 // point of view.
624 if (node() == reinterpreted_target_node) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800625 VLOG(1) << " Replaying as logged node " << filename();
626
627 if (configuration::ChannelIsSendableOnNode(channel, node())) {
628 VLOG(1) << " Data on node";
629 message_header_queue = &(channels_[channel_index].data);
630 } else if (configuration::ChannelIsReadableOnNode(channel, node())) {
631 VLOG(1) << " Timestamps on node";
632 message_header_queue =
633 &(channels_[channel_index].timestamps[configuration::GetNodeIndex(
634 configuration(), node())]);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800635 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800636 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800637 }
638 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800639 VLOG(1) << " Replaying as other node " << filename();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800640 // We are replaying from another node's point of view. The only interesting
Austin Schuhcde938c2020-02-02 17:30:07 -0800641 // data is data that is sent from our node and received on theirs.
642 if (configuration::ChannelIsReadableOnNode(channel,
643 reinterpreted_target_node) &&
644 configuration::ChannelIsSendableOnNode(channel, node())) {
645 VLOG(1) << " Readable on target node";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800646 // Data from another node.
647 message_header_queue = &(channels_[channel_index].data);
648 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800649 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800650 // This is either not sendable on the other node, or is a timestamp and
651 // therefore not interesting.
652 }
653 }
654
655 // If we found one, write it down. This will be nullptr when there is nothing
656 // relevant on this channel on this node for the target node. In that case,
657 // we want to drop the message instead of queueing it.
658 if (message_header_queue != nullptr) {
659 message_header_queue->timestamp_merger = timestamp_merger;
660 }
661}
662
663std::tuple<monotonic_clock::time_point, uint32_t,
664 FlatbufferVector<MessageHeader>>
665SplitMessageReader::PopOldest(int channel_index) {
666 CHECK_GT(channels_[channel_index].data.size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800667 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
668 timestamp = channels_[channel_index].data.front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800669 FlatbufferVector<MessageHeader> front =
670 std::move(channels_[channel_index].data.front());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700671 channels_[channel_index].data.PopFront();
Austin Schuhcde938c2020-02-02 17:30:07 -0800672
Austin Schuh2f8fd752020-09-01 22:38:28 -0700673 VLOG(1) << MaybeNodeName(target_node_) << "Popped Data " << this << " "
674 << std::get<0>(timestamp) << " for "
675 << configuration::StrippedChannelToString(
676 configuration()->channels()->Get(channel_index))
677 << " (" << channel_index << ")";
Austin Schuhcde938c2020-02-02 17:30:07 -0800678
679 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800680
681 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
682 std::move(front));
683}
684
685std::tuple<monotonic_clock::time_point, uint32_t,
686 FlatbufferVector<MessageHeader>>
Austin Schuh2f8fd752020-09-01 22:38:28 -0700687SplitMessageReader::PopOldestTimestamp(int channel, int node_index) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800688 CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800689 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
690 timestamp = channels_[channel].timestamps[node_index].front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800691 FlatbufferVector<MessageHeader> front =
692 std::move(channels_[channel].timestamps[node_index].front());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700693 channels_[channel].timestamps[node_index].PopFront();
Austin Schuhcde938c2020-02-02 17:30:07 -0800694
Austin Schuh2f8fd752020-09-01 22:38:28 -0700695 VLOG(1) << MaybeNodeName(target_node_) << "Popped timestamp " << this << " "
Austin Schuhee711052020-08-24 16:06:09 -0700696 << std::get<0>(timestamp) << " for "
697 << configuration::StrippedChannelToString(
698 configuration()->channels()->Get(channel))
Austin Schuh2f8fd752020-09-01 22:38:28 -0700699 << " on "
700 << configuration()->nodes()->Get(node_index)->name()->string_view()
701 << " (" << node_index << ")";
Austin Schuhcde938c2020-02-02 17:30:07 -0800702
703 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800704
705 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
706 std::move(front));
707}
708
Austin Schuhcde938c2020-02-02 17:30:07 -0800709bool SplitMessageReader::MessageHeaderQueue::emplace_back(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800710 FlatbufferVector<MessageHeader> &&msg) {
711 CHECK(split_reader != nullptr);
712
713 // If there is no timestamp merger for this queue, nobody is listening. Drop
714 // the message. This happens when a log file from another node is replayed,
715 // and the timestamp mergers down stream just don't care.
716 if (timestamp_merger == nullptr) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800717 return false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800718 }
719
720 CHECK(timestamps != msg.message().has_data())
721 << ": Got timestamps and data mixed up on a node. "
722 << FlatbufferToJson(msg);
723
724 data_.emplace_back(std::move(msg));
725
726 if (data_.size() == 1u) {
727 // Yup, new data. Notify.
728 if (timestamps) {
729 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
730 } else {
731 timestamp_merger->Update(split_reader, front_timestamp());
732 }
733 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800734
735 return true;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800736}
737
Austin Schuh2f8fd752020-09-01 22:38:28 -0700738void SplitMessageReader::MessageHeaderQueue::PopFront() {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800739 data_.pop_front();
740 if (data_.size() != 0u) {
741 // Yup, new data.
742 if (timestamps) {
743 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
744 } else {
745 timestamp_merger->Update(split_reader, front_timestamp());
746 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700747 } else {
748 // Poke anyways to update the heap.
749 if (timestamps) {
750 timestamp_merger->UpdateTimestamp(
751 nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr));
752 } else {
753 timestamp_merger->Update(
754 nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr));
755 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800756 }
Austin Schuh05b70472020-01-01 17:11:17 -0800757}
758
759namespace {
760
Austin Schuh6f3babe2020-01-26 20:34:50 -0800761bool SplitMessageReaderHeapCompare(
762 const std::tuple<monotonic_clock::time_point, uint32_t,
763 SplitMessageReader *>
764 first,
765 const std::tuple<monotonic_clock::time_point, uint32_t,
766 SplitMessageReader *>
767 second) {
768 if (std::get<0>(first) > std::get<0>(second)) {
769 return true;
770 } else if (std::get<0>(first) == std::get<0>(second)) {
771 if (std::get<1>(first) > std::get<1>(second)) {
772 return true;
773 } else if (std::get<1>(first) == std::get<1>(second)) {
774 return std::get<2>(first) > std::get<2>(second);
775 } else {
776 return false;
777 }
778 } else {
779 return false;
780 }
781}
782
Austin Schuh05b70472020-01-01 17:11:17 -0800783bool ChannelHeapCompare(
784 const std::pair<monotonic_clock::time_point, int> first,
785 const std::pair<monotonic_clock::time_point, int> second) {
786 if (first.first > second.first) {
787 return true;
788 } else if (first.first == second.first) {
789 return first.second > second.second;
790 } else {
791 return false;
792 }
793}
794
795} // namespace
796
Austin Schuh6f3babe2020-01-26 20:34:50 -0800797TimestampMerger::TimestampMerger(
798 const Configuration *configuration,
799 std::vector<SplitMessageReader *> split_message_readers, int channel_index,
800 const Node *target_node, ChannelMerger *channel_merger)
801 : configuration_(configuration),
802 split_message_readers_(std::move(split_message_readers)),
803 channel_index_(channel_index),
804 node_index_(configuration::MultiNode(configuration)
805 ? configuration::GetNodeIndex(configuration, target_node)
806 : -1),
807 channel_merger_(channel_merger) {
808 // Tell the readers we care so they know who to notify.
Austin Schuhcde938c2020-02-02 17:30:07 -0800809 VLOG(1) << "Configuring channel " << channel_index << " target node "
810 << FlatbufferToJson(target_node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800811 for (SplitMessageReader *reader : split_message_readers_) {
812 reader->SetTimestampMerger(this, channel_index, target_node);
813 }
814
815 // And then determine if we need to track timestamps.
816 const Channel *channel = configuration->channels()->Get(channel_index);
817 if (!configuration::ChannelIsSendableOnNode(channel, target_node) &&
818 configuration::ChannelIsReadableOnNode(channel, target_node)) {
819 has_timestamps_ = true;
820 }
821}
822
823void TimestampMerger::PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800824 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
825 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800826 SplitMessageReader *split_message_reader) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700827 if (split_message_reader != nullptr) {
828 DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(),
829 [split_message_reader](
830 const std::tuple<monotonic_clock::time_point,
831 uint32_t, SplitMessageReader *>
832 x) {
833 return std::get<2>(x) == split_message_reader;
834 }) == message_heap_.end())
835 << ": Pushing message when it is already in the heap.";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800836
Austin Schuh2f8fd752020-09-01 22:38:28 -0700837 message_heap_.push_back(std::make_tuple(
838 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800839
Austin Schuh2f8fd752020-09-01 22:38:28 -0700840 std::push_heap(message_heap_.begin(), message_heap_.end(),
841 &SplitMessageReaderHeapCompare);
842 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800843
844 // If we are just a data merger, don't wait for timestamps.
845 if (!has_timestamps_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700846 if (!message_heap_.empty()) {
847 channel_merger_->Update(std::get<0>(message_heap_[0]), channel_index_);
848 pushed_ = true;
849 } else {
850 // Remove ourselves if we are empty.
851 channel_merger_->Update(monotonic_clock::min_time, channel_index_);
852 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800853 }
854}
855
Austin Schuhcde938c2020-02-02 17:30:07 -0800856std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
857TimestampMerger::oldest_message() const {
858 CHECK_GT(message_heap_.size(), 0u);
859 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
860 oldest_message_reader = message_heap_.front();
861 return std::get<2>(oldest_message_reader)->oldest_message(channel_index_);
862}
863
864std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
865TimestampMerger::oldest_timestamp() const {
866 CHECK_GT(timestamp_heap_.size(), 0u);
867 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
868 oldest_message_reader = timestamp_heap_.front();
869 return std::get<2>(oldest_message_reader)
870 ->oldest_message(channel_index_, node_index_);
871}
872
Austin Schuh6f3babe2020-01-26 20:34:50 -0800873void TimestampMerger::PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800874 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
875 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800876 SplitMessageReader *split_message_reader) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700877 if (split_message_reader != nullptr) {
878 DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(),
879 [split_message_reader](
880 const std::tuple<monotonic_clock::time_point,
881 uint32_t, SplitMessageReader *>
882 x) {
883 return std::get<2>(x) == split_message_reader;
884 }) == timestamp_heap_.end())
885 << ": Pushing timestamp when it is already in the heap.";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800886
Austin Schuh2f8fd752020-09-01 22:38:28 -0700887 timestamp_heap_.push_back(std::make_tuple(
888 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800889
Austin Schuh2f8fd752020-09-01 22:38:28 -0700890 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
891 SplitMessageReaderHeapCompare);
892 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800893
894 // If we are a timestamp merger, don't wait for data. Missing data will be
895 // caught at read time.
896 if (has_timestamps_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700897 if (!timestamp_heap_.empty()) {
898 channel_merger_->Update(std::get<0>(timestamp_heap_[0]), channel_index_);
899 pushed_ = true;
900 } else {
901 // Remove ourselves if we are empty.
902 channel_merger_->Update(monotonic_clock::min_time, channel_index_);
903 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800904 }
905}
906
907std::tuple<monotonic_clock::time_point, uint32_t,
908 FlatbufferVector<MessageHeader>>
909TimestampMerger::PopMessageHeap() {
910 // Pop the oldest message reader pointer off the heap.
911 CHECK_GT(message_heap_.size(), 0u);
912 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
913 oldest_message_reader = message_heap_.front();
914
915 std::pop_heap(message_heap_.begin(), message_heap_.end(),
916 &SplitMessageReaderHeapCompare);
917 message_heap_.pop_back();
918
919 // Pop the oldest message. This re-pushes any messages from the reader to the
920 // message heap.
921 std::tuple<monotonic_clock::time_point, uint32_t,
922 FlatbufferVector<MessageHeader>>
923 oldest_message =
924 std::get<2>(oldest_message_reader)->PopOldest(channel_index_);
925
926 // Confirm that the time and queue_index we have recorded matches.
927 CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader));
928 CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader));
929
930 // Now, keep reading until we have found all duplicates.
Brian Silverman8a32ce62020-08-12 12:02:38 -0700931 while (!message_heap_.empty()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800932 // See if it is a duplicate.
933 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
934 next_oldest_message_reader = message_heap_.front();
935
Austin Schuhcde938c2020-02-02 17:30:07 -0800936 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
937 next_oldest_message_time = std::get<2>(next_oldest_message_reader)
938 ->oldest_message(channel_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800939
940 if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) &&
941 std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) {
942 // Pop the message reader pointer.
943 std::pop_heap(message_heap_.begin(), message_heap_.end(),
944 &SplitMessageReaderHeapCompare);
945 message_heap_.pop_back();
946
947 // Pop the next oldest message. This re-pushes any messages from the
948 // reader.
949 std::tuple<monotonic_clock::time_point, uint32_t,
950 FlatbufferVector<MessageHeader>>
951 next_oldest_message = std::get<2>(next_oldest_message_reader)
952 ->PopOldest(channel_index_);
953
954 // And make sure the message matches in it's entirety.
955 CHECK(std::get<2>(oldest_message).span() ==
956 std::get<2>(next_oldest_message).span())
957 << ": Data at the same timestamp doesn't match.";
958 } else {
959 break;
960 }
961 }
962
963 return oldest_message;
964}
965
966std::tuple<monotonic_clock::time_point, uint32_t,
967 FlatbufferVector<MessageHeader>>
968TimestampMerger::PopTimestampHeap() {
969 // Pop the oldest message reader pointer off the heap.
970 CHECK_GT(timestamp_heap_.size(), 0u);
971
972 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
973 oldest_timestamp_reader = timestamp_heap_.front();
974
975 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
976 &SplitMessageReaderHeapCompare);
977 timestamp_heap_.pop_back();
978
979 CHECK(node_index_ != -1) << ": Timestamps in a single node environment";
980
981 // Pop the oldest message. This re-pushes any timestamps from the reader to
982 // the timestamp heap.
983 std::tuple<monotonic_clock::time_point, uint32_t,
984 FlatbufferVector<MessageHeader>>
985 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700986 ->PopOldestTimestamp(channel_index_, node_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800987
988 // Confirm that the time we have recorded matches.
989 CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader));
990 CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader));
991
Austin Schuh2f8fd752020-09-01 22:38:28 -0700992 // Now, keep reading until we have found all duplicates.
993 while (!timestamp_heap_.empty()) {
994 // See if it is a duplicate.
995 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
996 next_oldest_timestamp_reader = timestamp_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800997
Austin Schuh2f8fd752020-09-01 22:38:28 -0700998 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
999 next_oldest_timestamp_time =
1000 std::get<2>(next_oldest_timestamp_reader)
1001 ->oldest_message(channel_index_, node_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001002
Austin Schuh2f8fd752020-09-01 22:38:28 -07001003 if (std::get<0>(next_oldest_timestamp_time) ==
1004 std::get<0>(oldest_timestamp) &&
1005 std::get<1>(next_oldest_timestamp_time) ==
1006 std::get<1>(oldest_timestamp)) {
1007 // Pop the timestamp reader pointer.
1008 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1009 &SplitMessageReaderHeapCompare);
1010 timestamp_heap_.pop_back();
1011
1012 // Pop the next oldest timestamp. This re-pushes any messages from the
1013 // reader.
1014 std::tuple<monotonic_clock::time_point, uint32_t,
1015 FlatbufferVector<MessageHeader>>
1016 next_oldest_timestamp =
1017 std::get<2>(next_oldest_timestamp_reader)
1018 ->PopOldestTimestamp(channel_index_, node_index_);
1019
1020 // And make sure the contents matches in it's entirety.
1021 CHECK(std::get<2>(oldest_timestamp).span() ==
1022 std::get<2>(next_oldest_timestamp).span())
1023 << ": Data at the same timestamp doesn't match, "
1024 << aos::FlatbufferToJson(std::get<2>(oldest_timestamp)) << " vs "
1025 << aos::FlatbufferToJson(std::get<2>(next_oldest_timestamp)) << " "
1026 << absl::BytesToHexString(std::string_view(
1027 reinterpret_cast<const char *>(
1028 std::get<2>(oldest_timestamp).span().data()),
1029 std::get<2>(oldest_timestamp).span().size()))
1030 << " vs "
1031 << absl::BytesToHexString(std::string_view(
1032 reinterpret_cast<const char *>(
1033 std::get<2>(next_oldest_timestamp).span().data()),
1034 std::get<2>(next_oldest_timestamp).span().size()));
1035
1036 } else {
1037 break;
1038 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001039 }
1040
Austin Schuh2f8fd752020-09-01 22:38:28 -07001041 return oldest_timestamp;
Austin Schuh8bd96322020-02-13 21:18:22 -08001042}
1043
Austin Schuh6f3babe2020-01-26 20:34:50 -08001044std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>>
1045TimestampMerger::PopOldest() {
1046 if (has_timestamps_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001047 VLOG(1) << "Looking for matching timestamp for "
1048 << configuration::StrippedChannelToString(
1049 configuration_->channels()->Get(channel_index_))
1050 << " (" << channel_index_ << ") "
1051 << " at " << std::get<0>(oldest_timestamp());
1052
Austin Schuh8bd96322020-02-13 21:18:22 -08001053 // Read the timestamps.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001054 std::tuple<monotonic_clock::time_point, uint32_t,
1055 FlatbufferVector<MessageHeader>>
1056 oldest_timestamp = PopTimestampHeap();
1057
1058 TimestampMerger::DeliveryTimestamp timestamp;
1059 timestamp.monotonic_event_time =
1060 monotonic_clock::time_point(chrono::nanoseconds(
1061 std::get<2>(oldest_timestamp).message().monotonic_sent_time()));
1062 timestamp.realtime_event_time =
1063 realtime_clock::time_point(chrono::nanoseconds(
1064 std::get<2>(oldest_timestamp).message().realtime_sent_time()));
1065
1066 // Consistency check.
1067 CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp));
1068 CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(),
1069 std::get<1>(oldest_timestamp));
1070
1071 monotonic_clock::time_point remote_timestamp_monotonic_time(
1072 chrono::nanoseconds(
1073 std::get<2>(oldest_timestamp).message().monotonic_remote_time()));
1074
Austin Schuh8bd96322020-02-13 21:18:22 -08001075 // See if we have any data. If not, pass the problem up the chain.
Brian Silverman8a32ce62020-08-12 12:02:38 -07001076 if (message_heap_.empty()) {
Austin Schuhee711052020-08-24 16:06:09 -07001077 LOG(WARNING) << MaybeNodeName(configuration_->nodes()->Get(node_index_))
1078 << "No data to match timestamp on "
1079 << configuration::CleanedChannelToString(
1080 configuration_->channels()->Get(channel_index_))
1081 << " (" << channel_index_ << ")";
Austin Schuh8bd96322020-02-13 21:18:22 -08001082 return std::make_tuple(timestamp,
1083 std::move(std::get<2>(oldest_timestamp)));
1084 }
1085
Austin Schuh6f3babe2020-01-26 20:34:50 -08001086 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001087 {
1088 // Ok, now try grabbing data until we find one which matches.
1089 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
1090 oldest_message_ref = oldest_message();
1091
1092 // Time at which the message was sent (this message is written from the
1093 // sending node's perspective.
1094 monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds(
1095 std::get<2>(oldest_message_ref)->monotonic_sent_time()));
1096
1097 if (remote_monotonic_time < remote_timestamp_monotonic_time) {
Austin Schuhee711052020-08-24 16:06:09 -07001098 LOG(WARNING) << configuration_->nodes()
1099 ->Get(node_index_)
1100 ->name()
1101 ->string_view()
1102 << " Undelivered message, skipping. Remote time is "
1103 << remote_monotonic_time << " timestamp is "
1104 << remote_timestamp_monotonic_time << " on channel "
1105 << configuration::StrippedChannelToString(
1106 configuration_->channels()->Get(channel_index_))
1107 << " (" << channel_index_ << ")";
Austin Schuhcde938c2020-02-02 17:30:07 -08001108 PopMessageHeap();
1109 continue;
1110 } else if (remote_monotonic_time > remote_timestamp_monotonic_time) {
Austin Schuhee711052020-08-24 16:06:09 -07001111 LOG(WARNING) << configuration_->nodes()
1112 ->Get(node_index_)
1113 ->name()
1114 ->string_view()
1115 << " Data not found. Remote time should be "
1116 << remote_timestamp_monotonic_time
1117 << ", message time is " << remote_monotonic_time
1118 << " on channel "
1119 << configuration::StrippedChannelToString(
1120 configuration_->channels()->Get(channel_index_))
Austin Schuh2f8fd752020-09-01 22:38:28 -07001121 << " (" << channel_index_ << ")"
1122 << (VLOG_IS_ON(1) ? DebugString() : "");
Austin Schuhcde938c2020-02-02 17:30:07 -08001123 return std::make_tuple(timestamp,
1124 std::move(std::get<2>(oldest_timestamp)));
1125 }
1126
1127 timestamp.monotonic_remote_time = remote_monotonic_time;
1128 }
1129
Austin Schuh2f8fd752020-09-01 22:38:28 -07001130 VLOG(1) << "Found matching data "
1131 << configuration::StrippedChannelToString(
1132 configuration_->channels()->Get(channel_index_))
1133 << " (" << channel_index_ << ")";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001134 std::tuple<monotonic_clock::time_point, uint32_t,
1135 FlatbufferVector<MessageHeader>>
1136 oldest_message = PopMessageHeap();
1137
Austin Schuh6f3babe2020-01-26 20:34:50 -08001138 timestamp.realtime_remote_time =
1139 realtime_clock::time_point(chrono::nanoseconds(
1140 std::get<2>(oldest_message).message().realtime_sent_time()));
1141 timestamp.remote_queue_index =
1142 std::get<2>(oldest_message).message().queue_index();
1143
Austin Schuhcde938c2020-02-02 17:30:07 -08001144 CHECK_EQ(timestamp.monotonic_remote_time,
1145 remote_timestamp_monotonic_time);
1146
1147 CHECK_EQ(timestamp.remote_queue_index,
1148 std::get<2>(oldest_timestamp).message().remote_queue_index())
1149 << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message())
1150 << " data "
1151 << FlatbufferToJson(&std::get<2>(oldest_message).message());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001152
Austin Schuh30dd5c52020-08-01 14:43:44 -07001153 return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message)));
Austin Schuh6f3babe2020-01-26 20:34:50 -08001154 }
1155 } else {
1156 std::tuple<monotonic_clock::time_point, uint32_t,
1157 FlatbufferVector<MessageHeader>>
1158 oldest_message = PopMessageHeap();
1159
1160 TimestampMerger::DeliveryTimestamp timestamp;
1161 timestamp.monotonic_event_time =
1162 monotonic_clock::time_point(chrono::nanoseconds(
1163 std::get<2>(oldest_message).message().monotonic_sent_time()));
1164 timestamp.realtime_event_time =
1165 realtime_clock::time_point(chrono::nanoseconds(
1166 std::get<2>(oldest_message).message().realtime_sent_time()));
1167 timestamp.remote_queue_index = 0xffffffff;
1168
1169 CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time);
1170 CHECK_EQ(std::get<1>(oldest_message),
1171 std::get<2>(oldest_message).message().queue_index());
1172
Austin Schuh30dd5c52020-08-01 14:43:44 -07001173 return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message)));
Austin Schuh6f3babe2020-01-26 20:34:50 -08001174 }
1175}
1176
Austin Schuh8bd96322020-02-13 21:18:22 -08001177void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); }
1178
Austin Schuh6f3babe2020-01-26 20:34:50 -08001179namespace {
1180std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders(
1181 const std::vector<std::vector<std::string>> &filenames) {
1182 CHECK_GT(filenames.size(), 0u);
1183 // Build up all the SplitMessageReaders.
1184 std::vector<std::unique_ptr<SplitMessageReader>> result;
1185 for (const std::vector<std::string> &filenames : filenames) {
1186 result.emplace_back(std::make_unique<SplitMessageReader>(filenames));
1187 }
1188 return result;
1189}
1190} // namespace
1191
1192ChannelMerger::ChannelMerger(
1193 const std::vector<std::vector<std::string>> &filenames)
1194 : split_message_readers_(MakeSplitMessageReaders(filenames)),
Austin Schuh97789fc2020-08-01 14:42:45 -07001195 log_file_header_(split_message_readers_[0]->raw_log_file_header()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001196 // Now, confirm that the configuration matches for each and pick a start time.
1197 // Also return the list of possible nodes.
1198 for (const std::unique_ptr<SplitMessageReader> &reader :
1199 split_message_readers_) {
1200 CHECK(CompareFlatBuffer(log_file_header_.message().configuration(),
1201 reader->log_file_header()->configuration()))
1202 << ": Replaying log files with different configurations isn't "
1203 "supported";
1204 }
1205
1206 nodes_ = configuration::GetNodes(configuration());
1207}
1208
1209bool ChannelMerger::SetNode(const Node *target_node) {
1210 std::vector<SplitMessageReader *> split_message_readers;
1211 for (const std::unique_ptr<SplitMessageReader> &reader :
1212 split_message_readers_) {
1213 split_message_readers.emplace_back(reader.get());
1214 }
1215
1216 // Go find a log_file_header for this node.
1217 {
1218 bool found_node = false;
1219
1220 for (const std::unique_ptr<SplitMessageReader> &reader :
1221 split_message_readers_) {
James Kuszmaulfc273dc2020-05-09 17:56:19 -07001222 // In order to identify which logfile(s) map to the target node, do a
1223 // logical comparison of the nodes, by confirming that we are either in a
1224 // single-node setup (where the nodes will both be nullptr) or that the
1225 // node names match (but the other node fields--e.g., hostname lists--may
1226 // not).
1227 const bool both_null =
1228 reader->node() == nullptr && target_node == nullptr;
1229 const bool both_have_name =
1230 (reader->node() != nullptr) && (target_node != nullptr) &&
1231 (reader->node()->has_name() && target_node->has_name());
1232 const bool node_names_identical =
1233 both_have_name &&
1234 (reader->node()->name()->string_view() ==
1235 target_node->name()->string_view());
1236 if (both_null || node_names_identical) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001237 if (!found_node) {
1238 found_node = true;
1239 log_file_header_ = CopyFlatBuffer(reader->log_file_header());
Austin Schuhcde938c2020-02-02 17:30:07 -08001240 VLOG(1) << "Found log file " << reader->filename() << " with node "
1241 << FlatbufferToJson(reader->node()) << " start_time "
1242 << monotonic_start_time();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001243 } else {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001244 // Find the earliest start time. That way, if we get a full log file
1245 // directly from the node, and a partial later, we start with the
1246 // full. Update our header to match that.
1247 const monotonic_clock::time_point new_monotonic_start_time(
1248 chrono::nanoseconds(
1249 reader->log_file_header()->monotonic_start_time()));
1250 const realtime_clock::time_point new_realtime_start_time(
1251 chrono::nanoseconds(
1252 reader->log_file_header()->realtime_start_time()));
1253
1254 if (monotonic_start_time() == monotonic_clock::min_time ||
1255 (new_monotonic_start_time != monotonic_clock::min_time &&
1256 new_monotonic_start_time < monotonic_start_time())) {
1257 log_file_header_.mutable_message()->mutate_monotonic_start_time(
1258 new_monotonic_start_time.time_since_epoch().count());
1259 log_file_header_.mutable_message()->mutate_realtime_start_time(
1260 new_realtime_start_time.time_since_epoch().count());
1261 VLOG(1) << "Updated log file " << reader->filename()
1262 << " with node " << FlatbufferToJson(reader->node())
1263 << " start_time " << new_monotonic_start_time;
1264 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001265 }
1266 }
1267 }
1268
1269 if (!found_node) {
1270 LOG(WARNING) << "Failed to find log file for node "
1271 << FlatbufferToJson(target_node);
1272 return false;
1273 }
1274 }
1275
1276 // Build up all the timestamp mergers. This connects up all the
1277 // SplitMessageReaders.
1278 timestamp_mergers_.reserve(configuration()->channels()->size());
1279 for (size_t channel_index = 0;
1280 channel_index < configuration()->channels()->size(); ++channel_index) {
1281 timestamp_mergers_.emplace_back(
1282 configuration(), split_message_readers, channel_index,
1283 configuration::GetNode(configuration(), target_node), this);
1284 }
1285
1286 // And prime everything.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001287 for (std::unique_ptr<SplitMessageReader> &split_message_reader :
1288 split_message_readers_) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001289 split_message_reader->QueueMessages(
1290 split_message_reader->monotonic_start_time());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001291 }
1292
1293 node_ = configuration::GetNodeOrDie(configuration(), target_node);
1294 return true;
1295}
1296
Austin Schuh858c9f32020-08-31 16:56:12 -07001297monotonic_clock::time_point ChannelMerger::OldestMessageTime() const {
Brian Silverman8a32ce62020-08-12 12:02:38 -07001298 if (channel_heap_.empty()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001299 return monotonic_clock::max_time;
1300 }
1301 return channel_heap_.front().first;
1302}
1303
1304void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp,
1305 int channel_index) {
1306 // Pop and recreate the heap if it has already been pushed. And since we are
1307 // pushing again, we don't need to clear pushed.
1308 if (timestamp_mergers_[channel_index].pushed()) {
Brian Silverman8a32ce62020-08-12 12:02:38 -07001309 const auto channel_iterator = std::find_if(
Austin Schuh6f3babe2020-01-26 20:34:50 -08001310 channel_heap_.begin(), channel_heap_.end(),
1311 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1312 return x.second == channel_index;
Brian Silverman8a32ce62020-08-12 12:02:38 -07001313 });
1314 DCHECK(channel_iterator != channel_heap_.end());
1315 if (std::get<0>(*channel_iterator) == timestamp) {
1316 // It's already in the heap, in the correct spot, so nothing
1317 // more for us to do here.
1318 return;
1319 }
1320 channel_heap_.erase(channel_iterator);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001321 std::make_heap(channel_heap_.begin(), channel_heap_.end(),
1322 ChannelHeapCompare);
1323 }
1324
Austin Schuh2f8fd752020-09-01 22:38:28 -07001325 if (timestamp == monotonic_clock::min_time) {
1326 timestamp_mergers_[channel_index].set_pushed(false);
1327 return;
1328 }
1329
Austin Schuh05b70472020-01-01 17:11:17 -08001330 channel_heap_.push_back(std::make_pair(timestamp, channel_index));
1331
1332 // The default sort puts the newest message first. Use a custom comparator to
1333 // put the oldest message first.
1334 std::push_heap(channel_heap_.begin(), channel_heap_.end(),
1335 ChannelHeapCompare);
1336}
1337
Austin Schuh2f8fd752020-09-01 22:38:28 -07001338void ChannelMerger::VerifyHeaps() {
Austin Schuh661a8d82020-09-13 17:25:56 -07001339 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1340 channel_heap_;
1341 std::make_heap(channel_heap.begin(), channel_heap.end(), &ChannelHeapCompare);
Austin Schuh2f8fd752020-09-01 22:38:28 -07001342
Austin Schuh661a8d82020-09-13 17:25:56 -07001343 for (size_t i = 0; i < channel_heap_.size(); ++i) {
1344 CHECK(channel_heap_[i] == channel_heap[i]) << ": Heaps diverged...";
1345 CHECK_EQ(
1346 std::get<0>(channel_heap[i]),
1347 timestamp_mergers_[std::get<1>(channel_heap[i])].channel_merger_time());
Austin Schuh2f8fd752020-09-01 22:38:28 -07001348 }
1349}
1350
Austin Schuh6f3babe2020-01-26 20:34:50 -08001351std::tuple<TimestampMerger::DeliveryTimestamp, int,
1352 FlatbufferVector<MessageHeader>>
1353ChannelMerger::PopOldest() {
Austin Schuh8bd96322020-02-13 21:18:22 -08001354 CHECK_GT(channel_heap_.size(), 0u);
Austin Schuh05b70472020-01-01 17:11:17 -08001355 std::pair<monotonic_clock::time_point, int> oldest_channel_data =
1356 channel_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001357 int channel_index = oldest_channel_data.second;
Austin Schuh05b70472020-01-01 17:11:17 -08001358 std::pop_heap(channel_heap_.begin(), channel_heap_.end(),
1359 &ChannelHeapCompare);
1360 channel_heap_.pop_back();
Austin Schuh8bd96322020-02-13 21:18:22 -08001361
Austin Schuh6f3babe2020-01-26 20:34:50 -08001362 timestamp_mergers_[channel_index].set_pushed(false);
Austin Schuh05b70472020-01-01 17:11:17 -08001363
Austin Schuh6f3babe2020-01-26 20:34:50 -08001364 TimestampMerger *merger = &timestamp_mergers_[channel_index];
Austin Schuh05b70472020-01-01 17:11:17 -08001365
Austin Schuhcde938c2020-02-02 17:30:07 -08001366 // Merger handles any queueing needed from here.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001367 std::tuple<TimestampMerger::DeliveryTimestamp,
1368 FlatbufferVector<MessageHeader>>
1369 message = merger->PopOldest();
Brian Silverman8a32ce62020-08-12 12:02:38 -07001370 DCHECK_EQ(std::get<0>(message).monotonic_event_time,
1371 oldest_channel_data.first)
1372 << ": channel_heap_ was corrupted for " << channel_index << ": "
1373 << DebugString();
Austin Schuh05b70472020-01-01 17:11:17 -08001374
Austin Schuh2f8fd752020-09-01 22:38:28 -07001375 CHECK_GE(std::get<0>(message).monotonic_event_time, last_popped_time_)
1376 << ": " << MaybeNodeName(log_file_header()->node())
1377 << "Messages came off the queue out of order. " << DebugString();
1378 last_popped_time_ = std::get<0>(message).monotonic_event_time;
1379
1380 VLOG(1) << "Popped " << last_popped_time_ << " "
1381 << configuration::StrippedChannelToString(
1382 configuration()->channels()->Get(channel_index))
1383 << " (" << channel_index << ")";
1384
Austin Schuh6f3babe2020-01-26 20:34:50 -08001385 return std::make_tuple(std::get<0>(message), channel_index,
1386 std::move(std::get<1>(message)));
1387}
1388
Austin Schuhcde938c2020-02-02 17:30:07 -08001389std::string SplitMessageReader::MessageHeaderQueue::DebugString() const {
1390 std::stringstream ss;
1391 for (size_t i = 0; i < data_.size(); ++i) {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001392 if (i < 5 || i + 5 > data_.size()) {
1393 if (timestamps) {
1394 ss << " msg: ";
1395 } else {
1396 ss << " timestamp: ";
1397 }
1398 ss << monotonic_clock::time_point(
1399 chrono::nanoseconds(data_[i].message().monotonic_sent_time()))
Austin Schuhcde938c2020-02-02 17:30:07 -08001400 << " ("
Austin Schuh2f8fd752020-09-01 22:38:28 -07001401 << realtime_clock::time_point(
1402 chrono::nanoseconds(data_[i].message().realtime_sent_time()))
1403 << ") " << data_[i].message().queue_index();
1404 if (timestamps) {
1405 ss << " <- remote "
1406 << monotonic_clock::time_point(chrono::nanoseconds(
1407 data_[i].message().monotonic_remote_time()))
1408 << " ("
1409 << realtime_clock::time_point(chrono::nanoseconds(
1410 data_[i].message().realtime_remote_time()))
1411 << ")";
1412 }
1413 ss << "\n";
1414 } else if (i == 5) {
1415 ss << " ...\n";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001416 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001417 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001418
Austin Schuhcde938c2020-02-02 17:30:07 -08001419 return ss.str();
1420}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001421
Austin Schuhcde938c2020-02-02 17:30:07 -08001422std::string SplitMessageReader::DebugString(int channel) const {
1423 std::stringstream ss;
1424 ss << "[\n";
1425 ss << channels_[channel].data.DebugString();
1426 ss << " ]";
1427 return ss.str();
1428}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001429
Austin Schuhcde938c2020-02-02 17:30:07 -08001430std::string SplitMessageReader::DebugString(int channel, int node_index) const {
1431 std::stringstream ss;
1432 ss << "[\n";
1433 ss << channels_[channel].timestamps[node_index].DebugString();
1434 ss << " ]";
1435 return ss.str();
1436}
1437
1438std::string TimestampMerger::DebugString() const {
1439 std::stringstream ss;
1440
1441 if (timestamp_heap_.size() > 0) {
1442 ss << " timestamp_heap {\n";
1443 std::vector<
1444 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1445 timestamp_heap = timestamp_heap_;
1446 while (timestamp_heap.size() > 0u) {
1447 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1448 oldest_timestamp_reader = timestamp_heap.front();
1449
1450 ss << " " << std::get<2>(oldest_timestamp_reader) << " "
1451 << std::get<0>(oldest_timestamp_reader) << " queue_index ("
1452 << std::get<1>(oldest_timestamp_reader) << ") ttq "
1453 << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " "
1454 << std::get<2>(oldest_timestamp_reader)->filename() << " -> "
1455 << std::get<2>(oldest_timestamp_reader)
1456 ->DebugString(channel_index_, node_index_)
1457 << "\n";
1458
1459 std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(),
1460 &SplitMessageReaderHeapCompare);
1461 timestamp_heap.pop_back();
1462 }
1463 ss << " }\n";
1464 }
1465
1466 ss << " message_heap {\n";
1467 {
1468 std::vector<
1469 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1470 message_heap = message_heap_;
Brian Silverman8a32ce62020-08-12 12:02:38 -07001471 while (!message_heap.empty()) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001472 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1473 oldest_message_reader = message_heap.front();
1474
1475 ss << " " << std::get<2>(oldest_message_reader) << " "
1476 << std::get<0>(oldest_message_reader) << " queue_index ("
1477 << std::get<1>(oldest_message_reader) << ") ttq "
1478 << std::get<2>(oldest_message_reader)->time_to_queue() << " "
1479 << std::get<2>(oldest_message_reader)->filename() << " -> "
1480 << std::get<2>(oldest_message_reader)->DebugString(channel_index_)
1481 << "\n";
1482
1483 std::pop_heap(message_heap.begin(), message_heap.end(),
1484 &SplitMessageReaderHeapCompare);
1485 message_heap.pop_back();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001486 }
Austin Schuh05b70472020-01-01 17:11:17 -08001487 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001488 ss << " }";
1489
1490 return ss.str();
1491}
1492
1493std::string ChannelMerger::DebugString() const {
1494 std::stringstream ss;
1495 ss << "start_time " << realtime_start_time() << " " << monotonic_start_time()
1496 << "\n";
1497 ss << "channel_heap {\n";
1498 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1499 channel_heap_;
Brian Silverman8a32ce62020-08-12 12:02:38 -07001500 while (!channel_heap.empty()) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001501 std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front();
1502 ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") "
1503 << configuration::CleanedChannelToString(
1504 configuration()->channels()->Get(std::get<1>(channel)))
1505 << "\n";
1506
1507 ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n";
1508
1509 std::pop_heap(channel_heap.begin(), channel_heap.end(),
1510 &ChannelHeapCompare);
1511 channel_heap.pop_back();
1512 }
1513 ss << "}";
1514
1515 return ss.str();
Austin Schuh05b70472020-01-01 17:11:17 -08001516}
1517
Austin Schuhee711052020-08-24 16:06:09 -07001518std::string MaybeNodeName(const Node *node) {
1519 if (node != nullptr) {
1520 return node->name()->str() + " ";
1521 }
1522 return "";
1523}
1524
Austin Schuha36c8902019-12-30 18:07:15 -08001525} // namespace logger
1526} // namespace aos