Skip to content

Commit cc2bfe5

Browse files
In middle of compilation
1 parent 63db87a commit cc2bfe5

File tree

4 files changed

+208
-81
lines changed

4 files changed

+208
-81
lines changed

common/events.cpp

+86-51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#include "events_pi.h"
22

3+
/*
4+
* The uuid_unparse() function converts the supplied UUID uu from
5+
* the binary representation into a 36-byte string (plus trailing
6+
* '\0') of the form 1b4e28ba-2fa1-11d2-883f-0016d3cca427 and stores
7+
* this value in the character string pointed to by out.
8+
*/
9+
#define UUID_STR_SIZE 40
10+
311
/*
412
* Publisher use echo service and subscriber uses cache service
513
* The eventd process runs this service, which could be down
@@ -18,7 +26,7 @@
1826
* duplicates helps reduce load.
1927
*/
2028

21-
typedef map <string, EventPublisher> lst_publishers_t;
29+
typedef map <string, EventPublisher *> lst_publishers_t;
2230
lst_publishers_t s_publishers;
2331

2432
EventPublisher::EventPublisher() :
@@ -31,29 +39,30 @@ int EventPublisher::init(const string event_source)
3139
m_zmq_ctx = zmq_ctx_new();
3240
m_socket = zmq_socket (m_zmq_ctx, ZMQ_PUB);
3341

34-
int rc = zmq_connect (m_socket, get_config(XSUB_END_KEY));
35-
RET_ON_ERR(rc == 0, "Publisher fails to connect %s", get_config(XSUB_END_KEY));
42+
int rc = zmq_connect (m_socket, get_config(XSUB_END_KEY).c_str());
43+
RET_ON_ERR(rc == 0, "Publisher fails to connect %s", get_config(XSUB_END_KEY).c_str());
3644

3745
// REQ socket is connected and a message is sent & received, more to
3846
// ensure PUB socket had enough time to establish connection.
3947
// Any message published before connection establishment is dropped.
4048
//
41-
event_service m_event_svc;
4249
/*
4350
* Event service could be down. So have a timeout.
4451
*
4552
*/
46-
rc = m_event_svc.init_client(m_zmq_ctx, EVENTS_SERVICE_TIMEOUT_MILLISECS);
53+
rc = m_event_service.init_client(m_zmq_ctx, EVENTS_SERVICE_TIMEOUT_MILLISECS);
4754
RET_ON_ERR (rc == 0, "Failed to init event service");
4855

49-
rc = m_event_svc.echo_send("hello");
56+
rc = m_event_service.echo_send("hello");
5057
RET_ON_ERR (rc == 0, "Failed to echo send in event service");
5158

52-
m_event_source(event_source);
59+
m_event_source = event_source;
5360

5461
uuid_t id;
62+
char uuid_str[UUID_STR_SIZE];
5563
uuid_generate(id);
56-
uuid_unparse(id, m_runtime_id);
64+
uuid_unparse(id, uuid_str);
65+
m_runtime_id = string(uuid_str);
5766

5867
m_init = true;
5968
out:
@@ -67,9 +76,12 @@ EventPublisher::~EventPublisher()
6776
}
6877

6978

