Belle II Software development
ZMQConfirmedInput Class Reference

Input part of a confirmed connection. More...

#include <ZMQConfirmedConnection.h>

Inheritance diagram for ZMQConfirmedInput:
ZMQConnectionOverSocket ZMQConnection ZMQLogger

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

 ZMQConfirmedInput (const std::string &inputAddress, const std::shared_ptr< ZMQParent > &parent)
 Create a new confirmed output by binding to the address.
 
std::unique_ptr< ZMQIdMessagehandleIncomingData ()
 Block until a message can be received from one of the inputs.
 
void clear ()
 Reset the counters for all received stop and terminate messages. Should be called on run start.
 
std::unique_ptr< ZMQIdMessageoverwriteStopMessage ()
 Manually overwrite the stop message counter and set it to have all stop messages received.
 
std::vector< zmq::socket_t * > getSockets () const final
 The socket used for polling is just the stored socket.
 
std::string getEndPoint () const
 Return the connection string for this socket.
 
virtual bool isReady () const
 Return true of this connection is able to send messages right now. Can be overloaded in derived classes.
 
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 storing 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.
 
static bool hasMessage (const ZMQConnection *connection)
 Check if the given connection as an incoming message (right now, no waiting).
 

Protected Attributes

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::set< std::string > m_receivedStopMessages
 The set of input identities which have already sent a stop message.
 
bool m_allStopMessages = false
 Have we received all stop messages?
 
std::set< std::string > m_receivedTerminateMessages
 The set of input identities which have already sent a terminate message.
 
bool m_allTerminateMessages = false
 Have we received all terminante messages?
 
std::set< std::string > m_registeredWorkersInput
 The set of all registered inputs.
 
bool m_eventAfterAllStopMessages = false
 A flag to check the events appear after the all stop messages.
 
std::chrono::time_point< std::chrono::system_clock > m_whenEventAfterAllStopMessages
 A time when the eventAfterAllStopMessages is issued.
 
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

Input part of a confirmed connection.

In a confirmed connection every message sent by the input to the output is confirmed by a confirmation message by the output. The output can handle multiple inputs, each input can only be connected to a single output.

The input part keeps track of the registered workers and if it has received all stop and/or terminate messages. Stop/Terminate messages are only passed on if received from all registered workers.

If the input receives a message from one of the outputs, it sends back a confirmation message. Depending on the message type, it does several things:

  • if the message is a hello message (the input registered itself), it adds the input identity to the registered workers. Nothing is returned to the caller in this case.
  • if the message is a delete message (someone unregistered a worker), it removes the specified input identity in the message from the registered workers. Nothing is returned to the caller in this case. (corner case: if it turns out the worker to be unregistered was the last one missing for a full stop or terminate message from all workers, this message is passed on)
  • if the message is a stop message, add it to the received stop messages. Only if all registered workers have send a stop message pass it on.
  • same for terminate messages
  • in all other cases just pass on the message.

Internally, a ZMQ_ROUTER is used in bind-mode. The order of starting output and input does not play any role.

Definition at line 55 of file ZMQConfirmedConnection.h.

Member Typedef Documentation

◆ ReactorFunction

using ReactorFunction = std::function<void(void)>
inherited

Typedef of a function which will be called if a connection has a message.

Definition at line 34 of file ZMQConnection.h.

Constructor & Destructor Documentation

◆ ZMQConfirmedInput()

ZMQConfirmedInput ( const std::string &  inputAddress,
const std::shared_ptr< ZMQParent > &  parent 
)

Create a new confirmed output by binding to the address.

Definition at line 16 of file ZMQConfirmedConnection.cc.

