Skip to content

Commit 6309ffb

Browse files
committed
[#25493] Fix DiscoveryDatabase endpoint cleanup for inactive updates and shutdown
Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent 8eaf047 commit 6309ffb

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

ddspipe_core/include/ddspipe_core/dynamic/DiscoveryDatabase.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <functional>
2020
#include <map>
2121
#include <mutex>
22+
#include <set>
2223
#include <shared_mutex>
2324
#include <string>
2425
#include <thread>
@@ -263,7 +264,7 @@ class DiscoveryDatabase
263264
//! Database of endpoints indexed by guid
264265
std::map<types::Guid, types::Endpoint> entities_;
265266

266-
//! Database of endpoints indexed by guid
267+
//! Database of filtered endpoint GUIDs
267268
std::set<types::Guid> entities_filter_;
268269

269270
//! Mutex to guard queries to the database

ddspipe_core/src/cpp/dynamic/DiscoveryDatabase.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ void DiscoveryDatabase::stop() noexcept
7676
{
7777
logDebug(DDSPIPE_DISCOVERY_DATABASE, "Processing thread routine already stopped.");
7878
}
79+
80+
{
81+
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
82+
entities_.clear();
83+
entities_filter_.clear();
84+
}
7985
}
8086

8187
bool DiscoveryDatabase::topic_exists(
@@ -107,6 +113,7 @@ bool DiscoveryDatabase::add_endpoint_(
107113
{
108114
{
109115
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
116+
entities_filter_.erase(new_endpoint.guid);
110117

111118
auto it = entities_.find(new_endpoint.guid);
112119
if (it != entities_.end())
@@ -151,8 +158,14 @@ bool DiscoveryDatabase::add_endpoint_(
151158
bool DiscoveryDatabase::update_endpoint_(
152159
const Endpoint& endpoint_to_update)
153160
{
161+
if (!endpoint_to_update.active)
162+
{
163+
return erase_endpoint_(endpoint_to_update) == utils::ReturnCode::RETCODE_OK;
164+
}
165+
154166
{
155167
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
168+
entities_filter_.erase(endpoint_to_update.guid);
156169

157170
auto it = entities_.find(endpoint_to_update.guid);
158171
if (it == entities_.end())
@@ -185,14 +198,18 @@ bool DiscoveryDatabase::update_endpoint_(
185198
utils::ReturnCode DiscoveryDatabase::erase_endpoint_(
186199
const Endpoint& endpoint_to_erase)
187200
{
201+
bool endpoint_erased = false;
202+
188203
{
189204
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
190205

191206
EPROSIMA_LOG_INFO(DDSPIPE_DISCOVERY_DATABASE, "Erasing Endpoint " << endpoint_to_erase << ".");
192207

193208
auto erased = entities_.erase(endpoint_to_erase.guid);
209+
auto filtered_erased = entities_filter_.erase(endpoint_to_erase.guid);
210+
endpoint_erased = erased > 0;
194211

195-
if (erased == 0)
212+
if (erased == 0 && filtered_erased == 0)
196213
{
197214
throw utils::InconsistencyException(
198215
utils::Formatter() <<
@@ -201,10 +218,13 @@ utils::ReturnCode DiscoveryDatabase::erase_endpoint_(
201218
}
202219
}
203220

204-
std::lock_guard<std::mutex> lock(callbacks_mutex_);
205-
for (auto erased_endpoint_callback : erased_endpoint_callbacks_)
221+
if (endpoint_erased)
206222
{
207-
erased_endpoint_callback(endpoint_to_erase);
223+
std::lock_guard<std::mutex> lock(callbacks_mutex_);
224+
for (auto erased_endpoint_callback : erased_endpoint_callbacks_)
225+
{
226+
erased_endpoint_callback(endpoint_to_erase);
227+
}
208228
}
209229

210230
return utils::ReturnCode::RETCODE_OK;
@@ -213,6 +233,7 @@ utils::ReturnCode DiscoveryDatabase::erase_endpoint_(
213233
void DiscoveryDatabase::add_filtered_endpoint(
214234
const types::Guid guid)
215235
{
236+
std::unique_lock<std::shared_timed_mutex> lock(mutex_);
216237
entities_filter_.insert(guid);
217238
}
218239

@@ -237,6 +258,7 @@ void DiscoveryDatabase::erase_endpoint(
237258
bool DiscoveryDatabase::exists_filtered_endpoint(
238259
const Guid endpoint_guid)
239260
{
261+
std::shared_lock<std::shared_timed_mutex> lock(mutex_);
240262
return entities_filter_.find(endpoint_guid) != entities_filter_.end();
241263
}
242264

0 commit comments

Comments
 (0)