70-
void
71-
EventPublisher::event_publish(const string tag, const event_params_t *params)
79+
int
80+
EventPublisher::publish(const string tag, const event_params_t *params)
7281
{
82+
int rc;
83+
internal_event_t event_data;
84+
7385
if (m_event_service.is_active()) {
7486
string s;
7587

@@ -78,28 +90,41 @@ EventPublisher::event_publish(const string tag, const event_params_t *params)
7890
* as provided in publisher init.
7991
*/
8092
m_event_service.echo_receive(s);
93+
m_event_service.close_service();
8194
}
8295

8396
string param_str;
84-
if ((params != NULL) && (param->find(event_ts) != params->end())) {
85-
86-
param_str = serialize(*params);
97+
event_params_t evt_params;
98+
if (params != NULL) {
99+
if (params->find(event_ts_param) == params->end()) {
100+
evt_params = *params;
101+
evt_params[event_ts_param] = get_timestamp();
102+
params = &evt_params;
103+
}
87104
}
88105
else {
89-
event_params_t evt_params = *params;
90-
evt_params[event_ts] = get_timestamp();
91-
param_str = serialize(evt_params);
106+
evt_params[event_ts_param] = get_timestamp();
107+
params = &evt_params;
92108
}
109+
rc = serialize(*params, param_str);
110+
RET_ON_ERR(rc == 0, "failed to serialize params %s",
111+
map_to_str(*params).c_str());
93112

94-
map_str_str_t event_str = { { m_event_source + ":" + tag, param_str}};
113+
{
114+
map_str_str_t event_str_map = { { m_event_source + ":" + tag, param_str}};
95115

96-
++m_sequence;
97-
internal_event_t event_data;
98-
event_data[EVENT_STR_DATA] = serialize(event_str);
116+
rc = serialize(event_str_map, event_data[EVENT_STR_DATA]);
117+
RET_ON_ERR(rc == 0, "failed to serialize event str %s",
118+
map_to_str(event_str_map));
119+
}
99120
event_data[EVENT_RUNTIME_ID] = m_runtime_id;
121+
++m_sequence;
100122
event_data[EVENT_SEQUENCE] = seq_to_str(m_sequence);
101123

102-
return zmq_message_send(m_socket, m_event_source, event_data);
124+
rc = zmq_message_send(m_socket, m_event_source, event_data);
125+
RET_ON_ERR(rc == 0, "failed to send for tag %s", tag.c_str());
126+
out:
127+
return rc;
103128
}
104129

105130
event_handle_t
@@ -121,7 +146,7 @@ events_init_publisher(const string event_source)
121146
}
122147
else {
123148
ret = p;
124-
s_publishers[key] = p;
149+
s_publishers[event_source] = p;
125150
}
126151
}
127152
return ret;
@@ -131,7 +156,6 @@ void
131156
events_deinit_publisher(event_handle_t &handle)
132157
{
133158
lst_publishers_t::iterator it;
134-
EventPublisher *pub = NULL;
135159

136160
for(it=s_publishers.begin(); it != s_publishers.end(); ++it) {
137161
if (it->second == handle) {
@@ -144,12 +168,13 @@ events_deinit_publisher(event_handle_t &handle)
144168

145169
}
146170

147-
void
171+
int
148172
event_publish(event_handle_t handle, const string tag, const event_params_t *params)
149173
{
150-
for(it=s_publishers.begin(); it != s_publishers.end(); ++it) {
151-
if (it->second == handle) {
152-
return it->second->event_publish(tag, params);
174+
lst_publishers_t::const_iterator itc;
175+
for(itc=s_publishers.begin(); itc != s_publishers.end(); ++itc) {
176+
if (itc->second == handle) {
177+
return itc->second->publish(tag, params);
153178
}
154179
}
155180
return -1;
@@ -175,17 +200,17 @@ EventSubscriber::~EventSubscriber()
175200
*/
176201
int rc = 0;
177202

178-
if (m_use_cache) {
203+
if (m_event_service.is_active()) {
179204
events_data_lst_t events;
180205

181-
rc = m_event_svc.cache_init();
206+
rc = m_event_service.cache_init();
182207
RET_ON_ERR(rc == 0, "Failed to init the cache");
183208

184209
/* Shadow the cache init request, as it is async */
185-
m_event_svc.send_recv(EVENT_ECHO);
210+
m_event_service.send_recv(EVENT_ECHO);
186211

187212
/* read for 2 seconds in non-block mode, to drain any local cache */
188-
chrono::steady_clock::timepoint start = chrono::steady_clock::now();
213+
chrono::steady_clock::time_point start = chrono::steady_clock::now();
189214
while(true) {
190215
string source, evt_str;
191216
rc = zmq_message_read(m_socket, ZMQ_DONTWAIT, source, evt_str);
@@ -196,15 +221,16 @@ EventSubscriber::~EventSubscriber()
196221
break;
197222
}
198223
events.push_back(evt_str);
199-
chrono::steady_clock::timepoint now = chrono::steady_clock::now();
200-
if (chrono::duration_cast<std::chrono::milliseconds>(now - start) >
224+
chrono::steady_clock::time_point now = chrono::steady_clock::now();
225+
if (chrono::duration_cast<std::chrono::milliseconds>(now - start).count() >
201226
CACHE_DRAIN_IN_MILLISECS)
202227
break;
203228
}
204229

205230
/* Start cache service with locally read events as initial stock */
206-
RET_ON_ERR(m_event_svc.cache_start(events) == 0,
231+
RET_ON_ERR(m_event_service.cache_start(events) == 0,
207232
"Failed to send cache start");
233+
m_event_service.close_service();
208234
}
209235
out:
210236
zmq_close(m_socket);
@@ -227,29 +253,28 @@ EventSubscriber::init(bool use_cache, const event_subscribe_sources_t *subs_sour
227253

228254
m_socket = zmq_socket (m_zmq_ctx, ZMQ_SUB);
229255

230-
int rc = zmq_connect (m_socket, get_config(XPUB_END_KEY));
231-
RET_ON_ERR(rc == 0, "Subscriber fails to connect %s", get_config(XPUB_END_KEY));
256+
int rc = zmq_connect (m_socket, get_config(XPUB_END_KEY).c_str());
257+
RET_ON_ERR(rc == 0, "Subscriber fails to connect %s", get_config(XPUB_END_KEY).c_str());
232258

233259
if ((subs_sources == NULL) || (subs_sources->empty())) {
234-
rc = zmq_setsockopt(sub_read, ZMQ_SUBSCRIBE, "", 0);
260+
rc = zmq_setsockopt(m_socket, ZMQ_SUBSCRIBE, "", 0);
235261
RET_ON_ERR(rc == 0, "Fails to set option");
236262
}
237263
else {
238264
for (const auto e: *subs_sources) {
239-
rc = zmq_setsockopt(sub_read, ZMQ_SUBSCRIBE, e.c_str(), e.size());
265+
rc = zmq_setsockopt(m_socket, ZMQ_SUBSCRIBE, e.c_str(), e.size());
240266
RET_ON_ERR(rc == 0, "Fails to set option");
241267
}
242268
}
243269

244270
if (use_cache) {
245-
rc = m_event_svc.init_client(m_zmq_ctx, EVENTS_SERVICE_TIMEOUT_MILLISECS);
271+
rc = m_event_service.init_client(m_zmq_ctx, EVENTS_SERVICE_TIMEOUT_MILLISECS);
246272
RET_ON_ERR(rc == 0, "Fails to init the service");
247273

248-
if (m_event_svc.cache_stop() == 0) {
274+
if (m_event_service.cache_stop() == 0) {
249275
// Stopped an active cache
250276
m_cache_read = true;
251277
}
252-
m_use_cache = true;
253278
}
254279
m_init = true;
255280
out:
@@ -277,25 +302,26 @@ EventSubscriber::prune_track()
277302

278303

279304
int
280-
EventSubscriber::event_receive(string &&key, event_params_t &params, int &missed_cnt)
305+
EventSubscriber::event_receive(string &key, event_params_t &params, int &missed_cnt)
281306
{
307+
int rc = 0;
282308
key.clear();
283309
event_params_t().swap(params);
284310

285311
while (key.empty()) {
286312
internal_event_t event_data;
287-
int rc = 0;
288313

289314
if (m_cache_read && m_from_cache.empty()) {
290-
m_event_svc.cache_read(m_from_cache);
315+
m_event_service.cache_read(m_from_cache);
291316
m_cache_read = !m_from_cache.empty();
292317
}
293318

294319
if (!m_from_cache.empty()) {
295320

296321
events_data_lst_t::iterator it = m_from_cache.begin();
297-
deserialize(*it, event_data);
322+
rc = deserialize(*it, event_data);
298323
m_from_cache.erase(it);
324+
RET_ON_ERR(rc == 0, "Failed to deserialize message from cache");
299325

300326
}
301327
else {
@@ -307,9 +333,9 @@ EventSubscriber::event_receive(string &&key, event_params_t &params, int &missed
307333

308334
/* Find any missed events for this runtime ID */
309335
missed_cnt = 0;
336+
sequence_t seq = events_base::str_to_seq(event_data[EVENT_SEQUENCE]);
310337
track_info_t::iterator it = m_track.find(event_data[EVENT_RUNTIME_ID]);
311338
if (it != m_track.end()) {
312-
sequence_t seq = events_base::str_to_seq(event_data[EVENT_SEQUENCE]);
313339
/* current seq - last read - 1 == 0 if none missed */
314340
missed_cnt = seq - it->second.seq - 1;
315341
it->second = evt_info_t(seq);
@@ -323,15 +349,24 @@ EventSubscriber::event_receive(string &&key, event_params_t &params, int &missed
323349
if (missed_cnt >= 0) {
324350
map_str_str_t ev;
325351

326-
deserialize(event_data[EVENT_STR_DATA], ev);
327-
RET_ON_ERR(ev.size() == 1, "Expect string (%s) deserialize to one key",
328-
event_data[EVENT_STR_DATA].c_str());
352+
rc = deserialize(event_data[EVENT_STR_DATA], ev);
353+
RET_ON_ERR(rc == 0, "Failed to deserialize %s",
354+
event_data[EVENT_STR_DATA].substr(0, 32).c_str());
355+
356+
if (ev.size() != 1) rc = -1;
357+
RET_ON_ERR(rc == 0, "Expect string (%s) deserialize to one key",
358+
event_data[EVENT_STR_DATA].substr(0, 32).c_str());
329359

330360
key = ev.begin()->first;
331361

332-
deserialize(ev.begin()->second, params);
362+
rc = deserialize(ev.begin()->second, params);
363+
RET_ON_ERR(rc == 0, "failed to deserialize params %s",
364+
ev.begin()->second.substr(0, 32).c_str());
333365

334366
}
367+
else {
368+
/* negative value implies duplicate; Possibly seen from cache */
369+
}
335370
}
336371
out:
337372
return rc;
@@ -340,7 +375,7 @@ EventSubscriber::event_receive(string &&key, event_params_t &params, int &missed
340375

341376

342377
/* Expect only one subscriber per process */
343-
EventSubscriber *s_subscriber = NULL;
378+
static EventSubscriber *s_subscriber = NULL;
344379

345380
event_handle_t
346381
events_init_subscriber(bool use_cache=false,

common/events.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ typedef std::map<std::string, std::string> event_params_t;
6060
/*
6161
* timestamp param name
6262
*/
63-
const std::string event_ts("timestamp");
63+
const std::string event_ts_param("timestamp");
6464

6565
/*
6666
* Publish an event

0 commit comments

Comments
 (0)