MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::fabrics::ofi::RCInitiatorEndpoint Class Reference

A single endpoint connected to a target that will be created when the user adds a target to an initiator. More...

#include <RCInitiator.hpp>

Collaboration diagram for mxl::lib::fabrics::ofi::RCInitiatorEndpoint:

Classes

struct  Connected
 The connected state. More...
struct  Connecting
 The connecting state. More...
struct  Disconnected
 The disconnected state. More...
struct  Done
 The endpoint is done and can be evicted from the initiator. More...
struct  Flushing
 The flushing state. More...
struct  Idle
 The idle state. More...

Public Member Functions

void activate (std::shared_ptr< CompletionQueue > const &cq, std::shared_ptr< EventQueue > const &eq)
 Try to activate the endpoint.
bool canEvict () const noexcept
 Returns true if the endpoint was shut down and can be evicted from the initiator.
void consume (Completion completion)
 Consume a completion that was posted to the associated completion queue.
void consume (Event event)
 Consume an event that was posted to the associated event queue.
void finishFlush ()
 Move the endpoint out of the flushing state when the flush is complete.
bool hasPendingWork () const noexcept
 Returns true if there is any pending events that the endpoint is waiting for, and for which the queues must be polled.
TargetInfo const & info () const noexcept
 Get the target info of the remote endpoint.
bool isIdle () const noexcept
 Returns true if the endpoint is idle and could be actived.
 RCInitiatorEndpoint (Endpoint, std::unique_ptr< EgressProtocol > proto, TargetInfo info)
 Construct a new RCInitiatorEndpoint object.
void shutdown ()
 Initiate a shutdown process.
void transferGrain (std::uint64_t localIndex, std::uint64_t remoteIndex, std::uint64_t remotePayloadOffset, SliceRange const &sliceRange)
 Post a data transfer request to this endpoint.
void transferSamples (std::uint64_t headIndex, std::size_t count)
 Post a data transfer request to this endpoint.

Static Public Attributes

static constexpr auto FlushTimeout = std::chrono::seconds(2)
 Upper bound for the flushing state. Some providers do not report a local shutdown event, and a peer that disappears may never complete the outstanding operations.

Private Types

enum class  AfterFlush { Reconnect , Disconnect , Done }
 The state that the endpoint goes to when a flush is complete. More...
using State = std::variant<Idle, Connecting, Connected, Flushing, Disconnected, Done>
 The various states that the endpoint can be in are stored inside a variant that we move from and then back into when processing events.

Private Member Functions

Idle restart (Endpoint const &endpoint)
 Restarts the endpoint. Produces a new Idle state from any previous state.

Static Private Member Functions

static Flushing startFlush (Endpoint ep, std::size_t pending, bool shutdownReceived, AfterFlush next)
 Create a flushing state that waits for the given number of outstanding operations.

Private Attributes

TargetInfo _info
std::unique_ptr< EgressProtocol > _proto
State _state

Detailed Description

A single endpoint connected to a target that will be created when the user adds a target to an initiator.

Definition at line 23 of file RCInitiator.hpp.

Member Typedef Documentation

◆ State

The various states that the endpoint can be in are stored inside a variant that we move from and then back into when processing events.

Definition at line 167 of file RCInitiator.hpp.

Member Enumeration Documentation

◆ AfterFlush

The state that the endpoint goes to when a flush is complete.

Enumerator
Reconnect 

Go to the idle state and connect again. Used after an error event, when the target can still be running.

Disconnect 

Go to the disconnected state. Used after the target has closed the connection.

Done 

Go to the done state, so that the endpoint is evicted. Used after the target was removed.

Definition at line 130 of file RCInitiator.hpp.

131 {
132 Reconnect,
133 Disconnect,
134 Done,
135 };

Constructor & Destructor Documentation

◆ RCInitiatorEndpoint()

