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

POSIX single-producer writer with private staging and monotonic timestamps. The caller guarantees one writer per flow and serializes transactions; readers may run concurrently. More...

#include <PosixEventFlowWriter.hpp>

Inheritance diagram for mxl::lib::PosixEventFlowWriter:
Collaboration diagram for mxl::lib::PosixEventFlowWriter:

Public Member Functions

virtual mxlStatus cancel () override
 Discard an open write without advancing the queue or timestamp.
virtual mxlStatus commit (mxlEventInfo const &info) override
 Publish the staged bytes as one complete event or fragment.
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 bool isExclusive () const override
virtual bool makeExclusive () override
virtual mxlStatus openEvent (mxlEventInfo *info, std::uint8_t **payload) override
 Begin a write and reset metadata to the event defaults.
 PosixEventFlowWriter (FlowManager const &manager, uuids::uuid const &flowId, std::unique_ptr< EventFlowData > &&data, std::shared_ptr< DomainWatcher > const &watcher)
 Attach the sole writer, recover an interrupted publication and restore timestamp ordering.
virtual ~PosixEventFlowWriter () override
 Unregister from the watcher and release mappings and the common flow lock.
Public Member Functions inherited from mxl::lib::FlowWriter
uuids::uuid const & getId () const
virtual ~FlowWriter ()

Private Attributes

std::unique_ptr< EventFlowData > _flowData
 Owns common metadata, ring mappings and the common flow lock.
std::uint64_t _lastTimestamp
 Last committed timestamp, restored from the ring on attachment.
bool _open
 True while one caller-owned write transaction is open.
std::vector< std::uint8_t > _payload
 Private staging buffer; publication copies only eventSize bytes.
std::shared_ptr< DomainWatcher > _watcher
 Watcher kept alive while the writer is registered.

Additional Inherited Members

Protected Member Functions inherited from mxl::lib::EventFlowWriter
 FlowWriter (uuids::uuid &&flowId, std::filesystem::path const &domain)
 Initialize flow identity and domain using the base writer constructors.
 FlowWriter (uuids::uuid const &flowId, std::filesystem::path const &domain)
 Initialize flow identity and domain using the base writer constructors.
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

POSIX single-producer writer with private staging and monotonic timestamps. The caller guarantees one writer per flow and serializes transactions; readers may run concurrently.

Definition at line 21 of file PosixEventFlowWriter.hpp.

Constructor & Destructor Documentation

◆ PosixEventFlowWriter()

mxl::lib::PosixEventFlowWriter::PosixEventFlowWriter ( FlowManager const & manager,
uuids::uuid const & flowId,
std::unique_ptr< EventFlowData > && data,
std::shared_ptr< DomainWatcher > const & watcher )

Attach the sole writer, recover an interrupted publication and restore timestamp ordering.

Parameters
managerManager supplying the flow domain.
flowIdIdentifier of the mapped flow.
dataEvent mappings whose ownership transfers to this writer.
watcherShared watcher used for reader access notifications.
Precondition
The caller guarantees that no other writer owns this flow.
Exceptions
std::runtime_errorThe retained head or next published entry is invalid.
std::exceptionAllocation or watcher registration fails.

Definition at line 22 of file PosixEventFlowWriter.cpp.

24 : EventFlowWriter{flowId, manager.getDomain()}
25 , _flowData{std::move(data)}
26 , _open{false}
28 , _payload(_flowData->flowInfo()->config.event.eventPayloadSize)
29 , _watcher{watcher}
30 {
31 // The caller guarantees sole writer ownership, keeping the committed head
32 // stable while we restore the timestamp from an existing flow.
33 auto flow = _flowData->flow();
34 auto& runtime = flow->info.runtime;
35 auto head = EventRingBuffer::load(runtime.headIndex);
36 if (head != MXL_UNDEFINED_INDEX)
37 {
38 auto info = mxlEventInfo{};
39 if (_flowData->ring().read(head, info, _payload.data()) != EventRingBuffer::ReadResult::Ready)
40 {
41 throw std::runtime_error{"Invalid last committed event."};
42 }
43 _lastTimestamp = info.timestamp;
44 }
45 // A crashed writer may have published the next slot without advancing the head.
46 auto const next = (head == MXL_UNDEFINED_INDEX) ? 0 : head + 1;
47 auto info = mxlEventInfo{};
48 auto const result = _flowData->ring().read(next, info, _payload.data());
50 {
51 if (info.timestamp < _lastTimestamp)
52 {
53 throw std::runtime_error{"Invalid recovered event timestamp."};
54 }
55 _lastTimestamp = info.timestamp;
56 std::atomic_ref{runtime.lastWriteTime}.store(currentTime(Clock::TAI).value, std::memory_order_release);
57 std::atomic_ref{runtime.headIndex}.store(next, std::memory_order_release);
58 head = next;
59 }
60 else if (result != EventRingBuffer::ReadResult::Pending)
61 {
62 throw std::runtime_error{"Invalid next event during writer recovery."};
63 }
64 _watcher->addFlow(this, flowId);
65 if (head != MXL_UNDEFINED_INDEX)
66 {
67 // Also cover a crash after head advancement but before reader notification.
68 std::atomic_ref{flow->state.syncCounter}.fetch_add(1, std::memory_order_release);
69 wakeAll(&flow->state.syncCounter);
70 }
71 }
@ Pending
The requested entry is not yet published or its index is unrepresentable.
@ Ready
Metadata and payload form a validated snapshot of the requested entry.
static std::uint64_t load(std::uint64_t const &value) noexcept
Load a shared 64-bit word with acquire ordering, including from a read-only mapping.
std::vector< std::uint8_t > _payload
Private staging buffer; publication copies only eventSize bytes.
std::uint64_t _lastTimestamp
Last committed timestamp, restored from the ring on attachment.
std::shared_ptr< DomainWatcher > _watcher
Watcher kept alive while the writer is registered.
std::unique_ptr< EventFlowData > _flowData
Owns common metadata, ring mappings and the common flow lock.
bool _open
True while one caller-owned write transaction is open.
struct mxlEventInfo_t mxlEventInfo
Metadata for one event queue entry, either a complete event or a fragment.
void wakeAll(T const *in_addr)
Definition Sync.cpp:131
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
auto const flowId
auto const currentTime
Definition test_time.cpp:58

