Belle II Software  release-05-01-25
ZMQLoadBalancedOutput Class Reference

Output part of a load-balanced connection. More...

#include <ZMQLoadBalancedConnection.h>

Inheritance diagram for ZMQLoadBalancedOutput:
Collaboration diagram for ZMQLoadBalancedOutput:

Public Types

using ReactorFunction = std::function< void(void)>
 Typedef of a function which will be called if a connection has a message.
 

Public Member Functions

 ZMQLoadBalancedOutput (const std::string &outputAddress, bool lax, const std::shared_ptr< ZMQParent > &parent)
 Create a new load-balanced output and bind to the given address.
 
void handleEvent (std::unique_ptr< ZMQNoIdMessage > message)
 Send the given message (without identity) to the next input in the ready list. More...
 
void handleIncomingData ()
 Block until a ready message from an input is received and add it to the ready queue.
 
void clear ()
 Clear the counter for sent stop and terminate messages. Should be called on run start.
 
bool isReady () const final
 If lax mode is disabled, the output is ready if at least a single input is ready. Else always.
 
std::vector< zmq::socket_t * > getSockets () const final
 The socket used for polling is just the stored socket.
 
virtual std::string getMonitoringJSON () const
 Convert the stored monitoring values to a JSON string ready for sending out via a message.
 
template<class AClass >
void log (const std::string &key, const AClass &value)
 Store a value under a certain key. Different types of values can be stored, namely long, double or string. Mixtures are not allowed for a given key.
 
void increment (const std::string &key)
 Increment the value with the given key (only numerical values). If not present, set to 1.
 
void decrement (const std::string &key)
 Decrement the value with the given key (only numerical values). If not present, set to -1.
 
template<size_t MAX_SIZE = 100>
void average (const std::string &key, double value)
 Instead of storeing the double value directly under the given key, store the average of the last MAX_SIZE values.
 
template<size_t AVERAGE_SIZE = 2000>
void timeit (const std::string &key)
 Measure the rate of calls with the same key every AVERAGE_SIZE calls (and also display the last time AVERAGE_SIZE was reached under <key>_last_measurement)
 
void logTime (const std::string &key)
 Store the current time as a string under the given key.
 

Static Public Member Functions

static bool poll (const std::map< const ZMQConnection *, ReactorFunction > &connectionList, int timeout)
 Poll on the given connections and call the attached function if a messages comes in. More...
 
static bool hasMessage (const ZMQConnection *connection)
 Check if the given connection as an incoming message (right now, no waiting).
 

Protected Attributes

std::deque< std::string > m_readyWorkers
 List of identities of ready inputs in LIFO order.
 
std::set< std::string > m_allWorkers
 All ever registered inputs.
 
bool m_sentStopMessages = false
 Did we already sent a stop message?
 
bool m_sentTerminateMessages = false
 Did we already sent a terminate message?
 
bool m_lax = false
 Parameter to enable lax mode.
 
std::shared_ptr< ZMQParentm_parent
 The shared ZMQParent instance.
 
std::unique_ptr< zmq::socket_t > m_socket
 The memory of the socket. Needs to be initialized in a derived class.
 

Private Attributes

std::map< std::string, std::variant< long, double, std::string > > m_monitoring
 Internal storage of all stored values.
 
std::unordered_map< std::string, std::tuple< std::vector< double >, size_t > > m_averages
 Internal storage of the previous values when calculating averages.
 
std::unordered_map< std::string, std::tuple< unsigned long, std::chrono::system_clock::time_point > > m_timeCounters
 Internal storage how often the timeit function for a given key was called and when it has last reached MAX_SIZE.
 

Detailed Description

Output part of a load-balanced connection.

Multiple inputs can connect to this output. The output keeps a list of all received ready messages and sends events on requests always to the next input in the list, which creates some sort of load-balancing.

Stop and terminate messages are sent all all inputs that have sent at least a single ready so far. There is no unregistration happening, so dead inputs will still be served with both events and stop/terminate messages. For stop/terminate messages this is no problem and event messages will only be sent as long as there are ready messages.

After a stop message is sent all additional incoming events will be dismissed.

If no input has send any ready message, the output is not ready and needs to be polled for new ready messages. However, there is also a "lax" mode which makes the events being silently discarded if no input is ready.

Internally a ZMQ_ROUTER in bind mode is used.

Definition at line 81 of file ZMQLoadBalancedConnection.h.

Member Function Documentation

◆ handleEvent()

void handleEvent ( std::unique_ptr< ZMQNoIdMessage message)

Send the given message (without identity) to the next input in the ready list.

If it is a stop or terminate message send the message to all inputs which have ever sent a ready message. If it is an event message, sent it only to the next ready input. If there is no ready input it either (a) throws and exception if lax mode is not enabled. Make sure to only sent events if the output is ready (which indicates exactly this: an input is ready). Or (b) discard the event without warning if lax mode is enabled.

Definition at line 83 of file ZMQLoadBalancedConnection.cc.