mxl::lib::fabrics::ofi::RCInitiatorEndpoint::RCInitiatorEndpoint ( Endpoint ep,
std::unique_ptr< EgressProtocol > proto,
TargetInfo info )

Construct a new RCInitiatorEndpoint object.

Parameters
EndpointThe underlying endpoint object.
FabricAddressThe fabric address of the remote target.
RemoteRegionsThe remote memory regions on the target where data must be written to.

Definition at line 27 of file RCInitiator.cpp.

28 : _proto(std::move(proto))
29 , _info(std::move(info))
30 , _state(Idle{.ep = std::move(ep), .idleSince = std::chrono::steady_clock::time_point{}})
31 {}
TargetInfo const & info() const noexcept
Get the target info of the remote endpoint.
std::unique_ptr< EgressProtocol > _proto

Member Function Documentation

◆ activate()

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::activate ( std::shared_ptr< CompletionQueue > const & cq,
std::shared_ptr< EventQueue > const & eq )

Try to activate the endpoint.

The endpoint has an internal timer to slow down repeated failures to activate and/or connect. Until this timer has elapsed, the activation function will do nothing, and RCInitiatorEndpoint::isIdle() will return true. When the endpoint is eventually actived, it will have pending work until it is connected.

Definition at line 149 of file RCInitiator.cpp.

150 {
151 _state = std::visit(
152 overloaded{
153 [&](Idle state) -> State
154 {
155 // If the target is in the idle state for more than 5 seconds, it will be restarted.
156 auto idleDuration = std::chrono::steady_clock::now() - state.idleSince;
157 if (idleDuration < std::chrono::seconds(5))
158 {
159 return state;
160 }
161
162 MXL_INFO("Endpoint has been idle for {}ms, activating",
163 std::chrono::duration_cast<std::chrono::milliseconds>(idleDuration).count());
164
165 // The endpoint in an idle target is always fresh and thus needs to be bound the the queues.
166 state.ep.bind(eq);
167 state.ep.bind(cq, FI_TRANSMIT);
168
169 // Transition into the connecting state
170 auto const faddr = _info.fabricAddress.decode();
171 MXL_INFO("Connecting to {}", faddr.toString());
172 state.ep.connect(_info.fabricAddress);
173 return Connecting{.ep = std::move(state.ep)};
174 },
175 [](Connecting state) -> State { return state; },
176 [](Connected state) -> State { return state; },
177 [](Flushing state) -> State { return state; },
178 [](Disconnected state) -> State { return state; },
179 [](Done state) -> State { return state; },
180 },
181 std::move(_state));
182 }
#define MXL_INFO(...)
Definition Logging.hpp:45
std::variant< Idle, Connecting, Connected, Flushing, Disconnected, Done > State
The various states that the endpoint can be in are stored inside a variant that we move from and then...
std::size_t count(uuids::uuid id) const noexcept
Returns the number of writers registered for flow id 'id'.
The endpoint is done and can be evicted from the initiator.

◆ canEvict()

bool mxl::lib::fabrics::ofi::RCInitiatorEndpoint::canEvict ( ) const
nodiscardnoexcept

Returns true if the endpoint was shut down and can be evicted from the initiator.

Definition at line 43 of file RCInitiator.cpp.

44 {
45 return std::holds_alternative<Done>(_state);
46 }

◆ consume() [1/2]

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::consume ( Completion completion)

Consume a completion that was posted to the associated completion queue.

Definition at line 259 of file RCInitiator.cpp.

