... and allow kea-admin path to be overridden in tests.
};
/// @brief Thrown when an initialization of the schema failed.
-class SchemaInitializationFailed: public Exception {
+class SchemaInitializationFailed : public Exception {
public:
SchemaInitializationFailed(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) {}
-// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
-// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
-# Copyright (C) 2012-2023 Internet Systems Consortium, Inc. ("ISC")
+# Copyright (C) 2012-2024 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
-// Copyright (C) 2012-2023 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
namespace isc {
namespace db {
+std::string MySqlConnection::KEA_ADMIN_ = KEA_ADMIN;
+
int MySqlHolder::atexit_ = [] {
return atexit([] { mysql_library_end(); });
}();
void
MySqlConnection::ensureSchemaVersion(const ParameterMap& parameters,
const DbCallback& cb,
- string timer_name) {
+ const string& timer_name) {
// retry-on-startup?
bool const retry(parameters.count("retry-on-startup") &&
parameters.at("retry-on-startup") == "true");
IOServiceAccessorPtr ac(new IOServiceAccessor(&DatabaseConnection::getIOService));
pair<uint32_t, uint32_t> schema_version;
try {
- // Attempt to get version without retrying or other sophistries. This
- // provides the most accurate view of the status of the database and the
- // most flexibility to reacting to errors.
- schema_version = getVersion(parameters);
+ schema_version = getVersion(parameters, ac, cb, retry ? timer_name : string());
} catch (DbOpenError const& exception) {
- // Could not establish a connection. Best thing is to wait for the
- // database server to come up. Establish he mechanism of retrying.
- if (retry && !timer_name.empty()) {
- MySqlConnection conn(parameters, ac, cb);
- conn.makeReconnectCtl(timer_name);
- conn.openDatabase();
- } else {
- rethrow_exception(current_exception());
- }
+ throw;
+ } catch (DbOpenErrorWithRetry const& exception) {
+ throw;
} catch (exception const& exception) {
// This failure may occur for a variety of reasons. We are looking at
// initializing schema as the only potential mitigation. We could narrow
return;
}
+ if (!isc::util::file::isFile(KEA_ADMIN_)) {
+ // It can happen for kea-admin to not exist, especially with
+ // packages that install it in a separate package.
+ return;
+ }
+
// Convert parameters.
vector<string> kea_admin_parameters(toKeaAdminParameters(parameters));
ProcessEnvVars const vars;
kea_admin_parameters.insert(kea_admin_parameters.begin(), "db-init");
// Run.
- ProcessSpawn kea_admin(KEA_ADMIN, kea_admin_parameters, vars,
+ ProcessSpawn kea_admin(KEA_ADMIN_, kea_admin_parameters, vars,
/* inherit_env = */ true);
DB_LOG_INFO(MYSQL_INITIALIZE_SCHEMA).arg(kea_admin.getCommandLine());
pid_t const pid(kea_admin.spawn());
static void
ensureSchemaVersion(const ParameterMap& parameters,
const DbCallback& cb = DbCallback(),
- std::string timer_name = std::string());
+ const std::string& timer_name = std::string());
/// @brief Initialize schema.
///
/// @brief TLS flag (true when TLS was required, false otherwise).
bool tls_;
+
+ static std::string KEA_ADMIN_;
};
} // end of isc::db namespace
AM_CPPFLAGS =
AM_CPPFLAGS += -DABS_TOP_BUILDDIR="\"$(abs_top_builddir)\""
+AM_CPPFLAGS += -DKEA_ADMIN=\"${abs_top_builddir}/src/bin/admin/kea-admin\"
AM_CPPFLAGS += -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES) $(MYSQL_CPPFLAGS)
-// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
MySqlConnectionTest(bool const primary_key = false)
: conn_(DatabaseConnection::parse(validMySQLConnectionString())) {
+ MySqlConnection::KEA_ADMIN_ = KEA_ADMIN;
+
try {
// Open new connection.
conn_.openDatabase();
EXPECT_EQ(MYSQL_SCHEMA_VERSION_MINOR, version.second);
}
-/// @brief Check ensureSchemaVersion when schema is not created.
+/// @brief Check initializeSchema when schema is not created.
TEST_F(MySqlConnectionTest, initializeSchemaNoSchema) {
pair<uint32_t, uint32_t> version;
auto const parameters(DatabaseConnection::parse(validMySQLConnectionString()));
EXPECT_EQ(MYSQL_SCHEMA_VERSION_MINOR, version.second);
}
-/// @brief Check ensureSchemaVersion when schema is created.
+/// @brief Check initializeSchema when schema is created.
TEST_F(MySqlConnectionTest, initializeSchema) {
pair<uint32_t, uint32_t> version;
auto const parameters(DatabaseConnection::parse(validMySQLConnectionString()));
EXPECT_EQ(MYSQL_SCHEMA_VERSION_MINOR, version.second);
}
-/// @brief Check ensureSchemaVersion when schema is created.
+/// @brief Check toKeaAdminParameters.
TEST_F(MySqlConnectionTest, toKeaAdminParameters) {
auto parameters(DatabaseConnection::parse(validMySQLConnectionString()));
vector<string> kea_admin_parameters(MySqlConnection::toKeaAdminParameters(parameters));
-// Copyright (C) 2016-2023 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
#include <database/db_exceptions.h>
#include <database/db_log.h>
#include <pgsql/pgsql_connection.h>
+#include <util/file_utilities.h>
#include <exception>
namespace isc {
namespace db {
+std::string PgSqlConnection::KEA_ADMIN_ = KEA_ADMIN;
+
// Default connection timeout
/// @todo: migrate this default timeout to src/bin/dhcpX/simple_parserX.cc
void
PgSqlConnection::ensureSchemaVersion(const ParameterMap& parameters,
const DbCallback& cb,
- string timer_name) {
+ const string& timer_name) {
// retry-on-startup?
bool const retry(parameters.count("retry-on-startup") &&
parameters.at("retry-on-startup") == "true");
// Attempt to get version without retrying or other sophistries. This
// provides the most accurate view of the status of the database and the
// most flexibility to reacting to errors.
- schema_version = getVersion(parameters);
+ schema_version = getVersion(parameters, ac, cb, retry ? timer_name : string());
} catch (DbOpenError const& exception) {
- // Could not establish a connection. Best thing is to wait for the
- // database server to come up. Establish he mechanism of retrying.
- if (retry && !timer_name.empty()) {
- PgSqlConnection conn(parameters, ac, cb);
- conn.makeReconnectCtl(timer_name);
- conn.openDatabaseInternal(false);
- } else {
- rethrow_exception(current_exception());
- }
+ throw;
+ } catch (DbOpenErrorWithRetry const& exception) {
+ throw;
} catch (exception const& exception) {
// This failure may occur for a variety of reasons. We are looking at
// initializing schema as the only potential mitigation. We could narrow
return;
}
+ if (!isc::util::file::isFile(KEA_ADMIN_)) {
+ // It can happen for kea-admin to not exist, especially with
+ // packages that install it in a separate package.
+ return;
+ }
+
// Convert parameters.
auto const tupl(toKeaAdminParameters(parameters));
vector<string> kea_admin_parameters(get<0>(tupl));
kea_admin_parameters.insert(kea_admin_parameters.begin(), "db-init");
// Run.
- ProcessSpawn kea_admin(KEA_ADMIN, kea_admin_parameters, vars,
+ ProcessSpawn kea_admin(KEA_ADMIN_, kea_admin_parameters, vars,
/* inherit_env = */ true);
DB_LOG_INFO(PGSQL_INITIALIZE_SCHEMA).arg(kea_admin.getCommandLine());
pid_t const pid(kea_admin.spawn());
/// @brief Destructor
virtual ~PgSqlConnection();
- /// @brief Convert MySQL library parameters to kea-admin parameters.
+ /// @brief Convert PostgreSQL library parameters to kea-admin parameters.
///
- /// @param params input MySQL parameters
+ /// @param params input PostgreSQL parameters
///
/// @return tuple of (vector of kea-admin parameters, vector of PostgreSQL
/// environment variables)
static void
ensureSchemaVersion(const ParameterMap& parameters,
const DbCallback& cb = DbCallback(),
- std::string timer_name = std::string());
+ const std::string& timer_name = std::string());
/// @brief Initialize schema.
///
/// ongoing transaction. We do not want to start new transactions when one
/// is already in progress.
int transaction_ref_count_;
+
+ static std::string KEA_ADMIN_;
};
/// @brief Defines a pointer to a PgSqlConnection
AM_CPPFLAGS =
AM_CPPFLAGS += -DABS_TOP_BUILDDIR="\"$(abs_top_builddir)\""
+AM_CPPFLAGS += -DKEA_ADMIN=\"${abs_top_builddir}/src/bin/admin/kea-admin\"
AM_CPPFLAGS += -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES) $(PGSQL_CPPFLAGS)
typedef std::vector<TestRow> TestRowSet;
/// @brief Constructor.
- PgSqlConnectionTest() : PgSqlBasicsTest() {};
+ PgSqlConnectionTest() : PgSqlBasicsTest() {
+ PgSqlConnection::KEA_ADMIN_ = KEA_ADMIN;
+ };
/// @brief Destructor.
virtual ~PgSqlConnectionTest() {
EXPECT_EQ(PGSQL_SCHEMA_VERSION_MINOR, version.second);
}
-/// @brief Check ensureSchemaVersion when schema is not created.
+/// @brief Check initializeSchema when schema is not created.
TEST_F(PgSqlConnectionTest, initializeSchemaNoSchema) {
pair<uint32_t, uint32_t> version;
auto const parameters(DatabaseConnection::parse(validPgSQLConnectionString()));
EXPECT_EQ(PGSQL_SCHEMA_VERSION_MINOR, version.second);
}
-/// @brief Check ensureSchemaVersion when schema is created.
+/// @brief Check initializeSchema when schema is created.
TEST_F(PgSqlConnectionTest, initializeSchema) {
pair<uint32_t, uint32_t> version;
auto const parameters(DatabaseConnection::parse(validPgSQLConnectionString()));
EXPECT_EQ(PGSQL_SCHEMA_VERSION_MINOR, version.second);
}
-/// @brief Check ensureSchemaVersion when schema is created.
+/// @brief Check toKeaAdminParameters.
TEST_F(PgSqlConnectionTest, toKeaAdminParameters) {
auto parameters(DatabaseConnection::parse(validPgSQLConnectionString()));
auto tupl(PgSqlConnection::toKeaAdminParameters(parameters));