18{
19 // These are all the log output we will have, set to 0 in the beginning.
20 log("last_received_message", "");
21 log("total_number_messages", 0l);
22 log("registered_workers", 0l);
23 log("hello_messages", 0l);
24 log("dead_workers", 0l);
25 log("all_stop_messages", 0l);
26 log("sent_stop_messages", 0l);
27 log("last_stop_sent", "");
28 log("received_stop_messages", 0l);
29 log("all_terminate_messages", 0l);
30 log("sent_terminate_messages", 0l);
31 log("last_terminate_sent", "");
32 log("received_terminate_messages", 0l);
33 log("received_messages_after_stop", 0l);
34 log("last_received_event_message", "");
35 log("last_clear", "");
36 log("stop_overwrites", 0l);
37 log("last_stop_overwrite", "");
38
39 log("data_size", 0.0);
40 log("received_events", 0l);
41 log("event_rate", 0.0);
42
43 // Create a binding socket of router type
44 m_socket = m_parent->createSocket<ZMQ_ROUTER>(inputAddress, true);
45}
Specialized connection over a ZMQ socket.
Definition: ZMQConnection.h:63
std::shared_ptr< ZMQParent > m_parent
The shared ZMQParent instance.
Definition: ZMQConnection.h:75
std::unique_ptr< zmq::socket_t > m_socket
The memory of the socket. Needs to be initialized in a derived class.
Definition: ZMQConnection.h:77
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:96

Member Function Documentation

◆ clear()

void clear ( )

Reset the counters for all received stop and terminate messages. Should be called on run start.

Definition at line 179 of file ZMQConfirmedConnection.cc.

180{
181 // We clear all our internal state and counters
183 m_allStopMessages = false;
187
188 log("received_stop_messages", static_cast<long>(m_receivedStopMessages.size()));
189 log("all_stop_messages", static_cast<long>(m_allStopMessages));
190 log("received_terminate_messages", static_cast<long>(m_receivedTerminateMessages.size()));
191 log("all_terminate_messages", static_cast<long>(m_allTerminateMessages));
192
193 logTime("last_clear");
194}
std::set< std::string > m_receivedStopMessages
The set of input identities which have already sent a stop message.
bool m_eventAfterAllStopMessages
A flag to check the events appear after the all stop messages.
std::set< std::string > m_receivedTerminateMessages
The set of input identities which have already sent a terminate message.
bool m_allTerminateMessages
Have we received all terminante messages?
bool m_allStopMessages
Have we received all stop messages?
void logTime(const std::string &key)
Store the current time as a string under the given key.
Definition: ZMQLogger.cc:42

◆ decrement()

void decrement ( const std::string &  key)
inherited

Decrement the value with the given key (only numerical values). If not present, set to -1.

Definition at line 37 of file ZMQLogger.cc.

38{
39 std::visit(Decrementor{}, m_monitoring[key]);
40}
std::map< std::string, std::variant< long, double, std::string > > m_monitoring
Internal storage of all stored values.
Definition: ZMQLogger.h:58

◆ getEndPoint()

std::string getEndPoint ( ) const
inherited

Return the connection string for this socket.

Definition at line 87 of file ZMQConnection.cc.

88{
89 std::string endpoint{""};
90 if (m_socket) {
91 endpoint = m_socket->get(zmq::sockopt::last_endpoint);
92 }
93 return endpoint;
94}

◆ getMonitoringJSON()

std::string getMonitoringJSON ( ) const
virtualinherited

Convert the stored monitoring values to a JSON string ready for sending out via a message.

Reimplemented in ZMQHistoServerToZMQOutput, ZMQHistoServerToRawOutput, ZMQROIOutput, and ZMQDataAndROIOutput.

Definition at line 15 of file ZMQLogger.cc.

16{
17 std::stringstream buffer;
18 buffer << "{";
19 bool first = true;
20 for (const auto& keyValue : m_monitoring) {
21 if (not first) {
22 buffer << ", ";
23 }
24 first = false;
25 buffer << "\"" << keyValue.first << "\": ";
26 buffer << std::visit(toJSON{}, keyValue.second);
27 }
28 buffer << "}" << std::endl;
29 return buffer.str();
30}

◆ getSockets()

std::vector< zmq::socket_t * > getSockets ( ) const
finalvirtualinherited

The socket used for polling is just the stored socket.

Implements ZMQConnection.

Definition at line 82 of file ZMQConnection.cc.

83{
84 return {m_socket.get()};
85}

◆ handleIncomingData()

std::unique_ptr< ZMQIdMessage > handleIncomingData ( )

Block until a message can be received from one of the inputs.

React as described in the general description of this class. Only if a message is to be passed on actually returns a message (in all other cases a nullptr).

