MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::PosixEventFlowReader Class Referencefinal

POSIX event consumer with a single-thread cursor and an owned payload snapshot. More...

#include <PosixEventFlowReader.hpp>

Inheritance diagram for mxl::lib::PosixEventFlowReader:
Collaboration diagram for mxl::lib::PosixEventFlowReader:

Public Member Functions

virtual mxlStatus getEvent (Timepoint deadline, mxlEventInfo *info, std::uint8_t **payload) override
 Copy the next available entry and advance this reader's cursor.
virtual mxlFlowConfigInfo getFlowConfigInfo () const override
virtual FlowData const & getFlowData () const override
virtual mxlFlowInfo getFlowInfo () const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo () const override
 PosixEventFlowReader (FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< EventFlowData > &&data)
 Attach a reader at the oldest retained entry.
virtual ~PosixEventFlowReader () override
 Close the access descriptor and release the snapshot and shared mappings.
Public Member Functions inherited from mxl::lib::FlowReader
std::filesystem::path const & getDomain () const
uuids::uuid const & getId () const
virtual ~FlowReader ()

Protected Member Functions

virtual bool isFlowValid () const override
Protected Member Functions inherited from mxl::lib::EventFlowReader
 FlowReader (uuids::uuid &&flowId, std::filesystem::path const &domain)
 Initialize flow identity and domain using the base reader constructors.
 FlowReader (uuids::uuid const &flowId, std::filesystem::path const &domain)
 Initialize flow identity and domain using the base reader constructors.
