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