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 | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 434 | PartsMessageReader::PartsMessageReader(LogParts log_parts) |
| 435 | : parts_(std::move(log_parts)), message_reader_(parts_.parts[0]) {} |
| 436 | |
| 437 | std::optional<FlatbufferVector<MessageHeader>> |
| 438 | PartsMessageReader::ReadMessage() { |
| 439 | while (!done_) { |
| 440 | std::optional<FlatbufferVector<MessageHeader>> message = |
| 441 | message_reader_.ReadMessage(); |
| 442 | if (message) { |
| 443 | newest_timestamp_ = message_reader_.newest_timestamp(); |
| 444 | return message; |
| 445 | } |
| 446 | NextLog(); |
| 447 | } |
| 448 | return std::nullopt; |
| 449 | } |
| 450 | |
| 451 | void PartsMessageReader::NextLog() { |
| 452 | if (next_part_index_ == parts_.parts.size()) { |
| 453 | done_ = true; |
| 454 | return; |
| 455 | } |
| 456 | message_reader_ = MessageReader(parts_.parts[next_part_index_]); |
| 457 | ++next_part_index_; |
| 458 | } |
| 459 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 460 | SplitMessageReader::SplitMessageReader( |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 461 | const std::vector<std::string> &filenames) |
| 462 | : filenames_(filenames), |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 463 | log_file_header_(FlatbufferVector<LogFileHeader>::Empty()) { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 464 | CHECK(NextLogFile()) << ": filenames is empty. Need files to read."; |
| 465 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 466 | // Grab any log file header. They should all match (and we will check as we |
| 467 | // open more of them). |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 468 | log_file_header_ = message_reader_->raw_log_file_header(); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 469 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 470 | for (size_t i = 1; i < filenames_.size(); ++i) { |
| 471 | MessageReader message_reader(filenames_[i]); |
| 472 | |
| 473 | const monotonic_clock::time_point new_monotonic_start_time( |
| 474 | chrono::nanoseconds( |
| 475 | message_reader.log_file_header()->monotonic_start_time())); |
| 476 | const realtime_clock::time_point new_realtime_start_time( |
| 477 | chrono::nanoseconds( |
| 478 | message_reader.log_file_header()->realtime_start_time())); |
| 479 | |
| 480 | // There are 2 types of part files. Part files from before time estimation |
| 481 | // has started, and part files after. We don't declare a log file "started" |
| 482 | // until time estimation is up. And once a log file starts, it should never |
| 483 | // stop again, and should remain constant. |
| 484 | // To compare both types of headers, we mutate our saved copy of the header |
| 485 | // to match the next chunk by updating time if we detect a stopped -> |
| 486 | // started transition. |
| 487 | if (monotonic_start_time() == monotonic_clock::min_time) { |
| 488 | CHECK_EQ(realtime_start_time(), realtime_clock::min_time); |
| 489 | // We should only be missing the monotonic start time when logging data |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 490 | // 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] | 491 | // realtime offset, so it shouldn't be filled out. |
| 492 | // TODO(austin): If we have a good way, feel free to fill it out. It |
| 493 | // probably won't be better than we could do in post though with the same |
| 494 | // data. |
| 495 | CHECK(!log_file_header_.mutable_message()->has_realtime_start_time()); |
| 496 | if (new_monotonic_start_time != monotonic_clock::min_time) { |
| 497 | // If we finally found our start time, update the header. Do this once |
| 498 | // because it should never change again. |
| 499 | log_file_header_.mutable_message()->mutate_monotonic_start_time( |
| 500 | new_monotonic_start_time.time_since_epoch().count()); |
| 501 | log_file_header_.mutable_message()->mutate_realtime_start_time( |
| 502 | new_realtime_start_time.time_since_epoch().count()); |
| 503 | } |
| 504 | } |
| 505 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 506 | // We don't have a good way to set the realtime start time on remote nodes. |
| 507 | // Confirm it remains consistent. |
| 508 | CHECK_EQ(log_file_header_.mutable_message()->has_realtime_start_time(), |
| 509 | message_reader.log_file_header()->has_realtime_start_time()); |
| 510 | |
| 511 | // Parts index will *not* match unless we set them to match. We only want |
| 512 | // to accept the start time and parts mismatching, so set them. |
| 513 | log_file_header_.mutable_message()->mutate_parts_index( |
| 514 | message_reader.log_file_header()->parts_index()); |
| 515 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 516 | // Now compare that the headers match. |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 517 | if (!CompareFlatBuffer(message_reader.raw_log_file_header(), |
| 518 | log_file_header_)) { |
Brian Silverman | ae7c033 | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 519 | if (message_reader.log_file_header()->has_log_event_uuid() && |
| 520 | log_file_header_.message().has_log_event_uuid() && |
| 521 | message_reader.log_file_header()->log_event_uuid()->string_view() != |
| 522 | log_file_header_.message().log_event_uuid()->string_view()) { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 523 | LOG(FATAL) << "Logger UUIDs don't match between log file chunks " |
| 524 | << filenames_[0] << " and " << filenames_[i] |
| 525 | << ", this is not supported."; |
| 526 | } |
| 527 | if (message_reader.log_file_header()->has_parts_uuid() && |
| 528 | log_file_header_.message().has_parts_uuid() && |
| 529 | message_reader.log_file_header()->parts_uuid()->string_view() != |
| 530 | log_file_header_.message().parts_uuid()->string_view()) { |
| 531 | LOG(FATAL) << "Parts UUIDs don't match between log file chunks " |
| 532 | << filenames_[0] << " and " << filenames_[i] |
| 533 | << ", this is not supported."; |
| 534 | } |
| 535 | |
| 536 | LOG(FATAL) << "Header is different between log file chunks " |
| 537 | << filenames_[0] << " and " << filenames_[i] |
| 538 | << ", this is not supported."; |
| 539 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 540 | } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 541 | // Put the parts index back to the first log file chunk. |
| 542 | log_file_header_.mutable_message()->mutate_parts_index( |
| 543 | message_reader_->log_file_header()->parts_index()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 544 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 545 | // Setup per channel state. |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 546 | channels_.resize(configuration()->channels()->size()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 547 | for (ChannelData &channel_data : channels_) { |
| 548 | channel_data.data.split_reader = this; |
| 549 | // Build up the timestamp list. |
| 550 | if (configuration::MultiNode(configuration())) { |
| 551 | channel_data.timestamps.resize(configuration()->nodes()->size()); |
| 552 | for (MessageHeaderQueue &queue : channel_data.timestamps) { |
| 553 | queue.timestamps = true; |
| 554 | queue.split_reader = this; |
| 555 | } |
| 556 | } |
| 557 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 558 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 559 | // Build up channels_to_write_ as an optimization to make it fast to figure |
| 560 | // out which datastructure to place any new data from a channel on. |
| 561 | for (const Channel *channel : *configuration()->channels()) { |
| 562 | // This is the main case. We will only see data on this node. |
| 563 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 564 | channels_to_write_.emplace_back( |
| 565 | &channels_[channels_to_write_.size()].data); |
| 566 | } else |
| 567 | // If we can't send, but can receive, we should be able to see |
| 568 | // timestamps here. |
| 569 | if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 570 | channels_to_write_.emplace_back( |
| 571 | &(channels_[channels_to_write_.size()] |
| 572 | .timestamps[configuration::GetNodeIndex(configuration(), |
| 573 | node())])); |
| 574 | } else { |
| 575 | channels_to_write_.emplace_back(nullptr); |
| 576 | } |
| 577 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 580 | bool SplitMessageReader::NextLogFile() { |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 581 | if (next_filename_index_ == filenames_.size()) { |
| 582 | return false; |
| 583 | } |
| 584 | message_reader_ = |
| 585 | std::make_unique<MessageReader>(filenames_[next_filename_index_]); |
| 586 | |
| 587 | // We can't support the config diverging between two log file headers. See if |
| 588 | // they are the same. |
| 589 | if (next_filename_index_ != 0) { |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 590 | // In order for the headers to identically compare, they need to have the |
| 591 | // same parts_index. Rewrite the saved header with the new parts_index, |
| 592 | // compare, and then restore. |
| 593 | const int32_t original_parts_index = |
| 594 | log_file_header_.message().parts_index(); |
| 595 | log_file_header_.mutable_message()->mutate_parts_index( |
| 596 | message_reader_->log_file_header()->parts_index()); |
| 597 | |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 598 | CHECK(CompareFlatBuffer(message_reader_->raw_log_file_header(), |
| 599 | log_file_header_)) |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 600 | << ": Header is different between log file chunks " |
| 601 | << filenames_[next_filename_index_] << " and " |
| 602 | << filenames_[next_filename_index_ - 1] << ", this is not supported."; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 603 | |
| 604 | log_file_header_.mutable_message()->mutate_parts_index( |
| 605 | original_parts_index); |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | ++next_filename_index_; |
| 609 | return true; |
| 610 | } |
| 611 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 612 | bool SplitMessageReader::QueueMessages( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 613 | monotonic_clock::time_point last_dequeued_time) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 614 | // TODO(austin): Once we are happy that everything works, read a 256kb chunk |
| 615 | // to reduce the need to re-heap down below. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 616 | |
| 617 | // Special case no more data. Otherwise we blow up on the CHECK statement |
| 618 | // confirming that we have enough data queued. |
| 619 | if (at_end_) { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | // If this isn't the first time around, confirm that we had enough data queued |
| 624 | // to follow the contract. |
| 625 | if (time_to_queue_ != monotonic_clock::min_time) { |
| 626 | CHECK_LE(last_dequeued_time, |
| 627 | newest_timestamp() - max_out_of_order_duration()) |
| 628 | << " node " << FlatbufferToJson(node()) << " on " << this; |
| 629 | |
| 630 | // Bail if there is enough data already queued. |
| 631 | if (last_dequeued_time < time_to_queue_) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 632 | VLOG(1) << MaybeNodeName(target_node_) << "All up to date on " << this |
| 633 | << ", dequeued " << last_dequeued_time << " queue time " |
| 634 | << time_to_queue_; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 635 | return true; |
| 636 | } |
| 637 | } else { |
| 638 | // Startup takes a special dance. We want to queue up until the start time, |
| 639 | // but we then want to find the next message to read. The conservative |
| 640 | // answer is to immediately trigger a second requeue to get things moving. |
| 641 | time_to_queue_ = monotonic_start_time(); |
Austin Schuh | eeba029 | 2020-10-11 16:20:05 -0700 | [diff] [blame] | 642 | CHECK_NE(time_to_queue_, monotonic_clock::min_time); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 643 | QueueMessages(time_to_queue_); |
| 644 | } |
| 645 | |
| 646 | // If we are asked to queue, queue for at least max_out_of_order_duration past |
| 647 | // the last known time in the log file (ie the newest timestep read). As long |
| 648 | // as we requeue exactly when time_to_queue_ is dequeued and go no further, we |
| 649 | // are safe. And since we pop in order, that works. |
| 650 | // |
| 651 | // Special case the start of the log file. There should be at most 1 message |
| 652 | // from each channel at the start of the log file. So always force the start |
| 653 | // of the log file to just be read. |
| 654 | time_to_queue_ = std::max(time_to_queue_, newest_timestamp()); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 655 | VLOG(1) << MaybeNodeName(target_node_) << "Queueing, going until " |
| 656 | << time_to_queue_ << " " << filename(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 657 | |
| 658 | bool was_emplaced = false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 659 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 660 | // Stop if we have enough. |
Brian Silverman | 98360e2 | 2020-04-28 16:51:20 -0700 | [diff] [blame] | 661 | if (newest_timestamp() > time_to_queue_ + max_out_of_order_duration() && |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 662 | was_emplaced) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 663 | VLOG(1) << MaybeNodeName(target_node_) << "Done queueing on " << this |
| 664 | << ", queued to " << newest_timestamp() << " with requeue time " |
| 665 | << time_to_queue_; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 666 | return true; |
| 667 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 668 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 669 | if (std::optional<FlatbufferVector<MessageHeader>> msg = |
| 670 | message_reader_->ReadMessage()) { |
| 671 | const MessageHeader &header = msg.value().message(); |
| 672 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 673 | const monotonic_clock::time_point timestamp = monotonic_clock::time_point( |
| 674 | chrono::nanoseconds(header.monotonic_sent_time())); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 675 | |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 676 | if (VLOG_IS_ON(2)) { |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 677 | LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " " |
| 678 | << filename() << " ttq: " << time_to_queue_ << " now " |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 679 | << newest_timestamp() << " start time " |
| 680 | << monotonic_start_time() << " " << FlatbufferToJson(&header); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 681 | } else if (VLOG_IS_ON(1)) { |
| 682 | FlatbufferVector<MessageHeader> copy = msg.value(); |
| 683 | copy.mutable_message()->clear_data(); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 684 | LOG(INFO) << MaybeNodeName(target_node_) << "Queued " << this << " " |
| 685 | << filename() << " ttq: " << time_to_queue_ << " now " |
| 686 | << newest_timestamp() << " start time " |
| 687 | << monotonic_start_time() << " " << FlatbufferToJson(copy); |
Austin Schuh | 0b5fd03 | 2020-03-28 17:36:49 -0700 | [diff] [blame] | 688 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 689 | |
| 690 | const int channel_index = header.channel_index(); |
| 691 | was_emplaced = channels_to_write_[channel_index]->emplace_back( |
| 692 | std::move(msg.value())); |
| 693 | if (was_emplaced) { |
| 694 | newest_timestamp_ = std::max(newest_timestamp_, timestamp); |
| 695 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 696 | } else { |
| 697 | if (!NextLogFile()) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 698 | VLOG(1) << MaybeNodeName(target_node_) << "No more files, last was " |
| 699 | << filenames_.back(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 700 | at_end_ = true; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 701 | for (MessageHeaderQueue *queue : channels_to_write_) { |
| 702 | if (queue == nullptr || queue->timestamp_merger == nullptr) { |
| 703 | continue; |
| 704 | } |
| 705 | queue->timestamp_merger->NoticeAtEnd(); |
| 706 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 710 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | void SplitMessageReader::SetTimestampMerger(TimestampMerger *timestamp_merger, |
| 714 | int channel_index, |
| 715 | const Node *target_node) { |
| 716 | const Node *reinterpreted_target_node = |
| 717 | configuration::GetNodeOrDie(configuration(), target_node); |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 718 | target_node_ = reinterpreted_target_node; |
| 719 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 720 | const Channel *const channel = |
| 721 | configuration()->channels()->Get(channel_index); |
| 722 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 723 | VLOG(1) << " Configuring merger " << this << " for channel " << channel_index |
| 724 | << " " |
| 725 | << configuration::CleanedChannelToString( |
| 726 | configuration()->channels()->Get(channel_index)); |
| 727 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 728 | MessageHeaderQueue *message_header_queue = nullptr; |
| 729 | |
| 730 | // Figure out if this log file is from our point of view, or the other node's |
| 731 | // point of view. |
| 732 | if (node() == reinterpreted_target_node) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 733 | VLOG(1) << " Replaying as logged node " << filename(); |
| 734 | |
| 735 | if (configuration::ChannelIsSendableOnNode(channel, node())) { |
| 736 | VLOG(1) << " Data on node"; |
| 737 | message_header_queue = &(channels_[channel_index].data); |
| 738 | } else if (configuration::ChannelIsReadableOnNode(channel, node())) { |
| 739 | VLOG(1) << " Timestamps on node"; |
| 740 | message_header_queue = |
| 741 | &(channels_[channel_index].timestamps[configuration::GetNodeIndex( |
| 742 | configuration(), node())]); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 743 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 744 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 745 | } |
| 746 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 747 | VLOG(1) << " Replaying as other node " << filename(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 748 | // 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] | 749 | // data is data that is sent from our node and received on theirs. |
| 750 | if (configuration::ChannelIsReadableOnNode(channel, |
| 751 | reinterpreted_target_node) && |
| 752 | configuration::ChannelIsSendableOnNode(channel, node())) { |
| 753 | VLOG(1) << " Readable on target node"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 754 | // Data from another node. |
| 755 | message_header_queue = &(channels_[channel_index].data); |
| 756 | } else { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 757 | VLOG(1) << " Dropping"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 758 | // This is either not sendable on the other node, or is a timestamp and |
| 759 | // therefore not interesting. |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // If we found one, write it down. This will be nullptr when there is nothing |
| 764 | // relevant on this channel on this node for the target node. In that case, |
| 765 | // we want to drop the message instead of queueing it. |
| 766 | if (message_header_queue != nullptr) { |
| 767 | message_header_queue->timestamp_merger = timestamp_merger; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 772 | FlatbufferVector<MessageHeader>> |
| 773 | SplitMessageReader::PopOldest(int channel_index) { |
| 774 | CHECK_GT(channels_[channel_index].data.size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 775 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 776 | timestamp = channels_[channel_index].data.front_timestamp(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 777 | FlatbufferVector<MessageHeader> front = |
| 778 | std::move(channels_[channel_index].data.front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 779 | channels_[channel_index].data.PopFront(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 780 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 781 | VLOG(1) << MaybeNodeName(target_node_) << "Popped Data " << this << " " |
| 782 | << std::get<0>(timestamp) << " for " |
| 783 | << configuration::StrippedChannelToString( |
| 784 | configuration()->channels()->Get(channel_index)) |
| 785 | << " (" << channel_index << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 786 | |
| 787 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 788 | |
| 789 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 790 | std::move(front)); |
| 791 | } |
| 792 | |
| 793 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 794 | FlatbufferVector<MessageHeader>> |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 795 | SplitMessageReader::PopOldestTimestamp(int channel, int node_index) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 796 | CHECK_GT(channels_[channel].timestamps[node_index].size(), 0u); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 797 | const std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 798 | timestamp = channels_[channel].timestamps[node_index].front_timestamp(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 799 | FlatbufferVector<MessageHeader> front = |
| 800 | std::move(channels_[channel].timestamps[node_index].front()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 801 | channels_[channel].timestamps[node_index].PopFront(); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 802 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 803 | VLOG(1) << MaybeNodeName(target_node_) << "Popped timestamp " << this << " " |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 804 | << std::get<0>(timestamp) << " for " |
| 805 | << configuration::StrippedChannelToString( |
| 806 | configuration()->channels()->Get(channel)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 807 | << " on " |
| 808 | << configuration()->nodes()->Get(node_index)->name()->string_view() |
| 809 | << " (" << node_index << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 810 | |
| 811 | QueueMessages(std::get<0>(timestamp)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 812 | |
| 813 | return std::make_tuple(std::get<0>(timestamp), std::get<1>(timestamp), |
| 814 | std::move(front)); |
| 815 | } |
| 816 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 817 | bool SplitMessageReader::MessageHeaderQueue::emplace_back( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 818 | FlatbufferVector<MessageHeader> &&msg) { |
| 819 | CHECK(split_reader != nullptr); |
| 820 | |
| 821 | // If there is no timestamp merger for this queue, nobody is listening. Drop |
| 822 | // the message. This happens when a log file from another node is replayed, |
| 823 | // and the timestamp mergers down stream just don't care. |
| 824 | if (timestamp_merger == nullptr) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 825 | return false; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | CHECK(timestamps != msg.message().has_data()) |
| 829 | << ": Got timestamps and data mixed up on a node. " |
| 830 | << FlatbufferToJson(msg); |
| 831 | |
| 832 | data_.emplace_back(std::move(msg)); |
| 833 | |
| 834 | if (data_.size() == 1u) { |
| 835 | // Yup, new data. Notify. |
| 836 | if (timestamps) { |
| 837 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 838 | } else { |
| 839 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 840 | } |
| 841 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 842 | |
| 843 | return true; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 846 | void SplitMessageReader::MessageHeaderQueue::PopFront() { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 847 | data_.pop_front(); |
| 848 | if (data_.size() != 0u) { |
| 849 | // Yup, new data. |
| 850 | if (timestamps) { |
| 851 | timestamp_merger->UpdateTimestamp(split_reader, front_timestamp()); |
| 852 | } else { |
| 853 | timestamp_merger->Update(split_reader, front_timestamp()); |
| 854 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 855 | } else { |
| 856 | // Poke anyways to update the heap. |
| 857 | if (timestamps) { |
| 858 | timestamp_merger->UpdateTimestamp( |
| 859 | nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr)); |
| 860 | } else { |
| 861 | timestamp_merger->Update( |
| 862 | nullptr, std::make_tuple(monotonic_clock::min_time, 0, nullptr)); |
| 863 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 864 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | namespace { |
| 868 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 869 | bool SplitMessageReaderHeapCompare( |
| 870 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 871 | SplitMessageReader *> |
| 872 | first, |
| 873 | const std::tuple<monotonic_clock::time_point, uint32_t, |
| 874 | SplitMessageReader *> |
| 875 | second) { |
| 876 | if (std::get<0>(first) > std::get<0>(second)) { |
| 877 | return true; |
| 878 | } else if (std::get<0>(first) == std::get<0>(second)) { |
| 879 | if (std::get<1>(first) > std::get<1>(second)) { |
| 880 | return true; |
| 881 | } else if (std::get<1>(first) == std::get<1>(second)) { |
| 882 | return std::get<2>(first) > std::get<2>(second); |
| 883 | } else { |
| 884 | return false; |
| 885 | } |
| 886 | } else { |
| 887 | return false; |
| 888 | } |
| 889 | } |
| 890 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 891 | bool ChannelHeapCompare( |
| 892 | const std::pair<monotonic_clock::time_point, int> first, |
| 893 | const std::pair<monotonic_clock::time_point, int> second) { |
| 894 | if (first.first > second.first) { |
| 895 | return true; |
| 896 | } else if (first.first == second.first) { |
| 897 | return first.second > second.second; |
| 898 | } else { |
| 899 | return false; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | } // namespace |
| 904 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 905 | TimestampMerger::TimestampMerger( |
| 906 | const Configuration *configuration, |
| 907 | std::vector<SplitMessageReader *> split_message_readers, int channel_index, |
| 908 | const Node *target_node, ChannelMerger *channel_merger) |
| 909 | : configuration_(configuration), |
| 910 | split_message_readers_(std::move(split_message_readers)), |
| 911 | channel_index_(channel_index), |
| 912 | node_index_(configuration::MultiNode(configuration) |
| 913 | ? configuration::GetNodeIndex(configuration, target_node) |
| 914 | : -1), |
| 915 | channel_merger_(channel_merger) { |
| 916 | // Tell the readers we care so they know who to notify. |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 917 | VLOG(1) << "Configuring channel " << channel_index << " target node " |
| 918 | << FlatbufferToJson(target_node); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 919 | for (SplitMessageReader *reader : split_message_readers_) { |
| 920 | reader->SetTimestampMerger(this, channel_index, target_node); |
| 921 | } |
| 922 | |
| 923 | // And then determine if we need to track timestamps. |
| 924 | const Channel *channel = configuration->channels()->Get(channel_index); |
| 925 | if (!configuration::ChannelIsSendableOnNode(channel, target_node) && |
| 926 | configuration::ChannelIsReadableOnNode(channel, target_node)) { |
| 927 | has_timestamps_ = true; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | void TimestampMerger::PushMessageHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 932 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 933 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 934 | SplitMessageReader *split_message_reader) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 935 | if (split_message_reader != nullptr) { |
| 936 | DCHECK(std::find_if(message_heap_.begin(), message_heap_.end(), |
| 937 | [split_message_reader]( |
| 938 | const std::tuple<monotonic_clock::time_point, |
| 939 | uint32_t, SplitMessageReader *> |
| 940 | x) { |
| 941 | return std::get<2>(x) == split_message_reader; |
| 942 | }) == message_heap_.end()) |
| 943 | << ": Pushing message when it is already in the heap."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 944 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 945 | message_heap_.push_back(std::make_tuple( |
| 946 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 947 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 948 | std::push_heap(message_heap_.begin(), message_heap_.end(), |
| 949 | &SplitMessageReaderHeapCompare); |
| 950 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 951 | |
| 952 | // If we are just a data merger, don't wait for timestamps. |
| 953 | if (!has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 954 | if (!message_heap_.empty()) { |
| 955 | channel_merger_->Update(std::get<0>(message_heap_[0]), channel_index_); |
| 956 | pushed_ = true; |
| 957 | } else { |
| 958 | // Remove ourselves if we are empty. |
| 959 | channel_merger_->Update(monotonic_clock::min_time, channel_index_); |
| 960 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 964 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 965 | TimestampMerger::oldest_message() const { |
| 966 | CHECK_GT(message_heap_.size(), 0u); |
| 967 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 968 | oldest_message_reader = message_heap_.front(); |
| 969 | return std::get<2>(oldest_message_reader)->oldest_message(channel_index_); |
| 970 | } |
| 971 | |
| 972 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 973 | TimestampMerger::oldest_timestamp() const { |
| 974 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 975 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 976 | oldest_message_reader = timestamp_heap_.front(); |
| 977 | return std::get<2>(oldest_message_reader) |
| 978 | ->oldest_message(channel_index_, node_index_); |
| 979 | } |
| 980 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 981 | void TimestampMerger::PushTimestampHeap( |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 982 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 983 | timestamp, |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 984 | SplitMessageReader *split_message_reader) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 985 | if (split_message_reader != nullptr) { |
| 986 | DCHECK(std::find_if(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 987 | [split_message_reader]( |
| 988 | const std::tuple<monotonic_clock::time_point, |
| 989 | uint32_t, SplitMessageReader *> |
| 990 | x) { |
| 991 | return std::get<2>(x) == split_message_reader; |
| 992 | }) == timestamp_heap_.end()) |
| 993 | << ": Pushing timestamp when it is already in the heap."; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 994 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 995 | timestamp_heap_.push_back(std::make_tuple( |
| 996 | std::get<0>(timestamp), std::get<1>(timestamp), split_message_reader)); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 997 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 998 | std::push_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 999 | SplitMessageReaderHeapCompare); |
| 1000 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1001 | |
| 1002 | // If we are a timestamp merger, don't wait for data. Missing data will be |
| 1003 | // caught at read time. |
| 1004 | if (has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1005 | if (!timestamp_heap_.empty()) { |
| 1006 | channel_merger_->Update(std::get<0>(timestamp_heap_[0]), channel_index_); |
| 1007 | pushed_ = true; |
| 1008 | } else { |
| 1009 | // Remove ourselves if we are empty. |
| 1010 | channel_merger_->Update(monotonic_clock::min_time, channel_index_); |
| 1011 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1016 | FlatbufferVector<MessageHeader>> |
| 1017 | TimestampMerger::PopMessageHeap() { |
| 1018 | // Pop the oldest message reader pointer off the heap. |
| 1019 | CHECK_GT(message_heap_.size(), 0u); |
| 1020 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1021 | oldest_message_reader = message_heap_.front(); |
| 1022 | |
| 1023 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 1024 | &SplitMessageReaderHeapCompare); |
| 1025 | message_heap_.pop_back(); |
| 1026 | |
| 1027 | // Pop the oldest message. This re-pushes any messages from the reader to the |
| 1028 | // message heap. |
| 1029 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1030 | FlatbufferVector<MessageHeader>> |
| 1031 | oldest_message = |
| 1032 | std::get<2>(oldest_message_reader)->PopOldest(channel_index_); |
| 1033 | |
| 1034 | // Confirm that the time and queue_index we have recorded matches. |
| 1035 | CHECK_EQ(std::get<0>(oldest_message), std::get<0>(oldest_message_reader)); |
| 1036 | CHECK_EQ(std::get<1>(oldest_message), std::get<1>(oldest_message_reader)); |
| 1037 | |
| 1038 | // Now, keep reading until we have found all duplicates. |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1039 | while (!message_heap_.empty()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1040 | // See if it is a duplicate. |
| 1041 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1042 | next_oldest_message_reader = message_heap_.front(); |
| 1043 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1044 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1045 | next_oldest_message_time = std::get<2>(next_oldest_message_reader) |
| 1046 | ->oldest_message(channel_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1047 | |
| 1048 | if (std::get<0>(next_oldest_message_time) == std::get<0>(oldest_message) && |
| 1049 | std::get<1>(next_oldest_message_time) == std::get<1>(oldest_message)) { |
| 1050 | // Pop the message reader pointer. |
| 1051 | std::pop_heap(message_heap_.begin(), message_heap_.end(), |
| 1052 | &SplitMessageReaderHeapCompare); |
| 1053 | message_heap_.pop_back(); |
| 1054 | |
| 1055 | // Pop the next oldest message. This re-pushes any messages from the |
| 1056 | // reader. |
| 1057 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1058 | FlatbufferVector<MessageHeader>> |
| 1059 | next_oldest_message = std::get<2>(next_oldest_message_reader) |
| 1060 | ->PopOldest(channel_index_); |
| 1061 | |
| 1062 | // And make sure the message matches in it's entirety. |
| 1063 | CHECK(std::get<2>(oldest_message).span() == |
| 1064 | std::get<2>(next_oldest_message).span()) |
| 1065 | << ": Data at the same timestamp doesn't match."; |
| 1066 | } else { |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return oldest_message; |
| 1072 | } |
| 1073 | |
| 1074 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1075 | FlatbufferVector<MessageHeader>> |
| 1076 | TimestampMerger::PopTimestampHeap() { |
| 1077 | // Pop the oldest message reader pointer off the heap. |
| 1078 | CHECK_GT(timestamp_heap_.size(), 0u); |
| 1079 | |
| 1080 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1081 | oldest_timestamp_reader = timestamp_heap_.front(); |
| 1082 | |
| 1083 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1084 | &SplitMessageReaderHeapCompare); |
| 1085 | timestamp_heap_.pop_back(); |
| 1086 | |
| 1087 | CHECK(node_index_ != -1) << ": Timestamps in a single node environment"; |
| 1088 | |
| 1089 | // Pop the oldest message. This re-pushes any timestamps from the reader to |
| 1090 | // the timestamp heap. |
| 1091 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1092 | FlatbufferVector<MessageHeader>> |
| 1093 | oldest_timestamp = std::get<2>(oldest_timestamp_reader) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1094 | ->PopOldestTimestamp(channel_index_, node_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1095 | |
| 1096 | // Confirm that the time we have recorded matches. |
| 1097 | CHECK_EQ(std::get<0>(oldest_timestamp), std::get<0>(oldest_timestamp_reader)); |
| 1098 | CHECK_EQ(std::get<1>(oldest_timestamp), std::get<1>(oldest_timestamp_reader)); |
| 1099 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1100 | // Now, keep reading until we have found all duplicates. |
| 1101 | while (!timestamp_heap_.empty()) { |
| 1102 | // See if it is a duplicate. |
| 1103 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1104 | next_oldest_timestamp_reader = timestamp_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1105 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1106 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1107 | next_oldest_timestamp_time = |
| 1108 | std::get<2>(next_oldest_timestamp_reader) |
| 1109 | ->oldest_message(channel_index_, node_index_); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1110 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1111 | if (std::get<0>(next_oldest_timestamp_time) == |
| 1112 | std::get<0>(oldest_timestamp) && |
| 1113 | std::get<1>(next_oldest_timestamp_time) == |
| 1114 | std::get<1>(oldest_timestamp)) { |
| 1115 | // Pop the timestamp reader pointer. |
| 1116 | std::pop_heap(timestamp_heap_.begin(), timestamp_heap_.end(), |
| 1117 | &SplitMessageReaderHeapCompare); |
| 1118 | timestamp_heap_.pop_back(); |
| 1119 | |
| 1120 | // Pop the next oldest timestamp. This re-pushes any messages from the |
| 1121 | // reader. |
| 1122 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1123 | FlatbufferVector<MessageHeader>> |
| 1124 | next_oldest_timestamp = |
| 1125 | std::get<2>(next_oldest_timestamp_reader) |
| 1126 | ->PopOldestTimestamp(channel_index_, node_index_); |
| 1127 | |
| 1128 | // And make sure the contents matches in it's entirety. |
| 1129 | CHECK(std::get<2>(oldest_timestamp).span() == |
| 1130 | std::get<2>(next_oldest_timestamp).span()) |
| 1131 | << ": Data at the same timestamp doesn't match, " |
| 1132 | << aos::FlatbufferToJson(std::get<2>(oldest_timestamp)) << " vs " |
| 1133 | << aos::FlatbufferToJson(std::get<2>(next_oldest_timestamp)) << " " |
| 1134 | << absl::BytesToHexString(std::string_view( |
| 1135 | reinterpret_cast<const char *>( |
| 1136 | std::get<2>(oldest_timestamp).span().data()), |
| 1137 | std::get<2>(oldest_timestamp).span().size())) |
| 1138 | << " vs " |
| 1139 | << absl::BytesToHexString(std::string_view( |
| 1140 | reinterpret_cast<const char *>( |
| 1141 | std::get<2>(next_oldest_timestamp).span().data()), |
| 1142 | std::get<2>(next_oldest_timestamp).span().size())); |
| 1143 | |
| 1144 | } else { |
| 1145 | break; |
| 1146 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1149 | return oldest_timestamp; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1152 | std::tuple<TimestampMerger::DeliveryTimestamp, FlatbufferVector<MessageHeader>> |
| 1153 | TimestampMerger::PopOldest() { |
| 1154 | if (has_timestamps_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1155 | VLOG(1) << "Looking for matching timestamp for " |
| 1156 | << configuration::StrippedChannelToString( |
| 1157 | configuration_->channels()->Get(channel_index_)) |
| 1158 | << " (" << channel_index_ << ") " |
| 1159 | << " at " << std::get<0>(oldest_timestamp()); |
| 1160 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1161 | // Read the timestamps. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1162 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1163 | FlatbufferVector<MessageHeader>> |
| 1164 | oldest_timestamp = PopTimestampHeap(); |
| 1165 | |
| 1166 | TimestampMerger::DeliveryTimestamp timestamp; |
| 1167 | timestamp.monotonic_event_time = |
| 1168 | monotonic_clock::time_point(chrono::nanoseconds( |
| 1169 | std::get<2>(oldest_timestamp).message().monotonic_sent_time())); |
| 1170 | timestamp.realtime_event_time = |
| 1171 | realtime_clock::time_point(chrono::nanoseconds( |
| 1172 | std::get<2>(oldest_timestamp).message().realtime_sent_time())); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1173 | timestamp.queue_index = |
| 1174 | std::get<2>(oldest_timestamp).message().queue_index(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1175 | |
| 1176 | // Consistency check. |
| 1177 | CHECK_EQ(timestamp.monotonic_event_time, std::get<0>(oldest_timestamp)); |
| 1178 | CHECK_EQ(std::get<2>(oldest_timestamp).message().queue_index(), |
| 1179 | std::get<1>(oldest_timestamp)); |
| 1180 | |
| 1181 | monotonic_clock::time_point remote_timestamp_monotonic_time( |
| 1182 | chrono::nanoseconds( |
| 1183 | std::get<2>(oldest_timestamp).message().monotonic_remote_time())); |
| 1184 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1185 | // 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] | 1186 | if (message_heap_.empty()) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1187 | LOG(WARNING) << MaybeNodeName(configuration_->nodes()->Get(node_index_)) |
| 1188 | << "No data to match timestamp on " |
| 1189 | << configuration::CleanedChannelToString( |
| 1190 | configuration_->channels()->Get(channel_index_)) |
| 1191 | << " (" << channel_index_ << ")"; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1192 | return std::make_tuple(timestamp, |
| 1193 | std::move(std::get<2>(oldest_timestamp))); |
| 1194 | } |
| 1195 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1196 | while (true) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1197 | { |
| 1198 | // Ok, now try grabbing data until we find one which matches. |
| 1199 | std::tuple<monotonic_clock::time_point, uint32_t, const MessageHeader *> |
| 1200 | oldest_message_ref = oldest_message(); |
| 1201 | |
| 1202 | // Time at which the message was sent (this message is written from the |
| 1203 | // sending node's perspective. |
| 1204 | monotonic_clock::time_point remote_monotonic_time(chrono::nanoseconds( |
| 1205 | std::get<2>(oldest_message_ref)->monotonic_sent_time())); |
| 1206 | |
| 1207 | if (remote_monotonic_time < remote_timestamp_monotonic_time) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1208 | LOG(WARNING) << configuration_->nodes() |
| 1209 | ->Get(node_index_) |
| 1210 | ->name() |
| 1211 | ->string_view() |
| 1212 | << " Undelivered message, skipping. Remote time is " |
| 1213 | << remote_monotonic_time << " timestamp is " |
| 1214 | << remote_timestamp_monotonic_time << " on channel " |
| 1215 | << configuration::StrippedChannelToString( |
| 1216 | configuration_->channels()->Get(channel_index_)) |
| 1217 | << " (" << channel_index_ << ")"; |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1218 | PopMessageHeap(); |
| 1219 | continue; |
| 1220 | } else if (remote_monotonic_time > remote_timestamp_monotonic_time) { |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1221 | LOG(WARNING) << configuration_->nodes() |
| 1222 | ->Get(node_index_) |
| 1223 | ->name() |
| 1224 | ->string_view() |
| 1225 | << " Data not found. Remote time should be " |
| 1226 | << remote_timestamp_monotonic_time |
| 1227 | << ", message time is " << remote_monotonic_time |
| 1228 | << " on channel " |
| 1229 | << configuration::StrippedChannelToString( |
| 1230 | configuration_->channels()->Get(channel_index_)) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1231 | << " (" << channel_index_ << ")" |
| 1232 | << (VLOG_IS_ON(1) ? DebugString() : ""); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1233 | return std::make_tuple(timestamp, |
| 1234 | std::move(std::get<2>(oldest_timestamp))); |
| 1235 | } |
| 1236 | |
| 1237 | timestamp.monotonic_remote_time = remote_monotonic_time; |
| 1238 | } |
| 1239 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1240 | VLOG(1) << "Found matching data " |
| 1241 | << configuration::StrippedChannelToString( |
| 1242 | configuration_->channels()->Get(channel_index_)) |
| 1243 | << " (" << channel_index_ << ")"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1244 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1245 | FlatbufferVector<MessageHeader>> |
| 1246 | oldest_message = PopMessageHeap(); |
| 1247 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1248 | timestamp.realtime_remote_time = |
| 1249 | realtime_clock::time_point(chrono::nanoseconds( |
| 1250 | std::get<2>(oldest_message).message().realtime_sent_time())); |
| 1251 | timestamp.remote_queue_index = |
| 1252 | std::get<2>(oldest_message).message().queue_index(); |
| 1253 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1254 | CHECK_EQ(timestamp.monotonic_remote_time, |
| 1255 | remote_timestamp_monotonic_time); |
| 1256 | |
| 1257 | CHECK_EQ(timestamp.remote_queue_index, |
| 1258 | std::get<2>(oldest_timestamp).message().remote_queue_index()) |
| 1259 | << ": " << FlatbufferToJson(&std::get<2>(oldest_timestamp).message()) |
| 1260 | << " data " |
| 1261 | << FlatbufferToJson(&std::get<2>(oldest_message).message()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1262 | |
Austin Schuh | 30dd5c5 | 2020-08-01 14:43:44 -0700 | [diff] [blame] | 1263 | return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1264 | } |
| 1265 | } else { |
| 1266 | std::tuple<monotonic_clock::time_point, uint32_t, |
| 1267 | FlatbufferVector<MessageHeader>> |
| 1268 | oldest_message = PopMessageHeap(); |
| 1269 | |
| 1270 | TimestampMerger::DeliveryTimestamp timestamp; |
| 1271 | timestamp.monotonic_event_time = |
| 1272 | monotonic_clock::time_point(chrono::nanoseconds( |
| 1273 | std::get<2>(oldest_message).message().monotonic_sent_time())); |
| 1274 | timestamp.realtime_event_time = |
| 1275 | realtime_clock::time_point(chrono::nanoseconds( |
| 1276 | std::get<2>(oldest_message).message().realtime_sent_time())); |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1277 | timestamp.queue_index = std::get<2>(oldest_message).message().queue_index(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1278 | timestamp.remote_queue_index = 0xffffffff; |
| 1279 | |
| 1280 | CHECK_EQ(std::get<0>(oldest_message), timestamp.monotonic_event_time); |
| 1281 | CHECK_EQ(std::get<1>(oldest_message), |
| 1282 | std::get<2>(oldest_message).message().queue_index()); |
| 1283 | |
Austin Schuh | 30dd5c5 | 2020-08-01 14:43:44 -0700 | [diff] [blame] | 1284 | return std::make_tuple(timestamp, std::move(std::get<2>(oldest_message))); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1288 | void TimestampMerger::NoticeAtEnd() { channel_merger_->NoticeAtEnd(); } |
| 1289 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1290 | namespace { |
| 1291 | std::vector<std::unique_ptr<SplitMessageReader>> MakeSplitMessageReaders( |
| 1292 | const std::vector<std::vector<std::string>> &filenames) { |
| 1293 | CHECK_GT(filenames.size(), 0u); |
| 1294 | // Build up all the SplitMessageReaders. |
| 1295 | std::vector<std::unique_ptr<SplitMessageReader>> result; |
| 1296 | for (const std::vector<std::string> &filenames : filenames) { |
| 1297 | result.emplace_back(std::make_unique<SplitMessageReader>(filenames)); |
| 1298 | } |
| 1299 | return result; |
| 1300 | } |
| 1301 | } // namespace |
| 1302 | |
| 1303 | ChannelMerger::ChannelMerger( |
| 1304 | const std::vector<std::vector<std::string>> &filenames) |
| 1305 | : split_message_readers_(MakeSplitMessageReaders(filenames)), |
Austin Schuh | 97789fc | 2020-08-01 14:42:45 -0700 | [diff] [blame] | 1306 | log_file_header_(split_message_readers_[0]->raw_log_file_header()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1307 | // Now, confirm that the configuration matches for each and pick a start time. |
| 1308 | // Also return the list of possible nodes. |
| 1309 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1310 | split_message_readers_) { |
| 1311 | CHECK(CompareFlatBuffer(log_file_header_.message().configuration(), |
| 1312 | reader->log_file_header()->configuration())) |
| 1313 | << ": Replaying log files with different configurations isn't " |
| 1314 | "supported"; |
| 1315 | } |
| 1316 | |
| 1317 | nodes_ = configuration::GetNodes(configuration()); |
| 1318 | } |
| 1319 | |
| 1320 | bool ChannelMerger::SetNode(const Node *target_node) { |
| 1321 | std::vector<SplitMessageReader *> split_message_readers; |
| 1322 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1323 | split_message_readers_) { |
| 1324 | split_message_readers.emplace_back(reader.get()); |
| 1325 | } |
| 1326 | |
| 1327 | // Go find a log_file_header for this node. |
| 1328 | { |
| 1329 | bool found_node = false; |
| 1330 | |
| 1331 | for (const std::unique_ptr<SplitMessageReader> &reader : |
| 1332 | split_message_readers_) { |
James Kuszmaul | fc273dc | 2020-05-09 17:56:19 -0700 | [diff] [blame] | 1333 | // In order to identify which logfile(s) map to the target node, do a |
| 1334 | // logical comparison of the nodes, by confirming that we are either in a |
| 1335 | // single-node setup (where the nodes will both be nullptr) or that the |
| 1336 | // node names match (but the other node fields--e.g., hostname lists--may |
| 1337 | // not). |
| 1338 | const bool both_null = |
| 1339 | reader->node() == nullptr && target_node == nullptr; |
| 1340 | const bool both_have_name = |
| 1341 | (reader->node() != nullptr) && (target_node != nullptr) && |
| 1342 | (reader->node()->has_name() && target_node->has_name()); |
| 1343 | const bool node_names_identical = |
Brian Silverman | d90905f | 2020-09-23 14:42:56 -0700 | [diff] [blame] | 1344 | both_have_name && (reader->node()->name()->string_view() == |
| 1345 | target_node->name()->string_view()); |
James Kuszmaul | fc273dc | 2020-05-09 17:56:19 -0700 | [diff] [blame] | 1346 | if (both_null || node_names_identical) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1347 | if (!found_node) { |
| 1348 | found_node = true; |
| 1349 | log_file_header_ = CopyFlatBuffer(reader->log_file_header()); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1350 | VLOG(1) << "Found log file " << reader->filename() << " with node " |
| 1351 | << FlatbufferToJson(reader->node()) << " start_time " |
| 1352 | << monotonic_start_time(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1353 | } else { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1354 | // Find the earliest start time. That way, if we get a full log file |
| 1355 | // directly from the node, and a partial later, we start with the |
| 1356 | // full. Update our header to match that. |
| 1357 | const monotonic_clock::time_point new_monotonic_start_time( |
| 1358 | chrono::nanoseconds( |
| 1359 | reader->log_file_header()->monotonic_start_time())); |
| 1360 | const realtime_clock::time_point new_realtime_start_time( |
| 1361 | chrono::nanoseconds( |
| 1362 | reader->log_file_header()->realtime_start_time())); |
| 1363 | |
| 1364 | if (monotonic_start_time() == monotonic_clock::min_time || |
| 1365 | (new_monotonic_start_time != monotonic_clock::min_time && |
| 1366 | new_monotonic_start_time < monotonic_start_time())) { |
| 1367 | log_file_header_.mutable_message()->mutate_monotonic_start_time( |
| 1368 | new_monotonic_start_time.time_since_epoch().count()); |
| 1369 | log_file_header_.mutable_message()->mutate_realtime_start_time( |
| 1370 | new_realtime_start_time.time_since_epoch().count()); |
| 1371 | VLOG(1) << "Updated log file " << reader->filename() |
| 1372 | << " with node " << FlatbufferToJson(reader->node()) |
| 1373 | << " start_time " << new_monotonic_start_time; |
| 1374 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | if (!found_node) { |
| 1380 | LOG(WARNING) << "Failed to find log file for node " |
| 1381 | << FlatbufferToJson(target_node); |
| 1382 | return false; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | // Build up all the timestamp mergers. This connects up all the |
| 1387 | // SplitMessageReaders. |
| 1388 | timestamp_mergers_.reserve(configuration()->channels()->size()); |
| 1389 | for (size_t channel_index = 0; |
| 1390 | channel_index < configuration()->channels()->size(); ++channel_index) { |
| 1391 | timestamp_mergers_.emplace_back( |
| 1392 | configuration(), split_message_readers, channel_index, |
| 1393 | configuration::GetNode(configuration(), target_node), this); |
| 1394 | } |
| 1395 | |
| 1396 | // And prime everything. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1397 | for (std::unique_ptr<SplitMessageReader> &split_message_reader : |
| 1398 | split_message_readers_) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1399 | split_message_reader->QueueMessages( |
| 1400 | split_message_reader->monotonic_start_time()); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | node_ = configuration::GetNodeOrDie(configuration(), target_node); |
| 1404 | return true; |
| 1405 | } |
| 1406 | |
Austin Schuh | 858c9f3 | 2020-08-31 16:56:12 -0700 | [diff] [blame] | 1407 | monotonic_clock::time_point ChannelMerger::OldestMessageTime() const { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1408 | if (channel_heap_.empty()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1409 | return monotonic_clock::max_time; |
| 1410 | } |
| 1411 | return channel_heap_.front().first; |
| 1412 | } |
| 1413 | |
| 1414 | void ChannelMerger::PushChannelHeap(monotonic_clock::time_point timestamp, |
| 1415 | int channel_index) { |
| 1416 | // Pop and recreate the heap if it has already been pushed. And since we are |
| 1417 | // pushing again, we don't need to clear pushed. |
| 1418 | if (timestamp_mergers_[channel_index].pushed()) { |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1419 | const auto channel_iterator = std::find_if( |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1420 | channel_heap_.begin(), channel_heap_.end(), |
| 1421 | [channel_index](const std::pair<monotonic_clock::time_point, int> x) { |
| 1422 | return x.second == channel_index; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1423 | }); |
| 1424 | DCHECK(channel_iterator != channel_heap_.end()); |
| 1425 | if (std::get<0>(*channel_iterator) == timestamp) { |
| 1426 | // It's already in the heap, in the correct spot, so nothing |
| 1427 | // more for us to do here. |
| 1428 | return; |
| 1429 | } |
| 1430 | channel_heap_.erase(channel_iterator); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1431 | std::make_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1432 | ChannelHeapCompare); |
| 1433 | } |
| 1434 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1435 | if (timestamp == monotonic_clock::min_time) { |
| 1436 | timestamp_mergers_[channel_index].set_pushed(false); |
| 1437 | return; |
| 1438 | } |
| 1439 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1440 | channel_heap_.push_back(std::make_pair(timestamp, channel_index)); |
| 1441 | |
| 1442 | // The default sort puts the newest message first. Use a custom comparator to |
| 1443 | // put the oldest message first. |
| 1444 | std::push_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1445 | ChannelHeapCompare); |
| 1446 | } |
| 1447 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1448 | void ChannelMerger::VerifyHeaps() { |
Austin Schuh | 661a8d8 | 2020-09-13 17:25:56 -0700 | [diff] [blame] | 1449 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1450 | channel_heap_; |
| 1451 | std::make_heap(channel_heap.begin(), channel_heap.end(), &ChannelHeapCompare); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1452 | |
Austin Schuh | 661a8d8 | 2020-09-13 17:25:56 -0700 | [diff] [blame] | 1453 | for (size_t i = 0; i < channel_heap_.size(); ++i) { |
| 1454 | CHECK(channel_heap_[i] == channel_heap[i]) << ": Heaps diverged..."; |
| 1455 | CHECK_EQ( |
| 1456 | std::get<0>(channel_heap[i]), |
| 1457 | timestamp_mergers_[std::get<1>(channel_heap[i])].channel_merger_time()); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1461 | std::tuple<TimestampMerger::DeliveryTimestamp, int, |
| 1462 | FlatbufferVector<MessageHeader>> |
| 1463 | ChannelMerger::PopOldest() { |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1464 | CHECK_GT(channel_heap_.size(), 0u); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1465 | std::pair<monotonic_clock::time_point, int> oldest_channel_data = |
| 1466 | channel_heap_.front(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1467 | int channel_index = oldest_channel_data.second; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1468 | std::pop_heap(channel_heap_.begin(), channel_heap_.end(), |
| 1469 | &ChannelHeapCompare); |
| 1470 | channel_heap_.pop_back(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 1471 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1472 | timestamp_mergers_[channel_index].set_pushed(false); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1473 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1474 | TimestampMerger *merger = ×tamp_mergers_[channel_index]; |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1475 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1476 | // Merger handles any queueing needed from here. |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1477 | std::tuple<TimestampMerger::DeliveryTimestamp, |
| 1478 | FlatbufferVector<MessageHeader>> |
| 1479 | message = merger->PopOldest(); |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1480 | DCHECK_EQ(std::get<0>(message).monotonic_event_time, |
| 1481 | oldest_channel_data.first) |
| 1482 | << ": channel_heap_ was corrupted for " << channel_index << ": " |
| 1483 | << DebugString(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1484 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1485 | CHECK_GE(std::get<0>(message).monotonic_event_time, last_popped_time_) |
| 1486 | << ": " << MaybeNodeName(log_file_header()->node()) |
| 1487 | << "Messages came off the queue out of order. " << DebugString(); |
| 1488 | last_popped_time_ = std::get<0>(message).monotonic_event_time; |
| 1489 | |
| 1490 | VLOG(1) << "Popped " << last_popped_time_ << " " |
| 1491 | << configuration::StrippedChannelToString( |
| 1492 | configuration()->channels()->Get(channel_index)) |
| 1493 | << " (" << channel_index << ")"; |
| 1494 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1495 | return std::make_tuple(std::get<0>(message), channel_index, |
| 1496 | std::move(std::get<1>(message))); |
| 1497 | } |
| 1498 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1499 | std::string SplitMessageReader::MessageHeaderQueue::DebugString() const { |
| 1500 | std::stringstream ss; |
| 1501 | for (size_t i = 0; i < data_.size(); ++i) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1502 | if (i < 5 || i + 5 > data_.size()) { |
| 1503 | if (timestamps) { |
| 1504 | ss << " msg: "; |
| 1505 | } else { |
| 1506 | ss << " timestamp: "; |
| 1507 | } |
| 1508 | ss << monotonic_clock::time_point( |
| 1509 | chrono::nanoseconds(data_[i].message().monotonic_sent_time())) |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1510 | << " (" |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 1511 | << realtime_clock::time_point( |
| 1512 | chrono::nanoseconds(data_[i].message().realtime_sent_time())) |
| 1513 | << ") " << data_[i].message().queue_index(); |
| 1514 | if (timestamps) { |
| 1515 | ss << " <- remote " |
| 1516 | << monotonic_clock::time_point(chrono::nanoseconds( |
| 1517 | data_[i].message().monotonic_remote_time())) |
| 1518 | << " (" |
| 1519 | << realtime_clock::time_point(chrono::nanoseconds( |
| 1520 | data_[i].message().realtime_remote_time())) |
| 1521 | << ")"; |
| 1522 | } |
| 1523 | ss << "\n"; |
| 1524 | } else if (i == 5) { |
| 1525 | ss << " ...\n"; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1526 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1527 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1528 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1529 | return ss.str(); |
| 1530 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1531 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1532 | std::string SplitMessageReader::DebugString(int channel) const { |
| 1533 | std::stringstream ss; |
| 1534 | ss << "[\n"; |
| 1535 | ss << channels_[channel].data.DebugString(); |
| 1536 | ss << " ]"; |
| 1537 | return ss.str(); |
| 1538 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1539 | |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1540 | std::string SplitMessageReader::DebugString(int channel, int node_index) const { |
| 1541 | std::stringstream ss; |
| 1542 | ss << "[\n"; |
| 1543 | ss << channels_[channel].timestamps[node_index].DebugString(); |
| 1544 | ss << " ]"; |
| 1545 | return ss.str(); |
| 1546 | } |
| 1547 | |
| 1548 | std::string TimestampMerger::DebugString() const { |
| 1549 | std::stringstream ss; |
| 1550 | |
| 1551 | if (timestamp_heap_.size() > 0) { |
| 1552 | ss << " timestamp_heap {\n"; |
| 1553 | std::vector< |
| 1554 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1555 | timestamp_heap = timestamp_heap_; |
| 1556 | while (timestamp_heap.size() > 0u) { |
| 1557 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1558 | oldest_timestamp_reader = timestamp_heap.front(); |
| 1559 | |
| 1560 | ss << " " << std::get<2>(oldest_timestamp_reader) << " " |
| 1561 | << std::get<0>(oldest_timestamp_reader) << " queue_index (" |
| 1562 | << std::get<1>(oldest_timestamp_reader) << ") ttq " |
| 1563 | << std::get<2>(oldest_timestamp_reader)->time_to_queue() << " " |
| 1564 | << std::get<2>(oldest_timestamp_reader)->filename() << " -> " |
| 1565 | << std::get<2>(oldest_timestamp_reader) |
| 1566 | ->DebugString(channel_index_, node_index_) |
| 1567 | << "\n"; |
| 1568 | |
| 1569 | std::pop_heap(timestamp_heap.begin(), timestamp_heap.end(), |
| 1570 | &SplitMessageReaderHeapCompare); |
| 1571 | timestamp_heap.pop_back(); |
| 1572 | } |
| 1573 | ss << " }\n"; |
| 1574 | } |
| 1575 | |
| 1576 | ss << " message_heap {\n"; |
| 1577 | { |
| 1578 | std::vector< |
| 1579 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *>> |
| 1580 | message_heap = message_heap_; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1581 | while (!message_heap.empty()) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1582 | std::tuple<monotonic_clock::time_point, uint32_t, SplitMessageReader *> |
| 1583 | oldest_message_reader = message_heap.front(); |
| 1584 | |
| 1585 | ss << " " << std::get<2>(oldest_message_reader) << " " |
| 1586 | << std::get<0>(oldest_message_reader) << " queue_index (" |
| 1587 | << std::get<1>(oldest_message_reader) << ") ttq " |
| 1588 | << std::get<2>(oldest_message_reader)->time_to_queue() << " " |
| 1589 | << std::get<2>(oldest_message_reader)->filename() << " -> " |
| 1590 | << std::get<2>(oldest_message_reader)->DebugString(channel_index_) |
| 1591 | << "\n"; |
| 1592 | |
| 1593 | std::pop_heap(message_heap.begin(), message_heap.end(), |
| 1594 | &SplitMessageReaderHeapCompare); |
| 1595 | message_heap.pop_back(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 1596 | } |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1597 | } |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1598 | ss << " }"; |
| 1599 | |
| 1600 | return ss.str(); |
| 1601 | } |
| 1602 | |
| 1603 | std::string ChannelMerger::DebugString() const { |
| 1604 | std::stringstream ss; |
| 1605 | ss << "start_time " << realtime_start_time() << " " << monotonic_start_time() |
| 1606 | << "\n"; |
| 1607 | ss << "channel_heap {\n"; |
| 1608 | std::vector<std::pair<monotonic_clock::time_point, int>> channel_heap = |
| 1609 | channel_heap_; |
Brian Silverman | 8a32ce6 | 2020-08-12 12:02:38 -0700 | [diff] [blame] | 1610 | while (!channel_heap.empty()) { |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 1611 | std::tuple<monotonic_clock::time_point, int> channel = channel_heap.front(); |
| 1612 | ss << " " << std::get<0>(channel) << " (" << std::get<1>(channel) << ") " |
| 1613 | << configuration::CleanedChannelToString( |
| 1614 | configuration()->channels()->Get(std::get<1>(channel))) |
| 1615 | << "\n"; |
| 1616 | |
| 1617 | ss << timestamp_mergers_[std::get<1>(channel)].DebugString() << "\n"; |
| 1618 | |
| 1619 | std::pop_heap(channel_heap.begin(), channel_heap.end(), |
| 1620 | &ChannelHeapCompare); |
| 1621 | channel_heap.pop_back(); |
| 1622 | } |
| 1623 | ss << "}"; |
| 1624 | |
| 1625 | return ss.str(); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
Austin Schuh | ee71105 | 2020-08-24 16:06:09 -0700 | [diff] [blame] | 1628 | std::string MaybeNodeName(const Node *node) { |
| 1629 | if (node != nullptr) { |
| 1630 | return node->name()->str() + " "; |
| 1631 | } |
| 1632 | return ""; |
| 1633 | } |
| 1634 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1635 | } // namespace aos::logger |