2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-31 14:05:33 +00:00

[#1073] implemented connection pool for mysql host mgr

This commit is contained in:
Razvan Becheriu
2020-01-10 16:17:18 +02:00
parent 1f819a415a
commit 2d323180d9
4 changed files with 548 additions and 307 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -22,12 +22,21 @@ namespace dhcp {
/// Forward declaration to the implementation of the @ref MySqlHostDataSource.
class MySqlHostDataSourceImpl;
/// @brief Type of pointers to MySqlHostDataSourceImpl.
typedef boost::shared_ptr<MySqlHostDataSourceImpl> MySqlHostDataSourceImplPtr;
/// Forward declaration for the thread context for the manager pool.
class MySqlHostContext;
/// @brief Type of pointers to contexts.
typedef boost::shared_ptr<MySqlHostContext> MySqlHostContextPtr;
/// @brief MySQL Host Data Source
///
/// This class implements the @ref isc::dhcp::BaseHostDataSource interface to
/// the MySQL database. Use of this backend presupposes that a MySQL database
/// is available and that the Kea schema has been created within it.
class MySqlHostDataSource: public BaseHostDataSource {
class MySqlHostDataSource : public BaseHostDataSource {
public:
/// @brief Constructor
@@ -80,7 +89,8 @@ public:
/// @param addr specified address.
/// @return true if deletion was successful, false if the host was not there.
/// @throw various exceptions in case of errors
virtual bool del(const SubnetID& subnet_id, const asiolink::IOAddress& addr);
virtual bool del(const SubnetID& subnet_id,
const asiolink::IOAddress& addr);
/// @brief Attempts to delete a host by (subnet4-id, identifier type, identifier)
///
@@ -96,7 +106,8 @@ public:
/// @throw various exceptions in case of errors
virtual bool del4(const SubnetID& subnet_id,
const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin, const size_t identifier_len);
const uint8_t* identifier_begin,
const size_t identifier_len);
/// @brief Attempts to delete a host by (subnet6-id, identifier type, identifier)
///
@@ -112,7 +123,8 @@ public:
/// @throw various exceptions in case of errors
virtual bool del6(const SubnetID& subnet_id,
const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin, const size_t identifier_len);
const uint8_t* identifier_begin,
const size_t identifier_len);
/// @brief Return all hosts connected to any subnet for which reservations
/// have been made using a specified identifier.
@@ -127,9 +139,9 @@ public:
/// @param identifier_len Identifier length.
///
/// @return Collection of const @c Host objects.
virtual ConstHostCollection
getAll(const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin, const size_t identifier_len) const;
virtual ConstHostCollection getAll(const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) const;
/// @brief Return all hosts in a DHCPv4 subnet.
///
@@ -139,8 +151,7 @@ public:
/// @param subnet_id subnet identifier to filter by
///
/// @return Collection of const @ref Host objects.
virtual ConstHostCollection
getAll4(const SubnetID& subnet_id) const;
virtual ConstHostCollection getAll4(const SubnetID& subnet_id) const;
/// @brief Return all hosts in a DHCPv6 subnet.
///
@@ -150,8 +161,7 @@ public:
/// @param subnet_id subnet identifier to filter by
///
/// @return Collection of const @ref Host objects.
virtual ConstHostCollection
getAll6(const SubnetID& subnet_id) const;
virtual ConstHostCollection getAll6(const SubnetID& subnet_id) const;
/// @brief Return all hosts with a hostname.
///
@@ -163,8 +173,7 @@ public:
/// @param hostname The lower case hostname.
///
/// @return Collection of const @c Host objects.
virtual ConstHostCollection
getAllbyHostname(const std::string& hostname) const;
virtual ConstHostCollection getAllbyHostname(const std::string& hostname) const;
/// @brief Return all hosts with a hostname in a DHCPv4 subnet.
///
@@ -175,8 +184,8 @@ public:
/// @param subnet_id Subnet identifier.
///
/// @return Collection of const @c Host objects.
virtual ConstHostCollection
getAllbyHostname4(const std::string& hostname, const SubnetID& subnet_id) const;
virtual ConstHostCollection getAllbyHostname4(const std::string& hostname,
const SubnetID& subnet_id) const;
/// @brief Return all hosts with a hostname in a DHCPv6 subnet.
///
@@ -187,8 +196,8 @@ public:
/// @param subnet_id Subnet identifier.
///
/// @return Collection of const @c Host objects.
virtual ConstHostCollection
getAllbyHostname6(const std::string& hostname, const SubnetID& subnet_id) const;
virtual ConstHostCollection getAllbyHostname6(const std::string& hostname,
const SubnetID& subnet_id) const;
/// @brief Returns range of hosts in a DHCPv4 subnet.
///
@@ -202,11 +211,10 @@ public:
/// @param page_size maximum size of the page returned.
///
/// @return Collection of const @c Host objects (may be empty).
virtual ConstHostCollection
getPage4(const SubnetID& subnet_id,
size_t& source_index,
uint64_t lower_host_id,
const HostPageSize& page_size) const;
virtual ConstHostCollection getPage4(const SubnetID& subnet_id,
size_t& source_index,
uint64_t lower_host_id,
const HostPageSize& page_size) const;
/// @brief Returns range of hosts in a DHCPv6 subnet.
///
@@ -220,11 +228,10 @@ public:
/// @param page_size maximum size of the page returned.
///
/// @return Collection of const @c Host objects (may be empty).
virtual ConstHostCollection
getPage6(const SubnetID& subnet_id,
size_t& source_index,
uint64_t lower_host_id,
const HostPageSize& page_size) const;
virtual ConstHostCollection getPage6(const SubnetID& subnet_id,
size_t& source_index,
uint64_t lower_host_id,
const HostPageSize& page_size) const;
/// @brief Returns a collection of hosts using the specified IPv4 address.
///
@@ -234,8 +241,7 @@ public:
/// @param address IPv4 address for which the @c Host object is searched.
///
/// @return Collection of const @c Host objects.
virtual ConstHostCollection
getAll4(const asiolink::IOAddress& address) const;
virtual ConstHostCollection getAll4(const asiolink::IOAddress& address) const;
/// @brief Returns a host connected to the IPv4 subnet.
///
@@ -247,9 +253,10 @@ public:
///
/// @return Const @c Host object for which reservation has been made using
/// the specified identifier.
virtual ConstHostPtr
get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin, const size_t identifier_len) const;
virtual ConstHostPtr get4(const SubnetID& subnet_id,
const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) const;
/// @brief Returns a host connected to the IPv4 subnet and having
/// a reservation for a specified IPv4 address.
@@ -267,8 +274,8 @@ public:
/// @param address reserved IPv4 address.
///
/// @return Const @c Host object using a specified IPv4 address.
virtual ConstHostPtr
get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
virtual ConstHostPtr get4(const SubnetID& subnet_id,
const asiolink::IOAddress& address) const;
/// @brief Returns a host connected to the IPv6 subnet.
///
@@ -280,9 +287,10 @@ public:
///
/// @return Const @c Host object for which reservation has been made using
/// the specified identifier.
virtual ConstHostPtr
get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin, const size_t identifier_len) const;
virtual ConstHostPtr get6(const SubnetID& subnet_id,
const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) const;
/// @brief Returns a host using the specified IPv6 prefix.
///
@@ -290,8 +298,8 @@ public:
/// @param prefix_len IPv6 prefix length.
///
/// @return Const @c Host object using a specified IPv6 prefix.
virtual ConstHostPtr
get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
virtual ConstHostPtr get6(const asiolink::IOAddress& prefix,
const uint8_t prefix_len) const;
/// @brief Returns a host connected to the IPv6 subnet and having
/// a reservation for a specified IPv6 address or prefix.
@@ -300,8 +308,8 @@ public:
/// @param address reserved IPv6 address/prefix.
///
/// @return Const @c Host object using a specified IPv6 address/prefix.
virtual ConstHostPtr
get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
virtual ConstHostPtr get6(const SubnetID& subnet_id,
const asiolink::IOAddress& address) const;
/// @brief Return backend type
///
@@ -346,9 +354,34 @@ public:
/// Rolls back all pending database operations.
virtual void rollback();
/// @brief Context RAII Allocator.
class MySqlHostContextAlloc {
public:
/// @brief Constructor
///
/// This constructor takes a context of the pool if one is available
/// or creates a new one.
///
/// @param mgr A parent instance
MySqlHostContextAlloc(const MySqlHostDataSourceImpl& mgr);
/// @brief Destructor
///
/// This destructor puts back the context in the pool.
~MySqlHostContextAlloc();
/// @brief The context
MySqlHostContextPtr ctx_;
private:
/// @brief The manager
const MySqlHostDataSourceImpl& mgr_;
};
private:
/// @brief Pointer to the implementation of the @ref MySqlHostDataSource.
MySqlHostDataSourceImpl* impl_;
MySqlHostDataSourceImplPtr impl_;
};
}

