2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-30 21:45:37 +00:00

[#1074] implemented connection pool for pgsql host mgr

This commit is contained in:
Razvan Becheriu
2020-01-10 20:57:42 +02:00
parent 45aed498aa
commit def2e03f87
4 changed files with 479 additions and 241 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -17,6 +17,15 @@ namespace dhcp {
/// Forward declaration to the implementation of the @ref PgSqlHostDataSource.
class PgSqlHostDataSourceImpl;
/// @brief Type of pointers to PgSqlHostDataSourceImpl.
typedef boost::shared_ptr<PgSqlHostDataSourceImpl> PgSqlHostDataSourceImplPtr;
/// Forward declaration for the thread context for the manager pool.
class PgSqlHostContext;
/// @brief Type of pointers to contexts.
typedef boost::shared_ptr<PgSqlHostContext> PgSqlHostContextPtr;
/// @brief PostgreSQL Host Data Source
///
/// This class implements the @ref isc::dhcp::BaseHostDataSource interface to
@@ -32,7 +41,7 @@ class PgSqlHostDataSourceImpl;
/// - IDENT_CIRCUIT_ID
/// - IDENT_CLIENT_ID
///
class PgSqlHostDataSource: public BaseHostDataSource {
class PgSqlHostDataSource : public BaseHostDataSource {
public:
/// @brief Constructor
@@ -107,7 +116,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)
///
@@ -123,7 +133,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)
///
@@ -139,7 +150,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.
@@ -154,9 +166,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.
///
@@ -167,8 +179,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.
///
@@ -179,8 +190,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.
///
@@ -192,8 +202,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.
///
@@ -204,8 +213,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.
///
@@ -216,8 +225,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.
///
@@ -236,11 +245,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.
///
@@ -259,11 +267,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.
///
@@ -273,8 +280,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.
///
@@ -286,9 +292,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.
@@ -304,8 +311,8 @@ public:
///
/// @return Const @c Host object using a specified IPv4 address.
/// @throw BadValue is given an IPv6 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.
///
@@ -317,9 +324,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.
///
@@ -327,8 +335,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.
@@ -337,8 +345,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
///
@@ -387,9 +395,34 @@ public:
/// Rolls back all pending database operations.
virtual void rollback();
/// @brief Context RAII Allocator.
class PgSqlHostContextAlloc {
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
PgSqlHostContextAlloc(const PgSqlHostDataSourceImpl& mgr);
/// @brief Destructor
///
/// This destructor puts back the context in the pool.
~PgSqlHostContextAlloc();
/// @brief The context
PgSqlHostContextPtr ctx_;
private:
/// @brief The manager
const PgSqlHostDataSourceImpl& mgr_;
};
private:
/// @brief Pointer to the implementation of the @ref PgSqlHostDataSource.
PgSqlHostDataSourceImpl* impl_;
PgSqlHostDataSourceImplPtr impl_;
};
}

View File

@@ -841,7 +841,7 @@ public:
/// prevent cryptic errors during conversions from NULL
/// to actual values.
isc::asiolink::IOAddress addr(getIPv6Value(r, row, ADDRESS_COL));
IOAddress addr(getIPv6Value(r, row, ADDRESS_COL));
convertFromBytea(r, row, DUID_COL, duid_buffer_, sizeof(duid_buffer_), duid_length_);
DuidPtr duid_ptr(new DUID(duid_buffer_, duid_length_));
@@ -1147,8 +1147,8 @@ protected:
// PgSqlLeaseContext Constructor
PgSqlLeaseContext::PgSqlLeaseContext(
const DatabaseConnection::ParameterMap& parameters) : conn_(parameters) {
PgSqlLeaseContext::PgSqlLeaseContext(const DatabaseConnection::ParameterMap& parameters)
: conn_(parameters) {
}
// PgSqlLeaseContextAlloc Constructor and Destructor
@@ -1216,7 +1216,6 @@ PgSqlLeaseMgr::~PgSqlLeaseMgr() {
PgSqlLeaseContextPtr
PgSqlLeaseMgr::createContext() const {
PgSqlLeaseContextPtr ctx(new PgSqlLeaseContext(parameters_));
// Open the database.
@@ -1252,7 +1251,7 @@ PgSqlLeaseMgr::getDBVersion() {
}
bool
PgSqlLeaseMgr::addLeaseCommon(PgSqlLeaseContextPtr ctx,
PgSqlLeaseMgr::addLeaseCommon(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
PsqlBindArray& bind_array) {
PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
@@ -1318,7 +1317,7 @@ PgSqlLeaseMgr::addLease(const Lease6Ptr& lease) {
template <typename Exchange, typename LeaseCollection>
void
PgSqlLeaseMgr::getLeaseCollection(PgSqlLeaseContextPtr ctx,
PgSqlLeaseMgr::getLeaseCollection(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
PsqlBindArray& bind_array,
Exchange& exchange,
@@ -1346,7 +1345,7 @@ PgSqlLeaseMgr::getLeaseCollection(PgSqlLeaseContextPtr ctx,
}
void
PgSqlLeaseMgr::getLease(PgSqlLeaseContextPtr ctx,
PgSqlLeaseMgr::getLease(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex, PsqlBindArray& bind_array,
Lease4Ptr& result) const {
// Create appropriate collection object and get all leases matching
@@ -1367,7 +1366,7 @@ PgSqlLeaseMgr::getLease(PgSqlLeaseContextPtr ctx,
}
void
PgSqlLeaseMgr::getLease(PgSqlLeaseContextPtr ctx,
PgSqlLeaseMgr::getLease(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex, PsqlBindArray& bind_array,
Lease6Ptr& result) const {
// Create appropriate collection object and get all leases matching
@@ -1388,7 +1387,7 @@ PgSqlLeaseMgr::getLease(PgSqlLeaseContextPtr ctx,
}
Lease4Ptr
PgSqlLeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
PgSqlLeaseMgr::getLease4(const IOAddress& addr) const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_ADDR4)
.arg(addr.toText());
@@ -1597,7 +1596,7 @@ PgSqlLeaseMgr::getLeases4() const {
}
Lease4Collection
PgSqlLeaseMgr::getLeases4(const asiolink::IOAddress& lower_bound_address,
PgSqlLeaseMgr::getLeases4(const IOAddress& lower_bound_address,
const LeasePageSize& page_size) const {
// Expecting IPv4 address.
if (!lower_bound_address.isV4()) {
@@ -1635,7 +1634,7 @@ PgSqlLeaseMgr::getLeases4(const asiolink::IOAddress& lower_bound_address,
Lease6Ptr
PgSqlLeaseMgr::getLease6(Lease::Type lease_type,
const isc::asiolink::IOAddress& addr) const {
const IOAddress& addr) const {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_GET_ADDR6)
.arg(addr.toText())
.arg(lease_type);
@@ -1824,7 +1823,7 @@ PgSqlLeaseMgr::getLeases6() const {
}
Lease6Collection
PgSqlLeaseMgr::getLeases6(const asiolink::IOAddress& lower_bound_address,
PgSqlLeaseMgr::getLeases6(const IOAddress& lower_bound_address,
const LeasePageSize& page_size) const {
// Expecting IPv6 address.
if (!lower_bound_address.isV6()) {
@@ -1915,7 +1914,7 @@ PgSqlLeaseMgr::getExpiredLeasesCommon(LeaseCollection& expired_leases,
template<typename LeasePtr>
void
PgSqlLeaseMgr::updateLeaseCommon(PgSqlLeaseContextPtr ctx,
PgSqlLeaseMgr::updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
PsqlBindArray& bind_array,
const LeasePtr& lease) {
@@ -2028,7 +2027,7 @@ PgSqlLeaseMgr::deleteLeaseCommon(StatementIndex stindex,
bool
PgSqlLeaseMgr::deleteLease(const Lease4Ptr& lease) {
const isc::asiolink::IOAddress& addr = lease->addr_;
const IOAddress& addr = lease->addr_;
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_PGSQL_DELETE_ADDR)
.arg(addr.toText());
@@ -2062,7 +2061,7 @@ PgSqlLeaseMgr::deleteLease(const Lease4Ptr& lease) {
bool
PgSqlLeaseMgr::deleteLease(const Lease6Ptr& lease) {
const isc::asiolink::IOAddress& addr = lease->addr_;
const IOAddress& addr = lease->addr_;
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
DHCPSRV_PGSQL_DELETE_ADDR)
.arg(addr.toText());

View File

@@ -668,8 +668,9 @@ private:
///
/// @throw isc::db::DbOperationError An operation on the open database has
/// failed.
bool addLeaseCommon(PgSqlLeaseContextPtr ctx,
StatementIndex stindex, db::PsqlBindArray& bind_array);
bool addLeaseCommon(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array);
/// @brief Get Lease Collection Common Code
///
@@ -693,7 +694,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(PgSqlLeaseContextPtr ctx,
void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array,
Exchange& exchange, LeaseCollection& result,
@@ -716,7 +717,7 @@ private:
/// failed.
/// @throw isc::db::MultipleRecords Multiple records were retrieved
/// from the database where only one was expected.
void getLeaseCollection(PgSqlLeaseContextPtr ctx,
void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array,
Lease4Collection& result) const {
@@ -739,7 +740,7 @@ private:
/// failed.
/// @throw isc::db::MultipleRecords Multiple records were retrieved
/// from the database where only one was expected.
void getLeaseCollection(PgSqlLeaseContextPtr ctx,
void getLeaseCollection(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array,
Lease6Collection& result) const {
@@ -756,7 +757,7 @@ private:
/// @param stindex Index of statement being executed
/// @param bind_array array for input parameters
/// @param result Lease4 object returned
void getLease(PgSqlLeaseContextPtr ctx,
void getLease(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array,
Lease4Ptr& result) const;
@@ -771,7 +772,7 @@ private:
/// @param stindex Index of statement being executed
/// @param bind_array array for input parameters
/// @param result Lease6 object returned
void getLease(PgSqlLeaseContextPtr ctx,
void getLease(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array,
Lease6Ptr& result) const;
@@ -812,7 +813,7 @@ private:
/// @throw isc::db::DbOperationError An operation on the open database has
/// failed.
template <typename LeasePtr>
void updateLeaseCommon(PgSqlLeaseContextPtr ctx,
void updateLeaseCommon(PgSqlLeaseContextPtr& ctx,
StatementIndex stindex,
db::PsqlBindArray& bind_array,
const LeasePtr& lease);