◆ ~PosixEventFlowWriter()

mxl::lib::PosixEventFlowWriter::~PosixEventFlowWriter ( )
overridevirtual

Unregister from the watcher and release mappings and the common flow lock.

Definition at line 73 of file PosixEventFlowWriter.cpp.

74 {
75 try
76 {
77 _watcher->removeFlow(this, _flowData->flowInfo()->config.common.id);
78 }
79 catch (...)
80 {
81 MXL_ERROR("Bug: exception while removing flow writer from watcher in destructor");
82 }
83 }
#define MXL_ERROR(...)
Definition Logging.hpp:67

Member Function Documentation

◆ cancel()

mxlStatus mxl::lib::PosixEventFlowWriter::cancel ( )
overridevirtual

Discard an open write without advancing the queue or timestamp.

Returns
MXL_STATUS_OK, or MXL_ERR_INVALID_ARG when no write is open.

Implements mxl::lib::EventFlowWriter.

Definition at line 142 of file PosixEventFlowWriter.cpp.

143 {
144 if (!_open)
145 {
146 return MXL_ERR_INVALID_ARG;
147 }
148 _open = false;
149 return MXL_STATUS_OK;
150 }
@ MXL_ERR_INVALID_ARG
Definition mxl.h:35
@ MXL_STATUS_OK
Definition mxl.h:23

◆ commit()

mxlStatus mxl::lib::PosixEventFlowWriter::commit ( mxlEventInfo const & info)
overridevirtual

Publish the staged bytes as one complete event or fragment.

Parameters
infoValid metadata whose timestamp is at least the last committed timestamp.
Returns
MXL_STATUS_OK on publication, INVALID_ARG on invalid metadata or state, FLOW_INVALID on interrupted-tag reuse, or TOO_LATE on index exhaustion.
Note
Failure leaves the transaction open for correction or cancellation.

Implements mxl::lib::EventFlowWriter.

Definition at line 172 of file PosixEventFlowWriter.cpp.

173 {
174 if (!_open || info.version != EVENT_HEADER_VERSION || info.size != sizeof info || info.flags != 0 || info.timestamp < _lastTimestamp ||
175 info.eventSize > _flowData->flowInfo()->config.event.eventPayloadSize)
176 {
177 return MXL_ERR_INVALID_ARG;
178 }
179 auto flow = _flowData->flow();
180 auto& runtime = flow->info.runtime;
181 auto const head = EventRingBuffer::load(runtime.headIndex);
182 auto const index = head == MXL_UNDEFINED_INDEX ? 0 : head + 1;
183 auto const status = _flowData->ring().publish(index, info, _payload.data());
184 if (status != MXL_STATUS_OK)
185 {
186 return status;
187 }
188 std::atomic_ref{runtime.lastWriteTime}.store(currentTime(Clock::TAI).value, std::memory_order_release);
189 std::atomic_ref{runtime.headIndex}.store(index, std::memory_order_release);
190 _lastTimestamp = info.timestamp;
191 _open = false;
192 std::atomic_ref{flow->state.syncCounter}.fetch_add(1, std::memory_order_release);
193 wakeAll(&flow->state.syncCounter);
194 return MXL_STATUS_OK;
195 }
constexpr auto EVENT_HEADER_VERSION
Version stored in the public mxlEventInfo metadata of every published entry.
Definition Flow.hpp:61
uint64_t index

◆ getFlowConfigInfo()