View File

@@ -175,9 +175,9 @@ tagged_statements = { {
"fqdn_fwd, fqdn_rev, hostname, "
"state, user_context "
"FROM lease4 "
"WHERE state != ?"
" AND valid_lifetime != 4294967295"
" AND expire < ? "
"WHERE state != ? "
"AND valid_lifetime != 4294967295 "
"AND expire < ? "
"ORDER BY expire ASC "
"LIMIT ?"},
{MySqlLeaseMgr::GET_LEASE6,
@@ -262,9 +262,9 @@ tagged_statements = { {
"hwaddr, hwtype, hwaddr_source, "
"state, user_context "
"FROM lease6 "
"WHERE state != ?"
" AND valid_lifetime != 4294967295"
" AND expire < ? "
"WHERE state != ? "
"AND valid_lifetime != 4294967295 "
"AND expire < ? "
"ORDER BY expire ASC "
"LIMIT ?"},
{MySqlLeaseMgr::INSERT_LEASE4,
@@ -682,7 +682,7 @@ public:
// bind_[2].is_null = &MLM_FALSE; // commented out for performance
// reasons, see memset() above
// lease_time: unsigned int
// valid lifetime: unsigned int
bind_[3].buffer_type = MYSQL_TYPE_LONG;
bind_[3].buffer = reinterpret_cast<char*>(&valid_lifetime_);
bind_[3].is_unsigned = MLM_TRUE;
@@ -736,7 +736,7 @@ public:
// bind_[9].is_null = &MLM_FALSE; // commented out for performance
// reasons, see memset() above
// user_context: text null
// user_context: text
user_context_null_ = MLM_FALSE;
user_context_length_ = sizeof(user_context_);
bind_[10].buffer_type = MYSQL_TYPE_STRING;
@@ -1109,7 +1109,7 @@ public:
bind_[12].is_null = &hwaddr_null_;
}
// hardware source: unsigned int (32 bits)
// hardware type: unsigned short int (16 bits)
if (hwaddr) {
hwtype_ = lease->hwaddr_->htype_;
bind_[13].buffer_type = MYSQL_TYPE_SHORT;
@@ -1223,7 +1223,7 @@ public:
// bind_[1].is_null = &MLM_FALSE; // commented out for performance
// reasons, see memset() above
// lease_time: unsigned int
// valid lifetime: unsigned int
bind_[2].buffer_type = MYSQL_TYPE_LONG;
bind_[2].buffer = reinterpret_cast<char*>(&valid_lifetime_);
bind_[2].is_unsigned = MLM_TRUE;
@@ -1321,7 +1321,7 @@ public:
// bind_[15].is_null = &MLM_FALSE; // commented out for performance
// reasons, see memset() above
// user_context: text null
// user_context: text
user_context_null_ = MLM_FALSE;
user_context_length_ = sizeof(user_context_);
bind_[16].buffer_type = MYSQL_TYPE_STRING;
@@ -1357,7 +1357,7 @@ public:
// Create the IOAddress object corresponding to the received data.
addr6_buffer_[addr6_length_] = '\0';
std::string address = addr6_buffer_;
isc::asiolink::IOAddress addr(address);
IOAddress addr(address);
// Set the lease type in a variable of the appropriate data type, which
// has been initialized with an arbitrary (but valid) value.
@@ -1500,7 +1500,7 @@ private:
/// lease storage. It does so by executing a query which returns a result
/// containing one row per monitored state per lease type per
/// subnet, ordered by subnet id in ascending order.
///
class MySqlLeaseStatsQuery : public LeaseStatsQuery {
public:
@@ -1717,8 +1717,8 @@ private:
// MySqlLeaseContext Constructor
MySqlLeaseContext::MySqlLeaseContext(
const DatabaseConnection::ParameterMap& parameters) : conn_(parameters) {
MySqlLeaseContext::MySqlLeaseContext(const DatabaseConnection::ParameterMap& parameters)
: conn_(parameters) {
}
// MySqlLeaseContextAlloc Constructor and Destructor
@@ -1775,15 +1775,12 @@ MySqlLeaseMgr::MySqlLeaseMgr(const MySqlConnection::ParameterMap& parameters)
}
MySqlLeaseMgr::~MySqlLeaseMgr() {
// There is no need to close the database in this destructor: it is
// closed in the destructor of the mysql_ member variable.
}
// Create context.
MySqlLeaseContextPtr
MySqlLeaseMgr::createContext() const {
MySqlLeaseContextPtr ctx(new MySqlLeaseContext(parameters_));
// Open the database.
@@ -1825,7 +1822,7 @@ MySqlLeaseMgr::getDBVersion() {
// statement, then call common code to execute the statement.
bool
MySqlLeaseMgr::addLeaseCommon(MySqlLeaseContextPtr ctx,
MySqlLeaseMgr::addLeaseCommon(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
std::vector<MYSQL_BIND>& bind) {
@@ -1926,7 +1923,6 @@ MySqlLeaseMgr::getLeaseCollection(MySqlLeaseContextPtr ctx,
Exchange& exchange,
LeaseCollection& result,
bool single) const {
int status;
if (bind) {
@@ -2028,7 +2024,7 @@ MySqlLeaseMgr::getLease(MySqlLeaseContextPtr ctx,
// criteria.
Lease4Ptr
MySqlLeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
MySqlLeaseMgr::getLease4(const IOAddress& addr) const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_ADDR4)
.arg(addr.toText());
@@ -2295,7 +2291,7 @@ MySqlLeaseMgr::getLeases4() const {
}
Lease4Collection
MySqlLeaseMgr::getLeases4(const asiolink::IOAddress& lower_bound_address,
MySqlLeaseMgr::getLeases4(const IOAddress& lower_bound_address,
const LeasePageSize& page_size) const {
// Expecting IPv4 address.
if (!lower_bound_address.isV4()) {
@@ -2338,7 +2334,7 @@ MySqlLeaseMgr::getLeases4(const asiolink::IOAddress& lower_bound_address,
Lease6Ptr
MySqlLeaseMgr::getLease6(Lease::Type lease_type,
const isc::asiolink::IOAddress& addr) const {
const IOAddress& addr) const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MYSQL_GET_ADDR6)
.arg(addr.toText())
.arg(lease_type);
@@ -2583,7 +2579,7 @@ MySqlLeaseMgr::getLeases6(const std::string& hostname) const {
}
Lease6Collection
MySqlLeaseMgr::getLeases6(const asiolink::IOAddress& lower_bound_address,
MySqlLeaseMgr::getLeases6(const IOAddress& lower_bound_address,
const LeasePageSize& page_size) const {
// Expecting IPv6 address.
if (!lower_bound_address.isV6()) {
@@ -2693,7 +2689,7 @@ MySqlLeaseMgr::getExpiredLeasesCommon(LeaseCollection& expired_leases,
template <typename LeasePtr>
void
MySqlLeaseMgr::updateLeaseCommon(MySqlLeaseContextPtr ctx,
MySqlLeaseMgr::updateLeaseCommon(MySqlLeaseContextPtr& ctx,
StatementIndex stindex, MYSQL_BIND* bind,
const LeasePtr& lease) {
@@ -3104,7 +3100,7 @@ MySqlLeaseMgr::rollback() {
}
void
MySqlLeaseMgr::checkError(MySqlLeaseContextPtr ctx,
MySqlLeaseMgr::checkError(MySqlLeaseContextPtr& ctx,
int status, StatementIndex index,
const char* what) const {
ctx->conn_.checkError(status, index, what);

View File

@@ -684,7 +684,7 @@ private:
///
/// @throw isc::db::DbOperationError An operation on the open database has
/// failed.
bool addLeaseCommon(MySqlLeaseContextPtr ctx,
bool addLeaseCommon(MySqlLeaseContextPtr& ctx,
StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
/// @brief Get Lease Collection Common Code
@@ -709,7 +709,7 @@ private:
/// @throw isc::db::MultipleRecords Multiple records were retrieved
/// from the database where only one was expected.
template <typename Exchange, typename LeaseCollection>
void getLeaseCollection(MySqlLeaseContextPtr ctx,
void getLeaseCollection(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind,
Exchange& exchange, LeaseCollection& result,
@@ -732,7 +732,7 @@ private:
/// failed.
/// @throw isc::db::MultipleRecords Multiple records were retrieved
/// from the database where only one was expected.
void getLeaseCollection(MySqlLeaseContextPtr ctx,
void getLeaseCollection(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind,
Lease4Collection& result) const {
@@ -755,7 +755,7 @@ private:
/// failed.
/// @throw isc::db::MultipleRecords Multiple records were retrieved
/// from the database where only one was expected.
void getLeaseCollection(MySqlLeaseContextPtr ctx,
void getLeaseCollection(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind,
Lease6Collection& result) const {
@@ -772,7 +772,7 @@ private:
/// @param stindex Index of statement being executed
/// @param bind MYSQL_BIND array for input parameters
/// @param result Lease4 object returned
void getLease(MySqlLeaseContextPtr ctx,
void getLease(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind,
Lease4Ptr& result) const;
@@ -787,7 +787,7 @@ private:
/// @param stindex Index of statement being executed
/// @param bind MYSQL_BIND array for input parameters
/// @param result Lease6 object returned
void getLease(MySqlLeaseContextPtr ctx,
void getLease(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind,
Lease6Ptr& result) const;
@@ -829,7 +829,7 @@ private:
/// @throw isc::db::DbOperationError An operation on the open database has
/// failed.
template <typename LeasePtr>
void updateLeaseCommon(MySqlLeaseContextPtr ctx,
void updateLeaseCommon(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind,
const LeasePtr& lease);
@@ -875,7 +875,7 @@ private:
///
/// @throw isc::db::DbOperationError An operation on the open database has
/// failed.
void checkError(MySqlLeaseContextPtr ctx,
void checkError(MySqlLeaseContextPtr& ctx,
int status, StatementIndex index,
const char* what) const;