Protected Member Functions inherited from mxl::lib::FlowReader
bool checkPermissions () const
 FlowReader (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowReader (uuids::uuid const &flowId, std::filesystem::path const &domain)

Private Member Functions

bool isFlowValidImpl () const

Private Attributes

int _accessFileFd
 Access notification descriptor, or -1 if unavailable.
std::unique_ptr< EventFlowData > _flowData
 Owned common metadata and read-only event mappings.
std::uint64_t _nextIndex
 Next queue entry for this reader's owning thread.
std::vector< std::uint8_t > _snapshot
 Payload storage reused on the next read attempt.

Detailed Description

POSIX event consumer with a single-thread cursor and an owned payload snapshot.

Definition at line 18 of file PosixEventFlowReader.hpp.

Constructor & Destructor Documentation

◆ PosixEventFlowReader()

mxl::lib::PosixEventFlowReader::PosixEventFlowReader ( FlowManager const & manager,
uuids::uuid const & flowId,
std::unique_ptr< EventFlowData > && data )

Attach a reader at the oldest retained entry.

Parameters
managerManager supplying the flow domain.
flowIdIdentifier of the mapped flow.
dataMappings whose ownership transfers to this reader.

Definition at line 46 of file PosixEventFlowReader.cpp.

47 : EventFlowReader{flowId, manager.getDomain()}
48 , _flowData{std::move(data)}
49 , _accessFileFd{-1}
50 , _nextIndex{0}
51 , _snapshot(_flowData->flowInfo()->config.event.eventPayloadSize)
52 {
53 _nextIndex = _flowData->oldestIndex();
54 auto const accessFile = makeFlowAccessFilePath(manager.getDomain(), to_string(flowId));
55 _accessFileFd = ::open(accessFile.string().c_str(), O_RDWR | O_CLOEXEC);
56
57 // Opening the access file may fail if the domain is in a read only volume.
58 // we can still execute properly but the 'lastReadTime' will never be updated.
59 // Ignore failures.
60 }
std::vector< std::uint8_t > _snapshot
Payload storage reused on the next read attempt.
int _accessFileFd
Access notification descriptor, or -1 if unavailable.
std::unique_ptr< EventFlowData > _flowData
Owned common metadata and read-only event mappings.
std::uint64_t _nextIndex
Next queue entry for this reader's owning thread.
std::filesystem::path makeFlowAccessFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:29
auto const flowId

◆ ~PosixEventFlowReader()

mxl::lib::PosixEventFlowReader::~PosixEventFlowReader ( )
overridevirtual

Close the access descriptor and release the snapshot and shared mappings.

Definition at line 62 of file PosixEventFlowReader.cpp.

63 {
64 if (_accessFileFd != -1)
65 {
66 if (::close(_accessFileFd) != 0)
67 {
68 auto const error = errno;
69 MXL_ERROR("Failed to close access file fd: {}", std::strerror(error));
70 }
71 _accessFileFd = -1;
72 }
73 }
#define MXL_ERROR(...)
Definition Logging.hpp:67

Member Function Documentation

◆ getEvent()

mxlStatus mxl::lib::PosixEventFlowReader::getEvent ( Timepoint deadline,
mxlEventInfo * info,
std::uint8_t ** payload )
overridevirtual

Copy the next available entry and advance this reader's cursor.

Parameters
deadlineAbsolute realtime deadline for waiting; an expired deadline permits one attempt.
[out]infoMetadata destination, valid on success; must not be null.
[out]payloadReceives private snapshot storage on success; must not be null.
Returns
MXL_STATUS_OK, TOO_EARLY if unavailable by the deadline, TOO_LATE on overwrite, or FLOW_INVALID for corrupt metadata or a detected removed flow.
Note
Snapshot storage is reused by the next read attempt on this handle. Release must be coordinated with callers.

Implements mxl::lib::EventFlowReader.

Definition at line 99 of file PosixEventFlowReader.cpp.

100 {
101 auto flow = _flowData->flow();
102 auto sync = std::atomic_ref{flow->state.syncCounter};
103 while (true)
104 {
105 auto const previous = sync.load(std::memory_order_acquire);
106 auto const oldest = _flowData->oldestIndex();
107 if (_nextIndex < oldest)
108 {
109 _nextIndex = oldest;
111 }
112 auto received = mxlEventInfo{};
113 auto const result = _flowData->ring().read(_nextIndex, received, _snapshot.data());
115 {
116 ++_nextIndex;
117 *info = received;
118 *payload = _snapshot.data();
119 (void)updateFileAccessTime(_accessFileFd);
120 return MXL_STATUS_OK;
121 }
123 {
124 _nextIndex = std::max(_nextIndex + 1, _flowData->oldestIndex());
126 }
128 {
130 }
131 if (!isFlowValidImpl())
132 {
134 }
135 if ((currentTime(Clock::Realtime) >= deadline) || !waitUntilChanged(&flow->state.syncCounter, previous, deadline))
136 {
138 }
139 }
140 }
@ Overwritten
The slot advanced past this index or changed while being copied.
@ Invalid
A stable snapshot contains unsupported or invalid metadata.
@ Ready
Metadata and payload form a validated snapshot of the requested entry.
struct mxlEventInfo_t mxlEventInfo
Metadata for one event queue entry, either a complete event or a fragment.
@ MXL_ERR_FLOW_INVALID
Definition mxl.h:41
@ MXL_ERR_OUT_OF_RANGE_TOO_EARLY
Definition mxl.h:31
@ MXL_STATUS_OK
Definition mxl.h:23
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
Definition mxl.h:30
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
Definition Sync.cpp:68
auto const currentTime
Definition test_time.cpp:58

◆ getFlowConfigInfo()

mxlFlowConfigInfo mxl::lib::PosixEventFlowReader::getFlowConfigInfo ( ) const
overridevirtual
Returns
The immutable configuration of the mapped event flow.

Implements mxl::lib::FlowReader.

Definition at line 89 of file PosixEventFlowReader.cpp.

90 {
91 return getFlowData().flowInfo()->config;
92 }
constexpr mxlFlowInfo * flowInfo() noexcept
Definition FlowData.hpp:92
virtual FlowData const & getFlowData() const override
mxlFlowConfigInfo config
Definition flowinfo.h:227

◆ getFlowData()

FlowData const & mxl::lib::PosixEventFlowReader::getFlowData ( ) const
overridevirtual
Returns
The owned common and event mappings.

Implements mxl::lib::FlowReader.

Definition at line 75 of file PosixEventFlowReader.cpp.

76 {
77 if (_flowData)
78 {
79 return *_flowData;
80 }
81 throw std::runtime_error{"No open flow."};
82 }

◆ getFlowInfo()

mxlFlowInfo mxl::lib::PosixEventFlowReader::getFlowInfo ( ) const
overridevirtual
Returns
A copy of the flow configuration and runtime fields.

Implements mxl::lib::FlowReader.

Definition at line 84 of file PosixEventFlowReader.cpp.

85 {
86 return *getFlowData().flowInfo();
87 }

◆ getFlowRuntimeInfo()

mxlFlowRuntimeInfo mxl::lib::PosixEventFlowReader::getFlowRuntimeInfo ( ) const
overridevirtual
Returns
A copy of the runtime fields.

Implements mxl::lib::FlowReader.

Definition at line 94 of file PosixEventFlowReader.cpp.

95 {
96 return getFlowData().flowInfo()->runtime;
97 }
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229

◆ isFlowValid()

bool mxl::lib::PosixEventFlowReader::isFlowValid ( ) const
overrideprotectedvirtual
Returns
True if the mapping exists and the flow's data file retains its recorded inode.

Implements mxl::lib::FlowReader.

Definition at line 142 of file PosixEventFlowReader.cpp.

143 {
144 return _flowData && isFlowValidImpl();
145 }

◆ isFlowValidImpl()

bool mxl::lib::PosixEventFlowReader::isFlowValidImpl ( ) const
private
Returns
True if the current data file's inode matches the mapping; requires _flowData.

Definition at line 147 of file PosixEventFlowReader.cpp.

148 {
149 auto const flowState = _flowData->flowState();
150 auto const flowDataPath = makeFlowDataFilePath(getDomain(), to_string(getId()));
151
152 using FileStatus = struct stat;
153 auto st = FileStatus{};
154 if (::stat(flowDataPath.string().c_str(), &st) != 0)
155 {
156 return false;
157 }
158 return (st.st_ino == flowState->inode);
159 }
uuids::uuid const & getId() const
std::filesystem::path const & getDomain() const
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:17

Member Data Documentation

◆ _accessFileFd

int mxl::lib::PosixEventFlowReader::_accessFileFd
private

Access notification descriptor, or -1 if unavailable.

Definition at line 51 of file PosixEventFlowReader.hpp.

◆ _flowData

std::unique_ptr<EventFlowData> mxl::lib::PosixEventFlowReader::_flowData
private

Owned common metadata and read-only event mappings.

Definition at line 50 of file PosixEventFlowReader.hpp.

◆ _nextIndex

std::uint64_t mxl::lib::PosixEventFlowReader::_nextIndex
private

Next queue entry for this reader's owning thread.

Definition at line 52 of file PosixEventFlowReader.hpp.

◆ _snapshot

std::vector<std::uint8_t> mxl::lib::PosixEventFlowReader::_snapshot
private

Payload storage reused on the next read attempt.

Definition at line 53 of file PosixEventFlowReader.hpp.


The documentation for this class was generated from the following files: