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

#include <PosixDiscreteFlowWriter.hpp>

Inheritance diagram for mxl::lib::PosixDiscreteFlowWriter:
Collaboration diagram for mxl::lib::PosixDiscreteFlowWriter:

Public Member Functions

virtual mxlStatus cancel () override
virtual mxlStatus commit (mxlGrainInfo const &mxlGrainInfo) override
virtual mxlFlowConfigInfo getFlowConfigInfo () const override
virtual FlowData const & getFlowData () const override
virtual FlowData & getFlowData () override
virtual mxlFlowInfo getFlowInfo () const override
virtual mxlFlowRuntimeInfo getFlowRuntimeInfo () const override
virtual mxlGrainInfo getGrainInfo (std::uint64_t in_index) const override
virtual bool isExclusive () const override
virtual bool makeExclusive () override
virtual mxlStatus openGrain (std::uint64_t in_index, mxlGrainInfo *out_grainInfo, std::uint8_t **out_payload) override
 PosixDiscreteFlowWriter (FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< DiscreteFlowData > &&data, DomainWatcher::ptr const &watcher)
 ~PosixDiscreteFlowWriter () override
Public Member Functions inherited from mxl::lib::FlowWriter
uuids::uuid const & getId () const
virtual ~FlowWriter ()

Private Attributes

std::uint64_t _currentIndex
std::unique_ptr< DiscreteFlowData > _flowData
std::uint64_t _lastCommittedIndex
DomainWatcher::ptr _watcher

Additional Inherited Members

Protected Member Functions inherited from mxl::lib::DiscreteFlowWriter
 FlowWriter (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowWriter (uuids::uuid const &flowId, std::filesystem::path const &domain)
Protected Member Functions inherited from mxl::lib::FlowWriter
bool checkPermissions () const
 FlowWriter (uuids::uuid &&flowId, std::filesystem::path const &domain)
 FlowWriter (uuids::uuid const &flowId, std::filesystem::path const &domain)

Detailed Description

Implementation of a FlowWriter based on POSIX shared memory mapping.

Definition at line 21 of file PosixDiscreteFlowWriter.hpp.

Constructor & Destructor Documentation

◆ PosixDiscreteFlowWriter()

mxl::lib::PosixDiscreteFlowWriter::PosixDiscreteFlowWriter ( FlowManager const & manager,
uuids::uuid const & flowId,
std::unique_ptr< DiscreteFlowData > && data,
DomainWatcher::ptr const & watcher )

Creates a PosixDiscreteFlowWriter

Parameters
[in]managerA referene to the flow manager used to obtain additional information about the flows context.

Definition at line 21 of file PosixDiscreteFlowWriter.cpp.

23 : DiscreteFlowWriter{flowId, manager.getDomain()}
24 , _flowData{std::move(data)}
27 , _watcher(watcher)
28 {
29 _watcher->addFlow(this, flowId);
30 }
std::unique_ptr< DiscreteFlowData > _flowData
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
auto const flowId

◆ ~PosixDiscreteFlowWriter()

mxl::lib::PosixDiscreteFlowWriter::~PosixDiscreteFlowWriter ( )
override

Definition at line 32 of file PosixDiscreteFlowWriter.cpp.

33 {
34 try
35 {
36 _watcher->removeFlow(this, _flowData->flowInfo()->config.common.id);
37 }
38 catch (...)
39 {
40 MXL_ERROR("Bug: exception while removing flow writer from watcher in destructor");
41 }
42 }
#define MXL_ERROR(...)
Definition Logging.hpp:67

Member Function Documentation

◆ cancel()

mxlStatus mxl::lib::PosixDiscreteFlowWriter::cancel ( )
overridevirtual
See also
DiscreteFlowWriter::cancel

Implements mxl::lib::DiscreteFlowWriter.

Definition at line 135 of file PosixDiscreteFlowWriter.cpp.

136 {
138 return MXL_STATUS_OK;
139 }
@ MXL_STATUS_OK
Definition mxl.h:23

◆ commit()

mxlStatus mxl::lib::PosixDiscreteFlowWriter::commit ( mxlGrainInfo const & mxlGrainInfo)
overridevirtual
See also
DiscreteFlowWriter::commit

Implements mxl::lib::DiscreteFlowWriter.

Definition at line 161 of file PosixDiscreteFlowWriter.cpp.

162 {
163 if (_flowData)
164 {
166 {
167 return MXL_ERR_INVALID_ARG;
168 }
169
170 auto const flow = _flowData->flow();
171 auto const offset = _currentIndex % flow->info.config.discrete.grainCount;
172
173 // Readers reject a header with these values, so refuse to publish one.
174 if ((mxlGrainInfo.grainSize > _flowData->grainPayloadCapacityAt(offset)) || (mxlGrainInfo.validSlices > mxlGrainInfo.totalSlices))
175 {
176 return MXL_ERR_INVALID_ARG;
177 }
178
179 flow->info.runtime.headIndex = _currentIndex;
180 *_flowData->grainInfoAt(offset) = mxlGrainInfo;
181 flow->info.runtime.lastWriteTime = currentTime(mxl::lib::Clock::TAI).value;
182
183 // If the grain is complete, reset the current index of the flow writer.
185 {
188 }
189
190 // Let readers know that the head has moved or that new data is available in a partial grain
191 flow->state.syncCounter++;
192 wakeAll(&flow->state.syncCounter);
193
194 return MXL_STATUS_OK;
195 }
196 return MXL_ERR_UNKNOWN;
197 }
struct mxlGrainInfo_t mxlGrainInfo
@ MXL_ERR_UNKNOWN
Definition mxl.h:27
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
void wakeAll(T const *in_addr)
Definition Sync.cpp:131
uint64_t index
Epoch Grain index used by that ring buffer entry.
Definition flow.h:138
uint32_t grainSize
Size in bytes of the complete payload of a grain.
Definition flow.h:143
uint16_t totalSlices
Definition flow.h:146
uint16_t validSlices
Definition flow.h:149
auto const currentTime
Definition test_time.cpp:58

◆ getFlowConfigInfo()

mxlFlowConfigInfo mxl::lib::PosixDiscreteFlowWriter::getFlowConfigInfo ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowConfigInfo

Implements mxl::lib::FlowWriter.

Definition at line 67 of file PosixDiscreteFlowWriter.cpp.

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

◆ getFlowData() [1/2]

FlowData const & mxl::lib::PosixDiscreteFlowWriter::getFlowData ( ) const
nodiscardoverridevirtual

Accessor for the underlying flow data. The flow writer must first open the flow before invoking this method.

Implements mxl::lib::FlowWriter.

Definition at line 53 of file PosixDiscreteFlowWriter.cpp.

54 {
55 if (_flowData)
56 {
57 return *_flowData;
58 }
59 throw std::runtime_error("No open flow.");
60 }

◆ getFlowData() [2/2]

FlowData & mxl::lib::PosixDiscreteFlowWriter::getFlowData ( )
nodiscardoverridevirtual

Accessor for the underlying flow data. The flow writer must first open the flow before invoking this method.

Implements mxl::lib::FlowWriter.

Definition at line 44 of file PosixDiscreteFlowWriter.cpp.

45 {
46 if (_flowData)
47 {
48 return *_flowData;
49 }
50 throw std::runtime_error("No open flow.");
51 }

◆ getFlowInfo()

mxlFlowInfo mxl::lib::PosixDiscreteFlowWriter::getFlowInfo ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowInfo

Implements mxl::lib::FlowWriter.

Definition at line 62 of file PosixDiscreteFlowWriter.cpp.

63 {
64 return *getFlowData().flowInfo();
65 }

◆ getFlowRuntimeInfo()

mxlFlowRuntimeInfo mxl::lib::PosixDiscreteFlowWriter::getFlowRuntimeInfo ( ) const
nodiscardoverridevirtual
See also
FlowWriter::getFlowRuntimeInfo

Implements mxl::lib::FlowWriter.

Definition at line 72 of file PosixDiscreteFlowWriter.cpp.

73 {
74 return getFlowData().flowInfo()->runtime;
75 }
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229

◆ getGrainInfo()

mxlGrainInfo mxl::lib::PosixDiscreteFlowWriter::getGrainInfo ( std::uint64_t in_index) const
nodiscardoverridevirtual
See also
DiscreteFlowWriter::getGrainInfo

Implements mxl::lib::DiscreteFlowWriter.

Definition at line 77 of file PosixDiscreteFlowWriter.cpp.

78 {
79 auto& flowData = static_cast<DiscreteFlowData const&>(getFlowData());
80 auto const offset = in_index % flowData.flowInfo()->config.discrete.grainCount;
81 return flowData.grainAt(offset)->header.info;
82 }

◆ isExclusive()

bool mxl::lib::PosixDiscreteFlowWriter::isExclusive ( ) const
overridevirtual

Check if this is the only writer for this flow.

Implements mxl::lib::FlowWriter.

Definition at line 141 of file PosixDiscreteFlowWriter.cpp.

142 {
143 if (!_flowData)
144 {
145 return false;
146 }
147
148 return _flowData->isExclusive();
149 }

◆ makeExclusive()

bool mxl::lib::PosixDiscreteFlowWriter::makeExclusive ( )
overridevirtual

Try to turn this into a exclusive writer.

Returns
true if the operation was successful and this is the only writer, false if the operation was not successful because there are other active writers for this flow.

Implements mxl::lib::FlowWriter.

Definition at line 151 of file PosixDiscreteFlowWriter.cpp.

152 {
153 if (!_flowData)
154 {
155 return false;
156 }
157
158 return _flowData->makeExclusive();
159 }

◆ openGrain()

mxlStatus mxl::lib::PosixDiscreteFlowWriter::openGrain ( std::uint64_t in_index,
mxlGrainInfo * out_grainInfo,
std::uint8_t ** out_payload )
overridevirtual
See also
DiscreteFlowWriter::openGrain

Implements mxl::lib::DiscreteFlowWriter.

Definition at line 84 of file PosixDiscreteFlowWriter.cpp.

85 {
86 if (!_flowData)
87 {
88 return MXL_ERR_UNKNOWN;
89 }
90
92 {
94 }
95
96 if ((_lastCommittedIndex != MXL_UNDEFINED_INDEX) && (in_index > (_lastCommittedIndex + 1U)))
97 {
98 // We have to invalidate any grain that would've been written to if the write was continuous. This is to ensure that the reader doesn't
99 // read stale data from a previous write. At most grainCount - 1 grains are invalidated, because grain in_index - grainCount shares its
100 // ring buffer entry with in_index. That entry can already hold the grain being opened: a fabrics initiator wrote it into a target
101 // flow, or an earlier commit of this incomplete grain did.
102 auto const grainCount = _flowData->flowInfo()->config.discrete.grainCount;
103 auto const skippedCount = in_index - _lastCommittedIndex - 1U;
104 auto const invalidatedCount = std::min<std::uint64_t>(skippedCount, grainCount - 1U);
105 auto const firstInvalidatedIndex = in_index - invalidatedCount;
106
107 for (auto missingIndex = firstInvalidatedIndex; missingIndex < in_index; ++missingIndex)
108 {
109 auto const missingOffset = missingIndex % grainCount;
110 auto const missingGrain = _flowData->grainAt(missingOffset);
111 missingGrain->header.info.index = missingIndex;
112 missingGrain->header.info.validSlices = 0;
113 missingGrain->header.info.flags |= MXL_GRAIN_FLAG_INVALID;
114 }
115 }
116
117 auto offset = in_index % _flowData->flowInfo()->config.discrete.grainCount;
118 auto const grain = _flowData->grainAt(offset);
119
120 // If the ring buffer entry already holds this grain index, keep its header. This is the case when an incomplete grain is reopened,
121 // and on a fabrics target, where the remote initiator writes the grain header and the target library sets validSlices before the
122 // application opens the grain to commit it. Otherwise the entry still holds an older grain and its state is reset.
123 if (grain->header.info.index != in_index)
124 {
125 grain->header.info.index = in_index;
126 grain->header.info.flags &= ~MXL_GRAIN_FLAG_INVALID;
127 grain->header.info.validSlices = 0;
128 }
129 *out_grainInfo = grain->header.info;
130 *out_payload = reinterpret_cast<std::uint8_t*>(&grain->header + 1);
131 _currentIndex = in_index;
132 return MXL_STATUS_OK;
133 }
#define MXL_GRAIN_FLAG_INVALID
Definition flow.h:33

Member Data Documentation

◆ _currentIndex

std::uint64_t mxl::lib::PosixDiscreteFlowWriter::_currentIndex
private

The currently opened grain index. MXL_UNDEFINED_INDEX if no grain is currently opened.

Definition at line 79 of file PosixDiscreteFlowWriter.hpp.

◆ _flowData

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

The FlowData for the currently opened flow. null if no flow is opened.

Definition at line 77 of file PosixDiscreteFlowWriter.hpp.

◆ _lastCommittedIndex

std::uint64_t mxl::lib::PosixDiscreteFlowWriter::_lastCommittedIndex
private

The last committed grain index. MXL_UNDEFINED_INDEX if no grain has been committed yet.

Definition at line 81 of file PosixDiscreteFlowWriter.hpp.

◆ _watcher

DomainWatcher::ptr mxl::lib::PosixDiscreteFlowWriter::_watcher
private

Definition at line 86 of file PosixDiscreteFlowWriter.hpp.


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