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