blob: 661f28db39ca45e17c35c19159719e693ef7b66c [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:
125 MultiNodeLogNamer(std::string_view base_name,
126 const Configuration *configuration, const Node *node);
Brian Silverman0465fcf2020-09-24 00:29:18 -0700127 ~MultiNodeLogNamer() override = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700128
Brian Silverman1f345222020-09-24 21:14:48 -0700129 std::string_view base_name() const { return base_name_; }
130
Austin Schuhcb5601b2020-09-10 15:29:59 -0700131 void WriteHeader(
132 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
133 const Node *node) override;
134
135 void Rotate(const Node *node,
136 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header)
137 override;
138
139 DetachedBufferWriter *MakeWriter(const Channel *channel) override;
140
Brian Silvermand90905f2020-09-23 14:42:56 -0700141 DetachedBufferWriter *MakeForwardedTimestampWriter(const Channel *channel,
142 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700143
144 DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) override;
145
Brian Silverman0465fcf2020-09-24 00:29:18 -0700146 // Indicates that at least one file ran out of space. Once this happens, we
147 // stop trying to open new files, to avoid writing any files with holes from
148 // previous parts.
149 //
150 // Besides this function, this object will silently stop logging data when
151 // this occurs. If you want to ensure log files are complete, you must call
152 // this method.
153 bool ran_out_of_space() const { return ran_out_of_space_; }
154
Brian Silverman1f345222020-09-24 21:14:48 -0700155 // Returns the maximum total_bytes() value for all existing
156 // DetachedBufferWriters.
157 //
158 // Returns 0 if no files are open.
159 size_t maximum_total_bytes() const {
160 size_t result = 0;
161 for (const std::pair<const Channel *const, DataWriter> &data_writer :
162 data_writers_) {
163 result = std::max(result, data_writer.second.writer->total_bytes());
164 }
165 if (data_writer_) {
166 result = std::max(result, data_writer_->total_bytes());
167 }
168 return result;
169 }
170
Brian Silverman0465fcf2020-09-24 00:29:18 -0700171 // Closes all existing log files. No more data may be written after this.
172 //
173 // This may set ran_out_of_space().
174 void Close();
175
Austin Schuhcb5601b2020-09-10 15:29:59 -0700176 private:
177 // Files to write remote data to. We want one per channel. Maps the channel
178 // to the writer, Node, and part number.
179 struct DataWriter {
180 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
181 const Node *node;
182 size_t part_number = 0;
183 UUID uuid = UUID::Random();
184 std::function<void(const Channel *, DataWriter *)> rotate;
185 };
186
187 // Opens up a writer for timestamps forwarded back.
188 void OpenForwardedTimestampWriter(const Channel *channel,
189 DataWriter *data_writer);
190
191 // Opens up a writer for remote data.
192 void OpenWriter(const Channel *channel, DataWriter *data_writer);
193
194 // Opens the main data writer file for this node responsible for data_writer_.
195 std::unique_ptr<DetachedBufferWriter> OpenDataWriter();
196
Brian Silverman0465fcf2020-09-24 00:29:18 -0700197 void CreateBufferWriter(std::string_view filename,
198 std::unique_ptr<DetachedBufferWriter> *destination);
199
Austin Schuhcb5601b2020-09-10 15:29:59 -0700200 const std::string base_name_;
201 const Configuration *const configuration_;
202 const UUID uuid_;
203
204 size_t part_number_ = 0;
205
Brian Silverman0465fcf2020-09-24 00:29:18 -0700206 bool ran_out_of_space_ = false;
207
Austin Schuhcb5601b2020-09-10 15:29:59 -0700208 // File to write both delivery timestamps and local data to.
209 std::unique_ptr<DetachedBufferWriter> data_writer_;
210
211 std::map<const Channel *, DataWriter> data_writers_;
212};
213
214} // namespace logger
215} // namespace aos
216
217#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_