84 {
85  if (message->isMessage(EMessageTypes::c_lastEventMessage)) {
86  // Tell all workers to stop this run
87  for (auto worker : m_allWorkers) {
88  auto sendMessage = ZMQMessageFactory::createMessage(worker, EMessageTypes::c_lastEventMessage);
89  ZMQParent::send(m_socket, std::move(sendMessage));
90  }
91  m_sentStopMessages = true;
92  log("all_stop_messages", static_cast<long>(m_sentStopMessages));
93  increment("sent_stop_messages");
94  logTime("last_stop_sent");
95  return;
96  } else if (message->isMessage(EMessageTypes::c_terminateMessage)) {
97  // Tell all workers to terminate
98  for (auto worker : m_allWorkers) {
99  auto sendMessage = ZMQMessageFactory::createMessage(worker, EMessageTypes::c_terminateMessage);
100  ZMQParent::send(m_socket, std::move(sendMessage));
101  }
103  log("all_terminate_messages", static_cast<long>(m_sentTerminateMessages));
104  increment("sent_terminate_messages");
105  logTime("last_terminate_sent");
106  return;
107  }
108 
109  if (m_lax and m_readyWorkers.empty()) {
110  // There is no one that can handle the event in the moment, dismiss it (if lax is true)
111  increment("dismissed_events");
112  return;
113  }
114  if (m_sentStopMessages) {
115  B2ERROR("Received events after stop! I will dismiss this event.");
116  increment("dismissed_events");
117  return;
118  }
119 
120  const auto dataSize = message->getDataMessage().size();
121 
122  B2ASSERT("Must be > 0", not m_readyWorkers.empty());
123  auto nextWorker = m_readyWorkers.front();
124  m_readyWorkers.pop_front();
125 
126  average("data_size", dataSize);
127  average("data_size_to[" + nextWorker + "]", dataSize);
128 
129  increment("sent_events");
130  increment("sent_events[" + nextWorker + "]");
131 
132  timeit("event_rate");
133  timeit<200>("event_rate_to[" + nextWorker + "]");
134 
135  m_socket->send(ZMQMessageHelper::createZMQMessage(nextWorker), ZMQ_SNDMORE);
136  ZMQParent::send(m_socket, std::move(message));
137 
138  log("ready_queue_size", static_cast<long>(m_readyWorkers.size()));
139  decrement("ready_messages[" + nextWorker + "]");
140 }

◆ poll()

bool poll ( const std::map< const ZMQConnection *, ReactorFunction > &  connectionList,
int  timeout 
)
staticinherited

Poll on the given connections and call the attached function if a messages comes in.

If after timeout milliseconds still no message is received, return anyways. If timeout is 0, do not wait. If timeout is -1, wait infinitely.

Returns true if a message was received on any socket, false otherwise. Attention: in case of an interrupted system call (e.g. because a signal was received) the function might return anyways with a negative result even before the timeout!

Definition at line 29 of file ZMQConnection.cc.


The documentation for this class was generated from the following files:
Belle2::ZMQLoadBalancedOutput::m_sentStopMessages
bool m_sentStopMessages
Did we already sent a stop message?
Definition: ZMQLoadBalancedConnection.h:113
Belle2::ZMQLogger::timeit
void timeit(const std::string &key)
Measure the rate of calls with the same key every AVERAGE_SIZE calls (and also display the last time ...
Definition: ZMQLogger.h:127
Belle2::ZMQParent::send
static void send(std::unique_ptr< zmq::socket_t > &socket, AZMQMessage message)
Send a given message over the given created socket. You need to move in the message for zero-copy.
Definition: ZMQParent.h:163
Belle2::ZMQLogger::logTime
void logTime(const std::string &key)
Store the current time as a string under the given key.
Definition: ZMQLogger.cc:44
Belle2::ZMQMessageHelper::createZMQMessage
static zmq::message_t createZMQMessage(zmq::message_t message)
Just pass a zmq message.
Definition: ZMQMessageHelper.h:39
Belle2::ZMQLogger::decrement
void decrement(const std::string &key)
Decrement the value with the given key (only numerical values). If not present, set to -1.
Definition: ZMQLogger.cc:39
Belle2::ZMQLogger::log
void log(const std::string &key, const AClass &value)
Store a value under a certain key. Different types of values can be stored, namely long,...
Definition: ZMQLogger.h:106
Belle2::ZMQLogger::increment
void increment(const std::string &key)
Increment the value with the given key (only numerical values). If not present, set to 1.
Definition: ZMQLogger.cc:34
Belle2::ZMQLoadBalancedOutput::m_lax
bool m_lax
Parameter to enable lax mode.
Definition: ZMQLoadBalancedConnection.h:117
Belle2::ZMQLoadBalancedOutput::m_sentTerminateMessages
bool m_sentTerminateMessages
Did we already sent a terminate message?
Definition: ZMQLoadBalancedConnection.h:115
Belle2::ZMQMessageFactory::createMessage
static auto createMessage(const std::string &msgIdentity, const EMessageTypes msgType, const std::unique_ptr< EvtMessage > &eventMessage)
Create an ID Message out of an identity, the type and an event message.
Definition: ZMQMessageFactory.h:37
Belle2::ZMQLogger::average
void average(const std::string &key, double value)
Instead of storeing the double value directly under the given key, store the average of the last MAX_...
Definition: ZMQLogger.h:112
Belle2::ZMQLoadBalancedOutput::m_readyWorkers
std::deque< std::string > m_readyWorkers
List of identities of ready inputs in LIFO order.
Definition: ZMQLoadBalancedConnection.h:109
Belle2::ZMQConnectionOverSocket::m_socket
std::unique_ptr< zmq::socket_t > m_socket
The memory of the socket. Needs to be initialized in a derived class.
Definition: ZMQConnection.h:84
Belle2::ZMQLoadBalancedOutput::m_allWorkers
std::set< std::string > m_allWorkers
All ever registered inputs.
Definition: ZMQLoadBalancedConnection.h:111