11#ifndef RTCTK_COMPONENTFRAMEWORK_EVENTRECORDINGUNIT_HPP
12#define RTCTK_COMPONENTFRAMEWORK_EVENTRECORDINGUNIT_HPP
22#include <fmt/format.h>
23#include <numapp/numapolicies.hpp>
24#include <numapp/thread.hpp>
25#include <taiclock/taiClock.hpp>
42template <
class OutputStage = std::ofstream>
55 const std::string& unit_id,
61 m_input_filter = std::move(input_filter);
63 m_input_filter = [](
const JsonPayload&) {
return true; };
66 auto rtr_topic_path =
DataPointPath(fmt::format(RTR_PATH_TOPIC_NAME, comp_id, unit_id));
67 m_topic_name =
m_rtr.GetDataPoint<std::string>(rtr_topic_path);
69 auto oldb_topic_path =
DataPointPath(fmt::format(OLDB_PATH_TOPIC_NAME, comp_id, unit_id));
70 m_oldb.CreateDataPoint<std::string>(oldb_topic_path);
71 m_oldb.SetDataPoint<std::string>(oldb_topic_path, m_topic_name);
75 {
"topic_name", m_topic_name},
78 m_samples_written_reg =
m_metrics.AddCounter(
81 unit_id +
"/samples_written",
"Samples written", base_tags,
"samples_written"));
86 if (m_process_thread.joinable()) {
87 m_process_thread.join();
96 void Prepare(
const std::filesystem::path& file_path)
override {
103 "EventRecorder tried to go from non-STOPPED State to PREPARING");
105 m_subscriber.reset();
106 m_subscriber = m_event_service.MakeSubscriber(m_topic_name);
110 auto policies = numapp::NumaPolicies();
112 numapp::MakeThread(
m_unit_id.substr(0, 15), policies, [&]() { return Process(); });
124 std::vector<std::filesystem::path>
Stop()
override {
126 if (m_process_thread.joinable()) {
127 m_process_thread.join();
130 std::vector<std::filesystem::path> files;
139 using namespace std::chrono_literals;
145 m_samples_written.Store(0);
149 "Event recorder was not in PREPARING when trying to go to IDLE");
151 while (m_stop ==
false) {
157 "Event recorder was not in IDLE when trying to go to WAITING");
160 std::this_thread::sleep_for(1ms);
164 m_subscriber->Subscribe([&](
const JsonPayload& ev) {
167 if (m_input_filter(ev)) {
168 output << ev <<
"\n";
180 "Event recorder was not in WAITING when trying to go to RUNNING");
183 std::this_thread::sleep_for(1ms);
189 "Event recorder was not running when trying to go to finish");
192 std::this_thread::sleep_for(1ms);
195 std::this_thread::sleep_for(1ms);
199 m_subscriber->Unsubscribe();
205 m_subscriber->Unsubscribe();
213 EventServiceIf& m_event_service;
214 std::unique_ptr<EventSubscriberIf> m_subscriber;
218 std::string m_topic_name;
220 perfc::CounterI64 m_samples_written;
221 perfc::ScopedRegistration m_samples_written_reg;
223 std::atomic<bool> m_start =
false;
224 std::atomic<bool> m_stop =
false;
225 std::thread m_process_thread;
230 inline static constexpr std::string_view RTR_PATH_TOPIC_NAME =
231 "/{}/static/rec_units/{}/topic_name";
236 inline static constexpr std::string_view OLDB_PATH_TOPIC_NAME =
"/{}/rec_units/{}/topic_name";
Defines auxiliary information associated with each counter registered with ComponentMetricsIf.
Definition componentMetricsIf.hpp:48
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:76
~EventRecordingUnit() override
Definition eventRecordingUnit.hpp:84
std::function< bool(const JsonPayload &)> FilterMethod
Definition eventRecordingUnit.hpp:45
EventRecordingUnit(const std::string &comp_id, const std::string &unit_id, ServiceContainer &services, FilterMethod input_filter=nullptr)
Create a new recording unit.
Definition eventRecordingUnit.hpp:54
std::vector< std::filesystem::path > Stop() override
Stop the recording and wait for it's termination.
Definition eventRecordingUnit.hpp:124
void Prepare(const std::filesystem::path &file_path) override
Prepare the recording.
Definition eventRecordingUnit.hpp:96
void Start() override
Start the recording.
Definition eventRecordingUnit.hpp:117
Interface class for providing pub/sub facilities for JSON events.
Definition eventServiceIf.hpp:28
Helper class for passing tags in Telegraf.
Definition influxTagMap.hpp:26
bool HasLeaders()
Check if this unit is following any leaders.
Definition recordingUnit.cpp:106
OldbIf & m_oldb
Definition recordingUnit.hpp:177
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:18
bool HasLastLeaderFinished()
This function is used to determine if this unit should stop recording when waiting for leaders.
Definition recordingUnit.cpp:115
bool HasFirstLeaderStarted()
This function is used to determine if this unit should start recording when waiting for leaders.
Definition recordingUnit.cpp:110
void SetFailed(const std::exception_ptr &exception)
Set the unit into failed state, with the given exception.
Definition recordingUnit.cpp:151
bool IsEnabled() const
Checks whether the Recording Unit is enabled.
Definition recordingUnit.cpp:167
std::string m_unit_id
Definition recordingUnit.hpp:175
void ResetLeaderStates()
Definition recordingUnit.cpp:120
ComponentMetricsIf & m_metrics
Definition recordingUnit.hpp:178
const std::string & GetId() const
Get the unit_it of this RecordingUnit.
Definition recordingUnit.cpp:129
RuntimeRepoIf & m_rtr
Definition recordingUnit.hpp:176
void SetStopped()
Set the Unit state to STOPPED independent of the current State.
Definition recordingUnit.cpp:158
@ STOPPED
Definition recordingUnit.hpp:52
@ WAITING
Definition recordingUnit.hpp:52
@ FINISHED
Definition recordingUnit.hpp:52
@ RUNNING
Definition recordingUnit.hpp:52
@ IDLE
Definition recordingUnit.hpp:52
@ PREPARING
Definition recordingUnit.hpp:52
State GetState() const
Get the current state of the Recording Unit.
Definition recordingUnit.cpp:163
bool SetState(State state, State precondition)
Sets the new state, only goes to new state, if expected state matches.
Definition recordingUnit.cpp:133
std::optional< std::filesystem::path > m_file_path
Definition recordingUnit.hpp:179
Container class that holds services of any type.
Definition serviceContainer.hpp:38
Header file for ComponentMetricsIf.
Framework-provided event definitions.
Low-level interface of the event service.
Provides macros and utilities for exception handling.
FitsRecorder allows to write ColumnData to into fits files in a specified directory.
Definition commandReplier.cpp:21
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:24
FitsRecorder allows to write ColumnData to into fits files in a specified directory.
Abstract base class defining functionality common to all recording units.