mxlFlowConfigInfo mxl::lib::PosixEventFlowWriter::getFlowConfigInfo ( ) const
overridevirtual
Returns
Immutable event flow configuration.

Implements mxl::lib::FlowWriter.

Definition at line 108 of file PosixEventFlowWriter.cpp.

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

◆ getFlowData() [1/2]

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

Implements mxl::lib::FlowWriter.

Definition at line 94 of file PosixEventFlowWriter.cpp.

95 {
96 if (_flowData)
97 {
98 return *_flowData;
99 }
100 throw std::runtime_error{"No open flow."};
101 }

◆ getFlowData() [2/2]

FlowData & mxl::lib::PosixEventFlowWriter::getFlowData ( )
overridevirtual
Returns
Mutable access to the owned common and event mappings.

Implements mxl::lib::FlowWriter.

Definition at line 85 of file PosixEventFlowWriter.cpp.

86 {
87 if (_flowData)
88 {
89 return *_flowData;
90 }
91 throw std::runtime_error{"No open flow."};
92 }

◆ getFlowInfo()

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

Implements mxl::lib::FlowWriter.

Definition at line 103 of file PosixEventFlowWriter.cpp.

104 {
105 return *getFlowData().flowInfo();
106 }

◆ getFlowRuntimeInfo()

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

Implements mxl::lib::FlowWriter.

Definition at line 113 of file PosixEventFlowWriter.cpp.

114 {
115 return getFlowData().flowInfo()->runtime;
116 }
mxlFlowRuntimeInfo runtime
Definition flowinfo.h:229

◆ isExclusive()

bool mxl::lib::PosixEventFlowWriter::isExclusive ( ) const
overridevirtual
Returns
True if the common flow metadata file is exclusively locked by this mapping.

Implements mxl::lib::FlowWriter.

Definition at line 152 of file PosixEventFlowWriter.cpp.

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

◆ makeExclusive()

bool mxl::lib::PosixEventFlowWriter::makeExclusive ( )
overridevirtual
Returns
True if exclusive common-metadata ownership is acquired, allowing flow deletion.

Implements mxl::lib::FlowWriter.

Definition at line 162 of file PosixEventFlowWriter.cpp.

163 {
164 if (!_flowData)
165 {
166 return false;
167 }
168
169 return _flowData->makeExclusive();
170 }

◆ openEvent()

mxlStatus mxl::lib::PosixEventFlowWriter::openEvent ( mxlEventInfo * info,
std::uint8_t ** payload )
overridevirtual

Begin a write and reset metadata to the event defaults.

Parameters
[out]infoReceives initialized metadata; must not be null.
[out]payloadReceives private writable storage; must not be null.
Returns
MXL_STATUS_OK, or MXL_ERR_INVALID_ARG if a write is already open.
See also
mxlFlowWriterOpenEvent for field defaults and payload lifetime.

Implements mxl::lib::EventFlowWriter.

Definition at line 118 of file PosixEventFlowWriter.cpp.

119 {
120 if (_open)
121 {
122 return MXL_ERR_INVALID_ARG;
123 }
124 *info = mxlEventInfo{
125 .version = EVENT_HEADER_VERSION,
126 .size = sizeof(mxlEventInfo),
127 .timestamp = 0,
128 .flags = 0,
129 .registryType = MXL_EVENT_REGISTRY_TYPE_MXL,
130 .dataItemType = {},
131 .eventSize = 0,
132 .offset = 0,
133 .complete = 1,
134 .reserved = {},
135 .index = MXL_UNDEFINED_INDEX,
136 };
137 *payload = _payload.data();
138 _open = true;
139 return MXL_STATUS_OK;
140 }
@ MXL_EVENT_REGISTRY_TYPE_MXL
Namespaced application DIT strings, such as x-mxl:example.
Definition flowinfo.h:130
gInfo flags
Mark the grain as invalid.
auto const timestamp
Definition test_time.cpp:61

Member Data Documentation

◆ _flowData

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

Owns common metadata, ring mappings and the common flow lock.

Definition at line 60 of file PosixEventFlowWriter.hpp.

◆ _lastTimestamp

std::uint64_t mxl::lib::PosixEventFlowWriter::_lastTimestamp
private

Last committed timestamp, restored from the ring on attachment.

Definition at line 63 of file PosixEventFlowWriter.hpp.

◆ _open

bool mxl::lib::PosixEventFlowWriter::_open
private

True while one caller-owned write transaction is open.

Definition at line 62 of file PosixEventFlowWriter.hpp.

◆ _payload

std::vector<std::uint8_t> mxl::lib::PosixEventFlowWriter::_payload
private

Private staging buffer; publication copies only eventSize bytes.

Definition at line 64 of file PosixEventFlowWriter.hpp.

◆ _watcher

std::shared_ptr<DomainWatcher> mxl::lib::PosixEventFlowWriter::_watcher
private

Watcher kept alive while the writer is registered.

Definition at line 65 of file PosixEventFlowWriter.hpp.


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