Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_LOG_NAMER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_LOG_NAMER_H_ |
| 3 | |
| 4 | #include <functional> |
| 5 | #include <map> |
| 6 | #include <memory> |
| 7 | #include <string_view> |
| 8 | #include <vector> |
| 9 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 10 | #include "absl/container/btree_map.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 11 | #include "flatbuffers/flatbuffers.h" |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 12 | #include "glog/logging.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 13 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 14 | #include "aos/events/logging/logfile_utils.h" |
| 15 | #include "aos/events/logging/logger_generated.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 16 | #include "aos/uuid.h" |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 17 | |
| 18 | namespace aos { |
| 19 | namespace logger { |
| 20 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 21 | class LogNamer; |
| 22 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 23 | // TODO(austin): Rename this back to DataWriter once all other callers are of |
| 24 | // the old DataWriter. |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 25 | // |
| 26 | // Class to manage writing data to log files. This lets us track which boot the |
| 27 | // written header has in it, and if the header has been written or not. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 28 | // |
| 29 | // The design of this class is that instead of being notified when any of the |
| 30 | // header data changes, it polls and owns that decision. This makes it much |
| 31 | // harder to write corrupted data. If that becomes a performance problem, we |
| 32 | // can DCHECK and take it out of production binaries. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 33 | class NewDataWriter { |
| 34 | public: |
| 35 | // Constructs a NewDataWriter. |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 36 | // log_namer is the log namer which holds the config and any other data we |
| 37 | // need for our header. |
| 38 | // node is the node whom's prespective we are logging from. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 39 | // reopen is called whenever a file needs to be reopened. |
| 40 | // close is called to close that file and extract any statistics. |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 41 | NewDataWriter(LogNamer *log_namer, const Node *node, const Node *logger_node, |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 42 | std::function<void(NewDataWriter *)> reopen, |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 43 | std::function<void(NewDataWriter *)> close, |
| 44 | size_t max_message_size); |
| 45 | |
| 46 | void UpdateMaxMessageSize(size_t new_size) { |
| 47 | if (new_size > max_message_size_) { |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 48 | CHECK(!header_written_) << ": Tried to update to " << new_size << ", was " |
| 49 | << max_message_size_ << " for " << name(); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 50 | max_message_size_ = new_size; |
| 51 | } |
| 52 | } |
| 53 | size_t max_message_size() const { return max_message_size_; } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 54 | |
Mithun Bharadwaj | a5cb8e0 | 2023-08-02 16:10:40 -0700 | [diff] [blame] | 55 | std::chrono::nanoseconds max_out_of_order_duration() const { |
| 56 | return max_out_of_order_duration_; |
| 57 | } |
| 58 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 59 | NewDataWriter(NewDataWriter &&other) = default; |
| 60 | aos::logger::NewDataWriter &operator=(NewDataWriter &&other) = default; |
| 61 | NewDataWriter(const NewDataWriter &) = delete; |
| 62 | void operator=(const NewDataWriter &) = delete; |
| 63 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 64 | ~NewDataWriter(); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 65 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 66 | // Rotates the log file, delaying writing the new header until data arrives. |
| 67 | void Rotate(); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 68 | |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 69 | // Updates all the metadata in the log file about the remote node which this |
| 70 | // message is from. |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 71 | void UpdateRemote(size_t remote_node_index, const UUID &remote_node_boot_uuid, |
| 72 | monotonic_clock::time_point monotonic_remote_time, |
| 73 | monotonic_clock::time_point monotonic_event_time, |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 74 | bool reliable, |
| 75 | monotonic_clock::time_point monotonic_timestamp_time = |
| 76 | monotonic_clock::min_time); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 77 | // Coppies a message with the provided boot UUID. |
| 78 | void CopyMessage(DataEncoder::Copier *coppier, |
| 79 | const UUID &source_node_boot_uuid, |
Mithun Bharadwaj | a5cb8e0 | 2023-08-02 16:10:40 -0700 | [diff] [blame] | 80 | aos::monotonic_clock::time_point now, |
| 81 | aos::monotonic_clock::time_point message_time); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 82 | |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 83 | // Updates the current boot for the source node. This is useful when you want |
| 84 | // to queue a message that may trigger a reboot rotation, but then need to |
| 85 | // update the remote timestamps. |
| 86 | void UpdateBoot(const UUID &source_node_boot_uuid); |
| 87 | |
Alexei Strots | bc082d8 | 2023-05-03 08:43:42 -0700 | [diff] [blame] | 88 | // Returns the name of the writer. It may be a filename, but assume it is not. |
| 89 | std::string_view name() const { return writer ? writer->name() : "(closed)"; } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 90 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 91 | void Close(); |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 92 | |
| 93 | std::unique_ptr<DetachedBufferWriter> writer = nullptr; |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 94 | |
| 95 | size_t node_index() const { return node_index_; } |
| 96 | const UUID &parts_uuid() const { return parts_uuid_; } |
| 97 | size_t parts_index() const { return parts_index_; } |
| 98 | const Node *node() const { return node_; } |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 99 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 100 | // Datastructure used to capture all the information about a remote node. |
| 101 | struct State { |
| 102 | // Boot UUID of the node. |
| 103 | UUID boot_uuid = UUID::Zero(); |
| 104 | // Timestamp on the remote monotonic clock of the oldest message sent to |
| 105 | // node_index_. |
| 106 | monotonic_clock::time_point oldest_remote_monotonic_timestamp = |
| 107 | monotonic_clock::max_time; |
| 108 | // Timestamp on the local monotonic clock of the message in |
| 109 | // oldest_remote_monotonic_timestamp. |
| 110 | monotonic_clock::time_point oldest_local_monotonic_timestamp = |
| 111 | monotonic_clock::max_time; |
| 112 | // Timestamp on the remote monotonic clock of the oldest message sent to |
| 113 | // node_index_, excluding messages forwarded with time_to_live() == 0. |
| 114 | monotonic_clock::time_point oldest_remote_unreliable_monotonic_timestamp = |
| 115 | monotonic_clock::max_time; |
| 116 | // Timestamp on the local monotonic clock of the message in |
| 117 | // oldest_local_unreliable_monotonic_timestamp. |
| 118 | monotonic_clock::time_point oldest_local_unreliable_monotonic_timestamp = |
| 119 | monotonic_clock::max_time; |
Austin Schuh | bfe6c57 | 2022-01-27 20:48:20 -0800 | [diff] [blame] | 120 | |
| 121 | // Timestamp on the remote monotonic clock of the oldest message sent to |
| 122 | // node_index_, only including messages forwarded with time_to_live() == 0. |
| 123 | monotonic_clock::time_point oldest_remote_reliable_monotonic_timestamp = |
| 124 | monotonic_clock::max_time; |
| 125 | // Timestamp on the local monotonic clock of the message in |
| 126 | // oldest_local_reliable_monotonic_timestamp. |
| 127 | monotonic_clock::time_point oldest_local_reliable_monotonic_timestamp = |
| 128 | monotonic_clock::max_time; |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 129 | |
| 130 | // Timestamp on the remote monotonic clock of the oldest message timestamp |
| 131 | // sent back to logger_node_index_. The remote here will be the node this |
| 132 | // part is from the perspective of, ie node_index_. |
| 133 | monotonic_clock::time_point |
| 134 | oldest_logger_remote_unreliable_monotonic_timestamp = |
| 135 | monotonic_clock::max_time; |
| 136 | // The time on the monotonic clock of the logger when this timestamp made it |
| 137 | // back to the logger (logger_node_index_). |
| 138 | monotonic_clock::time_point |
| 139 | oldest_logger_local_unreliable_monotonic_timestamp = |
| 140 | monotonic_clock::max_time; |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 143 | private: |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 144 | // Signals that a node has rebooted. |
Austin Schuh | 5e14d84 | 2022-01-21 12:02:15 -0800 | [diff] [blame] | 145 | void Reboot(const UUID &source_node_boot_uuid); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 146 | |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 147 | void QueueHeader( |
| 148 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header); |
| 149 | |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 150 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(); |
| 151 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 152 | monotonic_clock::time_point monotonic_start_time_ = monotonic_clock::min_time; |
| 153 | |
Austin Schuh | 577610e | 2021-12-08 12:07:19 -0800 | [diff] [blame] | 154 | const Node *node_ = nullptr; |
| 155 | size_t node_index_ = 0; |
Austin Schuh | f5f99f3 | 2022-02-07 20:05:37 -0800 | [diff] [blame] | 156 | size_t logger_node_index_ = 0; |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 157 | LogNamer *log_namer_; |
| 158 | UUID parts_uuid_ = UUID::Random(); |
| 159 | size_t parts_index_ = 0; |
| 160 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 161 | std::function<void(NewDataWriter *)> reopen_; |
| 162 | std::function<void(NewDataWriter *)> close_; |
Austin Schuh | 572924a | 2021-07-30 22:32:12 -0700 | [diff] [blame] | 163 | bool header_written_ = false; |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 164 | |
Austin Schuh | 72211ae | 2021-08-05 14:02:30 -0700 | [diff] [blame] | 165 | std::vector<State> state_; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 166 | |
| 167 | size_t max_message_size_; |
Mithun Bharadwaj | a5cb8e0 | 2023-08-02 16:10:40 -0700 | [diff] [blame] | 168 | |
| 169 | // Each data writer logs the channels for that node, i.e. |
| 170 | // each data writer writes one file. We may encounter messages which |
| 171 | // violate the max out of order duration specified in the header of that file. |
| 172 | // Rotate the data writer and start a new part for that particular file. |
| 173 | // This shouldn't affect the headers of other data writers, so make this |
| 174 | // a property of individual data writer instead of the overall log. |
| 175 | std::chrono::nanoseconds max_out_of_order_duration_; |
| 176 | |
| 177 | // Monotonic time point of the latest message we've logged so far, i.e |
| 178 | // Message X - time Z |
| 179 | // Message Y - time Z + 1 |
| 180 | // newest_message_time_ = Z + 1 (even if X was logged after Y) |
| 181 | // |
| 182 | // Since the messages can be logged out of order, this helps determine if |
| 183 | // max out of order duration was violated. |
| 184 | monotonic_clock::time_point newest_message_time_ = monotonic_clock::min_time; |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 187 | // Interface describing how to name, track, and add headers to log file parts. |
| 188 | class LogNamer { |
| 189 | public: |
| 190 | // Constructs a LogNamer with the primary node (ie the one the logger runs on) |
| 191 | // being node. |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 192 | LogNamer(const aos::Configuration *configuration, EventLoop *event_loop, |
| 193 | const aos::Node *node) |
Austin Schuh | a499cea | 2021-07-31 19:49:53 -0700 | [diff] [blame] | 194 | : event_loop_(event_loop), |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 195 | configuration_(configuration), |
| 196 | node_(node), |
Austin Schuh | a499cea | 2021-07-31 19:49:53 -0700 | [diff] [blame] | 197 | logger_node_index_(configuration::GetNodeIndex(configuration_, node_)) { |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 198 | nodes_.emplace_back(node_); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 199 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 200 | virtual ~LogNamer() = default; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 201 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 202 | // Returns a writer for writing data from messages on this channel (on the |
| 203 | // primary node). |
| 204 | // |
| 205 | // The returned pointer will stay valid across rotations, but the object it |
| 206 | // points to will be assigned to. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 207 | virtual NewDataWriter *MakeWriter(const Channel *channel) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 208 | |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 209 | // Returns a writer for writing timestamps from messages on this channel (on |
| 210 | // the primary node). |
| 211 | // |
| 212 | // The returned pointer will stay valid across rotations, but the object it |
| 213 | // points to will be assigned to. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 214 | virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 215 | |
| 216 | // Returns a writer for writing timestamps delivered over the special |
| 217 | // /aos/remote_timestamps/* channels. node is the node that the timestamps |
Brian Silverman | 87ac040 | 2020-09-17 14:47:01 -0700 | [diff] [blame] | 218 | // are forwarded back from (to the primary node). |
| 219 | // |
| 220 | // The returned pointer will stay valid across rotations, but the object it |
| 221 | // points to will be assigned to. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 222 | virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel, |
| 223 | const Node *node) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 224 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 225 | // Rotates all log files for the provided node. |
| 226 | virtual void Rotate(const Node *node) = 0; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 227 | |
| 228 | // Returns all the nodes that data is being written for. |
| 229 | const std::vector<const Node *> &nodes() const { return nodes_; } |
| 230 | |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 231 | // Closes all existing log data writers. No more data may be written after |
| 232 | // this. |
| 233 | virtual WriteCode Close() = 0; |
| 234 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 235 | // Returns the node the logger is running on. |
| 236 | const Node *node() const { return node_; } |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 237 | const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; } |
| 238 | size_t logger_node_index() const { return logger_node_index_; } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 239 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 240 | // Writes out the nested Configuration object to the config file location. |
| 241 | virtual void WriteConfiguration( |
| 242 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 243 | std::string_view config_sha256) = 0; |
| 244 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 245 | void SetHeaderTemplate( |
| 246 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) { |
| 247 | header_ = std::move(header); |
Austin Schuh | e46492f | 2021-07-31 19:49:41 -0700 | [diff] [blame] | 248 | logger_node_boot_uuid_ = |
| 249 | UUID::FromString(header_.message().logger_node_boot_uuid()); |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 250 | } |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 251 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 252 | void ClearStartTimes() { node_states_.clear(); } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 253 | |
| 254 | void SetStartTimes(size_t node_index, const UUID &boot_uuid, |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 255 | monotonic_clock::time_point monotonic_start_time, |
| 256 | realtime_clock::time_point realtime_start_time, |
| 257 | monotonic_clock::time_point logger_monotonic_start_time, |
| 258 | realtime_clock::time_point logger_realtime_start_time) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 259 | VLOG(1) << "Setting node " << node_index << " to start time " |
| 260 | << monotonic_start_time << " rt " << realtime_start_time << " UUID " |
| 261 | << boot_uuid; |
| 262 | NodeState *node_state = GetNodeState(node_index, boot_uuid); |
| 263 | node_state->monotonic_start_time = monotonic_start_time; |
| 264 | node_state->realtime_start_time = realtime_start_time; |
| 265 | node_state->logger_monotonic_start_time = logger_monotonic_start_time; |
| 266 | node_state->logger_realtime_start_time = logger_realtime_start_time; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 269 | monotonic_clock::time_point monotonic_start_time(size_t node_index, |
| 270 | const UUID &boot_uuid) { |
| 271 | DCHECK_NE(boot_uuid, UUID::Zero()); |
| 272 | |
| 273 | NodeState *node_state = GetNodeState(node_index, boot_uuid); |
| 274 | return node_state->monotonic_start_time; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Mithun Bharadwaj | a5cb8e0 | 2023-08-02 16:10:40 -0700 | [diff] [blame] | 277 | // This returns the initial out of order duration set in the header template |
| 278 | // by the logger based on polling period. It may be different than the actual |
| 279 | // duration used by the data writer. |
| 280 | std::chrono::nanoseconds base_max_out_of_order_duration() const { |
| 281 | return std::chrono::nanoseconds( |
| 282 | header_.message().max_out_of_order_duration()); |
| 283 | } |
| 284 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 285 | protected: |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 286 | // Structure with state per node about times and such. |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 287 | struct NodeState { |
| 288 | // Time when this node started logging. |
| 289 | monotonic_clock::time_point monotonic_start_time = |
| 290 | monotonic_clock::min_time; |
| 291 | realtime_clock::time_point realtime_start_time = realtime_clock::min_time; |
| 292 | |
| 293 | // Corresponding time on the logger node when it started logging. |
| 294 | monotonic_clock::time_point logger_monotonic_start_time = |
| 295 | monotonic_clock::min_time; |
| 296 | realtime_clock::time_point logger_realtime_start_time = |
| 297 | realtime_clock::min_time; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 298 | }; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 299 | |
| 300 | // Creates a new header by copying fields out of the template and combining |
| 301 | // them with the arguments provided. |
| 302 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader( |
| 303 | size_t node_index, const std::vector<NewDataWriter::State> &state, |
Mithun Bharadwaj | a5cb8e0 | 2023-08-02 16:10:40 -0700 | [diff] [blame] | 304 | const UUID &parts_uuid, int parts_index, |
| 305 | std::chrono::nanoseconds max_out_of_order_duration); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 306 | |
| 307 | EventLoop *event_loop_; |
| 308 | const Configuration *const configuration_; |
| 309 | const Node *const node_; |
| 310 | const size_t logger_node_index_; |
| 311 | UUID logger_node_boot_uuid_; |
| 312 | std::vector<const Node *> nodes_; |
| 313 | |
| 314 | friend NewDataWriter; |
| 315 | |
| 316 | // Returns the start/stop time state structure for a node and boot. We can |
| 317 | // have data from multiple boots, and it makes sense to reuse the start/stop |
| 318 | // times if we get data from the same boot again. |
| 319 | NodeState *GetNodeState(size_t node_index, const UUID &boot_uuid); |
| 320 | |
| 321 | absl::btree_map<std::pair<size_t, UUID>, NodeState> node_states_; |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 322 | |
| 323 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ = |
| 324 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 325 | }; |
| 326 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 327 | // Log namer which uses a config to name a bunch of files. |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 328 | class MultiNodeLogNamer : public LogNamer { |
| 329 | public: |
Alexei Strots | caf17d3 | 2023-04-03 22:31:11 -0700 | [diff] [blame] | 330 | MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend, |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 331 | EventLoop *event_loop); |
Alexei Strots | caf17d3 | 2023-04-03 22:31:11 -0700 | [diff] [blame] | 332 | MultiNodeLogNamer(std::unique_ptr<LogBackend> log_backend, |
Austin Schuh | 5b728b7 | 2021-06-16 14:57:15 -0700 | [diff] [blame] | 333 | const Configuration *configuration, EventLoop *event_loop, |
| 334 | const Node *node); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 335 | ~MultiNodeLogNamer() override; |
| 336 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 337 | // Sets the function for creating encoders. The argument is the max message |
| 338 | // size (including headers) that will be written into this encoder. |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 339 | // |
| 340 | // Defaults to just creating DummyEncoders. |
| 341 | void set_encoder_factory( |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 342 | std::function<std::unique_ptr<DataEncoder>(size_t)> encoder_factory) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 343 | encoder_factory_ = std::move(encoder_factory); |
| 344 | } |
| 345 | |
| 346 | // Sets an additional file extension. |
| 347 | // |
| 348 | // Defaults to nothing. |
| 349 | void set_extension(std::string_view extension) { extension_ = extension; } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 350 | |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 351 | // A list of all the filenames we've written. |
| 352 | // |
| 353 | // This only includes the part after base_name(). |
| 354 | const std::vector<std::string> &all_filenames() const { |
| 355 | return all_filenames_; |
| 356 | } |
| 357 | |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 358 | void Rotate(const Node *node) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 359 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 360 | void WriteConfiguration( |
| 361 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header, |
| 362 | std::string_view config_sha256) override; |
| 363 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 364 | NewDataWriter *MakeWriter(const Channel *channel) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 365 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 366 | NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel, |
Austin Schuh | 7334084 | 2021-07-30 22:32:06 -0700 | [diff] [blame] | 367 | const Node *node) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 368 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 369 | NewDataWriter *MakeTimestampWriter(const Channel *channel) override; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 370 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 371 | // Indicates that at least one file ran out of space. Once this happens, we |
| 372 | // stop trying to open new files, to avoid writing any files with holes from |
| 373 | // previous parts. |
| 374 | // |
| 375 | // Besides this function, this object will silently stop logging data when |
| 376 | // this occurs. If you want to ensure log files are complete, you must call |
| 377 | // this method. |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 378 | bool ran_out_of_space() const { |
| 379 | return accumulate_data_writers<bool>( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 380 | ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 381 | CHECK_NOTNULL(data_writer.writer); |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 382 | return x || |
| 383 | (data_writer.writer && data_writer.writer->ran_out_of_space()); |
| 384 | }); |
| 385 | } |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 386 | |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 387 | // Returns the maximum total_bytes() value for all existing |
| 388 | // DetachedBufferWriters. |
| 389 | // |
| 390 | // Returns 0 if no files are open. |
| 391 | size_t maximum_total_bytes() const { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 392 | return accumulate_data_writers<size_t>( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 393 | 0, [](size_t x, const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 394 | CHECK_NOTNULL(data_writer.writer); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 395 | return std::max(x, data_writer.writer->total_bytes()); |
| 396 | }); |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 399 | // Closes all existing log files. No more data may be written after this. |
| 400 | // |
| 401 | // This may set ran_out_of_space(). |
Austin Schuh | 08dba8f | 2023-05-01 08:29:30 -0700 | [diff] [blame] | 402 | WriteCode Close() override; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 403 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 404 | // Accessors for various statistics. See the identically-named methods in |
| 405 | // DetachedBufferWriter for documentation. These are aggregated across all |
| 406 | // past and present DetachedBufferWriters. |
| 407 | std::chrono::nanoseconds max_write_time() const { |
| 408 | return accumulate_data_writers( |
| 409 | max_write_time_, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 410 | [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 411 | CHECK_NOTNULL(data_writer.writer); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 412 | return std::max( |
| 413 | x, data_writer.writer->WriteStatistics()->max_write_time()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 414 | }); |
| 415 | } |
| 416 | int max_write_time_bytes() const { |
| 417 | return std::get<0>(accumulate_data_writers( |
| 418 | std::make_tuple(max_write_time_bytes_, max_write_time_), |
| 419 | [](std::tuple<int, std::chrono::nanoseconds> x, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 420 | const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 421 | CHECK_NOTNULL(data_writer.writer); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 422 | if (data_writer.writer->WriteStatistics()->max_write_time() > |
| 423 | std::get<1>(x)) { |
| 424 | return std::make_tuple( |
| 425 | data_writer.writer->WriteStatistics()->max_write_time_bytes(), |
| 426 | data_writer.writer->WriteStatistics()->max_write_time()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 427 | } |
| 428 | return x; |
| 429 | })); |
| 430 | } |
| 431 | int max_write_time_messages() const { |
| 432 | return std::get<0>(accumulate_data_writers( |
| 433 | std::make_tuple(max_write_time_messages_, max_write_time_), |
| 434 | [](std::tuple<int, std::chrono::nanoseconds> x, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 435 | const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 436 | CHECK_NOTNULL(data_writer.writer); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 437 | if (data_writer.writer->WriteStatistics()->max_write_time() > |
| 438 | std::get<1>(x)) { |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 439 | return std::make_tuple( |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 440 | data_writer.writer->WriteStatistics() |
| 441 | ->max_write_time_messages(), |
| 442 | data_writer.writer->WriteStatistics()->max_write_time()); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 443 | } |
| 444 | return x; |
| 445 | })); |
| 446 | } |
| 447 | std::chrono::nanoseconds total_write_time() const { |
| 448 | return accumulate_data_writers( |
| 449 | total_write_time_, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 450 | [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 451 | CHECK_NOTNULL(data_writer.writer); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 452 | return x + data_writer.writer->WriteStatistics()->total_write_time(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 453 | }); |
| 454 | } |
| 455 | int total_write_count() const { |
| 456 | return accumulate_data_writers( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 457 | total_write_count_, [](int x, const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 458 | CHECK_NOTNULL(data_writer.writer); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 459 | return x + data_writer.writer->WriteStatistics()->total_write_count(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 460 | }); |
| 461 | } |
| 462 | int total_write_messages() const { |
| 463 | return accumulate_data_writers( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 464 | total_write_messages_, [](int x, const NewDataWriter &data_writer) { |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 465 | return x + |
| 466 | data_writer.writer->WriteStatistics()->total_write_messages(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 467 | }); |
| 468 | } |
| 469 | int total_write_bytes() const { |
| 470 | return accumulate_data_writers( |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 471 | total_write_bytes_, [](int x, const NewDataWriter &data_writer) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 472 | CHECK_NOTNULL(data_writer.writer); |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 473 | return x + data_writer.writer->WriteStatistics()->total_write_bytes(); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 474 | }); |
| 475 | } |
| 476 | |
| 477 | void ResetStatistics(); |
| 478 | |
Alexei Strots | caf17d3 | 2023-04-03 22:31:11 -0700 | [diff] [blame] | 479 | protected: |
| 480 | // TODO (Alexei): consider to move ownership of log_namer to concrete sub |
| 481 | // class and make log_backend_ raw pointer. |
| 482 | LogBackend *log_backend() { return log_backend_.get(); } |
| 483 | const LogBackend *log_backend() const { return log_backend_.get(); } |
| 484 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 485 | private: |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 486 | // Opens up a writer for timestamps forwarded back. |
| 487 | void OpenForwardedTimestampWriter(const Channel *channel, |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 488 | NewDataWriter *data_writer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 489 | |
| 490 | // Opens up a writer for remote data. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 491 | void OpenWriter(const Channel *channel, NewDataWriter *data_writer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 492 | |
| 493 | // Opens the main data writer file for this node responsible for data_writer_. |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 494 | void MakeDataWriter(); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 495 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 496 | void CreateBufferWriter(std::string_view path, size_t max_message_size, |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 497 | std::unique_ptr<DetachedBufferWriter> *destination); |
| 498 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 499 | void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer); |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 500 | |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 501 | // A version of std::accumulate which operates over all of our DataWriters. |
| 502 | template <typename T, typename BinaryOperation> |
| 503 | T accumulate_data_writers(T t, BinaryOperation op) const { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 504 | for (const std::pair<const Channel *const, NewDataWriter> &data_writer : |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 505 | data_writers_) { |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 506 | if (data_writer.second.writer != nullptr) { |
| 507 | t = op(std::move(t), data_writer.second); |
| 508 | } |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 509 | } |
Maxwell Gumley | 8ad7778 | 2023-07-11 13:27:03 -0600 | [diff] [blame] | 510 | if (data_writer_ != nullptr && data_writer_->writer != nullptr) { |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 511 | t = op(std::move(t), *data_writer_); |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 512 | } |
| 513 | return t; |
| 514 | } |
| 515 | |
Alexei Strots | caf17d3 | 2023-04-03 22:31:11 -0700 | [diff] [blame] | 516 | std::unique_ptr<LogBackend> log_backend_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 517 | |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 518 | bool ran_out_of_space_ = false; |
Brian Silverman | a621f52 | 2020-09-30 16:52:43 -0700 | [diff] [blame] | 519 | std::vector<std::string> all_filenames_; |
Brian Silverman | 0465fcf | 2020-09-24 00:29:18 -0700 | [diff] [blame] | 520 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 521 | std::function<std::unique_ptr<DataEncoder>(size_t)> encoder_factory_; |
Brian Silverman | cb80582 | 2020-10-06 17:43:35 -0700 | [diff] [blame] | 522 | std::string extension_; |
| 523 | |
| 524 | // Storage for statistics from previously-rotated DetachedBufferWriters. |
| 525 | std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero(); |
| 526 | int max_write_time_bytes_ = -1; |
| 527 | int max_write_time_messages_ = -1; |
| 528 | std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero(); |
| 529 | int total_write_count_ = 0; |
| 530 | int total_write_messages_ = 0; |
| 531 | int total_write_bytes_ = 0; |
| 532 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 533 | // File to write both delivery timestamps and local data to. |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 534 | std::unique_ptr<NewDataWriter> data_writer_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 535 | |
Austin Schuh | b8bca73 | 2021-07-30 22:32:00 -0700 | [diff] [blame] | 536 | std::map<const Channel *, NewDataWriter> data_writers_; |
Mithun Bharadwaj | 0c62993 | 2023-08-02 16:10:40 -0700 | [diff] [blame^] | 537 | |
| 538 | // Data writer per remote node. |
| 539 | std::map<const Node *, NewDataWriter *> node_data_writers_; |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 540 | }; |
| 541 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 542 | // This is specialized log namer that deals with directory centric log events. |
| 543 | class MultiNodeFilesLogNamer : public MultiNodeLogNamer { |
| 544 | public: |
| 545 | MultiNodeFilesLogNamer(std::string_view base_name, EventLoop *event_loop) |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 546 | : MultiNodeLogNamer( |
| 547 | std::make_unique<RenamableFileBackend>(base_name, false), |
| 548 | event_loop) {} |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 549 | |
| 550 | MultiNodeFilesLogNamer(std::string_view base_name, |
| 551 | const Configuration *configuration, |
| 552 | EventLoop *event_loop, const Node *node) |
colleen | 61276dc | 2023-06-01 09:23:29 -0700 | [diff] [blame] | 553 | : MultiNodeLogNamer( |
| 554 | std::make_unique<RenamableFileBackend>(base_name, false), |
| 555 | configuration, event_loop, node) {} |
| 556 | |
| 557 | MultiNodeFilesLogNamer(EventLoop *event_loop, |
| 558 | std::unique_ptr<RenamableFileBackend> backend) |
| 559 | : MultiNodeLogNamer(std::move(backend), event_loop) {} |
| 560 | |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 561 | ~MultiNodeFilesLogNamer() override = default; |
Alexei Strots | caf17d3 | 2023-04-03 22:31:11 -0700 | [diff] [blame] | 562 | |
| 563 | std::string_view base_name() const { |
| 564 | return renamable_file_backend()->base_name(); |
| 565 | } |
| 566 | |
| 567 | // Rotate should be called at least once in between calls to set_base_name. |
| 568 | // Otherwise, temporary files will not be recoverable. |
| 569 | // Rotate is called by Logger::RenameLogBase, which is currently the only user |
| 570 | // of this method. |
| 571 | // Only renaming the folder is supported, not the file base name. |
| 572 | void set_base_name(std::string_view base_name) { |
| 573 | renamable_file_backend()->RenameLogBase(base_name); |
| 574 | } |
| 575 | |
| 576 | // When enabled, this will write files under names beginning |
| 577 | // with the .tmp suffix, and then rename them to the desired name after |
| 578 | // they are fully written. |
| 579 | // |
| 580 | // This is useful to enable incremental copying of the log files. |
| 581 | // |
| 582 | // Defaults to writing directly to the final filename. |
| 583 | void EnableTempFiles() { renamable_file_backend()->EnableTempFiles(); } |
| 584 | |
| 585 | private: |
| 586 | RenamableFileBackend *renamable_file_backend() { |
| 587 | return reinterpret_cast<RenamableFileBackend *>(log_backend()); |
| 588 | } |
| 589 | const RenamableFileBackend *renamable_file_backend() const { |
| 590 | return reinterpret_cast<const RenamableFileBackend *>(log_backend()); |
| 591 | } |
Alexei Strots | 0139549 | 2023-03-20 13:59:56 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
Austin Schuh | cb5601b | 2020-09-10 15:29:59 -0700 | [diff] [blame] | 594 | } // namespace logger |
| 595 | } // namespace aos |
| 596 | |
| 597 | #endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_ |