Definition at line 47 of file ZMQConfirmedConnection.cc.

48{
49 auto message = ZMQMessageFactory::fromSocket<ZMQIdMessage>(m_socket);
50 const auto fromIdentity = message->getIdentity();
51
52 logTime("last_received_message");
53 increment("total_number_messages");
54 increment("total_number_messages_from[" + fromIdentity + "]");
55
56 auto confirmMessage = ZMQMessageFactory::createMessage(fromIdentity, EMessageTypes::c_confirmMessage);
57 ZMQParent::send(m_socket, std::move(confirmMessage));
58
59 if (message->isMessage(EMessageTypes::c_helloMessage)) {
60 // a hello message makes us register the worker identity - which is the identity of the sender of the message
61 m_registeredWorkersInput.emplace(fromIdentity);
62 log("registered_workers", static_cast<long>(m_registeredWorkersInput.size()));
63 increment("hello_messages");
64 increment("hello_messages_from[" + fromIdentity + "]");
65 return {};
66 } else if (message->isMessage(EMessageTypes::c_deleteWorkerMessage)) {
67 // a delete message makes us forget about the worker identity. The identity is taken from the message data
68 // making it possible to delete other workers.
69 B2DEBUG(30, "Got message from " << message->getIdentity() << " to kill " << message->getMessagePartAsString<2>());
70 const std::string& killedIdentity = message->getMessagePartAsString<2>();
71 m_registeredWorkersInput.erase(killedIdentity);
72
73 log("registered_workers", static_cast<long>(m_registeredWorkersInput.size()));
74 increment("dead_workers");
75 increment("dead_worker_messaged_from[" + fromIdentity + "]");
76
77 if (m_registeredWorkersInput.empty()) {
78 B2ERROR("There is not a single worker registered anymore!");
79 return {};
80 }
81
82 // Corner case: could be that this was the one we were waiting for
84 m_allStopMessages = true;
85 log("all_stop_messages", static_cast<long>(m_allStopMessages));
86 increment("sent_stop_messages");
87 logTime("last_stop_sent");
88
89 return ZMQMessageFactory::createMessage(killedIdentity, EMessageTypes::c_lastEventMessage);
90 }
91 // Corner case: could be that this was the one we were waiting for
94 log("all_terminate_messages", static_cast<long>(m_allTerminateMessages));
95 increment("sent_terminate_messages");
96 logTime("last_terminate_sent");
97
98 return ZMQMessageFactory::createMessage(killedIdentity, EMessageTypes::c_terminateMessage);
99 }
100
101 return {};
102 }
103
104 B2ASSERT("Worker without proper registration!",
105 m_registeredWorkersInput.find(fromIdentity) != m_registeredWorkersInput.end());
106
107 if (message->isMessage(EMessageTypes::c_lastEventMessage)) {
108 // Increment the stop messages
109 m_receivedStopMessages.emplace(fromIdentity);
110 log("received_stop_messages", static_cast<long>(m_receivedStopMessages.size()));
111 increment("total_received_stop_messages");
112
114 // But only return this if everyone has sent a stop message already
115 m_allStopMessages = true;
116 log("all_stop_messages", static_cast<long>(m_allStopMessages));
117 increment("sent_stop_messages");
118 logTime("last_stop_sent");
119
120 return ZMQMessageFactory::createMessage(fromIdentity, EMessageTypes::c_lastEventMessage);
121 }
122
123 // Whatever we return here will be carried on to the application and eventually also to the output.
124 // This means as we are not passing the stop message now, we return nothing.
125 return {};
126 } else if (message->isMessage(EMessageTypes::c_terminateMessage)) {
127 // Increment the terminate messages
128 m_receivedTerminateMessages.emplace(fromIdentity);
129 log("received_terminate_messages", static_cast<long>(m_receivedTerminateMessages.size()));
130 increment("total_received_terminate_messages");
131
133 // But only return this if everyone has sent a terminate message already
135 log("all_terminate_messages", static_cast<long>(m_allTerminateMessages));
136 increment("sent_terminate_messages");
137 logTime("last_terminate_sent");
138
139 return ZMQMessageFactory::createMessage(fromIdentity, EMessageTypes::c_terminateMessage);
140 }
141
142 // Whatever we return here will be carried on to the application and eventually also to the output.
143 // This means as we are not passing the stop message now, we return nothing.
144 return {};
145 }
146
147 if (m_allStopMessages) {
150 m_whenEventAfterAllStopMessages = std::chrono::system_clock::now();
151 B2ERROR("Received an event after having received stop messages from every worker. This is not a good sign! I will dismiss this event and next events!");
152 }
153 increment("received_messages_after_stop");
154 auto t1 = std::chrono::system_clock::now();
155 const auto intervalAfterAllStopMessages = std::chrono::duration_cast<std::chrono::seconds> (t1 - m_whenEventAfterAllStopMessages);
156 if (intervalAfterAllStopMessages > std::chrono::seconds{150}) {
157 B2FATAL("Too many events after having received stop messages! This is abnormal. I will kill the process!");
158 }
159 return {};
160 }
161
162 // Now it can only be a plain normal data message, so just pass it on
163 const auto dataSize = message->getDataMessage().size();
164
165 average("data_size", dataSize);
166 average("data_size_from[" + fromIdentity + "]", dataSize);
167
168 increment("received_events");
169 increment("received_events[" + fromIdentity + "]");
170
171 timeit("event_rate");
172 timeit<200>("event_rate_from[" + fromIdentity + "]");
173
174 logTime("last_received_event_message");
175
176 return message;
177}
std::set< std::string > m_registeredWorkersInput
The set of all registered inputs.
std::chrono::time_point< std::chrono::system_clock > m_whenEventAfterAllStopMessages
A time when the eventAfterAllStopMessages is issued.
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:32
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.
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:117
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:153
void average(const std::string &key, double value)
Instead of storing the double value directly under the given key, store the average of the last MAX_S...
Definition: ZMQLogger.h:102

