blob: f269f43a6094866ea82b6e8ab7e9dcac9179b8c0 [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,
23 std::function<void(NewDataWriter *)> close)
24 : node_(node),
25 node_index_(configuration::GetNodeIndex(log_namer->configuration_, node)),
Austin Schuhf5f99f32022-02-07 20:05:37 -080026 logger_node_index_(
27 configuration::GetNodeIndex(log_namer->configuration_, logger_node)),
Austin Schuh572924a2021-07-30 22:32:12 -070028 log_namer_(log_namer),
29 reopen_(std::move(reopen)),
30 close_(std::move(close)) {
Austin Schuh72211ae2021-08-05 14:02:30 -070031 state_.resize(configuration::NodesCount(log_namer->configuration_));
32 CHECK_LT(node_index_, state_.size());
Austin Schuh572924a2021-07-30 22:32:12 -070033}
34
35NewDataWriter::~NewDataWriter() {
36 if (writer) {
37 Close();
38 }
39}
40
41void NewDataWriter::Rotate() {
Austin Schuhe46492f2021-07-31 19:49:41 -070042 // No need to rotate if nothing has been written.
43 if (header_written_) {
Austin Schuh58646e22021-08-23 23:51:46 -070044 VLOG(1) << "Rotated " << filename();
Austin Schuhe46492f2021-07-31 19:49:41 -070045 ++parts_index_;
46 reopen_(this);
47 header_written_ = false;
48 QueueHeader(MakeHeader());
49 }
Austin Schuh572924a2021-07-30 22:32:12 -070050}
51
Austin Schuh5e14d842022-01-21 12:02:15 -080052void NewDataWriter::Reboot(const UUID &source_node_boot_uuid) {
Austin Schuh572924a2021-07-30 22:32:12 -070053 parts_uuid_ = UUID::Random();
54 ++parts_index_;
55 reopen_(this);
56 header_written_ = false;
Austin Schuh5e14d842022-01-21 12:02:15 -080057 for (State &state : state_) {
58 state.boot_uuid = UUID::Zero();
59 state.oldest_remote_monotonic_timestamp = monotonic_clock::max_time;
60 state.oldest_local_monotonic_timestamp = monotonic_clock::max_time;
61 state.oldest_remote_unreliable_monotonic_timestamp =
62 monotonic_clock::max_time;
63 state.oldest_local_unreliable_monotonic_timestamp =
64 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -080065 state.oldest_remote_reliable_monotonic_timestamp =
66 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -080067 state.oldest_local_reliable_monotonic_timestamp = monotonic_clock::max_time;
68 state.oldest_logger_remote_unreliable_monotonic_timestamp =
69 monotonic_clock::max_time;
70 state.oldest_logger_local_unreliable_monotonic_timestamp =
Austin Schuhbfe6c572022-01-27 20:48:20 -080071 monotonic_clock::max_time;
Austin Schuh5e14d842022-01-21 12:02:15 -080072 }
73
74 state_[node_index_].boot_uuid = source_node_boot_uuid;
75
76 VLOG(1) << "Rebooted " << filename();
77}
78
79void NewDataWriter::UpdateBoot(const UUID &source_node_boot_uuid) {
80 if (state_[node_index_].boot_uuid != source_node_boot_uuid) {
81 state_[node_index_].boot_uuid = source_node_boot_uuid;
82 if (header_written_) {
83 Reboot(source_node_boot_uuid);
84 }
85 }
Austin Schuh572924a2021-07-30 22:32:12 -070086}
87
Austin Schuh72211ae2021-08-05 14:02:30 -070088void NewDataWriter::UpdateRemote(
89 const size_t remote_node_index, const UUID &remote_node_boot_uuid,
90 const monotonic_clock::time_point monotonic_remote_time,
Austin Schuhf5f99f32022-02-07 20:05:37 -080091 const monotonic_clock::time_point monotonic_event_time, const bool reliable,
92 monotonic_clock::time_point monotonic_timestamp_time) {
Austin Schuh58646e22021-08-23 23:51:46 -070093 // Trigger rotation if anything in the header changes.
Austin Schuh72211ae2021-08-05 14:02:30 -070094 bool rotate = false;
95 CHECK_LT(remote_node_index, state_.size());
96 State &state = state_[remote_node_index];
Austin Schuh58646e22021-08-23 23:51:46 -070097
98 // Did the remote boot UUID change?
Austin Schuh72211ae2021-08-05 14:02:30 -070099 if (state.boot_uuid != remote_node_boot_uuid) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700100 VLOG(1) << filename() << " Remote " << remote_node_index << " updated to "
Austin Schuh72211ae2021-08-05 14:02:30 -0700101 << remote_node_boot_uuid << " from " << state.boot_uuid;
102 state.boot_uuid = remote_node_boot_uuid;
103 state.oldest_remote_monotonic_timestamp = monotonic_clock::max_time;
104 state.oldest_local_monotonic_timestamp = monotonic_clock::max_time;
105 state.oldest_remote_unreliable_monotonic_timestamp =
106 monotonic_clock::max_time;
107 state.oldest_local_unreliable_monotonic_timestamp =
108 monotonic_clock::max_time;
Austin Schuhbfe6c572022-01-27 20:48:20 -0800109 state.oldest_remote_reliable_monotonic_timestamp =
110 monotonic_clock::max_time;
Austin Schuhf5f99f32022-02-07 20:05:37 -0800111 state.oldest_local_reliable_monotonic_timestamp = monotonic_clock::max_time;
112 state.oldest_logger_remote_unreliable_monotonic_timestamp =
113 monotonic_clock::max_time;
114 state.oldest_logger_local_unreliable_monotonic_timestamp =
Austin Schuhbfe6c572022-01-27 20:48:20 -0800115 monotonic_clock::max_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700116 rotate = true;
117 }
118
Austin Schuh58646e22021-08-23 23:51:46 -0700119 // Did the unreliable timestamps change?
Austin Schuh72211ae2021-08-05 14:02:30 -0700120 if (!reliable) {
121 if (state.oldest_remote_unreliable_monotonic_timestamp >
122 monotonic_remote_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700123 VLOG(1) << filename() << " Remote " << remote_node_index
124 << " oldest_remote_unreliable_monotonic_timestamp updated from "
125 << state.oldest_remote_unreliable_monotonic_timestamp << " to "
126 << monotonic_remote_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700127 state.oldest_remote_unreliable_monotonic_timestamp =
128 monotonic_remote_time;
129 state.oldest_local_unreliable_monotonic_timestamp = monotonic_event_time;
130 rotate = true;
131 }
Austin Schuhbfe6c572022-01-27 20:48:20 -0800132 } else {
133 if (state.oldest_remote_reliable_monotonic_timestamp >
134 monotonic_remote_time) {
135 VLOG(1) << filename() << " Remote " << remote_node_index
136 << " oldest_remote_reliable_monotonic_timestamp updated from "
137 << state.oldest_remote_reliable_monotonic_timestamp << " to "
138 << monotonic_remote_time;
139 state.oldest_remote_reliable_monotonic_timestamp = monotonic_remote_time;
140 state.oldest_local_reliable_monotonic_timestamp = monotonic_event_time;
141 rotate = true;
142 }
Austin Schuh72211ae2021-08-05 14:02:30 -0700143 }
144
Austin Schuhf5f99f32022-02-07 20:05:37 -0800145 // Track the logger timestamps too.
146 if (monotonic_timestamp_time != monotonic_clock::min_time) {
147 State &logger_state = state_[node_index_];
148 CHECK_EQ(remote_node_index, logger_node_index_);
149 if (monotonic_event_time <
150 logger_state.oldest_logger_remote_unreliable_monotonic_timestamp) {
151 VLOG(1)
152 << filename() << " Remote " << node_index_
153 << " oldest_logger_remote_unreliable_monotonic_timestamp updated "
154 "from "
155 << logger_state.oldest_logger_remote_unreliable_monotonic_timestamp
156 << " to " << monotonic_event_time;
157 logger_state.oldest_logger_remote_unreliable_monotonic_timestamp =
158 monotonic_event_time;
159 logger_state.oldest_logger_local_unreliable_monotonic_timestamp =
160 monotonic_timestamp_time;
161
162 rotate = true;
163 }
164 }
165
Austin Schuh58646e22021-08-23 23:51:46 -0700166 // Did any of the timestamps change?
Austin Schuh72211ae2021-08-05 14:02:30 -0700167 if (state.oldest_remote_monotonic_timestamp > monotonic_remote_time) {
Austin Schuh58646e22021-08-23 23:51:46 -0700168 VLOG(1) << filename() << " Remote " << remote_node_index
169 << " oldest_remote_monotonic_timestamp updated from "
170 << state.oldest_remote_monotonic_timestamp << " to "
171 << monotonic_remote_time;
Austin Schuh72211ae2021-08-05 14:02:30 -0700172 state.oldest_remote_monotonic_timestamp = monotonic_remote_time;
173 state.oldest_local_monotonic_timestamp = monotonic_event_time;
174 rotate = true;
175 }
176
177 if (rotate) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700178 Rotate();
179 }
180}
181
182void NewDataWriter::QueueMessage(flatbuffers::FlatBufferBuilder *fbb,
183 const UUID &source_node_boot_uuid,
184 aos::monotonic_clock::time_point now) {
Austin Schuh58646e22021-08-23 23:51:46 -0700185 // Trigger a reboot if we detect the boot UUID change.
Austin Schuh5e14d842022-01-21 12:02:15 -0800186 UpdateBoot(source_node_boot_uuid);
Austin Schuh572924a2021-07-30 22:32:12 -0700187
Austin Schuh5e14d842022-01-21 12:02:15 -0800188 if (!header_written_) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700189 QueueHeader(MakeHeader());
Austin Schuh572924a2021-07-30 22:32:12 -0700190 }
Austin Schuh58646e22021-08-23 23:51:46 -0700191
192 // If the start time has changed for this node, trigger a rotation.
193 if (log_namer_->monotonic_start_time(node_index_, source_node_boot_uuid) !=
Austin Schuh5e14d842022-01-21 12:02:15 -0800194 monotonic_start_time_) {
Austin Schuh58646e22021-08-23 23:51:46 -0700195 CHECK(header_written_);
196 Rotate();
197 }
198
199 CHECK_EQ(log_namer_->monotonic_start_time(node_index_, source_node_boot_uuid),
200 monotonic_start_time_);
Austin Schuh72211ae2021-08-05 14:02:30 -0700201 CHECK_EQ(state_[node_index_].boot_uuid, source_node_boot_uuid);
milind-ua50344f2021-08-25 18:22:20 -0700202 CHECK(writer);
Austin Schuh572924a2021-07-30 22:32:12 -0700203 CHECK(header_written_) << ": Attempting to write message before header to "
204 << writer->filename();
205 writer->QueueSizedFlatbuffer(fbb, now);
206}
207
Austin Schuhe46492f2021-07-31 19:49:41 -0700208aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>
209NewDataWriter::MakeHeader() {
210 const size_t logger_node_index = log_namer_->logger_node_index();
211 const UUID &logger_node_boot_uuid = log_namer_->logger_node_boot_uuid();
Austin Schuh72211ae2021-08-05 14:02:30 -0700212 if (state_[logger_node_index].boot_uuid == UUID::Zero()) {
Austin Schuhe46492f2021-07-31 19:49:41 -0700213 VLOG(1) << filename() << " Logger node is " << logger_node_index
214 << " and uuid is " << logger_node_boot_uuid;
Austin Schuh72211ae2021-08-05 14:02:30 -0700215 state_[logger_node_index].boot_uuid = logger_node_boot_uuid;
Austin Schuhe46492f2021-07-31 19:49:41 -0700216 } else {
Austin Schuh72211ae2021-08-05 14:02:30 -0700217 CHECK_EQ(state_[logger_node_index].boot_uuid, logger_node_boot_uuid);
Austin Schuhe46492f2021-07-31 19:49:41 -0700218 }
Austin Schuh72211ae2021-08-05 14:02:30 -0700219 return log_namer_->MakeHeader(node_index_, state_, parts_uuid(),
Austin Schuhe46492f2021-07-31 19:49:41 -0700220 parts_index_);
221}
222
Austin Schuh572924a2021-07-30 22:32:12 -0700223void NewDataWriter::QueueHeader(
224 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header) {
225 CHECK(!header_written_) << ": Attempting to write duplicate header to "
226 << writer->filename();
227 CHECK(header.message().has_source_node_boot_uuid());
Austin Schuh72211ae2021-08-05 14:02:30 -0700228 CHECK_EQ(state_[node_index_].boot_uuid,
Austin Schuhe46492f2021-07-31 19:49:41 -0700229 UUID::FromString(header.message().source_node_boot_uuid()));
Austin Schuh510dc622021-08-06 18:47:30 -0700230 if (!writer) {
231 reopen_(this);
232 }
233
Austin Schuh58646e22021-08-23 23:51:46 -0700234 VLOG(1) << "Writing to " << filename() << " "
235 << aos::FlatbufferToJson(
236 header, {.multi_line = false, .max_vector_size = 100});
237
Austin Schuh572924a2021-07-30 22:32:12 -0700238 // TODO(austin): This triggers a dummy allocation that we don't need as part
239 // of releasing. Can we skip it?
Austin Schuh510dc622021-08-06 18:47:30 -0700240 CHECK(writer);
Austin Schuh572924a2021-07-30 22:32:12 -0700241 writer->QueueSizedFlatbuffer(header.Release());
242 header_written_ = true;
Austin Schuh58646e22021-08-23 23:51:46 -0700243 monotonic_start_time_ = log_namer_->monotonic_start_time(
244 node_index_, state_[node_index_].boot_uuid);
Austin Schuh572924a2021-07-30 22:32:12 -0700245}
246
247void NewDataWriter::Close() {
248 CHECK(writer);
249 close_(this);
250 writer.reset();
251 header_written_ = false;
252}
253
Austin Schuh58646e22021-08-23 23:51:46 -0700254LogNamer::NodeState *LogNamer::GetNodeState(size_t node_index,
255 const UUID &boot_uuid) {
256 auto it = node_states_.find(std::make_pair(node_index, boot_uuid));
257 if (it == node_states_.end()) {
258 it =
259 node_states_.emplace(std::make_pair(node_index, boot_uuid), NodeState())
260 .first;
261 }
262 return &it->second;
263}
264
Austin Schuh73340842021-07-30 22:32:06 -0700265aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> LogNamer::MakeHeader(
Austin Schuh72211ae2021-08-05 14:02:30 -0700266 size_t node_index, const std::vector<NewDataWriter::State> &state,
Austin Schuh58646e22021-08-23 23:51:46 -0700267 const UUID &parts_uuid, int parts_index) {
Austin Schuh72211ae2021-08-05 14:02:30 -0700268 const UUID &source_node_boot_uuid = state[node_index].boot_uuid;
Austin Schuh73340842021-07-30 22:32:06 -0700269 const Node *const source_node =
270 configuration::GetNode(configuration_, node_index);
Austin Schuhf5f99f32022-02-07 20:05:37 -0800271 CHECK_EQ(LogFileHeader::MiniReflectTypeTable()->num_elems, 32u);
Austin Schuh73340842021-07-30 22:32:06 -0700272 flatbuffers::FlatBufferBuilder fbb;
273 fbb.ForceDefaults(true);
274
275 flatbuffers::Offset<flatbuffers::String> config_sha256_offset;
276 flatbuffers::Offset<aos::Configuration> configuration_offset;
277 if (header_.message().has_configuration()) {
278 CHECK(!header_.message().has_configuration_sha256());
279 configuration_offset =
280 CopyFlatBuffer(header_.message().configuration(), &fbb);
281 } else {
282 CHECK(!header_.message().has_configuration());
283 CHECK(header_.message().has_configuration_sha256());
284 config_sha256_offset = fbb.CreateString(
285 header_.message().configuration_sha256()->string_view());
286 }
287
288 CHECK(header_.message().has_name());
289 const flatbuffers::Offset<flatbuffers::String> name_offset =
290 fbb.CreateString(header_.message().name()->string_view());
291
292 CHECK(header_.message().has_log_event_uuid());
293 const flatbuffers::Offset<flatbuffers::String> log_event_uuid_offset =
294 fbb.CreateString(header_.message().log_event_uuid()->string_view());
295
296 CHECK(header_.message().has_logger_instance_uuid());
297 const flatbuffers::Offset<flatbuffers::String> logger_instance_uuid_offset =
298 fbb.CreateString(header_.message().logger_instance_uuid()->string_view());
299
300 flatbuffers::Offset<flatbuffers::String> log_start_uuid_offset;
301 if (header_.message().has_log_start_uuid()) {
302 log_start_uuid_offset =
303 fbb.CreateString(header_.message().log_start_uuid()->string_view());
304 }
305
306 CHECK(header_.message().has_logger_node_boot_uuid());
307 const flatbuffers::Offset<flatbuffers::String> logger_node_boot_uuid_offset =
308 fbb.CreateString(
309 header_.message().logger_node_boot_uuid()->string_view());
310
311 CHECK_NE(source_node_boot_uuid, UUID::Zero());
312 const flatbuffers::Offset<flatbuffers::String> source_node_boot_uuid_offset =
313 source_node_boot_uuid.PackString(&fbb);
314
315 const flatbuffers::Offset<flatbuffers::String> parts_uuid_offset =
316 parts_uuid.PackString(&fbb);
317
318 flatbuffers::Offset<Node> node_offset;
319 flatbuffers::Offset<Node> logger_node_offset;
320
321 if (configuration::MultiNode(configuration_)) {
322 node_offset = RecursiveCopyFlatBuffer(source_node, &fbb);
323 logger_node_offset = RecursiveCopyFlatBuffer(node_, &fbb);
324 }
325
Austin Schuhe46492f2021-07-31 19:49:41 -0700326 std::vector<flatbuffers::Offset<flatbuffers::String>> boot_uuid_offsets;
Austin Schuh72211ae2021-08-05 14:02:30 -0700327 boot_uuid_offsets.reserve(state.size());
Austin Schuhe46492f2021-07-31 19:49:41 -0700328
Austin Schuh4db9ec92021-09-22 13:11:12 -0700329 int64_t *unused;
Austin Schuh72211ae2021-08-05 14:02:30 -0700330 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
Austin Schuhf5f99f32022-02-07 20:05:37 -0800331 oldest_remote_monotonic_timestamps_offset =
332 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700333
Austin Schuh72211ae2021-08-05 14:02:30 -0700334 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
Austin Schuhf5f99f32022-02-07 20:05:37 -0800335 oldest_local_monotonic_timestamps_offset =
336 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700337
Austin Schuh72211ae2021-08-05 14:02:30 -0700338 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
339 oldest_remote_unreliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800340 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700341
Austin Schuh72211ae2021-08-05 14:02:30 -0700342 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
343 oldest_local_unreliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800344 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuh72211ae2021-08-05 14:02:30 -0700345
Austin Schuhbfe6c572022-01-27 20:48:20 -0800346 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
347 oldest_remote_reliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800348 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800349
350 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
351 oldest_local_reliable_monotonic_timestamps_offset =
Austin Schuhf5f99f32022-02-07 20:05:37 -0800352 fbb.CreateUninitializedVector(state.size(), &unused);
353
354 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
355 oldest_logger_remote_unreliable_monotonic_timestamps_offset =
356 fbb.CreateUninitializedVector(state.size(), &unused);
357
358 flatbuffers::Offset<flatbuffers::Vector<int64_t>>
359 oldest_logger_local_unreliable_monotonic_timestamps_offset =
360 fbb.CreateUninitializedVector(state.size(), &unused);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800361
Austin Schuh72211ae2021-08-05 14:02:30 -0700362 for (size_t i = 0; i < state.size(); ++i) {
Austin Schuh4db9ec92021-09-22 13:11:12 -0700363 if (state[i].boot_uuid != UUID::Zero()) {
364 boot_uuid_offsets.emplace_back(state[i].boot_uuid.PackString(&fbb));
365 } else {
366 boot_uuid_offsets.emplace_back(fbb.CreateString(""));
367 }
Austin Schuh5ae8f4a2021-09-11 19:09:50 -0700368 if (state[i].boot_uuid == UUID::Zero()) {
369 CHECK_EQ(state[i].oldest_remote_monotonic_timestamp,
370 monotonic_clock::max_time);
371 CHECK_EQ(state[i].oldest_local_monotonic_timestamp,
372 monotonic_clock::max_time);
373 CHECK_EQ(state[i].oldest_remote_unreliable_monotonic_timestamp,
374 monotonic_clock::max_time);
375 CHECK_EQ(state[i].oldest_local_unreliable_monotonic_timestamp,
376 monotonic_clock::max_time);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800377 CHECK_EQ(state[i].oldest_remote_reliable_monotonic_timestamp,
378 monotonic_clock::max_time);
379 CHECK_EQ(state[i].oldest_local_reliable_monotonic_timestamp,
380 monotonic_clock::max_time);
Austin Schuhf5f99f32022-02-07 20:05:37 -0800381 CHECK_EQ(state[i].oldest_logger_remote_unreliable_monotonic_timestamp,
382 monotonic_clock::max_time);
383 CHECK_EQ(state[i].oldest_logger_local_unreliable_monotonic_timestamp,
384 monotonic_clock::max_time);
Austin Schuh5ae8f4a2021-09-11 19:09:50 -0700385 }
386
Austin Schuh4db9ec92021-09-22 13:11:12 -0700387 flatbuffers::GetMutableTemporaryPointer(
388 fbb, oldest_remote_monotonic_timestamps_offset)
389 ->Mutate(i, state[i]
390 .oldest_remote_monotonic_timestamp.time_since_epoch()
391 .count());
392 flatbuffers::GetMutableTemporaryPointer(
393 fbb, oldest_local_monotonic_timestamps_offset)
394 ->Mutate(i, state[i]
395 .oldest_local_monotonic_timestamp.time_since_epoch()
396 .count());
397 flatbuffers::GetMutableTemporaryPointer(
398 fbb, oldest_remote_unreliable_monotonic_timestamps_offset)
399 ->Mutate(i, state[i]
Austin Schuhbfe6c572022-01-27 20:48:20 -0800400 .oldest_remote_unreliable_monotonic_timestamp
401 .time_since_epoch()
Austin Schuh4db9ec92021-09-22 13:11:12 -0700402 .count());
403 flatbuffers::GetMutableTemporaryPointer(
404 fbb, oldest_local_unreliable_monotonic_timestamps_offset)
405 ->Mutate(i, state[i]
Austin Schuhbfe6c572022-01-27 20:48:20 -0800406 .oldest_local_unreliable_monotonic_timestamp
407 .time_since_epoch()
Austin Schuh4db9ec92021-09-22 13:11:12 -0700408 .count());
Austin Schuhbfe6c572022-01-27 20:48:20 -0800409
410 flatbuffers::GetMutableTemporaryPointer(
411 fbb, oldest_remote_reliable_monotonic_timestamps_offset)
412 ->Mutate(i, state[i]
413 .oldest_remote_reliable_monotonic_timestamp
414 .time_since_epoch()
415 .count());
416 flatbuffers::GetMutableTemporaryPointer(
417 fbb, oldest_local_reliable_monotonic_timestamps_offset)
418 ->Mutate(
419 i, state[i]
420 .oldest_local_reliable_monotonic_timestamp.time_since_epoch()
421 .count());
Austin Schuhf5f99f32022-02-07 20:05:37 -0800422
423 flatbuffers::GetMutableTemporaryPointer(
424 fbb, oldest_logger_remote_unreliable_monotonic_timestamps_offset)
425 ->Mutate(i, state[i]
426 .oldest_logger_remote_unreliable_monotonic_timestamp
427 .time_since_epoch()
428 .count());
429 flatbuffers::GetMutableTemporaryPointer(
430 fbb, oldest_logger_local_unreliable_monotonic_timestamps_offset)
431 ->Mutate(i, state[i]
432 .oldest_logger_local_unreliable_monotonic_timestamp
433 .time_since_epoch()
434 .count());
Austin Schuh72211ae2021-08-05 14:02:30 -0700435 }
436
Austin Schuh4db9ec92021-09-22 13:11:12 -0700437 flatbuffers::Offset<
438 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
439 boot_uuids_offset = fbb.CreateVector(boot_uuid_offsets);
440
Austin Schuh73340842021-07-30 22:32:06 -0700441 aos::logger::LogFileHeader::Builder log_file_header_builder(fbb);
442
443 log_file_header_builder.add_name(name_offset);
444
445 // Only add the node if we are running in a multinode configuration.
446 if (!logger_node_offset.IsNull()) {
447 log_file_header_builder.add_node(node_offset);
448 log_file_header_builder.add_logger_node(logger_node_offset);
449 }
450
451 if (!configuration_offset.IsNull()) {
452 log_file_header_builder.add_configuration(configuration_offset);
453 }
454 log_file_header_builder.add_max_out_of_order_duration(
455 header_.message().max_out_of_order_duration());
456
Austin Schuh58646e22021-08-23 23:51:46 -0700457 NodeState *node_state = GetNodeState(node_index, source_node_boot_uuid);
Austin Schuh73340842021-07-30 22:32:06 -0700458 log_file_header_builder.add_monotonic_start_time(
459 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700460 node_state->monotonic_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700461 .count());
462 if (source_node == node_) {
463 log_file_header_builder.add_realtime_start_time(
464 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700465 node_state->realtime_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700466 .count());
467 } else {
468 // Fill out the legacy start times. Since these were implemented to never
469 // change on reboot, they aren't very helpful in tracking what happened.
470 log_file_header_builder.add_logger_monotonic_start_time(
471 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700472 node_state->logger_monotonic_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700473 .count());
474 log_file_header_builder.add_logger_realtime_start_time(
475 std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh58646e22021-08-23 23:51:46 -0700476 node_state->logger_realtime_start_time.time_since_epoch())
Austin Schuh73340842021-07-30 22:32:06 -0700477 .count());
478 }
479
480 // TODO(austin): Add more useful times. When was this part started? What do
481 // we know about both the logger and remote then?
482
483 log_file_header_builder.add_log_event_uuid(log_event_uuid_offset);
484 log_file_header_builder.add_logger_instance_uuid(logger_instance_uuid_offset);
485 if (!log_start_uuid_offset.IsNull()) {
486 log_file_header_builder.add_log_start_uuid(log_start_uuid_offset);
487 }
488 log_file_header_builder.add_logger_node_boot_uuid(
489 logger_node_boot_uuid_offset);
490 log_file_header_builder.add_source_node_boot_uuid(
491 source_node_boot_uuid_offset);
492
493 log_file_header_builder.add_parts_uuid(parts_uuid_offset);
494 log_file_header_builder.add_parts_index(parts_index);
495
496 if (!config_sha256_offset.IsNull()) {
497 log_file_header_builder.add_configuration_sha256(config_sha256_offset);
498 }
499
Austin Schuhe46492f2021-07-31 19:49:41 -0700500 log_file_header_builder.add_boot_uuids(boot_uuids_offset);
Austin Schuha499cea2021-07-31 19:49:53 -0700501 log_file_header_builder.add_logger_part_monotonic_start_time(
502 std::chrono::duration_cast<std::chrono::nanoseconds>(
503 event_loop_->monotonic_now().time_since_epoch())
504 .count());
505 log_file_header_builder.add_logger_part_realtime_start_time(
506 std::chrono::duration_cast<std::chrono::nanoseconds>(
507 event_loop_->realtime_now().time_since_epoch())
508 .count());
Austin Schuh72211ae2021-08-05 14:02:30 -0700509 log_file_header_builder.add_oldest_remote_monotonic_timestamps(
510 oldest_remote_monotonic_timestamps_offset);
511 log_file_header_builder.add_oldest_local_monotonic_timestamps(
512 oldest_local_monotonic_timestamps_offset);
513 log_file_header_builder.add_oldest_remote_unreliable_monotonic_timestamps(
514 oldest_remote_unreliable_monotonic_timestamps_offset);
515 log_file_header_builder.add_oldest_local_unreliable_monotonic_timestamps(
516 oldest_local_unreliable_monotonic_timestamps_offset);
Austin Schuhbfe6c572022-01-27 20:48:20 -0800517 log_file_header_builder.add_oldest_remote_reliable_monotonic_timestamps(
518 oldest_remote_reliable_monotonic_timestamps_offset);
519 log_file_header_builder.add_oldest_local_reliable_monotonic_timestamps(
520 oldest_local_reliable_monotonic_timestamps_offset);
Austin Schuhf5f99f32022-02-07 20:05:37 -0800521 log_file_header_builder
522 .add_oldest_logger_remote_unreliable_monotonic_timestamps(
523 oldest_logger_remote_unreliable_monotonic_timestamps_offset);
524 log_file_header_builder
525 .add_oldest_logger_local_unreliable_monotonic_timestamps(
526 oldest_logger_local_unreliable_monotonic_timestamps_offset);
Austin Schuh73340842021-07-30 22:32:06 -0700527 fbb.FinishSizePrefixed(log_file_header_builder.Finish());
528 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> result(
529 fbb.Release());
530
531 CHECK(result.Verify()) << ": Built a corrupted header.";
532
533 return result;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700534}
535
Austin Schuhb8bca732021-07-30 22:32:00 -0700536NewDataWriter *LocalLogNamer::MakeWriter(const Channel *channel) {
Austin Schuhdf576472020-10-19 09:39:37 -0700537 CHECK(configuration::ChannelIsSendableOnNode(channel, node()))
538 << ": " << configuration::CleanedChannelToString(channel);
Austin Schuhb8bca732021-07-30 22:32:00 -0700539 return &data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700540}
541
Austin Schuh73340842021-07-30 22:32:06 -0700542void LocalLogNamer::Rotate(const Node *node) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700543 CHECK(node == this->node());
Austin Schuhb8bca732021-07-30 22:32:00 -0700544 data_writer_.Rotate();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700545}
Austin Schuh8c399962020-12-25 21:51:45 -0800546
547void LocalLogNamer::WriteConfiguration(
548 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
549 std::string_view config_sha256) {
550 const std::string filename = absl::StrCat(base_name_, config_sha256, ".bfbs");
551
552 std::unique_ptr<DetachedBufferWriter> writer =
553 std::make_unique<DetachedBufferWriter>(
554 filename, std::make_unique<aos::logger::DummyEncoder>());
555 writer->QueueSizedFlatbuffer(header->Release());
556}
557
Austin Schuhb8bca732021-07-30 22:32:00 -0700558NewDataWriter *LocalLogNamer::MakeTimestampWriter(const Channel *channel) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700559 CHECK(configuration::ChannelIsReadableOnNode(channel, node_))
560 << ": Message is not delivered to this node.";
561 CHECK(node_ != nullptr) << ": Can't log timestamps in a single node world";
562 CHECK(configuration::ConnectionDeliveryTimeIsLoggedOnNode(channel, node_,
563 node_))
564 << ": Delivery times aren't logged for this channel on this node.";
Austin Schuhb8bca732021-07-30 22:32:00 -0700565 return &data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700566}
567
Austin Schuhb8bca732021-07-30 22:32:00 -0700568NewDataWriter *LocalLogNamer::MakeForwardedTimestampWriter(
Austin Schuhcb5601b2020-09-10 15:29:59 -0700569 const Channel * /*channel*/, const Node * /*node*/) {
570 LOG(FATAL) << "Can't log forwarded timestamps in a singe log file.";
571 return nullptr;
572}
Austin Schuhcb5601b2020-09-10 15:29:59 -0700573MultiNodeLogNamer::MultiNodeLogNamer(std::string_view base_name,
Austin Schuha499cea2021-07-31 19:49:53 -0700574 EventLoop *event_loop)
Austin Schuh5b728b72021-06-16 14:57:15 -0700575 : MultiNodeLogNamer(base_name, event_loop->configuration(), event_loop,
576 event_loop->node()) {}
577
578MultiNodeLogNamer::MultiNodeLogNamer(std::string_view base_name,
579 const Configuration *configuration,
580 EventLoop *event_loop, const Node *node)
581 : LogNamer(configuration, event_loop, node),
582 base_name_(base_name),
583 old_base_name_() {}
Austin Schuhcb5601b2020-09-10 15:29:59 -0700584
Brian Silverman48deab12020-09-30 18:39:28 -0700585MultiNodeLogNamer::~MultiNodeLogNamer() {
586 if (!ran_out_of_space_) {
587 // This handles renaming temporary files etc.
588 Close();
589 }
590}
591
Austin Schuh572924a2021-07-30 22:32:12 -0700592void MultiNodeLogNamer::Rotate(const Node *node) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700593 if (node == this->node()) {
Austin Schuhb8bca732021-07-30 22:32:00 -0700594 if (data_writer_) {
Austin Schuh572924a2021-07-30 22:32:12 -0700595 data_writer_->Rotate();
Brian Silvermancb805822020-10-06 17:43:35 -0700596 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700597 } else {
Austin Schuhb8bca732021-07-30 22:32:00 -0700598 for (std::pair<const Channel *const, NewDataWriter> &data_writer :
Austin Schuhcb5601b2020-09-10 15:29:59 -0700599 data_writers_) {
Austin Schuh572924a2021-07-30 22:32:12 -0700600 if (node == data_writer.second.node()) {
601 data_writer.second.Rotate();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700602 }
603 }
604 }
605}
606
Austin Schuh8c399962020-12-25 21:51:45 -0800607void MultiNodeLogNamer::WriteConfiguration(
608 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
609 std::string_view config_sha256) {
610 if (ran_out_of_space_) {
611 return;
612 }
613
614 const std::string_view separator = base_name_.back() == '/' ? "" : "_";
615 const std::string filename = absl::StrCat(
616 base_name_, separator, config_sha256, ".bfbs", extension_, temp_suffix_);
617
618 std::unique_ptr<DetachedBufferWriter> writer =
619 std::make_unique<DetachedBufferWriter>(filename, encoder_factory_());
620
621 writer->QueueSizedFlatbuffer(header->Release());
622
623 if (!writer->ran_out_of_space()) {
Austin Schuh5b728b72021-06-16 14:57:15 -0700624 all_filenames_.emplace_back(
625 absl::StrCat(config_sha256, ".bfbs", extension_));
Austin Schuh8c399962020-12-25 21:51:45 -0800626 }
627 CloseWriter(&writer);
628}
629
Austin Schuhb8bca732021-07-30 22:32:00 -0700630NewDataWriter *MultiNodeLogNamer::MakeWriter(const Channel *channel) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700631 // See if we can read the data on this node at all.
632 const bool is_readable =
633 configuration::ChannelIsReadableOnNode(channel, this->node());
634 if (!is_readable) {
635 return nullptr;
636 }
637
638 // Then, see if we are supposed to log the data here.
639 const bool log_message =
640 configuration::ChannelMessageIsLoggedOnNode(channel, this->node());
641
642 if (!log_message) {
643 return nullptr;
644 }
645
646 // Now, sort out if this is data generated on this node, or not. It is
647 // generated if it is sendable on this node.
648 if (configuration::ChannelIsSendableOnNode(channel, this->node())) {
Austin Schuhb8bca732021-07-30 22:32:00 -0700649 if (!data_writer_) {
Brian Silvermancb805822020-10-06 17:43:35 -0700650 OpenDataWriter();
651 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700652 return data_writer_.get();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700653 }
654
655 // Ok, we have data that is being forwarded to us that we are supposed to
656 // log. It needs to be logged with send timestamps, but be sorted enough
657 // to be able to be processed.
658 CHECK(data_writers_.find(channel) == data_writers_.end());
659
660 // Track that this node is being logged.
661 const Node *source_node = configuration::GetNode(
662 configuration_, channel->source_node()->string_view());
663
664 if (std::find(nodes_.begin(), nodes_.end(), source_node) == nodes_.end()) {
665 nodes_.emplace_back(source_node);
666 }
667
Austin Schuhf5f99f32022-02-07 20:05:37 -0800668 NewDataWriter data_writer(
669 this, source_node, node_,
670 [this, channel](NewDataWriter *data_writer) {
671 OpenWriter(channel, data_writer);
672 },
673 [this](NewDataWriter *data_writer) {
674 CloseWriter(&data_writer->writer);
675 });
Austin Schuhb8bca732021-07-30 22:32:00 -0700676 return &(
677 data_writers_.emplace(channel, std::move(data_writer)).first->second);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700678}
679
Austin Schuhb8bca732021-07-30 22:32:00 -0700680NewDataWriter *MultiNodeLogNamer::MakeForwardedTimestampWriter(
Austin Schuhcb5601b2020-09-10 15:29:59 -0700681 const Channel *channel, const Node *node) {
682 // See if we can read the data on this node at all.
683 const bool is_readable =
684 configuration::ChannelIsReadableOnNode(channel, this->node());
685 CHECK(is_readable) << ": " << configuration::CleanedChannelToString(channel);
686
687 CHECK(data_writers_.find(channel) == data_writers_.end());
688
689 if (std::find(nodes_.begin(), nodes_.end(), node) == nodes_.end()) {
690 nodes_.emplace_back(node);
691 }
692
Austin Schuhf5f99f32022-02-07 20:05:37 -0800693 NewDataWriter data_writer(
694 this, configuration::GetNode(configuration_, node), node_,
695 [this, channel](NewDataWriter *data_writer) {
696 OpenForwardedTimestampWriter(channel, data_writer);
697 },
698 [this](NewDataWriter *data_writer) {
699 CloseWriter(&data_writer->writer);
700 });
Austin Schuhb8bca732021-07-30 22:32:00 -0700701 return &(
702 data_writers_.emplace(channel, std::move(data_writer)).first->second);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700703}
704
Austin Schuhb8bca732021-07-30 22:32:00 -0700705NewDataWriter *MultiNodeLogNamer::MakeTimestampWriter(const Channel *channel) {
Brian Silverman0465fcf2020-09-24 00:29:18 -0700706 bool log_delivery_times = false;
707 if (this->node() != nullptr) {
708 log_delivery_times = configuration::ConnectionDeliveryTimeIsLoggedOnNode(
709 channel, this->node(), this->node());
710 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700711 if (!log_delivery_times) {
712 return nullptr;
713 }
714
Austin Schuhb8bca732021-07-30 22:32:00 -0700715 if (!data_writer_) {
Brian Silvermancb805822020-10-06 17:43:35 -0700716 OpenDataWriter();
717 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700718 return data_writer_.get();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700719}
720
Brian Silverman0465fcf2020-09-24 00:29:18 -0700721void MultiNodeLogNamer::Close() {
Austin Schuhb8bca732021-07-30 22:32:00 -0700722 data_writers_.clear();
723 data_writer_.reset();
Brian Silvermancb805822020-10-06 17:43:35 -0700724}
725
726void MultiNodeLogNamer::ResetStatistics() {
Austin Schuhb8bca732021-07-30 22:32:00 -0700727 for (std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700728 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800729 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700730 data_writer.second.writer->ResetStatistics();
Brian Silverman0465fcf2020-09-24 00:29:18 -0700731 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700732 if (data_writer_) {
733 data_writer_->writer->ResetStatistics();
Brian Silvermancb805822020-10-06 17:43:35 -0700734 }
735 max_write_time_ = std::chrono::nanoseconds::zero();
736 max_write_time_bytes_ = -1;
737 max_write_time_messages_ = -1;
738 total_write_time_ = std::chrono::nanoseconds::zero();
739 total_write_count_ = 0;
740 total_write_messages_ = 0;
741 total_write_bytes_ = 0;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700742}
743
Austin Schuhb8bca732021-07-30 22:32:00 -0700744void MultiNodeLogNamer::OpenForwardedTimestampWriter(
745 const Channel *channel, NewDataWriter *data_writer) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700746 std::string filename =
Austin Schuhe715eae2020-10-10 15:39:30 -0700747 absl::StrCat("timestamps", channel->name()->string_view(), "/",
Brian Silvermana621f522020-09-30 16:52:43 -0700748 channel->type()->string_view(), ".part",
Austin Schuh572924a2021-07-30 22:32:12 -0700749 data_writer->parts_index(), ".bfbs", extension_);
Brian Silverman0465fcf2020-09-24 00:29:18 -0700750 CreateBufferWriter(filename, &data_writer->writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700751}
752
753void MultiNodeLogNamer::OpenWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700754 NewDataWriter *data_writer) {
Austin Schuhcb5601b2020-09-10 15:29:59 -0700755 const std::string filename = absl::StrCat(
Austin Schuhe715eae2020-10-10 15:39:30 -0700756 CHECK_NOTNULL(channel->source_node())->string_view(), "_data",
Brian Silvermana621f522020-09-30 16:52:43 -0700757 channel->name()->string_view(), "/", channel->type()->string_view(),
Austin Schuh572924a2021-07-30 22:32:12 -0700758 ".part", data_writer->parts_index(), ".bfbs", extension_);
Brian Silverman0465fcf2020-09-24 00:29:18 -0700759 CreateBufferWriter(filename, &data_writer->writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700760}
761
Brian Silvermana621f522020-09-30 16:52:43 -0700762void MultiNodeLogNamer::OpenDataWriter() {
Austin Schuhb8bca732021-07-30 22:32:00 -0700763 if (!data_writer_) {
764 data_writer_ = std::make_unique<NewDataWriter>(
Austin Schuhf5f99f32022-02-07 20:05:37 -0800765 this, node_, node_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700766 [this](NewDataWriter *writer) {
767 std::string name;
768 if (node() != nullptr) {
769 name = absl::StrCat(name, node()->name()->string_view(), "_");
770 }
Austin Schuh572924a2021-07-30 22:32:12 -0700771 absl::StrAppend(&name, "data.part", writer->parts_index(), ".bfbs",
Austin Schuhb8bca732021-07-30 22:32:00 -0700772 extension_);
773 CreateBufferWriter(name, &writer->writer);
774 },
775 [this](NewDataWriter *data_writer) {
776 CloseWriter(&data_writer->writer);
777 });
Brian Silverman7af8c902020-09-29 16:14:04 -0700778 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700779}
780
Brian Silverman0465fcf2020-09-24 00:29:18 -0700781void MultiNodeLogNamer::CreateBufferWriter(
Brian Silvermana621f522020-09-30 16:52:43 -0700782 std::string_view path, std::unique_ptr<DetachedBufferWriter> *destination) {
Brian Silverman0465fcf2020-09-24 00:29:18 -0700783 if (ran_out_of_space_) {
784 // Refuse to open any new files, which might skip data. Any existing files
785 // are in the same folder, which means they're on the same filesystem, which
786 // means they're probably going to run out of space and get stuck too.
Austin Schuha426f1f2021-03-31 22:27:41 -0700787 if (!destination->get()) {
788 // But avoid leaving a nullptr writer if we're out of space when
789 // attempting to open the first file.
790 *destination = std::make_unique<DetachedBufferWriter>(
791 DetachedBufferWriter::already_out_of_space_t());
792 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700793 return;
794 }
Austin Schuhe715eae2020-10-10 15:39:30 -0700795 const std::string_view separator = base_name_.back() == '/' ? "" : "_";
796 const std::string filename =
797 absl::StrCat(base_name_, separator, path, temp_suffix_);
Brian Silverman0465fcf2020-09-24 00:29:18 -0700798 if (!destination->get()) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700799 if (ran_out_of_space_) {
800 *destination = std::make_unique<DetachedBufferWriter>(
801 DetachedBufferWriter::already_out_of_space_t());
802 return;
803 }
Brian Silvermancb805822020-10-06 17:43:35 -0700804 *destination =
805 std::make_unique<DetachedBufferWriter>(filename, encoder_factory_());
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700806 if (!destination->get()->ran_out_of_space()) {
807 all_filenames_.emplace_back(path);
808 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700809 return;
810 }
Brian Silvermancb805822020-10-06 17:43:35 -0700811
812 CloseWriter(destination);
813 if (ran_out_of_space_) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700814 *destination->get() =
815 DetachedBufferWriter(DetachedBufferWriter::already_out_of_space_t());
Brian Silverman0465fcf2020-09-24 00:29:18 -0700816 return;
817 }
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700818
Brian Silvermancb805822020-10-06 17:43:35 -0700819 *destination->get() = DetachedBufferWriter(filename, encoder_factory_());
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700820 if (!destination->get()->ran_out_of_space()) {
821 all_filenames_.emplace_back(path);
822 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700823}
824
Brian Silverman48deab12020-09-30 18:39:28 -0700825void MultiNodeLogNamer::RenameTempFile(DetachedBufferWriter *destination) {
826 if (temp_suffix_.empty()) {
827 return;
828 }
Austin Schuh6bb8a822021-03-31 23:04:39 -0700829 std::string current_filename = std::string(destination->filename());
Brian Silverman48deab12020-09-30 18:39:28 -0700830 CHECK(current_filename.size() > temp_suffix_.size());
Austin Schuh6bb8a822021-03-31 23:04:39 -0700831 std::string final_filename =
Brian Silverman48deab12020-09-30 18:39:28 -0700832 current_filename.substr(0, current_filename.size() - temp_suffix_.size());
Austin Schuh6bb8a822021-03-31 23:04:39 -0700833 int result = rename(current_filename.c_str(), final_filename.c_str());
834
835 // When changing the base name, we rename the log folder while there active
836 // buffer writers. Therefore, the name of that active buffer may still refer
837 // to the old file location rather than the new one. This minimized changes to
838 // existing code.
839 if (result != 0 && errno != ENOSPC && !old_base_name_.empty()) {
840 auto offset = current_filename.find(old_base_name_);
841 if (offset != std::string::npos) {
842 current_filename.replace(offset, old_base_name_.length(), base_name_);
843 }
844 offset = final_filename.find(old_base_name_);
845 if (offset != std::string::npos) {
846 final_filename.replace(offset, old_base_name_.length(), base_name_);
847 }
848 result = rename(current_filename.c_str(), final_filename.c_str());
849 }
850
Brian Silverman48deab12020-09-30 18:39:28 -0700851 if (result != 0) {
852 if (errno == ENOSPC) {
853 ran_out_of_space_ = true;
854 return;
855 } else {
856 PLOG(FATAL) << "Renaming " << current_filename << " to " << final_filename
857 << " failed";
858 }
Austin Schuh6bb8a822021-03-31 23:04:39 -0700859 } else {
860 VLOG(1) << "Renamed " << current_filename << " -> " << final_filename;
Brian Silverman48deab12020-09-30 18:39:28 -0700861 }
862}
863
Brian Silvermancb805822020-10-06 17:43:35 -0700864void MultiNodeLogNamer::CloseWriter(
865 std::unique_ptr<DetachedBufferWriter> *writer_pointer) {
866 DetachedBufferWriter *const writer = writer_pointer->get();
867 if (!writer) {
868 return;
869 }
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700870 const bool was_open = writer->is_open();
Brian Silvermancb805822020-10-06 17:43:35 -0700871 writer->Close();
872
873 if (writer->max_write_time() > max_write_time_) {
874 max_write_time_ = writer->max_write_time();
875 max_write_time_bytes_ = writer->max_write_time_bytes();
876 max_write_time_messages_ = writer->max_write_time_messages();
877 }
878 total_write_time_ += writer->total_write_time();
879 total_write_count_ += writer->total_write_count();
880 total_write_messages_ += writer->total_write_messages();
881 total_write_bytes_ += writer->total_write_bytes();
882
883 if (writer->ran_out_of_space()) {
884 ran_out_of_space_ = true;
885 writer->acknowledge_out_of_space();
886 }
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700887 if (was_open) {
888 RenameTempFile(writer);
889 } else {
890 CHECK(access(std::string(writer->filename()).c_str(), F_OK) == -1)
891 << ": File should not exist: " << writer->filename();
892 }
Brian Silvermancb805822020-10-06 17:43:35 -0700893}
894
Austin Schuhcb5601b2020-09-10 15:29:59 -0700895} // namespace logger
896} // namespace aos