blob: 5265f24dcd0e3c2e0957d7a9d2316fe5e4402627 [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
129 void WriteHeader(
130 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
131 const Node *node) override;
132
133 void Rotate(const Node *node,
134 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header)
135 override;
136
137 DetachedBufferWriter *MakeWriter(const Channel *channel) override;
138
Brian Silvermand90905f2020-09-23 14:42:56 -0700139 DetachedBufferWriter *MakeForwardedTimestampWriter(const Channel *channel,
140 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700141
142 DetachedBufferWriter *MakeTimestampWriter(const Channel *channel) override;
143
Brian Silverman0465fcf2020-09-24 00:29:18 -0700144 // Indicates that at least one file ran out of space. Once this happens, we
145 // stop trying to open new files, to avoid writing any files with holes from
146 // previous parts.
147 //
148 // Besides this function, this object will silently stop logging data when
149 // this occurs. If you want to ensure log files are complete, you must call
150 // this method.
151 bool ran_out_of_space() const { return ran_out_of_space_; }
152
153 // Closes all existing log files. No more data may be written after this.
154 //
155 // This may set ran_out_of_space().
156 void Close();
157
Austin Schuhcb5601b2020-09-10 15:29:59 -0700158 private:
159 // Files to write remote data to. We want one per channel. Maps the channel
160 // to the writer, Node, and part number.
161 struct DataWriter {
162 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
163 const Node *node;
164 size_t part_number = 0;
165 UUID uuid = UUID::Random();
166 std::function<void(const Channel *, DataWriter *)> rotate;
167 };
168
169 // Opens up a writer for timestamps forwarded back.
170 void OpenForwardedTimestampWriter(const Channel *channel,
171 DataWriter *data_writer);
172
173 // Opens up a writer for remote data.
174 void OpenWriter(const Channel *channel, DataWriter *data_writer);
175
176 // Opens the main data writer file for this node responsible for data_writer_.
177 std::unique_ptr<DetachedBufferWriter> OpenDataWriter();
178
Brian Silverman0465fcf2020-09-24 00:29:18 -0700179 void CreateBufferWriter(std::string_view filename,
180 std::unique_ptr<DetachedBufferWriter> *destination);
181
Austin Schuhcb5601b2020-09-10 15:29:59 -0700182 const std::string base_name_;
183 const Configuration *const configuration_;
184 const UUID uuid_;
185
186 size_t part_number_ = 0;
187
Brian Silverman0465fcf2020-09-24 00:29:18 -0700188 bool ran_out_of_space_ = false;
189
Austin Schuhcb5601b2020-09-10 15:29:59 -0700190 // File to write both delivery timestamps and local data to.
191 std::unique_ptr<DetachedBufferWriter> data_writer_;
192
193 std::map<const Channel *, DataWriter> data_writers_;
194};
195
196} // namespace logger
197} // namespace aos
198
199#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_