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

Commit

Permalink
fix(mysql): connections close well.
Browse files Browse the repository at this point in the history
REFS: MON-11846
  • Loading branch information
bouda1 committed May 31, 2022
1 parent c139bf6 commit c82e684
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
6 changes: 5 additions & 1 deletion core/inc/com/centreon/broker/mysql_manager.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** Copyright 2018 Centreon
** Copyright 2018-2022 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,6 +64,8 @@ CCB_BEGIN()
*
*/
class mysql_manager {
static mysql_manager* _instance;

mutable std::mutex _cfg_mutex;
std::vector<std::shared_ptr<mysql_connection>> _connection;

Expand All @@ -76,6 +78,8 @@ class mysql_manager {

public:
~mysql_manager();
static void load();
static void unload();
static mysql_manager& instance();
std::vector<std::shared_ptr<mysql_connection>> get_connections(
database_config const& db_cfg);
Expand Down
3 changes: 3 additions & 0 deletions core/src/config/applier/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "com/centreon/broker/io/protocols.hh"
#include "com/centreon/broker/log_v2.hh"
#include "com/centreon/broker/multiplexing/engine.hh"
#include "com/centreon/broker/mysql_manager.hh"
#include "com/centreon/broker/pool.hh"
#include "com/centreon/broker/time/timezone_manager.hh"

Expand All @@ -43,6 +44,7 @@ void config::applier::init(size_t n_thread, const std::string& name) {
// Load singletons.
pool::load(n_thread);
stats::center::load();
mysql_manager::load();
config::applier::state::load();
multiplexing::engine::load();
io::protocols::load();
Expand All @@ -62,6 +64,7 @@ void config::applier::deinit() {
config::applier::state::unload();
io::events::unload();
io::protocols::unload();
mysql_manager::unload();
stats::center::unload();
pool::unload();
}
Expand Down
6 changes: 2 additions & 4 deletions core/src/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::database;

std::once_flag init_flag;

/**
* Constructor.
*
Expand Down Expand Up @@ -179,8 +177,8 @@ int mysql::run_query_and_get_result(std::string const& query,
// Here, we use _current_thread
thread_id = choose_best_connection(-1);

log_v2::sql()->trace("Connection {} chosen to execute << {} >>",
thread_id, query);
log_v2::sql()->trace("Connection {} chosen to execute << {} >>", thread_id,
query);
_connection[thread_id]->run_query_and_get_result(query, promise);
return thread_id;
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/mysql_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void mysql_connection::_clear_connection() {
}
_stmt.clear();
mysql_close(_conn);
_conn = nullptr;
}

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

std::time_t now = std::time(nullptr);
if (_tasks_list.empty())
Expand Down
28 changes: 24 additions & 4 deletions core/src/mysql_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,34 @@ using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::database;

mysql_manager* mysql_manager::_instance{nullptr};

/**
* @brief The function to use to call the unique instance of mysql_manager.
*
* @return A reference to the mysql_manager object.
*/
mysql_manager& mysql_manager::instance() {
static mysql_manager _singleton;
return _singleton;
assert(_instance);
return *_instance;
}

/**
* @brief Load the instance of mysql_manager
*/
void mysql_manager::load() {
if (!_instance)
_instance = new mysql_manager();
}

/**
* @brief Unload the instance of mysql_manager
*/
void mysql_manager::unload() {
if (_instance) {
delete _instance;
_instance = nullptr;
}
}

/**
Expand Down Expand Up @@ -87,7 +107,7 @@ std::vector<std::shared_ptr<mysql_connection>> mysql_manager::get_connections(
{
uint32_t current_connection{0};
std::lock_guard<std::mutex> lock(_cfg_mutex);
for (std::shared_ptr<mysql_connection> c : _connection) {
for (auto c : _connection) {
// Is this thread matching what the configuration needs?
if (c->match_config(db_cfg) && !c->is_finished() &&
!c->is_finish_asked() && !c->is_in_error()) {
Expand Down Expand Up @@ -123,7 +143,7 @@ void mysql_manager::clear() {
conn->finish();
} catch (const std::exception& e) {
log_v2::sql()->info("mysql_manager: Unable to stop a connection: {}",
e.what());
e.what());
}
}
log_v2::sql()->debug("mysql_manager: clear finished");
Expand Down
10 changes: 7 additions & 3 deletions stats/test/stats.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 - 2021 Centreon (https://www.centreon.com/)
* Copyright 2019 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,7 @@
#include "com/centreon/broker/misc/misc.hh"
#include "com/centreon/broker/misc/string.hh"
#include "com/centreon/broker/multiplexing/engine.hh"
#include "com/centreon/broker/mysql_manager.hh"
#include "com/centreon/broker/pool.hh"
#include "com/centreon/broker/stats/builder.hh"
#include "com/centreon/broker/stats/center.hh"
Expand All @@ -49,19 +50,22 @@ class StatsTest : public ::testing::Test {
void SetUp() override {
pool::load(0);
stats::center::load();
io::protocols::load();
io::events::load();
mysql_manager::load();
config::applier::state::load();
multiplexing::engine::load();
io::protocols::load();
io::events::load();
config::applier::endpoint::load();
}

void TearDown() override {
config::applier::endpoint::unload();
multiplexing::engine::instance().clear();
multiplexing::engine::unload();
config::applier::state::unload();
io::events::unload();
io::protocols::unload();
mysql_manager::unload();
stats::center::unload();
pool::unload();
}
Expand Down

0 comments on commit c82e684

Please sign in to comment.