260 {
261 auto const error = completion.tryErr();
262
263 std::visit(
264 overloaded{
265 [](Idle const&) { MXL_WARN("Received a completion event while idle, ignoring."); },
266 [](Connecting const&) { MXL_WARN("Received a completion event while connecting, ignoring."); },
267 [&](Connected const&)
268 {
269 // A failed operation is finished as well, so it is counted like a successful one.
270 if (error)
271 {
272 MXL_ERROR("Received a completion error: {}", error->toString());
273 }
274 _proto->processCompletion(completion);
275 },
276 [&](Flushing& state)
277 {
278 // Operations that were still queued when the connection was shut down complete with an error. They count as flushed.
279 if (error)
280 {
281 MXL_DEBUG("Received a completion error while flushing: {}", error->toString());
282 }
283 if (state.pending > 0)
284 {
285 --state.pending;
286 }
287 },
288 [](Disconnected const&) { MXL_DEBUG("Ignoring completion after the target closed the connection."); },
289 [](Done const&) { MXL_DEBUG("Ignoring completion after shutdown."); },
290 },
291 _state);
292 }
#define MXL_ERROR(...)
Definition Logging.hpp:67
#define MXL_WARN(...)
Definition Logging.hpp:56
#define MXL_DEBUG(...)
Definition Logging.hpp:34

◆ consume() [2/2]

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::consume ( Event event)

Consume an event that was posted to the associated event queue.

Definition at line 184 of file RCInitiator.cpp.

185 {
186 _state = std::visit(
187 overloaded{
188 [](Idle state) -> State { return state; }, // Nothing to do when idle.
189 [&](Connecting state) -> State
190 {
191 if (ev.isError())
192 {
193 MXL_ERROR("Failed to connect endpoint: {}", ev.error().toString());
194
195 // Go into the idle state with a new endpoint
196 return restart(state.ep);
197 }
198 else if (ev.isConnected())
199 {
200 MXL_INFO("Endpoint is now connected");
201 return Connected{.ep = std::move(state.ep)};
202 }
203 else if (ev.isShutdown())
204 {
205 MXL_WARN("Received a shutdown event while connecting, going idle");
206
207 // Go to idle state with a new endpoint
208 return restart(state.ep);
209 }
210
211 MXL_WARN("Received an unexpected event while establishing a connection");
212 return state;
213 },
214 [&](Connected state) -> State
215 {
216 if (ev.isError())
217 {
218 MXL_WARN("Received an error event in connected state, going idle after flushing. Error: {}", ev.error().toString());
219 catchAndLogFabricError([&]() { state.ep.shutdown(); }, "Failed to shut down the endpoint after an error event");
220 return startFlush(std::move(state.ep), _proto->reset(), true, AfterFlush::Reconnect);
221 }
222 else if (ev.isShutdown())
223 {
224 // A target closes the connection only when it is destroyed. A restarted target has new target info, so the
225 // endpoint does not reconnect. The target stays added, so that it can be removed.
226 MXL_INFO("Remote endpoint has closed the connection.");
227 catchAndLogFabricError([&]() { state.ep.shutdown(); },
228 "Failed to shut down the endpoint after the remote endpoint closed it");
229 return startFlush(std::move(state.ep), _proto->reset(), true, AfterFlush::Disconnect);
230 }
231
232 return state;
233 },
234 [&](Flushing state) -> State
235 {
236 if (ev.isShutdown())
237 {
238 MXL_INFO("Received a shutdown event while flushing the completion queue");
239 state.shutdownReceived = true;
240 }
241 else if (ev.isError())
242 {
243 // The connection is gone and no shutdown event will follow. Outstanding operations can still complete.
244 MXL_ERROR("Received an error while shutting down: {}", ev.error().toString());
245 state.shutdownReceived = true;
246 }
247 else
248 {
249 MXL_ERROR("Received an unexpected event while shutting down");
250 }
251 return state;
252 },
253 [](Disconnected state) -> State { return state; },
254 [](Done state) -> State { return state; },
255 },
256 std::move(_state));
257 }
static Flushing startFlush(Endpoint ep, std::size_t pending, bool shutdownReceived, AfterFlush next)
Create a flushing state that waits for the given number of outstanding operations.
Idle restart(Endpoint const &endpoint)
Restarts the endpoint. Produces a new Idle state from any previous state.
void catchAndLogFabricError(F &&fun, std::string_view msg)

