Skip to content

Commit 8796a1e

Browse files
author
jparisu
committed
Refs #15841: apply suggestions
Signed-off-by: jparisu <[email protected]>
1 parent aacf5cc commit 8796a1e

File tree

7 files changed

+135
-189
lines changed

7 files changed

+135
-189
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ jobs:
257257
uses: ./src/Fast-DDS-statistics-backend/.github/actions/install-python-packages
258258

259259
- name: Fetch eProsima dependencies
260-
uses: ./src/Fast-DDS-statistics-backend/.github/actions/fetch-fastdds-repos
260+
run: |
261+
vcs import src < ./src/Fast-DDS-statistics-backend/.github/workflows/ci.repos
261262
262263
- name: Update colcon mixin
263264
run: |

src/cpp/StatisticsBackend.cpp

Lines changed: 39 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "StatisticsBackendData.hpp"
5555
#include "detail/data_getters.hpp"
5656
#include "detail/data_aggregation.hpp"
57+
#include "detail/ScopeExit.hpp"
5758

5859
using namespace eprosima::fastdds::dds;
5960
using namespace eprosima::fastdds::rtps;
@@ -187,20 +188,15 @@ EntityId create_and_register_monitor(
187188
// What should happen is that all this logic is moved to StatisticsBackendData. You know, some day...
188189

189190
details::StatisticsBackendData::get_instance()->lock();
191+
MAKE_UNNAMED_SCOPE_EXIT(details::StatisticsBackendData::get_instance()->unlock());
190192

191193
// Create monitor instance.
192194
std::shared_ptr<details::Monitor> monitor = std::make_shared<details::Monitor>();
193195
std::shared_ptr<database::Domain> domain = std::make_shared<database::Domain>(domain_name);
194196

195-
try
196-
{
197-
domain->id = details::StatisticsBackendData::get_instance()->database_->insert(domain);
198-
}
199-
catch (const std::exception&)
200-
{
201-
details::StatisticsBackendData::get_instance()->unlock();
202-
throw;
203-
}
197+
// Throw exception in fail case
198+
domain->id = details::StatisticsBackendData::get_instance()->database_->insert(domain);
199+
204200
// TODO: in case this function fails afterwards, the domain will be kept in the database without associated
205201
// Participant. There must exist a way in database to delete a domain, or to make a rollback.
206202

@@ -209,14 +205,19 @@ EntityId create_and_register_monitor(
209205
monitor->domain_callback_mask = callback_mask;
210206
monitor->data_mask = data_mask;
211207
details::StatisticsBackendData::get_instance()->monitors_by_entity_[domain->id] = monitor;
208+
auto se_erase_monitor_database_ =
209+
MAKE_SCOPE_EXIT(details::StatisticsBackendData::get_instance()->monitors_by_entity_.erase(domain->id));
212210

213211
monitor->participant_listener = new subscriber::StatisticsParticipantListener(
214212
domain->id,
215213
details::StatisticsBackendData::get_instance()->database_.get(),
216214
details::StatisticsBackendData::get_instance()->entity_queue_,
217215
details::StatisticsBackendData::get_instance()->data_queue_);
216+
auto se_participant_listener_ = MAKE_SCOPE_EXIT(delete monitor->participant_listener);
217+
218218
monitor->reader_listener = new subscriber::StatisticsReaderListener(
219219
details::StatisticsBackendData::get_instance()->data_queue_);
220+
auto se_reader_listener_ = MAKE_SCOPE_EXIT(delete monitor->reader_listener);
220221

221222
/* Create DomainParticipant */
222223
StatusMask participant_mask = StatusMask::all();
@@ -229,14 +230,9 @@ EntityId create_and_register_monitor(
229230

230231
if (monitor->participant == nullptr)
231232
{
232-
// Remove those elements that have been set
233-
delete monitor->reader_listener;
234-
delete monitor->participant_listener;
235-
details::StatisticsBackendData::get_instance()->monitors_by_entity_.erase(domain->id);
236-
237-
details::StatisticsBackendData::get_instance()->unlock();
238233
throw Error("Error initializing monitor. Could not create participant");
239234
}
235+
auto se_participant_ = MAKE_SCOPE_EXIT(DomainParticipantFactory::get_instance()->delete_participant(monitor->participant));
240236

241237
/* Create Subscriber */
242238
monitor->subscriber = monitor->participant->create_subscriber(
@@ -246,15 +242,29 @@ EntityId create_and_register_monitor(
246242

247243
if (monitor->subscriber == nullptr)
248244
{
249-
// Remove those elements that have been set
250-
DomainParticipantFactory::get_instance()->delete_participant(monitor->participant);
251-
delete monitor->reader_listener;
252-
delete monitor->participant_listener;
253-
details::StatisticsBackendData::get_instance()->monitors_by_entity_.erase(domain->id);
254-
255-
details::StatisticsBackendData::get_instance()->unlock();
256245
throw Error("Error initializing monitor. Could not create subscriber");
257246
}
247+
auto se_subscriber_ = MAKE_SCOPE_EXIT(monitor->participant->delete_subscriber(monitor->subscriber));
248+
249+
auto se_topics_datareaders_ =
250+
MAKE_SCOPE_EXIT(
251+
{
252+
for (auto& it : monitor->readers)
253+
{
254+
if (nullptr != it.second)
255+
{
256+
monitor->subscriber->delete_datareader(it.second);
257+
}
258+
}
259+
for (auto& it : monitor->topics)
260+
{
261+
if (nullptr != it.second)
262+
{
263+
monitor->participant->delete_topic(it.second);
264+
}
265+
}
266+
}
267+
);
258268

259269
for (const auto& topic : topics)
260270
{
@@ -265,28 +275,6 @@ EntityId create_and_register_monitor(
265275
}
266276
catch (const std::exception& e)
267277
{
268-
// Remove those elements that have been set
269-
for (auto& it : monitor->readers)
270-
{
271-
if (nullptr != it.second)
272-
{
273-
monitor->subscriber->delete_datareader(it.second);
274-
}
275-
}
276-
for (auto& it : monitor->topics)
277-
{
278-
if (nullptr != it.second)
279-
{
280-
monitor->participant->delete_topic(it.second);
281-
}
282-
}
283-
monitor->participant->delete_subscriber(monitor->subscriber);
284-
DomainParticipantFactory::get_instance()->delete_participant(monitor->participant);
285-
delete monitor->reader_listener;
286-
delete monitor->participant_listener;
287-
details::StatisticsBackendData::get_instance()->monitors_by_entity_.erase(domain->id);
288-
289-
details::StatisticsBackendData::get_instance()->unlock();
290278
throw Error("Error registering topic " + std::string(topic) + " : " + e.what());
291279
}
292280

@@ -299,33 +287,17 @@ EntityId create_and_register_monitor(
299287

300288
if (monitor->readers[topic] == nullptr)
301289
{
302-
// Remove those elements that have been set
303-
for (auto& it : monitor->readers)
304-
{
305-
if (nullptr != it.second)
306-
{
307-
monitor->subscriber->delete_datareader(it.second);
308-
}
309-
}
310-
for (auto& it : monitor->topics)
311-
{
312-
if (nullptr != it.second)
313-
{
314-
monitor->participant->delete_topic(it.second);
315-
}
316-
}
317-
monitor->participant->delete_subscriber(monitor->subscriber);
318-
DomainParticipantFactory::get_instance()->delete_participant(monitor->participant);
319-
delete monitor->reader_listener;
320-
delete monitor->participant_listener;
321-
details::StatisticsBackendData::get_instance()->monitors_by_entity_.erase(domain->id);
322-
323-
details::StatisticsBackendData::get_instance()->unlock();
324290
throw Error("Error initializing monitor. Could not create reader for topic " + std::string(topic));
325291
}
326292
}
327293

328-
details::StatisticsBackendData::get_instance()->unlock();
294+
se_erase_monitor_database_.cancel();
295+
se_participant_listener_.cancel();
296+
se_reader_listener_.cancel();
297+
se_participant_.cancel();
298+
se_subscriber_.cancel();
299+
se_topics_datareaders_.cancel();
300+
329301
return domain->id;
330302
}
331303

src/cpp/StatisticsBackendData.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @file StatisticsBackendData.cpp
1717
*/
1818

19+
#include "StatisticsBackendData.hpp"
1920

2021
#include <map>
2122
#include <string>
@@ -31,8 +32,6 @@
3132
#include <fastdds_statistics_backend/listener/DomainListener.hpp>
3233
#include <fastdds_statistics_backend/listener/PhysicalListener.hpp>
3334

34-
#include "StatisticsBackendData.hpp"
35-
3635
#include "Monitor.hpp"
3736
#include <database/database_queue.hpp>
3837
#include <database/database.hpp>
@@ -52,10 +51,7 @@ StatisticsBackendData::StatisticsBackendData()
5251
, lock_(mutex_, std::defer_lock)
5352
, participant_factory_instance_(eprosima::fastdds::dds::DomainParticipantFactory::get_shared_instance())
5453
{
55-
// Set in DomainParticipantFactory that entities are created disabled
56-
eprosima::fastdds::dds::DomainParticipantFactoryQos qos;
57-
participant_factory_instance_->get_qos(qos);
58-
qos.entity_factory().autoenable_created_entities = false;
54+
// Do nothing
5955
}
6056

6157
StatisticsBackendData::~StatisticsBackendData()

src/cpp/detail/ScopeExit.hpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2015-2020 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
16+
//
17+
// Licensed under the Apache License, Version 2.0 (the "License");
18+
// you may not use this file except in compliance with the License.
19+
// You may obtain a copy of the License at
20+
//
21+
// http://www.apache.org/licenses/LICENSE-2.0
22+
//
23+
// Unless required by applicable law or agreed to in writing, software
24+
// distributed under the License is distributed on an "AS IS" BASIS,
25+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26+
// See the License for the specific language governing permissions and
27+
// limitations under the License.
28+
29+
#ifndef _EPROSIMA_FASTDDS_STATISTICS_BACKEND_DETAIL_SCOPE_EXIT_HPP_
30+
#define _EPROSIMA_FASTDDS_STATISTICS_BACKEND_DETAIL_SCOPE_EXIT_HPP_
31+
32+
#include <utility>
33+
34+
namespace eprosima {
35+
namespace statistics_backend {
36+
namespace details {
37+
38+
template<typename CallableT>
39+
class ScopeExit final
40+
{
41+
public:
42+
explicit ScopeExit(CallableT && callable)
43+
: callable_(std::forward<CallableT>(callable))
44+
{
45+
}
46+
47+
ScopeExit(const ScopeExit &) = delete;
48+
ScopeExit(ScopeExit &&) = default;
49+
50+
ScopeExit & operator=(const ScopeExit &) = delete;
51+
ScopeExit & operator=(ScopeExit &&) = default;
52+
53+
~ScopeExit()
54+
{
55+
if (!cancelled_) {
56+
callable_();
57+
}
58+
}
59+
60+
void cancel()
61+
{
62+
cancelled_ = true;
63+
}
64+
65+
private:
66+
CallableT callable_;
67+
bool cancelled_{false};
68+
};
69+
70+
template<typename CallableT>
71+
ScopeExit<CallableT>
72+
make_scope_exit(CallableT && callable)
73+
{
74+
return ScopeExit<CallableT>(std::forward<CallableT>(callable));
75+
}
76+
77+
} // namespace details
78+
} // namespace statistics_backend
79+
} // namespace eprosima
80+
81+
#define MAKE_SCOPE_EXIT(code) \
82+
eprosima::statistics_backend::details::make_scope_exit([&]() {code;})
83+
84+
#define JOIN_IMPL(arg1, arg2) arg1 ## arg2
85+
#define MAKE_UNNAMED_SCOPE_EXIT(code) \
86+
auto JOIN_IMPL(scope_exit_, __LINE__) = eprosima::statistics_backend::details::make_scope_exit([&]() {code;})
87+
88+
#endif // _EPROSIMA_FASTDDS_STATISTICS_BACKEND_DETAIL_SCOPE_EXIT_HPP_

test/mock/dds/DomainParticipantFactory/fastdds/dds/domain/DomainParticipantFactory_mock.hpp

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)