Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit c82e684

Browse files
committed
fix(mysql): connections close well.
REFS: MON-11846
1 parent c139bf6 commit c82e684

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

core/inc/com/centreon/broker/mysql_manager.hh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** Copyright 2018 Centreon
2+
** Copyright 2018-2022 Centreon
33
**
44
** Licensed under the Apache License, Version 2.0 (the "License");
55
** you may not use this file except in compliance with the License.
@@ -64,6 +64,8 @@ CCB_BEGIN()
6464
*
6565
*/
6666
class mysql_manager {
67+
static mysql_manager* _instance;
68+
6769
mutable std::mutex _cfg_mutex;
6870
std::vector<std::shared_ptr<mysql_connection>> _connection;
6971

@@ -76,6 +78,8 @@ class mysql_manager {
7678

7779
public:
7880
~mysql_manager();
81+
static void load();
82+
static void unload();
7983
static mysql_manager& instance();
8084
std::vector<std::shared_ptr<mysql_connection>> get_connections(
8185
database_config const& db_cfg);

core/src/config/applier/init.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "com/centreon/broker/io/protocols.hh"
2626
#include "com/centreon/broker/log_v2.hh"
2727
#include "com/centreon/broker/multiplexing/engine.hh"
28+
#include "com/centreon/broker/mysql_manager.hh"
2829
#include "com/centreon/broker/pool.hh"
2930
#include "com/centreon/broker/time/timezone_manager.hh"
3031

@@ -43,6 +44,7 @@ void config::applier::init(size_t n_thread, const std::string& name) {
4344
// Load singletons.
4445
pool::load(n_thread);
4546
stats::center::load();
47+
mysql_manager::load();
4648
config::applier::state::load();
4749
multiplexing::engine::load();
4850
io::protocols::load();
@@ -62,6 +64,7 @@ void config::applier::deinit() {
6264
config::applier::state::unload();
6365
io::events::unload();
6466
io::protocols::unload();
67+
mysql_manager::unload();
6568
stats::center::unload();
6669
pool::unload();
6770
}

core/src/mysql.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ using namespace com::centreon::exceptions;
2424
using namespace com::centreon::broker;
2525
using namespace com::centreon::broker::database;
2626

27-
std::once_flag init_flag;
28-
2927
/**
3028
* Constructor.
3129
*
@@ -179,8 +177,8 @@ int mysql::run_query_and_get_result(std::string const& query,
179177
// Here, we use _current_thread
180178
thread_id = choose_best_connection(-1);
181179

182-
log_v2::sql()->trace("Connection {} chosen to execute << {} >>",
183-
thread_id, query);
180+
log_v2::sql()->trace("Connection {} chosen to execute << {} >>", thread_id,
181+
query);
184182
_connection[thread_id]->run_query_and_get_result(query, promise);
185183
return thread_id;
186184
}

core/src/mysql_connection.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void mysql_connection::_clear_connection() {
101101
}
102102
_stmt.clear();
103103
mysql_close(_conn);
104+
_conn = nullptr;
104105
}
105106

106107
/**
@@ -616,7 +617,8 @@ void mysql_connection::_run() {
616617
} else {
617618
/* We are waiting for some activity, nothing to do for now it is time
618619
* to make some ping */
619-
_tasks_condition.wait(lock, [this] { return _finish_asked || !_tasks_list.empty(); });
620+
_tasks_condition.wait(
621+
lock, [this] { return _finish_asked || !_tasks_list.empty(); });
620622

621623
std::time_t now = std::time(nullptr);
622624
if (_tasks_list.empty())

core/src/mysql_manager.cc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,34 @@ using namespace com::centreon::exceptions;
2727
using namespace com::centreon::broker;
2828
using namespace com::centreon::broker::database;
2929

30+
mysql_manager* mysql_manager::_instance{nullptr};
31+
3032
/**
3133
* @brief The function to use to call the unique instance of mysql_manager.
3234
*
3335
* @return A reference to the mysql_manager object.
3436
*/
3537
mysql_manager& mysql_manager::instance() {
36-
static mysql_manager _singleton;
37-
return _singleton;
38+
assert(_instance);
39+
return *_instance;
40+
}
41+
42+
/**
43+
* @brief Load the instance of mysql_manager
44+
*/
45+
void mysql_manager::load() {
46+
if (!_instance)
47+
_instance = new mysql_manager();
48+
}
49+
50+
/**
51+
* @brief Unload the instance of mysql_manager
52+
*/
53+
void mysql_manager::unload() {
54+
if (_instance) {
55+
delete _instance;
56+
_instance = nullptr;
57+
}
3858
}
3959

4060
/**
@@ -87,7 +107,7 @@ std::vector<std::shared_ptr<mysql_connection>> mysql_manager::get_connections(
87107
{
88108
uint32_t current_connection{0};
89109
std::lock_guard<std::mutex> lock(_cfg_mutex);
90-
for (std::shared_ptr<mysql_connection> c : _connection) {
110+
for (auto c : _connection) {
91111
// Is this thread matching what the configuration needs?
92112
if (c->match_config(db_cfg) && !c->is_finished() &&
93113
!c->is_finish_asked() && !c->is_in_error()) {
@@ -123,7 +143,7 @@ void mysql_manager::clear() {
123143
conn->finish();
124144
} catch (const std::exception& e) {
125145
log_v2::sql()->info("mysql_manager: Unable to stop a connection: {}",
126-
e.what());
146+
e.what());
127147
}
128148
}
129149
log_v2::sql()->debug("mysql_manager: clear finished");

stats/test/stats.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 - 2021 Centreon (https://www.centreon.com/)
2+
* Copyright 2019 - 2022 Centreon (https://www.centreon.com/)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
#include "com/centreon/broker/misc/misc.hh"
3737
#include "com/centreon/broker/misc/string.hh"
3838
#include "com/centreon/broker/multiplexing/engine.hh"
39+
#include "com/centreon/broker/mysql_manager.hh"
3940
#include "com/centreon/broker/pool.hh"
4041
#include "com/centreon/broker/stats/builder.hh"
4142
#include "com/centreon/broker/stats/center.hh"
@@ -49,19 +50,22 @@ class StatsTest : public ::testing::Test {
4950
void SetUp() override {
5051
pool::load(0);
5152
stats::center::load();
52-
io::protocols::load();
53-
io::events::load();
53+
mysql_manager::load();
5454
config::applier::state::load();
5555
multiplexing::engine::load();
56+
io::protocols::load();
57+
io::events::load();
5658
config::applier::endpoint::load();
5759
}
5860

5961
void TearDown() override {
6062
config::applier::endpoint::unload();
63+
multiplexing::engine::instance().clear();
6164
multiplexing::engine::unload();
6265
config::applier::state::unload();
6366
io::events::unload();
6467
io::protocols::unload();
68+
mysql_manager::unload();
6569
stats::center::unload();
6670
pool::unload();
6771
}

0 commit comments

Comments
 (0)