◆ finishFlush()

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::finishFlush ( )

Move the endpoint out of the flushing state when the flush is complete.

The flush is complete when a shutdown or error event was received and all operations that were outstanding at shutdown have completed, or when FlushTimeout has elapsed since the flush started. The endpoint then goes to the state that Flushing::next selects. In any other state, this is a no-op.

Definition at line 98 of file RCInitiator.cpp.

99 {
100 auto const state = std::get_if<Flushing>(&_state);
101 if (state == nullptr)
102 {
103 return;
104 }
105
106 auto const nextName = [next = state->next]()
107 {
108 switch (next)
109 {
110 case AfterFlush::Reconnect: return "idle";
111 case AfterFlush::Disconnect: return "disconnected";
112 case AfterFlush::Done: return "done";
113 }
114 return "unknown";
115 }();
116 if (state->shutdownReceived && (state->pending == 0))
117 {
118 MXL_INFO("Flushing complete, transitioning to {} state.", nextName);
119 }
120 else if (std::chrono::steady_clock::now() >= state->deadline)
121 {
122 MXL_WARN("Flushing timed out with {} outstanding operations, shutdown event received: {}. Transitioning to {} state.",
123 state->pending,
124 state->shutdownReceived,
125 nextName);
126 }
127 else
128 {
129 return;
130 }
131
132 switch (state->next)
133 {
134 case AfterFlush::Reconnect: _state = restart(state->ep); break;
136 case AfterFlush::Done: _state = Done{}; break;
137 }
138 }

◆ hasPendingWork()

bool mxl::lib::fabrics::ofi::RCInitiatorEndpoint::hasPendingWork ( ) const
nodiscardnoexcept

Returns true if there is any pending events that the endpoint is waiting for, and for which the queues must be polled.

Definition at line 48 of file RCInitiator.cpp.

49 {
50 return std::visit(
51 overloaded{
52 [](std::monostate) { return false; }, // Something went wrong with this target, but there is probably no work to do.
53 [](Idle const&) { return false; }, // An idle target has nothing in flight, and is not connected.
54 [](Connecting const&) { return true; }, // In the connecting state, the target is waiting for a connected event.
55 [&](Connected const&) { return _proto->hasPendingWork(); }, // While connected, a target has pending work when
56 // there are transfers that have not yet completed.
57 [](Flushing const&) { return true; }, // While flushing, the target waits for a shutdown event and outstanding completions.
58 [](Disconnected const&) { return false; }, // A disconnected target only waits to be removed.
59 [](Done const&) { return false; }, // In the done state, there is no pending work.
60 },
61 _state);
62 }

◆ info()

TargetInfo const & mxl::lib::fabrics::ofi::RCInitiatorEndpoint::info ( ) const
nodiscardnoexcept

Get the target info of the remote endpoint.

Definition at line 33 of file RCInitiator.cpp.

34 {
35 return _info;
36 }

◆ isIdle()

bool mxl::lib::fabrics::ofi::RCInitiatorEndpoint::isIdle ( ) const
nodiscardnoexcept

Returns true if the endpoint is idle and could be actived.

Definition at line 38 of file RCInitiator.cpp.

39 {
40 return std::holds_alternative<Idle>(_state);
41 }

◆ restart()

RCInitiatorEndpoint::Idle mxl::lib::fabrics::ofi::RCInitiatorEndpoint::restart ( Endpoint const & endpoint)
nodiscardprivate

Restarts the endpoint. Produces a new Idle state from any previous state.

The new endpoint has the same id as the old one, so completions of the old endpoint must not be in the completion queue anymore.

Definition at line 311 of file RCInitiator.cpp.

312 {
313 return Idle{.ep = Endpoint::create(old.domain(), old.id(), old.info()), .idleSince = std::chrono::steady_clock::now()};
314 }
static Endpoint create(std::shared_ptr< Domain > domain, FabricInfoView info)
Allocates (active) new endpoint.
Definition Endpoint.cpp:56

