blob: d9601ae98f2b437e8c0e24bb62d87409db9057d9 [file] [log] [blame]
Austin Schuhcb5601b2020-09-10 15:29:59 -07001#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
10#include "aos/events/logging/logfile_utils.h"
11#include "aos/events/logging/logger_generated.h"
12#include "aos/events/logging/uuid.h"
13#include "flatbuffers/flatbuffers.h"
14
15namespace aos {
16namespace logger {
17
18// Interface describing how to name, track, and add headers to log file parts.
19class LogNamer {
20 public:
21 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
22 // being node.
23 LogNamer(const Node *node) : node_(node) { nodes_.emplace_back(node_); }
24 virtual ~LogNamer() {}
25
26 // Writes the header to all log files for a specific node. This function
27 // needs to be called after all the writers are created.
28 //
29 // Modifies header to contain the uuid and part number for each writer as it
30 // writes it. Since this is done unconditionally, it does not restore the
31 // previous value at the end.
32 virtual void WriteHeader(
33 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
34 const Node *node) = 0;
35
Brian Silverman87ac0402020-09-17 14:47:01 -070036 // Returns a writer for writing data from messages on this channel (on the
37 // primary node).
38 //
39 // The returned pointer will stay valid across rotations, but the object it
40 // points to will be assigned to.
Austin Schuhcb5601b2020-09-10 15:29:59 -070041 virtual DetachedBufferWriter *MakeWriter(const Channel *channel) = 0;
42
Brian Silverman87ac0402020-09-17 14:47:01 -070043 // Returns a writer for writing timestamps from messages on this channel (on
44 // the primary node).
45 //
46 // The returned pointer will stay valid across rotations, but the object it
47 // points to will be assigned to.
Austin Schuhcb5601b2020-09-10 15:29:59 -070048 virtual DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) = 0;
49
50 // Returns a writer for writing timestamps delivered over the special
51 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -070052 // are forwarded back from (to the primary node).
53 //
54 // The returned pointer will stay valid across rotations, but the object it
55 // points to will be assigned to.
Austin Schuhcb5601b2020-09-10 15:29:59 -070056 virtual DetachedBufferWriter *MakeForwardedTimestampWriter(
57 const Channel *channel, const Node *node) = 0;
58
59 // Rotates all log files for the provided node. The provided header will be
60 // modified and written per WriteHeader above.
61 virtual void Rotate(
62 const Node *node,
63 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header) = 0;
64
65 // Returns all the nodes that data is being written for.
66 const std::vector<const Node *> &nodes() const { return nodes_; }
67
68 // Returns the node the logger is running on.
69 const Node *node() const { return node_; }
70
71 protected:
72 // Modifies the header to have the provided UUID and part id.
73 void UpdateHeader(
74 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
75 const UUID &uuid, int part_id) const;
76
77 const Node *const node_;
78 std::vector<const Node *> nodes_;
79};
80
81// Local log namer is a simple version which only names things
82// "base_name.part#.bfbs" and increments the part number. It doesn't support
83// any other log type.
84class LocalLogNamer : public LogNamer {
85 public:
86 LocalLogNamer(std::string_view base_name, const Node *node)
87 : LogNamer(node),
88 base_name_(base_name),
89 uuid_(UUID::Random()),
90 data_writer_(OpenDataWriter()) {}
Brian Silverman0465fcf2020-09-24 00:29:18 -070091 ~LocalLogNamer() override = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -070092
93 void WriteHeader(
94 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
95 const Node *node) override;
96
97 DetachedBufferWriter *MakeWriter(const Channel *channel) override;
98
99 void Rotate(const Node *node,
100 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header)
101 override;
102
103 DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) override;
104
105 DetachedBufferWriter *MakeForwardedTimestampWriter(
106 const Channel * /*channel*/, const Node * /*node*/) override;
107
108 private:
109 // Creates a new data writer with the new part number.
110 std::unique_ptr<DetachedBufferWriter> OpenDataWriter() {
111 return std::make_unique<DetachedBufferWriter>(
Brian Silvermanf51499a2020-09-21 12:49:08 -0700112 absl::StrCat(base_name_, ".part", part_number_, ".bfbs"),
113 std::make_unique<aos::logger::DummyEncoder>());
Austin Schuhcb5601b2020-09-10 15:29:59 -0700114 }
115
116 const std::string base_name_;
117 const UUID uuid_;
118 size_t part_number_ = 0;
119 std::unique_ptr<DetachedBufferWriter> data_writer_;
120};
121
122// Log namer which uses a config and a base name to name a bunch of files.
123class MultiNodeLogNamer : public LogNamer {
124 public:
Brian Silverman48deab12020-09-30 18:39:28 -0700125 // If temp_suffix is set, then this will write files under names beginning
126 // with the specified suffix, and then rename them to the desired name after
127 // they are fully written.
128 //
129 // This is useful to enable incremental copying of the log files.
130 //
131 // Defaults to writing directly to the final filename.
Austin Schuhcb5601b2020-09-10 15:29:59 -0700132 MultiNodeLogNamer(std::string_view base_name,
Brian Silverman48deab12020-09-30 18:39:28 -0700133 const Configuration *configuration, const Node *node,
134 std::string_view temp_suffix = "");
135 ~MultiNodeLogNamer() override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700136
Brian Silverman1f345222020-09-24 21:14:48 -0700137 std::string_view base_name() const { return base_name_; }
138
Brian Silvermana621f522020-09-30 16:52:43 -0700139 // A list of all the filenames we've written.
140 //
141 // This only includes the part after base_name().
142 const std::vector<std::string> &all_filenames() const {
143 return all_filenames_;
144 }
145
Austin Schuhcb5601b2020-09-10 15:29:59 -0700146 void WriteHeader(
147 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
148 const Node *node) override;
149
150 void Rotate(const Node *node,
151 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header)
152 override;
153
154 DetachedBufferWriter *MakeWriter(const Channel *channel) override;
155
Brian Silvermand90905f2020-09-23 14:42:56 -0700156 DetachedBufferWriter *MakeForwardedTimestampWriter(const Channel *channel,
157 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700158
159 DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) override;
160
Brian Silverman0465fcf2020-09-24 00:29:18 -0700161 // Indicates that at least one file ran out of space. Once this happens, we
162 // stop trying to open new files, to avoid writing any files with holes from
163 // previous parts.
164 //
165 // Besides this function, this object will silently stop logging data when
166 // this occurs. If you want to ensure log files are complete, you must call
167 // this method.
168 bool ran_out_of_space() const { return ran_out_of_space_; }
169
Brian Silverman1f345222020-09-24 21:14:48 -0700170 // Returns the maximum total_bytes() value for all existing
171 // DetachedBufferWriters.
172 //
173 // Returns 0 if no files are open.
174 size_t maximum_total_bytes() const {
175 size_t result = 0;
176 for (const std::pair<const Channel *const, DataWriter> &data_writer :
177 data_writers_) {
178 result = std::max(result, data_writer.second.writer->total_bytes());
179 }
180 if (data_writer_) {
181 result = std::max(result, data_writer_->total_bytes());
182 }
183 return result;
184 }
185
Brian Silverman0465fcf2020-09-24 00:29:18 -0700186 // Closes all existing log files. No more data may be written after this.
187 //
188 // This may set ran_out_of_space().
189 void Close();
190
Austin Schuhcb5601b2020-09-10 15:29:59 -0700191 private:
192 // Files to write remote data to. We want one per channel. Maps the channel
193 // to the writer, Node, and part number.
194 struct DataWriter {
195 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
196 const Node *node;
197 size_t part_number = 0;
198 UUID uuid = UUID::Random();
199 std::function<void(const Channel *, DataWriter *)> rotate;
200 };
201
202 // Opens up a writer for timestamps forwarded back.
203 void OpenForwardedTimestampWriter(const Channel *channel,
204 DataWriter *data_writer);
205
206 // Opens up a writer for remote data.
207 void OpenWriter(const Channel *channel, DataWriter *data_writer);
208
209 // Opens the main data writer file for this node responsible for data_writer_.
Brian Silvermana621f522020-09-30 16:52:43 -0700210 void OpenDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700211
Brian Silvermana621f522020-09-30 16:52:43 -0700212 void CreateBufferWriter(std::string_view path,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700213 std::unique_ptr<DetachedBufferWriter> *destination);
214
Brian Silverman48deab12020-09-30 18:39:28 -0700215 void RenameTempFile(DetachedBufferWriter *destination);
216
Austin Schuhcb5601b2020-09-10 15:29:59 -0700217 const std::string base_name_;
Brian Silverman48deab12020-09-30 18:39:28 -0700218 const std::string temp_suffix_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700219 const Configuration *const configuration_;
220 const UUID uuid_;
221
222 size_t part_number_ = 0;
223
Brian Silverman0465fcf2020-09-24 00:29:18 -0700224 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700225 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700226
Austin Schuhcb5601b2020-09-10 15:29:59 -0700227 // File to write both delivery timestamps and local data to.
228 std::unique_ptr<DetachedBufferWriter> data_writer_;
229
230 std::map<const Channel *, DataWriter> data_writers_;
231};
232
233} // namespace logger
234} // namespace aos
235
236#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_