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