Commit e3ae3afc authored by Tien Thinh NGUYEN's avatar Tien Thinh NGUYEN

Add DB Connection Manager

parent e5d5d7a6
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#define HEART_BEAT_TIMER 10 #define HEART_BEAT_TIMER 10
#define DB_CONNECTION_TIMER 1000
#define _unused(x) ((void) (x)) #define _unused(x) ((void) (x))
#define NNRF_NFM_BASE "/nnrf-nfm/" #define NNRF_NFM_BASE "/nnrf-nfm/"
...@@ -78,7 +80,7 @@ typedef enum db_type_s { ...@@ -78,7 +80,7 @@ typedef enum db_type_s {
DB_TYPE_CASSANDRA = 2 DB_TYPE_CASSANDRA = 2
} db_type_t; } db_type_t;
static const std::vector<std::string> db_type_e2str = {"Unknown", "MySQL", static const std::vector<std::string> db_type_e2str = {
"Cassandra"}; "Unknown", "MySQL", "Cassandra"};
#endif #endif
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (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.openairinterface.org/?page_id=698
*
* 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.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file db_connection_manager.cpp
\brief
\author Tien-Thinh NGUYEN
\company Eurecom
\date 2022
\email:
*/
#include "db_connection_manager.hpp"
#include "logger.hpp"
#include "udr.h"
#include "udr_app.hpp"
using namespace oai::udr::config;
using namespace oai::udr::app;
using json = nlohmann::json;
extern udr_config udr_cfg;
//------------------------------------------------------------------------------
db_connection_manager::db_connection_manager(udr_event& ev) : m_event_sub(ev) {}
//---------------------------------------------------------------------------------------------
void db_connection_manager::start_event_connection_initialization(
std::shared_ptr<database_wrapper_abstraction>& db_connector) {
// get current time
uint64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
struct itimerspec its;
its.it_value.tv_sec = DB_CONNECTION_TIMER; // seconds
its.it_value.tv_nsec = 0; // 100 * 1000 * 1000; //100ms
const uint64_t interval =
its.it_value.tv_sec * 1000 +
its.it_value.tv_nsec / 1000000; // convert sec, nsec to msec
db_connection = m_event_sub.subscribe_task_db_connection_reset(
boost::bind(&udr_nrf::trigger_connection_reset_procedure, this, _1, _2),
db_connector, interval, ms + interval);
}
//---------------------------------------------------------------------------------------------
void db_connection_manager::trigger_connection_reset_procedure(
std::shared_ptr<database_wrapper_abstraction>& db_connector, uint64_t ms) {
_unused(ms);
// Close the current connection
db_connector.close_connection();
// and start a new one
db_connector.initialize();
}
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (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.openairinterface.org/?page_id=698
*
* 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.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
/*! \file db_connection_manager.hpp
\author Tien-Thinh NGUYEN
\company Eurecom
\date 2022
\email:
*/
#ifndef FILE_DB_CONNECTION_MANAGER_SEEN
#define FILE_DB_CONNECTION_MANAGER_SEEN
#include "database_wrapper_abstraction.hpp"
#include "udr_event.hpp"
namespace oai {
namespace udr {
namespace app {
class db_connection_manager {
private:
public:
db_connection_manager(udr_event& ev);
db_connection_manager(db_connection_manager const&) = delete;
void operator=(db_connection_manager const&) = delete;
/*
* Start event connection initialization procedure
* @param [void]
* @return void
*/
void start_event_connection_initialization(
std::shared_ptr<database_wrapper_abstraction>& db_connector);
/*
* Trigger connection reset procedure (NF Heartbeat)
* @param [void]
* @return void
*/
void trigger_connection_reset_procedure(
uint64_t ms, std::shared_ptr<database_wrapper_abstraction>& db_connector);
private:
udr_event& m_event_sub;
bs2::connection task_connection;
bs2::connection db_connection;
};
} // namespace app
} // namespace udr
} // namespace oai
#endif /* FILE_DB_CONNECTION_MANAGER_SEEN */
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "mysql_db.hpp" #include "mysql_db.hpp"
#include "udr_config.hpp" #include "udr_config.hpp"
#include "udr_nrf.hpp" #include "udr_nrf.hpp"
#include "db_connection_manager.hpp"
using namespace oai::udr::app; using namespace oai::udr::app;
using namespace oai::udr::model; using namespace oai::udr::model;
...@@ -44,7 +45,8 @@ using namespace oai::udr::config; ...@@ -44,7 +45,8 @@ using namespace oai::udr::config;
extern udr_app* udr_app_inst; extern udr_app* udr_app_inst;
extern udr_config udr_cfg; extern udr_config udr_cfg;
udr_nrf* udr_nrf_inst = nullptr; udr_nrf* udr_nrf_inst = nullptr;
db_connection_manager* db_connection_manager_inst = nullptr;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
udr_app::udr_app(const std::string& config_file, udr_event& ev) udr_app::udr_app(const std::string& config_file, udr_event& ev)
...@@ -73,12 +75,23 @@ udr_app::udr_app(const std::string& config_file, udr_event& ev) ...@@ -73,12 +75,23 @@ udr_app::udr_app(const std::string& config_file, udr_event& ev)
throw; throw;
} }
} }
// DB Connection Manager
try {
db_connection_manager_inst = new db_connection_manager(ev);
Logger::udr_app().info("DB Connection Task Created ");
} catch (std::exception& e) {
Logger::udr_app().error("Cannot create DB Connection Task: %s", e.what());
throw;
}
Logger::udr_app().startup("Started"); Logger::udr_app().startup("Started");
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
udr_app::~udr_app() { udr_app::~udr_app() {
Logger::udr_app().debug("Delete UDR APP instance..."); Logger::udr_app().debug("Delete UDR APP instance...");
// Close DB connection
db_connector->close_connection();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
......
...@@ -42,3 +42,13 @@ bs2::connection udr_event::subscribe_task_nf_heartbeat( ...@@ -42,3 +42,13 @@ bs2::connection udr_event::subscribe_task_nf_heartbeat(
}; };
return task_tick.connect(f); return task_tick.connect(f);
} }
bs2::connection udr_event::subscribe_task_db_connection_reset(
const db_connection_sig_t::slot_type& sig,
std::shared_ptr<database_wrapper_abstraction>& db_connector,
uint64_t period, uint64_t start) {
auto f = [db_connector, period, start, sig](uint64_t t) {
if (t >= start && (t - start) % period == 0) sig(db_connector, t);
};
return db_connection_sig.connect(f);
}
...@@ -66,8 +66,20 @@ class udr_event { ...@@ -66,8 +66,20 @@ class udr_event {
bs2::connection subscribe_task_nf_heartbeat( bs2::connection subscribe_task_nf_heartbeat(
const task_sig_t::slot_type& sig, uint64_t period, uint64_t start = 0); const task_sig_t::slot_type& sig, uint64_t period, uint64_t start = 0);
/*
* Subscribe to the task db connection reset event
* @param [const db_connection_sig_t::slot_type &] sig
* @param [uint64_t] period: interval between two events
* @param [uint64_t] start:
* @return void
*/
bs2::connection subscribe_task_db_connection_reset(
const db_connection_sig_t::slot_type& sig, uint64_t period,
uint64_t start = 0);
private: private:
task_sig_t task_tick; task_sig_t task_tick;
db_connection_sig_t db_connection_sig;
}; };
} // namespace oai::udr::app } // namespace oai::udr::app
#endif #endif
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include <string> #include <string>
#include "database_wrapper_abstraction.hpp"
namespace bs2 = boost::signals2; namespace bs2 = boost::signals2;
namespace oai::udr::app { namespace oai::udr::app {
...@@ -41,5 +43,9 @@ typedef bs2::signal_type< ...@@ -41,5 +43,9 @@ typedef bs2::signal_type<
void(uint64_t), bs2::keywords::mutex_type<bs2::dummy_mutex>>::type void(uint64_t), bs2::keywords::mutex_type<bs2::dummy_mutex>>::type
task_sig_t; task_sig_t;
typedef bs2::signal_type<
void(uint64_t, std::shared_ptr<database_wrapper_abstraction>&),
bs2::keywords::mutex_type<bs2::dummy_mutex>>::type db_connection_sig_t;
} // namespace oai::udr::app } // namespace oai::udr::app
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment