blob: 5388df7a241671605b56a3cefb2ccf69caaba951 [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;
65}
66
Austin Schuha36c8902019-12-30 18:07:15 -080067void DetachedBufferWriter::QueueSizedFlatbuffer(
68 flatbuffers::DetachedBuffer &&buffer) {
69 queued_size_ += buffer.size();
70 queue_.emplace_back(std::move(buffer));
71
72 // Flush if we are at the max number of iovs per writev, or have written
73 // enough data. Otherwise writev will fail with an invalid argument.
74 if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) ||
75 queue_.size() == IOV_MAX) {
76 Flush();
77 }
78}
79
80void DetachedBufferWriter::Flush() {
81 if (queue_.size() == 0u) {
82 return;
83 }
84 iovec_.clear();
85 iovec_.reserve(queue_.size());
86 size_t counted_size = 0;
87 for (size_t i = 0; i < queue_.size(); ++i) {
88 struct iovec n;
89 n.iov_base = queue_[i].data();
90 n.iov_len = queue_[i].size();
91 counted_size += n.iov_len;
92 iovec_.emplace_back(std::move(n));
93 }
94 CHECK_EQ(counted_size, queued_size_);
95 const ssize_t written = writev(fd_, iovec_.data(), iovec_.size());
96
97 PCHECK(written == static_cast<ssize_t>(queued_size_))
98 << ": Wrote " << written << " expected " << queued_size_;
99
100 queued_size_ = 0;
101 queue_.clear();
102 // TODO(austin): Handle partial writes in some way other than crashing...
103}
104
105flatbuffers::Offset<MessageHeader> PackMessage(
106 flatbuffers::FlatBufferBuilder *fbb, const Context &context,
107 int channel_index, LogType log_type) {
108 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset;
109
110 switch (log_type) {
111 case LogType::kLogMessage:
112 case LogType::kLogMessageAndDeliveryTime:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800113 case LogType::kLogRemoteMessage:
Austin Schuha36c8902019-12-30 18:07:15 -0800114 data_offset =
115 fbb->CreateVector(static_cast<uint8_t *>(context.data), context.size);
116 break;
117
118 case LogType::kLogDeliveryTimeOnly:
119 break;
120 }
121
122 MessageHeader::Builder message_header_builder(*fbb);
123 message_header_builder.add_channel_index(channel_index);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800124
125 switch (log_type) {
126 case LogType::kLogRemoteMessage:
127 message_header_builder.add_queue_index(context.remote_queue_index);
128 message_header_builder.add_monotonic_sent_time(
129 context.monotonic_remote_time.time_since_epoch().count());
130 message_header_builder.add_realtime_sent_time(
131 context.realtime_remote_time.time_since_epoch().count());
132 break;
133
134 case LogType::kLogMessage:
135 case LogType::kLogMessageAndDeliveryTime:
136 case LogType::kLogDeliveryTimeOnly:
137 message_header_builder.add_queue_index(context.queue_index);
138 message_header_builder.add_monotonic_sent_time(
139 context.monotonic_event_time.time_since_epoch().count());
140 message_header_builder.add_realtime_sent_time(
141 context.realtime_event_time.time_since_epoch().count());
142 break;
143 }
Austin Schuha36c8902019-12-30 18:07:15 -0800144
145 switch (log_type) {
146 case LogType::kLogMessage:
Austin Schuh6f3babe2020-01-26 20:34:50 -0800147 case LogType::kLogRemoteMessage:
Austin Schuha36c8902019-12-30 18:07:15 -0800148 message_header_builder.add_data(data_offset);
149 break;
150
151 case LogType::kLogMessageAndDeliveryTime:
152 message_header_builder.add_data(data_offset);
153 [[fallthrough]];
154
155 case LogType::kLogDeliveryTimeOnly:
156 message_header_builder.add_monotonic_remote_time(
157 context.monotonic_remote_time.time_since_epoch().count());
158 message_header_builder.add_realtime_remote_time(
159 context.realtime_remote_time.time_since_epoch().count());
160 message_header_builder.add_remote_queue_index(context.remote_queue_index);
161 break;
162 }
163
164 return message_header_builder.Finish();
165}
166
Austin Schuh05b70472020-01-01 17:11:17 -0800167SpanReader::SpanReader(std::string_view filename)
Austin Schuh6f3babe2020-01-26 20:34:50 -0800168 : filename_(filename),
169 fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) {
Austin Schuh05b70472020-01-01 17:11:17 -0800170 PCHECK(fd_ != -1) << ": Failed to open " << filename;
171}
172
173absl::Span<const uint8_t> SpanReader::ReadMessage() {
174 // Make sure we have enough for the size.
175 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
176 if (!ReadBlock()) {
177 return absl::Span<const uint8_t>();
178 }
179 }
180
181 // Now make sure we have enough for the message.
182 const size_t data_size =
183 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
184 sizeof(flatbuffers::uoffset_t);
Austin Schuhe4fca832020-03-07 16:58:53 -0800185 if (data_size == sizeof(flatbuffers::uoffset_t)) {
186 LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping.";
187 LOG(ERROR) << " Rest of log file is "
188 << absl::BytesToHexString(std::string_view(
189 reinterpret_cast<const char *>(data_.data() +
190 consumed_data_),
191 data_.size() - consumed_data_));
192 return absl::Span<const uint8_t>();
193 }
Austin Schuh05b70472020-01-01 17:11:17 -0800194 while (data_.size() < consumed_data_ + data_size) {
195 if (!ReadBlock()) {
196 return absl::Span<const uint8_t>();
197 }
198 }
199
200 // And return it, consuming the data.
201 const uint8_t *data_ptr = data_.data() + consumed_data_;
202
203 consumed_data_ += data_size;
204
205 return absl::Span<const uint8_t>(data_ptr, data_size);
206}
207
208bool SpanReader::MessageAvailable() {
209 // Are we big enough to read the size?
210 if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) {
211 return false;
212 }
213
214 // Then, are we big enough to read the full message?
215 const size_t data_size =
216 flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) +
217 sizeof(flatbuffers::uoffset_t);
218 if (data_.size() < consumed_data_ + data_size) {
219 return false;
220 }
221
222 return true;
223}
224
225bool SpanReader::ReadBlock() {
226 if (end_of_file_) {
227 return false;
228 }
229
230 // Appends 256k. This is enough that the read call is efficient. We don't
231 // want to spend too much time reading small chunks because the syscalls for
232 // that will be expensive.
233 constexpr size_t kReadSize = 256 * 1024;
234
235 // Strip off any unused data at the front.
236 if (consumed_data_ != 0) {
237 data_.erase(data_.begin(), data_.begin() + consumed_data_);
238 consumed_data_ = 0;
239 }
240
241 const size_t starting_size = data_.size();
242
243 // This should automatically grow the backing store. It won't shrink if we
244 // get a small chunk later. This reduces allocations when we want to append
245 // more data.
246 data_.resize(data_.size() + kReadSize);
247
248 ssize_t count = read(fd_, &data_[starting_size], kReadSize);
249 data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0)));
250 if (count == 0) {
251 end_of_file_ = true;
252 return false;
253 }
254 PCHECK(count > 0);
255
256 return true;
257}
258
Austin Schuh6f3babe2020-01-26 20:34:50 -0800259FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename) {
260 SpanReader span_reader(filename);
261 // Make sure we have enough to read the size.
262 absl::Span<const uint8_t> config_data = span_reader.ReadMessage();
263
264 // Make sure something was read.
265 CHECK(config_data != absl::Span<const uint8_t>());
266
267 // And copy the config so we have it forever.
268 std::vector<uint8_t> data(
269 config_data.begin() + sizeof(flatbuffers::uoffset_t), config_data.end());
270 return FlatbufferVector<LogFileHeader>(std::move(data));
271}
272
Austin Schuh05b70472020-01-01 17:11:17 -0800273MessageReader::MessageReader(std::string_view filename)
274 : span_reader_(filename) {
275 // Make sure we have enough to read the size.
276 absl::Span<const uint8_t> config_data = span_reader_.ReadMessage();
277
278 // Make sure something was read.
279 CHECK(config_data != absl::Span<const uint8_t>());
280
281 // And copy the config so we have it forever.
282 configuration_ = std::vector<uint8_t>(config_data.begin(), config_data.end());
283
Austin Schuhcde938c2020-02-02 17:30:07 -0800284 max_out_of_order_duration_ =
285 std::chrono::nanoseconds(log_file_header()->max_out_of_order_duration());
286
287 VLOG(1) << "Opened " << filename << " as node "
288 << FlatbufferToJson(log_file_header()->node());
Austin Schuh05b70472020-01-01 17:11:17 -0800289}
290
291std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() {
292 absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage();
293 if (msg_data == absl::Span<const uint8_t>()) {
294 return std::nullopt;
295 }
296
297 FlatbufferVector<MessageHeader> result{std::vector<uint8_t>(
298 msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())};
299
300 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
301 chrono::nanoseconds(result.message().monotonic_sent_time()));
302
303 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
Austin Schuh8bd96322020-02-13 21:18:22 -0800304 VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800305 return std::move(result);
Austin Schuh05b70472020-01-01 17:11:17 -0800306}
307
Austin Schuh6f3babe2020-01-26 20:34:50 -0800308SplitMessageReader::SplitMessageReader(
Austin Schuhfa895892020-01-07 20:07:41 -0800309 const std::vector<std::string> &filenames)
310 : filenames_(filenames),
311 log_file_header_(FlatbufferDetachedBuffer<LogFileHeader>::Empty()) {
312 CHECK(NextLogFile()) << ": filenames is empty. Need files to read.";
313
Austin Schuh6f3babe2020-01-26 20:34:50 -0800314 // Grab any log file header. They should all match (and we will check as we
315 // open more of them).
Austin Schuhfa895892020-01-07 20:07:41 -0800316 log_file_header_ = CopyFlatBuffer(message_reader_->log_file_header());
317
Austin Schuh6f3babe2020-01-26 20:34:50 -0800318 // Setup per channel state.
Austin Schuh05b70472020-01-01 17:11:17 -0800319 channels_.resize(configuration()->channels()->size());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800320 for (ChannelData &channel_data : channels_) {
321 channel_data.data.split_reader = this;
322 // Build up the timestamp list.
323 if (configuration::MultiNode(configuration())) {
324 channel_data.timestamps.resize(configuration()->nodes()->size());
325 for (MessageHeaderQueue &queue : channel_data.timestamps) {
326 queue.timestamps = true;
327 queue.split_reader = this;
328 }
329 }
330 }
Austin Schuh05b70472020-01-01 17:11:17 -0800331
Austin Schuh6f3babe2020-01-26 20:34:50 -0800332 // Build up channels_to_write_ as an optimization to make it fast to figure
333 // out which datastructure to place any new data from a channel on.
334 for (const Channel *channel : *configuration()->channels()) {
335 // This is the main case. We will only see data on this node.
336 if (configuration::ChannelIsSendableOnNode(channel, node())) {
337 channels_to_write_.emplace_back(
338 &channels_[channels_to_write_.size()].data);
339 } else
340 // If we can't send, but can receive, we should be able to see
341 // timestamps here.
342 if (configuration::ChannelIsReadableOnNode(channel, node())) {
343 channels_to_write_.emplace_back(
344 &(channels_[channels_to_write_.size()]
345 .timestamps[configuration::GetNodeIndex(configuration(),
346 node())]));
347 } else {
348 channels_to_write_.emplace_back(nullptr);
349 }
350 }
Austin Schuh05b70472020-01-01 17:11:17 -0800351}
352
Austin Schuh6f3babe2020-01-26 20:34:50 -0800353bool SplitMessageReader::NextLogFile() {
Austin Schuhfa895892020-01-07 20:07:41 -0800354 if (next_filename_index_ == filenames_.size()) {
355 return false;
356 }
357 message_reader_ =
358 std::make_unique<MessageReader>(filenames_[next_filename_index_]);
359
360 // We can't support the config diverging between two log file headers. See if
361 // they are the same.
362 if (next_filename_index_ != 0) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800363 CHECK(CompareFlatBuffer(&log_file_header_.message(),
364 message_reader_->log_file_header()))
Austin Schuhfa895892020-01-07 20:07:41 -0800365 << ": Header is different between log file chunks "
366 << filenames_[next_filename_index_] << " and "
367 << filenames_[next_filename_index_ - 1] << ", this is not supported.";
368 }
369
370 ++next_filename_index_;
371 return true;
372}
373
Austin Schuh6f3babe2020-01-26 20:34:50 -0800374bool SplitMessageReader::QueueMessages(
Austin Schuhcde938c2020-02-02 17:30:07 -0800375 monotonic_clock::time_point last_dequeued_time) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800376 // TODO(austin): Once we are happy that everything works, read a 256kb chunk
377 // to reduce the need to re-heap down below.
Austin Schuhcde938c2020-02-02 17:30:07 -0800378
379 // Special case no more data. Otherwise we blow up on the CHECK statement
380 // confirming that we have enough data queued.
381 if (at_end_) {
382 return false;
383 }
384
385 // If this isn't the first time around, confirm that we had enough data queued
386 // to follow the contract.
387 if (time_to_queue_ != monotonic_clock::min_time) {
388 CHECK_LE(last_dequeued_time,
389 newest_timestamp() - max_out_of_order_duration())
390 << " node " << FlatbufferToJson(node()) << " on " << this;
391
392 // Bail if there is enough data already queued.
393 if (last_dequeued_time < time_to_queue_) {
394 VLOG(1) << "All up to date on " << this << ", dequeued "
395 << last_dequeued_time << " queue time " << time_to_queue_;
396 return true;
397 }
398 } else {
399 // Startup takes a special dance. We want to queue up until the start time,
400 // but we then want to find the next message to read. The conservative
401 // answer is to immediately trigger a second requeue to get things moving.
402 time_to_queue_ = monotonic_start_time();
403 QueueMessages(time_to_queue_);
404 }
405
406 // If we are asked to queue, queue for at least max_out_of_order_duration past
407 // the last known time in the log file (ie the newest timestep read). As long
408 // as we requeue exactly when time_to_queue_ is dequeued and go no further, we
409 // are safe. And since we pop in order, that works.
410 //
411 // Special case the start of the log file. There should be at most 1 message
412 // from each channel at the start of the log file. So always force the start
413 // of the log file to just be read.
414 time_to_queue_ = std::max(time_to_queue_, newest_timestamp());
415 VLOG(1) << "Queueing, going until " << time_to_queue_ << " " << filename();
416
417 bool was_emplaced = false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800418 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800419 // Stop if we have enough.
420 if (newest_timestamp() >
421 time_to_queue_ + max_out_of_order_duration() &&
422 was_emplaced) {
423 VLOG(1) << "Done queueing on " << this << ", queued to "
424 << newest_timestamp() << " with requeue time " << time_to_queue_;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800425 return true;
426 }
Austin Schuh05b70472020-01-01 17:11:17 -0800427
Austin Schuh6f3babe2020-01-26 20:34:50 -0800428 if (std::optional<FlatbufferVector<MessageHeader>> msg =
429 message_reader_->ReadMessage()) {
430 const MessageHeader &header = msg.value().message();
431
Austin Schuhcde938c2020-02-02 17:30:07 -0800432 const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
433 chrono::nanoseconds(header.monotonic_sent_time()));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800434
Austin Schuh0b5fd032020-03-28 17:36:49 -0700435 if (VLOG_IS_ON(2)) {
436 LOG(INFO) << "Queued " << this << " " << filename()
437 << " ttq: " << time_to_queue_ << " now " << newest_timestamp()
438 << " start time " << monotonic_start_time() << " "
439 << FlatbufferToJson(&header);
440 } else if (VLOG_IS_ON(1)) {
441 FlatbufferVector<MessageHeader> copy = msg.value();
442 copy.mutable_message()->clear_data();
443 LOG(INFO) << "Queued " << this << " " << filename()
444 << " ttq: " << time_to_queue_ << " now " << newest_timestamp()
445 << " start time " << monotonic_start_time() << " "
446 << FlatbufferToJson(copy);
447 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800448
449 const int channel_index = header.channel_index();
450 was_emplaced = channels_to_write_[channel_index]->emplace_back(
451 std::move(msg.value()));
452 if (was_emplaced) {
453 newest_timestamp_ = std::max(newest_timestamp_, timestamp);
454 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800455 } else {
456 if (!NextLogFile()) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800457 VLOG(1) << "End of log file " << filenames_.back();
Austin Schuhcde938c2020-02-02 17:30:07 -0800458 at_end_ = true;
Austin Schuh8bd96322020-02-13 21:18:22 -0800459 for (MessageHeaderQueue *queue : channels_to_write_) {
460 if (queue == nullptr || queue->timestamp_merger == nullptr) {
461 continue;
462 }
463 queue->timestamp_merger->NoticeAtEnd();
464 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800465 return false;
466 }
467 }
Austin Schuh05b70472020-01-01 17:11:17 -0800468 }
Austin Schuh6f3babe2020-01-26 20:34:50 -0800469}
470
471void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger,
472 int channel_index,
473 const Node *target_node) {
474 const Node *reinterpreted_target_node =
475 configuration::GetNodeOrDie(configuration(), target_node);
476 const Channel *const channel =
477 configuration()->channels()->Get(channel_index);
478
Austin Schuhcde938c2020-02-02 17:30:07 -0800479 VLOG(1) << " Configuring merger " << this << " for channel " << channel_index
480 << " "
481 << configuration::CleanedChannelToString(
482 configuration()->channels()->Get(channel_index));
483
Austin Schuh6f3babe2020-01-26 20:34:50 -0800484 MessageHeaderQueue *message_header_queue = nullptr;
485
486 // Figure out if this log file is from our point of view, or the other node's
487 // point of view.
488 if (node() == reinterpreted_target_node) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800489 VLOG(1) << " Replaying as logged node " << filename();
490
491 if (configuration::ChannelIsSendableOnNode(channel, node())) {
492 VLOG(1) << " Data on node";
493 message_header_queue = &(channels_[channel_index].data);
494 } else if (configuration::ChannelIsReadableOnNode(channel, node())) {
495 VLOG(1) << " Timestamps on node";
496 message_header_queue =
497 &(channels_[channel_index].timestamps[configuration::GetNodeIndex(
498 configuration(), node())]);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800499 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800500 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800501 }
502 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800503 VLOG(1) << " Replaying as other node " << filename();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800504 // We are replaying from another node's point of view. The only interesting
Austin Schuhcde938c2020-02-02 17:30:07 -0800505 // data is data that is sent from our node and received on theirs.
506 if (configuration::ChannelIsReadableOnNode(channel,
507 reinterpreted_target_node) &&
508 configuration::ChannelIsSendableOnNode(channel, node())) {
509 VLOG(1) << " Readable on target node";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800510 // Data from another node.
511 message_header_queue = &(channels_[channel_index].data);
512 } else {
Austin Schuhcde938c2020-02-02 17:30:07 -0800513 VLOG(1) << " Dropping";
Austin Schuh6f3babe2020-01-26 20:34:50 -0800514 // This is either not sendable on the other node, or is a timestamp and
515 // therefore not interesting.
516 }
517 }
518
519 // If we found one, write it down. This will be nullptr when there is nothing
520 // relevant on this channel on this node for the target node. In that case,
521 // we want to drop the message instead of queueing it.
522 if (message_header_queue != nullptr) {
523 message_header_queue->timestamp_merger = timestamp_merger;
524 }
525}
526
527std::tuple<monotonic_clock::time_point, uint32_t,
528 FlatbufferVector<MessageHeader>>
529SplitMessageReader::PopOldest(int channel_index) {
530 CHECK_GT(channels_[channel_index].data.size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800531 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
532 timestamp = channels_[channel_index].data.front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800533 FlatbufferVector<MessageHeader> front =
534 std::move(channels_[channel_index].data.front());
535 channels_[channel_index].data.pop_front();
Austin Schuhcde938c2020-02-02 17:30:07 -0800536
537 VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp);
538
539 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800540
541 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
542 std::move(front));
543}
544
545std::tuple<monotonic_clock::time_point, uint32_t,
546 FlatbufferVector<MessageHeader>>
547SplitMessageReader::PopOldest(int channel, int node_index) {
548 CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u);
Austin Schuhcde938c2020-02-02 17:30:07 -0800549 const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
550 timestamp = channels_[channel].timestamps[node_index].front_timestamp();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800551 FlatbufferVector<MessageHeader> front =
552 std::move(channels_[channel].timestamps[node_index].front());
553 channels_[channel].timestamps[node_index].pop_front();
Austin Schuhcde938c2020-02-02 17:30:07 -0800554
555 VLOG(1) << "Popped " << this << " " << std::get<0>(timestamp);
556
557 QueueMessages(std::get<0>(timestamp));
Austin Schuh6f3babe2020-01-26 20:34:50 -0800558
559 return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp),
560 std::move(front));
561}
562
Austin Schuhcde938c2020-02-02 17:30:07 -0800563bool SplitMessageReader::MessageHeaderQueue::emplace_back(
Austin Schuh6f3babe2020-01-26 20:34:50 -0800564 FlatbufferVector<MessageHeader> &&msg) {
565 CHECK(split_reader != nullptr);
566
567 // If there is no timestamp merger for this queue, nobody is listening. Drop
568 // the message. This happens when a log file from another node is replayed,
569 // and the timestamp mergers down stream just don't care.
570 if (timestamp_merger == nullptr) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800571 return false;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800572 }
573
574 CHECK(timestamps != msg.message().has_data())
575 << ": Got timestamps and data mixed up on a node. "
576 << FlatbufferToJson(msg);
577
578 data_.emplace_back(std::move(msg));
579
580 if (data_.size() == 1u) {
581 // Yup, new data. Notify.
582 if (timestamps) {
583 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
584 } else {
585 timestamp_merger->Update(split_reader, front_timestamp());
586 }
587 }
Austin Schuhcde938c2020-02-02 17:30:07 -0800588
589 return true;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800590}
591
592void SplitMessageReader::MessageHeaderQueue::pop_front() {
593 data_.pop_front();
594 if (data_.size() != 0u) {
595 // Yup, new data.
596 if (timestamps) {
597 timestamp_merger->UpdateTimestamp(split_reader, front_timestamp());
598 } else {
599 timestamp_merger->Update(split_reader, front_timestamp());
600 }
601 }
Austin Schuh05b70472020-01-01 17:11:17 -0800602}
603
604namespace {
605
Austin Schuh6f3babe2020-01-26 20:34:50 -0800606bool SplitMessageReaderHeapCompare(
607 const std::tuple<monotonic_clock::time_point, uint32_t,
608 SplitMessageReader *>
609 first,
610 const std::tuple<monotonic_clock::time_point, uint32_t,
611 SplitMessageReader *>
612 second) {
613 if (std::get<0>(first) > std::get<0>(second)) {
614 return true;
615 } else if (std::get<0>(first) == std::get<0>(second)) {
616 if (std::get<1>(first) > std::get<1>(second)) {
617 return true;
618 } else if (std::get<1>(first) == std::get<1>(second)) {
619 return std::get<2>(first) > std::get<2>(second);
620 } else {
621 return false;
622 }
623 } else {
624 return false;
625 }
626}
627
Austin Schuh05b70472020-01-01 17:11:17 -0800628bool ChannelHeapCompare(
629 const std::pair<monotonic_clock::time_point, int> first,
630 const std::pair<monotonic_clock::time_point, int> second) {
631 if (first.first > second.first) {
632 return true;
633 } else if (first.first == second.first) {
634 return first.second > second.second;
635 } else {
636 return false;
637 }
638}
639
640} // namespace
641
Austin Schuh6f3babe2020-01-26 20:34:50 -0800642TimestampMerger::TimestampMerger(
643 const Configuration *configuration,
644 std::vector<SplitMessageReader *> split_message_readers, int channel_index,
645 const Node *target_node, ChannelMerger *channel_merger)
646 : configuration_(configuration),
647 split_message_readers_(std::move(split_message_readers)),
648 channel_index_(channel_index),
649 node_index_(configuration::MultiNode(configuration)
650 ? configuration::GetNodeIndex(configuration, target_node)
651 : -1),
652 channel_merger_(channel_merger) {
653 // Tell the readers we care so they know who to notify.
Austin Schuhcde938c2020-02-02 17:30:07 -0800654 VLOG(1) << "Configuring channel " << channel_index << " target node "
655 << FlatbufferToJson(target_node);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800656 for (SplitMessageReader *reader : split_message_readers_) {
657 reader->SetTimestampMerger(this, channel_index, target_node);
658 }
659
660 // And then determine if we need to track timestamps.
661 const Channel *channel = configuration->channels()->Get(channel_index);
662 if (!configuration::ChannelIsSendableOnNode(channel, target_node) &&
663 configuration::ChannelIsReadableOnNode(channel, target_node)) {
664 has_timestamps_ = true;
665 }
666}
667
668void TimestampMerger::PushMessageHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800669 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
670 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800671 SplitMessageReader *split_message_reader) {
672 DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(),
673 [split_message_reader](
674 const std::tuple<monotonic_clock::time_point,
675 uint32_t, SplitMessageReader *>
676 x) {
677 return std::get<2>(x) == split_message_reader;
678 }) == message_heap_.end())
679 << ": Pushing message when it is already in the heap.";
680
681 message_heap_.push_back(std::make_tuple(
682 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
683
684 std::push_heap(message_heap_.begin(), message_heap_.end(),
685 &SplitMessageReaderHeapCompare);
686
687 // If we are just a data merger, don't wait for timestamps.
688 if (!has_timestamps_) {
689 channel_merger_->Update(std::get<0>(timestamp), channel_index_);
690 pushed_ = true;
691 }
692}
693
Austin Schuhcde938c2020-02-02 17:30:07 -0800694std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
695TimestampMerger::oldest_message() const {
696 CHECK_GT(message_heap_.size(), 0u);
697 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
698 oldest_message_reader = message_heap_.front();
699 return std::get<2>(oldest_message_reader)->oldest_message(channel_index_);
700}
701
702std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
703TimestampMerger::oldest_timestamp() const {
704 CHECK_GT(timestamp_heap_.size(), 0u);
705 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
706 oldest_message_reader = timestamp_heap_.front();
707 return std::get<2>(oldest_message_reader)
708 ->oldest_message(channel_index_, node_index_);
709}
710
Austin Schuh6f3babe2020-01-26 20:34:50 -0800711void TimestampMerger::PushTimestampHeap(
Austin Schuhcde938c2020-02-02 17:30:07 -0800712 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
713 timestamp,
Austin Schuh6f3babe2020-01-26 20:34:50 -0800714 SplitMessageReader *split_message_reader) {
715 DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(),
716 [split_message_reader](
717 const std::tuple<monotonic_clock::time_point,
718 uint32_t, SplitMessageReader *>
719 x) {
720 return std::get<2>(x) == split_message_reader;
721 }) == timestamp_heap_.end())
722 << ": Pushing timestamp when it is already in the heap.";
723
724 timestamp_heap_.push_back(std::make_tuple(
725 std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader));
726
727 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
728 SplitMessageReaderHeapCompare);
729
730 // If we are a timestamp merger, don't wait for data. Missing data will be
731 // caught at read time.
732 if (has_timestamps_) {
733 channel_merger_->Update(std::get<0>(timestamp), channel_index_);
734 pushed_ = true;
735 }
736}
737
738std::tuple<monotonic_clock::time_point, uint32_t,
739 FlatbufferVector<MessageHeader>>
740TimestampMerger::PopMessageHeap() {
741 // Pop the oldest message reader pointer off the heap.
742 CHECK_GT(message_heap_.size(), 0u);
743 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
744 oldest_message_reader = message_heap_.front();
745
746 std::pop_heap(message_heap_.begin(), message_heap_.end(),
747 &SplitMessageReaderHeapCompare);
748 message_heap_.pop_back();
749
750 // Pop the oldest message. This re-pushes any messages from the reader to the
751 // message heap.
752 std::tuple<monotonic_clock::time_point, uint32_t,
753 FlatbufferVector<MessageHeader>>
754 oldest_message =
755 std::get<2>(oldest_message_reader)->PopOldest(channel_index_);
756
757 // Confirm that the time and queue_index we have recorded matches.
758 CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader));
759 CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader));
760
761 // Now, keep reading until we have found all duplicates.
762 while (message_heap_.size() > 0u) {
763 // See if it is a duplicate.
764 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
765 next_oldest_message_reader = message_heap_.front();
766
Austin Schuhcde938c2020-02-02 17:30:07 -0800767 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
768 next_oldest_message_time = std::get<2>(next_oldest_message_reader)
769 ->oldest_message(channel_index_);
Austin Schuh6f3babe2020-01-26 20:34:50 -0800770
771 if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) &&
772 std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) {
773 // Pop the message reader pointer.
774 std::pop_heap(message_heap_.begin(), message_heap_.end(),
775 &SplitMessageReaderHeapCompare);
776 message_heap_.pop_back();
777
778 // Pop the next oldest message. This re-pushes any messages from the
779 // reader.
780 std::tuple<monotonic_clock::time_point, uint32_t,
781 FlatbufferVector<MessageHeader>>
782 next_oldest_message = std::get<2>(next_oldest_message_reader)
783 ->PopOldest(channel_index_);
784
785 // And make sure the message matches in it's entirety.
786 CHECK(std::get<2>(oldest_message).span() ==
787 std::get<2>(next_oldest_message).span())
788 << ": Data at the same timestamp doesn't match.";
789 } else {
790 break;
791 }
792 }
793
794 return oldest_message;
795}
796
797std::tuple<monotonic_clock::time_point, uint32_t,
798 FlatbufferVector<MessageHeader>>
799TimestampMerger::PopTimestampHeap() {
800 // Pop the oldest message reader pointer off the heap.
801 CHECK_GT(timestamp_heap_.size(), 0u);
802
803 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
804 oldest_timestamp_reader = timestamp_heap_.front();
805
806 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
807 &SplitMessageReaderHeapCompare);
808 timestamp_heap_.pop_back();
809
810 CHECK(node_index_ != -1) << ": Timestamps in a single node environment";
811
812 // Pop the oldest message. This re-pushes any timestamps from the reader to
813 // the timestamp heap.
814 std::tuple<monotonic_clock::time_point, uint32_t,
815 FlatbufferVector<MessageHeader>>
816 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
817 ->PopOldest(channel_index_, node_index_);
818
819 // Confirm that the time we have recorded matches.
820 CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader));
821 CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader));
822
823 // TODO(austin): What if we get duplicate timestamps?
824
825 return oldest_timestamp;
826}
827
Austin Schuh8bd96322020-02-13 21:18:22 -0800828TimestampMerger::DeliveryTimestamp TimestampMerger::OldestTimestamp() const {
829 if (!has_timestamps_ || timestamp_heap_.size() == 0u) {
830 return TimestampMerger::DeliveryTimestamp{};
831 }
832
833 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
834 oldest_timestamp_reader = timestamp_heap_.front();
835
836 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
837 oldest_timestamp = std::get<2>(oldest_timestamp_reader)
838 ->oldest_message(channel_index_, node_index_);
839
840 TimestampMerger::DeliveryTimestamp timestamp;
841 timestamp.monotonic_event_time =
842 monotonic_clock::time_point(chrono::nanoseconds(
843 std::get<2>(oldest_timestamp)->monotonic_sent_time()));
844 timestamp.realtime_event_time = realtime_clock::time_point(
845 chrono::nanoseconds(std::get<2>(oldest_timestamp)->realtime_sent_time()));
846
847 timestamp.monotonic_remote_time =
848 monotonic_clock::time_point(chrono::nanoseconds(
849 std::get<2>(oldest_timestamp)->monotonic_remote_time()));
850 timestamp.realtime_remote_time =
851 realtime_clock::time_point(chrono::nanoseconds(
852 std::get<2>(oldest_timestamp)->realtime_remote_time()));
853
854 timestamp.remote_queue_index = std::get<2>(oldest_timestamp)->queue_index();
855 return timestamp;
856}
857
Austin Schuh6f3babe2020-01-26 20:34:50 -0800858std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>>
859TimestampMerger::PopOldest() {
860 if (has_timestamps_) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800861 // Read the timestamps.
Austin Schuh6f3babe2020-01-26 20:34:50 -0800862 std::tuple<monotonic_clock::time_point, uint32_t,
863 FlatbufferVector<MessageHeader>>
864 oldest_timestamp = PopTimestampHeap();
865
866 TimestampMerger::DeliveryTimestamp timestamp;
867 timestamp.monotonic_event_time =
868 monotonic_clock::time_point(chrono::nanoseconds(
869 std::get<2>(oldest_timestamp).message().monotonic_sent_time()));
870 timestamp.realtime_event_time =
871 realtime_clock::time_point(chrono::nanoseconds(
872 std::get<2>(oldest_timestamp).message().realtime_sent_time()));
873
874 // Consistency check.
875 CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp));
876 CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(),
877 std::get<1>(oldest_timestamp));
878
879 monotonic_clock::time_point remote_timestamp_monotonic_time(
880 chrono::nanoseconds(
881 std::get<2>(oldest_timestamp).message().monotonic_remote_time()));
882
Austin Schuh8bd96322020-02-13 21:18:22 -0800883 // See if we have any data. If not, pass the problem up the chain.
884 if (message_heap_.size() == 0u) {
885 VLOG(1) << "No data to match timestamp on "
886 << configuration::CleanedChannelToString(
887 configuration_->channels()->Get(channel_index_));
888 return std::make_tuple(timestamp,
889 std::move(std::get<2>(oldest_timestamp)));
890 }
891
Austin Schuh6f3babe2020-01-26 20:34:50 -0800892 while (true) {
Austin Schuhcde938c2020-02-02 17:30:07 -0800893 {
894 // Ok, now try grabbing data until we find one which matches.
895 std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *>
896 oldest_message_ref = oldest_message();
897
898 // Time at which the message was sent (this message is written from the
899 // sending node's perspective.
900 monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds(
901 std::get<2>(oldest_message_ref)->monotonic_sent_time()));
902
903 if (remote_monotonic_time < remote_timestamp_monotonic_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800904 VLOG(1) << "Undelivered message, skipping. Remote time is "
905 << remote_monotonic_time << " timestamp is "
906 << remote_timestamp_monotonic_time << " on channel "
907 << channel_index_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800908 PopMessageHeap();
909 continue;
910 } else if (remote_monotonic_time > remote_timestamp_monotonic_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800911 VLOG(1) << "Data not found. Remote time should be "
912 << remote_timestamp_monotonic_time << " on channel "
913 << channel_index_;
Austin Schuhcde938c2020-02-02 17:30:07 -0800914 return std::make_tuple(timestamp,
915 std::move(std::get<2>(oldest_timestamp)));
916 }
917
918 timestamp.monotonic_remote_time = remote_monotonic_time;
919 }
920
Austin Schuh6f3babe2020-01-26 20:34:50 -0800921 std::tuple<monotonic_clock::time_point, uint32_t,
922 FlatbufferVector<MessageHeader>>
923 oldest_message = PopMessageHeap();
924
Austin Schuh6f3babe2020-01-26 20:34:50 -0800925 timestamp.realtime_remote_time =
926 realtime_clock::time_point(chrono::nanoseconds(
927 std::get<2>(oldest_message).message().realtime_sent_time()));
928 timestamp.remote_queue_index =
929 std::get<2>(oldest_message).message().queue_index();
930
Austin Schuhcde938c2020-02-02 17:30:07 -0800931 CHECK_EQ(timestamp.monotonic_remote_time,
932 remote_timestamp_monotonic_time);
933
934 CHECK_EQ(timestamp.remote_queue_index,
935 std::get<2>(oldest_timestamp).message().remote_queue_index())
936 << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message())
937 << " data "
938 << FlatbufferToJson(&std::get<2>(oldest_message).message());
Austin Schuh6f3babe2020-01-26 20:34:50 -0800939
940 return std::make_tuple(timestamp, std::get<2>(oldest_message));
941 }
942 } else {
943 std::tuple<monotonic_clock::time_point, uint32_t,
944 FlatbufferVector<MessageHeader>>
945 oldest_message = PopMessageHeap();
946
947 TimestampMerger::DeliveryTimestamp timestamp;
948 timestamp.monotonic_event_time =
949 monotonic_clock::time_point(chrono::nanoseconds(
950 std::get<2>(oldest_message).message().monotonic_sent_time()));
951 timestamp.realtime_event_time =
952 realtime_clock::time_point(chrono::nanoseconds(
953 std::get<2>(oldest_message).message().realtime_sent_time()));
954 timestamp.remote_queue_index = 0xffffffff;
955
956 CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time);
957 CHECK_EQ(std::get<1>(oldest_message),
958 std::get<2>(oldest_message).message().queue_index());
959
960 return std::make_tuple(timestamp, std::get<2>(oldest_message));
961 }
962}
963
Austin Schuh8bd96322020-02-13 21:18:22 -0800964void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); }
965
Austin Schuh6f3babe2020-01-26 20:34:50 -0800966namespace {
967std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders(
968 const std::vector<std::vector<std::string>> &filenames) {
969 CHECK_GT(filenames.size(), 0u);
970 // Build up all the SplitMessageReaders.
971 std::vector<std::unique_ptr<SplitMessageReader>> result;
972 for (const std::vector<std::string> &filenames : filenames) {
973 result.emplace_back(std::make_unique<SplitMessageReader>(filenames));
974 }
975 return result;
976}
977} // namespace
978
979ChannelMerger::ChannelMerger(
980 const std::vector<std::vector<std::string>> &filenames)
981 : split_message_readers_(MakeSplitMessageReaders(filenames)),
982 log_file_header_(
983 CopyFlatBuffer(split_message_readers_[0]->log_file_header())) {
984 // Now, confirm that the configuration matches for each and pick a start time.
985 // Also return the list of possible nodes.
986 for (const std::unique_ptr<SplitMessageReader> &reader :
987 split_message_readers_) {
988 CHECK(CompareFlatBuffer(log_file_header_.message().configuration(),
989 reader->log_file_header()->configuration()))
990 << ": Replaying log files with different configurations isn't "
991 "supported";
992 }
993
994 nodes_ = configuration::GetNodes(configuration());
995}
996
997bool ChannelMerger::SetNode(const Node *target_node) {
998 std::vector<SplitMessageReader *> split_message_readers;
999 for (const std::unique_ptr<SplitMessageReader> &reader :
1000 split_message_readers_) {
1001 split_message_readers.emplace_back(reader.get());
1002 }
1003
1004 // Go find a log_file_header for this node.
1005 {
1006 bool found_node = false;
1007
1008 for (const std::unique_ptr<SplitMessageReader> &reader :
1009 split_message_readers_) {
1010 if (CompareFlatBuffer(reader->node(), target_node)) {
1011 if (!found_node) {
1012 found_node = true;
1013 log_file_header_ = CopyFlatBuffer(reader->log_file_header());
Austin Schuhcde938c2020-02-02 17:30:07 -08001014 VLOG(1) << "Found log file " << reader->filename() << " with node "
1015 << FlatbufferToJson(reader->node()) << " start_time "
1016 << monotonic_start_time();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001017 } else {
1018 // And then make sure all the other files have matching headers.
Austin Schuhcde938c2020-02-02 17:30:07 -08001019 CHECK(CompareFlatBuffer(log_file_header(), reader->log_file_header()))
1020 << ": " << FlatbufferToJson(log_file_header()) << " reader "
1021 << FlatbufferToJson(reader->log_file_header());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001022 }
1023 }
1024 }
1025
1026 if (!found_node) {
1027 LOG(WARNING) << "Failed to find log file for node "
1028 << FlatbufferToJson(target_node);
1029 return false;
1030 }
1031 }
1032
1033 // Build up all the timestamp mergers. This connects up all the
1034 // SplitMessageReaders.
1035 timestamp_mergers_.reserve(configuration()->channels()->size());
1036 for (size_t channel_index = 0;
1037 channel_index < configuration()->channels()->size(); ++channel_index) {
1038 timestamp_mergers_.emplace_back(
1039 configuration(), split_message_readers, channel_index,
1040 configuration::GetNode(configuration(), target_node), this);
1041 }
1042
1043 // And prime everything.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001044 for (std::unique_ptr<SplitMessageReader> &split_message_reader :
1045 split_message_readers_) {
Austin Schuhcde938c2020-02-02 17:30:07 -08001046 split_message_reader->QueueMessages(
1047 split_message_reader->monotonic_start_time());
Austin Schuh6f3babe2020-01-26 20:34:50 -08001048 }
1049
1050 node_ = configuration::GetNodeOrDie(configuration(), target_node);
1051 return true;
1052}
1053
1054monotonic_clock::time_point ChannelMerger::OldestMessage() const {
1055 if (channel_heap_.size() == 0u) {
1056 return monotonic_clock::max_time;
1057 }
1058 return channel_heap_.front().first;
1059}
1060
Austin Schuh8bd96322020-02-13 21:18:22 -08001061TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestamp() const {
1062 if (timestamp_heap_.size() == 0u) {
1063 return TimestampMerger::DeliveryTimestamp{};
1064 }
1065 return timestamp_mergers_[timestamp_heap_.front().second].OldestTimestamp();
1066}
1067
1068TimestampMerger::DeliveryTimestamp ChannelMerger::OldestTimestampForChannel(
1069 int channel) const {
Austin Schuh6aa77be2020-02-22 21:06:40 -08001070 // If we didn't find any data for this node, we won't have any mergers. Return
1071 // an invalid timestamp in that case.
1072 if (timestamp_mergers_.size() <= static_cast<size_t>(channel)) {
1073 TimestampMerger::DeliveryTimestamp result;
1074 return result;
1075 }
Austin Schuh8bd96322020-02-13 21:18:22 -08001076 return timestamp_mergers_[channel].OldestTimestamp();
1077}
1078
Austin Schuh6f3babe2020-01-26 20:34:50 -08001079void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp,
1080 int channel_index) {
1081 // Pop and recreate the heap if it has already been pushed. And since we are
1082 // pushing again, we don't need to clear pushed.
1083 if (timestamp_mergers_[channel_index].pushed()) {
1084 channel_heap_.erase(std::find_if(
1085 channel_heap_.begin(), channel_heap_.end(),
1086 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1087 return x.second == channel_index;
1088 }));
1089 std::make_heap(channel_heap_.begin(), channel_heap_.end(),
1090 ChannelHeapCompare);
Austin Schuh8bd96322020-02-13 21:18:22 -08001091
1092 if (timestamp_mergers_[channel_index].has_timestamps()) {
1093 timestamp_heap_.erase(std::find_if(
1094 timestamp_heap_.begin(), timestamp_heap_.end(),
1095 [channel_index](const std::pair<monotonic_clock::time_point, int> x) {
1096 return x.second == channel_index;
1097 }));
1098 std::make_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1099 ChannelHeapCompare);
1100 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001101 }
1102
Austin Schuh05b70472020-01-01 17:11:17 -08001103 channel_heap_.push_back(std::make_pair(timestamp, channel_index));
1104
1105 // The default sort puts the newest message first. Use a custom comparator to
1106 // put the oldest message first.
1107 std::push_heap(channel_heap_.begin(), channel_heap_.end(),
1108 ChannelHeapCompare);
Austin Schuh8bd96322020-02-13 21:18:22 -08001109
1110 if (timestamp_mergers_[channel_index].has_timestamps()) {
1111 timestamp_heap_.push_back(std::make_pair(timestamp, channel_index));
1112 std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1113 ChannelHeapCompare);
1114 }
Austin Schuh05b70472020-01-01 17:11:17 -08001115}
1116
Austin Schuh6f3babe2020-01-26 20:34:50 -08001117std::tuple<TimestampMerger::DeliveryTimestamp, int,
1118 FlatbufferVector<MessageHeader>>
1119ChannelMerger::PopOldest() {
Austin Schuh8bd96322020-02-13 21:18:22 -08001120 CHECK_GT(channel_heap_.size(), 0u);
Austin Schuh05b70472020-01-01 17:11:17 -08001121 std::pair<monotonic_clock::time_point, int> oldest_channel_data =
1122 channel_heap_.front();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001123 int channel_index = oldest_channel_data.second;
Austin Schuh05b70472020-01-01 17:11:17 -08001124 std::pop_heap(channel_heap_.begin(), channel_heap_.end(),
1125 &ChannelHeapCompare);
1126 channel_heap_.pop_back();
Austin Schuh8bd96322020-02-13 21:18:22 -08001127
Austin Schuh6f3babe2020-01-26 20:34:50 -08001128 timestamp_mergers_[channel_index].set_pushed(false);
Austin Schuh05b70472020-01-01 17:11:17 -08001129
Austin Schuh6f3babe2020-01-26 20:34:50 -08001130 TimestampMerger *merger = &timestamp_mergers_[channel_index];
Austin Schuh05b70472020-01-01 17:11:17 -08001131
Austin Schuh8bd96322020-02-13 21:18:22 -08001132 if (merger->has_timestamps()) {
1133 CHECK_GT(timestamp_heap_.size(), 0u);
1134 std::pair<monotonic_clock::time_point, int> oldest_timestamp_data =
1135 timestamp_heap_.front();
1136 CHECK(oldest_timestamp_data == oldest_channel_data)
1137 << ": Timestamp heap out of sync.";
1138 std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(),
1139 &ChannelHeapCompare);
1140 timestamp_heap_.pop_back();
1141 }
1142
Austin Schuhcde938c2020-02-02 17:30:07 -08001143 // Merger handles any queueing needed from here.
Austin Schuh6f3babe2020-01-26 20:34:50 -08001144 std::tuple<TimestampMerger::DeliveryTimestamp,
1145 FlatbufferVector<MessageHeader>>
1146 message = merger->PopOldest();
Austin Schuh05b70472020-01-01 17:11:17 -08001147
Austin Schuh6f3babe2020-01-26 20:34:50 -08001148 return std::make_tuple(std::get<0>(message), channel_index,
1149 std::move(std::get<1>(message)));
1150}
1151
Austin Schuhcde938c2020-02-02 17:30:07 -08001152std::string SplitMessageReader::MessageHeaderQueue::DebugString() const {
1153 std::stringstream ss;
1154 for (size_t i = 0; i < data_.size(); ++i) {
1155 if (timestamps) {
1156 ss << " msg: ";
1157 } else {
1158 ss << " timestamp: ";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001159 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001160 ss << monotonic_clock::time_point(std::chrono::nanoseconds(
1161 data_[i].message().monotonic_sent_time()))
1162 << " ("
1163 << realtime_clock::time_point(
1164 std::chrono::nanoseconds(data_[i].message().realtime_sent_time()))
1165 << ") " << data_[i].message().queue_index();
1166 if (timestamps) {
1167 ss << " <- remote "
1168 << monotonic_clock::time_point(std::chrono::nanoseconds(
1169 data_[i].message().monotonic_remote_time()))
1170 << " ("
1171 << realtime_clock::time_point(std::chrono::nanoseconds(
1172 data_[i].message().realtime_remote_time()))
1173 << ")";
Austin Schuh6f3babe2020-01-26 20:34:50 -08001174 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001175 ss << "\n";
1176 }
Austin Schuh6f3babe2020-01-26 20:34:50 -08001177
Austin Schuhcde938c2020-02-02 17:30:07 -08001178 return ss.str();
1179}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001180
Austin Schuhcde938c2020-02-02 17:30:07 -08001181std::string SplitMessageReader::DebugString(int channel) const {
1182 std::stringstream ss;
1183 ss << "[\n";
1184 ss << channels_[channel].data.DebugString();
1185 ss << " ]";
1186 return ss.str();
1187}
Austin Schuh6f3babe2020-01-26 20:34:50 -08001188
Austin Schuhcde938c2020-02-02 17:30:07 -08001189std::string SplitMessageReader::DebugString(int channel, int node_index) const {
1190 std::stringstream ss;
1191 ss << "[\n";
1192 ss << channels_[channel].timestamps[node_index].DebugString();
1193 ss << " ]";
1194 return ss.str();
1195}
1196
1197std::string TimestampMerger::DebugString() const {
1198 std::stringstream ss;
1199
1200 if (timestamp_heap_.size() > 0) {
1201 ss << " timestamp_heap {\n";
1202 std::vector<
1203 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1204 timestamp_heap = timestamp_heap_;
1205 while (timestamp_heap.size() > 0u) {
1206 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1207 oldest_timestamp_reader = timestamp_heap.front();
1208
1209 ss << " " << std::get<2>(oldest_timestamp_reader) << " "
1210 << std::get<0>(oldest_timestamp_reader) << " queue_index ("
1211 << std::get<1>(oldest_timestamp_reader) << ") ttq "
1212 << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " "
1213 << std::get<2>(oldest_timestamp_reader)->filename() << " -> "
1214 << std::get<2>(oldest_timestamp_reader)
1215 ->DebugString(channel_index_, node_index_)
1216 << "\n";
1217
1218 std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(),
1219 &SplitMessageReaderHeapCompare);
1220 timestamp_heap.pop_back();
1221 }
1222 ss << " }\n";
1223 }
1224
1225 ss << " message_heap {\n";
1226 {
1227 std::vector<
1228 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>>
1229 message_heap = message_heap_;
1230 while (message_heap.size() > 0u) {
1231 std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>
1232 oldest_message_reader = message_heap.front();
1233
1234 ss << " " << std::get<2>(oldest_message_reader) << " "
1235 << std::get<0>(oldest_message_reader) << " queue_index ("
1236 << std::get<1>(oldest_message_reader) << ") ttq "
1237 << std::get<2>(oldest_message_reader)->time_to_queue() << " "
1238 << std::get<2>(oldest_message_reader)->filename() << " -> "
1239 << std::get<2>(oldest_message_reader)->DebugString(channel_index_)
1240 << "\n";
1241
1242 std::pop_heap(message_heap.begin(), message_heap.end(),
1243 &SplitMessageReaderHeapCompare);
1244 message_heap.pop_back();
Austin Schuh6f3babe2020-01-26 20:34:50 -08001245 }
Austin Schuh05b70472020-01-01 17:11:17 -08001246 }
Austin Schuhcde938c2020-02-02 17:30:07 -08001247 ss << " }";
1248
1249 return ss.str();
1250}
1251
1252std::string ChannelMerger::DebugString() const {
1253 std::stringstream ss;
1254 ss << "start_time " << realtime_start_time() << " " << monotonic_start_time()
1255 << "\n";
1256 ss << "channel_heap {\n";
1257 std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap =
1258 channel_heap_;
1259 while (channel_heap.size() > 0u) {
1260 std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front();
1261 ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") "
1262 << configuration::CleanedChannelToString(
1263 configuration()->channels()->Get(std::get<1>(channel)))
1264 << "\n";
1265
1266 ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n";
1267
1268 std::pop_heap(channel_heap.begin(), channel_heap.end(),
1269 &ChannelHeapCompare);
1270 channel_heap.pop_back();
1271 }
1272 ss << "}";
1273
1274 return ss.str();
Austin Schuh05b70472020-01-01 17:11:17 -08001275}
1276
Austin Schuha36c8902019-12-30 18:07:15 -08001277} // namespace logger
1278} // namespace aos