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