blob: 120bd795d3119680ec8094c2238aca202b5d6f85 [file] [log] [blame]
Austin Schuhcb5601b2020-09-10 15:29:59 -07001#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 Schuh73340842021-07-30 22:32:06 -070012#include "aos/flatbuffer_merge.h"
Austin Schuh4385b142021-03-14 21:31:13 -070013#include "aos/uuid.h"
Austin Schuhcb5601b2020-09-10 15:29:59 -070014#include "flatbuffers/flatbuffers.h"
15#include "glog/logging.h"
16
17namespace aos {
18namespace logger {
19
Austin Schuh572924a2021-07-30 22:32:12 -070020NewDataWriter::NewDataWriter(LogNamer *log_namer, const Node *node,
Austin Schuhf5f99f32022-02-07 20:05:37 -080021 const Node *logger_node,
Austin Schuh572924a2021-07-30 22:32:12 -070022 std::function<void(NewDataWriter *)> reopen,
Austin Schuh48d10d62022-10-16 22:19:23 -070023 std::function<void(NewDataWriter *)> close,
24 size_t max_message_size)
Austin Schuh572924a2021-07-30 22:32:12 -070025 : node_(node),
26 node_index_(configuration::GetNodeIndex(log_namer->configuration_, node)),
Austin Schuhf5f99f32022-02-07 20:05:37 -080027 logger_node_index_(
28 configuration::GetNodeIndex(log_namer->configuration_, logger_node)),
Austin Schuh572924a2021-07-30 22:32:12 -070029 log_namer_(log_namer),
30 reopen_(std::move(reopen)),
Austin Schuh48d10d62022-10-16 22:19:23 -070031 close_(std::move(close)),
32 max_message_size_(max_message_size) {
Austin Schuh72211ae2021-08-05 14:02:30 -070033 state_.resize(configuration::NodesCount(log_namer->configuration_));
34 CHECK_LT(node_index_, state_.size());
Austin Schuh572924a2021-07-30 22:32:12 -070035}
36
37NewDataWriter::~NewDataWriter() {
38 if (writer) {
39 Close();
40 }
41}
42
43void NewDataWriter::Rotate() {
Austin Schuhe46492f2021-07-31 19:49:41 -070044 // No need to rotate if nothing has been written.
45 if (header_written_) {
Austin Schuh58646e22021-08-23 23:51:46 -070046 VLOG(1) << "Rotated " << filename();
Austin Schuhe46492f2021-07-31 19:49:41 -070047 ++parts_index_;
48 reopen_(this);
49 header_written_ = false;
50 QueueHeader(MakeHeader());
51 }
Austin Schuh572924a2021-07-30 22:32:12 -070052}
53
Austin Schuh5e14d842022-01-21 12:02:15 -080054void NewDataWriter::Reboot(const UUID &source_node_boot_uuid) {
Austin Schuh572924a2021-07-30 22:32:12 -070055 parts_uuid_ = UUID::Random();
56 ++parts_index_;
57 reopen_(this);
58 header_written_ = false;
Austin Schuh5e14d842022-01-21 12:02:15 -080059 for (State &state : state_) {
60 state.boot_uuid = UUID::Zero();
61 state.oldest_remote_monotonic_timestamp = monotonic_clock::max_time;
62 state.oldest_local_monotonic_timestamp = monotonic_clock::max_time;
63 state.oldest_remote_unreliable_monotonic_timestamp =
64 monotonic_clock::max_time;
65 state.oldest_local_unreliable_monotonic_timestamp =
66 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -080067 state.oldest_remote_reliable_monotonic_timestamp =
68 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -080069 state.oldest_local_reliable_monotonic_timestamp = monotonic_clock::max_time;
70 state.oldest_logger_remote_unreliable_monotonic_timestamp =
71 monotonic_clock::max_time;
72 state.oldest_logger_local_unreliable_monotonic_timestamp =
Austin Schuhbfe6c572022-01-27 20:48:20 -080073 monotonic_clock::max_time;
Austin Schuh5e14d842022-01-21 12:02:15 -080074 }
75
76 state_[node_index_].boot_uuid = source_node_boot_uuid;
77
78 VLOG(1) << "Rebooted " << filename();
79}
80
81void NewDataWriter::UpdateBoot(const UUID &source_node_boot_uuid) {
82 if (state_[node_index_].boot_uuid != source_node_boot_uuid) {
83 state_[node_index_].boot_uuid = source_node_boot_uuid;
84 if (header_written_) {
85 Reboot(source_node_boot_uuid);
86 }
87 }
Austin Schuh572924a2021-07-30 22:32:12 -070088}
89
Austin Schuh72211ae2021-08-05 14:02:30 -070090void NewDataWriter::UpdateRemote(
91 const size_t remote_node_index, const UUID &remote_node_boot_uuid,
92 const monotonic_clock::time_point monotonic_remote_time,
Austin Schuhf5f99f32022-02-07 20:05:37 -080093 const monotonic_clock::time_point monotonic_event_time, const bool reliable,
94 monotonic_clock::time_point monotonic_timestamp_time) {
Austin Schuh58646e22021-08-23 23:51:46 -070095 // Trigger rotation if anything in the header changes.
Austin Schuh72211ae2021-08-05 14:02:30 -070096 bool rotate = false;
97 CHECK_LT(remote_node_index, state_.size());
98 State &state = state_[remote_node_index];
Austin Schuh58646e22021-08-23 23:51:46 -070099
100 // Did the remote boot UUID change?
Austin Schuh72211ae2021-08-05 14:02:30 -0700101 if (state.boot_uuid != remote_node_boot_uuid) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700102 VLOG(1) << filename() << " Remote " << remote_node_index << " updated to "
Austin Schuh72211ae2021-08-05 14:02:30 -0700103 << remote_node_boot_uuid << " from " << state.boot_uuid;
104 state.boot_uuid = remote_node_boot_uuid;
105 state.oldest_remote_monotonic_timestamp = monotonic_clock::max_time;
106 state.oldest_local_monotonic_timestamp = monotonic_clock::max_time;
107 state.oldest_remote_unreliable_monotonic_timestamp =
108 monotonic_clock::max_time;
109 state.oldest_local_unreliable_monotonic_timestamp =
110 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -0800111 state.oldest_remote_reliable_monotonic_timestamp =
112 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800113 state.oldest_local_reliable_monotonic_timestamp = monotonic_clock::max_time;
114 state.oldest_logger_remote_unreliable_monotonic_timestamp =
115 monotonic_clock::max_time;
116 state.oldest_logger_local_unreliable_monotonic_timestamp =
Austin Schuhbfe6c572022-01-27 20:48:20 -0800117 monotonic_clock::max_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700118 rotate = true;
119 }
120
Austin Schuh58646e22021-08-23 23:51:46 -0700121 // Did the unreliable timestamps change?
Austin Schuh72211ae2021-08-05 14:02:30 -0700122 if (!reliable) {
123 if (state.oldest_remote_unreliable_monotonic_timestamp >
124 monotonic_remote_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700125 VLOG(1) << filename() << " Remote " << remote_node_index
126 << " oldest_remote_unreliable_monotonic_timestamp updated from "
127 << state.oldest_remote_unreliable_monotonic_timestamp << " to "
128 << monotonic_remote_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700129 state.oldest_remote_unreliable_monotonic_timestamp =
130 monotonic_remote_time;
131 state.oldest_local_unreliable_monotonic_timestamp = monotonic_event_time;
132 rotate = true;
133 }
Austin Schuhbfe6c572022-01-27 20:48:20 -0800134 } else {
135 if (state.oldest_remote_reliable_monotonic_timestamp >
136 monotonic_remote_time) {
137 VLOG(1) << filename() << " Remote " << remote_node_index
138 << " oldest_remote_reliable_monotonic_timestamp updated from "
139 << state.oldest_remote_reliable_monotonic_timestamp << " to "
140 << monotonic_remote_time;
141 state.oldest_remote_reliable_monotonic_timestamp = monotonic_remote_time;
142 state.oldest_local_reliable_monotonic_timestamp = monotonic_event_time;
143 rotate = true;
144 }
Austin Schuh72211ae2021-08-05 14:02:30 -0700145 }
146
Austin Schuhf5f99f32022-02-07 20:05:37 -0800147 // Track the logger timestamps too.
148 if (monotonic_timestamp_time != monotonic_clock::min_time) {
149 State &logger_state = state_[node_index_];
150 CHECK_EQ(remote_node_index, logger_node_index_);
151 if (monotonic_event_time <
152 logger_state.oldest_logger_remote_unreliable_monotonic_timestamp) {
153 VLOG(1)
154 << filename() << " Remote " << node_index_
155 << " oldest_logger_remote_unreliable_monotonic_timestamp updated "
156 "from "
157 << logger_state.oldest_logger_remote_unreliable_monotonic_timestamp
158 << " to " << monotonic_event_time;
159 logger_state.oldest_logger_remote_unreliable_monotonic_timestamp =
160 monotonic_event_time;
161 logger_state.oldest_logger_local_unreliable_monotonic_timestamp =
162 monotonic_timestamp_time;
163
164 rotate = true;
165 }
166 }
167
Austin Schuh58646e22021-08-23 23:51:46 -0700168 // Did any of the timestamps change?
Austin Schuh72211ae2021-08-05 14:02:30 -0700169 if (state.oldest_remote_monotonic_timestamp > monotonic_remote_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700170 VLOG(1) << filename() << " Remote " << remote_node_index
171 << " oldest_remote_monotonic_timestamp updated from "
172 << state.oldest_remote_monotonic_timestamp << " to "
173 << monotonic_remote_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700174 state.oldest_remote_monotonic_timestamp = monotonic_remote_time;
175 state.oldest_local_monotonic_timestamp = monotonic_event_time;
176 rotate = true;
177 }
178
179 if (rotate) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700180 Rotate();
181 }
182}
183
Austin Schuh48d10d62022-10-16 22:19:23 -0700184void NewDataWriter::CopyMessage(DataEncoder::Copier *coppier,
185 const UUID &source_node_boot_uuid,
186 aos::monotonic_clock::time_point now) {
Austin Schuh58646e22021-08-23 23:51:46 -0700187 // Trigger a reboot if we detect the boot UUID change.
Austin Schuh5e14d842022-01-21 12:02:15 -0800188 UpdateBoot(source_node_boot_uuid);
Austin Schuh572924a2021-07-30 22:32:12 -0700189
Austin Schuh5e14d842022-01-21 12:02:15 -0800190 if (!header_written_) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700191 QueueHeader(MakeHeader());
Austin Schuh572924a2021-07-30 22:32:12 -0700192 }
Austin Schuh58646e22021-08-23 23:51:46 -0700193
194 // If the start time has changed for this node, trigger a rotation.
195 if (log_namer_->monotonic_start_time(node_index_, source_node_boot_uuid) !=
Austin Schuh5e14d842022-01-21 12:02:15 -0800196 monotonic_start_time_) {
Austin Schuh58646e22021-08-23 23:51:46 -0700197 CHECK(header_written_);
198 Rotate();
199 }
200
201 CHECK_EQ(log_namer_->monotonic_start_time(node_index_, source_node_boot_uuid),
202 monotonic_start_time_);
Austin Schuh72211ae2021-08-05 14:02:30 -0700203 CHECK_EQ(state_[node_index_].boot_uuid, source_node_boot_uuid);
milind-ua50344f2021-08-25 18:22:20 -0700204 CHECK(writer);
Austin Schuh572924a2021-07-30 22:32:12 -0700205 CHECK(header_written_) << ": Attempting to write message before header to "
206 << writer->filename();
Austin Schuh48d10d62022-10-16 22:19:23 -0700207 writer->CopyMessage(coppier, now);
Austin Schuh572924a2021-07-30 22:32:12 -0700208}
209
Austin Schuhe46492f2021-07-31 19:49:41 -0700210aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>
211NewDataWriter::MakeHeader() {
212 const size_t logger_node_index = log_namer_->logger_node_index();
213 const UUID &logger_node_boot_uuid = log_namer_->logger_node_boot_uuid();
Austin Schuh72211ae2021-08-05 14:02:30 -0700214 if (state_[logger_node_index].boot_uuid == UUID::Zero()) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700215 VLOG(1) << filename() << " Logger node is " << logger_node_index
216 << " and uuid is " << logger_node_boot_uuid;
Austin Schuh72211ae2021-08-05 14:02:30 -0700217 state_[logger_node_index].boot_uuid = logger_node_boot_uuid;
Austin Schuhe46492f2021-07-31 19:49:41 -0700218 } else {
Austin Schuh72211ae2021-08-05 14:02:30 -0700219 CHECK_EQ(state_[logger_node_index].boot_uuid, logger_node_boot_uuid);
Austin Schuhe46492f2021-07-31 19:49:41 -0700220 }
Austin Schuh72211ae2021-08-05 14:02:30 -0700221 return log_namer_->MakeHeader(node_index_, state_, parts_uuid(),
Austin Schuhe46492f2021-07-31 19:49:41 -0700222 parts_index_);
223}
224
Austin Schuh572924a2021-07-30 22:32:12 -0700225void NewDataWriter::QueueHeader(
226 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header) {
227 CHECK(!header_written_) << ": Attempting to write duplicate header to "
228 << writer->filename();
229 CHECK(header.message().has_source_node_boot_uuid());
Austin Schuh72211ae2021-08-05 14:02:30 -0700230 CHECK_EQ(state_[node_index_].boot_uuid,
Austin Schuhe46492f2021-07-31 19:49:41 -0700231 UUID::FromString(header.message().source_node_boot_uuid()));
Austin Schuh510dc622021-08-06 18:47:30 -0700232 if (!writer) {
Austin Schuh48d10d62022-10-16 22:19:23 -0700233 // Since we haven't opened the first time, it's still not too late to update
234 // the max message size. Make sure the header fits.
235 //
236 // This won't work well on reboots, but the structure of the header is fixed
237 // by that point in time, so it's size is fixed too.
238 //
239 // Most of the time, the minimum buffer size inside the encoder of around
240 // 128k will make this a non-issue.
241 UpdateMaxMessageSize(header.span().size());
242
Austin Schuh510dc622021-08-06 18:47:30 -0700243 reopen_(this);
244 }
245
Austin Schuh58646e22021-08-23 23:51:46 -0700246 VLOG(1) << "Writing to " << filename() << " "
247 << aos::FlatbufferToJson(
248 header, {.multi_line = false, .max_vector_size = 100});
249
Austin Schuh510dc622021-08-06 18:47:30 -0700250 CHECK(writer);
Austin Schuh48d10d62022-10-16 22:19:23 -0700251 writer->QueueSpan(header.span());
Austin Schuh572924a2021-07-30 22:32:12 -0700252 header_written_ = true;
Austin Schuh58646e22021-08-23 23:51:46 -0700253 monotonic_start_time_ = log_namer_->monotonic_start_time(
254 node_index_, state_[node_index_].boot_uuid);
Austin Schuh572924a2021-07-30 22:32:12 -0700255}
256
257void NewDataWriter::Close() {
258 CHECK(writer);
259 close_(this);
260 writer.reset();
261 header_written_ = false;
262}
263
Austin Schuh58646e22021-08-23 23:51:46 -0700264LogNamer::NodeState *LogNamer::GetNodeState(size_t node_index,
265 const UUID &boot_uuid) {
266 auto it = node_states_.find(std::make_pair(node_index, boot_uuid));
267 if (it == node_states_.end()) {
268 it =
269 node_states_.emplace(std::make_pair(node_index, boot_uuid), NodeState())
270 .first;
271 }
272 return &it->second;
273}
274
Austin Schuh73340842021-07-30 22:32:06 -0700275aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> LogNamer::MakeHeader(
Austin Schuh72211ae2021-08-05 14:02:30 -0700276 size_t node_index, const std::vector<NewDataWriter::State> &state,
Austin Schuh58646e22021-08-23 23:51:46 -0700277 const UUID &parts_uuid, int parts_index) {
Austin Schuh72211ae2021-08-05 14:02:30 -0700278 const UUID &source_node_boot_uuid = state[node_index].boot_uuid;
Austin Schuh73340842021-07-30 22:32:06 -0700279 const Node *const source_node =
280 configuration::GetNode(configuration_, node_index);
Austin Schuhfa712682022-05-11 16:43:42 -0700281 CHECK_EQ(LogFileHeader::MiniReflectTypeTable()->num_elems, 34u);
Austin Schuh73340842021-07-30 22:32:06 -0700282 flatbuffers::FlatBufferBuilder fbb;
283 fbb.ForceDefaults(true);
284
285 flatbuffers::Offset<flatbuffers::String> config_sha256_offset;
286 flatbuffers::Offset<aos::Configuration> configuration_offset;
287 if (header_.message().has_configuration()) {
288 CHECK(!header_.message().has_configuration_sha256());
289 configuration_offset =
290 CopyFlatBuffer(header_.message().configuration(), &fbb);
291 } else {
292 CHECK(!header_.message().has_configuration());
293 CHECK(header_.message().has_configuration_sha256());
294 config_sha256_offset = fbb.CreateString(
295 header_.message().configuration_sha256()->string_view());
296 }
297
298 CHECK(header_.message().has_name());
299 const flatbuffers::Offset<flatbuffers::String> name_offset =
300 fbb.CreateString(header_.message().name()->string_view());
Austin Schuhfa712682022-05-11 16:43:42 -0700301 const flatbuffers::Offset<flatbuffers::String> logger_sha1_offset =
302 header_.message().has_logger_sha1()
303 ? fbb.CreateString(header_.message().logger_sha1()->string_view())
304 : 0;
305 const flatbuffers::Offset<flatbuffers::String> logger_version_offset =
306 header_.message().has_logger_version()
307 ? fbb.CreateString(header_.message().logger_version()->string_view())
308 : 0;
Austin Schuh73340842021-07-30 22:32:06 -0700309
310 CHECK(header_.message().has_log_event_uuid());
311 const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset =
312 fbb.CreateString(header_.message().log_event_uuid()->string_view());
313
314 CHECK(header_.message().has_logger_instance_uuid());
315 const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset =
316 fbb.CreateString(header_.message().logger_instance_uuid()->string_view());
317
318 flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset;
319 if (header_.message().has_log_start_uuid()) {
320 log_start_uuid_offset =
321 fbb.CreateString(header_.message().log_start_uuid()->string_view());
322 }
323
324 CHECK(header_.message().has_logger_node_boot_uuid());
325 const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset =
326 fbb.CreateString(
327 header_.message().logger_node_boot_uuid()->string_view());
328
329 CHECK_NE(source_node_boot_uuid, UUID::Zero());
330 const flatbuffers::Offset<flatbuffers::String> source_node_boot_uuid_offset =
331 source_node_boot_uuid.PackString(&fbb);
332
333 const flatbuffers::Offset<flatbuffers::String> parts_uuid_offset =
334 parts_uuid.PackString(&fbb);
335
336 flatbuffers::Offset<Node> node_offset;
337 flatbuffers::Offset<Node> logger_node_offset;
338
339 if (configuration::MultiNode(configuration_)) {
340 node_offset = RecursiveCopyFlatBuffer(source_node, &fbb);
341 logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb);
342 }
343
Austin Schuhe46492f2021-07-31 19:49:41 -0700344 std::vector<flatbuffers::Offset<flatbuffers::String>> boot_uuid_offsets;
Austin Schuh72211ae2021-08-05 14:02:30 -0700345 boot_uuid_offsets.reserve(state.size());
Austin Schuhe46492f2021-07-31 19:49:41 -0700346
Austin Schuh4db9ec92021-09-22 13:11:12 -0700347 int64_t *unused;
Austin Schuh72211ae2021-08-05 14:02:30 -0700348 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
Austin Schuhf5f99f32022-02-07 20:05:37 -0800349 oldest_remote_monotonic_timestamps_offset =
350 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700351
Austin Schuh72211ae2021-08-05 14:02:30 -0700352 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
Austin Schuhf5f99f32022-02-07 20:05:37 -0800353 oldest_local_monotonic_timestamps_offset =
354 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700355
Austin Schuh72211ae2021-08-05 14:02:30 -0700356 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
357 oldest_remote_unreliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800358 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700359
Austin Schuh72211ae2021-08-05 14:02:30 -0700360 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
361 oldest_local_unreliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800362 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700363
Austin Schuhbfe6c572022-01-27 20:48:20 -0800364 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
365 oldest_remote_reliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800366 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800367
368 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
369 oldest_local_reliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800370 fbb.CreateUninitializedVector(state.size(), &unused);
371
372 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
373 oldest_logger_remote_unreliable_monotonic_timestamps_offset =
374 fbb.CreateUninitializedVector(state.size(), &unused);
375
376 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
377 oldest_logger_local_unreliable_monotonic_timestamps_offset =
378 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800379
Austin Schuh72211ae2021-08-05 14:02:30 -0700380 for (size_t i = 0; i < state.size(); ++i) {
Austin Schuh4db9ec92021-09-22 13:11:12 -0700381 if (state[i].boot_uuid != UUID::Zero()) {
382 boot_uuid_offsets.emplace_back(state[i].boot_uuid.PackString(&fbb));
383 } else {
384 boot_uuid_offsets.emplace_back(fbb.CreateString(""));
385 }
Austin Schuh5ae8f4a2021-09-11 19:09:50 -0700386 if (state[i].boot_uuid == UUID::Zero()) {
387 CHECK_EQ(state[i].oldest_remote_monotonic_timestamp,
388 monotonic_clock::max_time);
389 CHECK_EQ(state[i].oldest_local_monotonic_timestamp,
390 monotonic_clock::max_time);
391 CHECK_EQ(state[i].oldest_remote_unreliable_monotonic_timestamp,
392 monotonic_clock::max_time);
393 CHECK_EQ(state[i].oldest_local_unreliable_monotonic_timestamp,
394 monotonic_clock::max_time);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800395 CHECK_EQ(state[i].oldest_remote_reliable_monotonic_timestamp,
396 monotonic_clock::max_time);
397 CHECK_EQ(state[i].oldest_local_reliable_monotonic_timestamp,
398 monotonic_clock::max_time);
Austin Schuhf5f99f32022-02-07 20:05:37 -0800399 CHECK_EQ(state[i].oldest_logger_remote_unreliable_monotonic_timestamp,
400 monotonic_clock::max_time);
401 CHECK_EQ(state[i].oldest_logger_local_unreliable_monotonic_timestamp,
402 monotonic_clock::max_time);
Austin Schuh5ae8f4a2021-09-11 19:09:50 -0700403 }
404
Austin Schuh4db9ec92021-09-22 13:11:12 -0700405 flatbuffers::GetMutableTemporaryPointer(
406 fbb, oldest_remote_monotonic_timestamps_offset)
407 ->Mutate(i, state[i]
408 .oldest_remote_monotonic_timestamp.time_since_epoch()
409 .count());
410 flatbuffers::GetMutableTemporaryPointer(
411 fbb, oldest_local_monotonic_timestamps_offset)
412 ->Mutate(i, state[i]
413 .oldest_local_monotonic_timestamp.time_since_epoch()
414 .count());
415 flatbuffers::GetMutableTemporaryPointer(
416 fbb, oldest_remote_unreliable_monotonic_timestamps_offset)
417 ->Mutate(i, state[i]
Austin Schuhbfe6c572022-01-27 20:48:20 -0800418 .oldest_remote_unreliable_monotonic_timestamp
419 .time_since_epoch()
Austin Schuh4db9ec92021-09-22 13:11:12 -0700420 .count());
421 flatbuffers::GetMutableTemporaryPointer(
422 fbb, oldest_local_unreliable_monotonic_timestamps_offset)
423 ->Mutate(i, state[i]
Austin Schuhbfe6c572022-01-27 20:48:20 -0800424 .oldest_local_unreliable_monotonic_timestamp
425 .time_since_epoch()
Austin Schuh4db9ec92021-09-22 13:11:12 -0700426 .count());
Austin Schuhbfe6c572022-01-27 20:48:20 -0800427
428 flatbuffers::GetMutableTemporaryPointer(
429 fbb, oldest_remote_reliable_monotonic_timestamps_offset)
430 ->Mutate(i, state[i]
431 .oldest_remote_reliable_monotonic_timestamp
432 .time_since_epoch()
433 .count());
434 flatbuffers::GetMutableTemporaryPointer(
435 fbb, oldest_local_reliable_monotonic_timestamps_offset)
436 ->Mutate(
437 i, state[i]
438 .oldest_local_reliable_monotonic_timestamp.time_since_epoch()
439 .count());
Austin Schuhf5f99f32022-02-07 20:05:37 -0800440
441 flatbuffers::GetMutableTemporaryPointer(
442 fbb, oldest_logger_remote_unreliable_monotonic_timestamps_offset)
443 ->Mutate(i, state[i]
444 .oldest_logger_remote_unreliable_monotonic_timestamp
445 .time_since_epoch()
446 .count());
447 flatbuffers::GetMutableTemporaryPointer(
448 fbb, oldest_logger_local_unreliable_monotonic_timestamps_offset)
449 ->Mutate(i, state[i]
450 .oldest_logger_local_unreliable_monotonic_timestamp
451 .time_since_epoch()
452 .count());
Austin Schuh72211ae2021-08-05 14:02:30 -0700453 }
454
Austin Schuh4db9ec92021-09-22 13:11:12 -0700455 flatbuffers::Offset<
456 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
457 boot_uuids_offset = fbb.CreateVector(boot_uuid_offsets);
458
Austin Schuh73340842021-07-30 22:32:06 -0700459 aos::logger::LogFileHeader::Builder log_file_header_builder(fbb);
460
461 log_file_header_builder.add_name(name_offset);
Austin Schuhfa712682022-05-11 16:43:42 -0700462 if (!logger_sha1_offset.IsNull()) {
463 log_file_header_builder.add_logger_sha1(logger_sha1_offset);
464 }
465 if (!logger_version_offset.IsNull()) {
466 log_file_header_builder.add_logger_version(logger_version_offset);
467 }
Austin Schuh73340842021-07-30 22:32:06 -0700468
469 // Only add the node if we are running in a multinode configuration.
470 if (!logger_node_offset.IsNull()) {
471 log_file_header_builder.add_node(node_offset);
472 log_file_header_builder.add_logger_node(logger_node_offset);
473 }
474
475 if (!configuration_offset.IsNull()) {
476 log_file_header_builder.add_configuration(configuration_offset);
477 }
478 log_file_header_builder.add_max_out_of_order_duration(
479 header_.message().max_out_of_order_duration());
480
Austin Schuh58646e22021-08-23 23:51:46 -0700481 NodeState *node_state = GetNodeState(node_index, source_node_boot_uuid);
Austin Schuh73340842021-07-30 22:32:06 -0700482 log_file_header_builder.add_monotonic_start_time(
483 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700484 node_state->monotonic_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700485 .count());
486 if (source_node == node_) {
487 log_file_header_builder.add_realtime_start_time(
488 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700489 node_state->realtime_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700490 .count());
491 } else {
492 // Fill out the legacy start times. Since these were implemented to never
493 // change on reboot, they aren't very helpful in tracking what happened.
494 log_file_header_builder.add_logger_monotonic_start_time(
495 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700496 node_state->logger_monotonic_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700497 .count());
498 log_file_header_builder.add_logger_realtime_start_time(
499 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700500 node_state->logger_realtime_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700501 .count());
502 }
503
504 // TODO(austin): Add more useful times. When was this part started? What do
505 // we know about both the logger and remote then?
506
507 log_file_header_builder.add_log_event_uuid(log_event_uuid_offset);
508 log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset);
509 if (!log_start_uuid_offset.IsNull()) {
510 log_file_header_builder.add_log_start_uuid(log_start_uuid_offset);
511 }
512 log_file_header_builder.add_logger_node_boot_uuid(
513 logger_node_boot_uuid_offset);
514 log_file_header_builder.add_source_node_boot_uuid(
515 source_node_boot_uuid_offset);
516
517 log_file_header_builder.add_parts_uuid(parts_uuid_offset);
518 log_file_header_builder.add_parts_index(parts_index);
519
520 if (!config_sha256_offset.IsNull()) {
521 log_file_header_builder.add_configuration_sha256(config_sha256_offset);
522 }
523
Austin Schuhe46492f2021-07-31 19:49:41 -0700524 log_file_header_builder.add_boot_uuids(boot_uuids_offset);
Austin Schuha499cea2021-07-31 19:49:53 -0700525 log_file_header_builder.add_logger_part_monotonic_start_time(
526 std::chrono::duration_cast<std::chrono::nanoseconds>(
527 event_loop_->monotonic_now().time_since_epoch())
528 .count());
529 log_file_header_builder.add_logger_part_realtime_start_time(
530 std::chrono::duration_cast<std::chrono::nanoseconds>(
531 event_loop_->realtime_now().time_since_epoch())
532 .count());
Austin Schuh72211ae2021-08-05 14:02:30 -0700533 log_file_header_builder.add_oldest_remote_monotonic_timestamps(
534 oldest_remote_monotonic_timestamps_offset);
535 log_file_header_builder.add_oldest_local_monotonic_timestamps(
536 oldest_local_monotonic_timestamps_offset);
537 log_file_header_builder.add_oldest_remote_unreliable_monotonic_timestamps(
538 oldest_remote_unreliable_monotonic_timestamps_offset);
539 log_file_header_builder.add_oldest_local_unreliable_monotonic_timestamps(
540 oldest_local_unreliable_monotonic_timestamps_offset);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800541 log_file_header_builder.add_oldest_remote_reliable_monotonic_timestamps(
542 oldest_remote_reliable_monotonic_timestamps_offset);
543 log_file_header_builder.add_oldest_local_reliable_monotonic_timestamps(
544 oldest_local_reliable_monotonic_timestamps_offset);
Austin Schuhf5f99f32022-02-07 20:05:37 -0800545 log_file_header_builder
546 .add_oldest_logger_remote_unreliable_monotonic_timestamps(
547 oldest_logger_remote_unreliable_monotonic_timestamps_offset);
548 log_file_header_builder
549 .add_oldest_logger_local_unreliable_monotonic_timestamps(
550 oldest_logger_local_unreliable_monotonic_timestamps_offset);
Austin Schuh73340842021-07-30 22:32:06 -0700551 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
552 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result(
553 fbb.Release());
554
555 CHECK(result.Verify()) << ": Built a corrupted header.";
556
557 return result;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700558}
559
Austin Schuhcb5601b2020-09-10 15:29:59 -0700560MultiNodeLogNamer::MultiNodeLogNamer(std::string_view base_name,
Austin Schuha499cea2021-07-31 19:49:53 -0700561 EventLoop *event_loop)
Austin Schuh5b728b72021-06-16 14:57:15 -0700562 : MultiNodeLogNamer(base_name, event_loop->configuration(), event_loop,
563 event_loop->node()) {}
564
565MultiNodeLogNamer::MultiNodeLogNamer(std::string_view base_name,
566 const Configuration *configuration,
567 EventLoop *event_loop, const Node *node)
568 : LogNamer(configuration, event_loop, node),
569 base_name_(base_name),
570 old_base_name_() {}
Austin Schuhcb5601b2020-09-10 15:29:59 -0700571
Brian Silverman48deab12020-09-30 18:39:28 -0700572MultiNodeLogNamer::~MultiNodeLogNamer() {
573 if (!ran_out_of_space_) {
574 // This handles renaming temporary files etc.
575 Close();
576 }
577}
578
Austin Schuh572924a2021-07-30 22:32:12 -0700579void MultiNodeLogNamer::Rotate(const Node *node) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700580 if (node == this->node()) {
Austin Schuhb8bca732021-07-30 22:32:00 -0700581 if (data_writer_) {
Austin Schuh572924a2021-07-30 22:32:12 -0700582 data_writer_->Rotate();
Brian Silvermancb805822020-10-06 17:43:35 -0700583 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700584 } else {
Austin Schuhb8bca732021-07-30 22:32:00 -0700585 for (std::pair<const Channel *const, NewDataWriter> &data_writer :
Austin Schuhcb5601b2020-09-10 15:29:59 -0700586 data_writers_) {
Austin Schuh572924a2021-07-30 22:32:12 -0700587 if (node == data_writer.second.node()) {
588 data_writer.second.Rotate();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700589 }
590 }
591 }
592}
593
Austin Schuh8c399962020-12-25 21:51:45 -0800594void MultiNodeLogNamer::WriteConfiguration(
595 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
596 std::string_view config_sha256) {
597 if (ran_out_of_space_) {
598 return;
599 }
600
601 const std::string_view separator = base_name_.back() == '/' ? "" : "_";
602 const std::string filename = absl::StrCat(
603 base_name_, separator, config_sha256, ".bfbs", extension_, temp_suffix_);
604
605 std::unique_ptr<DetachedBufferWriter> writer =
Austin Schuh48d10d62022-10-16 22:19:23 -0700606 std::make_unique<DetachedBufferWriter>(
607 filename, encoder_factory_(header->span().size()));
Austin Schuh8c399962020-12-25 21:51:45 -0800608
Austin Schuh48d10d62022-10-16 22:19:23 -0700609 writer->QueueSpan(header->span());
Austin Schuh8c399962020-12-25 21:51:45 -0800610
611 if (!writer->ran_out_of_space()) {
Austin Schuh5b728b72021-06-16 14:57:15 -0700612 all_filenames_.emplace_back(
613 absl::StrCat(config_sha256, ".bfbs", extension_));
Austin Schuh8c399962020-12-25 21:51:45 -0800614 }
615 CloseWriter(&writer);
616}
617
Austin Schuhb8bca732021-07-30 22:32:00 -0700618NewDataWriter *MultiNodeLogNamer::MakeWriter(const Channel *channel) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700619 // See if we can read the data on this node at all.
620 const bool is_readable =
621 configuration::ChannelIsReadableOnNode(channel, this->node());
622 if (!is_readable) {
623 return nullptr;
624 }
625
626 // Then, see if we are supposed to log the data here.
627 const bool log_message =
628 configuration::ChannelMessageIsLoggedOnNode(channel, this->node());
629
630 if (!log_message) {
631 return nullptr;
632 }
633
634 // Now, sort out if this is data generated on this node, or not. It is
635 // generated if it is sendable on this node.
636 if (configuration::ChannelIsSendableOnNode(channel, this->node())) {
Austin Schuhb8bca732021-07-30 22:32:00 -0700637 if (!data_writer_) {
Austin Schuh48d10d62022-10-16 22:19:23 -0700638 MakeDataWriter();
Brian Silvermancb805822020-10-06 17:43:35 -0700639 }
Austin Schuh48d10d62022-10-16 22:19:23 -0700640 data_writer_->UpdateMaxMessageSize(PackMessageSize(
641 LogType::kLogRemoteMessage, channel->max_size()));
Austin Schuhb8bca732021-07-30 22:32:00 -0700642 return data_writer_.get();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700643 }
644
645 // Ok, we have data that is being forwarded to us that we are supposed to
646 // log. It needs to be logged with send timestamps, but be sorted enough
647 // to be able to be processed.
648 CHECK(data_writers_.find(channel) == data_writers_.end());
649
650 // Track that this node is being logged.
651 const Node *source_node = configuration::GetNode(
652 configuration_, channel->source_node()->string_view());
653
654 if (std::find(nodes_.begin(), nodes_.end(), source_node) == nodes_.end()) {
655 nodes_.emplace_back(source_node);
656 }
657
Austin Schuhf5f99f32022-02-07 20:05:37 -0800658 NewDataWriter data_writer(
659 this, source_node, node_,
660 [this, channel](NewDataWriter *data_writer) {
661 OpenWriter(channel, data_writer);
662 },
Austin Schuh48d10d62022-10-16 22:19:23 -0700663 [this](NewDataWriter *data_writer) { CloseWriter(&data_writer->writer); },
664 PackMessageSize(LogType::kLogRemoteMessage, channel->max_size()));
Austin Schuhb8bca732021-07-30 22:32:00 -0700665 return &(
666 data_writers_.emplace(channel, std::move(data_writer)).first->second);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700667}
668
Austin Schuhb8bca732021-07-30 22:32:00 -0700669NewDataWriter *MultiNodeLogNamer::MakeForwardedTimestampWriter(
Austin Schuhcb5601b2020-09-10 15:29:59 -0700670 const Channel *channel, const Node *node) {
671 // See if we can read the data on this node at all.
672 const bool is_readable =
673 configuration::ChannelIsReadableOnNode(channel, this->node());
674 CHECK(is_readable) << ": " << configuration::CleanedChannelToString(channel);
675
676 CHECK(data_writers_.find(channel) == data_writers_.end());
677
678 if (std::find(nodes_.begin(), nodes_.end(), node) == nodes_.end()) {
679 nodes_.emplace_back(node);
680 }
681
Austin Schuhf5f99f32022-02-07 20:05:37 -0800682 NewDataWriter data_writer(
683 this, configuration::GetNode(configuration_, node), node_,
684 [this, channel](NewDataWriter *data_writer) {
685 OpenForwardedTimestampWriter(channel, data_writer);
686 },
Austin Schuh48d10d62022-10-16 22:19:23 -0700687 [this](NewDataWriter *data_writer) { CloseWriter(&data_writer->writer); },
688 PackRemoteMessageSize());
Austin Schuhb8bca732021-07-30 22:32:00 -0700689 return &(
690 data_writers_.emplace(channel, std::move(data_writer)).first->second);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700691}
692
Austin Schuhb8bca732021-07-30 22:32:00 -0700693NewDataWriter *MultiNodeLogNamer::MakeTimestampWriter(const Channel *channel) {
Brian Silverman0465fcf2020-09-24 00:29:18 -0700694 bool log_delivery_times = false;
695 if (this->node() != nullptr) {
696 log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode(
697 channel, this->node(), this->node());
698 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700699 if (!log_delivery_times) {
700 return nullptr;
701 }
702
Austin Schuhb8bca732021-07-30 22:32:00 -0700703 if (!data_writer_) {
Austin Schuh48d10d62022-10-16 22:19:23 -0700704 MakeDataWriter();
Brian Silvermancb805822020-10-06 17:43:35 -0700705 }
Austin Schuh48d10d62022-10-16 22:19:23 -0700706 data_writer_->UpdateMaxMessageSize(
707 PackMessageSize(LogType::kLogDeliveryTimeOnly, 0));
Austin Schuhb8bca732021-07-30 22:32:00 -0700708 return data_writer_.get();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700709}
710
Brian Silverman0465fcf2020-09-24 00:29:18 -0700711void MultiNodeLogNamer::Close() {
Austin Schuhb8bca732021-07-30 22:32:00 -0700712 data_writers_.clear();
713 data_writer_.reset();
Brian Silvermancb805822020-10-06 17:43:35 -0700714}
715
716void MultiNodeLogNamer::ResetStatistics() {
Austin Schuhb8bca732021-07-30 22:32:00 -0700717 for (std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700718 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800719 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700720 data_writer.second.writer->ResetStatistics();
Brian Silverman0465fcf2020-09-24 00:29:18 -0700721 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700722 if (data_writer_) {
723 data_writer_->writer->ResetStatistics();
Brian Silvermancb805822020-10-06 17:43:35 -0700724 }
725 max_write_time_ = std::chrono::nanoseconds::zero();
726 max_write_time_bytes_ = -1;
727 max_write_time_messages_ = -1;
728 total_write_time_ = std::chrono::nanoseconds::zero();
729 total_write_count_ = 0;
730 total_write_messages_ = 0;
731 total_write_bytes_ = 0;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700732}
733
Austin Schuhb8bca732021-07-30 22:32:00 -0700734void MultiNodeLogNamer::OpenForwardedTimestampWriter(
735 const Channel *channel, NewDataWriter *data_writer) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700736 std::string filename =
Austin Schuhe715eae2020-10-10 15:39:30 -0700737 absl::StrCat("timestamps", channel->name()->string_view(), "/",
Brian Silvermana621f522020-09-30 16:52:43 -0700738 channel->type()->string_view(), ".part",
Austin Schuh572924a2021-07-30 22:32:12 -0700739 data_writer->parts_index(), ".bfbs", extension_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700740 CreateBufferWriter(filename, data_writer->max_message_size(),
741 &data_writer->writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700742}
743
744void MultiNodeLogNamer::OpenWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700745 NewDataWriter *data_writer) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700746 const std::string filename = absl::StrCat(
Austin Schuhe715eae2020-10-10 15:39:30 -0700747 CHECK_NOTNULL(channel->source_node())->string_view(), "_data",
Brian Silvermana621f522020-09-30 16:52:43 -0700748 channel->name()->string_view(), "/", channel->type()->string_view(),
Austin Schuh572924a2021-07-30 22:32:12 -0700749 ".part", data_writer->parts_index(), ".bfbs", extension_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700750 CreateBufferWriter(filename, data_writer->max_message_size(),
751 &data_writer->writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700752}
753
Austin Schuh48d10d62022-10-16 22:19:23 -0700754void MultiNodeLogNamer::MakeDataWriter() {
Austin Schuhb8bca732021-07-30 22:32:00 -0700755 if (!data_writer_) {
756 data_writer_ = std::make_unique<NewDataWriter>(
Austin Schuhf5f99f32022-02-07 20:05:37 -0800757 this, node_, node_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700758 [this](NewDataWriter *writer) {
759 std::string name;
760 if (node() != nullptr) {
761 name = absl::StrCat(name, node()->name()->string_view(), "_");
762 }
Austin Schuh572924a2021-07-30 22:32:12 -0700763 absl::StrAppend(&name, "data.part", writer->parts_index(), ".bfbs",
Austin Schuhb8bca732021-07-30 22:32:00 -0700764 extension_);
Austin Schuh48d10d62022-10-16 22:19:23 -0700765 CreateBufferWriter(name, writer->max_message_size(), &writer->writer);
Austin Schuhb8bca732021-07-30 22:32:00 -0700766 },
767 [this](NewDataWriter *data_writer) {
768 CloseWriter(&data_writer->writer);
Austin Schuh48d10d62022-10-16 22:19:23 -0700769 },
770 // Default size is 0 so it will be obvious if something doesn't fix it
771 // afterwards.
772 0);
Brian Silverman7af8c902020-09-29 16:14:04 -0700773 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700774}
775
Brian Silverman0465fcf2020-09-24 00:29:18 -0700776void MultiNodeLogNamer::CreateBufferWriter(
Austin Schuh48d10d62022-10-16 22:19:23 -0700777 std::string_view path, size_t max_message_size,
778 std::unique_ptr<DetachedBufferWriter> *destination) {
Brian Silverman0465fcf2020-09-24 00:29:18 -0700779 if (ran_out_of_space_) {
780 // Refuse to open any new files, which might skip data. Any existing files
781 // are in the same folder, which means they're on the same filesystem, which
782 // means they're probably going to run out of space and get stuck too.
Austin Schuha426f1f2021-03-31 22:27:41 -0700783 if (!destination->get()) {
784 // But avoid leaving a nullptr writer if we're out of space when
785 // attempting to open the first file.
786 *destination = std::make_unique<DetachedBufferWriter>(
787 DetachedBufferWriter::already_out_of_space_t());
788 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700789 return;
790 }
Austin Schuhe715eae2020-10-10 15:39:30 -0700791 const std::string_view separator = base_name_.back() == '/' ? "" : "_";
792 const std::string filename =
793 absl::StrCat(base_name_, separator, path, temp_suffix_);
Brian Silverman0465fcf2020-09-24 00:29:18 -0700794 if (!destination->get()) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700795 if (ran_out_of_space_) {
796 *destination = std::make_unique<DetachedBufferWriter>(
797 DetachedBufferWriter::already_out_of_space_t());
798 return;
799 }
Austin Schuh48d10d62022-10-16 22:19:23 -0700800 *destination = std::make_unique<DetachedBufferWriter>(
801 filename, encoder_factory_(max_message_size));
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700802 if (!destination->get()->ran_out_of_space()) {
803 all_filenames_.emplace_back(path);
804 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700805 return;
806 }
Brian Silvermancb805822020-10-06 17:43:35 -0700807
808 CloseWriter(destination);
809 if (ran_out_of_space_) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700810 *destination->get() =
811 DetachedBufferWriter(DetachedBufferWriter::already_out_of_space_t());
Brian Silverman0465fcf2020-09-24 00:29:18 -0700812 return;
813 }
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700814
Austin Schuh48d10d62022-10-16 22:19:23 -0700815 *destination->get() =
816 DetachedBufferWriter(filename, encoder_factory_(max_message_size));
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700817 if (!destination->get()->ran_out_of_space()) {
818 all_filenames_.emplace_back(path);
819 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700820}
821
Brian Silverman48deab12020-09-30 18:39:28 -0700822void MultiNodeLogNamer::RenameTempFile(DetachedBufferWriter *destination) {
823 if (temp_suffix_.empty()) {
824 return;
825 }
Austin Schuh6bb8a822021-03-31 23:04:39 -0700826 std::string current_filename = std::string(destination->filename());
Brian Silverman48deab12020-09-30 18:39:28 -0700827 CHECK(current_filename.size() > temp_suffix_.size());
Austin Schuh6bb8a822021-03-31 23:04:39 -0700828 std::string final_filename =
Brian Silverman48deab12020-09-30 18:39:28 -0700829 current_filename.substr(0, current_filename.size() - temp_suffix_.size());
Austin Schuh6bb8a822021-03-31 23:04:39 -0700830 int result = rename(current_filename.c_str(), final_filename.c_str());
831
832 // When changing the base name, we rename the log folder while there active
833 // buffer writers. Therefore, the name of that active buffer may still refer
834 // to the old file location rather than the new one. This minimized changes to
835 // existing code.
836 if (result != 0 && errno != ENOSPC && !old_base_name_.empty()) {
837 auto offset = current_filename.find(old_base_name_);
838 if (offset != std::string::npos) {
839 current_filename.replace(offset, old_base_name_.length(), base_name_);
840 }
841 offset = final_filename.find(old_base_name_);
842 if (offset != std::string::npos) {
843 final_filename.replace(offset, old_base_name_.length(), base_name_);
844 }
845 result = rename(current_filename.c_str(), final_filename.c_str());
846 }
847
Brian Silverman48deab12020-09-30 18:39:28 -0700848 if (result != 0) {
849 if (errno == ENOSPC) {
850 ran_out_of_space_ = true;
851 return;
852 } else {
853 PLOG(FATAL) << "Renaming " << current_filename << " to " << final_filename
854 << " failed";
855 }
Austin Schuh6bb8a822021-03-31 23:04:39 -0700856 } else {
857 VLOG(1) << "Renamed " << current_filename << " -> " << final_filename;
Brian Silverman48deab12020-09-30 18:39:28 -0700858 }
859}
860
Brian Silvermancb805822020-10-06 17:43:35 -0700861void MultiNodeLogNamer::CloseWriter(
862 std::unique_ptr<DetachedBufferWriter> *writer_pointer) {
863 DetachedBufferWriter *const writer = writer_pointer->get();
864 if (!writer) {
865 return;
866 }
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700867 const bool was_open = writer->is_open();
Brian Silvermancb805822020-10-06 17:43:35 -0700868 writer->Close();
869
870 if (writer->max_write_time() > max_write_time_) {
871 max_write_time_ = writer->max_write_time();
872 max_write_time_bytes_ = writer->max_write_time_bytes();
873 max_write_time_messages_ = writer->max_write_time_messages();
874 }
875 total_write_time_ += writer->total_write_time();
876 total_write_count_ += writer->total_write_count();
877 total_write_messages_ += writer->total_write_messages();
878 total_write_bytes_ += writer->total_write_bytes();
879
880 if (writer->ran_out_of_space()) {
881 ran_out_of_space_ = true;
882 writer->acknowledge_out_of_space();
883 }
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700884 if (was_open) {
885 RenameTempFile(writer);
886 } else {
887 CHECK(access(std::string(writer->filename()).c_str(), F_OK) == -1)
888 << ": File should not exist: " << writer->filename();
889 }
Brian Silvermancb805822020-10-06 17:43:35 -0700890}
891
Austin Schuhcb5601b2020-09-10 15:29:59 -0700892} // namespace logger
893} // namespace aos