-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMonitor.cpp
More file actions
592 lines (552 loc) · 25.3 KB
/
Copy pathMonitor.cpp
File metadata and controls
592 lines (552 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file Monitor.cpp
*/
#include <list>
#include <string>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/topic/TopicDataType.hpp>
#include <fastdds/statistics/topic_names.hpp>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <fastdds_statistics_backend/exception/Exception.hpp>
#include <fastdds_statistics_backend/listener/CallbackMask.hpp>
#include <fastdds_statistics_backend/listener/DomainListener.hpp>
#include <fastdds_statistics_backend/StatisticsBackend.hpp>
#include <fastdds_statistics_backend/types/EntityId.hpp>
#include <fastdds_statistics_backend/types/types.hpp>
#include <database/database_queue.hpp>
#include <Monitor.hpp>
#include <StatisticsBackendData.hpp>
#include <topic_types/typesPubSubTypes.h>
using namespace eprosima::statistics_backend;
using namespace eprosima::fastdds::dds;
using namespace eprosima::fastdds::statistics;
class MonitorListener : public DomainListener
{
public:
MonitorListener()
: num_entities_discovered_(0)
{
}
virtual ~MonitorListener() override
{
}
/*!
* This function is called when a new DomainParticipant is discovered by the library,
* or a previously discovered DomainParticipant changes its QOS or is removed.
*
* @param domain_id Entity ID of the domain in which the DataReader has been discovered.
* @param participant_id Entity ID of the discovered DomainParticipant.
* @param status The status of the discovered DomainParticipants.
*/
void on_participant_discovery(
EntityId domain_id,
EntityId participant_id,
const Status& status) override
{
static_cast<void>(domain_id);
static_cast<void>(participant_id);
std::unique_lock<std::mutex> lock(mutex_);
num_entities_discovered_ += status.current_count_change;
cv_.notify_all();
}
/*!
* This function is called when a new Topic is discovered by the library.
*
* @param domain_id Entity ID of the domain in which the topic has been discovered.
* @param topic_id Entity ID of the discovered topic.
* @param status The status of the discovered topic.
*/
virtual void on_topic_discovery(
EntityId domain_id,
EntityId topic_id,
const Status& status)
{
static_cast<void>(domain_id);
static_cast<void>(topic_id);
std::unique_lock<std::mutex> lock(mutex_);
num_entities_discovered_ += status.current_count_change;
cv_.notify_all();
}
/*!
* This function is called when a new DataReader is discovered by the library,
* or a previously discovered DataReader changes its QOS or is removed.
*
* @param domain_id Entity ID of the domain in which the DataReader has been discovered.
* @param datareader_id Entity ID of the discovered DataReader.
* @param status The status of the discovered DataReaders.
*/
virtual void on_datareader_discovery(
EntityId domain_id,
EntityId datareader_id,
const Status& status)
{
static_cast<void>(domain_id);
static_cast<void>(datareader_id);
std::unique_lock<std::mutex> lock(mutex_);
num_entities_discovered_ += status.current_count_change;
cv_.notify_all();
}
/*!
* This function is called when a new DataWriter is discovered by the library,
* or a previously discovered DataWriter changes its QOS or is removed.
*
* @param domain_id Entity ID of the domain in which the DataWriter has been discovered.
* @param datawriter_id Entity ID of the discovered DataWriter.
* @param status The status of the discovered DataWriters.
*/
virtual void on_datawriter_discovery(
EntityId domain_id,
EntityId datawriter_id,
const Status& status)
{
static_cast<void>(domain_id);
static_cast<void>(datawriter_id);
std::unique_lock<std::mutex> lock(mutex_);
num_entities_discovered_ += status.current_count_change;
cv_.notify_all();
}
std::mutex mutex_;
std::condition_variable cv_;
unsigned int num_entities_discovered_;
};
/* ARGUMENTS
*/
int main(
int argc,
char** argv)
{
int arg_count = 1;
uint32_t seed = 7800;
unsigned int num_participants = 2;
unsigned int num_readers = 1;
// Each participant creates a meta traffic endpoint
unsigned int num_writers = num_participants + 1;
// Additionally, we need a meta traffic topic per domain
unsigned int num_topics = 2;
unsigned int num_entities = num_participants + num_topics + num_readers + num_writers;
// Process arguments
while (arg_count < argc)
{
if (strcmp(argv[arg_count], "--seed") == 0)
{
if (++arg_count >= argc)
{
std::cout << "--seed expects a parameter" << std::endl;
return -1;
}
seed = strtol(argv[arg_count], nullptr, 10);
}
++arg_count;
}
DomainId domain_id = seed % 230;
MonitorListener listener;
EntityId monitor_id;
// Test: The database starts empty
{
// Check the database is empty
try
{
if (!StatisticsBackend::get_entities(EntityKind::HOST).empty() ||
!StatisticsBackend::get_entities(EntityKind::USER).empty() ||
!StatisticsBackend::get_entities(EntityKind::PROCESS).empty() ||
!StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).empty() ||
!StatisticsBackend::get_entities(EntityKind::TOPIC).empty() ||
!StatisticsBackend::get_entities(EntityKind::PARTICIPANT).empty() ||
!StatisticsBackend::get_entities(EntityKind::DATAWRITER).empty() ||
!StatisticsBackend::get_entities(EntityKind::DATAREADER).empty() ||
!StatisticsBackend::get_entities(EntityKind::LOCATOR).empty())
{
throw Error("Error: database contains unexpected entities");
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return 1;
}
}
// Test: Init the monitor activate the monitor entity
{
// Init the monitor
domain_id = seed % 230;
monitor_id = StatisticsBackend::init_monitor(domain_id, &listener);
if (!monitor_id.is_valid())
{
std::cout << "Error creating monitor" << std::endl;
return 1;
}
// Check the database only contains the monitor domain
try
{
if (!StatisticsBackend::get_entities(EntityKind::HOST).empty() ||
!StatisticsBackend::get_entities(EntityKind::USER).empty() ||
!StatisticsBackend::get_entities(EntityKind::PROCESS).empty() ||
!StatisticsBackend::get_entities(EntityKind::TOPIC).empty() ||
!StatisticsBackend::get_entities(EntityKind::PARTICIPANT).empty() ||
!StatisticsBackend::get_entities(EntityKind::DATAWRITER).empty() ||
!StatisticsBackend::get_entities(EntityKind::DATAREADER).empty() ||
!StatisticsBackend::get_entities(EntityKind::LOCATOR).empty() ||
(StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).size() != 1) ||
(StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).begin()->value() != monitor_id))
{
throw Error("Error: database contains unexpected entities");
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: DOMAIN with id: " + std::to_string(
entity.value()) + " is inactive after init_monitor");
}
}
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return 1;
}
}
// Test: After discover participants and endpoints, all the entities are active
{
// Start the subprocesses
std::cout << "Init Monitor_" << seed << std::endl;
// Wait until discover all entities
{
std::unique_lock<std::mutex> lock(listener.mutex_);
listener.cv_.wait(lock, [&]
{
return listener.num_entities_discovered_ >= num_entities;
});
}
// Check that the database contains all entities related to subscriber and publisher entities
// and check that all entities are active
try
{
if (!StatisticsBackend::get_entities(EntityKind::HOST).empty() ||
!StatisticsBackend::get_entities(EntityKind::USER).empty() ||
!StatisticsBackend::get_entities(EntityKind::PROCESS).empty())
{
throw Error("Error: database contains unexpected entities");
}
else if (StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).size() != 1 ||
StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).begin()->value() != monitor_id)
{
throw Error("Error: database contains unexpected DOMAIN");
}
else if (StatisticsBackend::get_entities(EntityKind::TOPIC).size() != num_topics ||
StatisticsBackend::get_entities(EntityKind::TOPIC, monitor_id).size() != num_topics)
{
throw Error("Error: database contains unexpected TOPIC");
}
else if (StatisticsBackend::get_entities(EntityKind::PARTICIPANT).size() != num_participants ||
StatisticsBackend::get_entities(EntityKind::PARTICIPANT, monitor_id).size() != num_participants)
{
throw Error("Error: database contains unexpected PARTICIPANT");
}
else if (StatisticsBackend::get_entities(EntityKind::DATAWRITER).size() != num_writers ||
StatisticsBackend::get_entities(EntityKind::DATAWRITER, monitor_id).size() != num_writers)
{
throw Error("Error: database contains unexpected DATAWRITER");
}
else if (StatisticsBackend::get_entities(EntityKind::DATAREADER).size() != num_readers ||
StatisticsBackend::get_entities(EntityKind::DATAREADER, monitor_id).size() != num_readers)
{
throw Error("Error: database contains unexpected DATAREADER");
}
else if (StatisticsBackend::get_entities(EntityKind::LOCATOR).size() != num_readers + num_writers ||
StatisticsBackend::get_entities(EntityKind::LOCATOR,
monitor_id).size() != num_readers + num_writers)
{
throw Error("Error: database contains unexpected LOCATOR");
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: DOMAIN with id: " + std::to_string(
entity.value()) + " is inactive after discovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::TOPIC, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: TOPIC with id: " + std::to_string(
entity.value()) + " is inactive after discovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::PARTICIPANT, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: PARTICIPANT with id: " + std::to_string(
entity.value()) + " is inactive after discovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DATAWRITER, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: DATAWRITER with id: " + std::to_string(
entity.value()) + " is inactive after discovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DATAREADER, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: DATAREADER with id: " + std::to_string(
entity.value()) + " is inactive after discovering participants");
}
}
}
catch (const std::exception& e)
{
std::cerr << "Stop Monitor_" << seed << "\n" << e.what() << '\n';
return 1;
}
}
// Test: After undiscover participants and endpoints, all the entities except the monitor are inactive
{
// Wait until Undiscover participants and endpoints. The topic is not undiscovered by callback
{
std::unique_lock<std::mutex> lock(listener.mutex_);
listener.cv_.wait(lock, [&]
{
return listener.num_entities_discovered_ == 0;
});
}
// Check that the database contains all entities related to subscriber and publisher entities
// and check that all entities except the monitor are inactive
try
{
if (!StatisticsBackend::get_entities(EntityKind::HOST).empty() ||
!StatisticsBackend::get_entities(EntityKind::USER).empty() ||
!StatisticsBackend::get_entities(EntityKind::PROCESS).empty())
{
throw Error("Error: database contains unexpected entities");
}
else if (StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).size() != 1 ||
StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).begin()->value() != monitor_id)
{
throw Error("Error: database contains unexpected DOMAIN");
}
else if (StatisticsBackend::get_entities(EntityKind::TOPIC).size() != num_topics ||
StatisticsBackend::get_entities(EntityKind::TOPIC, monitor_id).size() != num_topics)
{
throw Error("Error: database contains unexpected TOPIC");
}
else if (StatisticsBackend::get_entities(EntityKind::PARTICIPANT).size() != num_participants ||
StatisticsBackend::get_entities(EntityKind::PARTICIPANT, monitor_id).size() != num_participants)
{
throw Error("Error: database contains unexpected PARTICIPANT");
}
else if (StatisticsBackend::get_entities(EntityKind::DATAWRITER).size() != num_writers ||
StatisticsBackend::get_entities(EntityKind::DATAWRITER, monitor_id).size() != num_writers)
{
throw Error("Error: database contains unexpected DATAWRITER");
}
else if (StatisticsBackend::get_entities(EntityKind::DATAREADER).size() != num_readers ||
StatisticsBackend::get_entities(EntityKind::DATAREADER, monitor_id).size() != num_readers)
{
throw Error("Error: database contains unexpected DATAREADER");
}
else if (StatisticsBackend::get_entities(EntityKind::LOCATOR).size() != num_readers + num_writers ||
StatisticsBackend::get_entities(EntityKind::LOCATOR,
monitor_id).size() != num_readers + num_writers)
{
throw Error("Error: database contains unexpected LOCATOR");
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY, monitor_id))
{
if (!StatisticsBackend::is_active(entity))
{
throw Error("Error: DOMAIN with id: " + std::to_string(
entity.value()) + " is inactive after undiscovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::TOPIC, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: TOPIC with id: " + std::to_string(
entity.value()) + " is active after undiscovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::PARTICIPANT, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: PARTICIPANT with id: " + std::to_string(
entity.value()) + " is active after undiscovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DATAWRITER, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: DATAWRITER with id: " + std::to_string(
entity.value()) + " is active after undiscovering participants");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DATAREADER, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: DATAREADER with id: " + std::to_string(
entity.value()) + " is active after undiscovering participants");
}
}
}
catch (const std::exception& e)
{
std::cerr << "Stop Monitor_" << seed << "\n" << e.what() << '\n';
return 1;
}
}
// Test: Stopping the monitor deactivate all entities
{
// Stop the monitor
try
{
StatisticsBackend::stop_monitor(monitor_id);
}
catch (const std::exception& e)
{
std::cerr << "Stop Monitor_" << seed << "\n" << "Error stopping monitor" << e.what() << '\n';
return 1;
}
// Check that the database contains all entities related to subscriber and publisher entities
// and check that all entities are inactive
try
{
if (!StatisticsBackend::get_entities(EntityKind::HOST).empty() ||
!StatisticsBackend::get_entities(EntityKind::USER).empty() ||
!StatisticsBackend::get_entities(EntityKind::PROCESS).empty())
{
throw Error("Error: database contains unexpected entities");
}
else if (StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).size() != 1 ||
StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).begin()->value() != monitor_id)
{
throw Error("Error: database contains unexpected DOMAIN");
}
else if (StatisticsBackend::get_entities(EntityKind::TOPIC).size() != num_topics ||
StatisticsBackend::get_entities(EntityKind::TOPIC, monitor_id).size() != num_topics)
{
throw Error("Error: database contains unexpected TOPIC");
}
else if (StatisticsBackend::get_entities(EntityKind::PARTICIPANT).size() != num_participants ||
StatisticsBackend::get_entities(EntityKind::PARTICIPANT, monitor_id).size() != num_participants)
{
throw Error("Error: database contains unexpected PARTICIPANT");
}
else if (StatisticsBackend::get_entities(EntityKind::DATAWRITER).size() != num_writers ||
StatisticsBackend::get_entities(EntityKind::DATAWRITER, monitor_id).size() != num_writers)
{
throw Error("Error: database contains unexpected DATAWRITER");
}
else if (StatisticsBackend::get_entities(EntityKind::DATAREADER).size() != num_readers ||
StatisticsBackend::get_entities(EntityKind::DATAREADER, monitor_id).size() != num_readers)
{
throw Error("Error: database contains unexpected DATAREADER");
}
else if (StatisticsBackend::get_entities(EntityKind::LOCATOR).size() != num_readers + num_writers ||
StatisticsBackend::get_entities(EntityKind::LOCATOR,
monitor_id).size() != num_readers + num_writers)
{
throw Error("Error: database contains unexpected LOCATOR");
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: DOMAIN with id: " + std::to_string(
entity.value()) + " is active after stopping monitor");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::TOPIC, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: TOPIC with id: " + std::to_string(
entity.value()) + " is active after stopping monitor");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::PARTICIPANT, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: PARTICIPANT with id: " + std::to_string(
entity.value()) + " is active after stopping monitor");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DATAWRITER, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: DATAWRITER with id: " + std::to_string(
entity.value()) + " is active after stopping monitor");
}
}
for (auto entity : StatisticsBackend::get_entities(EntityKind::DATAREADER, monitor_id))
{
if (StatisticsBackend::is_active(entity))
{
throw Error("Error: DATAREADER with id: " + std::to_string(
entity.value()) + " is active after stopping monitor");
}
}
}
catch (const std::exception& e)
{
std::cerr << "Stop Monitor_" << seed << "\n" << e.what() << '\n';
return 1;
}
}
/* TODO This can be activated once the clear_monitor is in place */
// Test: Clearing the monitor empties the database except physical data
// {
// try
// {
// StatisticsBackend::clear_monitor(monitor_id);
// // Check the database is empty
// if (!StatisticsBackend::get_entities(EntityKind::HOST).empty() ||
// !StatisticsBackend::get_entities(EntityKind::USER).empty() ||
// !StatisticsBackend::get_entities(EntityKind::PROCESS).empty() ||
// !StatisticsBackend::get_entities(EntityKind::DOMAIN_ENTITY).empty() ||
// !StatisticsBackend::get_entities(EntityKind::TOPIC).empty() ||
// !StatisticsBackend::get_entities(EntityKind::PARTICIPANT).empty() ||
// !StatisticsBackend::get_entities(EntityKind::DATAWRITER).empty() ||
// !StatisticsBackend::get_entities(EntityKind::DATAREADER).empty())
// {
// throw Error("Error: database contains unexpected entities");
// }
// if (StatisticsBackend::get_entities(EntityKind::LOCATOR).size() != num_endpoints ||
// StatisticsBackend::get_entities(EntityKind::LOCATOR, monitor_id).size() != num_endpoints)
// {
// throw Error("Error: database contains unexpected LOCATOR");
// }
// }
// catch (const std::exception& e)
// {
// std::cerr << "Stop Monitor_" << seed << "\n" << e.what() << '\n';
// return 1;
// }
// }
std::cerr << "Stop Monitor_" << seed << '\n';
return 0;
}