Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1 | #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 Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 11 | #include "absl/strings/escaping.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 12 | #include "aos/configuration.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 13 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 14 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 15 | #include "aos/util/file.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 16 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 17 | #include "gflags/gflags.h" |
| 18 | #include "glog/logging.h" |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 19 | |
| 20 | DEFINE_int32(flush_size, 1000000, |
| 21 | "Number of outstanding bytes to allow before flushing to disk."); |
| 22 | |
| 23 | namespace aos { |
| 24 | namespace logger { |
| 25 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 26 | namespace chrono = std::chrono; |
| 27 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 28 | DetachedBufferWriter::DetachedBufferWriter(std::string_view filename) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 29 | : 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 Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | DetachedBufferWriter::~DetachedBufferWriter() { |
| 38 | Flush(); |
| 39 | PLOG_IF(ERROR, close(fd_) == -1) << " Failed to close logfile"; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 40 | VLOG(1) << "Closed " << filename_; |
| 41 | } |
| 42 | |
| 43 | DetachedBufferWriter::DetachedBufferWriter( |
| 44 | DetachedBufferWriter &&other) { |
| 45 | *this = std::move(other); |
| 46 | } |
| 47 | |
| 48 | DetachedBufferWriter &DetachedBufferWriter::operator=( |
| 49 | DetachedBufferWriter &&other) { |
| 50 | Flush(); |
| 51 | std::swap(filename_, other.filename_); |
| 52 | std::swap(fd_, other.fd_); |
| 53 | std::swap(queued_size_, other.queued_size_); |
| 54 | std::swap(written_size_, other.written_size_); |
| 55 | std::swap(queue_, other.queue_); |
| 56 | std::swap(iovec_, other.iovec_); |
| 57 | return *this; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void DetachedBufferWriter::QueueSizedFlatbuffer( |
| 61 | flatbuffers::FlatBufferBuilder *fbb) { |
| 62 | QueueSizedFlatbuffer(fbb->Release()); |
| 63 | } |
| 64 | |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 65 | void DetachedBufferWriter::WriteSizedFlatbuffer( |
| 66 | absl::Span<const uint8_t> span) { |
| 67 | // Cheat aggressively... Write out the queued up data, and then write this |
| 68 | // data once without buffering. It is hard to make a DetachedBuffer out of |
| 69 | // this data, and we don't want to worry about lifetimes. |
| 70 | Flush(); |
| 71 | iovec_.clear(); |
| 72 | iovec_.reserve(1); |
| 73 | |
| 74 | struct iovec n; |
| 75 | n.iov_base = const_cast<uint8_t *>(span.data()); |
| 76 | n.iov_len = span.size(); |
| 77 | iovec_.emplace_back(n); |
| 78 | |
| 79 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
| 80 | |
| 81 | PCHECK(written == static_cast<ssize_t>(n.iov_len)) |
| 82 | << ": Wrote " << written << " expected " << n.iov_len; |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 83 | written_size_ += written; |
Austin Schuh | de031b7 | 2020-01-10 19:34:41 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 86 | void DetachedBufferWriter::QueueSizedFlatbuffer( |
| 87 | flatbuffers::DetachedBuffer &&buffer) { |
| 88 | queued_size_ += buffer.size(); |
| 89 | queue_.emplace_back(std::move(buffer)); |
| 90 | |
| 91 | // Flush if we are at the max number of iovs per writev, or have written |
| 92 | // enough data. Otherwise writev will fail with an invalid argument. |
| 93 | if (queued_size_ > static_cast<size_t>(FLAGS_flush_size) || |
| 94 | queue_.size() == IOV_MAX) { |
| 95 | Flush(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void DetachedBufferWriter::Flush() { |
| 100 | if (queue_.size() == 0u) { |
| 101 | return; |
| 102 | } |
| 103 | iovec_.clear(); |
| 104 | iovec_.reserve(queue_.size()); |
| 105 | size_t counted_size = 0; |
| 106 | for (size_t i = 0; i < queue_.size(); ++i) { |
| 107 | struct iovec n; |
| 108 | n.iov_base = queue_[i].data(); |
| 109 | n.iov_len = queue_[i].size(); |
| 110 | counted_size += n.iov_len; |
| 111 | iovec_.emplace_back(std::move(n)); |
| 112 | } |
| 113 | CHECK_EQ(counted_size, queued_size_); |
| 114 | const ssize_t written = writev(fd_, iovec_.data(), iovec_.size()); |
| 115 | |
| 116 | PCHECK(written == static_cast<ssize_t>(queued_size_)) |
| 117 | << ": Wrote " << written << " expected " << queued_size_; |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 118 | written_size_ += written; |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 119 | |
| 120 | queued_size_ = 0; |
| 121 | queue_.clear(); |
| 122 | // TODO(austin): Handle partial writes in some way other than crashing... |
| 123 | } |
| 124 | |
| 125 | flatbuffers::Offset<MessageHeader> PackMessage( |
| 126 | flatbuffers::FlatBufferBuilder *fbb, const Context &context, |
| 127 | int channel_index, LogType log_type) { |
| 128 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset; |
| 129 | |
| 130 | switch (log_type) { |
| 131 | case LogType::kLogMessage: |
| 132 | case LogType::kLogMessageAndDeliveryTime: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 133 | case LogType::kLogRemoteMessage: |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 134 | data_offset = fbb->CreateVector( |
| 135 | static_cast<const uint8_t *>(context.data), context.size); |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 136 | break; |
| 137 | |
| 138 | case LogType::kLogDeliveryTimeOnly: |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | MessageHeader::Builder message_header_builder(*fbb); |
| 143 | message_header_builder.add_channel_index(channel_index); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 144 | |
| 145 | switch (log_type) { |
| 146 | case LogType::kLogRemoteMessage: |
| 147 | message_header_builder.add_queue_index(context.remote_queue_index); |
| 148 | message_header_builder.add_monotonic_sent_time( |
| 149 | context.monotonic_remote_time.time_since_epoch().count()); |
| 150 | message_header_builder.add_realtime_sent_time( |
| 151 | context.realtime_remote_time.time_since_epoch().count()); |
| 152 | break; |
| 153 | |
| 154 | case LogType::kLogMessage: |
| 155 | case LogType::kLogMessageAndDeliveryTime: |
| 156 | case LogType::kLogDeliveryTimeOnly: |
| 157 | message_header_builder.add_queue_index(context.queue_index); |
| 158 | message_header_builder.add_monotonic_sent_time( |
| 159 | context.monotonic_event_time.time_since_epoch().count()); |
| 160 | message_header_builder.add_realtime_sent_time( |
| 161 | context.realtime_event_time.time_since_epoch().count()); |
| 162 | break; |
| 163 | } |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 164 | |
| 165 | switch (log_type) { |
| 166 | case LogType::kLogMessage: |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 167 | case LogType::kLogRemoteMessage: |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 168 | message_header_builder.add_data(data_offset); |
| 169 | break; |
| 170 | |
| 171 | case LogType::kLogMessageAndDeliveryTime: |
| 172 | message_header_builder.add_data(data_offset); |
| 173 | [[fallthrough]]; |
| 174 | |
| 175 | case LogType::kLogDeliveryTimeOnly: |
| 176 | message_header_builder.add_monotonic_remote_time( |
| 177 | context.monotonic_remote_time.time_since_epoch().count()); |
| 178 | message_header_builder.add_realtime_remote_time( |
| 179 | context.realtime_remote_time.time_since_epoch().count()); |
| 180 | message_header_builder.add_remote_queue_index(context.remote_queue_index); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | return message_header_builder.Finish(); |
| 185 | } |
| 186 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 187 | SpanReader::SpanReader(std::string_view filename) |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 188 | : filename_(filename), |
| 189 | fd_(open(std::string(filename).c_str(), O_RDONLY | O_CLOEXEC)) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 190 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 191 | } |
| 192 | |
| 193 | absl::Span<const uint8_t> SpanReader::ReadMessage() { |
| 194 | // Make sure we have enough for the size. |
| 195 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 196 | if (!ReadBlock()) { |
| 197 | return absl::Span<const uint8_t>(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Now make sure we have enough for the message. |
| 202 | const size_t data_size = |
| 203 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 204 | sizeof(flatbuffers::uoffset_t); |
Austin Schuh | e4fca83 | 2020-03-07 16:58:53 -0800 | [diff] [blame] | 205 | if (data_size == sizeof(flatbuffers::uoffset_t)) { |
| 206 | LOG(ERROR) << "Size of data is zero. Log file end is corrupted, skipping."; |
| 207 | LOG(ERROR) << " Rest of log file is " |
| 208 | << absl::BytesToHexString(std::string_view( |
| 209 | reinterpret_cast<const char *>(data_.data() + |
| 210 | consumed_data_), |
| 211 | data_.size() - consumed_data_)); |
| 212 | return absl::Span<const uint8_t>(); |
| 213 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 214 | while (data_.size() < consumed_data_ + data_size) { |
| 215 | if (!ReadBlock()) { |
| 216 | return absl::Span<const uint8_t>(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // And return it, consuming the data. |
| 221 | const uint8_t *data_ptr = data_.data() + consumed_data_; |
| 222 | |
| 223 | consumed_data_ += data_size; |
| 224 | |
| 225 | return absl::Span<const uint8_t>(data_ptr, data_size); |
| 226 | } |
| 227 | |
| 228 | bool SpanReader::MessageAvailable() { |
| 229 | // Are we big enough to read the size? |
| 230 | if (data_.size() - consumed_data_ < sizeof(flatbuffers::uoffset_t)) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // Then, are we big enough to read the full message? |
| 235 | const size_t data_size = |
| 236 | flatbuffers::GetPrefixedSize(data_.data() + consumed_data_) + |
| 237 | sizeof(flatbuffers::uoffset_t); |
| 238 | if (data_.size() < consumed_data_ + data_size) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | bool SpanReader::ReadBlock() { |
| 246 | if (end_of_file_) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // Appends 256k. This is enough that the read call is efficient. We don't |
| 251 | // want to spend too much time reading small chunks because the syscalls for |
| 252 | // that will be expensive. |
| 253 | constexpr size_t kReadSize = 256 * 1024; |
| 254 | |
| 255 | // Strip off any unused data at the front. |
| 256 | if (consumed_data_ != 0) { |
| 257 | data_.erase(data_.begin(), data_.begin() + consumed_data_); |
| 258 | consumed_data_ = 0; |
| 259 | } |
| 260 | |
| 261 | const size_t starting_size = data_.size(); |
| 262 | |
| 263 | // This should automatically grow the backing store. It won't shrink if we |
| 264 | // get a small chunk later. This reduces allocations when we want to append |
| 265 | // more data. |
| 266 | data_.resize(data_.size() + kReadSize); |
| 267 | |
| 268 | ssize_t count = read(fd_, &data_[starting_size], kReadSize); |
| 269 | data_.resize(starting_size + std::max(count, static_cast<ssize_t>(0))); |
| 270 | if (count == 0) { |
| 271 | end_of_file_ = true; |
| 272 | return false; |
| 273 | } |
| 274 | PCHECK(count > 0); |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 279 | FlatbufferVector<LogFileHeader> ReadHeader(std::string_view filename) { |
| 280 | SpanReader span_reader(filename); |
| 281 | // Make sure we have enough to read the size. |
| 282 | absl::Span<const uint8_t> config_data = span_reader.ReadMessage(); |
| 283 | |
| 284 | // Make sure something was read. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 285 | CHECK(config_data != absl::Span<const uint8_t>()) |
| 286 | << ": Failed to read header from: " << filename; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 287 | |
| 288 | // And copy the config so we have it forever. |
| 289 | std::vector<uint8_t> data( |
| 290 | config_data.begin() + sizeof(flatbuffers::uoffset_t), config_data.end()); |
| 291 | return FlatbufferVector<LogFileHeader>(std::move(data)); |
| 292 | } |
| 293 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 294 | MessageReader::MessageReader(std::string_view filename) |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 295 | : span_reader_(filename), |
| 296 | raw_log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 297 | // Make sure we have enough to read the size. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 298 | absl::Span<const uint8_t> header_data = span_reader_.ReadMessage(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 299 | |
| 300 | // Make sure something was read. |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 301 | CHECK(header_data != absl::Span<const uint8_t>()) |
| 302 | << ": Failed to read header from: " << filename; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 303 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 304 | // And copy the header data so we have it forever. |
| 305 | std::vector<uint8_t> header_data_copy( |
| 306 | header_data.begin() + sizeof(flatbuffers::uoffset_t), header_data.end()); |
| 307 | raw_log_file_header_ = |
| 308 | FlatbufferVector<LogFileHeader>(std::move(header_data_copy)); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 309 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 310 | max_out_of_order_duration_ = |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 311 | chrono::nanoseconds(log_file_header()->max_out_of_order_duration()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 312 | |
| 313 | VLOG(1) << "Opened " << filename << " as node " |
| 314 | << FlatbufferToJson(log_file_header()->node()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | std::optional<FlatbufferVector<MessageHeader>> MessageReader::ReadMessage() { |
| 318 | absl::Span<const uint8_t> msg_data = span_reader_.ReadMessage(); |
| 319 | if (msg_data == absl::Span<const uint8_t>()) { |
| 320 | return std::nullopt; |
| 321 | } |
| 322 | |
| 323 | FlatbufferVector<MessageHeader> result{std::vector<uint8_t>( |
| 324 | msg_data.begin() + sizeof(flatbuffers::uoffset_t), msg_data.end())}; |
| 325 | |
| 326 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 327 | chrono::nanoseconds(result.message().monotonic_sent_time())); |
| 328 | |
| 329 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 330 | VLOG(2) << "Read from " << filename() << " data " << FlatbufferToJson(result); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 331 | return std::move(result); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 334 | SplitMessageReader::SplitMessageReader( |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 335 | const std::vector<std::string> &filenames) |
| 336 | : filenames_(filenames), |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 337 | log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 338 | CHECK(NextLogFile()) << ": filenames is empty. Need files to read."; |
| 339 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 340 | // Grab any log file header. They should all match (and we will check as we |
| 341 | // open more of them). |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 342 | log_file_header_ = message_reader_->raw_log_file_header(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 343 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 344 | for (size_t i = 1; i < filenames_.size(); ++i) { |
| 345 | MessageReader message_reader(filenames_[i]); |
| 346 | |
| 347 | const monotonic_clock::time_point new_monotonic_start_time( |
| 348 | chrono::nanoseconds( |
| 349 | message_reader.log_file_header()->monotonic_start_time())); |
| 350 | const realtime_clock::time_point new_realtime_start_time( |
| 351 | chrono::nanoseconds( |
| 352 | message_reader.log_file_header()->realtime_start_time())); |
| 353 | |
| 354 | // There are 2 types of part files. Part files from before time estimation |
| 355 | // has started, and part files after. We don't declare a log file "started" |
| 356 | // until time estimation is up. And once a log file starts, it should never |
| 357 | // stop again, and should remain constant. |
| 358 | // To compare both types of headers, we mutate our saved copy of the header |
| 359 | // to match the next chunk by updating time if we detect a stopped -> |
| 360 | // started transition. |
| 361 | if (monotonic_start_time() == monotonic_clock::min_time) { |
| 362 | CHECK_EQ(realtime_start_time(), realtime_clock::min_time); |
| 363 | // We should only be missing the monotonic start time when logging data |
| 364 | // for remote nodes. We don't have a good way to deteremine the remote |
| 365 | // realtime offset, so it shouldn't be filled out. |
| 366 | // TODO(austin): If we have a good way, feel free to fill it out. It |
| 367 | // probably won't be better than we could do in post though with the same |
| 368 | // data. |
| 369 | CHECK(!log_file_header_.mutable_message()->has_realtime_start_time()); |
| 370 | if (new_monotonic_start_time != monotonic_clock::min_time) { |
| 371 | // If we finally found our start time, update the header. Do this once |
| 372 | // because it should never change again. |
| 373 | log_file_header_.mutable_message()->mutate_monotonic_start_time( |
| 374 | new_monotonic_start_time.time_since_epoch().count()); |
| 375 | log_file_header_.mutable_message()->mutate_realtime_start_time( |
| 376 | new_realtime_start_time.time_since_epoch().count()); |
| 377 | } |
| 378 | } |
| 379 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame^] | 380 | // We don't have a good way to set the realtime start time on remote nodes. |
| 381 | // Confirm it remains consistent. |
| 382 | CHECK_EQ(log_file_header_.mutable_message()->has_realtime_start_time(), |
| 383 | message_reader.log_file_header()->has_realtime_start_time()); |
| 384 | |
| 385 | // Parts index will *not* match unless we set them to match. We only want |
| 386 | // to accept the start time and parts mismatching, so set them. |
| 387 | log_file_header_.mutable_message()->mutate_parts_index( |
| 388 | message_reader.log_file_header()->parts_index()); |
| 389 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 390 | // Now compare that the headers match. |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame^] | 391 | if (!CompareFlatBuffer(message_reader.raw_log_file_header(), |
| 392 | log_file_header_)) { |
| 393 | if (message_reader.log_file_header()->has_logger_uuid() && |
| 394 | log_file_header_.message().has_logger_uuid() && |
| 395 | message_reader.log_file_header()->logger_uuid()->string_view() != |
| 396 | log_file_header_.message().logger_uuid()->string_view()) { |
| 397 | LOG(FATAL) << "Logger UUIDs don't match between log file chunks " |
| 398 | << filenames_[0] << " and " << filenames_[i] |
| 399 | << ", this is not supported."; |
| 400 | } |
| 401 | if (message_reader.log_file_header()->has_parts_uuid() && |
| 402 | log_file_header_.message().has_parts_uuid() && |
| 403 | message_reader.log_file_header()->parts_uuid()->string_view() != |
| 404 | log_file_header_.message().parts_uuid()->string_view()) { |
| 405 | LOG(FATAL) << "Parts UUIDs don't match between log file chunks " |
| 406 | << filenames_[0] << " and " << filenames_[i] |
| 407 | << ", this is not supported."; |
| 408 | } |
| 409 | |
| 410 | LOG(FATAL) << "Header is different between log file chunks " |
| 411 | << filenames_[0] << " and " << filenames_[i] |
| 412 | << ", this is not supported."; |
| 413 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 414 | } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame^] | 415 | // Put the parts index back to the first log file chunk. |
| 416 | log_file_header_.mutable_message()->mutate_parts_index( |
| 417 | message_reader_->log_file_header()->parts_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 418 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 419 | // Setup per channel state. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 420 | channels_.resize(configuration()->channels()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 421 | for (ChannelData &channel_data : channels_) { |
| 422 | channel_data.data.split_reader = this; |
| 423 | // Build up the timestamp list. |
| 424 | if (configuration::MultiNode(configuration())) { |
| 425 | channel_data.timestamps.resize(configuration()->nodes()->size()); |
| 426 | for (MessageHeaderQueue &queue : channel_data.timestamps) { |
| 427 | queue.timestamps = true; |
| 428 | queue.split_reader = this; |
| 429 | } |
| 430 | } |
| 431 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 432 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 433 | // Build up channels_to_write_ as an optimization to make it fast to figure |
| 434 | // out which datastructure to place any new data from a channel on. |
| 435 | for (const Channel *channel : *configuration()->channels()) { |
| 436 | // This is the main case. We will only see data on this node. |
| 437 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 438 | channels_to_write_.emplace_back( |
| 439 | &channels_[channels_to_write_.size()].data); |
| 440 | } else |
| 441 | // If we can't send, but can receive, we should be able to see |
| 442 | // timestamps here. |
| 443 | if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 444 | channels_to_write_.emplace_back( |
| 445 | &(channels_[channels_to_write_.size()] |
| 446 | .timestamps[configuration::GetNodeIndex(configuration(), |
| 447 | node())])); |
| 448 | } else { |
| 449 | channels_to_write_.emplace_back(nullptr); |
| 450 | } |
| 451 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 454 | bool SplitMessageReader::NextLogFile() { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 455 | if (next_filename_index_ == filenames_.size()) { |
| 456 | return false; |
| 457 | } |
| 458 | message_reader_ = |
| 459 | std::make_unique<MessageReader>(filenames_[next_filename_index_]); |
| 460 | |
| 461 | // We can't support the config diverging between two log file headers. See if |
| 462 | // they are the same. |
| 463 | if (next_filename_index_ != 0) { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame^] | 464 | // In order for the headers to identically compare, they need to have the |
| 465 | // same parts_index. Rewrite the saved header with the new parts_index, |
| 466 | // compare, and then restore. |
| 467 | const int32_t original_parts_index = |
| 468 | log_file_header_.message().parts_index(); |
| 469 | log_file_header_.mutable_message()->mutate_parts_index( |
| 470 | message_reader_->log_file_header()->parts_index()); |
| 471 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 472 | CHECK(CompareFlatBuffer(message_reader_->raw_log_file_header(), |
| 473 | log_file_header_)) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 474 | << ": Header is different between log file chunks " |
| 475 | << filenames_[next_filename_index_] << " and " |
| 476 | << filenames_[next_filename_index_ - 1] << ", this is not supported."; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame^] | 477 | |
| 478 | log_file_header_.mutable_message()->mutate_parts_index( |
| 479 | original_parts_index); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | ++next_filename_index_; |
| 483 | return true; |
| 484 | } |
| 485 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 486 | bool SplitMessageReader::QueueMessages( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 487 | monotonic_clock::time_point last_dequeued_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 488 | // TODO(austin): Once we are happy that everything works, read a 256kb chunk |
| 489 | // to reduce the need to re-heap down below. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 490 | |
| 491 | // Special case no more data. Otherwise we blow up on the CHECK statement |
| 492 | // confirming that we have enough data queued. |
| 493 | if (at_end_) { |
| 494 | return false; |
| 495 | } |
| 496 | |
| 497 | // If this isn't the first time around, confirm that we had enough data queued |
| 498 | // to follow the contract. |
| 499 | if (time_to_queue_ != monotonic_clock::min_time) { |
| 500 | CHECK_LE(last_dequeued_time, |
| 501 | newest_timestamp() - max_out_of_order_duration()) |
| 502 | << " node " << FlatbufferToJson(node()) << " on " << this; |
| 503 | |
| 504 | // Bail if there is enough data already queued. |
| 505 | if (last_dequeued_time < time_to_queue_) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 506 | VLOG(1) << MaybeNodeName(target_node_) << "All up to date on " << this |
| 507 | << ", dequeued " << last_dequeued_time << " queue time " |
| 508 | << time_to_queue_; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | } else { |
| 512 | // Startup takes a special dance. We want to queue up until the start time, |
| 513 | // but we then want to find the next message to read. The conservative |
| 514 | // answer is to immediately trigger a second requeue to get things moving. |
| 515 | time_to_queue_ = monotonic_start_time(); |
| 516 | QueueMessages(time_to_queue_); |
| 517 | } |
| 518 | |
| 519 | // If we are asked to queue, queue for at least max_out_of_order_duration past |
| 520 | // the last known time in the log file (ie the newest timestep read). As long |
| 521 | // as we requeue exactly when time_to_queue_ is dequeued and go no further, we |
| 522 | // are safe. And since we pop in order, that works. |
| 523 | // |
| 524 | // Special case the start of the log file. There should be at most 1 message |
| 525 | // from each channel at the start of the log file. So always force the start |
| 526 | // of the log file to just be read. |
| 527 | time_to_queue_ = std::max(time_to_queue_, newest_timestamp()); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 528 | VLOG(1) << MaybeNodeName(target_node_) << "Queueing, going until " |
| 529 | << time_to_queue_ << " " << filename(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 530 | |
| 531 | bool was_emplaced = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 532 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 533 | // Stop if we have enough. |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 534 | if (newest_timestamp() > time_to_queue_ + max_out_of_order_duration() && |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 535 | was_emplaced) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 536 | VLOG(1) << MaybeNodeName(target_node_) << "Done queueing on " << this |
| 537 | << ", queued to " << newest_timestamp() << " with requeue time " |
| 538 | << time_to_queue_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 539 | return true; |
| 540 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 541 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 542 | if (std::optional<FlatbufferVector<MessageHeader>> msg = |
| 543 | message_reader_->ReadMessage()) { |
| 544 | const MessageHeader &header = msg.value().message(); |
| 545 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 546 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 547 | chrono::nanoseconds(header.monotonic_sent_time())); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 548 | |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 549 | if (VLOG_IS_ON(2)) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 550 | LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this |
| 551 | << " " << filename() << " ttq: " << time_to_queue_ << " now " |
| 552 | << newest_timestamp() << " start time " |
| 553 | << monotonic_start_time() << " " << FlatbufferToJson(&header); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 554 | } else if (VLOG_IS_ON(1)) { |
| 555 | FlatbufferVector<MessageHeader> copy = msg.value(); |
| 556 | copy.mutable_message()->clear_data(); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 557 | LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " " |
| 558 | << filename() << " ttq: " << time_to_queue_ << " now " |
| 559 | << newest_timestamp() << " start time " |
| 560 | << monotonic_start_time() << " " << FlatbufferToJson(copy); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 561 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 562 | |
| 563 | const int channel_index = header.channel_index(); |
| 564 | was_emplaced = channels_to_write_[channel_index]->emplace_back( |
| 565 | std::move(msg.value())); |
| 566 | if (was_emplaced) { |
| 567 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
| 568 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 569 | } else { |
| 570 | if (!NextLogFile()) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 571 | VLOG(1) << MaybeNodeName(target_node_) << "No more files, last was " |
| 572 | << filenames_.back(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 573 | at_end_ = true; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 574 | for (MessageHeaderQueue *queue : channels_to_write_) { |
| 575 | if (queue == nullptr || queue->timestamp_merger == nullptr) { |
| 576 | continue; |
| 577 | } |
| 578 | queue->timestamp_merger->NoticeAtEnd(); |
| 579 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 580 | return false; |
| 581 | } |
| 582 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 583 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger, |
| 587 | int channel_index, |
| 588 | const Node *target_node) { |
| 589 | const Node *reinterpreted_target_node = |
| 590 | configuration::GetNodeOrDie(configuration(), target_node); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 591 | target_node_ = reinterpreted_target_node; |
| 592 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 593 | const Channel *const channel = |
| 594 | configuration()->channels()->Get(channel_index); |
| 595 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 596 | VLOG(1) << " Configuring merger " << this << " for channel " << channel_index |
| 597 | << " " |
| 598 | << configuration::CleanedChannelToString( |
| 599 | configuration()->channels()->Get(channel_index)); |
| 600 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 601 | MessageHeaderQueue *message_header_queue = nullptr; |
| 602 | |
| 603 | // Figure out if this log file is from our point of view, or the other node's |
| 604 | // point of view. |
| 605 | if (node() == reinterpreted_target_node) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 606 | VLOG(1) << " Replaying as logged node " << filename(); |
| 607 | |
| 608 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 609 | VLOG(1) << " Data on node"; |
| 610 | message_header_queue = &(channels_[channel_index].data); |
| 611 | } else if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 612 | VLOG(1) << " Timestamps on node"; |
| 613 | message_header_queue = |
| 614 | &(channels_[channel_index].timestamps[configuration::GetNodeIndex( |
| 615 | configuration(), node())]); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 616 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 617 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 618 | } |
| 619 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 620 | VLOG(1) << " Replaying as other node " << filename(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 621 | // We are replaying from another node's point of view. The only interesting |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 622 | // data is data that is sent from our node and received on theirs. |
| 623 | if (configuration::ChannelIsReadableOnNode(channel, |
| 624 | reinterpreted_target_node) && |
| 625 | configuration::ChannelIsSendableOnNode(channel, node())) { |
| 626 | VLOG(1) << " Readable on target node"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 627 | // Data from another node. |
| 628 | message_header_queue = &(channels_[channel_index].data); |
| 629 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 630 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 631 | // This is either not sendable on the other node, or is a timestamp and |
| 632 | // therefore not interesting. |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // If we found one, write it down. This will be nullptr when there is nothing |
| 637 | // relevant on this channel on this node for the target node. In that case, |
| 638 | // we want to drop the message instead of queueing it. |
| 639 | if (message_header_queue != nullptr) { |
| 640 | message_header_queue->timestamp_merger = timestamp_merger; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 645 | FlatbufferVector<MessageHeader>> |
| 646 | SplitMessageReader::PopOldest(int channel_index) { |
| 647 | CHECK_GT(channels_[channel_index].data.size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 648 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 649 | timestamp = channels_[channel_index].data.front_timestamp(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 650 | FlatbufferVector<MessageHeader> front = |
| 651 | std::move(channels_[channel_index].data.front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 652 | channels_[channel_index].data.PopFront(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 653 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 654 | VLOG(1) << MaybeNodeName(target_node_) << "Popped Data " << this << " " |
| 655 | << std::get<0>(timestamp) << " for " |
| 656 | << configuration::StrippedChannelToString( |
| 657 | configuration()->channels()->Get(channel_index)) |
| 658 | << " (" << channel_index << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 659 | |
| 660 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 661 | |
| 662 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 663 | std::move(front)); |
| 664 | } |
| 665 | |
| 666 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 667 | FlatbufferVector<MessageHeader>> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 668 | SplitMessageReader::PopOldestTimestamp(int channel, int node_index) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 669 | CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 670 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 671 | timestamp = channels_[channel].timestamps[node_index].front_timestamp(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 672 | FlatbufferVector<MessageHeader> front = |
| 673 | std::move(channels_[channel].timestamps[node_index].front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 674 | channels_[channel].timestamps[node_index].PopFront(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 675 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 676 | VLOG(1) << MaybeNodeName(target_node_) << "Popped timestamp " << this << " " |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 677 | << std::get<0>(timestamp) << " for " |
| 678 | << configuration::StrippedChannelToString( |
| 679 | configuration()->channels()->Get(channel)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 680 | << " on " |
| 681 | << configuration()->nodes()->Get(node_index)->name()->string_view() |
| 682 | << " (" << node_index << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 683 | |
| 684 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 685 | |
| 686 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 687 | std::move(front)); |
| 688 | } |
| 689 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 690 | bool SplitMessageReader::MessageHeaderQueue::emplace_back( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 691 | FlatbufferVector<MessageHeader> &&msg) { |
| 692 | CHECK(split_reader != nullptr); |
| 693 | |
| 694 | // If there is no timestamp merger for this queue, nobody is listening. Drop |
| 695 | // the message. This happens when a log file from another node is replayed, |
| 696 | // and the timestamp mergers down stream just don't care. |
| 697 | if (timestamp_merger == nullptr) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 698 | return false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | CHECK(timestamps != msg.message().has_data()) |
| 702 | << ": Got timestamps and data mixed up on a node. " |
| 703 | << FlatbufferToJson(msg); |
| 704 | |
| 705 | data_.emplace_back(std::move(msg)); |
| 706 | |
| 707 | if (data_.size() == 1u) { |
| 708 | // Yup, new data. Notify. |
| 709 | if (timestamps) { |
| 710 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 711 | } else { |
| 712 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 713 | } |
| 714 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 715 | |
| 716 | return true; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 719 | void SplitMessageReader::MessageHeaderQueue::PopFront() { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 720 | data_.pop_front(); |
| 721 | if (data_.size() != 0u) { |
| 722 | // Yup, new data. |
| 723 | if (timestamps) { |
| 724 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 725 | } else { |
| 726 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 727 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 728 | } else { |
| 729 | // Poke anyways to update the heap. |
| 730 | if (timestamps) { |
| 731 | timestamp_merger->UpdateTimestamp( |
| 732 | nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr)); |
| 733 | } else { |
| 734 | timestamp_merger->Update( |
| 735 | nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr)); |
| 736 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 737 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | namespace { |
| 741 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 742 | bool SplitMessageReaderHeapCompare( |
| 743 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 744 | SplitMessageReader *> |
| 745 | first, |
| 746 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 747 | SplitMessageReader *> |
| 748 | second) { |
| 749 | if (std::get<0>(first) > std::get<0>(second)) { |
| 750 | return true; |
| 751 | } else if (std::get<0>(first) == std::get<0>(second)) { |
| 752 | if (std::get<1>(first) > std::get<1>(second)) { |
| 753 | return true; |
| 754 | } else if (std::get<1>(first) == std::get<1>(second)) { |
| 755 | return std::get<2>(first) > std::get<2>(second); |
| 756 | } else { |
| 757 | return false; |
| 758 | } |
| 759 | } else { |
| 760 | return false; |
| 761 | } |
| 762 | } |
| 763 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 764 | bool ChannelHeapCompare( |
| 765 | const std::pair<monotonic_clock::time_point, int> first, |
| 766 | const std::pair<monotonic_clock::time_point, int> second) { |
| 767 | if (first.first > second.first) { |
| 768 | return true; |
| 769 | } else if (first.first == second.first) { |
| 770 | return first.second > second.second; |
| 771 | } else { |
| 772 | return false; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | } // namespace |
| 777 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 778 | TimestampMerger::TimestampMerger( |
| 779 | const Configuration *configuration, |
| 780 | std::vector<SplitMessageReader *> split_message_readers, int channel_index, |
| 781 | const Node *target_node, ChannelMerger *channel_merger) |
| 782 | : configuration_(configuration), |
| 783 | split_message_readers_(std::move(split_message_readers)), |
| 784 | channel_index_(channel_index), |
| 785 | node_index_(configuration::MultiNode(configuration) |
| 786 | ? configuration::GetNodeIndex(configuration, target_node) |
| 787 | : -1), |
| 788 | channel_merger_(channel_merger) { |
| 789 | // Tell the readers we care so they know who to notify. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 790 | VLOG(1) << "Configuring channel " << channel_index << " target node " |
| 791 | << FlatbufferToJson(target_node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 792 | for (SplitMessageReader *reader : split_message_readers_) { |
| 793 | reader->SetTimestampMerger(this, channel_index, target_node); |
| 794 | } |
| 795 | |
| 796 | // And then determine if we need to track timestamps. |
| 797 | const Channel *channel = configuration->channels()->Get(channel_index); |
| 798 | if (!configuration::ChannelIsSendableOnNode(channel, target_node) && |
| 799 | configuration::ChannelIsReadableOnNode(channel, target_node)) { |
| 800 | has_timestamps_ = true; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | void TimestampMerger::PushMessageHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 805 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 806 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 807 | SplitMessageReader *split_message_reader) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 808 | if (split_message_reader != nullptr) { |
| 809 | DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(), |
| 810 | [split_message_reader]( |
| 811 | const std::tuple<monotonic_clock::time_point, |
| 812 | uint32_t, SplitMessageReader *> |
| 813 | x) { |
| 814 | return std::get<2>(x) == split_message_reader; |
| 815 | }) == message_heap_.end()) |
| 816 | << ": Pushing message when it is already in the heap."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 817 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 818 | message_heap_.push_back(std::make_tuple( |
| 819 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 820 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 821 | std::push_heap(message_heap_.begin(), message_heap_.end(), |
| 822 | &SplitMessageReaderHeapCompare); |
| 823 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 824 | |
| 825 | // If we are just a data merger, don't wait for timestamps. |
| 826 | if (!has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 827 | if (!message_heap_.empty()) { |
| 828 | channel_merger_->Update(std::get<0>(message_heap_[0]), channel_index_); |
| 829 | pushed_ = true; |
| 830 | } else { |
| 831 | // Remove ourselves if we are empty. |
| 832 | channel_merger_->Update(monotonic_clock::min_time, channel_index_); |
| 833 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 837 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 838 | TimestampMerger::oldest_message() const { |
| 839 | CHECK_GT(message_heap_.size(), 0u); |
| 840 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 841 | oldest_message_reader = message_heap_.front(); |
| 842 | return std::get<2>(oldest_message_reader)->oldest_message(channel_index_); |
| 843 | } |
| 844 | |
| 845 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 846 | TimestampMerger::oldest_timestamp() const { |
| 847 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 848 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 849 | oldest_message_reader = timestamp_heap_.front(); |
| 850 | return std::get<2>(oldest_message_reader) |
| 851 | ->oldest_message(channel_index_, node_index_); |
| 852 | } |
| 853 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 854 | void TimestampMerger::PushTimestampHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 855 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 856 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 857 | SplitMessageReader *split_message_reader) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 858 | if (split_message_reader != nullptr) { |
| 859 | DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 860 | [split_message_reader]( |
| 861 | const std::tuple<monotonic_clock::time_point, |
| 862 | uint32_t, SplitMessageReader *> |
| 863 | x) { |
| 864 | return std::get<2>(x) == split_message_reader; |
| 865 | }) == timestamp_heap_.end()) |
| 866 | << ": Pushing timestamp when it is already in the heap."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 867 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 868 | timestamp_heap_.push_back(std::make_tuple( |
| 869 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 870 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 871 | std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 872 | SplitMessageReaderHeapCompare); |
| 873 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 874 | |
| 875 | // If we are a timestamp merger, don't wait for data. Missing data will be |
| 876 | // caught at read time. |
| 877 | if (has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 878 | if (!timestamp_heap_.empty()) { |
| 879 | channel_merger_->Update(std::get<0>(timestamp_heap_[0]), channel_index_); |
| 880 | pushed_ = true; |
| 881 | } else { |
| 882 | // Remove ourselves if we are empty. |
| 883 | channel_merger_->Update(monotonic_clock::min_time, channel_index_); |
| 884 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
| 888 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 889 | FlatbufferVector<MessageHeader>> |
| 890 | TimestampMerger::PopMessageHeap() { |
| 891 | // Pop the oldest message reader pointer off the heap. |
| 892 | CHECK_GT(message_heap_.size(), 0u); |
| 893 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 894 | oldest_message_reader = message_heap_.front(); |
| 895 | |
| 896 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 897 | &SplitMessageReaderHeapCompare); |
| 898 | message_heap_.pop_back(); |
| 899 | |
| 900 | // Pop the oldest message. This re-pushes any messages from the reader to the |
| 901 | // message heap. |
| 902 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 903 | FlatbufferVector<MessageHeader>> |
| 904 | oldest_message = |
| 905 | std::get<2>(oldest_message_reader)->PopOldest(channel_index_); |
| 906 | |
| 907 | // Confirm that the time and queue_index we have recorded matches. |
| 908 | CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader)); |
| 909 | CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader)); |
| 910 | |
| 911 | // Now, keep reading until we have found all duplicates. |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 912 | while (!message_heap_.empty()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 913 | // See if it is a duplicate. |
| 914 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 915 | next_oldest_message_reader = message_heap_.front(); |
| 916 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 917 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 918 | next_oldest_message_time = std::get<2>(next_oldest_message_reader) |
| 919 | ->oldest_message(channel_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 920 | |
| 921 | if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) && |
| 922 | std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) { |
| 923 | // Pop the message reader pointer. |
| 924 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 925 | &SplitMessageReaderHeapCompare); |
| 926 | message_heap_.pop_back(); |
| 927 | |
| 928 | // Pop the next oldest message. This re-pushes any messages from the |
| 929 | // reader. |
| 930 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 931 | FlatbufferVector<MessageHeader>> |
| 932 | next_oldest_message = std::get<2>(next_oldest_message_reader) |
| 933 | ->PopOldest(channel_index_); |
| 934 | |
| 935 | // And make sure the message matches in it's entirety. |
| 936 | CHECK(std::get<2>(oldest_message).span() == |
| 937 | std::get<2>(next_oldest_message).span()) |
| 938 | << ": Data at the same timestamp doesn't match."; |
| 939 | } else { |
| 940 | break; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | return oldest_message; |
| 945 | } |
| 946 | |
| 947 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 948 | FlatbufferVector<MessageHeader>> |
| 949 | TimestampMerger::PopTimestampHeap() { |
| 950 | // Pop the oldest message reader pointer off the heap. |
| 951 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 952 | |
| 953 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 954 | oldest_timestamp_reader = timestamp_heap_.front(); |
| 955 | |
| 956 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 957 | &SplitMessageReaderHeapCompare); |
| 958 | timestamp_heap_.pop_back(); |
| 959 | |
| 960 | CHECK(node_index_ != -1) << ": Timestamps in a single node environment"; |
| 961 | |
| 962 | // Pop the oldest message. This re-pushes any timestamps from the reader to |
| 963 | // the timestamp heap. |
| 964 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 965 | FlatbufferVector<MessageHeader>> |
| 966 | oldest_timestamp = std::get<2>(oldest_timestamp_reader) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 967 | ->PopOldestTimestamp(channel_index_, node_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 968 | |
| 969 | // Confirm that the time we have recorded matches. |
| 970 | CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader)); |
| 971 | CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader)); |
| 972 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 973 | // Now, keep reading until we have found all duplicates. |
| 974 | while (!timestamp_heap_.empty()) { |
| 975 | // See if it is a duplicate. |
| 976 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 977 | next_oldest_timestamp_reader = timestamp_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 978 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 979 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 980 | next_oldest_timestamp_time = |
| 981 | std::get<2>(next_oldest_timestamp_reader) |
| 982 | ->oldest_message(channel_index_, node_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 983 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 984 | if (std::get<0>(next_oldest_timestamp_time) == |
| 985 | std::get<0>(oldest_timestamp) && |
| 986 | std::get<1>(next_oldest_timestamp_time) == |
| 987 | std::get<1>(oldest_timestamp)) { |
| 988 | // Pop the timestamp reader pointer. |
| 989 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 990 | &SplitMessageReaderHeapCompare); |
| 991 | timestamp_heap_.pop_back(); |
| 992 | |
| 993 | // Pop the next oldest timestamp. This re-pushes any messages from the |
| 994 | // reader. |
| 995 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 996 | FlatbufferVector<MessageHeader>> |
| 997 | next_oldest_timestamp = |
| 998 | std::get<2>(next_oldest_timestamp_reader) |
| 999 | ->PopOldestTimestamp(channel_index_, node_index_); |
| 1000 | |
| 1001 | // And make sure the contents matches in it's entirety. |
| 1002 | CHECK(std::get<2>(oldest_timestamp).span() == |
| 1003 | std::get<2>(next_oldest_timestamp).span()) |
| 1004 | << ": Data at the same timestamp doesn't match, " |
| 1005 | << aos::FlatbufferToJson(std::get<2>(oldest_timestamp)) << " vs " |
| 1006 | << aos::FlatbufferToJson(std::get<2>(next_oldest_timestamp)) << " " |
| 1007 | << absl::BytesToHexString(std::string_view( |
| 1008 | reinterpret_cast<const char *>( |
| 1009 | std::get<2>(oldest_timestamp).span().data()), |
| 1010 | std::get<2>(oldest_timestamp).span().size())) |
| 1011 | << " vs " |
| 1012 | << absl::BytesToHexString(std::string_view( |
| 1013 | reinterpret_cast<const char *>( |
| 1014 | std::get<2>(next_oldest_timestamp).span().data()), |
| 1015 | std::get<2>(next_oldest_timestamp).span().size())); |
| 1016 | |
| 1017 | } else { |
| 1018 | break; |
| 1019 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1022 | return oldest_timestamp; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1023 | } |
| 1024 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1025 | std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>> |
| 1026 | TimestampMerger::PopOldest() { |
| 1027 | if (has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1028 | VLOG(1) << "Looking for matching timestamp for " |
| 1029 | << configuration::StrippedChannelToString( |
| 1030 | configuration_->channels()->Get(channel_index_)) |
| 1031 | << " (" << channel_index_ << ") " |
| 1032 | << " at " << std::get<0>(oldest_timestamp()); |
| 1033 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1034 | // Read the timestamps. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1035 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1036 | FlatbufferVector<MessageHeader>> |
| 1037 | oldest_timestamp = PopTimestampHeap(); |
| 1038 | |
| 1039 | TimestampMerger::DeliveryTimestamp timestamp; |
| 1040 | timestamp.monotonic_event_time = |
| 1041 | monotonic_clock::time_point(chrono::nanoseconds( |
| 1042 | std::get<2>(oldest_timestamp).message().monotonic_sent_time())); |
| 1043 | timestamp.realtime_event_time = |
| 1044 | realtime_clock::time_point(chrono::nanoseconds( |
| 1045 | std::get<2>(oldest_timestamp).message().realtime_sent_time())); |
| 1046 | |
| 1047 | // Consistency check. |
| 1048 | CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp)); |
| 1049 | CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(), |
| 1050 | std::get<1>(oldest_timestamp)); |
| 1051 | |
| 1052 | monotonic_clock::time_point remote_timestamp_monotonic_time( |
| 1053 | chrono::nanoseconds( |
| 1054 | std::get<2>(oldest_timestamp).message().monotonic_remote_time())); |
| 1055 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1056 | // See if we have any data. If not, pass the problem up the chain. |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1057 | if (message_heap_.empty()) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1058 | LOG(WARNING) << MaybeNodeName(configuration_->nodes()->Get(node_index_)) |
| 1059 | << "No data to match timestamp on " |
| 1060 | << configuration::CleanedChannelToString( |
| 1061 | configuration_->channels()->Get(channel_index_)) |
| 1062 | << " (" << channel_index_ << ")"; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1063 | return std::make_tuple(timestamp, |
| 1064 | std::move(std::get<2>(oldest_timestamp))); |
| 1065 | } |
| 1066 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1067 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1068 | { |
| 1069 | // Ok, now try grabbing data until we find one which matches. |
| 1070 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1071 | oldest_message_ref = oldest_message(); |
| 1072 | |
| 1073 | // Time at which the message was sent (this message is written from the |
| 1074 | // sending node's perspective. |
| 1075 | monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds( |
| 1076 | std::get<2>(oldest_message_ref)->monotonic_sent_time())); |
| 1077 | |
| 1078 | if (remote_monotonic_time < remote_timestamp_monotonic_time) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1079 | LOG(WARNING) << configuration_->nodes() |
| 1080 | ->Get(node_index_) |
| 1081 | ->name() |
| 1082 | ->string_view() |
| 1083 | << " Undelivered message, skipping. Remote time is " |
| 1084 | << remote_monotonic_time << " timestamp is " |
| 1085 | << remote_timestamp_monotonic_time << " on channel " |
| 1086 | << configuration::StrippedChannelToString( |
| 1087 | configuration_->channels()->Get(channel_index_)) |
| 1088 | << " (" << channel_index_ << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1089 | PopMessageHeap(); |
| 1090 | continue; |
| 1091 | } else if (remote_monotonic_time > remote_timestamp_monotonic_time) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1092 | LOG(WARNING) << configuration_->nodes() |
| 1093 | ->Get(node_index_) |
| 1094 | ->name() |
| 1095 | ->string_view() |
| 1096 | << " Data not found. Remote time should be " |
| 1097 | << remote_timestamp_monotonic_time |
| 1098 | << ", message time is " << remote_monotonic_time |
| 1099 | << " on channel " |
| 1100 | << configuration::StrippedChannelToString( |
| 1101 | configuration_->channels()->Get(channel_index_)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1102 | << " (" << channel_index_ << ")" |
| 1103 | << (VLOG_IS_ON(1) ? DebugString() : ""); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1104 | return std::make_tuple(timestamp, |
| 1105 | std::move(std::get<2>(oldest_timestamp))); |
| 1106 | } |
| 1107 | |
| 1108 | timestamp.monotonic_remote_time = remote_monotonic_time; |
| 1109 | } |
| 1110 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1111 | VLOG(1) << "Found matching data " |
| 1112 | << configuration::StrippedChannelToString( |
| 1113 | configuration_->channels()->Get(channel_index_)) |
| 1114 | << " (" << channel_index_ << ")"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1115 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1116 | FlatbufferVector<MessageHeader>> |
| 1117 | oldest_message = PopMessageHeap(); |
| 1118 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1119 | timestamp.realtime_remote_time = |
| 1120 | realtime_clock::time_point(chrono::nanoseconds( |
| 1121 | std::get<2>(oldest_message).message().realtime_sent_time())); |
| 1122 | timestamp.remote_queue_index = |
| 1123 | std::get<2>(oldest_message).message().queue_index(); |
| 1124 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1125 | CHECK_EQ(timestamp.monotonic_remote_time, |
| 1126 | remote_timestamp_monotonic_time); |
| 1127 | |
| 1128 | CHECK_EQ(timestamp.remote_queue_index, |
| 1129 | std::get<2>(oldest_timestamp).message().remote_queue_index()) |
| 1130 | << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message()) |
| 1131 | << " data " |
| 1132 | << FlatbufferToJson(&std::get<2>(oldest_message).message()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1133 | |
Austin Schuh | 30dd5c5 | 2020-08-01 14:43:44 -0700 | [diff] [blame] | 1134 | return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } else { |
| 1137 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1138 | FlatbufferVector<MessageHeader>> |
| 1139 | oldest_message = PopMessageHeap(); |
| 1140 | |
| 1141 | TimestampMerger::DeliveryTimestamp timestamp; |
| 1142 | timestamp.monotonic_event_time = |
| 1143 | monotonic_clock::time_point(chrono::nanoseconds( |
| 1144 | std::get<2>(oldest_message).message().monotonic_sent_time())); |
| 1145 | timestamp.realtime_event_time = |
| 1146 | realtime_clock::time_point(chrono::nanoseconds( |
| 1147 | std::get<2>(oldest_message).message().realtime_sent_time())); |
| 1148 | timestamp.remote_queue_index = 0xffffffff; |
| 1149 | |
| 1150 | CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time); |
| 1151 | CHECK_EQ(std::get<1>(oldest_message), |
| 1152 | std::get<2>(oldest_message).message().queue_index()); |
| 1153 | |
Austin Schuh | 30dd5c5 | 2020-08-01 14:43:44 -0700 | [diff] [blame] | 1154 | return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1158 | void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); } |
| 1159 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1160 | namespace { |
| 1161 | std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders( |
| 1162 | const std::vector<std::vector<std::string>> &filenames) { |
| 1163 | CHECK_GT(filenames.size(), 0u); |
| 1164 | // Build up all the SplitMessageReaders. |
| 1165 | std::vector<std::unique_ptr<SplitMessageReader>> result; |
| 1166 | for (const std::vector<std::string> &filenames : filenames) { |
| 1167 | result.emplace_back(std::make_unique<SplitMessageReader>(filenames)); |
| 1168 | } |
| 1169 | return result; |
| 1170 | } |
| 1171 | } // namespace |
| 1172 | |
| 1173 | ChannelMerger::ChannelMerger( |
| 1174 | const std::vector<std::vector<std::string>> &filenames) |
| 1175 | : split_message_readers_(MakeSplitMessageReaders(filenames)), |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 1176 | log_file_header_(split_message_readers_[0]->raw_log_file_header()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1177 | // Now, confirm that the configuration matches for each and pick a start time. |
| 1178 | // Also return the list of possible nodes. |
| 1179 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1180 | split_message_readers_) { |
| 1181 | CHECK(CompareFlatBuffer(log_file_header_.message().configuration(), |
| 1182 | reader->log_file_header()->configuration())) |
| 1183 | << ": Replaying log files with different configurations isn't " |
| 1184 | "supported"; |
| 1185 | } |
| 1186 | |
| 1187 | nodes_ = configuration::GetNodes(configuration()); |
| 1188 | } |
| 1189 | |
| 1190 | bool ChannelMerger::SetNode(const Node *target_node) { |
| 1191 | std::vector<SplitMessageReader *> split_message_readers; |
| 1192 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1193 | split_message_readers_) { |
| 1194 | split_message_readers.emplace_back(reader.get()); |
| 1195 | } |
| 1196 | |
| 1197 | // Go find a log_file_header for this node. |
| 1198 | { |
| 1199 | bool found_node = false; |
| 1200 | |
| 1201 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1202 | split_message_readers_) { |
James Kuszmaul | fc273dc | 2020-05-09 17:56:19 -0700 | [diff] [blame] | 1203 | // In order to identify which logfile(s) map to the target node, do a |
| 1204 | // logical comparison of the nodes, by confirming that we are either in a |
| 1205 | // single-node setup (where the nodes will both be nullptr) or that the |
| 1206 | // node names match (but the other node fields--e.g., hostname lists--may |
| 1207 | // not). |
| 1208 | const bool both_null = |
| 1209 | reader->node() == nullptr && target_node == nullptr; |
| 1210 | const bool both_have_name = |
| 1211 | (reader->node() != nullptr) && (target_node != nullptr) && |
| 1212 | (reader->node()->has_name() && target_node->has_name()); |
| 1213 | const bool node_names_identical = |
| 1214 | both_have_name && |
| 1215 | (reader->node()->name()->string_view() == |
| 1216 | target_node->name()->string_view()); |
| 1217 | if (both_null || node_names_identical) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1218 | if (!found_node) { |
| 1219 | found_node = true; |
| 1220 | log_file_header_ = CopyFlatBuffer(reader->log_file_header()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1221 | VLOG(1) << "Found log file " << reader->filename() << " with node " |
| 1222 | << FlatbufferToJson(reader->node()) << " start_time " |
| 1223 | << monotonic_start_time(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1224 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1225 | // Find the earliest start time. That way, if we get a full log file |
| 1226 | // directly from the node, and a partial later, we start with the |
| 1227 | // full. Update our header to match that. |
| 1228 | const monotonic_clock::time_point new_monotonic_start_time( |
| 1229 | chrono::nanoseconds( |
| 1230 | reader->log_file_header()->monotonic_start_time())); |
| 1231 | const realtime_clock::time_point new_realtime_start_time( |
| 1232 | chrono::nanoseconds( |
| 1233 | reader->log_file_header()->realtime_start_time())); |
| 1234 | |
| 1235 | if (monotonic_start_time() == monotonic_clock::min_time || |
| 1236 | (new_monotonic_start_time != monotonic_clock::min_time && |
| 1237 | new_monotonic_start_time < monotonic_start_time())) { |
| 1238 | log_file_header_.mutable_message()->mutate_monotonic_start_time( |
| 1239 | new_monotonic_start_time.time_since_epoch().count()); |
| 1240 | log_file_header_.mutable_message()->mutate_realtime_start_time( |
| 1241 | new_realtime_start_time.time_since_epoch().count()); |
| 1242 | VLOG(1) << "Updated log file " << reader->filename() |
| 1243 | << " with node " << FlatbufferToJson(reader->node()) |
| 1244 | << " start_time " << new_monotonic_start_time; |
| 1245 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | if (!found_node) { |
| 1251 | LOG(WARNING) << "Failed to find log file for node " |
| 1252 | << FlatbufferToJson(target_node); |
| 1253 | return false; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | // Build up all the timestamp mergers. This connects up all the |
| 1258 | // SplitMessageReaders. |
| 1259 | timestamp_mergers_.reserve(configuration()->channels()->size()); |
| 1260 | for (size_t channel_index = 0; |
| 1261 | channel_index < configuration()->channels()->size(); ++channel_index) { |
| 1262 | timestamp_mergers_.emplace_back( |
| 1263 | configuration(), split_message_readers, channel_index, |
| 1264 | configuration::GetNode(configuration(), target_node), this); |
| 1265 | } |
| 1266 | |
| 1267 | // And prime everything. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1268 | for (std::unique_ptr<SplitMessageReader> &split_message_reader : |
| 1269 | split_message_readers_) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1270 | split_message_reader->QueueMessages( |
| 1271 | split_message_reader->monotonic_start_time()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | node_ = configuration::GetNodeOrDie(configuration(), target_node); |
| 1275 | return true; |
| 1276 | } |
| 1277 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1278 | monotonic_clock::time_point ChannelMerger::OldestMessageTime() const { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1279 | if (channel_heap_.empty()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1280 | return monotonic_clock::max_time; |
| 1281 | } |
| 1282 | return channel_heap_.front().first; |
| 1283 | } |
| 1284 | |
| 1285 | void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp, |
| 1286 | int channel_index) { |
| 1287 | // Pop and recreate the heap if it has already been pushed. And since we are |
| 1288 | // pushing again, we don't need to clear pushed. |
| 1289 | if (timestamp_mergers_[channel_index].pushed()) { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1290 | const auto channel_iterator = std::find_if( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1291 | channel_heap_.begin(), channel_heap_.end(), |
| 1292 | [channel_index](const std::pair<monotonic_clock::time_point, int> x) { |
| 1293 | return x.second == channel_index; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1294 | }); |
| 1295 | DCHECK(channel_iterator != channel_heap_.end()); |
| 1296 | if (std::get<0>(*channel_iterator) == timestamp) { |
| 1297 | // It's already in the heap, in the correct spot, so nothing |
| 1298 | // more for us to do here. |
| 1299 | return; |
| 1300 | } |
| 1301 | channel_heap_.erase(channel_iterator); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1302 | std::make_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1303 | ChannelHeapCompare); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1304 | |
| 1305 | if (timestamp_mergers_[channel_index].has_timestamps()) { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1306 | const auto timestamp_iterator = std::find_if( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1307 | timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1308 | [channel_index](const std::pair<monotonic_clock::time_point, int> x) { |
| 1309 | return x.second == channel_index; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1310 | }); |
| 1311 | DCHECK(timestamp_iterator != timestamp_heap_.end()); |
| 1312 | if (std::get<0>(*timestamp_iterator) == timestamp) { |
| 1313 | // It's already in the heap, in the correct spot, so nothing |
| 1314 | // more for us to do here. |
| 1315 | return; |
| 1316 | } |
| 1317 | timestamp_heap_.erase(timestamp_iterator); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1318 | std::make_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1319 | ChannelHeapCompare); |
| 1320 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1323 | if (timestamp == monotonic_clock::min_time) { |
| 1324 | timestamp_mergers_[channel_index].set_pushed(false); |
| 1325 | return; |
| 1326 | } |
| 1327 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1328 | channel_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 1329 | |
| 1330 | // The default sort puts the newest message first. Use a custom comparator to |
| 1331 | // put the oldest message first. |
| 1332 | std::push_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1333 | ChannelHeapCompare); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1334 | |
| 1335 | if (timestamp_mergers_[channel_index].has_timestamps()) { |
| 1336 | timestamp_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 1337 | std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1338 | ChannelHeapCompare); |
| 1339 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1342 | void ChannelMerger::VerifyHeaps() { |
| 1343 | { |
| 1344 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1345 | channel_heap_; |
| 1346 | std::make_heap(channel_heap.begin(), channel_heap.end(), |
| 1347 | &ChannelHeapCompare); |
| 1348 | |
| 1349 | for (size_t i = 0; i < channel_heap_.size(); ++i) { |
| 1350 | CHECK(channel_heap_[i] == channel_heap[i]) << ": Heaps diverged..."; |
| 1351 | CHECK_EQ(std::get<0>(channel_heap[i]), |
| 1352 | timestamp_mergers_[std::get<1>(channel_heap[i])] |
| 1353 | .channel_merger_time()); |
| 1354 | } |
| 1355 | } |
| 1356 | { |
| 1357 | std::vector<std::pair<monotonic_clock::time_point, int>> timestamp_heap = |
| 1358 | timestamp_heap_; |
| 1359 | std::make_heap(timestamp_heap.begin(), timestamp_heap.end(), |
| 1360 | &ChannelHeapCompare); |
| 1361 | |
| 1362 | for (size_t i = 0; i < timestamp_heap_.size(); ++i) { |
| 1363 | CHECK(timestamp_heap_[i] == timestamp_heap[i]) << ": Heaps diverged..."; |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1368 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
| 1369 | FlatbufferVector<MessageHeader>> |
| 1370 | ChannelMerger::PopOldest() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1371 | CHECK_GT(channel_heap_.size(), 0u); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1372 | std::pair<monotonic_clock::time_point, int> oldest_channel_data = |
| 1373 | channel_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1374 | int channel_index = oldest_channel_data.second; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1375 | std::pop_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1376 | &ChannelHeapCompare); |
| 1377 | channel_heap_.pop_back(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1378 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1379 | timestamp_mergers_[channel_index].set_pushed(false); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1380 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1381 | TimestampMerger *merger = ×tamp_mergers_[channel_index]; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1382 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1383 | if (merger->has_timestamps()) { |
| 1384 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 1385 | std::pair<monotonic_clock::time_point, int> oldest_timestamp_data = |
| 1386 | timestamp_heap_.front(); |
| 1387 | CHECK(oldest_timestamp_data == oldest_channel_data) |
| 1388 | << ": Timestamp heap out of sync."; |
| 1389 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1390 | &ChannelHeapCompare); |
| 1391 | timestamp_heap_.pop_back(); |
| 1392 | } |
| 1393 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1394 | // Merger handles any queueing needed from here. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1395 | std::tuple<TimestampMerger::DeliveryTimestamp, |
| 1396 | FlatbufferVector<MessageHeader>> |
| 1397 | message = merger->PopOldest(); |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1398 | DCHECK_EQ(std::get<0>(message).monotonic_event_time, |
| 1399 | oldest_channel_data.first) |
| 1400 | << ": channel_heap_ was corrupted for " << channel_index << ": " |
| 1401 | << DebugString(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1402 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1403 | CHECK_GE(std::get<0>(message).monotonic_event_time, last_popped_time_) |
| 1404 | << ": " << MaybeNodeName(log_file_header()->node()) |
| 1405 | << "Messages came off the queue out of order. " << DebugString(); |
| 1406 | last_popped_time_ = std::get<0>(message).monotonic_event_time; |
| 1407 | |
| 1408 | VLOG(1) << "Popped " << last_popped_time_ << " " |
| 1409 | << configuration::StrippedChannelToString( |
| 1410 | configuration()->channels()->Get(channel_index)) |
| 1411 | << " (" << channel_index << ")"; |
| 1412 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1413 | return std::make_tuple(std::get<0>(message), channel_index, |
| 1414 | std::move(std::get<1>(message))); |
| 1415 | } |
| 1416 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1417 | std::string SplitMessageReader::MessageHeaderQueue::DebugString() const { |
| 1418 | std::stringstream ss; |
| 1419 | for (size_t i = 0; i < data_.size(); ++i) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1420 | if (i < 5 || i + 5 > data_.size()) { |
| 1421 | if (timestamps) { |
| 1422 | ss << " msg: "; |
| 1423 | } else { |
| 1424 | ss << " timestamp: "; |
| 1425 | } |
| 1426 | ss << monotonic_clock::time_point( |
| 1427 | chrono::nanoseconds(data_[i].message().monotonic_sent_time())) |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1428 | << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1429 | << realtime_clock::time_point( |
| 1430 | chrono::nanoseconds(data_[i].message().realtime_sent_time())) |
| 1431 | << ") " << data_[i].message().queue_index(); |
| 1432 | if (timestamps) { |
| 1433 | ss << " <- remote " |
| 1434 | << monotonic_clock::time_point(chrono::nanoseconds( |
| 1435 | data_[i].message().monotonic_remote_time())) |
| 1436 | << " (" |
| 1437 | << realtime_clock::time_point(chrono::nanoseconds( |
| 1438 | data_[i].message().realtime_remote_time())) |
| 1439 | << ")"; |
| 1440 | } |
| 1441 | ss << "\n"; |
| 1442 | } else if (i == 5) { |
| 1443 | ss << " ...\n"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1444 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1445 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1446 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1447 | return ss.str(); |
| 1448 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1449 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1450 | std::string SplitMessageReader::DebugString(int channel) const { |
| 1451 | std::stringstream ss; |
| 1452 | ss << "[\n"; |
| 1453 | ss << channels_[channel].data.DebugString(); |
| 1454 | ss << " ]"; |
| 1455 | return ss.str(); |
| 1456 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1457 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1458 | std::string SplitMessageReader::DebugString(int channel, int node_index) const { |
| 1459 | std::stringstream ss; |
| 1460 | ss << "[\n"; |
| 1461 | ss << channels_[channel].timestamps[node_index].DebugString(); |
| 1462 | ss << " ]"; |
| 1463 | return ss.str(); |
| 1464 | } |
| 1465 | |
| 1466 | std::string TimestampMerger::DebugString() const { |
| 1467 | std::stringstream ss; |
| 1468 | |
| 1469 | if (timestamp_heap_.size() > 0) { |
| 1470 | ss << " timestamp_heap {\n"; |
| 1471 | std::vector< |
| 1472 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1473 | timestamp_heap = timestamp_heap_; |
| 1474 | while (timestamp_heap.size() > 0u) { |
| 1475 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1476 | oldest_timestamp_reader = timestamp_heap.front(); |
| 1477 | |
| 1478 | ss << " " << std::get<2>(oldest_timestamp_reader) << " " |
| 1479 | << std::get<0>(oldest_timestamp_reader) << " queue_index (" |
| 1480 | << std::get<1>(oldest_timestamp_reader) << ") ttq " |
| 1481 | << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " " |
| 1482 | << std::get<2>(oldest_timestamp_reader)->filename() << " -> " |
| 1483 | << std::get<2>(oldest_timestamp_reader) |
| 1484 | ->DebugString(channel_index_, node_index_) |
| 1485 | << "\n"; |
| 1486 | |
| 1487 | std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(), |
| 1488 | &SplitMessageReaderHeapCompare); |
| 1489 | timestamp_heap.pop_back(); |
| 1490 | } |
| 1491 | ss << " }\n"; |
| 1492 | } |
| 1493 | |
| 1494 | ss << " message_heap {\n"; |
| 1495 | { |
| 1496 | std::vector< |
| 1497 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1498 | message_heap = message_heap_; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1499 | while (!message_heap.empty()) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1500 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1501 | oldest_message_reader = message_heap.front(); |
| 1502 | |
| 1503 | ss << " " << std::get<2>(oldest_message_reader) << " " |
| 1504 | << std::get<0>(oldest_message_reader) << " queue_index (" |
| 1505 | << std::get<1>(oldest_message_reader) << ") ttq " |
| 1506 | << std::get<2>(oldest_message_reader)->time_to_queue() << " " |
| 1507 | << std::get<2>(oldest_message_reader)->filename() << " -> " |
| 1508 | << std::get<2>(oldest_message_reader)->DebugString(channel_index_) |
| 1509 | << "\n"; |
| 1510 | |
| 1511 | std::pop_heap(message_heap.begin(), message_heap.end(), |
| 1512 | &SplitMessageReaderHeapCompare); |
| 1513 | message_heap.pop_back(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1514 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1515 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1516 | ss << " }"; |
| 1517 | |
| 1518 | return ss.str(); |
| 1519 | } |
| 1520 | |
| 1521 | std::string ChannelMerger::DebugString() const { |
| 1522 | std::stringstream ss; |
| 1523 | ss << "start_time " << realtime_start_time() << " " << monotonic_start_time() |
| 1524 | << "\n"; |
| 1525 | ss << "channel_heap {\n"; |
| 1526 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1527 | channel_heap_; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1528 | while (!channel_heap.empty()) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1529 | std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front(); |
| 1530 | ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") " |
| 1531 | << configuration::CleanedChannelToString( |
| 1532 | configuration()->channels()->Get(std::get<1>(channel))) |
| 1533 | << "\n"; |
| 1534 | |
| 1535 | ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n"; |
| 1536 | |
| 1537 | std::pop_heap(channel_heap.begin(), channel_heap.end(), |
| 1538 | &ChannelHeapCompare); |
| 1539 | channel_heap.pop_back(); |
| 1540 | } |
| 1541 | ss << "}"; |
| 1542 | |
| 1543 | return ss.str(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1546 | std::string MaybeNodeName(const Node *node) { |
| 1547 | if (node != nullptr) { |
| 1548 | return node->name()->str() + " "; |
| 1549 | } |
| 1550 | return ""; |
| 1551 | } |
| 1552 | |
Austin Schuh | a36c890 | 2019-12-30 18:07:15 -0800 | [diff] [blame] | 1553 | } // namespace logger |
| 1554 | } // namespace aos |