blob: 123c579595200e14d5952bf0f29931d44a499583 [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
20DEFINE_int32(flush_size, 1000000,
21 "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";
40}
41
42void DetachedBufferWriter::QueueSizedFlatbuffer(
43 flatbuffers::FlatBufferBuilder *fbb) {
44 QueueSizedFlatbuffer(fbb->Release());
45}
46
Austin Schuhde031b72020-01-10 19:34:41 -080047void DetachedBufferWriter::WriteSizedFlatbuffer(
48 absl::Span<const uint8_t> span) {
49 // Cheat aggressively... Write out the queued up data, and then write this
50 // data once without buffering. It is hard to make a DetachedBuffer out of
51 // this data, and we don't want to worry about lifetimes.
52 Flush();
53 iovec_.clear();
54 iovec_.reserve(1);
55
56 struct iovec n;
57 n.iov_base = const_cast<uint8_t *>(span.data());
58 n.iov_len = span.size();
59 iovec_.emplace_back(n);
60
61 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
62
63 PCHECK(written == static_cast<ssize_t>(n.iov_len))
64 << ": Wrote " << written << " expected " << n.iov_len;
Brian Silverman98360e22020-04-28 16:51:20 -070065 written_size_ += written;
Austin Schuhde031b72020-01-10 19:34:41 -080066}
67
Austin Schuha36c8902019-12-30 18:07:15 -080068void DetachedBufferWriter::QueueSizedFlatbuffer(
69 flatbuffers::DetachedBuffer &&buffer) {
70 queued_size_ += buffer.size();
71 queue_.emplace_back(std::move(buffer));
72
73 // Flush if we are at the max number of iovs per writev, or have written
74 // enough data. Otherwise writev will fail with an invalid argument.
75 if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) ||
76 queue_.size() == IOV_MAX) {
77 Flush();
78 }
79}
80
81void DetachedBufferWriter::Flush() {
82 if (queue_.size() == 0u) {
83 return;
84 }
85 iovec_.clear();
86 iovec_.reserve(queue_.size());
87 size_t counted_size = 0;
88 for (size_t i = 0; i < queue_.size(); ++i) {
89 struct iovec n;
90 n.iov_base = queue_[i].data();
91 n.iov_len = queue_[i].size();
92 counted_size += n.iov_len;
93 iovec_.emplace_back(std::move(n));
94 }
95 CHECK_EQ(counted_size, queued_size_);
96 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
97
98 PCHECK(written == static_cast<ssize_t>(queued_size_))
99 << ": Wrote " << written << " expected " << queued_size_;
Brian Silverman98360e22020-04-28 16:51:20 -0700100 written_size_ += written;
Austin Schuha36c8902019-12-30 18:07:15 -0800101
102 queued_size_ = 0;
103 queue_.clear();
104 // TODO(austin): Handle partial writes in some way other than crashing...
105}
106
107flatbuffers::Offset<MessageHeader> PackMessage(
108 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
109 int channel_index, LogType log_type) {
110 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset;
111
112 switch (log_type) {
113 case LogType::kLogMessage:
114 case LogType::kLogMessageAndDeliveryTime:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800115 case LogType::kLogRemoteMessage:
Brian Silvermaneaa41d62020-07-08 19:47:35 -0700116 data_offset = fbb->CreateVector(
117 static_cast<const uint8_t *>(context.data), context.size);
Austin Schuha36c8902019-12-30 18:07:15 -0800118 break;
119
120 case LogType::kLogDeliveryTimeOnly:
121 break;
122 }
123
124 MessageHeader::Builder message_header_builder(*fbb);
125 message_header_builder.add_channel_index(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800126
127 switch (log_type) {
128 case LogType::kLogRemoteMessage:
129 message_header_builder.add_queue_index(context.remote_queue_index);
130 message_header_builder.add_monotonic_sent_time(
131 context.monotonic_remote_time.time_since_epoch().count());
132 message_header_builder.add_realtime_sent_time(
133 context.realtime_remote_time.time_since_epoch().count());
134 break;
135
136 case LogType::kLogMessage:
137 case LogType::kLogMessageAndDeliveryTime:
138 case LogType::kLogDeliveryTimeOnly:
139 message_header_builder.add_queue_index(context.queue_index);
140 message_header_builder.add_monotonic_sent_time(
141 context.monotonic_event_time.time_since_epoch().count());
142 message_header_builder.add_realtime_sent_time(
143 context.realtime_event_time.time_since_epoch().count());
144 break;
145 }
Austin Schuha36c8902019-12-30 18:07:15 -0800146
147 switch (log_type) {
148 case LogType::kLogMessage:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800149 case LogType::kLogRemoteMessage:
Austin Schuha36c8902019-12-30 18:07:15 -0800150 message_header_builder.add_data(data_offset);
151 break;
152
153 case LogType::kLogMessageAndDeliveryTime:
154 message_header_builder.add_data(data_offset);
155 [[fallthrough]];
156
157 case LogType::kLogDeliveryTimeOnly:
158 message_header_builder.add_monotonic_remote_time(
159 context.monotonic_remote_time.time_since_epoch().count());
160 message_header_builder.add_realtime_remote_time(
161 context.realtime_remote_time.time_since_epoch().count());
162 message_header_builder.add_remote_queue_index(context.remote_queue_index);
163 break;
164 }
165
166 return message_header_builder.Finish();
167}
168
Austin Schuh05b70472020-01-01 17:11:17 -0800169SpanReader::SpanReader(std::string_view filename)
Austin Schuh6f3babe2020-01-26 20:34:50 -0800170 : filename_(filename),
171 fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) {
Austin Schuh05b70472020-01-01 17:11:17 -0800172 PCHECK(fd_ != -1) << ": Failed to open " << filename;
173}
174
175absl::Span<const uint8_t> SpanReader::ReadMessage() {
176 // Make sure we have enough for the size.
177 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
178 if (!ReadBlock()) {
179 return absl::Span<const uint8_t>();
180 }
181 }
182
183 // Now make sure we have enough for the message.
184 const size_t data_size =
185 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
186 sizeof(flatbuffers::uoffset_t);
Austin Schuhe4fca832020-03-07 16:58:53 -0800187 if (data_size == sizeof(flatbuffers::uoffset_t)) {
188 LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping.";
189 LOG(ERROR) << " Rest of log file is "
190 << absl::BytesToHexString(std::string_view(
191 reinterpret_cast<const char *>(data_.data() +
192 consumed_data_),
193 data_.size() - consumed_data_));
194 return absl::Span<const uint8_t>();
195 }
Austin Schuh05b70472020-01-01 17:11:17 -0800196 while (data_.size() < consumed_data_ + data_size) {
197 if (!ReadBlock()) {
198 return absl::Span<const uint8_t>();
199 }
200 }
201
202 // And return it, consuming the data.
203 const uint8_t *data_ptr = data_.data() + consumed_data_;
204
205 consumed_data_ += data_size;
206
207 return absl::Span<const uint8_t>(data_ptr, data_size);
208}
209
210bool SpanReader::MessageAvailable() {
211 // Are we big enough to read the size?
212 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
213 return false;
214 }
215
216 // Then, are we big enough to read the full message?
217 const size_t data_size =
218 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
219 sizeof(flatbuffers::uoffset_t);
220 if (data_.size() < consumed_data_ + data_size) {
221 return false;
222 }
223
224 return true;
225}
226
227bool SpanReader::ReadBlock() {
228 if (end_of_file_) {
229 return false;
230 }
231
232 // Appends 256k. This is enough that the read call is efficient. We don't
233 // want to spend too much time reading small chunks because the syscalls for
234 // that will be expensive.
235 constexpr size_t kReadSize = 256 * 1024;
236
237 // Strip off any unused data at the front.
238 if (consumed_data_ != 0) {
239 data_.erase(data_.begin(), data_.begin() + consumed_data_);
240 consumed_data_ = 0;
241 }
242
243 const size_t starting_size = data_.size();
244
245 // This should automatically grow the backing store. It won't shrink if we
246 // get a small chunk later. This reduces allocations when we want to append
247 // more data.
248 data_.resize(data_.size() + kReadSize);
249
250 ssize_t count = read(fd_, &data_[starting_size], kReadSize);
251 data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0)));
252 if (count == 0) {
253 end_of_file_ = true;
254 return false;
255 }
256 PCHECK(count > 0);
257
258 return true;
259}
260
Austin Schuh6f3babe2020-01-26 20:34:50 -0800261FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename) {
262 SpanReader span_reader(filename);
263 // Make sure we have enough to read the size.
264 absl::Span<const uint8_t> config_data = span_reader.ReadMessage();
265
266 // Make sure something was read.
267 CHECK(config_data != absl::Span<const uint8_t>());
268
269 // And copy the config so we have it forever.
270 std::vector<uint8_t> data(
271 config_data.begin() + sizeof(flatbuffers::uoffset_t), config_data.end());
272 return FlatbufferVector<LogFileHeader>(std::move(data));
273}
274
Austin Schuh05b70472020-01-01 17:11:17 -0800275MessageReader::MessageReader(std::string_view filename)
276 : span_reader_(filename) {
277 // Make sure we have enough to read the size.
278 absl::Span<const uint8_t> config_data = span_reader_.ReadMessage();
279
280 // Make sure something was read.
281 CHECK(config_data != absl::Span<const uint8_t>());
282
283 // And copy the config so we have it forever.
284 configuration_ = std::vector<uint8_t>(config_data.begin(), config_data.end());
285
Austin Schuhcde938c2020-02-02 17:30:07 -0800286 max_out_of_order_duration_ =
287 std::chrono::nanoseconds(log_file_header()->max_out_of_order_duration());
288
289 VLOG(1) << "Opened " << filename << " as node "
290 << FlatbufferToJson(log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800291}
292
293std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() {
294 absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage();
295 if (msg_data == absl::Span<const uint8_t>()) {
296 return std::nullopt;
297 }
298
299 FlatbufferVector<MessageHeader> result{std::vector<uint8_t>(
300 msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())};
301
302 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
303 chrono::nanoseconds(result.message().monotonic_sent_time()));
304
305 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
Austin Schuh8bd96322020-02-13 21:18:22 -0800306 VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800307 return std::move(result);
Austin Schuh05b70472020-01-01 17:11:17 -0800308}
309
Austin Schuh6f3babe2020-01-26 20:34:50 -0800310SplitMessageReader::SplitMessageReader(
Austin Schuhfa895892020-01-07 20:07:41 -0800311 const std::vector<std::string> &filenames)
312 : filenames_(filenames),
313 log_file_header_(FlatbufferDetachedBuffer<LogFileHeader>::Empty()) {
314 CHECK(NextLogFile()) << ": filenames is empty. Need files to read.";
315
Austin Schuh6f3babe2020-01-26 20:34:50 -0800316 // Grab any log file header. They should all match (and we will check as we
317 // open more of them).
Austin Schuhfa895892020-01-07 20:07:41 -0800318 log_file_header_ = CopyFlatBuffer(message_reader_->log_file_header());
319
Austin Schuh6f3babe2020-01-26 20:34:50 -0800320 // Setup per channel state.
Austin Schuh05b70472020-01-01 17:11:17 -0800321 channels_.resize(configuration()->channels()->size());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800322 for (ChannelData &channel_data : channels_) {
323 channel_data.data.split_reader = this;
324 // Build up the timestamp list.
325 if (configuration::MultiNode(configuration())) {
326 channel_data.timestamps.resize(configuration()->nodes()->size());
327 for (MessageHeaderQueue &queue : channel_data.timestamps) {
328 queue.timestamps = true;
329 queue.split_reader = this;
330 }
331 }
332 }
Austin Schuh05b70472020-01-01 17:11:17 -0800333
Austin Schuh6f3babe2020-01-26 20:34:50 -0800334 // Build up channels_to_write_ as an optimization to make it fast to figure
335 // out which datastructure to place any new data from a channel on.
336 for (const Channel *channel : *configuration()->channels()) {
337 // This is the main case. We will only see data on this node.
338 if (configuration::ChannelIsSendableOnNode(channel, node())) {
339 channels_to_write_.emplace_back(
340 &channels_[channels_to_write_.size()].data);
341 } else
342 // If we can't send, but can receive, we should be able to see
343 // timestamps here.
344 if (configuration::ChannelIsReadableOnNode(channel, node())) {
345 channels_to_write_.emplace_back(
346 &(channels_[channels_to_write_.size()]
347 .timestamps[configuration::GetNodeIndex(configuration(),
348 node())]));
349 } else {
350 channels_to_write_.emplace_back(nullptr);
351 }
352 }
Austin Schuh05b70472020-01-01 17:11:17 -0800353}
354
Austin Schuh6f3babe2020-01-26 20:34:50 -0800355bool SplitMessageReader::NextLogFile() {
Austin Schuhfa895892020-01-07 20:07:41 -0800356 if (next_filename_index_ == filenames_.size()) {
357 return false;
358 }
359 message_reader_ =
360 std::make_unique<MessageReader>(filenames_[next_filename_index_]);
361
362 // We can't support the config diverging between two log file headers. See if
363 // they are the same.
364 if (next_filename_index_ != 0) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800365 CHECK(CompareFlatBuffer(&log_file_header_.message(),
366 message_reader_->log_file_header()))
Austin Schuhfa895892020-01-07 20:07:41 -0800367 << ": Header is different between log file chunks "
368 << filenames_[next_filename_index_] << " and "
369 << filenames_[next_filename_index_ - 1] << ", this is not supported.";
370 }
371
372 ++next_filename_index_;
373 return true;
374}
375
Austin Schuh6f3babe2020-01-26 20:34:50 -0800376bool SplitMessageReader::QueueMessages(
Austin Schuhcde938c2020-02-02 17:30:07 -0800377 monotonic_clock::time_point last_dequeued_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800378 // TODO(austin): Once we are happy that everything works, read a 256kb chunk
379 // to reduce the need to re-heap down below.
Austin Schuhcde938c2020-02-02 17:30:07 -0800380
381 // Special case no more data. Otherwise we blow up on the CHECK statement
382 // confirming that we have enough data queued.
383 if (at_end_) {
384 return false;
385 }
386
387 // If this isn't the first time around, confirm that we had enough data queued
388 // to follow the contract.
389 if (time_to_queue_ != monotonic_clock::min_time) {
390 CHECK_LE(last_dequeued_time,
391 newest_timestamp() - max_out_of_order_duration())
392 << " node " << FlatbufferToJson(node()) << " on " << this;
393
394 // Bail if there is enough data already queued.
395 if (last_dequeued_time < time_to_queue_) {
396 VLOG(1) << "All up to date on " << this << ", dequeued "
397 << last_dequeued_time << " queue time " << time_to_queue_;
398 return true;
399 }
400 } else {
401 // Startup takes a special dance. We want to queue up until the start time,
402 // but we then want to find the next message to read. The conservative
403 // answer is to immediately trigger a second requeue to get things moving.
404 time_to_queue_ = monotonic_start_time();
405 QueueMessages(time_to_queue_);
406 }
407
408 // If we are asked to queue, queue for at least max_out_of_order_duration past
409 // the last known time in the log file (ie the newest timestep read). As long
410 // as we requeue exactly when time_to_queue_ is dequeued and go no further, we
411 // are safe. And since we pop in order, that works.
412 //
413 // Special case the start of the log file. There should be at most 1 message
414 // from each channel at the start of the log file. So always force the start
415 // of the log file to just be read.
416 time_to_queue_ = std::max(time_to_queue_, newest_timestamp());
417 VLOG(1) << "Queueing, going until " << time_to_queue_ << " " << filename();
418
419 bool was_emplaced = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800420 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800421 // Stop if we have enough.
Brian Silverman98360e22020-04-28 16:51:20 -0700422 if (newest_timestamp() > time_to_queue_ + max_out_of_order_duration() &&
Austin Schuhcde938c2020-02-02 17:30:07 -0800423 was_emplaced) {
424 VLOG(1) << "Done queueing on " << this << ", queued to "
425 << newest_timestamp() << " with requeue time " << time_to_queue_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800426 return true;
427 }
Austin Schuh05b70472020-01-01 17:11:17 -0800428
Austin Schuh6f3babe2020-01-26 20:34:50 -0800429 if (std::optional<FlatbufferVector<MessageHeader>> msg =
430 message_reader_->ReadMessage()) {
431 const MessageHeader &header = msg.value().message();
432
Austin Schuhcde938c2020-02-02 17:30:07 -0800433 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
434 chrono::nanoseconds(header.monotonic_sent_time()));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800435
Austin Schuh0b5fd032020-03-28 17:36:49 -0700436 if (VLOG_IS_ON(2)) {
437 LOG(INFO) << "Queued " << this << " " << filename()
438 << " ttq: " << time_to_queue_ << " now " << newest_timestamp()
439 << " start time " << monotonic_start_time() << " "
440 << FlatbufferToJson(&header);
441 } else if (VLOG_IS_ON(1)) {
442 FlatbufferVector<MessageHeader> copy = msg.value();
443 copy.mutable_message()->clear_data();
444 LOG(INFO) << "Queued " << this << " " << filename()
445 << " ttq: " << time_to_queue_ << " now " << newest_timestamp()
446 << " start time " << monotonic_start_time() << " "
447 << FlatbufferToJson(copy);
448 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800449
450 const int channel_index = header.channel_index();
451 was_emplaced = channels_to_write_[channel_index]->emplace_back(
452 std::move(msg.value()));
453 if (was_emplaced) {
454 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
455 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800456 } else {
457 if (!NextLogFile()) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800458 VLOG(1) << "End of log file " << filenames_.back();
Austin Schuhcde938c2020-02-02 17:30:07 -0800459 at_end_ = true;
Austin Schuh8bd96322020-02-13 21:18:22 -0800460 for (MessageHeaderQueue *queue : channels_to_write_) {
461 if (queue == nullptr || queue->timestamp_merger == nullptr) {
462 continue;
463 }
464 queue->timestamp_merger->NoticeAtEnd();
465 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800466 return false;
467 }
468 }
Austin Schuh05b70472020-01-01 17:11:17 -0800469 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800470}
471
472void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger,
473 int channel_index,
474 const Node *target_node) {
475 const Node *reinterpreted_target_node =
476 configuration::GetNodeOrDie(configuration(), target_node);
477 const Channel *const channel =
478 configuration()->channels()->Get(channel_index);
479
Austin Schuhcde938c2020-02-02 17:30:07 -0800480 VLOG(1) << " Configuring merger " << this << " for channel " << channel_index
481 << " "
482 << configuration::CleanedChannelToString(
483 configuration()->channels()->Get(channel_index));
484
Austin Schuh6f3babe2020-01-26 20:34:50 -0800485 MessageHeaderQueue *message_header_queue = nullptr;
486
487 // Figure out if this log file is from our point of view, or the other node's
488 // point of view.
489 if (node() == reinterpreted_target_node) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800490 VLOG(1) << " Replaying as logged node " << filename();
491
492 if (configuration::ChannelIsSendableOnNode(channel, node())) {
493 VLOG(1) << " Data on node";
494 message_header_queue = &(channels_[channel_index].data);
495 } else if (configuration::ChannelIsReadableOnNode(channel, node())) {
496 VLOG(1) << " Timestamps on node";
497 message_header_queue =
498 &(channels_[channel_index].timestamps[configuration::GetNodeIndex(
499 configuration(), node())]);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800500 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800501 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800502 }
503 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800504 VLOG(1) << " Replaying as other node " << filename();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800505 // We are replaying from another node's point of view. The only interesting
Austin Schuhcde938c2020-02-02 17:30:07 -0800506 // data is data that is sent from our node and received on theirs.
507 if (configuration::ChannelIsReadableOnNode(channel,
508 reinterpreted_target_node) &&
509 configuration::ChannelIsSendableOnNode(channel, node())) {
510 VLOG(1) << " Readable on target node";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800511 // Data from another node.
512 message_header_queue = &(channels_[channel_index].data);
513 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800514 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800515 // This is either not sendable on the other node, or is a timestamp and
516 // therefore not interesting.
517 }
518 }
519
520 // If we found one, write it down. This will be nullptr when there is nothing
521 // relevant on this channel on this node for the target node. In that case,
522 // we want to drop the message instead of queueing it.
523 if (message_header_queue != nullptr) {
524 message_header_queue->timestamp_merger = timestamp_merger;
525 }
526}
527
528std::tuple<monotonic_clock::time_point, uint32_t,
529 FlatbufferVector<MessageHeader>>
530SplitMessageReader::PopOldest(int channel_index) {
531 CHECK_GT(channels_[channel_index].data.size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800532 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
533 timestamp = channels_[channel_index].data.front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800534 FlatbufferVector<MessageHeader> front =
535 std::move(channels_[channel_index].data.front());
536 channels_[channel_index].data.pop_front();
Austin Schuhcde938c2020-02-02 17:30:07 -0800537
538 VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp);
539
540 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800541
542 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
543 std::move(front));
544}
545
546std::tuple<monotonic_clock::time_point, uint32_t,
547 FlatbufferVector<MessageHeader>>
548SplitMessageReader::PopOldest(int channel, int node_index) {
549 CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800550 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
551 timestamp = channels_[channel].timestamps[node_index].front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800552 FlatbufferVector<MessageHeader> front =
553 std::move(channels_[channel].timestamps[node_index].front());
554 channels_[channel].timestamps[node_index].pop_front();
Austin Schuhcde938c2020-02-02 17:30:07 -0800555
556 VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp);
557
558 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800559
560 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
561 std::move(front));
562}
563
Austin Schuhcde938c2020-02-02 17:30:07 -0800564bool SplitMessageReader::MessageHeaderQueue::emplace_back(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800565 FlatbufferVector<MessageHeader> &&msg) {
566 CHECK(split_reader != nullptr);
567
568 // If there is no timestamp merger for this queue, nobody is listening. Drop
569 // the message. This happens when a log file from another node is replayed,
570 // and the timestamp mergers down stream just don't care.
571 if (timestamp_merger == nullptr) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800572 return false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800573 }
574
575 CHECK(timestamps != msg.message().has_data())
576 << ": Got timestamps and data mixed up on a node. "
577 << FlatbufferToJson(msg);
578
579 data_.emplace_back(std::move(msg));
580
581 if (data_.size() == 1u) {
582 // Yup, new data. Notify.
583 if (timestamps) {
584 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
585 } else {
586 timestamp_merger->Update(split_reader, front_timestamp());
587 }
588 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800589
590 return true;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800591}
592
593void SplitMessageReader::MessageHeaderQueue::pop_front() {
594 data_.pop_front();
595 if (data_.size() != 0u) {
596 // Yup, new data.
597 if (timestamps) {
598 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
599 } else {
600 timestamp_merger->Update(split_reader, front_timestamp());
601 }
602 }
Austin Schuh05b70472020-01-01 17:11:17 -0800603}
604
605namespace {
606
Austin Schuh6f3babe2020-01-26 20:34:50 -0800607bool SplitMessageReaderHeapCompare(
608 const std::tuple<monotonic_clock::time_point, uint32_t,
609 SplitMessageReader *>
610 first,
611 const std::tuple<monotonic_clock::time_point, uint32_t,
612 SplitMessageReader *>
613 second) {
614 if (std::get<0>(first) > std::get<0>(second)) {
615 return true;
616 } else if (std::get<0>(first) == std::get<0>(second)) {
617 if (std::get<1>(first) > std::get<1>(second)) {
618 return true;
619 } else if (std::get<1>(first) == std::get<1>(second)) {
620 return std::get<2>(first) > std::get<2>(second);
621 } else {
622 return false;
623 }
624 } else {
625 return false;
626 }
627}
628
Austin Schuh05b70472020-01-01 17:11:17 -0800629bool ChannelHeapCompare(
630 const std::pair<monotonic_clock::time_point, int> first,
631 const std::pair<monotonic_clock::time_point, int> second) {
632 if (first.first > second.first) {
633 return true;
634 } else if (first.first == second.first) {
635 return first.second > second.second;
636 } else {
637 return false;
638 }
639}
640
641} // namespace
642
Austin Schuh6f3babe2020-01-26 20:34:50 -0800643TimestampMerger::TimestampMerger(
644 const Configuration *configuration,
645 std::vector<SplitMessageReader *> split_message_readers, int channel_index,
646 const Node *target_node, ChannelMerger *channel_merger)
647 : configuration_(configuration),
648 split_message_readers_(std::move(split_message_readers)),
649 channel_index_(channel_index),
650 node_index_(configuration::MultiNode(configuration)
651 ? configuration::GetNodeIndex(configuration, target_node)
652 : -1),
653 channel_merger_(channel_merger) {
654 // Tell the readers we care so they know who to notify.
Austin Schuhcde938c2020-02-02 17:30:07 -0800655 VLOG(1) << "Configuring channel " << channel_index << " target node "
656 << FlatbufferToJson(target_node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800657 for (SplitMessageReader *reader : split_message_readers_) {
658 reader->SetTimestampMerger(this, channel_index, target_node);
659 }
660
661 // And then determine if we need to track timestamps.
662 const Channel *channel = configuration->channels()->Get(channel_index);
663 if (!configuration::ChannelIsSendableOnNode(channel, target_node) &&
664 configuration::ChannelIsReadableOnNode(channel, target_node)) {
665 has_timestamps_ = true;
666 }
667}
668
669void TimestampMerger::PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800670 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
671 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800672 SplitMessageReader *split_message_reader) {
673 DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(),
674 [split_message_reader](
675 const std::tuple<monotonic_clock::time_point,
676 uint32_t, SplitMessageReader *>
677 x) {
678 return std::get<2>(x) == split_message_reader;
679 }) == message_heap_.end())
680 << ": Pushing message when it is already in the heap.";
681
682 message_heap_.push_back(std::make_tuple(
683 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
684
685 std::push_heap(message_heap_.begin(), message_heap_.end(),
686 &SplitMessageReaderHeapCompare);
687
688 // If we are just a data merger, don't wait for timestamps.
689 if (!has_timestamps_) {
690 channel_merger_->Update(std::get<0>(timestamp), channel_index_);
691 pushed_ = true;
692 }
693}
694
Austin Schuhcde938c2020-02-02 17:30:07 -0800695std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
696TimestampMerger::oldest_message() const {
697 CHECK_GT(message_heap_.size(), 0u);
698 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
699 oldest_message_reader = message_heap_.front();
700 return std::get<2>(oldest_message_reader)->oldest_message(channel_index_);
701}
702
703std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
704TimestampMerger::oldest_timestamp() const {
705 CHECK_GT(timestamp_heap_.size(), 0u);
706 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
707 oldest_message_reader = timestamp_heap_.front();
708 return std::get<2>(oldest_message_reader)
709 ->oldest_message(channel_index_, node_index_);
710}
711
Austin Schuh6f3babe2020-01-26 20:34:50 -0800712void TimestampMerger::PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800713 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
714 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800715 SplitMessageReader *split_message_reader) {
716 DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(),
717 [split_message_reader](
718 const std::tuple<monotonic_clock::time_point,
719 uint32_t, SplitMessageReader *>
720 x) {
721 return std::get<2>(x) == split_message_reader;
722 }) == timestamp_heap_.end())
723 << ": Pushing timestamp when it is already in the heap.";
724
725 timestamp_heap_.push_back(std::make_tuple(
726 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
727
728 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
729 SplitMessageReaderHeapCompare);
730
731 // If we are a timestamp merger, don't wait for data. Missing data will be
732 // caught at read time.
733 if (has_timestamps_) {
734 channel_merger_->Update(std::get<0>(timestamp), channel_index_);
735 pushed_ = true;
736 }
737}
738
739std::tuple<monotonic_clock::time_point, uint32_t,
740 FlatbufferVector<MessageHeader>>
741TimestampMerger::PopMessageHeap() {
742 // Pop the oldest message reader pointer off the heap.
743 CHECK_GT(message_heap_.size(), 0u);
744 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
745 oldest_message_reader = message_heap_.front();
746
747 std::pop_heap(message_heap_.begin(), message_heap_.end(),
748 &SplitMessageReaderHeapCompare);
749 message_heap_.pop_back();
750
751 // Pop the oldest message. This re-pushes any messages from the reader to the
752 // message heap.
753 std::tuple<monotonic_clock::time_point, uint32_t,
754 FlatbufferVector<MessageHeader>>
755 oldest_message =
756 std::get<2>(oldest_message_reader)->PopOldest(channel_index_);
757
758 // Confirm that the time and queue_index we have recorded matches.
759 CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader));
760 CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader));
761
762 // Now, keep reading until we have found all duplicates.
763 while (message_heap_.size() > 0u) {
764 // See if it is a duplicate.
765 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
766 next_oldest_message_reader = message_heap_.front();
767
Austin Schuhcde938c2020-02-02 17:30:07 -0800768 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
769 next_oldest_message_time = std::get<2>(next_oldest_message_reader)
770 ->oldest_message(channel_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800771
772 if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) &&
773 std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) {
774 // Pop the message reader pointer.
775 std::pop_heap(message_heap_.begin(), message_heap_.end(),
776 &SplitMessageReaderHeapCompare);
777 message_heap_.pop_back();
778
779 // Pop the next oldest message. This re-pushes any messages from the
780 // reader.
781 std::tuple<monotonic_clock::time_point, uint32_t,
782 FlatbufferVector<MessageHeader>>
783 next_oldest_message = std::get<2>(next_oldest_message_reader)
784 ->PopOldest(channel_index_);
785
786 // And make sure the message matches in it's entirety.
787 CHECK(std::get<2>(oldest_message).span() ==
788 std::get<2>(next_oldest_message).span())
789 << ": Data at the same timestamp doesn't match.";
790 } else {
791 break;
792 }
793 }
794
795 return oldest_message;
796}
797
798std::tuple<monotonic_clock::time_point, uint32_t,
799 FlatbufferVector<MessageHeader>>
800TimestampMerger::PopTimestampHeap() {
801 // Pop the oldest message reader pointer off the heap.
802 CHECK_GT(timestamp_heap_.size(), 0u);
803
804 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
805 oldest_timestamp_reader = timestamp_heap_.front();
806
807 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
808 &SplitMessageReaderHeapCompare);
809 timestamp_heap_.pop_back();
810
811 CHECK(node_index_ != -1) << ": Timestamps in a single node environment";
812
813 // Pop the oldest message. This re-pushes any timestamps from the reader to
814 // the timestamp heap.
815 std::tuple<monotonic_clock::time_point, uint32_t,
816 FlatbufferVector<MessageHeader>>
817 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
818 ->PopOldest(channel_index_, node_index_);
819
820 // Confirm that the time we have recorded matches.
821 CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader));
822 CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader));
823
824 // TODO(austin): What if we get duplicate timestamps?
825
826 return oldest_timestamp;
827}
828
Austin Schuh8bd96322020-02-13 21:18:22 -0800829TimestampMerger::DeliveryTimestamp TimestampMerger::OldestTimestamp() const {
830 if (!has_timestamps_ || timestamp_heap_.size() == 0u) {
831 return TimestampMerger::DeliveryTimestamp{};
832 }
833
834 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
835 oldest_timestamp_reader = timestamp_heap_.front();
836
837 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
838 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
839 ->oldest_message(channel_index_, node_index_);
840
841 TimestampMerger::DeliveryTimestamp timestamp;
842 timestamp.monotonic_event_time =
843 monotonic_clock::time_point(chrono::nanoseconds(
844 std::get<2>(oldest_timestamp)->monotonic_sent_time()));
845 timestamp.realtime_event_time = realtime_clock::time_point(
846 chrono::nanoseconds(std::get<2>(oldest_timestamp)->realtime_sent_time()));
847
848 timestamp.monotonic_remote_time =
849 monotonic_clock::time_point(chrono::nanoseconds(
850 std::get<2>(oldest_timestamp)->monotonic_remote_time()));
851 timestamp.realtime_remote_time =
852 realtime_clock::time_point(chrono::nanoseconds(
853 std::get<2>(oldest_timestamp)->realtime_remote_time()));
854
855 timestamp.remote_queue_index = std::get<2>(oldest_timestamp)->queue_index();
856 return timestamp;
857}
858
Austin Schuh6f3babe2020-01-26 20:34:50 -0800859std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>>
860TimestampMerger::PopOldest() {
861 if (has_timestamps_) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800862 // Read the timestamps.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800863 std::tuple<monotonic_clock::time_point, uint32_t,
864 FlatbufferVector<MessageHeader>>
865 oldest_timestamp = PopTimestampHeap();
866
867 TimestampMerger::DeliveryTimestamp timestamp;
868 timestamp.monotonic_event_time =
869 monotonic_clock::time_point(chrono::nanoseconds(
870 std::get<2>(oldest_timestamp).message().monotonic_sent_time()));
871 timestamp.realtime_event_time =
872 realtime_clock::time_point(chrono::nanoseconds(
873 std::get<2>(oldest_timestamp).message().realtime_sent_time()));
874
875 // Consistency check.
876 CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp));
877 CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(),
878 std::get<1>(oldest_timestamp));
879
880 monotonic_clock::time_point remote_timestamp_monotonic_time(
881 chrono::nanoseconds(
882 std::get<2>(oldest_timestamp).message().monotonic_remote_time()));
883
Austin Schuh8bd96322020-02-13 21:18:22 -0800884 // See if we have any data. If not, pass the problem up the chain.
885 if (message_heap_.size() == 0u) {
886 VLOG(1) << "No data to match timestamp on "
887 << configuration::CleanedChannelToString(
888 configuration_->channels()->Get(channel_index_));
889 return std::make_tuple(timestamp,
890 std::move(std::get<2>(oldest_timestamp)));
891 }
892
Austin Schuh6f3babe2020-01-26 20:34:50 -0800893 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800894 {
895 // Ok, now try grabbing data until we find one which matches.
896 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
897 oldest_message_ref = oldest_message();
898
899 // Time at which the message was sent (this message is written from the
900 // sending node's perspective.
901 monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds(
902 std::get<2>(oldest_message_ref)->monotonic_sent_time()));
903
904 if (remote_monotonic_time < remote_timestamp_monotonic_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800905 VLOG(1) << "Undelivered message, skipping. Remote time is "
906 << remote_monotonic_time << " timestamp is "
907 << remote_timestamp_monotonic_time << " on channel "
908 << channel_index_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800909 PopMessageHeap();
910 continue;
911 } else if (remote_monotonic_time > remote_timestamp_monotonic_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800912 VLOG(1) << "Data not found. Remote time should be "
913 << remote_timestamp_monotonic_time << " on channel "
914 << channel_index_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800915 return std::make_tuple(timestamp,
916 std::move(std::get<2>(oldest_timestamp)));
917 }
918
919 timestamp.monotonic_remote_time = remote_monotonic_time;
920 }
921
Austin Schuh6f3babe2020-01-26 20:34:50 -0800922 std::tuple<monotonic_clock::time_point, uint32_t,
923 FlatbufferVector<MessageHeader>>
924 oldest_message = PopMessageHeap();
925
Austin Schuh6f3babe2020-01-26 20:34:50 -0800926 timestamp.realtime_remote_time =
927 realtime_clock::time_point(chrono::nanoseconds(
928 std::get<2>(oldest_message).message().realtime_sent_time()));
929 timestamp.remote_queue_index =
930 std::get<2>(oldest_message).message().queue_index();
931
Austin Schuhcde938c2020-02-02 17:30:07 -0800932 CHECK_EQ(timestamp.monotonic_remote_time,
933 remote_timestamp_monotonic_time);
934
935 CHECK_EQ(timestamp.remote_queue_index,
936 std::get<2>(oldest_timestamp).message().remote_queue_index())
937 << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message())
938 << " data "
939 << FlatbufferToJson(&std::get<2>(oldest_message).message());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800940
941 return std::make_tuple(timestamp, std::get<2>(oldest_message));
942 }
943 } else {
944 std::tuple<monotonic_clock::time_point, uint32_t,
945 FlatbufferVector<MessageHeader>>
946 oldest_message = PopMessageHeap();
947
948 TimestampMerger::DeliveryTimestamp timestamp;
949 timestamp.monotonic_event_time =
950 monotonic_clock::time_point(chrono::nanoseconds(
951 std::get<2>(oldest_message).message().monotonic_sent_time()));
952 timestamp.realtime_event_time =
953 realtime_clock::time_point(chrono::nanoseconds(
954 std::get<2>(oldest_message).message().realtime_sent_time()));
955 timestamp.remote_queue_index = 0xffffffff;
956
957 CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time);
958 CHECK_EQ(std::get<1>(oldest_message),
959 std::get<2>(oldest_message).message().queue_index());
960
961 return std::make_tuple(timestamp, std::get<2>(oldest_message));
962 }
963}
964
Austin Schuh8bd96322020-02-13 21:18:22 -0800965void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); }
966
Austin Schuh6f3babe2020-01-26 20:34:50 -0800967namespace {
968std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders(
969 const std::vector<std::vector<std::string>> &filenames) {
970 CHECK_GT(filenames.size(), 0u);
971 // Build up all the SplitMessageReaders.
972 std::vector<std::unique_ptr<SplitMessageReader>> result;
973 for (const std::vector<std::string> &filenames : filenames) {
974 result.emplace_back(std::make_unique<SplitMessageReader>(filenames));
975 }
976 return result;
977}
978} // namespace
979
980ChannelMerger::ChannelMerger(
981 const std::vector<std::vector<std::string>> &filenames)
982 : split_message_readers_(MakeSplitMessageReaders(filenames)),
983 log_file_header_(
984 CopyFlatBuffer(split_message_readers_[0]->log_file_header())) {
985 // Now, confirm that the configuration matches for each and pick a start time.
986 // Also return the list of possible nodes.
987 for (const std::unique_ptr<SplitMessageReader> &reader :
988 split_message_readers_) {
989 CHECK(CompareFlatBuffer(log_file_header_.message().configuration(),
990 reader->log_file_header()->configuration()))
991 << ": Replaying log files with different configurations isn't "
992 "supported";
993 }
994
995 nodes_ = configuration::GetNodes(configuration());
996}
997
998bool ChannelMerger::SetNode(const Node *target_node) {
999 std::vector<SplitMessageReader *> split_message_readers;
1000 for (const std::unique_ptr<SplitMessageReader> &reader :
1001 split_message_readers_) {
1002 split_message_readers.emplace_back(reader.get());
1003 }
1004
1005 // Go find a log_file_header for this node.
1006 {
1007 bool found_node = false;
1008
1009 for (const std::unique_ptr<SplitMessageReader> &reader :
1010 split_message_readers_) {
James Kuszmaulfc273dc2020-05-09 17:56:19 -07001011 // In order to identify which logfile(s) map to the target node, do a
1012 // logical comparison of the nodes, by confirming that we are either in a
1013 // single-node setup (where the nodes will both be nullptr) or that the
1014 // node names match (but the other node fields--e.g., hostname lists--may
1015 // not).
1016 const bool both_null =
1017 reader->node() == nullptr && target_node == nullptr;
1018 const bool both_have_name =
1019 (reader->node() != nullptr) && (target_node != nullptr) &&
1020 (reader->node()->has_name() && target_node->has_name());
1021 const bool node_names_identical =
1022 both_have_name &&
1023 (reader->node()->name()->string_view() ==
1024 target_node->name()->string_view());
1025 if (both_null || node_names_identical) {
Austin Schuh6f3babe2020-01-26 20:34:50 -08001026 if (!found_node) {
1027 found_node = true;
1028 log_file_header_ = CopyFlatBuffer(reader->log_file_header());
Austin Schuhcde938c2020-02-02 17:30:07 -08001029 VLOG(1) << "Found log file " << reader->filename() << " with node "
1030 << FlatbufferToJson(reader->node()) << " start_time "
1031 << monotonic_start_time();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001032 } else {
1033 // And then make sure all the other files have matching headers.
Austin Schuhcde938c2020-02-02 17:30:07 -08001034 CHECK(CompareFlatBuffer(log_file_header(), reader->log_file_header()))
1035 << ": " << FlatbufferToJson(log_file_header()) << " reader "
1036 << FlatbufferToJson(reader->log_file_header());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001037 }
1038 }
1039 }
1040
1041 if (!found_node) {
1042 LOG(WARNING) << "Failed to find log file for node "
1043 << FlatbufferToJson(target_node);
1044 return false;
1045 }
1046 }
1047
1048 // Build up all the timestamp mergers. This connects up all the
1049 // SplitMessageReaders.
1050 timestamp_mergers_.reserve(configuration()->channels()->size());
1051 for (size_t channel_index = 0;
1052 channel_index < configuration()->channels()->size(); ++channel_index) {
1053 timestamp_mergers_.emplace_back(
1054 configuration(), split_message_readers, channel_index,
1055 configuration::GetNode(configuration(), target_node), this);
1056 }
1057
1058 // And prime everything.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001059 for (std::unique_ptr<SplitMessageReader> &split_message_reader :
1060 split_message_readers_) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001061 split_message_reader->QueueMessages(
1062 split_message_reader->monotonic_start_time());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001063 }
1064
1065 node_ = configuration::GetNodeOrDie(configuration(), target_node);
1066 return true;
1067}
1068
1069monotonic_clock::time_point ChannelMerger::OldestMessage() const {
1070 if (channel_heap_.size() == 0u) {
1071 return monotonic_clock::max_time;
1072 }
1073 return channel_heap_.front().first;
1074}
1075
Austin Schuh8bd96322020-02-13 21:18:22 -08001076TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestamp() const {
1077 if (timestamp_heap_.size() == 0u) {
1078 return TimestampMerger::DeliveryTimestamp{};
1079 }
1080 return timestamp_mergers_[timestamp_heap_.front().second].OldestTimestamp();
1081}
1082
1083TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestampForChannel(
1084 int channel) const {
Austin Schuh6aa77be2020-02-22 21:06:40 -08001085 // If we didn't find any data for this node, we won't have any mergers. Return
1086 // an invalid timestamp in that case.
1087 if (timestamp_mergers_.size() <= static_cast<size_t>(channel)) {
1088 TimestampMerger::DeliveryTimestamp result;
1089 return result;
1090 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001091 return timestamp_mergers_[channel].OldestTimestamp();
1092}
1093
Austin Schuh6f3babe2020-01-26 20:34:50 -08001094void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp,
1095 int channel_index) {
1096 // Pop and recreate the heap if it has already been pushed. And since we are
1097 // pushing again, we don't need to clear pushed.
1098 if (timestamp_mergers_[channel_index].pushed()) {
1099 channel_heap_.erase(std::find_if(
1100 channel_heap_.begin(), channel_heap_.end(),
1101 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1102 return x.second == channel_index;
1103 }));
1104 std::make_heap(channel_heap_.begin(), channel_heap_.end(),
1105 ChannelHeapCompare);
Austin Schuh8bd96322020-02-13 21:18:22 -08001106
1107 if (timestamp_mergers_[channel_index].has_timestamps()) {
1108 timestamp_heap_.erase(std::find_if(
1109 timestamp_heap_.begin(), timestamp_heap_.end(),
1110 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1111 return x.second == channel_index;
1112 }));
1113 std::make_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1114 ChannelHeapCompare);
1115 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001116 }
1117
Austin Schuh05b70472020-01-01 17:11:17 -08001118 channel_heap_.push_back(std::make_pair(timestamp, channel_index));
1119
1120 // The default sort puts the newest message first. Use a custom comparator to
1121 // put the oldest message first.
1122 std::push_heap(channel_heap_.begin(), channel_heap_.end(),
1123 ChannelHeapCompare);
Austin Schuh8bd96322020-02-13 21:18:22 -08001124
1125 if (timestamp_mergers_[channel_index].has_timestamps()) {
1126 timestamp_heap_.push_back(std::make_pair(timestamp, channel_index));
1127 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1128 ChannelHeapCompare);
1129 }
Austin Schuh05b70472020-01-01 17:11:17 -08001130}
1131
Austin Schuh6f3babe2020-01-26 20:34:50 -08001132std::tuple<TimestampMerger::DeliveryTimestamp, int,
1133 FlatbufferVector<MessageHeader>>
1134ChannelMerger::PopOldest() {
Austin Schuh8bd96322020-02-13 21:18:22 -08001135 CHECK_GT(channel_heap_.size(), 0u);
Austin Schuh05b70472020-01-01 17:11:17 -08001136 std::pair<monotonic_clock::time_point, int> oldest_channel_data =
1137 channel_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001138 int channel_index = oldest_channel_data.second;
Austin Schuh05b70472020-01-01 17:11:17 -08001139 std::pop_heap(channel_heap_.begin(), channel_heap_.end(),
1140 &ChannelHeapCompare);
1141 channel_heap_.pop_back();
Austin Schuh8bd96322020-02-13 21:18:22 -08001142
Austin Schuh6f3babe2020-01-26 20:34:50 -08001143 timestamp_mergers_[channel_index].set_pushed(false);
Austin Schuh05b70472020-01-01 17:11:17 -08001144
Austin Schuh6f3babe2020-01-26 20:34:50 -08001145 TimestampMerger *merger = &timestamp_mergers_[channel_index];
Austin Schuh05b70472020-01-01 17:11:17 -08001146
Austin Schuh8bd96322020-02-13 21:18:22 -08001147 if (merger->has_timestamps()) {
1148 CHECK_GT(timestamp_heap_.size(), 0u);
1149 std::pair<monotonic_clock::time_point, int> oldest_timestamp_data =
1150 timestamp_heap_.front();
1151 CHECK(oldest_timestamp_data == oldest_channel_data)
1152 << ": Timestamp heap out of sync.";
1153 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1154 &ChannelHeapCompare);
1155 timestamp_heap_.pop_back();
1156 }
1157
Austin Schuhcde938c2020-02-02 17:30:07 -08001158 // Merger handles any queueing needed from here.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001159 std::tuple<TimestampMerger::DeliveryTimestamp,
1160 FlatbufferVector<MessageHeader>>
1161 message = merger->PopOldest();
Austin Schuh05b70472020-01-01 17:11:17 -08001162
Austin Schuh6f3babe2020-01-26 20:34:50 -08001163 return std::make_tuple(std::get<0>(message), channel_index,
1164 std::move(std::get<1>(message)));
1165}
1166
Austin Schuhcde938c2020-02-02 17:30:07 -08001167std::string SplitMessageReader::MessageHeaderQueue::DebugString() const {
1168 std::stringstream ss;
1169 for (size_t i = 0; i < data_.size(); ++i) {
1170 if (timestamps) {
1171 ss << " msg: ";
1172 } else {
1173 ss << " timestamp: ";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001174 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001175 ss << monotonic_clock::time_point(std::chrono::nanoseconds(
1176 data_[i].message().monotonic_sent_time()))
1177 << " ("
1178 << realtime_clock::time_point(
1179 std::chrono::nanoseconds(data_[i].message().realtime_sent_time()))
1180 << ") " << data_[i].message().queue_index();
1181 if (timestamps) {
1182 ss << " <- remote "
1183 << monotonic_clock::time_point(std::chrono::nanoseconds(
1184 data_[i].message().monotonic_remote_time()))
1185 << " ("
1186 << realtime_clock::time_point(std::chrono::nanoseconds(
1187 data_[i].message().realtime_remote_time()))
1188 << ")";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001189 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001190 ss << "\n";
1191 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001192
Austin Schuhcde938c2020-02-02 17:30:07 -08001193 return ss.str();
1194}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001195
Austin Schuhcde938c2020-02-02 17:30:07 -08001196std::string SplitMessageReader::DebugString(int channel) const {
1197 std::stringstream ss;
1198 ss << "[\n";
1199 ss << channels_[channel].data.DebugString();
1200 ss << " ]";
1201 return ss.str();
1202}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001203
Austin Schuhcde938c2020-02-02 17:30:07 -08001204std::string SplitMessageReader::DebugString(int channel, int node_index) const {
1205 std::stringstream ss;
1206 ss << "[\n";
1207 ss << channels_[channel].timestamps[node_index].DebugString();
1208 ss << " ]";
1209 return ss.str();
1210}
1211
1212std::string TimestampMerger::DebugString() const {
1213 std::stringstream ss;
1214
1215 if (timestamp_heap_.size() > 0) {
1216 ss << " timestamp_heap {\n";
1217 std::vector<
1218 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1219 timestamp_heap = timestamp_heap_;
1220 while (timestamp_heap.size() > 0u) {
1221 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1222 oldest_timestamp_reader = timestamp_heap.front();
1223
1224 ss << " " << std::get<2>(oldest_timestamp_reader) << " "
1225 << std::get<0>(oldest_timestamp_reader) << " queue_index ("
1226 << std::get<1>(oldest_timestamp_reader) << ") ttq "
1227 << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " "
1228 << std::get<2>(oldest_timestamp_reader)->filename() << " -> "
1229 << std::get<2>(oldest_timestamp_reader)
1230 ->DebugString(channel_index_, node_index_)
1231 << "\n";
1232
1233 std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(),
1234 &SplitMessageReaderHeapCompare);
1235 timestamp_heap.pop_back();
1236 }
1237 ss << " }\n";
1238 }
1239
1240 ss << " message_heap {\n";
1241 {
1242 std::vector<
1243 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1244 message_heap = message_heap_;
1245 while (message_heap.size() > 0u) {
1246 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1247 oldest_message_reader = message_heap.front();
1248
1249 ss << " " << std::get<2>(oldest_message_reader) << " "
1250 << std::get<0>(oldest_message_reader) << " queue_index ("
1251 << std::get<1>(oldest_message_reader) << ") ttq "
1252 << std::get<2>(oldest_message_reader)->time_to_queue() << " "
1253 << std::get<2>(oldest_message_reader)->filename() << " -> "
1254 << std::get<2>(oldest_message_reader)->DebugString(channel_index_)
1255 << "\n";
1256
1257 std::pop_heap(message_heap.begin(), message_heap.end(),
1258 &SplitMessageReaderHeapCompare);
1259 message_heap.pop_back();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001260 }
Austin Schuh05b70472020-01-01 17:11:17 -08001261 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001262 ss << " }";
1263
1264 return ss.str();
1265}
1266
1267std::string ChannelMerger::DebugString() const {
1268 std::stringstream ss;
1269 ss << "start_time " << realtime_start_time() << " " << monotonic_start_time()
1270 << "\n";
1271 ss << "channel_heap {\n";
1272 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1273 channel_heap_;
1274 while (channel_heap.size() > 0u) {
1275 std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front();
1276 ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") "
1277 << configuration::CleanedChannelToString(
1278 configuration()->channels()->Get(std::get<1>(channel)))
1279 << "\n";
1280
1281 ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n";
1282
1283 std::pop_heap(channel_heap.begin(), channel_heap.end(),
1284 &ChannelHeapCompare);
1285 channel_heap.pop_back();
1286 }
1287 ss << "}";
1288
1289 return ss.str();
Austin Schuh05b70472020-01-01 17:11:17 -08001290}
1291
Austin Schuha36c8902019-12-30 18:07:15 -08001292} // namespace logger
1293} // namespace aos