◆ shutdown()

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::shutdown ( )

Initiate a shutdown process.

The endpoint will have pending work until a shutdown or error event will be received. After which it can be evicted from the initiator.

Definition at line 64 of file RCInitiator.cpp.

65 {
66 _state = std::visit(
67 overloaded{
68 [](Idle) -> State
69 {
70 MXL_INFO("Shutdown requested while waiting to activate, aborting.");
71 return Done{};
72 },
73 [](Connecting) -> State
74 {
75 MXL_INFO("Shutdown requested while trying to connect, aborting.");
76 return Done{};
77 },
78 [&](Connected state) -> State
79 {
80 MXL_INFO("Shutting down");
81 auto pending = _proto->reset();
82 state.ep.shutdown();
83
84 return startFlush(std::move(state.ep), pending, false, AfterFlush::Done);
85 },
86 [](Flushing state) -> State
87 {
88 MXL_INFO("Shutdown requested while the endpoint is already flushing.");
89 state.next = AfterFlush::Done;
90 return state;
91 },
92 [](Disconnected) -> State { return Done{}; },
93 [](Done state) -> State { return state; },
94 },
95 std::move(_state));
96 }

◆ startFlush()

RCInitiatorEndpoint::Flushing mxl::lib::fabrics::ofi::RCInitiatorEndpoint::startFlush ( Endpoint ep,
std::size_t pending,
bool shutdownReceived,
AfterFlush next )
staticnodiscardprivate

Create a flushing state that waits for the given number of outstanding operations.

Definition at line 140 of file RCInitiator.cpp.

141 {
142 return Flushing{.ep = std::move(ep),
143 .pending = pending,
144 .shutdownReceived = shutdownReceived,
145 .next = next,
146 .deadline = std::chrono::steady_clock::now() + FlushTimeout};
147 }
static constexpr auto FlushTimeout
Upper bound for the flushing state. Some providers do not report a local shutdown event,...

◆ transferGrain()

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::transferGrain ( std::uint64_t localIndex,
std::uint64_t remoteIndex,
std::uint64_t remotePayloadOffset,
SliceRange const & sliceRange )

Post a data transfer request to this endpoint.

Definition at line 294 of file RCInitiator.cpp.

296 {
297 if (auto const state = std::get_if<Connected>(&_state); state != nullptr)
298 {
299 _proto->transferGrain(state->ep, localIndex, remoteIndex, remotePayloadOffset, sliceRange);
300 }
301 }

◆ transferSamples()

void mxl::lib::fabrics::ofi::RCInitiatorEndpoint::transferSamples ( std::uint64_t headIndex,
std::size_t count )

Post a data transfer request to this endpoint.

Definition at line 303 of file RCInitiator.cpp.

304 {
305 if (auto const state = std::get_if<Connected>(&_state); state != nullptr)
306 {
307 _proto->transferSamples(state->ep, headIndex, count);
308 }
309 }

Member Data Documentation

◆ _info

TargetInfo mxl::lib::fabrics::ofi::RCInitiatorEndpoint::_info
private

The target info of the remote endpoint

Definition at line 186 of file RCInitiator.hpp.

◆ _proto

std::unique_ptr<EgressProtocol> mxl::lib::fabrics::ofi::RCInitiatorEndpoint::_proto
private

Definition at line 185 of file RCInitiator.hpp.

◆ _state

State mxl::lib::fabrics::ofi::RCInitiatorEndpoint::_state
private

The internal state object.

Definition at line 187 of file RCInitiator.hpp.

◆ FlushTimeout

auto mxl::lib::fabrics::ofi::RCInitiatorEndpoint::FlushTimeout = std::chrono::seconds(2)
staticconstexpr

Upper bound for the flushing state. Some providers do not report a local shutdown event, and a peer that disappears may never complete the outstanding operations.

Definition at line 96 of file RCInitiator.hpp.


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