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