◆ hasMessage()

bool hasMessage ( const ZMQConnection connection)
staticinherited

Check if the given connection as an incoming message (right now, no waiting).

Definition at line 20 of file ZMQConnection.cc.

21{
22 // Just poll with 0 timeout and no reaction function. Hacky trick to reduce code duplication
23 const auto emptyFunction = []() {};
24 return ZMQConnection::poll({{connection, emptyFunction}}, 0);
25}
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.

◆ increment()

void increment ( const std::string &  key)
inherited

Increment the value with the given key (only numerical values). If not present, set to 1.

Definition at line 32 of file ZMQLogger.cc.

33{
34 std::visit(Incrementor{}, m_monitoring[key]);
35}

◆ isReady()

bool isReady ( ) const
virtualinherited

Return true of this connection is able to send messages right now. Can be overloaded in derived classes.

Reimplemented in ZMQROIOutput, ZMQDataAndROIOutput, ZMQLoadBalancedOutput, ZMQRawOutput, ZMQHistoServerToZMQOutput, and ZMQHistoServerToRawOutput.

Definition at line 15 of file ZMQConnection.cc.

16{
17 return true;
18}

◆ logTime()

void logTime ( const std::string &  key)
inherited

Store the current time as a string under the given key.

Definition at line 42 of file ZMQLogger.cc.

43{
44 auto current = std::chrono::system_clock::now();
45 auto displayTime = std::chrono::system_clock::to_time_t(current);
46 log(key, std::ctime(&displayTime));
47}

◆ overwriteStopMessage()

std::unique_ptr< ZMQIdMessage > overwriteStopMessage ( )

Manually overwrite the stop message counter and set it to have all stop messages received.

Definition at line 196 of file ZMQConfirmedConnection.cc.

197{
198 if (not m_allStopMessages) {
199 // We did not already receive all stop messages, but someone externally asked us to stop anyways. So lets do it.
200 B2ERROR("Sending out a stop message although not all of the workers are finished already!");
201 increment("stop_overwrites");
202 logTime("last_stop_overwrite");
203
204 m_allStopMessages = true;
205 log("all_stop_messages", static_cast<long>(m_allStopMessages));
206 increment("sent_stop_messages");
207 logTime("last_stop_sent");
208
209 return ZMQMessageFactory::createMessage("", EMessageTypes::c_lastEventMessage);
210 }
211
212 // We have already stopped, no need to sent it twice.
213 return {};
214}

