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

#include <PosixDiscreteFlowReader.hpp>

Inheritance diagram for mxl::lib::PosixDiscreteFlowReader:
Collaboration diagram for mxl::lib::PosixDiscreteFlowReader:

Public Member Functions

virtual mxlFlowConfigInfo getFlowConfigInfo () const override
virtual FlowData const & getFlowData () const override
virtual mxlFlowInfo getFlowInfo () const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo () const override
virtual mxlStatus getGrain (std::uint64_t in_index, std::uint16_t in_minValidSlices, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) override
virtual mxlStatus getGrain (std::uint64_t in_index, std::uint16_t in_minValidSlices, Timepoint in_deadline, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) override
 PosixDiscreteFlowReader (FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< DiscreteFlowData > &&data)
virtual mxlStatus waitForGrain (std::uint64_t in_index, std::uint16_t in_minValidSlices, Timepoint in_deadline) const override
virtual ~PosixDiscreteFlowReader ()
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::DiscreteFlowReader
 FlowReader (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowReader (uuids::uuid const &flowId, std::filesystem::path const &domain)
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

mxlStatus getGrainImpl (std::uint64_t in_index, std::uint16_t in_minValidSlices, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) const
mxlStatus getGrainImpl (std::uint64_t in_index, std::uint16_t in_minValidSlices, Timepoint in_deadline, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) const
bool isFlowValidImpl () const

Private Attributes

int _accessFileFd
std::unique_ptr< DiscreteFlowData > _flowData

Detailed Description

Implementation of a flow reader based on POSIX shared memory.

Definition at line 21 of file PosixDiscreteFlowReader.hpp.

Constructor & Destructor Documentation

◆ PosixDiscreteFlowReader()

mxl::lib::PosixDiscreteFlowReader::PosixDiscreteFlowReader ( FlowManager const & manager,
uuids::uuid const & flowId,
std::unique_ptr< DiscreteFlowData > && data )
Parameters
[in]managerA referene to the flow manager used to obtain additional information about the flows context.

Definition at line 40 of file PosixDiscreteFlowReader.cpp.

41 : DiscreteFlowReader{flowId, manager.getDomain()}
42 , _flowData{std::move(data)}
43 , _accessFileFd{-1}
44 {
45 auto const accessFile = makeFlowAccessFilePath(manager.getDomain(), to_string(flowId));
46 _accessFileFd = ::open(accessFile.string().c_str(), O_RDWR);
47
48 // Opening the access file may fail if the domain is in a read only volume.
49 // we can still execute properly but the 'lastReadTime' will never be updated.
50 // Ignore failures.
51 }
std::unique_ptr< DiscreteFlowData > _flowData
std::filesystem::path makeFlowAccessFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:29
auto const flowId

◆ ~PosixDiscreteFlowReader()

mxl::lib::PosixDiscreteFlowReader::~PosixDiscreteFlowReader ( )
virtual

Close the access file fd.

Definition at line 53 of file PosixDiscreteFlowReader.cpp.

54 {
55 if (_accessFileFd != -1)
56 {
57 if (::close(_accessFileFd) != 0)
58 {
59 auto const error = errno;
60 MXL_ERROR("Failed to close access file fd: {}", ::strerror(error));
61 }
62 _accessFileFd = -1;
63 }
64 }
#define MXL_ERROR(...)
Definition Logging.hpp:67

Member Function Documentation

◆ getFlowConfigInfo()

mxlFlowConfigInfo mxl::lib::PosixDiscreteFlowReader::getFlowConfigInfo ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowConfigInfo

Implements mxl::lib::FlowReader.

Definition at line 80 of file PosixDiscreteFlowReader.cpp.

81 {
82 return getFlowData().flowInfo()->config;
83 }
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::PosixDiscreteFlowReader::getFlowData ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowData

Implements mxl::lib::FlowReader.

Definition at line 66 of file PosixDiscreteFlowReader.cpp.

67 {
68 if (_flowData)
69 {
70 return *_flowData;
71 }
72 throw std::runtime_error("No open flow.");
73 }

◆ getFlowInfo()

mxlFlowInfo mxl::lib::PosixDiscreteFlowReader::getFlowInfo ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowInfo

Implements mxl::lib::FlowReader.

Definition at line 75 of file PosixDiscreteFlowReader.cpp.

76 {
77 return *getFlowData().flowInfo();
78 }

◆ getFlowRuntimeInfo()

mxlFlowRuntimeInfo mxl::lib::PosixDiscreteFlowReader::getFlowRuntimeInfo ( ) const
nodiscardoverridevirtual
See also
FlowReader::getFlowRuntimeInfo

Implements mxl::lib::FlowReader.

Definition at line 85 of file PosixDiscreteFlowReader.cpp.

86 {
87 return getFlowData().flowInfo()->runtime;
88 }
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229

◆ getGrain() [1/2]

mxlStatus mxl::lib::PosixDiscreteFlowReader::getGrain ( std::uint64_t in_index,
std::uint16_t in_minValidSlices,
mxlGrainInfo * out_grainInfo,
std::uint8_t ** out_payload )
overridevirtual
See also
DiscreteFlowReader::getGrain

Implements mxl::lib::DiscreteFlowReader.

Definition at line 136 of file PosixDiscreteFlowReader.cpp.

138 {
139 auto result = MXL_ERR_UNKNOWN;
140 if (_flowData)
141 {
142 result = getGrainImpl(in_index, in_minValidSlices, out_grainInfo, out_payload);
143 if (result == MXL_STATUS_OK)
144 {
145 // We ignore the return value of updateFileAccessTime. It may fail if the domain is in a read-only volume.
146 (void)updateFileAccessTime(_accessFileFd);
147 }
148 else if (result == MXL_ERR_OUT_OF_RANGE_TOO_EARLY)
149 {
150 // If we were too early it could very well be that we're operating
151 // on a stale flow, so we use the opportunity to check whether it's
152 // valid.
153 if (!isFlowValidImpl())
154 {
155 result = MXL_ERR_FLOW_INVALID;
156 }
157 }
158 }
159 return result;
160 }
mxlStatus getGrainImpl(std::uint64_t in_index, std::uint16_t in_minValidSlices, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) const
@ MXL_ERR_FLOW_INVALID
Definition mxl.h:41
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_OUT_OF_RANGE_TOO_EARLY
Definition mxl.h:31
@ MXL_STATUS_OK
Definition mxl.h:23

◆ getGrain() [2/2]

mxlStatus mxl::lib::PosixDiscreteFlowReader::getGrain ( std::uint64_t in_index,
std::uint16_t in_minValidSlices,
Timepoint in_deadline,
mxlGrainInfo * out_grainInfo,
std::uint8_t ** out_payload )
overridevirtual
See also
DiscreteFlowReader::getGrain

Implements mxl::lib::DiscreteFlowReader.

Definition at line 110 of file PosixDiscreteFlowReader.cpp.

112 {
113 auto result = MXL_ERR_UNKNOWN;
114 if (_flowData)
115 {
116 result = getGrainImpl(in_index, in_minValidSlices, in_deadline, out_grainInfo, out_payload);
117 if (result == MXL_STATUS_OK)
118 {
119 // We ignore the return value of updateFileAccessTime. It may fail if the domain is in a read-only volume.
120 (void)updateFileAccessTime(_accessFileFd);
121 }
122 else if (result == MXL_ERR_OUT_OF_RANGE_TOO_EARLY)
123 {
124 // If we were ultimately too early, even with blocking for a certain amount
125 // of time it could very well be that we're operating on a stale flow, so we
126 // use the opportunity to check whether it's valid.
127 if (!isFlowValidImpl())
128 {
129 result = MXL_ERR_FLOW_INVALID;
130 }
131 }
132 }
133 return result;
134 }

◆ getGrainImpl() [1/2]

mxlStatus mxl::lib::PosixDiscreteFlowReader::getGrainImpl ( std::uint64_t in_index,
std::uint16_t in_minValidSlices,
mxlGrainInfo * out_grainInfo,
std::uint8_t ** out_payload ) const
private

Implementation of the various forms of getGrain() that can also be used by other methods that have previously asserted that we're operating on a valid flow (i.e. that _flowData is a valid pointer).

Definition at line 162 of file PosixDiscreteFlowReader.cpp.

164 {
165 auto result = MXL_ERR_UNKNOWN;
166 auto const flow = _flowData->flow();
167 if (auto const headIndex = flow->info.runtime.headIndex; in_index <= headIndex)
168 {
169 auto const grainCount = flow->info.config.discrete.grainCount;
170
171 // Reserve the tail position for the writer. The +2U advances
172 // minIndex one position past the tail. Since the tail and
173 // headIndex+1 alias the same physical ring-buffer position,
174 // this hides uncommitted headIndex+1 write state from
175 // readers.
176 auto const minIndex = (headIndex >= grainCount) ? (headIndex - grainCount + 2U) : std::uint64_t{0};
177
178 if (in_index >= minIndex)
179 {
180 auto const offset = in_index % grainCount;
181 auto const grain = _flowData->grainAt(offset);
182
183 // The header lives in shared memory and any process that can open the flow can write it. Check a copy so that the
184 // values tested are the values returned.
185 auto const info = grain->header.info;
186 if ((info.grainSize > _flowData->grainPayloadCapacityAt(offset)) || (info.validSlices > info.totalSlices))
187 {
188 MXL_ERROR("Grain {} has an inconsistent header: grainSize={}, totalSlices={}, validSlices={}",
189 in_index,
190 info.grainSize,
191 info.totalSlices,
192 info.validSlices);
194 }
195
196 if ((info.validSlices >= std::min(in_minValidSlices, info.totalSlices)) || ((info.flags & MXL_GRAIN_FLAG_INVALID) != 0))
197 {
198 if (out_grainInfo != nullptr)
199 {
200 *out_grainInfo = info;
201 }
202 if (out_payload != nullptr)
203 {
204 *out_payload = reinterpret_cast<std::uint8_t*>(&grain->header + 1);
205 }
206
207 result = MXL_STATUS_OK;
208 }
209 else
210 {
212 }
213 }
214 else
215 {
217 }
218 }
219 else
220 {
222 }
223
224 return result;
225 }
#define MXL_GRAIN_FLAG_INVALID
Definition flow.h:33
@ MXL_ERR_INVALID_STATE
Definition mxl.h:47
@ MXL_ERR_OUT_OF_RANGE_TOO_LATE
Definition mxl.h:30

◆ getGrainImpl() [2/2]

mxlStatus mxl::lib::PosixDiscreteFlowReader::getGrainImpl ( std::uint64_t in_index,
std::uint16_t in_minValidSlices,
Timepoint in_deadline,
mxlGrainInfo * out_grainInfo,
std::uint8_t ** out_payload ) const
private

Implementation of the blocking form of getGrain() and waitForGrain() that can also be used by other methods that have previously asserted that we're operating on a valid flow (i.e. that _flowData is a valid pointer).

Definition at line 227 of file PosixDiscreteFlowReader.cpp.

229 {
230 auto const flow = _flowData->flow();
231 auto const syncObject = std::atomic_ref{flow->state.syncCounter};
232 while (true)
233 {
234 // We remember the sync counter before checking the head index, otherwise we would introduce a race condition:
235 // 1. We check the header index, data won't be available yet.
236 // 2. Writer writes the data and updates the counter.
237 // 3. If we used the current value of the counter for the futex, we would delay everything by 1 grain.
238 auto const previousSyncCounter = syncObject.load(std::memory_order_acquire);
239 auto const result = getGrainImpl(in_index, in_minValidSlices, out_grainInfo, out_payload);
240 // NOTE: Before C++26 there is no way to access the address of the object wrapped
241 // by an atomic_ref. If there were it would be much more appropriate to pass
242 // syncObject by reference here and only unwrap the underlying integer in the
243 // implementation of waitUntilChanged.
244 if ((result != MXL_ERR_OUT_OF_RANGE_TOO_EARLY) || !waitUntilChanged(&flow->state.syncCounter, previousSyncCounter, in_deadline))
245 {
246 return result;
247 }
248 }
249 }
bool waitUntilChanged(T const *in_addr, T in_expected, Timepoint in_deadline)
Definition Sync.cpp:68

◆ isFlowValid()

bool mxl::lib::PosixDiscreteFlowReader::isFlowValid ( ) const
nodiscardoverrideprotectedvirtual
See also
FlowReader::isFlowValid

Implements mxl::lib::FlowReader.

Definition at line 251 of file PosixDiscreteFlowReader.cpp.

252 {
253 return _flowData && isFlowValidImpl();
254 }

◆ isFlowValidImpl()

bool mxl::lib::PosixDiscreteFlowReader::isFlowValidImpl ( ) const
nodiscardprivate

Implementation of isFlowValid() that can be used by other methods that have previously asserted that we're operating on a valid flow (i.e. that _flowData is a valid pointer).

Definition at line 256 of file PosixDiscreteFlowReader.cpp.

257 {
258 auto const flowState = _flowData->flowState();
259 auto const flowDataPath = makeFlowDataFilePath(getDomain(), to_string(getId()));
260
261 struct stat st;
262 if (::stat(flowDataPath.string().c_str(), &st) != 0)
263 {
264 return false;
265 }
266 return (st.st_ino == flowState->inode);
267 }
uuids::uuid const & getId() const
std::filesystem::path const & getDomain() const
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:17

◆ waitForGrain()

mxlStatus mxl::lib::PosixDiscreteFlowReader::waitForGrain ( std::uint64_t in_index,
std::uint16_t in_minValidSlices,
Timepoint in_deadline ) const
overridevirtual
See also
DiscreteFlowReader::waitForGrain

Implements mxl::lib::DiscreteFlowReader.

Definition at line 90 of file PosixDiscreteFlowReader.cpp.

91 {
92 auto result = MXL_ERR_UNKNOWN;
93 if (_flowData)
94 {
95 result = getGrainImpl(in_index, in_minValidSlices, in_deadline, nullptr, nullptr);
97 {
98 // If we were ultimately too early, even with blocking for a certain amount
99 // of time it could very well be that we're operating on a stale flow, so we
100 // use the opportunity to check whether it's valid.
101 if (!isFlowValidImpl())
102 {
103 result = MXL_ERR_FLOW_INVALID;
104 }
105 }
106 }
107 return result;
108 }

Member Data Documentation

◆ _accessFileFd

int mxl::lib::PosixDiscreteFlowReader::_accessFileFd
private

Definition at line 96 of file PosixDiscreteFlowReader.hpp.

◆ _flowData

std::unique_ptr<DiscreteFlowData> mxl::lib::PosixDiscreteFlowReader::_flowData
private

Definition at line 95 of file PosixDiscreteFlowReader.hpp.


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