2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-03 15:35:17 +00:00

[960-mysql-connection-pool] Moved schema version checking

This commit is contained in:
Francis Dupont
2019-11-13 15:09:02 +01:00
parent 624db298b1
commit a28a5773a3
2 changed files with 21 additions and 50 deletions

View File

@@ -1725,8 +1725,21 @@ MySqlLeaseMgr::MySqlLeaseContextAlloc::~MySqlLeaseContextAlloc() {
MySqlLeaseMgr::MySqlLeaseMgr(const MySqlConnection::ParameterMap& parameters) MySqlLeaseMgr::MySqlLeaseMgr(const MySqlConnection::ParameterMap& parameters)
: parameters_(parameters) { : parameters_(parameters) {
// Test schema version.
std::pair<uint32_t, uint32_t> code_version(MYSQL_SCHEMA_VERSION_MAJOR,
MYSQL_SCHEMA_VERSION_MINOR);
std::pair<uint32_t, uint32_t> db_version = getVersion();
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);
}
// Create an initial context.
pool_.reset(new MySqlLeaseContextPool()); pool_.reset(new MySqlLeaseContextPool());
pool_->pool_.push_back(createContext(true)); pool_->pool_.push_back(createContext());
} }
MySqlLeaseMgr::~MySqlLeaseMgr() { MySqlLeaseMgr::~MySqlLeaseMgr() {
@@ -1737,27 +1750,13 @@ MySqlLeaseMgr::~MySqlLeaseMgr() {
// Create context. // Create context.
MySqlLeaseContextPtr MySqlLeaseContextPtr
MySqlLeaseMgr::createContext(bool check_version /* = false */) const { MySqlLeaseMgr::createContext() const {
MySqlLeaseContextPtr ctx(new MySqlLeaseContext(parameters_)); MySqlLeaseContextPtr ctx(new MySqlLeaseContext(parameters_));
// Open the database. // Open the database.
ctx->conn_.openDatabase(); ctx->conn_.openDatabase();
// Test schema version before we try to prepare statements.
if (check_version) {
std::pair<uint32_t, uint32_t> code_version(MYSQL_SCHEMA_VERSION_MAJOR,
MYSQL_SCHEMA_VERSION_MINOR);
std::pair<uint32_t, uint32_t> db_version = getVersion(ctx->conn_);
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);
}
}
// Enable autocommit. To avoid a flush to disk on every commit, the global // Enable autocommit. To avoid a flush to disk on every commit, the global
// parameter innodb_flush_log_at_trx_commit should be set to 2. This will // parameter innodb_flush_log_at_trx_commit should be set to 2. This will
// cause the changes to be written to the log, but flushed to disk in the // cause the changes to be written to the log, but flushed to disk in the
@@ -2959,21 +2958,15 @@ MySqlLeaseMgr::getDescription() const {
std::pair<uint32_t, uint32_t> std::pair<uint32_t, uint32_t>
MySqlLeaseMgr::getVersion() const { MySqlLeaseMgr::getVersion() const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
DHCPSRV_MYSQL_GET_VERSION);
// Get a connection. // Get a connection.
MySqlConnection conn(parameters_); MySqlConnection conn(parameters_);
// Open the database. // Open the database.
conn.openDatabase(); conn.openDatabase();
// Get the version.
return (getVersion(conn));
}
std::pair<uint32_t, uint32_t>
MySqlLeaseMgr::getVersion(MySqlConnection& conn) const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
DHCPSRV_MYSQL_GET_VERSION);
// Allocate a new statement. // Allocate a new statement.
MYSQL_STMT *stmt = mysql_stmt_init(conn.mysql_); MYSQL_STMT *stmt = mysql_stmt_init(conn.mysql_);
if (stmt == NULL) { if (stmt == NULL) {

View File

@@ -87,7 +87,7 @@ public:
/// - user - Username under which to connect (optional) /// - user - Username under which to connect (optional)
/// - password - Password for "user" on the database (optional) /// - password - Password for "user" on the database (optional)
/// ///
/// Create an initial context checking the schema version. /// Check the schema version and create an initial context.
/// ///
/// @param parameters A data structure relating keywords and values /// @param parameters A data structure relating keywords and values
/// concerned with the database. /// concerned with the database.
@@ -104,20 +104,13 @@ public:
/// #brief Create a new context. /// #brief Create a new context.
/// ///
/// The database in opened. /// The database in opened withh all the SQL commands pre-compiled.
/// If wanted the version number in the schema_version table will be
/// checked against hard-coded value in the implementation file.
/// Finally, all the SQL commands are pre-compiled.
/// ///
/// @param check_version If true and the database successfully opened
/// check the schema version.
/// @return A new (never null) context. /// @return A new (never null) context.
/// @throw isc::dhcp::NoDatabaseName Mandatory database name not given. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given.
/// @throw isc::db::DbOpenError Error opening the database or the schema
/// version is incorrect.
/// @throw isc::db::DbOperationError An operation on the open database has /// @throw isc::db::DbOperationError An operation on the open database has
/// failed. /// failed.
MySqlLeaseContextPtr createContext(bool check_version = false) const; MySqlLeaseContextPtr createContext() const;
/// @brief Local version of getDBVersion() class method /// @brief Local version of getDBVersion() class method
static std::string getDBVersion(); static std::string getDBVersion();
@@ -671,21 +664,6 @@ public:
}; };
private: private:
/// @brief Returns backend version.
///
/// The method is called by the constructor after opening the database
/// but prior to preparing SQL statements, to verify that the schema version
/// is correct. Thus it must not rely on a pre-prepared statement or
/// formal statement execution error checking.
///
/// @param conn Initial connecion.
/// @return Version number as a pair of unsigned integers. "first" is the
/// major version number, "second" the minor number.
///
/// @throw isc::db::DbOperationError An operation on the open database has
/// failed.
virtual std::pair<uint32_t, uint32_t> getVersion(db::MySqlConnection& conn) const;
/// @brief Add Lease Common Code /// @brief Add Lease Common Code
/// ///
/// This method performs the common actions for both flavours (V4 and V6) /// This method performs the common actions for both flavours (V4 and V6)