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