12#ifndef RTCTK_COMPONENTFRAMEWORK_EVENTRECORDINGUNIT_HPP
13#define RTCTK_COMPONENTFRAMEWORK_EVENTRECORDINGUNIT_HPP
23#include <fmt/format.h>
24#include <numapp/numapolicies.hpp>
25#include <numapp/thread.hpp>
26#include <taiclock/taiClock.hpp>
43template <
class OutputStage = std::ofstream>
56 const std::string& unit_id,
62 m_input_filter = std::move(input_filter);
64 m_input_filter = [](
const JsonPayload&) {
return true; };
67 auto rtr_topic_path =
DataPointPath(fmt::format(RTR_PATH_TOPIC_NAME, comp_id, unit_id));
68 m_topic_name =
m_rtr.GetDataPoint<std::string>(rtr_topic_path);
70 auto oldb_topic_path =
DataPointPath(fmt::format(OLDB_PATH_TOPIC_NAME, comp_id, unit_id));
71 m_oldb.CreateDataPoint<std::string>(oldb_topic_path);
72 m_oldb.SetDataPoint<std::string>(oldb_topic_path, m_topic_name);
76 {
"topic_name", m_topic_name},
79 m_samples_written_reg =
m_metrics.AddCounter(
81 unit_id +
"/samples_written",
91 if (m_process_thread.joinable()) {
92 m_process_thread.join();
101 void Prepare(
const std::filesystem::path& file_path)
override {
108 "EventRecorder tried to go from non-STOPPED State to PREPARING");
110 m_subscriber.reset();
111 m_subscriber = m_event_service.MakeSubscriber(m_topic_name);
115 auto policies = numapp::NumaPolicies();
117 numapp::MakeThread(
m_unit_id.substr(0, 15), policies, [&]() { return Process(); });
129 std::vector<std::filesystem::path>
Stop()
override {
131 if (m_process_thread.joinable()) {
132 m_process_thread.join();
135 std::vector<std::filesystem::path> files;
144 using namespace std::chrono_literals;
150 m_samples_written.Store(0);
154 "Event recorder was not in PREPARING when trying to go to IDLE");
156 while (m_stop ==
false) {
162 "Event recorder was not in IDLE when trying to go to WAITING");
165 std::this_thread::sleep_for(1ms);
170 m_subscriber->Subscribe([&](
const JsonPayload& ev) {
173 if (m_input_filter(ev)) {
174 output << ev <<
"\n";
186 "Event recorder was not in WAITING when trying to go to RUNNING");
189 std::this_thread::sleep_for(1ms);
195 "Event recorder was not running when trying to go to finish");
198 std::this_thread::sleep_for(1ms);
201 std::this_thread::sleep_for(1ms);
205 m_subscriber->Unsubscribe();
211 m_subscriber->Unsubscribe();
219 EventServiceIf& m_event_service;
220 std::unique_ptr<EventSubscriberIf> m_subscriber;
224 std::string m_topic_name;
226 perfc::CounterI64 m_samples_written;
227 perfc::ScopedRegistration m_samples_written_reg;
229 std::atomic<bool> m_start =
false;
230 std::atomic<bool> m_stop =
false;
231 std::thread m_process_thread;
236 inline static constexpr std::string_view RTR_PATH_TOPIC_NAME =
237 "/{}/static/rec_units/{}/topic_name";
242 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:49
This class provides a wrapper for a data point path.
Definition dataPointPath.hpp:77
~EventRecordingUnit() override
Definition eventRecordingUnit.hpp:89
std::function< bool(const JsonPayload &)> FilterMethod
Definition eventRecordingUnit.hpp:46
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:55
std::vector< std::filesystem::path > Stop() override
Stop the recording and wait for it's termination.
Definition eventRecordingUnit.hpp:129
void Prepare(const std::filesystem::path &file_path) override
Prepare the recording.
Definition eventRecordingUnit.hpp:101
void Start() override
Start the recording.
Definition eventRecordingUnit.hpp:122
Interface class for providing pub/sub facilities for JSON events.
Definition eventServiceIf.hpp:29
Helper class for passing tags in Telegraf.
Definition influxTagMap.hpp:27
bool HasLeaders()
Check if this unit is following any leaders.
Definition recordingUnit.cpp:105
OldbIf & m_oldb
Definition recordingUnit.hpp:178
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
bool HasLastLeaderFinished()
This function is used to determine if this unit should stop recording when waiting for leaders.
Definition recordingUnit.cpp:114
bool HasFirstLeaderStarted()
This function is used to determine if this unit should start recording when waiting for leaders.
Definition recordingUnit.cpp:109
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 ResetLeaderStates()
Definition recordingUnit.cpp:119
ComponentMetricsIf & m_metrics
Definition recordingUnit.hpp:179
RuntimeRepoIf & m_rtr
Definition recordingUnit.hpp:177
void SetStopped()
Set the Unit state to STOPPED independent of the current State.
Definition recordingUnit.cpp:157
@ STOPPED
Definition recordingUnit.hpp:53
@ WAITING
Definition recordingUnit.hpp:53
@ FINISHED
Definition recordingUnit.hpp:53
@ RUNNING
Definition recordingUnit.hpp:53
@ IDLE
Definition recordingUnit.hpp:53
@ PREPARING
Definition recordingUnit.hpp:53
State GetState()
Get the current state of the Recording Unit.
Definition recordingUnit.cpp:162
bool SetState(State state, State precondition)
Sets the new state, only goes to new state, if expected state matches.
Definition recordingUnit.cpp:132
std::optional< std::filesystem::path > m_file_path
Definition recordingUnit.hpp:180
bool IsEnabled()
Checks whether the Recording Unit is enabled.
Definition recordingUnit.cpp:166
Container class that holds services of any type.
Definition serviceContainer.hpp:39
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:22
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:25
FitsRecorder allows to write ColumnData to into fits files in a specified directory.
Abstract base class defining functionality common to all recording units.