◆ 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 27 of file ZMQConnection.cc.

28{
29 std::vector<const ReactorFunction*> socketMapping;
30 std::vector<zmq::pollitem_t> pollItems;
31
32 // zmq needs a special format for its polling, so create it here.
33 for (const auto& [connection, function] : connectionList) {
34 auto sockets = connection->getSockets();
35 for (zmq::socket_t* socket : sockets) {
36 zmq::pollitem_t pollItem;
37 pollItem.socket = static_cast<void*>(*socket);
38 pollItem.events = ZMQ_POLLIN;
39 pollItem.revents = 0;
40 pollItems.push_back(std::move(pollItem));
41
42 // but keep reference to the original function, so we can call the correct one later
43 socketMapping.push_back(&function);
44 }
45 }
46
47 if (pollItems.empty()) {
48 return false;
49 }
50
51 try {
52 zmq::poll(&pollItems[0], pollItems.size(), timeout);
53
54 bool anySocket = false;
55 unsigned int counter = 0;
56 for (const auto& pollItem : pollItems) {
57 if (pollItem.revents & ZMQ_POLLIN) {
58 anySocket = true;
59 const auto* functionPtr = socketMapping.at(counter);
60 const auto function = *functionPtr;
61 function();
62 }
63 counter++;
64 }
65
66 return anySocket;
67 } catch (zmq::error_t& error) {
68 if (error.num() == EINTR) {
69 // Could happen if there was an interrupt, return false so the caller knows the time did not pass already
70 return false;
71 } else {
72 // cannot handle, rethrow exception
73 throw;
74 }
75 }
76}

Member Data Documentation

◆ m_allStopMessages

bool m_allStopMessages = false
private

Have we received all stop messages?

Definition at line 78 of file ZMQConfirmedConnection.h.

◆ m_allTerminateMessages

bool m_allTerminateMessages = false
private

Have we received all terminante messages?

Definition at line 82 of file ZMQConfirmedConnection.h.

◆ m_averages

std::unordered_map<std::string, std::tuple<std::vector<double>, size_t> > m_averages
privateinherited

Internal storage of the previous values when calculating averages.

Definition at line 60 of file ZMQLogger.h.

◆ m_eventAfterAllStopMessages

bool m_eventAfterAllStopMessages = false
private

A flag to check the events appear after the all stop messages.

Definition at line 86 of file ZMQConfirmedConnection.h.

◆ m_monitoring

std::map<std::string, std::variant<long, double, std::string> > m_monitoring
privateinherited

Internal storage of all stored values.

Definition at line 58 of file ZMQLogger.h.

◆ m_parent

std::shared_ptr<ZMQParent> m_parent
protectedinherited

The shared ZMQParent instance.

Definition at line 75 of file ZMQConnection.h.

◆ m_receivedStopMessages

std::set<std::string> m_receivedStopMessages
private

The set of input identities which have already sent a stop message.

Definition at line 76 of file ZMQConfirmedConnection.h.

◆ m_receivedTerminateMessages

std::set<std::string> m_receivedTerminateMessages
private

The set of input identities which have already sent a terminate message.

Definition at line 80 of file ZMQConfirmedConnection.h.

◆ m_registeredWorkersInput

std::set<std::string> m_registeredWorkersInput
private

The set of all registered inputs.

Definition at line 84 of file ZMQConfirmedConnection.h.

◆ m_socket

std::unique_ptr<zmq::socket_t> m_socket
protectedinherited

The memory of the socket. Needs to be initialized in a derived class.

Definition at line 77 of file ZMQConnection.h.

◆ m_timeCounters

std::unordered_map<std::string, std::tuple<unsigned long, std::chrono::system_clock::time_point> > m_timeCounters
privateinherited

Internal storage how often the timeit function for a given key was called and when it has last reached MAX_SIZE.

Definition at line 62 of file ZMQLogger.h.

◆ m_whenEventAfterAllStopMessages

std::chrono::time_point<std::chrono::system_clock> m_whenEventAfterAllStopMessages
private

A time when the eventAfterAllStopMessages is issued.

Definition at line 88 of file ZMQConfirmedConnection.h.


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