blob: 26c3825aa2ef77157cff1f3f9ad474fd47c2af13 [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
Brian Silvermand90905f2020-09-23 14:42:56 -070043DetachedBufferWriter::DetachedBufferWriter(DetachedBufferWriter &&other) {
Austin Schuh2f8fd752020-09-01 22:38:28 -070044 *this = std::move(other);
45}
46
Brian Silverman87ac0402020-09-17 14:47:01 -070047// When other is destroyed "soon" (which it should be because we're getting an
48// rvalue reference to it), it will flush etc all the data we have queued up
49// (because that data will then be its data).
Austin Schuh2f8fd752020-09-01 22:38:28 -070050DetachedBufferWriter &DetachedBufferWriter::operator=(
51 DetachedBufferWriter &&other) {
Austin Schuh2f8fd752020-09-01 22:38:28 -070052 std::swap(filename_, other.filename_);
53 std::swap(fd_, other.fd_);
54 std::swap(queued_size_, other.queued_size_);
55 std::swap(written_size_, other.written_size_);
56 std::swap(queue_, other.queue_);
57 std::swap(iovec_, other.iovec_);
58 return *this;
Austin Schuha36c8902019-12-30 18:07:15 -080059}
60
61void DetachedBufferWriter::QueueSizedFlatbuffer(
62 flatbuffers::FlatBufferBuilder *fbb) {
63 QueueSizedFlatbuffer(fbb->Release());
64}
65
Austin Schuhde031b72020-01-10 19:34:41 -080066void DetachedBufferWriter::WriteSizedFlatbuffer(
67 absl::Span<const uint8_t> span) {
68 // Cheat aggressively... Write out the queued up data, and then write this
69 // data once without buffering. It is hard to make a DetachedBuffer out of
70 // this data, and we don't want to worry about lifetimes.
71 Flush();
72 iovec_.clear();
73 iovec_.reserve(1);
74
75 struct iovec n;
76 n.iov_base = const_cast<uint8_t *>(span.data());
77 n.iov_len = span.size();
78 iovec_.emplace_back(n);
79
80 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
81
82 PCHECK(written == static_cast<ssize_t>(n.iov_len))
83 << ": Wrote " << written << " expected " << n.iov_len;
Brian Silverman98360e22020-04-28 16:51:20 -070084 written_size_ += written;
Austin Schuhde031b72020-01-10 19:34:41 -080085}
86
Austin Schuha36c8902019-12-30 18:07:15 -080087void DetachedBufferWriter::QueueSizedFlatbuffer(
88 flatbuffers::DetachedBuffer &&buffer) {
89 queued_size_ += buffer.size();
90 queue_.emplace_back(std::move(buffer));
91
92 // Flush if we are at the max number of iovs per writev, or have written
93 // enough data. Otherwise writev will fail with an invalid argument.
94 if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) ||
95 queue_.size() == IOV_MAX) {
96 Flush();
97 }
98}
99
100void DetachedBufferWriter::Flush() {
101 if (queue_.size() == 0u) {
102 return;
103 }
104 iovec_.clear();
105 iovec_.reserve(queue_.size());
106 size_t counted_size = 0;
107 for (size_t i = 0; i < queue_.size(); ++i) {
108 struct iovec n;
109 n.iov_base = queue_[i].data();
110 n.iov_len = queue_[i].size();
111 counted_size += n.iov_len;
112 iovec_.emplace_back(std::move(n));
113 }
114 CHECK_EQ(counted_size, queued_size_);
115 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
116
117 PCHECK(written == static_cast<ssize_t>(queued_size_))
118 << ": Wrote " << written << " expected " << queued_size_;
Brian Silverman98360e22020-04-28 16:51:20 -0700119 written_size_ += written;
Austin Schuha36c8902019-12-30 18:07:15 -0800120
121 queued_size_ = 0;
122 queue_.clear();
123 // TODO(austin): Handle partial writes in some way other than crashing...
124}
125
126flatbuffers::Offset<MessageHeader> PackMessage(
127 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
128 int channel_index, LogType log_type) {
129 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset;
130
131 switch (log_type) {
132 case LogType::kLogMessage:
133 case LogType::kLogMessageAndDeliveryTime:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800134 case LogType::kLogRemoteMessage:
Brian Silvermaneaa41d62020-07-08 19:47:35 -0700135 data_offset = fbb->CreateVector(
136 static_cast<const uint8_t *>(context.data), context.size);
Austin Schuha36c8902019-12-30 18:07:15 -0800137 break;
138
139 case LogType::kLogDeliveryTimeOnly:
140 break;
141 }
142
143 MessageHeader::Builder message_header_builder(*fbb);
144 message_header_builder.add_channel_index(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800145
146 switch (log_type) {
147 case LogType::kLogRemoteMessage:
148 message_header_builder.add_queue_index(context.remote_queue_index);
149 message_header_builder.add_monotonic_sent_time(
150 context.monotonic_remote_time.time_since_epoch().count());
151 message_header_builder.add_realtime_sent_time(
152 context.realtime_remote_time.time_since_epoch().count());
153 break;
154
155 case LogType::kLogMessage:
156 case LogType::kLogMessageAndDeliveryTime:
157 case LogType::kLogDeliveryTimeOnly:
158 message_header_builder.add_queue_index(context.queue_index);
159 message_header_builder.add_monotonic_sent_time(
160 context.monotonic_event_time.time_since_epoch().count());
161 message_header_builder.add_realtime_sent_time(
162 context.realtime_event_time.time_since_epoch().count());
163 break;
164 }
Austin Schuha36c8902019-12-30 18:07:15 -0800165
166 switch (log_type) {
167 case LogType::kLogMessage:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800168 case LogType::kLogRemoteMessage:
Austin Schuha36c8902019-12-30 18:07:15 -0800169 message_header_builder.add_data(data_offset);
170 break;
171
172 case LogType::kLogMessageAndDeliveryTime:
173 message_header_builder.add_data(data_offset);
174 [[fallthrough]];
175
176 case LogType::kLogDeliveryTimeOnly:
177 message_header_builder.add_monotonic_remote_time(
178 context.monotonic_remote_time.time_since_epoch().count());
179 message_header_builder.add_realtime_remote_time(
180 context.realtime_remote_time.time_since_epoch().count());
181 message_header_builder.add_remote_queue_index(context.remote_queue_index);
182 break;
183 }
184
185 return message_header_builder.Finish();
186}
187
Austin Schuh05b70472020-01-01 17:11:17 -0800188SpanReader::SpanReader(std::string_view filename)
Austin Schuh6f3babe2020-01-26 20:34:50 -0800189 : filename_(filename),
190 fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) {
Austin Schuh05b70472020-01-01 17:11:17 -0800191 PCHECK(fd_ != -1) << ": Failed to open " << filename;
192}
193
194absl::Span<const uint8_t> SpanReader::ReadMessage() {
195 // Make sure we have enough for the size.
196 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
197 if (!ReadBlock()) {
198 return absl::Span<const uint8_t>();
199 }
200 }
201
202 // Now make sure we have enough for the message.
203 const size_t data_size =
204 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
205 sizeof(flatbuffers::uoffset_t);
Austin Schuhe4fca832020-03-07 16:58:53 -0800206 if (data_size == sizeof(flatbuffers::uoffset_t)) {
207 LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping.";
208 LOG(ERROR) << " Rest of log file is "
209 << absl::BytesToHexString(std::string_view(
210 reinterpret_cast<const char *>(data_.data() +
211 consumed_data_),
212 data_.size() - consumed_data_));
213 return absl::Span<const uint8_t>();
214 }
Austin Schuh05b70472020-01-01 17:11:17 -0800215 while (data_.size() < consumed_data_ + data_size) {
216 if (!ReadBlock()) {
217 return absl::Span<const uint8_t>();
218 }
219 }
220
221 // And return it, consuming the data.
222 const uint8_t *data_ptr = data_.data() + consumed_data_;
223
224 consumed_data_ += data_size;
225
226 return absl::Span<const uint8_t>(data_ptr, data_size);
227}
228
229bool SpanReader::MessageAvailable() {
230 // Are we big enough to read the size?
231 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
232 return false;
233 }
234
235 // Then, are we big enough to read the full message?
236 const size_t data_size =
237 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
238 sizeof(flatbuffers::uoffset_t);
239 if (data_.size() < consumed_data_ + data_size) {
240 return false;
241 }
242
243 return true;
244}
245
246bool SpanReader::ReadBlock() {
247 if (end_of_file_) {
248 return false;
249 }
250
251 // Appends 256k. This is enough that the read call is efficient. We don't
252 // want to spend too much time reading small chunks because the syscalls for
253 // that will be expensive.
254 constexpr size_t kReadSize = 256 * 1024;
255
256 // Strip off any unused data at the front.
257 if (consumed_data_ != 0) {
258 data_.erase(data_.begin(), data_.begin() + consumed_data_);
259 consumed_data_ = 0;
260 }
261
262 const size_t starting_size = data_.size();
263
264 // This should automatically grow the backing store. It won't shrink if we
265 // get a small chunk later. This reduces allocations when we want to append
266 // more data.
267 data_.resize(data_.size() + kReadSize);
268
269 ssize_t count = read(fd_, &data_[starting_size], kReadSize);
270 data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0)));
271 if (count == 0) {
272 end_of_file_ = true;
273 return false;
274 }
275 PCHECK(count > 0);
276
277 return true;
278}
279
Austin Schuh6f3babe2020-01-26 20:34:50 -0800280FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename) {
281 SpanReader span_reader(filename);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800282 absl::Span<const uint8_t> config_data = span_reader.ReadMessage();
283
284 // Make sure something was read.
Austin Schuh97789fc2020-08-01 14:42:45 -0700285 CHECK(config_data != absl::Span<const uint8_t>())
286 << ": Failed to read header from: " << filename;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800287
Austin Schuh5212cad2020-09-09 23:12:09 -0700288 // And copy the config so we have it forever, removing the size prefix.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800289 std::vector<uint8_t> data(
290 config_data.begin() + sizeof(flatbuffers::uoffset_t), config_data.end());
291 return FlatbufferVector<LogFileHeader>(std::move(data));
292}
293
Austin Schuh5212cad2020-09-09 23:12:09 -0700294FlatbufferVector<MessageHeader> ReadNthMessage(std::string_view filename,
295 size_t n) {
296 SpanReader span_reader(filename);
297 absl::Span<const uint8_t> data_span = span_reader.ReadMessage();
298 for (size_t i = 0; i < n + 1; ++i) {
299 data_span = span_reader.ReadMessage();
300
301 // Make sure something was read.
302 CHECK(data_span != absl::Span<const uint8_t>())
303 << ": Failed to read data from: " << filename;
304 }
305
306 // And copy the data so we have it forever.
307 std::vector<uint8_t> data(data_span.begin() + sizeof(flatbuffers::uoffset_t),
308 data_span.end());
309 return FlatbufferVector<MessageHeader>(std::move(data));
310}
311
Austin Schuh05b70472020-01-01 17:11:17 -0800312MessageReader::MessageReader(std::string_view filename)
Austin Schuh97789fc2020-08-01 14:42:45 -0700313 : span_reader_(filename),
314 raw_log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) {
Austin Schuh05b70472020-01-01 17:11:17 -0800315 // Make sure we have enough to read the size.
Austin Schuh97789fc2020-08-01 14:42:45 -0700316 absl::Span<const uint8_t> header_data = span_reader_.ReadMessage();
Austin Schuh05b70472020-01-01 17:11:17 -0800317
318 // Make sure something was read.
Austin Schuh97789fc2020-08-01 14:42:45 -0700319 CHECK(header_data != absl::Span<const uint8_t>())
320 << ": Failed to read header from: " << filename;
Austin Schuh05b70472020-01-01 17:11:17 -0800321
Austin Schuh97789fc2020-08-01 14:42:45 -0700322 // And copy the header data so we have it forever.
323 std::vector<uint8_t> header_data_copy(
324 header_data.begin() + sizeof(flatbuffers::uoffset_t), header_data.end());
325 raw_log_file_header_ =
326 FlatbufferVector<LogFileHeader>(std::move(header_data_copy));
Austin Schuh05b70472020-01-01 17:11:17 -0800327
Austin Schuhcde938c2020-02-02 17:30:07 -0800328 max_out_of_order_duration_ =
Austin Schuh2f8fd752020-09-01 22:38:28 -0700329 chrono::nanoseconds(log_file_header()->max_out_of_order_duration());
Austin Schuhcde938c2020-02-02 17:30:07 -0800330
331 VLOG(1) << "Opened " << filename << " as node "
332 << FlatbufferToJson(log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800333}
334
335std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() {
336 absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage();
337 if (msg_data == absl::Span<const uint8_t>()) {
338 return std::nullopt;
339 }
340
341 FlatbufferVector<MessageHeader> result{std::vector<uint8_t>(
342 msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())};
343
344 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
345 chrono::nanoseconds(result.message().monotonic_sent_time()));
346
347 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
Austin Schuh8bd96322020-02-13 21:18:22 -0800348 VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800349 return std::move(result);
Austin Schuh05b70472020-01-01 17:11:17 -0800350}
351
Austin Schuh6f3babe2020-01-26 20:34:50 -0800352SplitMessageReader::SplitMessageReader(
Austin Schuhfa895892020-01-07 20:07:41 -0800353 const std::vector<std::string> &filenames)
354 : filenames_(filenames),
Austin Schuh97789fc2020-08-01 14:42:45 -0700355 log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) {
Austin Schuhfa895892020-01-07 20:07:41 -0800356 CHECK(NextLogFile()) << ": filenames is empty. Need files to read.";
357
Austin Schuh6f3babe2020-01-26 20:34:50 -0800358 // Grab any log file header. They should all match (and we will check as we
359 // open more of them).
Austin Schuh97789fc2020-08-01 14:42:45 -0700360 log_file_header_ = message_reader_->raw_log_file_header();
Austin Schuhfa895892020-01-07 20:07:41 -0800361
Austin Schuh2f8fd752020-09-01 22:38:28 -0700362 for (size_t i = 1; i < filenames_.size(); ++i) {
363 MessageReader message_reader(filenames_[i]);
364
365 const monotonic_clock::time_point new_monotonic_start_time(
366 chrono::nanoseconds(
367 message_reader.log_file_header()->monotonic_start_time()));
368 const realtime_clock::time_point new_realtime_start_time(
369 chrono::nanoseconds(
370 message_reader.log_file_header()->realtime_start_time()));
371
372 // There are 2 types of part files. Part files from before time estimation
373 // has started, and part files after. We don't declare a log file "started"
374 // until time estimation is up. And once a log file starts, it should never
375 // stop again, and should remain constant.
376 // To compare both types of headers, we mutate our saved copy of the header
377 // to match the next chunk by updating time if we detect a stopped ->
378 // started transition.
379 if (monotonic_start_time() == monotonic_clock::min_time) {
380 CHECK_EQ(realtime_start_time(), realtime_clock::min_time);
381 // We should only be missing the monotonic start time when logging data
Brian Silverman87ac0402020-09-17 14:47:01 -0700382 // for remote nodes. We don't have a good way to determine the remote
Austin Schuh2f8fd752020-09-01 22:38:28 -0700383 // realtime offset, so it shouldn't be filled out.
384 // TODO(austin): If we have a good way, feel free to fill it out. It
385 // probably won't be better than we could do in post though with the same
386 // data.
387 CHECK(!log_file_header_.mutable_message()->has_realtime_start_time());
388 if (new_monotonic_start_time != monotonic_clock::min_time) {
389 // If we finally found our start time, update the header. Do this once
390 // because it should never change again.
391 log_file_header_.mutable_message()->mutate_monotonic_start_time(
392 new_monotonic_start_time.time_since_epoch().count());
393 log_file_header_.mutable_message()->mutate_realtime_start_time(
394 new_realtime_start_time.time_since_epoch().count());
395 }
396 }
397
Austin Schuh64fab802020-09-09 22:47:47 -0700398 // We don't have a good way to set the realtime start time on remote nodes.
399 // Confirm it remains consistent.
400 CHECK_EQ(log_file_header_.mutable_message()->has_realtime_start_time(),
401 message_reader.log_file_header()->has_realtime_start_time());
402
403 // Parts index will *not* match unless we set them to match. We only want
404 // to accept the start time and parts mismatching, so set them.
405 log_file_header_.mutable_message()->mutate_parts_index(
406 message_reader.log_file_header()->parts_index());
407
Austin Schuh2f8fd752020-09-01 22:38:28 -0700408 // Now compare that the headers match.
Austin Schuh64fab802020-09-09 22:47:47 -0700409 if (!CompareFlatBuffer(message_reader.raw_log_file_header(),
410 log_file_header_)) {
411 if (message_reader.log_file_header()->has_logger_uuid() &&
412 log_file_header_.message().has_logger_uuid() &&
413 message_reader.log_file_header()->logger_uuid()->string_view() !=
414 log_file_header_.message().logger_uuid()->string_view()) {
415 LOG(FATAL) << "Logger UUIDs don't match between log file chunks "
416 << filenames_[0] << " and " << filenames_[i]
417 << ", this is not supported.";
418 }
419 if (message_reader.log_file_header()->has_parts_uuid() &&
420 log_file_header_.message().has_parts_uuid() &&
421 message_reader.log_file_header()->parts_uuid()->string_view() !=
422 log_file_header_.message().parts_uuid()->string_view()) {
423 LOG(FATAL) << "Parts UUIDs don't match between log file chunks "
424 << filenames_[0] << " and " << filenames_[i]
425 << ", this is not supported.";
426 }
427
428 LOG(FATAL) << "Header is different between log file chunks "
429 << filenames_[0] << " and " << filenames_[i]
430 << ", this is not supported.";
431 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700432 }
Austin Schuh64fab802020-09-09 22:47:47 -0700433 // Put the parts index back to the first log file chunk.
434 log_file_header_.mutable_message()->mutate_parts_index(
435 message_reader_->log_file_header()->parts_index());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700436
Austin Schuh6f3babe2020-01-26 20:34:50 -0800437 // Setup per channel state.
Austin Schuh05b70472020-01-01 17:11:17 -0800438 channels_.resize(configuration()->channels()->size());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800439 for (ChannelData &channel_data : channels_) {
440 channel_data.data.split_reader = this;
441 // Build up the timestamp list.
442 if (configuration::MultiNode(configuration())) {
443 channel_data.timestamps.resize(configuration()->nodes()->size());
444 for (MessageHeaderQueue &queue : channel_data.timestamps) {
445 queue.timestamps = true;
446 queue.split_reader = this;
447 }
448 }
449 }
Austin Schuh05b70472020-01-01 17:11:17 -0800450
Austin Schuh6f3babe2020-01-26 20:34:50 -0800451 // Build up channels_to_write_ as an optimization to make it fast to figure
452 // out which datastructure to place any new data from a channel on.
453 for (const Channel *channel : *configuration()->channels()) {
454 // This is the main case. We will only see data on this node.
455 if (configuration::ChannelIsSendableOnNode(channel, node())) {
456 channels_to_write_.emplace_back(
457 &channels_[channels_to_write_.size()].data);
458 } else
459 // If we can't send, but can receive, we should be able to see
460 // timestamps here.
461 if (configuration::ChannelIsReadableOnNode(channel, node())) {
462 channels_to_write_.emplace_back(
463 &(channels_[channels_to_write_.size()]
464 .timestamps[configuration::GetNodeIndex(configuration(),
465 node())]));
466 } else {
467 channels_to_write_.emplace_back(nullptr);
468 }
469 }
Austin Schuh05b70472020-01-01 17:11:17 -0800470}
471
Austin Schuh6f3babe2020-01-26 20:34:50 -0800472bool SplitMessageReader::NextLogFile() {
Austin Schuhfa895892020-01-07 20:07:41 -0800473 if (next_filename_index_ == filenames_.size()) {
474 return false;
475 }
476 message_reader_ =
477 std::make_unique<MessageReader>(filenames_[next_filename_index_]);
478
479 // We can't support the config diverging between two log file headers. See if
480 // they are the same.
481 if (next_filename_index_ != 0) {
Austin Schuh64fab802020-09-09 22:47:47 -0700482 // In order for the headers to identically compare, they need to have the
483 // same parts_index. Rewrite the saved header with the new parts_index,
484 // compare, and then restore.
485 const int32_t original_parts_index =
486 log_file_header_.message().parts_index();
487 log_file_header_.mutable_message()->mutate_parts_index(
488 message_reader_->log_file_header()->parts_index());
489
Austin Schuh97789fc2020-08-01 14:42:45 -0700490 CHECK(CompareFlatBuffer(message_reader_->raw_log_file_header(),
491 log_file_header_))
Austin Schuhfa895892020-01-07 20:07:41 -0800492 << ": Header is different between log file chunks "
493 << filenames_[next_filename_index_] << " and "
494 << filenames_[next_filename_index_ - 1] << ", this is not supported.";
Austin Schuh64fab802020-09-09 22:47:47 -0700495
496 log_file_header_.mutable_message()->mutate_parts_index(
497 original_parts_index);
Austin Schuhfa895892020-01-07 20:07:41 -0800498 }
499
500 ++next_filename_index_;
501 return true;
502}
503
Austin Schuh6f3babe2020-01-26 20:34:50 -0800504bool SplitMessageReader::QueueMessages(
Austin Schuhcde938c2020-02-02 17:30:07 -0800505 monotonic_clock::time_point last_dequeued_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800506 // TODO(austin): Once we are happy that everything works, read a 256kb chunk
507 // to reduce the need to re-heap down below.
Austin Schuhcde938c2020-02-02 17:30:07 -0800508
509 // Special case no more data. Otherwise we blow up on the CHECK statement
510 // confirming that we have enough data queued.
511 if (at_end_) {
512 return false;
513 }
514
515 // If this isn't the first time around, confirm that we had enough data queued
516 // to follow the contract.
517 if (time_to_queue_ != monotonic_clock::min_time) {
518 CHECK_LE(last_dequeued_time,
519 newest_timestamp() - max_out_of_order_duration())
520 << " node " << FlatbufferToJson(node()) << " on " << this;
521
522 // Bail if there is enough data already queued.
523 if (last_dequeued_time < time_to_queue_) {
Austin Schuhee711052020-08-24 16:06:09 -0700524 VLOG(1) << MaybeNodeName(target_node_) << "All up to date on " << this
525 << ", dequeued " << last_dequeued_time << " queue time "
526 << time_to_queue_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800527 return true;
528 }
529 } else {
530 // Startup takes a special dance. We want to queue up until the start time,
531 // but we then want to find the next message to read. The conservative
532 // answer is to immediately trigger a second requeue to get things moving.
533 time_to_queue_ = monotonic_start_time();
534 QueueMessages(time_to_queue_);
535 }
536
537 // If we are asked to queue, queue for at least max_out_of_order_duration past
538 // the last known time in the log file (ie the newest timestep read). As long
539 // as we requeue exactly when time_to_queue_ is dequeued and go no further, we
540 // are safe. And since we pop in order, that works.
541 //
542 // Special case the start of the log file. There should be at most 1 message
543 // from each channel at the start of the log file. So always force the start
544 // of the log file to just be read.
545 time_to_queue_ = std::max(time_to_queue_, newest_timestamp());
Austin Schuhee711052020-08-24 16:06:09 -0700546 VLOG(1) << MaybeNodeName(target_node_) << "Queueing, going until "
547 << time_to_queue_ << " " << filename();
Austin Schuhcde938c2020-02-02 17:30:07 -0800548
549 bool was_emplaced = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800550 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800551 // Stop if we have enough.
Brian Silverman98360e22020-04-28 16:51:20 -0700552 if (newest_timestamp() > time_to_queue_ + max_out_of_order_duration() &&
Austin Schuhcde938c2020-02-02 17:30:07 -0800553 was_emplaced) {
Austin Schuhee711052020-08-24 16:06:09 -0700554 VLOG(1) << MaybeNodeName(target_node_) << "Done queueing on " << this
555 << ", queued to " << newest_timestamp() << " with requeue time "
556 << time_to_queue_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800557 return true;
558 }
Austin Schuh05b70472020-01-01 17:11:17 -0800559
Austin Schuh6f3babe2020-01-26 20:34:50 -0800560 if (std::optional<FlatbufferVector<MessageHeader>> msg =
561 message_reader_->ReadMessage()) {
562 const MessageHeader &header = msg.value().message();
563
Austin Schuhcde938c2020-02-02 17:30:07 -0800564 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
565 chrono::nanoseconds(header.monotonic_sent_time()));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800566
Austin Schuh0b5fd032020-03-28 17:36:49 -0700567 if (VLOG_IS_ON(2)) {
Brian Silvermand90905f2020-09-23 14:42:56 -0700568 LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " "
569 << filename() << " ttq: " << time_to_queue_ << " now "
Austin Schuhee711052020-08-24 16:06:09 -0700570 << newest_timestamp() << " start time "
571 << monotonic_start_time() << " " << FlatbufferToJson(&header);
Austin Schuh0b5fd032020-03-28 17:36:49 -0700572 } else if (VLOG_IS_ON(1)) {
573 FlatbufferVector<MessageHeader> copy = msg.value();
574 copy.mutable_message()->clear_data();
Austin Schuhee711052020-08-24 16:06:09 -0700575 LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " "
576 << filename() << " ttq: " << time_to_queue_ << " now "
577 << newest_timestamp() << " start time "
578 << monotonic_start_time() << " " << FlatbufferToJson(copy);
Austin Schuh0b5fd032020-03-28 17:36:49 -0700579 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800580
581 const int channel_index = header.channel_index();
582 was_emplaced = channels_to_write_[channel_index]->emplace_back(
583 std::move(msg.value()));
584 if (was_emplaced) {
585 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
586 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800587 } else {
588 if (!NextLogFile()) {
Austin Schuhee711052020-08-24 16:06:09 -0700589 VLOG(1) << MaybeNodeName(target_node_) << "No more files, last was "
590 << filenames_.back();
Austin Schuhcde938c2020-02-02 17:30:07 -0800591 at_end_ = true;
Austin Schuh8bd96322020-02-13 21:18:22 -0800592 for (MessageHeaderQueue *queue : channels_to_write_) {
593 if (queue == nullptr || queue->timestamp_merger == nullptr) {
594 continue;
595 }
596 queue->timestamp_merger->NoticeAtEnd();
597 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800598 return false;
599 }
600 }
Austin Schuh05b70472020-01-01 17:11:17 -0800601 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800602}
603
604void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger,
605 int channel_index,
606 const Node *target_node) {
607 const Node *reinterpreted_target_node =
608 configuration::GetNodeOrDie(configuration(), target_node);
Austin Schuhee711052020-08-24 16:06:09 -0700609 target_node_ = reinterpreted_target_node;
610
Austin Schuh6f3babe2020-01-26 20:34:50 -0800611 const Channel *const channel =
612 configuration()->channels()->Get(channel_index);
613
Austin Schuhcde938c2020-02-02 17:30:07 -0800614 VLOG(1) << " Configuring merger " << this << " for channel " << channel_index
615 << " "
616 << configuration::CleanedChannelToString(
617 configuration()->channels()->Get(channel_index));
618
Austin Schuh6f3babe2020-01-26 20:34:50 -0800619 MessageHeaderQueue *message_header_queue = nullptr;
620
621 // Figure out if this log file is from our point of view, or the other node's
622 // point of view.
623 if (node() == reinterpreted_target_node) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800624 VLOG(1) << " Replaying as logged node " << filename();
625
626 if (configuration::ChannelIsSendableOnNode(channel, node())) {
627 VLOG(1) << " Data on node";
628 message_header_queue = &(channels_[channel_index].data);
629 } else if (configuration::ChannelIsReadableOnNode(channel, node())) {
630 VLOG(1) << " Timestamps on node";
631 message_header_queue =
632 &(channels_[channel_index].timestamps[configuration::GetNodeIndex(
633 configuration(), node())]);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800634 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800635 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800636 }
637 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800638 VLOG(1) << " Replaying as other node " << filename();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800639 // We are replaying from another node's point of view. The only interesting
Austin Schuhcde938c2020-02-02 17:30:07 -0800640 // data is data that is sent from our node and received on theirs.
641 if (configuration::ChannelIsReadableOnNode(channel,
642 reinterpreted_target_node) &&
643 configuration::ChannelIsSendableOnNode(channel, node())) {
644 VLOG(1) << " Readable on target node";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800645 // Data from another node.
646 message_header_queue = &(channels_[channel_index].data);
647 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800648 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800649 // This is either not sendable on the other node, or is a timestamp and
650 // therefore not interesting.
651 }
652 }
653
654 // If we found one, write it down. This will be nullptr when there is nothing
655 // relevant on this channel on this node for the target node. In that case,
656 // we want to drop the message instead of queueing it.
657 if (message_header_queue != nullptr) {
658 message_header_queue->timestamp_merger = timestamp_merger;
659 }
660}
661
662std::tuple<monotonic_clock::time_point, uint32_t,
663 FlatbufferVector<MessageHeader>>
664SplitMessageReader::PopOldest(int channel_index) {
665 CHECK_GT(channels_[channel_index].data.size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800666 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
667 timestamp = channels_[channel_index].data.front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800668 FlatbufferVector<MessageHeader> front =
669 std::move(channels_[channel_index].data.front());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700670 channels_[channel_index].data.PopFront();
Austin Schuhcde938c2020-02-02 17:30:07 -0800671
Austin Schuh2f8fd752020-09-01 22:38:28 -0700672 VLOG(1) << MaybeNodeName(target_node_) << "Popped Data " << this << " "
673 << std::get<0>(timestamp) << " for "
674 << configuration::StrippedChannelToString(
675 configuration()->channels()->Get(channel_index))
676 << " (" << channel_index << ")";
Austin Schuhcde938c2020-02-02 17:30:07 -0800677
678 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800679
680 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
681 std::move(front));
682}
683
684std::tuple<monotonic_clock::time_point, uint32_t,
685 FlatbufferVector<MessageHeader>>
Austin Schuh2f8fd752020-09-01 22:38:28 -0700686SplitMessageReader::PopOldestTimestamp(int channel, int node_index) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800687 CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800688 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
689 timestamp = channels_[channel].timestamps[node_index].front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800690 FlatbufferVector<MessageHeader> front =
691 std::move(channels_[channel].timestamps[node_index].front());
Austin Schuh2f8fd752020-09-01 22:38:28 -0700692 channels_[channel].timestamps[node_index].PopFront();
Austin Schuhcde938c2020-02-02 17:30:07 -0800693
Austin Schuh2f8fd752020-09-01 22:38:28 -0700694 VLOG(1) << MaybeNodeName(target_node_) << "Popped timestamp " << this << " "
Austin Schuhee711052020-08-24 16:06:09 -0700695 << std::get<0>(timestamp) << " for "
696 << configuration::StrippedChannelToString(
697 configuration()->channels()->Get(channel))
Austin Schuh2f8fd752020-09-01 22:38:28 -0700698 << " on "
699 << configuration()->nodes()->Get(node_index)->name()->string_view()
700 << " (" << node_index << ")";
Austin Schuhcde938c2020-02-02 17:30:07 -0800701
702 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800703
704 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
705 std::move(front));
706}
707
Austin Schuhcde938c2020-02-02 17:30:07 -0800708bool SplitMessageReader::MessageHeaderQueue::emplace_back(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800709 FlatbufferVector<MessageHeader> &&msg) {
710 CHECK(split_reader != nullptr);
711
712 // If there is no timestamp merger for this queue, nobody is listening. Drop
713 // the message. This happens when a log file from another node is replayed,
714 // and the timestamp mergers down stream just don't care.
715 if (timestamp_merger == nullptr) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800716 return false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800717 }
718
719 CHECK(timestamps != msg.message().has_data())
720 << ": Got timestamps and data mixed up on a node. "
721 << FlatbufferToJson(msg);
722
723 data_.emplace_back(std::move(msg));
724
725 if (data_.size() == 1u) {
726 // Yup, new data. Notify.
727 if (timestamps) {
728 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
729 } else {
730 timestamp_merger->Update(split_reader, front_timestamp());
731 }
732 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800733
734 return true;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800735}
736
Austin Schuh2f8fd752020-09-01 22:38:28 -0700737void SplitMessageReader::MessageHeaderQueue::PopFront() {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800738 data_.pop_front();
739 if (data_.size() != 0u) {
740 // Yup, new data.
741 if (timestamps) {
742 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
743 } else {
744 timestamp_merger->Update(split_reader, front_timestamp());
745 }
Austin Schuh2f8fd752020-09-01 22:38:28 -0700746 } else {
747 // Poke anyways to update the heap.
748 if (timestamps) {
749 timestamp_merger->UpdateTimestamp(
750 nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr));
751 } else {
752 timestamp_merger->Update(
753 nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr));
754 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800755 }
Austin Schuh05b70472020-01-01 17:11:17 -0800756}
757
758namespace {
759
Austin Schuh6f3babe2020-01-26 20:34:50 -0800760bool SplitMessageReaderHeapCompare(
761 const std::tuple<monotonic_clock::time_point, uint32_t,
762 SplitMessageReader *>
763 first,
764 const std::tuple<monotonic_clock::time_point, uint32_t,
765 SplitMessageReader *>
766 second) {
767 if (std::get<0>(first) > std::get<0>(second)) {
768 return true;
769 } else if (std::get<0>(first) == std::get<0>(second)) {
770 if (std::get<1>(first) > std::get<1>(second)) {
771 return true;
772 } else if (std::get<1>(first) == std::get<1>(second)) {
773 return std::get<2>(first) > std::get<2>(second);
774 } else {
775 return false;
776 }
777 } else {
778 return false;
779 }
780}
781
Austin Schuh05b70472020-01-01 17:11:17 -0800782bool ChannelHeapCompare(
783 const std::pair<monotonic_clock::time_point, int> first,
784 const std::pair<monotonic_clock::time_point, int> second) {
785 if (first.first > second.first) {
786 return true;
787 } else if (first.first == second.first) {
788 return first.second > second.second;
789 } else {
790 return false;
791 }
792}
793
794} // namespace
795
Austin Schuh6f3babe2020-01-26 20:34:50 -0800796TimestampMerger::TimestampMerger(
797 const Configuration *configuration,
798 std::vector<SplitMessageReader *> split_message_readers, int channel_index,
799 const Node *target_node, ChannelMerger *channel_merger)
800 : configuration_(configuration),
801 split_message_readers_(std::move(split_message_readers)),
802 channel_index_(channel_index),
803 node_index_(configuration::MultiNode(configuration)
804 ? configuration::GetNodeIndex(configuration, target_node)
805 : -1),
806 channel_merger_(channel_merger) {
807 // Tell the readers we care so they know who to notify.
Austin Schuhcde938c2020-02-02 17:30:07 -0800808 VLOG(1) << "Configuring channel " << channel_index << " target node "
809 << FlatbufferToJson(target_node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800810 for (SplitMessageReader *reader : split_message_readers_) {
811 reader->SetTimestampMerger(this, channel_index, target_node);
812 }
813
814 // And then determine if we need to track timestamps.
815 const Channel *channel = configuration->channels()->Get(channel_index);
816 if (!configuration::ChannelIsSendableOnNode(channel, target_node) &&
817 configuration::ChannelIsReadableOnNode(channel, target_node)) {
818 has_timestamps_ = true;
819 }
820}
821
822void TimestampMerger::PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800823 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
824 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800825 SplitMessageReader *split_message_reader) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700826 if (split_message_reader != nullptr) {
827 DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(),
828 [split_message_reader](
829 const std::tuple<monotonic_clock::time_point,
830 uint32_t, SplitMessageReader *>
831 x) {
832 return std::get<2>(x) == split_message_reader;
833 }) == message_heap_.end())
834 << ": Pushing message when it is already in the heap.";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800835
Austin Schuh2f8fd752020-09-01 22:38:28 -0700836 message_heap_.push_back(std::make_tuple(
837 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800838
Austin Schuh2f8fd752020-09-01 22:38:28 -0700839 std::push_heap(message_heap_.begin(), message_heap_.end(),
840 &SplitMessageReaderHeapCompare);
841 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800842
843 // If we are just a data merger, don't wait for timestamps.
844 if (!has_timestamps_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700845 if (!message_heap_.empty()) {
846 channel_merger_->Update(std::get<0>(message_heap_[0]), channel_index_);
847 pushed_ = true;
848 } else {
849 // Remove ourselves if we are empty.
850 channel_merger_->Update(monotonic_clock::min_time, channel_index_);
851 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800852 }
853}
854
Austin Schuhcde938c2020-02-02 17:30:07 -0800855std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
856TimestampMerger::oldest_message() const {
857 CHECK_GT(message_heap_.size(), 0u);
858 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
859 oldest_message_reader = message_heap_.front();
860 return std::get<2>(oldest_message_reader)->oldest_message(channel_index_);
861}
862
863std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
864TimestampMerger::oldest_timestamp() const {
865 CHECK_GT(timestamp_heap_.size(), 0u);
866 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
867 oldest_message_reader = timestamp_heap_.front();
868 return std::get<2>(oldest_message_reader)
869 ->oldest_message(channel_index_, node_index_);
870}
871
Austin Schuh6f3babe2020-01-26 20:34:50 -0800872void TimestampMerger::PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800873 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
874 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800875 SplitMessageReader *split_message_reader) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700876 if (split_message_reader != nullptr) {
877 DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(),
878 [split_message_reader](
879 const std::tuple<monotonic_clock::time_point,
880 uint32_t, SplitMessageReader *>
881 x) {
882 return std::get<2>(x) == split_message_reader;
883 }) == timestamp_heap_.end())
884 << ": Pushing timestamp when it is already in the heap.";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800885
Austin Schuh2f8fd752020-09-01 22:38:28 -0700886 timestamp_heap_.push_back(std::make_tuple(
887 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800888
Austin Schuh2f8fd752020-09-01 22:38:28 -0700889 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
890 SplitMessageReaderHeapCompare);
891 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800892
893 // If we are a timestamp merger, don't wait for data. Missing data will be
894 // caught at read time.
895 if (has_timestamps_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700896 if (!timestamp_heap_.empty()) {
897 channel_merger_->Update(std::get<0>(timestamp_heap_[0]), channel_index_);
898 pushed_ = true;
899 } else {
900 // Remove ourselves if we are empty.
901 channel_merger_->Update(monotonic_clock::min_time, channel_index_);
902 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800903 }
904}
905
906std::tuple<monotonic_clock::time_point, uint32_t,
907 FlatbufferVector<MessageHeader>>
908TimestampMerger::PopMessageHeap() {
909 // Pop the oldest message reader pointer off the heap.
910 CHECK_GT(message_heap_.size(), 0u);
911 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
912 oldest_message_reader = message_heap_.front();
913
914 std::pop_heap(message_heap_.begin(), message_heap_.end(),
915 &SplitMessageReaderHeapCompare);
916 message_heap_.pop_back();
917
918 // Pop the oldest message. This re-pushes any messages from the reader to the
919 // message heap.
920 std::tuple<monotonic_clock::time_point, uint32_t,
921 FlatbufferVector<MessageHeader>>
922 oldest_message =
923 std::get<2>(oldest_message_reader)->PopOldest(channel_index_);
924
925 // Confirm that the time and queue_index we have recorded matches.
926 CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader));
927 CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader));
928
929 // Now, keep reading until we have found all duplicates.
Brian Silverman8a32ce62020-08-12 12:02:38 -0700930 while (!message_heap_.empty()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800931 // See if it is a duplicate.
932 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
933 next_oldest_message_reader = message_heap_.front();
934
Austin Schuhcde938c2020-02-02 17:30:07 -0800935 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
936 next_oldest_message_time = std::get<2>(next_oldest_message_reader)
937 ->oldest_message(channel_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800938
939 if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) &&
940 std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) {
941 // Pop the message reader pointer.
942 std::pop_heap(message_heap_.begin(), message_heap_.end(),
943 &SplitMessageReaderHeapCompare);
944 message_heap_.pop_back();
945
946 // Pop the next oldest message. This re-pushes any messages from the
947 // reader.
948 std::tuple<monotonic_clock::time_point, uint32_t,
949 FlatbufferVector<MessageHeader>>
950 next_oldest_message = std::get<2>(next_oldest_message_reader)
951 ->PopOldest(channel_index_);
952
953 // And make sure the message matches in it's entirety.
954 CHECK(std::get<2>(oldest_message).span() ==
955 std::get<2>(next_oldest_message).span())
956 << ": Data at the same timestamp doesn't match.";
957 } else {
958 break;
959 }
960 }
961
962 return oldest_message;
963}
964
965std::tuple<monotonic_clock::time_point, uint32_t,
966 FlatbufferVector<MessageHeader>>
967TimestampMerger::PopTimestampHeap() {
968 // Pop the oldest message reader pointer off the heap.
969 CHECK_GT(timestamp_heap_.size(), 0u);
970
971 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
972 oldest_timestamp_reader = timestamp_heap_.front();
973
974 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
975 &SplitMessageReaderHeapCompare);
976 timestamp_heap_.pop_back();
977
978 CHECK(node_index_ != -1) << ": Timestamps in a single node environment";
979
980 // Pop the oldest message. This re-pushes any timestamps from the reader to
981 // the timestamp heap.
982 std::tuple<monotonic_clock::time_point, uint32_t,
983 FlatbufferVector<MessageHeader>>
984 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700985 ->PopOldestTimestamp(channel_index_, node_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800986
987 // Confirm that the time we have recorded matches.
988 CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader));
989 CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader));
990
Austin Schuh2f8fd752020-09-01 22:38:28 -0700991 // Now, keep reading until we have found all duplicates.
992 while (!timestamp_heap_.empty()) {
993 // See if it is a duplicate.
994 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
995 next_oldest_timestamp_reader = timestamp_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800996
Austin Schuh2f8fd752020-09-01 22:38:28 -0700997 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
998 next_oldest_timestamp_time =
999 std::get<2>(next_oldest_timestamp_reader)
1000 ->oldest_message(channel_index_, node_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001001
Austin Schuh2f8fd752020-09-01 22:38:28 -07001002 if (std::get<0>(next_oldest_timestamp_time) ==
1003 std::get<0>(oldest_timestamp) &&
1004 std::get<1>(next_oldest_timestamp_time) ==
1005 std::get<1>(oldest_timestamp)) {
1006 // Pop the timestamp reader pointer.
1007 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1008 &SplitMessageReaderHeapCompare);
1009 timestamp_heap_.pop_back();
1010
1011 // Pop the next oldest timestamp. This re-pushes any messages from the
1012 // reader.
1013 std::tuple<monotonic_clock::time_point, uint32_t,
1014 FlatbufferVector<MessageHeader>>
1015 next_oldest_timestamp =
1016 std::get<2>(next_oldest_timestamp_reader)
1017 ->PopOldestTimestamp(channel_index_, node_index_);
1018
1019 // And make sure the contents matches in it's entirety.
1020 CHECK(std::get<2>(oldest_timestamp).span() ==
1021 std::get<2>(next_oldest_timestamp).span())
1022 << ": Data at the same timestamp doesn't match, "
1023 << aos::FlatbufferToJson(std::get<2>(oldest_timestamp)) << " vs "
1024 << aos::FlatbufferToJson(std::get<2>(next_oldest_timestamp)) << " "
1025 << absl::BytesToHexString(std::string_view(
1026 reinterpret_cast<const char *>(
1027 std::get<2>(oldest_timestamp).span().data()),
1028 std::get<2>(oldest_timestamp).span().size()))
1029 << " vs "
1030 << absl::BytesToHexString(std::string_view(
1031 reinterpret_cast<const char *>(
1032 std::get<2>(next_oldest_timestamp).span().data()),
1033 std::get<2>(next_oldest_timestamp).span().size()));
1034
1035 } else {
1036 break;
1037 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001038 }
1039
Austin Schuh2f8fd752020-09-01 22:38:28 -07001040 return oldest_timestamp;
Austin Schuh8bd96322020-02-13 21:18:22 -08001041}
1042
Austin Schuh6f3babe2020-01-26 20:34:50 -08001043std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>>
1044TimestampMerger::PopOldest() {
1045 if (has_timestamps_) {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001046 VLOG(1) << "Looking for matching timestamp for "
1047 << configuration::StrippedChannelToString(
1048 configuration_->channels()->Get(channel_index_))
1049 << " (" << channel_index_ << ") "
1050 << " at " << std::get<0>(oldest_timestamp());
1051
Austin Schuh8bd96322020-02-13 21:18:22 -08001052 // Read the timestamps.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001053 std::tuple<monotonic_clock::time_point, uint32_t,
1054 FlatbufferVector<MessageHeader>>
1055 oldest_timestamp = PopTimestampHeap();
1056
1057 TimestampMerger::DeliveryTimestamp timestamp;
1058 timestamp.monotonic_event_time =
1059 monotonic_clock::time_point(chrono::nanoseconds(
1060 std::get<2>(oldest_timestamp).message().monotonic_sent_time()));
1061 timestamp.realtime_event_time =
1062 realtime_clock::time_point(chrono::nanoseconds(
1063 std::get<2>(oldest_timestamp).message().realtime_sent_time()));
1064
1065 // Consistency check.
1066 CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp));
1067 CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(),
1068 std::get<1>(oldest_timestamp));
1069
1070 monotonic_clock::time_point remote_timestamp_monotonic_time(
1071 chrono::nanoseconds(
1072 std::get<2>(oldest_timestamp).message().monotonic_remote_time()));
1073
Austin Schuh8bd96322020-02-13 21:18:22 -08001074 // See if we have any data. If not, pass the problem up the chain.
Brian Silverman8a32ce62020-08-12 12:02:38 -07001075 if (message_heap_.empty()) {
Austin Schuhee711052020-08-24 16:06:09 -07001076 LOG(WARNING) << MaybeNodeName(configuration_->nodes()->Get(node_index_))
1077 << "No data to match timestamp on "
1078 << configuration::CleanedChannelToString(
1079 configuration_->channels()->Get(channel_index_))
1080 << " (" << channel_index_ << ")";
Austin Schuh8bd96322020-02-13 21:18:22 -08001081 return std::make_tuple(timestamp,
1082 std::move(std::get<2>(oldest_timestamp)));
1083 }
1084
Austin Schuh6f3babe2020-01-26 20:34:50 -08001085 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001086 {
1087 // Ok, now try grabbing data until we find one which matches.
1088 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
1089 oldest_message_ref = oldest_message();
1090
1091 // Time at which the message was sent (this message is written from the
1092 // sending node's perspective.
1093 monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds(
1094 std::get<2>(oldest_message_ref)->monotonic_sent_time()));
1095
1096 if (remote_monotonic_time < remote_timestamp_monotonic_time) {
Austin Schuhee711052020-08-24 16:06:09 -07001097 LOG(WARNING) << configuration_->nodes()
1098 ->Get(node_index_)
1099 ->name()
1100 ->string_view()
1101 << " Undelivered message, skipping. Remote time is "
1102 << remote_monotonic_time << " timestamp is "
1103 << remote_timestamp_monotonic_time << " on channel "
1104 << configuration::StrippedChannelToString(
1105 configuration_->channels()->Get(channel_index_))
1106 << " (" << channel_index_ << ")";
Austin Schuhcde938c2020-02-02 17:30:07 -08001107 PopMessageHeap();
1108 continue;
1109 } else if (remote_monotonic_time > remote_timestamp_monotonic_time) {
Austin Schuhee711052020-08-24 16:06:09 -07001110 LOG(WARNING) << configuration_->nodes()
1111 ->Get(node_index_)
1112 ->name()
1113 ->string_view()
1114 << " Data not found. Remote time should be "
1115 << remote_timestamp_monotonic_time
1116 << ", message time is " << remote_monotonic_time
1117 << " on channel "
1118 << configuration::StrippedChannelToString(
1119 configuration_->channels()->Get(channel_index_))
Austin Schuh2f8fd752020-09-01 22:38:28 -07001120 << " (" << channel_index_ << ")"
1121 << (VLOG_IS_ON(1) ? DebugString() : "");
Austin Schuhcde938c2020-02-02 17:30:07 -08001122 return std::make_tuple(timestamp,
1123 std::move(std::get<2>(oldest_timestamp)));
1124 }
1125
1126 timestamp.monotonic_remote_time = remote_monotonic_time;
1127 }
1128
Austin Schuh2f8fd752020-09-01 22:38:28 -07001129 VLOG(1) << "Found matching data "
1130 << configuration::StrippedChannelToString(
1131 configuration_->channels()->Get(channel_index_))
1132 << " (" << channel_index_ << ")";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001133 std::tuple<monotonic_clock::time_point, uint32_t,
1134 FlatbufferVector<MessageHeader>>
1135 oldest_message = PopMessageHeap();
1136
Austin Schuh6f3babe2020-01-26 20:34:50 -08001137 timestamp.realtime_remote_time =
1138 realtime_clock::time_point(chrono::nanoseconds(
1139 std::get<2>(oldest_message).message().realtime_sent_time()));
1140 timestamp.remote_queue_index =
1141 std::get<2>(oldest_message).message().queue_index();
1142
Austin Schuhcde938c2020-02-02 17:30:07 -08001143 CHECK_EQ(timestamp.monotonic_remote_time,
1144 remote_timestamp_monotonic_time);
1145
1146 CHECK_EQ(timestamp.remote_queue_index,
1147 std::get<2>(oldest_timestamp).message().remote_queue_index())
1148 << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message())
1149 << " data "
1150 << FlatbufferToJson(&std::get<2>(oldest_message).message());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001151
Austin Schuh30dd5c52020-08-01 14:43:44 -07001152 return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message)));
Austin Schuh6f3babe2020-01-26 20:34:50 -08001153 }
1154 } else {
1155 std::tuple<monotonic_clock::time_point, uint32_t,
1156 FlatbufferVector<MessageHeader>>
1157 oldest_message = PopMessageHeap();
1158
1159 TimestampMerger::DeliveryTimestamp timestamp;
1160 timestamp.monotonic_event_time =
1161 monotonic_clock::time_point(chrono::nanoseconds(
1162 std::get<2>(oldest_message).message().monotonic_sent_time()));
1163 timestamp.realtime_event_time =
1164 realtime_clock::time_point(chrono::nanoseconds(
1165 std::get<2>(oldest_message).message().realtime_sent_time()));
1166 timestamp.remote_queue_index = 0xffffffff;
1167
1168 CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time);
1169 CHECK_EQ(std::get<1>(oldest_message),
1170 std::get<2>(oldest_message).message().queue_index());
1171
Austin Schuh30dd5c52020-08-01 14:43:44 -07001172 return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message)));
Austin Schuh6f3babe2020-01-26 20:34:50 -08001173 }
1174}
1175
Austin Schuh8bd96322020-02-13 21:18:22 -08001176void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); }
1177
Austin Schuh6f3babe2020-01-26 20:34:50 -08001178namespace {
1179std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders(
1180 const std::vector<std::vector<std::string>> &filenames) {
1181 CHECK_GT(filenames.size(), 0u);
1182 // Build up all the SplitMessageReaders.
1183 std::vector<std::unique_ptr<SplitMessageReader>> result;
1184 for (const std::vector<std::string> &filenames : filenames) {
1185 result.emplace_back(std::make_unique<SplitMessageReader>(filenames));
1186 }
1187 return result;
1188}
1189} // namespace
1190
1191ChannelMerger::ChannelMerger(
1192 const std::vector<std::vector<std::string>> &filenames)
1193 : split_message_readers_(MakeSplitMessageReaders(filenames)),
Austin Schuh97789fc2020-08-01 14:42:45 -07001194 log_file_header_(split_message_readers_[0]->raw_log_file_header()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001195 // Now, confirm that the configuration matches for each and pick a start time.
1196 // Also return the list of possible nodes.
1197 for (const std::unique_ptr<SplitMessageReader> &reader :
1198 split_message_readers_) {
1199 CHECK(CompareFlatBuffer(log_file_header_.message().configuration(),
1200 reader->log_file_header()->configuration()))
1201 << ": Replaying log files with different configurations isn't "
1202 "supported";
1203 }
1204
1205 nodes_ = configuration::GetNodes(configuration());
1206}
1207
1208bool ChannelMerger::SetNode(const Node *target_node) {
1209 std::vector<SplitMessageReader *> split_message_readers;
1210 for (const std::unique_ptr<SplitMessageReader> &reader :
1211 split_message_readers_) {
1212 split_message_readers.emplace_back(reader.get());
1213 }
1214
1215 // Go find a log_file_header for this node.
1216 {
1217 bool found_node = false;
1218
1219 for (const std::unique_ptr<SplitMessageReader> &reader :
1220 split_message_readers_) {
James Kuszmaulfc273dc2020-05-09 17:56:19 -07001221 // In order to identify which logfile(s) map to the target node, do a
1222 // logical comparison of the nodes, by confirming that we are either in a
1223 // single-node setup (where the nodes will both be nullptr) or that the
1224 // node names match (but the other node fields--e.g., hostname lists--may
1225 // not).
1226 const bool both_null =
1227 reader->node() == nullptr && target_node == nullptr;
1228 const bool both_have_name =
1229 (reader->node() != nullptr) && (target_node != nullptr) &&
1230 (reader->node()->has_name() && target_node->has_name());
1231 const bool node_names_identical =
Brian Silvermand90905f2020-09-23 14:42:56 -07001232 both_have_name && (reader->node()->name()->string_view() ==
1233 target_node->name()->string_view());
James Kuszmaulfc273dc2020-05-09 17:56:19 -07001234 if (both_null || node_names_identical) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001235 if (!found_node) {
1236 found_node = true;
1237 log_file_header_ = CopyFlatBuffer(reader->log_file_header());
Austin Schuhcde938c2020-02-02 17:30:07 -08001238 VLOG(1) << "Found log file " << reader->filename() << " with node "
1239 << FlatbufferToJson(reader->node()) << " start_time "
1240 << monotonic_start_time();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001241 } else {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001242 // Find the earliest start time. That way, if we get a full log file
1243 // directly from the node, and a partial later, we start with the
1244 // full. Update our header to match that.
1245 const monotonic_clock::time_point new_monotonic_start_time(
1246 chrono::nanoseconds(
1247 reader->log_file_header()->monotonic_start_time()));
1248 const realtime_clock::time_point new_realtime_start_time(
1249 chrono::nanoseconds(
1250 reader->log_file_header()->realtime_start_time()));
1251
1252 if (monotonic_start_time() == monotonic_clock::min_time ||
1253 (new_monotonic_start_time != monotonic_clock::min_time &&
1254 new_monotonic_start_time < monotonic_start_time())) {
1255 log_file_header_.mutable_message()->mutate_monotonic_start_time(
1256 new_monotonic_start_time.time_since_epoch().count());
1257 log_file_header_.mutable_message()->mutate_realtime_start_time(
1258 new_realtime_start_time.time_since_epoch().count());
1259 VLOG(1) << "Updated log file " << reader->filename()
1260 << " with node " << FlatbufferToJson(reader->node())
1261 << " start_time " << new_monotonic_start_time;
1262 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001263 }
1264 }
1265 }
1266
1267 if (!found_node) {
1268 LOG(WARNING) << "Failed to find log file for node "
1269 << FlatbufferToJson(target_node);
1270 return false;
1271 }
1272 }
1273
1274 // Build up all the timestamp mergers. This connects up all the
1275 // SplitMessageReaders.
1276 timestamp_mergers_.reserve(configuration()->channels()->size());
1277 for (size_t channel_index = 0;
1278 channel_index < configuration()->channels()->size(); ++channel_index) {
1279 timestamp_mergers_.emplace_back(
1280 configuration(), split_message_readers, channel_index,
1281 configuration::GetNode(configuration(), target_node), this);
1282 }
1283
1284 // And prime everything.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001285 for (std::unique_ptr<SplitMessageReader> &split_message_reader :
1286 split_message_readers_) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001287 split_message_reader->QueueMessages(
1288 split_message_reader->monotonic_start_time());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001289 }
1290
1291 node_ = configuration::GetNodeOrDie(configuration(), target_node);
1292 return true;
1293}
1294
Austin Schuh858c9f32020-08-31 16:56:12 -07001295monotonic_clock::time_point ChannelMerger::OldestMessageTime() const {
Brian Silverman8a32ce62020-08-12 12:02:38 -07001296 if (channel_heap_.empty()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001297 return monotonic_clock::max_time;
1298 }
1299 return channel_heap_.front().first;
1300}
1301
1302void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp,
1303 int channel_index) {
1304 // Pop and recreate the heap if it has already been pushed. And since we are
1305 // pushing again, we don't need to clear pushed.
1306 if (timestamp_mergers_[channel_index].pushed()) {
Brian Silverman8a32ce62020-08-12 12:02:38 -07001307 const auto channel_iterator = std::find_if(
Austin Schuh6f3babe2020-01-26 20:34:50 -08001308 channel_heap_.begin(), channel_heap_.end(),
1309 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1310 return x.second == channel_index;
Brian Silverman8a32ce62020-08-12 12:02:38 -07001311 });
1312 DCHECK(channel_iterator != channel_heap_.end());
1313 if (std::get<0>(*channel_iterator) == timestamp) {
1314 // It's already in the heap, in the correct spot, so nothing
1315 // more for us to do here.
1316 return;
1317 }
1318 channel_heap_.erase(channel_iterator);
Austin Schuh6f3babe2020-01-26 20:34:50 -08001319 std::make_heap(channel_heap_.begin(), channel_heap_.end(),
1320 ChannelHeapCompare);
1321 }
1322
Austin Schuh2f8fd752020-09-01 22:38:28 -07001323 if (timestamp == monotonic_clock::min_time) {
1324 timestamp_mergers_[channel_index].set_pushed(false);
1325 return;
1326 }
1327
Austin Schuh05b70472020-01-01 17:11:17 -08001328 channel_heap_.push_back(std::make_pair(timestamp, channel_index));
1329
1330 // The default sort puts the newest message first. Use a custom comparator to
1331 // put the oldest message first.
1332 std::push_heap(channel_heap_.begin(), channel_heap_.end(),
1333 ChannelHeapCompare);
1334}
1335
Austin Schuh2f8fd752020-09-01 22:38:28 -07001336void ChannelMerger::VerifyHeaps() {
Austin Schuh661a8d82020-09-13 17:25:56 -07001337 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1338 channel_heap_;
1339 std::make_heap(channel_heap.begin(), channel_heap.end(), &ChannelHeapCompare);
Austin Schuh2f8fd752020-09-01 22:38:28 -07001340
Austin Schuh661a8d82020-09-13 17:25:56 -07001341 for (size_t i = 0; i < channel_heap_.size(); ++i) {
1342 CHECK(channel_heap_[i] == channel_heap[i]) << ": Heaps diverged...";
1343 CHECK_EQ(
1344 std::get<0>(channel_heap[i]),
1345 timestamp_mergers_[std::get<1>(channel_heap[i])].channel_merger_time());
Austin Schuh2f8fd752020-09-01 22:38:28 -07001346 }
1347}
1348
Austin Schuh6f3babe2020-01-26 20:34:50 -08001349std::tuple<TimestampMerger::DeliveryTimestamp, int,
1350 FlatbufferVector<MessageHeader>>
1351ChannelMerger::PopOldest() {
Austin Schuh8bd96322020-02-13 21:18:22 -08001352 CHECK_GT(channel_heap_.size(), 0u);
Austin Schuh05b70472020-01-01 17:11:17 -08001353 std::pair<monotonic_clock::time_point, int> oldest_channel_data =
1354 channel_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001355 int channel_index = oldest_channel_data.second;
Austin Schuh05b70472020-01-01 17:11:17 -08001356 std::pop_heap(channel_heap_.begin(), channel_heap_.end(),
1357 &ChannelHeapCompare);
1358 channel_heap_.pop_back();
Austin Schuh8bd96322020-02-13 21:18:22 -08001359
Austin Schuh6f3babe2020-01-26 20:34:50 -08001360 timestamp_mergers_[channel_index].set_pushed(false);
Austin Schuh05b70472020-01-01 17:11:17 -08001361
Austin Schuh6f3babe2020-01-26 20:34:50 -08001362 TimestampMerger *merger = &timestamp_mergers_[channel_index];
Austin Schuh05b70472020-01-01 17:11:17 -08001363
Austin Schuhcde938c2020-02-02 17:30:07 -08001364 // Merger handles any queueing needed from here.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001365 std::tuple<TimestampMerger::DeliveryTimestamp,
1366 FlatbufferVector<MessageHeader>>
1367 message = merger->PopOldest();
Brian Silverman8a32ce62020-08-12 12:02:38 -07001368 DCHECK_EQ(std::get<0>(message).monotonic_event_time,
1369 oldest_channel_data.first)
1370 << ": channel_heap_ was corrupted for " << channel_index << ": "
1371 << DebugString();
Austin Schuh05b70472020-01-01 17:11:17 -08001372
Austin Schuh2f8fd752020-09-01 22:38:28 -07001373 CHECK_GE(std::get<0>(message).monotonic_event_time, last_popped_time_)
1374 << ": " << MaybeNodeName(log_file_header()->node())
1375 << "Messages came off the queue out of order. " << DebugString();
1376 last_popped_time_ = std::get<0>(message).monotonic_event_time;
1377
1378 VLOG(1) << "Popped " << last_popped_time_ << " "
1379 << configuration::StrippedChannelToString(
1380 configuration()->channels()->Get(channel_index))
1381 << " (" << channel_index << ")";
1382
Austin Schuh6f3babe2020-01-26 20:34:50 -08001383 return std::make_tuple(std::get<0>(message), channel_index,
1384 std::move(std::get<1>(message)));
1385}
1386
Austin Schuhcde938c2020-02-02 17:30:07 -08001387std::string SplitMessageReader::MessageHeaderQueue::DebugString() const {
1388 std::stringstream ss;
1389 for (size_t i = 0; i < data_.size(); ++i) {
Austin Schuh2f8fd752020-09-01 22:38:28 -07001390 if (i < 5 || i + 5 > data_.size()) {
1391 if (timestamps) {
1392 ss << " msg: ";
1393 } else {
1394 ss << " timestamp: ";
1395 }
1396 ss << monotonic_clock::time_point(
1397 chrono::nanoseconds(data_[i].message().monotonic_sent_time()))
Austin Schuhcde938c2020-02-02 17:30:07 -08001398 << " ("
Austin Schuh2f8fd752020-09-01 22:38:28 -07001399 << realtime_clock::time_point(
1400 chrono::nanoseconds(data_[i].message().realtime_sent_time()))
1401 << ") " << data_[i].message().queue_index();
1402 if (timestamps) {
1403 ss << " <- remote "
1404 << monotonic_clock::time_point(chrono::nanoseconds(
1405 data_[i].message().monotonic_remote_time()))
1406 << " ("
1407 << realtime_clock::time_point(chrono::nanoseconds(
1408 data_[i].message().realtime_remote_time()))
1409 << ")";
1410 }
1411 ss << "\n";
1412 } else if (i == 5) {
1413 ss << " ...\n";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001414 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001415 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001416
Austin Schuhcde938c2020-02-02 17:30:07 -08001417 return ss.str();
1418}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001419
Austin Schuhcde938c2020-02-02 17:30:07 -08001420std::string SplitMessageReader::DebugString(int channel) const {
1421 std::stringstream ss;
1422 ss << "[\n";
1423 ss << channels_[channel].data.DebugString();
1424 ss << " ]";
1425 return ss.str();
1426}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001427
Austin Schuhcde938c2020-02-02 17:30:07 -08001428std::string SplitMessageReader::DebugString(int channel, int node_index) const {
1429 std::stringstream ss;
1430 ss << "[\n";
1431 ss << channels_[channel].timestamps[node_index].DebugString();
1432 ss << " ]";
1433 return ss.str();
1434}
1435
1436std::string TimestampMerger::DebugString() const {
1437 std::stringstream ss;
1438
1439 if (timestamp_heap_.size() > 0) {
1440 ss << " timestamp_heap {\n";
1441 std::vector<
1442 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1443 timestamp_heap = timestamp_heap_;
1444 while (timestamp_heap.size() > 0u) {
1445 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1446 oldest_timestamp_reader = timestamp_heap.front();
1447
1448 ss << " " << std::get<2>(oldest_timestamp_reader) << " "
1449 << std::get<0>(oldest_timestamp_reader) << " queue_index ("
1450 << std::get<1>(oldest_timestamp_reader) << ") ttq "
1451 << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " "
1452 << std::get<2>(oldest_timestamp_reader)->filename() << " -> "
1453 << std::get<2>(oldest_timestamp_reader)
1454 ->DebugString(channel_index_, node_index_)
1455 << "\n";
1456
1457 std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(),
1458 &SplitMessageReaderHeapCompare);
1459 timestamp_heap.pop_back();
1460 }
1461 ss << " }\n";
1462 }
1463
1464 ss << " message_heap {\n";
1465 {
1466 std::vector<
1467 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1468 message_heap = message_heap_;
Brian Silverman8a32ce62020-08-12 12:02:38 -07001469 while (!message_heap.empty()) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001470 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1471 oldest_message_reader = message_heap.front();
1472
1473 ss << " " << std::get<2>(oldest_message_reader) << " "
1474 << std::get<0>(oldest_message_reader) << " queue_index ("
1475 << std::get<1>(oldest_message_reader) << ") ttq "
1476 << std::get<2>(oldest_message_reader)->time_to_queue() << " "
1477 << std::get<2>(oldest_message_reader)->filename() << " -> "
1478 << std::get<2>(oldest_message_reader)->DebugString(channel_index_)
1479 << "\n";
1480
1481 std::pop_heap(message_heap.begin(), message_heap.end(),
1482 &SplitMessageReaderHeapCompare);
1483 message_heap.pop_back();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001484 }
Austin Schuh05b70472020-01-01 17:11:17 -08001485 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001486 ss << " }";
1487
1488 return ss.str();
1489}
1490
1491std::string ChannelMerger::DebugString() const {
1492 std::stringstream ss;
1493 ss << "start_time " << realtime_start_time() << " " << monotonic_start_time()
1494 << "\n";
1495 ss << "channel_heap {\n";
1496 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1497 channel_heap_;
Brian Silverman8a32ce62020-08-12 12:02:38 -07001498 while (!channel_heap.empty()) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001499 std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front();
1500 ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") "
1501 << configuration::CleanedChannelToString(
1502 configuration()->channels()->Get(std::get<1>(channel)))
1503 << "\n";
1504
1505 ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n";
1506
1507 std::pop_heap(channel_heap.begin(), channel_heap.end(),
1508 &ChannelHeapCompare);
1509 channel_heap.pop_back();
1510 }
1511 ss << "}";
1512
1513 return ss.str();
Austin Schuh05b70472020-01-01 17:11:17 -08001514}
1515
Austin Schuhee711052020-08-24 16:06:09 -07001516std::string MaybeNodeName(const Node *node) {
1517 if (node != nullptr) {
1518 return node->name()->str() + " ";
1519 }
1520 return "";
1521}
1522
Austin Schuha36c8902019-12-30 18:07:15 -08001523} // namespace logger
1524} // namespace aos