Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/log_namer.h" |
| 2 | |
| 3 | #include <functional> |
| 4 | #include <map> |
| 5 | #include <memory> |
| 6 | #include <string_view> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "absl/strings/str_cat.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 10 | #include "flatbuffers/flatbuffers.h" |
| 11 | #include "glog/logging.h" |
| 12 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 13 | #include "aos/events/logging/logfile_utils.h" |
| 14 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 15 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 16 | #include "aos/uuid.h" |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 17 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 18 | DECLARE_int32(flush_size); |
| 19 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 20 | namespace aos { |
| 21 | namespace logger { |
| 22 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 23 | NewDataWriter::NewDataWriter(LogNamer *log_namer, const Node *node, |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 24 | const Node *logger_node, |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 25 | std::function<void(NewDataWriter *)> reopen, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 26 | std::function<void(NewDataWriter *)> close, |
| 27 | size_t max_message_size) |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 28 | : node_(node), |
| 29 | node_index_(configuration::GetNodeIndex(log_namer->configuration_, node)), |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 30 | logger_node_index_( |
| 31 | configuration::GetNodeIndex(log_namer->configuration_, logger_node)), |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 32 | log_namer_(log_namer), |
| 33 | reopen_(std::move(reopen)), |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 34 | close_(std::move(close)), |
| 35 | max_message_size_(max_message_size) { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 36 | state_.resize(configuration::NodesCount(log_namer->configuration_)); |
| 37 | CHECK_LT(node_index_, state_.size()); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | NewDataWriter::~NewDataWriter() { |
| 41 | if (writer) { |
| 42 | Close(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void NewDataWriter::Rotate() { |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 47 | // No need to rotate if nothing has been written. |
| 48 | if (header_written_) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 49 | VLOG(1) << "Rotated " << name(); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 50 | ++parts_index_; |
| 51 | reopen_(this); |
| 52 | header_written_ = false; |
| 53 | QueueHeader(MakeHeader()); |
| 54 | } |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 57 | void NewDataWriter::Reboot(const UUID &source_node_boot_uuid) { |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 58 | parts_uuid_ = UUID::Random(); |
| 59 | ++parts_index_; |
| 60 | reopen_(this); |
| 61 | header_written_ = false; |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 62 | for (State &state : state_) { |
| 63 | state.boot_uuid = UUID::Zero(); |
| 64 | state.oldest_remote_monotonic_timestamp = monotonic_clock::max_time; |
| 65 | state.oldest_local_monotonic_timestamp = monotonic_clock::max_time; |
| 66 | state.oldest_remote_unreliable_monotonic_timestamp = |
| 67 | monotonic_clock::max_time; |
| 68 | state.oldest_local_unreliable_monotonic_timestamp = |
| 69 | monotonic_clock::max_time; |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 70 | state.oldest_remote_reliable_monotonic_timestamp = |
| 71 | monotonic_clock::max_time; |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 72 | state.oldest_local_reliable_monotonic_timestamp = monotonic_clock::max_time; |
| 73 | state.oldest_logger_remote_unreliable_monotonic_timestamp = |
| 74 | monotonic_clock::max_time; |
| 75 | state.oldest_logger_local_unreliable_monotonic_timestamp = |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 76 | monotonic_clock::max_time; |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | state_[node_index_].boot_uuid = source_node_boot_uuid; |
| 80 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 81 | VLOG(1) << "Rebooted " << name(); |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void NewDataWriter::UpdateBoot(const UUID &source_node_boot_uuid) { |
| 85 | if (state_[node_index_].boot_uuid != source_node_boot_uuid) { |
| 86 | state_[node_index_].boot_uuid = source_node_boot_uuid; |
| 87 | if (header_written_) { |
| 88 | Reboot(source_node_boot_uuid); |
| 89 | } |
| 90 | } |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 93 | void NewDataWriter::UpdateRemote( |
| 94 | const size_t remote_node_index, const UUID &remote_node_boot_uuid, |
| 95 | const monotonic_clock::time_point monotonic_remote_time, |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 96 | const monotonic_clock::time_point monotonic_event_time, const bool reliable, |
| 97 | monotonic_clock::time_point monotonic_timestamp_time) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 98 | // Trigger rotation if anything in the header changes. |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 99 | bool rotate = false; |
| 100 | CHECK_LT(remote_node_index, state_.size()); |
| 101 | State &state = state_[remote_node_index]; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 102 | |
| 103 | // Did the remote boot UUID change? |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 104 | if (state.boot_uuid != remote_node_boot_uuid) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 105 | VLOG(1) << name() << " Remote " << remote_node_index << " updated to " |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 106 | << remote_node_boot_uuid << " from " << state.boot_uuid; |
| 107 | state.boot_uuid = remote_node_boot_uuid; |
| 108 | state.oldest_remote_monotonic_timestamp = monotonic_clock::max_time; |
| 109 | state.oldest_local_monotonic_timestamp = monotonic_clock::max_time; |
| 110 | state.oldest_remote_unreliable_monotonic_timestamp = |
| 111 | monotonic_clock::max_time; |
| 112 | state.oldest_local_unreliable_monotonic_timestamp = |
| 113 | monotonic_clock::max_time; |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 114 | state.oldest_remote_reliable_monotonic_timestamp = |
| 115 | monotonic_clock::max_time; |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 116 | state.oldest_local_reliable_monotonic_timestamp = monotonic_clock::max_time; |
| 117 | state.oldest_logger_remote_unreliable_monotonic_timestamp = |
| 118 | monotonic_clock::max_time; |
| 119 | state.oldest_logger_local_unreliable_monotonic_timestamp = |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 120 | monotonic_clock::max_time; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 121 | rotate = true; |
| 122 | } |
| 123 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 124 | // Did the unreliable timestamps change? |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 125 | if (!reliable) { |
| 126 | if (state.oldest_remote_unreliable_monotonic_timestamp > |
| 127 | monotonic_remote_time) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 128 | VLOG(1) << name() << " Remote " << remote_node_index |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 129 | << " oldest_remote_unreliable_monotonic_timestamp updated from " |
| 130 | << state.oldest_remote_unreliable_monotonic_timestamp << " to " |
| 131 | << monotonic_remote_time; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 132 | state.oldest_remote_unreliable_monotonic_timestamp = |
| 133 | monotonic_remote_time; |
| 134 | state.oldest_local_unreliable_monotonic_timestamp = monotonic_event_time; |
| 135 | rotate = true; |
| 136 | } |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 137 | } else { |
| 138 | if (state.oldest_remote_reliable_monotonic_timestamp > |
| 139 | monotonic_remote_time) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 140 | VLOG(1) << name() << " Remote " << remote_node_index |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 141 | << " oldest_remote_reliable_monotonic_timestamp updated from " |
| 142 | << state.oldest_remote_reliable_monotonic_timestamp << " to " |
| 143 | << monotonic_remote_time; |
| 144 | state.oldest_remote_reliable_monotonic_timestamp = monotonic_remote_time; |
| 145 | state.oldest_local_reliable_monotonic_timestamp = monotonic_event_time; |
| 146 | rotate = true; |
| 147 | } |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 150 | // Track the logger timestamps too. |
| 151 | if (monotonic_timestamp_time != monotonic_clock::min_time) { |
| 152 | State &logger_state = state_[node_index_]; |
| 153 | CHECK_EQ(remote_node_index, logger_node_index_); |
| 154 | if (monotonic_event_time < |
| 155 | logger_state.oldest_logger_remote_unreliable_monotonic_timestamp) { |
| 156 | VLOG(1) |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 157 | << name() << " Remote " << node_index_ |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 158 | << " oldest_logger_remote_unreliable_monotonic_timestamp updated " |
| 159 | "from " |
| 160 | << logger_state.oldest_logger_remote_unreliable_monotonic_timestamp |
| 161 | << " to " << monotonic_event_time; |
| 162 | logger_state.oldest_logger_remote_unreliable_monotonic_timestamp = |
| 163 | monotonic_event_time; |
| 164 | logger_state.oldest_logger_local_unreliable_monotonic_timestamp = |
| 165 | monotonic_timestamp_time; |
| 166 | |
| 167 | rotate = true; |
| 168 | } |
| 169 | } |
| 170 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 171 | // Did any of the timestamps change? |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 172 | if (state.oldest_remote_monotonic_timestamp > monotonic_remote_time) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 173 | VLOG(1) << name() << " Remote " << remote_node_index |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 174 | << " oldest_remote_monotonic_timestamp updated from " |
| 175 | << state.oldest_remote_monotonic_timestamp << " to " |
| 176 | << monotonic_remote_time; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 177 | state.oldest_remote_monotonic_timestamp = monotonic_remote_time; |
| 178 | state.oldest_local_monotonic_timestamp = monotonic_event_time; |
| 179 | rotate = true; |
| 180 | } |
| 181 | |
| 182 | if (rotate) { |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 183 | Rotate(); |
| 184 | } |
| 185 | } |
| 186 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 187 | void NewDataWriter::CopyMessage(DataEncoder::Copier *coppier, |
| 188 | const UUID &source_node_boot_uuid, |
| 189 | aos::monotonic_clock::time_point now) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 190 | // Trigger a reboot if we detect the boot UUID change. |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 191 | UpdateBoot(source_node_boot_uuid); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 192 | |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 193 | if (!header_written_) { |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 194 | QueueHeader(MakeHeader()); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 195 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 196 | |
| 197 | // If the start time has changed for this node, trigger a rotation. |
| 198 | if (log_namer_->monotonic_start_time(node_index_, source_node_boot_uuid) != |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 199 | monotonic_start_time_) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 200 | CHECK(header_written_); |
| 201 | Rotate(); |
| 202 | } |
| 203 | |
| 204 | CHECK_EQ(log_namer_->monotonic_start_time(node_index_, source_node_boot_uuid), |
| 205 | monotonic_start_time_); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 206 | CHECK_EQ(state_[node_index_].boot_uuid, source_node_boot_uuid); |
milind-u | a50344f | 2021-08-25 18:22:20 -0700 | [diff] [blame] | 207 | CHECK(writer); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 208 | CHECK(header_written_) << ": Attempting to write message before header to " |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 209 | << writer->name(); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 210 | writer->CopyMessage(coppier, now); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 213 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> |
| 214 | NewDataWriter::MakeHeader() { |
| 215 | const size_t logger_node_index = log_namer_->logger_node_index(); |
| 216 | const UUID &logger_node_boot_uuid = log_namer_->logger_node_boot_uuid(); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 217 | if (state_[logger_node_index].boot_uuid == UUID::Zero()) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 218 | VLOG(1) << name() << " Logger node is " << logger_node_index |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 219 | << " and uuid is " << logger_node_boot_uuid; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 220 | state_[logger_node_index].boot_uuid = logger_node_boot_uuid; |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 221 | } else { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 222 | CHECK_EQ(state_[logger_node_index].boot_uuid, logger_node_boot_uuid); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 223 | } |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 224 | return log_namer_->MakeHeader(node_index_, state_, parts_uuid(), |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 225 | parts_index_); |
| 226 | } |
| 227 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 228 | void NewDataWriter::QueueHeader( |
| 229 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header) { |
| 230 | CHECK(!header_written_) << ": Attempting to write duplicate header to " |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 231 | << writer->name(); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 232 | CHECK(header.message().has_source_node_boot_uuid()); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 233 | CHECK_EQ(state_[node_index_].boot_uuid, |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 234 | UUID::FromString(header.message().source_node_boot_uuid())); |
Austin Schuh | 510dc62 | 2021-08-06 18:47:30 -0700 | [diff] [blame] | 235 | if (!writer) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 236 | // Since we haven't opened the first time, it's still not too late to update |
| 237 | // the max message size. Make sure the header fits. |
| 238 | // |
| 239 | // This won't work well on reboots, but the structure of the header is fixed |
| 240 | // by that point in time, so it's size is fixed too. |
| 241 | // |
| 242 | // Most of the time, the minimum buffer size inside the encoder of around |
| 243 | // 128k will make this a non-issue. |
| 244 | UpdateMaxMessageSize(header.span().size()); |
| 245 | |
Austin Schuh | 510dc62 | 2021-08-06 18:47:30 -0700 | [diff] [blame] | 246 | reopen_(this); |
| 247 | } |
| 248 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 249 | VLOG(1) << "Writing to " << name() << " " |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 250 | << aos::FlatbufferToJson( |
| 251 | header, {.multi_line = false, .max_vector_size = 100}); |
| 252 | |
Austin Schuh | 510dc62 | 2021-08-06 18:47:30 -0700 | [diff] [blame] | 253 | CHECK(writer); |
Austin Schuh | 7ef11a4 | 2023-02-04 17:15:12 -0800 | [diff] [blame] | 254 | DataEncoder::SpanCopier coppier(header.span()); |
| 255 | writer->CopyMessage(&coppier, aos::monotonic_clock::now()); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 256 | header_written_ = true; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 257 | monotonic_start_time_ = log_namer_->monotonic_start_time( |
| 258 | node_index_, state_[node_index_].boot_uuid); |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void NewDataWriter::Close() { |
| 262 | CHECK(writer); |
| 263 | close_(this); |
| 264 | writer.reset(); |
| 265 | header_written_ = false; |
| 266 | } |
| 267 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 268 | LogNamer::NodeState *LogNamer::GetNodeState(size_t node_index, |
| 269 | const UUID &boot_uuid) { |
| 270 | auto it = node_states_.find(std::make_pair(node_index, boot_uuid)); |
| 271 | if (it == node_states_.end()) { |
| 272 | it = |
| 273 | node_states_.emplace(std::make_pair(node_index, boot_uuid), NodeState()) |
| 274 | .first; |
| 275 | } |
| 276 | return &it->second; |
| 277 | } |
| 278 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 279 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> LogNamer::MakeHeader( |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 280 | size_t node_index, const std::vector<NewDataWriter::State> &state, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 281 | const UUID &parts_uuid, int parts_index) { |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 282 | const UUID &source_node_boot_uuid = state[node_index].boot_uuid; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 283 | const Node *const source_node = |
| 284 | configuration::GetNode(configuration_, node_index); |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 285 | CHECK_EQ(LogFileHeader::MiniReflectTypeTable()->num_elems, 34u); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 286 | flatbuffers::FlatBufferBuilder fbb; |
| 287 | fbb.ForceDefaults(true); |
| 288 | |
| 289 | flatbuffers::Offset<flatbuffers::String> config_sha256_offset; |
| 290 | flatbuffers::Offset<aos::Configuration> configuration_offset; |
| 291 | if (header_.message().has_configuration()) { |
| 292 | CHECK(!header_.message().has_configuration_sha256()); |
| 293 | configuration_offset = |
| 294 | CopyFlatBuffer(header_.message().configuration(), &fbb); |
| 295 | } else { |
| 296 | CHECK(!header_.message().has_configuration()); |
| 297 | CHECK(header_.message().has_configuration_sha256()); |
| 298 | config_sha256_offset = fbb.CreateString( |
| 299 | header_.message().configuration_sha256()->string_view()); |
| 300 | } |
| 301 | |
| 302 | CHECK(header_.message().has_name()); |
| 303 | const flatbuffers::Offset<flatbuffers::String> name_offset = |
| 304 | fbb.CreateString(header_.message().name()->string_view()); |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 305 | const flatbuffers::Offset<flatbuffers::String> logger_sha1_offset = |
| 306 | header_.message().has_logger_sha1() |
| 307 | ? fbb.CreateString(header_.message().logger_sha1()->string_view()) |
| 308 | : 0; |
| 309 | const flatbuffers::Offset<flatbuffers::String> logger_version_offset = |
| 310 | header_.message().has_logger_version() |
| 311 | ? fbb.CreateString(header_.message().logger_version()->string_view()) |
| 312 | : 0; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 313 | |
| 314 | CHECK(header_.message().has_log_event_uuid()); |
| 315 | const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset = |
| 316 | fbb.CreateString(header_.message().log_event_uuid()->string_view()); |
| 317 | |
| 318 | CHECK(header_.message().has_logger_instance_uuid()); |
| 319 | const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset = |
| 320 | fbb.CreateString(header_.message().logger_instance_uuid()->string_view()); |
| 321 | |
| 322 | flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset; |
| 323 | if (header_.message().has_log_start_uuid()) { |
| 324 | log_start_uuid_offset = |
| 325 | fbb.CreateString(header_.message().log_start_uuid()->string_view()); |
| 326 | } |
| 327 | |
| 328 | CHECK(header_.message().has_logger_node_boot_uuid()); |
| 329 | const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset = |
| 330 | fbb.CreateString( |
| 331 | header_.message().logger_node_boot_uuid()->string_view()); |
| 332 | |
| 333 | CHECK_NE(source_node_boot_uuid, UUID::Zero()); |
| 334 | const flatbuffers::Offset<flatbuffers::String> source_node_boot_uuid_offset = |
| 335 | source_node_boot_uuid.PackString(&fbb); |
| 336 | |
| 337 | const flatbuffers::Offset<flatbuffers::String> parts_uuid_offset = |
| 338 | parts_uuid.PackString(&fbb); |
| 339 | |
| 340 | flatbuffers::Offset<Node> node_offset; |
| 341 | flatbuffers::Offset<Node> logger_node_offset; |
| 342 | |
| 343 | if (configuration::MultiNode(configuration_)) { |
| 344 | node_offset = RecursiveCopyFlatBuffer(source_node, &fbb); |
| 345 | logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb); |
| 346 | } |
| 347 | |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 348 | std::vector<flatbuffers::Offset<flatbuffers::String>> boot_uuid_offsets; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 349 | boot_uuid_offsets.reserve(state.size()); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 350 | |
Austin Schuh | 4db9ec9 | 2021-09-22 13:11:12 -0700 | [diff] [blame] | 351 | int64_t *unused; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 352 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 353 | oldest_remote_monotonic_timestamps_offset = |
| 354 | fbb.CreateUninitializedVector(state.size(), &unused); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 355 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 356 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 357 | oldest_local_monotonic_timestamps_offset = |
| 358 | fbb.CreateUninitializedVector(state.size(), &unused); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 359 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 360 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
| 361 | oldest_remote_unreliable_monotonic_timestamps_offset = |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 362 | fbb.CreateUninitializedVector(state.size(), &unused); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 363 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 364 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
| 365 | oldest_local_unreliable_monotonic_timestamps_offset = |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 366 | fbb.CreateUninitializedVector(state.size(), &unused); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 367 | |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 368 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
| 369 | oldest_remote_reliable_monotonic_timestamps_offset = |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 370 | fbb.CreateUninitializedVector(state.size(), &unused); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 371 | |
| 372 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
| 373 | oldest_local_reliable_monotonic_timestamps_offset = |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 374 | fbb.CreateUninitializedVector(state.size(), &unused); |
| 375 | |
| 376 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
| 377 | oldest_logger_remote_unreliable_monotonic_timestamps_offset = |
| 378 | fbb.CreateUninitializedVector(state.size(), &unused); |
| 379 | |
| 380 | flatbuffers::Offset<flatbuffers::Vector<int64_t>> |
| 381 | oldest_logger_local_unreliable_monotonic_timestamps_offset = |
| 382 | fbb.CreateUninitializedVector(state.size(), &unused); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 383 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 384 | for (size_t i = 0; i < state.size(); ++i) { |
Austin Schuh | 4db9ec9 | 2021-09-22 13:11:12 -0700 | [diff] [blame] | 385 | if (state[i].boot_uuid != UUID::Zero()) { |
| 386 | boot_uuid_offsets.emplace_back(state[i].boot_uuid.PackString(&fbb)); |
| 387 | } else { |
| 388 | boot_uuid_offsets.emplace_back(fbb.CreateString("")); |
| 389 | } |
Austin Schuh | 5ae8f4a | 2021-09-11 19:09:50 -0700 | [diff] [blame] | 390 | if (state[i].boot_uuid == UUID::Zero()) { |
| 391 | CHECK_EQ(state[i].oldest_remote_monotonic_timestamp, |
| 392 | monotonic_clock::max_time); |
| 393 | CHECK_EQ(state[i].oldest_local_monotonic_timestamp, |
| 394 | monotonic_clock::max_time); |
| 395 | CHECK_EQ(state[i].oldest_remote_unreliable_monotonic_timestamp, |
| 396 | monotonic_clock::max_time); |
| 397 | CHECK_EQ(state[i].oldest_local_unreliable_monotonic_timestamp, |
| 398 | monotonic_clock::max_time); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 399 | CHECK_EQ(state[i].oldest_remote_reliable_monotonic_timestamp, |
| 400 | monotonic_clock::max_time); |
| 401 | CHECK_EQ(state[i].oldest_local_reliable_monotonic_timestamp, |
| 402 | monotonic_clock::max_time); |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 403 | CHECK_EQ(state[i].oldest_logger_remote_unreliable_monotonic_timestamp, |
| 404 | monotonic_clock::max_time); |
| 405 | CHECK_EQ(state[i].oldest_logger_local_unreliable_monotonic_timestamp, |
| 406 | monotonic_clock::max_time); |
Austin Schuh | 5ae8f4a | 2021-09-11 19:09:50 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Austin Schuh | 4db9ec9 | 2021-09-22 13:11:12 -0700 | [diff] [blame] | 409 | flatbuffers::GetMutableTemporaryPointer( |
| 410 | fbb, oldest_remote_monotonic_timestamps_offset) |
| 411 | ->Mutate(i, state[i] |
| 412 | .oldest_remote_monotonic_timestamp.time_since_epoch() |
| 413 | .count()); |
| 414 | flatbuffers::GetMutableTemporaryPointer( |
| 415 | fbb, oldest_local_monotonic_timestamps_offset) |
| 416 | ->Mutate(i, state[i] |
| 417 | .oldest_local_monotonic_timestamp.time_since_epoch() |
| 418 | .count()); |
| 419 | flatbuffers::GetMutableTemporaryPointer( |
| 420 | fbb, oldest_remote_unreliable_monotonic_timestamps_offset) |
| 421 | ->Mutate(i, state[i] |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 422 | .oldest_remote_unreliable_monotonic_timestamp |
| 423 | .time_since_epoch() |
Austin Schuh | 4db9ec9 | 2021-09-22 13:11:12 -0700 | [diff] [blame] | 424 | .count()); |
| 425 | flatbuffers::GetMutableTemporaryPointer( |
| 426 | fbb, oldest_local_unreliable_monotonic_timestamps_offset) |
| 427 | ->Mutate(i, state[i] |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 428 | .oldest_local_unreliable_monotonic_timestamp |
| 429 | .time_since_epoch() |
Austin Schuh | 4db9ec9 | 2021-09-22 13:11:12 -0700 | [diff] [blame] | 430 | .count()); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 431 | |
| 432 | flatbuffers::GetMutableTemporaryPointer( |
| 433 | fbb, oldest_remote_reliable_monotonic_timestamps_offset) |
| 434 | ->Mutate(i, state[i] |
| 435 | .oldest_remote_reliable_monotonic_timestamp |
| 436 | .time_since_epoch() |
| 437 | .count()); |
| 438 | flatbuffers::GetMutableTemporaryPointer( |
| 439 | fbb, oldest_local_reliable_monotonic_timestamps_offset) |
| 440 | ->Mutate( |
| 441 | i, state[i] |
| 442 | .oldest_local_reliable_monotonic_timestamp.time_since_epoch() |
| 443 | .count()); |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 444 | |
| 445 | flatbuffers::GetMutableTemporaryPointer( |
| 446 | fbb, oldest_logger_remote_unreliable_monotonic_timestamps_offset) |
| 447 | ->Mutate(i, state[i] |
| 448 | .oldest_logger_remote_unreliable_monotonic_timestamp |
| 449 | .time_since_epoch() |
| 450 | .count()); |
| 451 | flatbuffers::GetMutableTemporaryPointer( |
| 452 | fbb, oldest_logger_local_unreliable_monotonic_timestamps_offset) |
| 453 | ->Mutate(i, state[i] |
| 454 | .oldest_logger_local_unreliable_monotonic_timestamp |
| 455 | .time_since_epoch() |
| 456 | .count()); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Austin Schuh | 4db9ec9 | 2021-09-22 13:11:12 -0700 | [diff] [blame] | 459 | flatbuffers::Offset< |
| 460 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 461 | boot_uuids_offset = fbb.CreateVector(boot_uuid_offsets); |
| 462 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 463 | aos::logger::LogFileHeader::Builder log_file_header_builder(fbb); |
| 464 | |
| 465 | log_file_header_builder.add_name(name_offset); |
Austin Schuh | fa71268 | 2022-05-11 16:43:42 -0700 | [diff] [blame] | 466 | if (!logger_sha1_offset.IsNull()) { |
| 467 | log_file_header_builder.add_logger_sha1(logger_sha1_offset); |
| 468 | } |
| 469 | if (!logger_version_offset.IsNull()) { |
| 470 | log_file_header_builder.add_logger_version(logger_version_offset); |
| 471 | } |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 472 | |
| 473 | // Only add the node if we are running in a multinode configuration. |
| 474 | if (!logger_node_offset.IsNull()) { |
| 475 | log_file_header_builder.add_node(node_offset); |
| 476 | log_file_header_builder.add_logger_node(logger_node_offset); |
| 477 | } |
| 478 | |
| 479 | if (!configuration_offset.IsNull()) { |
| 480 | log_file_header_builder.add_configuration(configuration_offset); |
| 481 | } |
| 482 | log_file_header_builder.add_max_out_of_order_duration( |
| 483 | header_.message().max_out_of_order_duration()); |
| 484 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 485 | NodeState *node_state = GetNodeState(node_index, source_node_boot_uuid); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 486 | log_file_header_builder.add_monotonic_start_time( |
| 487 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 488 | node_state->monotonic_start_time.time_since_epoch()) |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 489 | .count()); |
| 490 | if (source_node == node_) { |
| 491 | log_file_header_builder.add_realtime_start_time( |
| 492 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 493 | node_state->realtime_start_time.time_since_epoch()) |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 494 | .count()); |
| 495 | } else { |
| 496 | // Fill out the legacy start times. Since these were implemented to never |
| 497 | // change on reboot, they aren't very helpful in tracking what happened. |
| 498 | log_file_header_builder.add_logger_monotonic_start_time( |
| 499 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 500 | node_state->logger_monotonic_start_time.time_since_epoch()) |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 501 | .count()); |
| 502 | log_file_header_builder.add_logger_realtime_start_time( |
| 503 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 504 | node_state->logger_realtime_start_time.time_since_epoch()) |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 505 | .count()); |
| 506 | } |
| 507 | |
| 508 | // TODO(austin): Add more useful times. When was this part started? What do |
| 509 | // we know about both the logger and remote then? |
| 510 | |
| 511 | log_file_header_builder.add_log_event_uuid(log_event_uuid_offset); |
| 512 | log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset); |
| 513 | if (!log_start_uuid_offset.IsNull()) { |
| 514 | log_file_header_builder.add_log_start_uuid(log_start_uuid_offset); |
| 515 | } |
| 516 | log_file_header_builder.add_logger_node_boot_uuid( |
| 517 | logger_node_boot_uuid_offset); |
| 518 | log_file_header_builder.add_source_node_boot_uuid( |
| 519 | source_node_boot_uuid_offset); |
| 520 | |
| 521 | log_file_header_builder.add_parts_uuid(parts_uuid_offset); |
| 522 | log_file_header_builder.add_parts_index(parts_index); |
| 523 | |
| 524 | if (!config_sha256_offset.IsNull()) { |
| 525 | log_file_header_builder.add_configuration_sha256(config_sha256_offset); |
| 526 | } |
| 527 | |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 528 | log_file_header_builder.add_boot_uuids(boot_uuids_offset); |
Austin Schuh | a499cea | 2021-07-31 19:49:53 -0700 | [diff] [blame] | 529 | log_file_header_builder.add_logger_part_monotonic_start_time( |
| 530 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 531 | event_loop_->monotonic_now().time_since_epoch()) |
| 532 | .count()); |
| 533 | log_file_header_builder.add_logger_part_realtime_start_time( |
| 534 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 535 | event_loop_->realtime_now().time_since_epoch()) |
| 536 | .count()); |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 537 | log_file_header_builder.add_oldest_remote_monotonic_timestamps( |
| 538 | oldest_remote_monotonic_timestamps_offset); |
| 539 | log_file_header_builder.add_oldest_local_monotonic_timestamps( |
| 540 | oldest_local_monotonic_timestamps_offset); |
| 541 | log_file_header_builder.add_oldest_remote_unreliable_monotonic_timestamps( |
| 542 | oldest_remote_unreliable_monotonic_timestamps_offset); |
| 543 | log_file_header_builder.add_oldest_local_unreliable_monotonic_timestamps( |
| 544 | oldest_local_unreliable_monotonic_timestamps_offset); |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 545 | log_file_header_builder.add_oldest_remote_reliable_monotonic_timestamps( |
| 546 | oldest_remote_reliable_monotonic_timestamps_offset); |
| 547 | log_file_header_builder.add_oldest_local_reliable_monotonic_timestamps( |
| 548 | oldest_local_reliable_monotonic_timestamps_offset); |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 549 | log_file_header_builder |
| 550 | .add_oldest_logger_remote_unreliable_monotonic_timestamps( |
| 551 | oldest_logger_remote_unreliable_monotonic_timestamps_offset); |
| 552 | log_file_header_builder |
| 553 | .add_oldest_logger_local_unreliable_monotonic_timestamps( |
| 554 | oldest_logger_local_unreliable_monotonic_timestamps_offset); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 555 | fbb.FinishSizePrefixed(log_file_header_builder.Finish()); |
| 556 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result( |
| 557 | fbb.Release()); |
| 558 | |
| 559 | CHECK(result.Verify()) << ": Built a corrupted header."; |
| 560 | |
| 561 | return result; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 564 | MultiNodeLogNamer::MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend, |
| 565 | EventLoop *event_loop) |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 566 | : MultiNodeLogNamer(std::move(log_backend), event_loop->configuration(), |
| 567 | event_loop, event_loop->node()) {} |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 568 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 569 | MultiNodeLogNamer::MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend, |
| 570 | const Configuration *configuration, |
| 571 | EventLoop *event_loop, const Node *node) |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 572 | : LogNamer(configuration, event_loop, node), |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 573 | log_backend_(std::move(log_backend)), |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 574 | encoder_factory_([](size_t max_message_size) { |
| 575 | // TODO(austin): For slow channels, can we allocate less memory? |
| 576 | return std::make_unique<DummyEncoder>(max_message_size, |
| 577 | FLAGS_flush_size); |
| 578 | }) {} |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 579 | |
Brian Silverman | 48deab1 | 2020-09-30 18:39:28 -0700 | [diff] [blame] | 580 | MultiNodeLogNamer::~MultiNodeLogNamer() { |
| 581 | if (!ran_out_of_space_) { |
| 582 | // This handles renaming temporary files etc. |
| 583 | Close(); |
| 584 | } |
| 585 | } |
| 586 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 587 | void MultiNodeLogNamer::Rotate(const Node *node) { |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 588 | if (node == this->node()) { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 589 | if (data_writer_) { |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 590 | data_writer_->Rotate(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 591 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 592 | } else { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 593 | for (std::pair<const Channel *const, NewDataWriter> &data_writer : |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 594 | data_writers_) { |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 595 | if (node == data_writer.second.node()) { |
| 596 | data_writer.second.Rotate(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 602 | void MultiNodeLogNamer::WriteConfiguration( |
| 603 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 604 | std::string_view config_sha256) { |
| 605 | if (ran_out_of_space_) { |
| 606 | return; |
| 607 | } |
| 608 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 609 | const std::string filename = absl::StrCat(config_sha256, ".bfbs", extension_); |
| 610 | auto file_handle = log_backend_->RequestFile(filename); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 611 | std::unique_ptr<DetachedBufferWriter> writer = |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 612 | std::make_unique<DetachedBufferWriter>( |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 613 | std::move(file_handle), encoder_factory_(header->span().size())); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 614 | |
Austin Schuh | 7ef11a4 | 2023-02-04 17:15:12 -0800 | [diff] [blame] | 615 | DataEncoder::SpanCopier coppier(header->span()); |
| 616 | writer->CopyMessage(&coppier, aos::monotonic_clock::now()); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 617 | |
| 618 | if (!writer->ran_out_of_space()) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 619 | all_filenames_.emplace_back(filename); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 620 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 621 | // Close the file and maybe rename it too. |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 622 | CloseWriter(&writer); |
| 623 | } |
| 624 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 625 | NewDataWriter *MultiNodeLogNamer::MakeWriter(const Channel *channel) { |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 626 | // See if we can read the data on this node at all. |
| 627 | const bool is_readable = |
| 628 | configuration::ChannelIsReadableOnNode(channel, this->node()); |
| 629 | if (!is_readable) { |
| 630 | return nullptr; |
| 631 | } |
| 632 | |
| 633 | // Then, see if we are supposed to log the data here. |
| 634 | const bool log_message = |
| 635 | configuration::ChannelMessageIsLoggedOnNode(channel, this->node()); |
| 636 | |
| 637 | if (!log_message) { |
| 638 | return nullptr; |
| 639 | } |
| 640 | |
| 641 | // Now, sort out if this is data generated on this node, or not. It is |
| 642 | // generated if it is sendable on this node. |
| 643 | if (configuration::ChannelIsSendableOnNode(channel, this->node())) { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 644 | if (!data_writer_) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 645 | MakeDataWriter(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 646 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 647 | data_writer_->UpdateMaxMessageSize( |
| 648 | PackMessageSize(LogType::kLogRemoteMessage, channel->max_size())); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 649 | return data_writer_.get(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // Ok, we have data that is being forwarded to us that we are supposed to |
| 653 | // log. It needs to be logged with send timestamps, but be sorted enough |
| 654 | // to be able to be processed. |
| 655 | CHECK(data_writers_.find(channel) == data_writers_.end()); |
| 656 | |
| 657 | // Track that this node is being logged. |
| 658 | const Node *source_node = configuration::GetNode( |
| 659 | configuration_, channel->source_node()->string_view()); |
| 660 | |
| 661 | if (std::find(nodes_.begin(), nodes_.end(), source_node) == nodes_.end()) { |
| 662 | nodes_.emplace_back(source_node); |
| 663 | } |
| 664 | |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 665 | NewDataWriter data_writer( |
| 666 | this, source_node, node_, |
| 667 | [this, channel](NewDataWriter *data_writer) { |
| 668 | OpenWriter(channel, data_writer); |
| 669 | }, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 670 | [this](NewDataWriter *data_writer) { CloseWriter(&data_writer->writer); }, |
| 671 | PackMessageSize(LogType::kLogRemoteMessage, channel->max_size())); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 672 | return &( |
| 673 | data_writers_.emplace(channel, std::move(data_writer)).first->second); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 676 | NewDataWriter *MultiNodeLogNamer::MakeForwardedTimestampWriter( |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 677 | const Channel *channel, const Node *node) { |
| 678 | // See if we can read the data on this node at all. |
| 679 | const bool is_readable = |
| 680 | configuration::ChannelIsReadableOnNode(channel, this->node()); |
| 681 | CHECK(is_readable) << ": " << configuration::CleanedChannelToString(channel); |
| 682 | |
| 683 | CHECK(data_writers_.find(channel) == data_writers_.end()); |
| 684 | |
| 685 | if (std::find(nodes_.begin(), nodes_.end(), node) == nodes_.end()) { |
| 686 | nodes_.emplace_back(node); |
| 687 | } |
| 688 | |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 689 | NewDataWriter data_writer( |
| 690 | this, configuration::GetNode(configuration_, node), node_, |
| 691 | [this, channel](NewDataWriter *data_writer) { |
| 692 | OpenForwardedTimestampWriter(channel, data_writer); |
| 693 | }, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 694 | [this](NewDataWriter *data_writer) { CloseWriter(&data_writer->writer); }, |
| 695 | PackRemoteMessageSize()); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 696 | return &( |
| 697 | data_writers_.emplace(channel, std::move(data_writer)).first->second); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 700 | NewDataWriter *MultiNodeLogNamer::MakeTimestampWriter(const Channel *channel) { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 701 | bool log_delivery_times = false; |
| 702 | if (this->node() != nullptr) { |
| 703 | log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 704 | channel, this->node(), this->node()); |
| 705 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 706 | if (!log_delivery_times) { |
| 707 | return nullptr; |
| 708 | } |
| 709 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 710 | if (!data_writer_) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 711 | MakeDataWriter(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 712 | } |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 713 | data_writer_->UpdateMaxMessageSize( |
| 714 | PackMessageSize(LogType::kLogDeliveryTimeOnly, 0)); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 715 | return data_writer_.get(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 718 | WriteCode MultiNodeLogNamer::Close() { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 719 | data_writers_.clear(); |
| 720 | data_writer_.reset(); |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 721 | if (ran_out_of_space_) { |
| 722 | return WriteCode::kOutOfSpace; |
| 723 | } |
| 724 | return WriteCode::kOk; |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void MultiNodeLogNamer::ResetStatistics() { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 728 | for (std::pair<const Channel *const, NewDataWriter> &data_writer : |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 729 | data_writers_) { |
Austin Schuh | ad0cfc3 | 2020-12-21 12:34:26 -0800 | [diff] [blame] | 730 | if (!data_writer.second.writer) continue; |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 731 | data_writer.second.writer->WriteStatistics()->ResetStats(); |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 732 | } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 733 | if (data_writer_) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 734 | data_writer_->writer->WriteStatistics()->ResetStats(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 735 | } |
| 736 | max_write_time_ = std::chrono::nanoseconds::zero(); |
| 737 | max_write_time_bytes_ = -1; |
| 738 | max_write_time_messages_ = -1; |
| 739 | total_write_time_ = std::chrono::nanoseconds::zero(); |
| 740 | total_write_count_ = 0; |
| 741 | total_write_messages_ = 0; |
| 742 | total_write_bytes_ = 0; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 745 | void MultiNodeLogNamer::OpenForwardedTimestampWriter( |
| 746 | const Channel *channel, NewDataWriter *data_writer) { |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 747 | std::string filename = |
Austin Schuh | e715eae | 2020-10-10 15:39:30 -0700 | [diff] [blame] | 748 | absl::StrCat("timestamps", channel->name()->string_view(), "/", |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 749 | channel->type()->string_view(), ".part", |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 750 | data_writer->parts_index(), ".bfbs", extension_); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 751 | CreateBufferWriter(filename, data_writer->max_message_size(), |
| 752 | &data_writer->writer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void MultiNodeLogNamer::OpenWriter(const Channel *channel, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 756 | NewDataWriter *data_writer) { |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 757 | const std::string filename = absl::StrCat( |
Austin Schuh | e715eae | 2020-10-10 15:39:30 -0700 | [diff] [blame] | 758 | CHECK_NOTNULL(channel->source_node())->string_view(), "_data", |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 759 | channel->name()->string_view(), "/", channel->type()->string_view(), |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 760 | ".part", data_writer->parts_index(), ".bfbs", extension_); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 761 | CreateBufferWriter(filename, data_writer->max_message_size(), |
| 762 | &data_writer->writer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 765 | void MultiNodeLogNamer::MakeDataWriter() { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 766 | if (!data_writer_) { |
| 767 | data_writer_ = std::make_unique<NewDataWriter>( |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 768 | this, node_, node_, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 769 | [this](NewDataWriter *writer) { |
| 770 | std::string name; |
| 771 | if (node() != nullptr) { |
| 772 | name = absl::StrCat(name, node()->name()->string_view(), "_"); |
| 773 | } |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 774 | absl::StrAppend(&name, "data.part", writer->parts_index(), ".bfbs", |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 775 | extension_); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 776 | CreateBufferWriter(name, writer->max_message_size(), &writer->writer); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 777 | }, |
| 778 | [this](NewDataWriter *data_writer) { |
| 779 | CloseWriter(&data_writer->writer); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 780 | }, |
| 781 | // Default size is 0 so it will be obvious if something doesn't fix it |
| 782 | // afterwards. |
| 783 | 0); |
Brian Silverman | 7af8c90 | 2020-09-29 16:14:04 -0700 | [diff] [blame] | 784 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 787 | void MultiNodeLogNamer::CreateBufferWriter( |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 788 | std::string_view path, size_t max_message_size, |
| 789 | std::unique_ptr<DetachedBufferWriter> *destination) { |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 790 | if (ran_out_of_space_) { |
| 791 | // Refuse to open any new files, which might skip data. Any existing files |
| 792 | // are in the same folder, which means they're on the same filesystem, which |
| 793 | // means they're probably going to run out of space and get stuck too. |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 794 | if (!(*destination)) { |
Austin Schuh | a426f1f | 2021-03-31 22:27:41 -0700 | [diff] [blame] | 795 | // But avoid leaving a nullptr writer if we're out of space when |
| 796 | // attempting to open the first file. |
| 797 | *destination = std::make_unique<DetachedBufferWriter>( |
| 798 | DetachedBufferWriter::already_out_of_space_t()); |
| 799 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 800 | return; |
| 801 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 802 | |
| 803 | // Let's check that we need to close and replace current driver. |
| 804 | if (*destination) { |
| 805 | // Let's close the current writer. |
| 806 | CloseWriter(destination); |
| 807 | // Are we out of space now? |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 808 | if (ran_out_of_space_) { |
| 809 | *destination = std::make_unique<DetachedBufferWriter>( |
| 810 | DetachedBufferWriter::already_out_of_space_t()); |
| 811 | return; |
| 812 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 813 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 814 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 815 | const std::string filename(path); |
| 816 | *destination = std::make_unique<DetachedBufferWriter>( |
| 817 | log_backend_->RequestFile(filename), encoder_factory_(max_message_size)); |
| 818 | if (!(*destination)->ran_out_of_space()) { |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 819 | all_filenames_.emplace_back(path); |
| 820 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 823 | void MultiNodeLogNamer::CloseWriter( |
| 824 | std::unique_ptr<DetachedBufferWriter> *writer_pointer) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 825 | CHECK_NOTNULL(writer_pointer); |
| 826 | if (!(*writer_pointer)) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 827 | return; |
| 828 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 829 | DetachedBufferWriter *const writer = writer_pointer->get(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 830 | writer->Close(); |
| 831 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 832 | const auto *stats = writer->WriteStatistics(); |
| 833 | if (stats->max_write_time() > max_write_time_) { |
| 834 | max_write_time_ = stats->max_write_time(); |
| 835 | max_write_time_bytes_ = stats->max_write_time_bytes(); |
| 836 | max_write_time_messages_ = stats->max_write_time_messages(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 837 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 838 | total_write_time_ += stats->total_write_time(); |
| 839 | total_write_count_ += stats->total_write_count(); |
| 840 | total_write_messages_ += stats->total_write_messages(); |
| 841 | total_write_bytes_ += stats->total_write_bytes(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 842 | |
| 843 | if (writer->ran_out_of_space()) { |
| 844 | ran_out_of_space_ = true; |
| 845 | writer->acknowledge_out_of_space(); |
| 846 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 849 | } // namespace logger |
| 850 | } // namespace aos |