From: Andrei Pavel Date: Tue, 13 Feb 2024 17:17:57 +0000 (+0200) Subject: [#3025] add db init on startup to other managers X-Git-Tag: Kea-2.5.6~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7d471912e61ee290f930d8685fc80503ea69320;p=thirdparty%2Fkea.git [#3025] add db init on startup to other managers --- diff --git a/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc b/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc index f845f27519..1f4b0a38d3 100644 --- a/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc +++ b/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc @@ -55,7 +55,7 @@ MySqlConfigBackendImpl(const std::string& space, const DbCallback db_reconnect_callback) : conn_(parameters, IOServiceAccessorPtr(new IOServiceAccessor(&MySqlConfigBackendImpl::getIOService)), - db_reconnect_callback), timer_name_(""), + db_reconnect_callback), audit_revision_ref_count_(0), parameters_(parameters) { // Create unique timer name per instance. @@ -65,35 +65,11 @@ MySqlConfigBackendImpl(const std::string& space, timer_name_ += boost::lexical_cast(reinterpret_cast(this)); timer_name_ += "]DbReconnectTimer"; + MySqlConnection::ensureSchemaVersion(parameters, db_reconnect_callback, timer_name_); + // Create ReconnectCtl for this connection. conn_.makeReconnectCtl(timer_name_); - // Test schema version first. - std::pair code_version(MYSQL_SCHEMA_VERSION_MAJOR, - MYSQL_SCHEMA_VERSION_MINOR); - - std::string timer_name; - bool retry = false; - if (parameters.count("retry-on-startup")) { - if (parameters.at("retry-on-startup") == "true") { - retry = true; - } - } - if (retry) { - timer_name = timer_name_; - } - - IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); - - std::pair db_version = - MySqlConnection::getVersion(parameters, ac, db_reconnect_callback, timer_name); - if (code_version != db_version) { - isc_throw(DbOpenError, "MySQL schema version mismatch: need version: " - << code_version.first << "." << code_version.second - << " found version: " << db_version.first << "." - << db_version.second); - } - // Open the database. conn_.openDatabase(); diff --git a/src/hooks/dhcp/pgsql_cb/pgsql_cb_impl.cc b/src/hooks/dhcp/pgsql_cb/pgsql_cb_impl.cc index aba64dbac8..07f4e29294 100644 --- a/src/hooks/dhcp/pgsql_cb/pgsql_cb_impl.cc +++ b/src/hooks/dhcp/pgsql_cb/pgsql_cb_impl.cc @@ -77,19 +77,9 @@ PgSqlConfigBackendImpl::PgSqlConfigBackendImpl(const std::string& space, size_t last_insert_id_index) : conn_(parameters, IOServiceAccessorPtr(new IOServiceAccessor(&PgSqlConfigBackendImpl::getIOService)), - db_reconnect_callback), timer_name_(""), - audit_revision_ref_count_(0), parameters_(parameters), - last_insert_id_index_(last_insert_id_index) { - - // Create unique timer name per instance. - timer_name_ = "PgSqlConfigBackend"; - timer_name_ += space; - timer_name_ += "["; - timer_name_ += boost::lexical_cast(reinterpret_cast(this)); - timer_name_ += "]DbReconnectTimer"; - - // Create ReconnectCtl for this connection. - conn_.makeReconnectCtl(timer_name_); + db_reconnect_callback), + audit_revision_ref_count_(0), parameters_(parameters), + last_insert_id_index_(last_insert_id_index) { // Check TLS support. size_t tls(0); @@ -113,31 +103,17 @@ PgSqlConfigBackendImpl::PgSqlConfigBackendImpl(const std::string& space, } #endif - // Test schema version first. - std::pair code_version(PGSQL_SCHEMA_VERSION_MAJOR, - PGSQL_SCHEMA_VERSION_MINOR); - - std::string timer_name; - bool retry = false; - if (parameters.count("retry-on-startup")) { - if (parameters.at("retry-on-startup") == "true") { - retry = true; - } - } - if (retry) { - timer_name = timer_name_; - } + // Create unique timer name per instance. + timer_name_ = "PgSqlConfigBackend"; + timer_name_ += space; + timer_name_ += "["; + timer_name_ += boost::lexical_cast(reinterpret_cast(this)); + timer_name_ += "]DbReconnectTimer"; - IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); + PgSqlConnection::ensureSchemaVersion(parameters, db_reconnect_callback, timer_name_); - std::pair db_version = - PgSqlConnection::getVersion(parameters, ac, db_reconnect_callback, timer_name); - if (code_version != db_version) { - isc_throw(DbOpenError, "PostgreSQL schema version mismatch: need version: " - << code_version.first << "." << code_version.second - << " found version: " << db_version.first << "." - << db_version.second); - } + // Create ReconnectCtl for this connection. + conn_.makeReconnectCtl(timer_name_); // Open the database. conn_.openDatabase(); diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index 7a6bd82973..9208b9b361 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -2873,37 +2873,16 @@ MySqlHostDataSource::MySqlHostContextAlloc::~MySqlHostContextAlloc() { } MySqlHostDataSourceImpl::MySqlHostDataSourceImpl(const DatabaseConnection::ParameterMap& parameters) - : parameters_(parameters), ip_reservations_unique_(true), unusable_(false), - timer_name_("") { + : parameters_(parameters), ip_reservations_unique_(true), unusable_(false) { // Create unique timer name per instance. timer_name_ = "MySqlHostMgr["; timer_name_ += boost::lexical_cast(reinterpret_cast(this)); timer_name_ += "]DbReconnectTimer"; - // Validate the schema version first. - std::pair code_version(MYSQL_SCHEMA_VERSION_MAJOR, - MYSQL_SCHEMA_VERSION_MINOR); - - std::string timer_name; - bool retry = false; - if (parameters.count("retry-on-startup")) { - if (parameters.at("retry-on-startup") == "true") { - retry = true; - } - } - if (retry) { - timer_name = timer_name_; - } - - std::pair db_version = getVersion(timer_name); - if (code_version != db_version) { - isc_throw(DbOpenError, - "MySQL schema version mismatch: need version: " - << code_version.first << "." << code_version.second - << " found version: " << db_version.first << "." - << db_version.second); - } + MySqlConnection::ensureSchemaVersion(parameters, + DbCallback(&MySqlHostDataSourceImpl::dbReconnect), + timer_name_); // Create an initial context. pool_.reset(new MySqlHostContextPool()); @@ -3045,8 +3024,7 @@ MySqlHostDataSourceImpl::dbReconnect(ReconnectCtlPtr db_reconnect_ctl) { std::pair MySqlHostDataSourceImpl::getVersion(const std::string& timer_name) const { - LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, - DHCPSRV_MYSQL_HOST_DB_GET_VERSION); + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_HOST_DB_GET_VERSION); IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); DbCallback cb(&MySqlHostDataSourceImpl::dbReconnect); diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.cc b/src/lib/dhcpsrv/mysql_lease_mgr.cc index 23ad6f2d76..c54de05ba7 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.cc +++ b/src/lib/dhcpsrv/mysql_lease_mgr.cc @@ -1142,7 +1142,7 @@ public: private: // Note: All array lengths are equal to the corresponding variable in the - // schema. + // schema. // Note: Arrays are declared fixed length for speed of creation uint32_t addr4_; ///< IPv4 address MYSQL_BIND bind_[LEASE_COLUMNS]; ///< Bind array @@ -2190,24 +2190,13 @@ MySqlLeaseMgr::MySqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters) // Check if the extended info tables are enabled. setExtendedInfoTablesEnabled(parameters); - // retry-on-startup? - bool const retry(parameters.count("retry-on-startup") && - parameters.at("retry-on-startup") == "true"); - - // retry-on-startup disabled. Ensure schema version with empty timer name / no retry. - if (!retry) { - ensureSchemaVersion(); - } - // Create unique timer name per instance. timer_name_ = "MySqlLeaseMgr["; timer_name_ += boost::lexical_cast(reinterpret_cast(this)); timer_name_ += "]DbReconnectTimer"; - // retry-on-startup enabled. Ensure schema version with timer name set / retries. - if (retry) { - ensureSchemaVersion(); - } + MySqlConnection::ensureSchemaVersion(parameters, DbCallback(&MySqlLeaseMgr::dbReconnect), + timer_name_); // Create an initial context. pool_.reset(new MySqlLeaseContextPool()); @@ -3879,16 +3868,6 @@ MySqlLeaseMgr::getDescription() const { return (std::string("MySQL Database")); } -void -MySqlLeaseMgr::ensureSchemaVersion() const { - LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_VERSION); - - IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); - DbCallback cb(&MySqlLeaseMgr::dbReconnect); - - return (MySqlConnection::ensureSchemaVersion(parameters_, ac, cb, timer_name_)); -} - std::pair MySqlLeaseMgr::getVersion(const string& timer_name) const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_VERSION); diff --git a/src/lib/dhcpsrv/mysql_lease_mgr.h b/src/lib/dhcpsrv/mysql_lease_mgr.h index 7f6cb14130..af1c964eaf 100644 --- a/src/lib/dhcpsrv/mysql_lease_mgr.h +++ b/src/lib/dhcpsrv/mysql_lease_mgr.h @@ -700,8 +700,6 @@ public: /// @return Description of the backend. virtual std::string getDescription() const override; - void ensureSchemaVersion() const; - /// @brief Returns backend version. /// /// @param timer_name The DB reconnect timer name. diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index b1bbc95feb..7659b508d9 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -2268,13 +2268,7 @@ PgSqlHostDataSource::PgSqlHostContextAlloc::~PgSqlHostContextAlloc() { } PgSqlHostDataSourceImpl::PgSqlHostDataSourceImpl(const DatabaseConnection::ParameterMap& parameters) - : parameters_(parameters), ip_reservations_unique_(true), unusable_(false), - timer_name_("") { - - // Create unique timer name per instance. - timer_name_ = "PgSqlHostMgr["; - timer_name_ += boost::lexical_cast(reinterpret_cast(this)); - timer_name_ += "]DbReconnectTimer"; + : parameters_(parameters), ip_reservations_unique_(true), unusable_(false) { // Check TLS support. size_t tls(0); @@ -2298,29 +2292,14 @@ PgSqlHostDataSourceImpl::PgSqlHostDataSourceImpl(const DatabaseConnection::Param } #endif - // Validate the schema version first. - std::pair code_version(PGSQL_SCHEMA_VERSION_MAJOR, - PGSQL_SCHEMA_VERSION_MINOR); - - std::string timer_name; - bool retry = false; - if (parameters.count("retry-on-startup")) { - if (parameters.at("retry-on-startup") == "true") { - retry = true; - } - } - if (retry) { - timer_name = timer_name_; - } + // Create unique timer name per instance. + timer_name_ = "PgSqlHostMgr["; + timer_name_ += boost::lexical_cast(reinterpret_cast(this)); + timer_name_ += "]DbReconnectTimer"; - std::pair db_version = getVersion(timer_name); - if (code_version != db_version) { - isc_throw(DbOpenError, - "PostgreSQL schema version mismatch: need version: " - << code_version.first << "." << code_version.second - << " found version: " << db_version.first << "." - << db_version.second); - } + PgSqlConnection::ensureSchemaVersion(parameters_, + DbCallback(&PgSqlHostDataSourceImpl::dbReconnect), + timer_name_); // Create an initial context. pool_.reset(new PgSqlHostContextPool()); @@ -2631,8 +2610,7 @@ PgSqlHostDataSourceImpl::getHost(PgSqlHostContextPtr& ctx, std::pair PgSqlHostDataSourceImpl::getVersion(const std::string& timer_name) const { - LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, - DHCPSRV_PGSQL_HOST_DB_GET_VERSION); + LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_HOST_DB_GET_VERSION); IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); DbCallback cb(&PgSqlHostDataSourceImpl::dbReconnect); diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index 5f21fab9a9..c860ebbf6b 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -1617,20 +1617,6 @@ PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters) // Check if the extended info tables are enabled. setExtendedInfoTablesEnabled(parameters); - // retry-on-startup? - bool const retry(parameters.count("retry-on-startup") && - parameters.at("retry-on-startup") == "true"); - - // retry-on-startup disabled. Ensure schema version with empty timer name / no retry. - if (!retry) { - ensureSchemaVersion(); - } - - // Create unique timer name per instance. - timer_name_ = "PgSqlLeaseMgr["; - timer_name_ += boost::lexical_cast(reinterpret_cast(this)); - timer_name_ += "]DbReconnectTimer"; - // Check TLS support. size_t tls(0); tls += parameters.count("trust-anchor"); @@ -1653,10 +1639,13 @@ PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters) } #endif - // retry-on-startup enabled. Ensure schema version with timer name set / retries. - if (retry) { - ensureSchemaVersion(); - } + // Create unique timer name per instance. + timer_name_ = "PgSqlLeaseMgr["; + timer_name_ += boost::lexical_cast(reinterpret_cast(this)); + timer_name_ += "]DbReconnectTimer"; + + PgSqlConnection::ensureSchemaVersion(parameters_, DbCallback(&PgSqlLeaseMgr::dbReconnect), + timer_name_); // Create an initial context. pool_.reset(new PgSqlLeaseContextPool()); @@ -3034,16 +3023,6 @@ PgSqlLeaseMgr::getDescription() const { return (std::string("PostgreSQL Database")); } -void -PgSqlLeaseMgr::ensureSchemaVersion() const { - LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_VERSION); - - IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); - DbCallback cb(&PgSqlLeaseMgr::dbReconnect); - - return (PgSqlConnection::ensureSchemaVersion(parameters_, ac, cb, timer_name_)); -} - std::pair PgSqlLeaseMgr::getVersion(const string& timer_name) const { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_VERSION); diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.h b/src/lib/dhcpsrv/pgsql_lease_mgr.h index 553415feaa..d6bc94a212 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.h +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.h @@ -676,8 +676,6 @@ public: /// @return Description of the backend. virtual std::string getDescription() const override; - void ensureSchemaVersion() const; - /// @brief Returns backend version. /// /// @param timer_name The DB reconnect timer name. diff --git a/src/lib/mysql/mysql_connection.cc b/src/lib/mysql/mysql_connection.cc index aee087e266..206c069450 100644 --- a/src/lib/mysql/mysql_connection.cc +++ b/src/lib/mysql/mysql_connection.cc @@ -371,9 +371,19 @@ MySqlConnection::getVersion(const ParameterMap& parameters, void MySqlConnection::ensureSchemaVersion(const ParameterMap& parameters, - const IOServiceAccessorPtr& ac, const DbCallback& cb, - const string& timer_name) { + string timer_name) { + // retry-on-startup? + bool const retry(parameters.count("retry-on-startup") && + parameters.at("retry-on-startup") == "true"); + if (!retry) { + // If not, then we need timer_name to be empty to signal that retrying + // is not desired. + timer_name = string(); + } + + IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); + pair schema_version; try { schema_version = getVersion(parameters, ac, cb, timer_name); diff --git a/src/lib/mysql/mysql_connection.h b/src/lib/mysql/mysql_connection.h index d7a3ce5dfc..410aaa2de5 100644 --- a/src/lib/mysql/mysql_connection.h +++ b/src/lib/mysql/mysql_connection.h @@ -282,15 +282,16 @@ public: /// version, and attempt to initialize the schema if there is an /// error during retrieval. /// + /// Properly handles retrying of the database connection. + /// /// @param parameters A data structure relating keywords and values /// concerned with the database. /// /// @throw isc::db::ScehamInitializationFailed if the initialization fails static void ensureSchemaVersion(const ParameterMap& parameters, - const IOServiceAccessorPtr& ac = IOServiceAccessorPtr(), const DbCallback& cb = DbCallback(), - const std::string& timer_name = std::string()); + std::string timer_name = std::string()); /// @brief Initialize schema. /// diff --git a/src/lib/pgsql/pgsql_connection.cc b/src/lib/pgsql/pgsql_connection.cc index 10da6db422..addf7c6b64 100644 --- a/src/lib/pgsql/pgsql_connection.cc +++ b/src/lib/pgsql/pgsql_connection.cc @@ -170,9 +170,19 @@ PgSqlConnection::getVersion(const ParameterMap& parameters, void PgSqlConnection::ensureSchemaVersion(const ParameterMap& parameters, - const IOServiceAccessorPtr& ac, const DbCallback& cb, - const string& timer_name) { + string timer_name) { + // retry-on-startup? + bool const retry(parameters.count("retry-on-startup") && + parameters.at("retry-on-startup") == "true"); + if (!retry) { + // If not, then we need timer_name to be empty to signal that retrying + // is not desired. + timer_name = string(); + } + + IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService)); + pair schema_version; try { schema_version = getVersion(parameters, ac, cb, timer_name); diff --git a/src/lib/pgsql/pgsql_connection.h b/src/lib/pgsql/pgsql_connection.h index 66e69aad5f..d0b5b755df 100644 --- a/src/lib/pgsql/pgsql_connection.h +++ b/src/lib/pgsql/pgsql_connection.h @@ -252,15 +252,16 @@ public: /// version, and attempt to initialize the schema if there is an /// error during retrieval. /// + /// Properly handles retrying of the database connection. + /// /// @param parameters A data structure relating keywords and values /// concerned with the database. /// /// @throw isc::db::ScehamInitializationFailed if the initialization fails static void ensureSchemaVersion(const ParameterMap& parameters, - const IOServiceAccessorPtr& ac = IOServiceAccessorPtr(), const DbCallback& cb = DbCallback(), - const std::string& timer_name = std::string()); + std::string timer_name = std::string()); /// @brief Initialize schema. ///