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