RTC Toolkit 6.0.0-pre2
Loading...
Searching...
No Matches
dataPointRecordingUnit.hpp
Go to the documentation of this file.
1
12#ifndef RTCTK_COMPONENTFRAMEWORK_DATAPOINTRECORDINGUNIT_HPP
13#define RTCTK_COMPONENTFRAMEWORK_DATAPOINTRECORDINGUNIT_HPP
14
15#include <memory>
23#include <taiclock/taiClock.hpp>
24
25#include <fmt/format.h>
26#include <numapp/numapolicies.hpp>
27#include <numapp/thread.hpp>
28
29#include <cstdint>
30#include <exception>
31#include <string>
32#include <string_view>
33#include <thread>
34#include <typeinfo>
35#include <vector>
36
38
39using namespace std::string_view_literals;
40
41enum class DataPointRecordingSource : uint8_t { OLDB, RUNTIMEREPO };
42
48template <typename DpType>
49class [[deprecated("Use RepositoryRecordingUnit instead.")]]DataPointRecordingUnit : public RecordingUnit {
50public:
51 // we are using a bit-mask to configure when to capture data
52 using CaptureMask = uint32_t;
53 static constexpr uint32_t CAPTURE_ON_START = 1;
54 static constexpr uint32_t CAPTURE_ON_STOP = 2;
55 static constexpr uint32_t CAPTURE_ON_CHANGE = 4;
56
57 using TimepointType = taiclock::TaiClock::time_point::rep;
58
59 using OutputDpType = std::conditional_t<IS_SPAN_CONVERTIBLE<DpType>, AsSpan<DpType>, DpType>;
61
72 DataPointRecordingUnit(const std::string& comp_id,
73 const std::string& unit_id,
74 ServiceContainer& services,
75 const DataPointPath& dp_path,
77 std::unique_ptr<OutputStageType>&& output_stage = {},
78 std::optional<size_t> fixed_recording_length = std::nullopt)
79 : RecordingUnit(comp_id, unit_id, "DataPoint", services)
80 , m_dp_path{dp_path}
81 , m_recording_source{source}
82 , m_dp_buffer()
83 , m_output{std::move(output_stage)}
84 , m_recording_length{fixed_recording_length} {
85 if (m_output == nullptr) {
86 m_output = std::make_unique<FitsRecorder<TimepointType, OutputDpType>>(COLUMNS);
87 }
88
89 if (m_recording_source == DataPointRecordingSource::OLDB) {
90 m_repository = static_cast<RepositoryIf*>(&m_oldb);
91 m_repository_subscriber = static_cast<RepositorySubscriberIf*>(&m_oldb);
92 } else if (m_recording_source == DataPointRecordingSource::RUNTIMEREPO) {
93 m_repository = static_cast<RepositoryIf*>(&m_rtr);
94 m_repository_subscriber = static_cast<RepositorySubscriberIf*>(&m_rtr);
95 } else {
96 CII_THROW(InvalidSetting, "Invalid recording source");
97 }
98 if (not m_repository or not m_repository_subscriber) {
99 CII_THROW(InvalidSetting, "Invalid recording source");
100 }
101
102 auto input_dp_path = DataPointPath(fmt::format(OLDB_PATH_DP_NAME, comp_id, unit_id));
103 if (not m_oldb.DataPointExists(input_dp_path)) {
104 m_oldb.CreateDataPoint<std::string>(input_dp_path);
105 }
106 m_oldb.SetDataPoint<std::string>(input_dp_path, dp_path);
107
108 auto capture_mask_path =
109 DataPointPath(fmt::format(RTR_PATH_CAPTURE_MASK, comp_id, unit_id));
110 if (m_rtr.DataPointExists(capture_mask_path)) {
111 m_capture_mask = m_rtr.GetDataPoint<int32_t>(capture_mask_path);
112 } else {
113 m_capture_mask = CAPTURE_ON_START + CAPTURE_ON_CHANGE + CAPTURE_ON_STOP;
114 }
115
116 const InfluxTagMap base_tags = {
117 {"unit_id", unit_id}
118 };
119
120 m_samples_written_reg = m_metrics.AddCounter(
121 &m_samples_written, CounterMetricInfo(
122 unit_id + "/samples_written",
123 "Samples written",
124 base_tags,
125 "samples_written"
126 )
127 );
128 }
129
131 m_stop = true;
132 if (m_process_thread.joinable()) {
133 m_process_thread.join();
134 }
135 SetStopped();
136 }
137
141 void Prepare(const std::filesystem::path& file_path) override {
142 if(not IsEnabled()) {
143 return;
144 }
145
148 "Error in transition to PREPARING, DataPointRecordingUnit not in STOPPED State");
149
150 m_file_path = file_path / (GetId() + m_output->DefaultFileExtension());
151
152 if (not m_repository->DataPointExists(m_dp_path)) {
153 SetFailed(nullptr);
154 CII_THROW(InvalidSetting, "DataPoint does not exist");
155 }
156
157 if (m_capture_mask == 0) {
158 SetFailed(nullptr);
159 CII_THROW(InvalidSetting, "Invalid capture mask 0");
160 }
161
162 using FitsRecorderType = FitsRecorder<TimepointType, OutputDpType>;
163 FitsRecorderType* fits_output = dynamic_cast<FitsRecorderType*>(m_output.get());
164 if (fits_output != nullptr) {
165 auto rec_length =
166 m_recording_length.value_or(m_repository->GetDataPointSize(m_dp_path));
167 fits_output->SetColumnLength(1, rec_length);
168 }
169
170 auto policies = numapp::NumaPolicies();
171 m_process_thread =
172 numapp::MakeThread(m_unit_id.substr(0, 15), policies, [&]() { return Process(); });
173 }
174
177 void Start() override {
178 m_start = true;
179 }
180
184 std::vector<std::filesystem::path> Stop() override {
185 m_stop = true;
186 if (m_process_thread.joinable()) {
187 m_process_thread.join();
188 }
189 std::vector<std::filesystem::path> files;
190 if (m_file_path) {
191 files.push_back(*m_file_path);
192 }
193 m_file_path.reset();
194 SetStopped();
195 return files;
196 }
197
198 static constexpr typename OutputStageType::ColumnDescription COLUMNS =
199 typename OutputStageType::ColumnDescription{{{"timestamp"sv, ""sv}, {"payload"sv, ""sv}}};
200
201private:
202 void Process() {
203 using namespace std::chrono_literals;
204
205 try {
206 m_output->Open(*m_file_path);
207 m_samples_written.Store(0);
209
210 if (m_capture_mask & CAPTURE_ON_CHANGE) {
211 subscription = std::move(m_repository_subscriber->Subscribe<DpType>(
212 m_dp_path,
213 [this](auto& path, auto& value, auto& metadata) {
214 if (GetState() == State::RUNNING) {
215 try {
216 m_output->Write(AsTuple(value));
217 m_samples_written++;
218 } catch (...) {
219 SetFailed(std::current_exception());
220 return;
221 }
222 }
223 },
224 // NOLINTNEXTLINE(performance-unnecessary-value-param)
225 [this](std::exception_ptr error) {
226 if (error) {
227 std::rethrow_exception(error);
228 }
229 }));
230 }
231
232 SetState(State::IDLE,
233 State::PREPARING,
234 "Error in transition to IDLE, DataPointRecordingUnit not in PREPARING State");
235
236 while (m_stop == false) {
237 switch (GetState()) {
238 case State::IDLE:
239 if (m_start) {
240 SetState(State::WAITING,
241 State::IDLE,
242 "Error in transition to WAITING, DataPointRecordingUnit not in "
243 "IDLE State");
244 break;
245 }
246 std::this_thread::sleep_for(1ms);
247 break;
248 case State::WAITING:
249 if (not HasLeaders() or (HasLeaders() and HasFirstLeaderStarted())) {
250 if (m_capture_mask & CAPTURE_ON_START) {
251 m_repository->ReadDataPoint(m_dp_path, m_dp_buffer);
252 m_output->Write(AsTuple(m_dp_buffer));
253 m_samples_written++;
254 }
255
256 SetState(State::RUNNING,
257 State::WAITING,
258 "Error in transition to RUNNING, DataPointRecordingUnit not in "
259 "WAITING State");
260 break;
261 }
262 std::this_thread::sleep_for(1ms);
263 break;
264 case State::RUNNING:
265 if (HasLeaders() and HasLastLeaderFinished()) {
266 SetState(State::FINISHED,
267 State::RUNNING,
268 "Error in transition to FINISHED, DataPointRecordingUnit not in "
269 "RUNNING State");
270 break;
271 }
272 std::this_thread::sleep_for(1ms);
273 break;
274 default:
275 std::this_thread::sleep_for(1ms);
276 }
277 }
278
279 if (m_capture_mask & CAPTURE_ON_CHANGE) {
280 subscription.Unsubscribe();
281 }
282
283 if (m_capture_mask & CAPTURE_ON_STOP) {
284 m_repository->ReadDataPoint(m_dp_path, m_dp_buffer);
285 m_output->Write(AsTuple(m_dp_buffer));
286 m_samples_written++;
287 }
288
289 m_output->Close();
290 // State is set to stopped after join in Stop function
291 m_start = false;
292 m_stop = false;
293 ResetLeaderStates();
294
295 } catch (...) {
296 m_output->Close();
297 m_start = false;
298 m_stop = false;
299 SetFailed(std::current_exception());
300 ResetLeaderStates();
301 }
302 }
303
304 static std::tuple<TimepointType, OutputDpType> AsTuple(const DpType& data) {
305 auto ts = taiclock::TaiClock::now().time_since_epoch().count();
306 if constexpr (IS_SPAN_CONVERTIBLE<DpType>) {
307 return std::make_tuple(ts, ToSpan(data));
308 } else {
309 return std::make_tuple(ts, data);
310 }
311 }
312
313 std::optional<std::filesystem::path> m_file_path;
314
315 DataPointPath m_dp_path;
316
317 DataPointRecordingSource m_recording_source;
318
319 RepositoryIf* m_repository;
320 RepositorySubscriberIf* m_repository_subscriber;
321
322 DpType m_dp_buffer;
323
324 CaptureMask m_capture_mask;
325
326 std::unique_ptr<OutputStageType> m_output;
327
328 std::optional<size_t> m_recording_length;
329
330 std::atomic<bool> m_start = false;
331 std::atomic<bool> m_stop = false;
332 std::thread m_process_thread;
333
334 perfc::CounterI64 m_samples_written;
335 perfc::ScopedRegistration m_samples_written_reg;
336
340 inline static constexpr std::string_view RTR_PATH_CAPTURE_MASK =
341 "/{}/static/rec_units/{}/capture_mask";
345 inline static constexpr std::string_view OLDB_PATH_DP_NAME = "/{}/rec_units/{}/dp_name";
346};
347
348} // namespace rtctk::componentFramework
349
350#endif // RTCTK_COMPONENTFRAMEWORK_DATAPOINTRECORDINGUNIT_HPP
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:77
static constexpr uint32_t CAPTURE_ON_START
Definition dataPointRecordingUnit.hpp:53
static constexpr OutputStageType::ColumnDescription COLUMNS
Definition dataPointRecordingUnit.hpp:198
std::vector< std::filesystem::path > Stop() override
Stop the recording and wait for it's termination.
Definition dataPointRecordingUnit.hpp:184
DataRecorder< TimepointType, OutputDpType > OutputStageType
Definition dataPointRecordingUnit.hpp:60
std::conditional_t< IS_SPAN_CONVERTIBLE< DpType >, AsSpan< DpType >, DpType > OutputDpType
Definition dataPointRecordingUnit.hpp:59
void Prepare(const std::filesystem::path &file_path) override
Prepare the recording.
Definition dataPointRecordingUnit.hpp:141
taiclock::TaiClock::time_point::rep TimepointType
Definition dataPointRecordingUnit.hpp:57
~DataPointRecordingUnit() override
Definition dataPointRecordingUnit.hpp:130
uint32_t CaptureMask
Definition dataPointRecordingUnit.hpp:52
static constexpr uint32_t CAPTURE_ON_CHANGE
Definition dataPointRecordingUnit.hpp:55
DataPointRecordingUnit(const std::string &comp_id, const std::string &unit_id, ServiceContainer &services, const DataPointPath &dp_path, DataPointRecordingSource source=DataPointRecordingSource::RUNTIMEREPO, std::unique_ptr< OutputStageType > &&output_stage={}, std::optional< size_t > fixed_recording_length=std::nullopt)
Create a new recording unit.
Definition dataPointRecordingUnit.hpp:72
void Start() override
Start the recording.
Definition dataPointRecordingUnit.hpp:177
static constexpr uint32_t CAPTURE_ON_STOP
Definition dataPointRecordingUnit.hpp:54
This is an abstract class that can be used to implement an OutputStage for a Recording Unit.
Definition dataRecorder.hpp:29
const std::array< ColumnMetaData, sizeof...(T)> ColumnDescription
Definition dataRecorder.hpp:49
Definition fitsDataRecorder.hpp:71
This Exception is raised when a invalid setting was used in the runtime repo.
Definition exceptions.hpp:340
Abstract base class for all sources that can be recorded by the MetadataCollector and TelemetryRecord...
Definition recordingUnit.hpp:51
RecordingUnit(const std::string &comp_id, const std::string &unit_id, const std::string &unit_type, ServiceContainer &services)
Create a new RecordingIngestion.
Definition recordingUnit.cpp:19
void SetFailed(const std::exception_ptr &exception)
Set the unit into failed state, with the given exception.
Definition recordingUnit.cpp:150
std::string GetId()
Get the unit_it of this RecordingUnit.
Definition recordingUnit.cpp:128
std::string m_unit_id
Definition recordingUnit.hpp:176
void SetStopped()
Set the Unit state to STOPPED independent of the current State.
Definition recordingUnit.cpp:157
@ STOPPED
Definition recordingUnit.hpp:53
@ PREPARING
Definition recordingUnit.hpp:53
bool SetState(State state, State precondition)
Sets the new state, only goes to new state, if expected state matches.
Definition recordingUnit.cpp:132
bool IsEnabled()
Checks whether the Recording Unit is enabled.
Definition recordingUnit.cpp:166
Abstract interface providing basic read and write facilities to a repository.
Definition repositoryIf.hpp:51
RAII wrapper class used to manage the life-time of individual subscriptions.
Definition repositorySubscriberIf.hpp:64
Abstract interface providing I/O and additional subscription facilities for a repository.
Definition repositorySubscriberIf.hpp:27
Container class that holds services of any type.
Definition serviceContainer.hpp:39
Header file for ComponentMetricsIf.
Provides an abstract DataRecorder class as the output stage for a recording unit.
Provides macros and utilities for exception handling.
FitsRecorder allows to write ColumnData to into fits files in a specified directory.
Definition commandReplier.cpp:22
DataPointRecordingSource
Definition dataPointRecordingUnit.hpp:41
@ RUNTIMEREPO
Definition dataPointRecordingUnit.hpp:41
@ OLDB
Definition dataPointRecordingUnit.hpp:41
AsSpanT< T > ToSpan(T &data)
Simple function that converts types that are convertible to spans to a span.
Definition recordingUtils.hpp:25
constexpr bool IS_SPAN_CONVERTIBLE
Small helper alias for IsSpanConvertible.
Definition recordingTypeTraits.hpp:66
Definition ddsSub.hpp:156
FitsRecorder allows to write ColumnData to into fits files in a specified directory.
Abstract base class defining functionality common to all recording units.
A container that can hold any type of service.
Gets the span type for converting type T to a span.
Definition recordingTypeTraits.hpp:28