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

[#2445] implement PostgreSQL lease manager methods for limit checking

This commit is contained in:
Andrei Pavel
2022-06-27 11:08:33 +03:00
parent 91a644d032
commit f5419843a1
4 changed files with 68 additions and 55 deletions

View File

@@ -3135,6 +3135,28 @@ MySqlLeaseMgr::checkLimits6(ConstElementPtr const& user_context) const {
return checkLimits(user_context, CHECK_LEASE6_LIMITS);
}
bool
MySqlLeaseMgr::isJsonSupported() const {
// Get a context.
MySqlLeaseContextAlloc get_context(*this);
MySqlLeaseContextPtr ctx = get_context.ctx_;
// Create bindings.
MySqlBindingCollection in_bindings;
MySqlBindingCollection out_bindings({
MySqlBinding::createBool()
});
// Execute the select.
bool json_supported(false);
ctx->conn_.selectQuery(IS_JSON_SUPPORTED, in_bindings, out_bindings,
[&json_supported] (MySqlBindingCollection const& result) {
json_supported = result[0]->getBool();
});
return json_supported;
}
LeaseStatsQueryPtr
MySqlLeaseMgr::startLeaseStatsQuery4() {
// Get a context
@@ -3231,28 +3253,6 @@ MySqlLeaseMgr::wipeLeases6(const SubnetID& /*subnet_id*/) {
isc_throw(NotImplemented, "wipeLeases6 is not implemented for MySQL backend");
}
bool
MySqlLeaseMgr::isJsonSupported() const {
// Get a context.
MySqlLeaseContextAlloc get_context(*this);
MySqlLeaseContextPtr ctx = get_context.ctx_;
// Create bindings.
MySqlBindingCollection in_bindings;
MySqlBindingCollection out_bindings({
MySqlBinding::createBool()
});
// Execute the select.
bool json_supported(false);
ctx->conn_.selectQuery(IS_JSON_SUPPORTED, in_bindings, out_bindings,
[&json_supported] (MySqlBindingCollection const& result) {
json_supported = result[0]->getBool();
});
return json_supported;
}
// Miscellaneous database methods.
std::string

View File

@@ -638,12 +638,6 @@ public:
/// @return number of leases removed.
virtual size_t wipeLeases6(const SubnetID& subnet_id) override;
/// @brief Checks if JSON support is enabled in the database.
/// MySQL implementation.
///
/// @return true if there is JSON support, false otherwise
virtual bool isJsonSupported() const override;
/// @brief Return backend type
///
/// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
@@ -976,6 +970,12 @@ private:
virtual std::string
checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
/// @brief Checks if JSON support is enabled in the database.
/// MySQL implementation.
///
/// @return true if there is JSON support, false otherwise
bool isJsonSupported() const override;
/// @brief Check Error and Throw Exception
///
/// This method invokes @ref MySqlConnection::checkError.

View File

@@ -359,17 +359,20 @@ PgSqlTaggedStatement tagged_statements[] = {
" WHERE subnet_id >= $1 and subnet_id <= $2 "
" ORDER BY subnet_id, lease_type, state" },
// TODO: remove single quotes from the following two SELECTs when the functions are implemented
// CHECK_LEASE4_LIMITS
{ 1, { OID_TEXT },
"check_lease4_limits",
"SELECT 'checkLease4Limits($1)'" },
"SELECT checkLease4Limits($1)" },
// CHECK_LEASE6_LIMITS
{ 1, { OID_TEXT },
"check_lease6_limits",
"SELECT 'checkLease6Limits($1)'" },
"SELECT checkLease6Limits($1)" },
// IS_JSON_SUPPORTED
{ 0, { OID_NONE },
"is_json_supported",
"SELECT isJsonSupported()" },
// End of list sentinel
{ 0, { 0 }, NULL, NULL}
@@ -2290,31 +2293,29 @@ PgSqlLeaseMgr::deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
string
PgSqlLeaseMgr::checkLimits(ConstElementPtr const& user_context, StatementIndex const stindex) const {
// Bindings
PsqlBindArray bind_array;
std::string const user_context_str(user_context->str());
bind_array.add(user_context_str);
// No user context means no limits means allocation allowed means empty string.
if (!user_context) {
return string();
}
// Get a context.
PgSqlLeaseContextAlloc get_context(*this);
PgSqlLeaseContextPtr ctx(get_context.ctx_);
// Create bindings.
PsqlBindArray bind_array;
std::string const user_context_str(user_context->str());
bind_array.add(user_context_str);
// Execute the select.
PgSqlResult r(PQexecPrepared(ctx->conn_,
tagged_statements[stindex].name,
tagged_statements[stindex].nbparams,
&bind_array.values_[0],
&bind_array.lengths_[0],
&bind_array.formats_[0], 0));
ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
int rows = PQntuples(r);
if (rows > 1) {
isc_throw(MultipleRecords, "multiple records were found in the "
"database where only one was expected for query "
<< tagged_statements[stindex].name);
}
std::string const limits(PgSqlExchange::getRawColumnValue(r, 0, 0));
return limits;
}
@@ -2329,6 +2330,22 @@ PgSqlLeaseMgr::checkLimits6(ConstElementPtr const& user_context) const {
return checkLimits(user_context, CHECK_LEASE6_LIMITS);
}
bool
PgSqlLeaseMgr::isJsonSupported() const {
// Get a context.
PgSqlLeaseContextAlloc get_context(*this);
PgSqlLeaseContextPtr ctx(get_context.ctx_);
// Execute the select.
StatementIndex const stindex(IS_JSON_SUPPORTED);
PgSqlResult r(PQexecPrepared(ctx->conn_, tagged_statements[stindex].name,
0, 0, 0, 0, 0));
ctx->conn_.checkStatementError(r, tagged_statements[stindex]);
bool const json_supported(PgSqlExchange::getRawColumnValue(r, 0, 0));
return json_supported;
}
LeaseStatsQueryPtr
PgSqlLeaseMgr::startLeaseStatsQuery4() {
// Get a context
@@ -2425,11 +2442,6 @@ PgSqlLeaseMgr::wipeLeases6(const SubnetID& /*subnet_id*/) {
isc_throw(NotImplemented, "wipeLeases6 is not implemented for PostgreSQL backend");
}
bool
PgSqlLeaseMgr::isJsonSupported() const {
isc_throw(NotImplemented, "PgSqlLeaseMgr::isJsonSupported() not implemented");
}
std::string
PgSqlLeaseMgr::getName() const {
// Get a context

View File

@@ -613,12 +613,6 @@ public:
/// @return number of leases removed.
virtual size_t wipeLeases6(const SubnetID& subnet_id) override;
/// @brief Checks if JSON support is enabled in the database.
/// PostgreSQL implementation.
///
/// @return true if there is JSON support, false otherwise
virtual bool isJsonSupported() const override;
/// @brief Return backend type
///
/// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
@@ -707,6 +701,7 @@ public:
SUBNET_RANGE_LEASE6_STATS, // Fetched IPv6 lease stats for a subnet range.
CHECK_LEASE4_LIMITS, // Check if allocated IPv4 leases are inside the set limits.
CHECK_LEASE6_LIMITS, // Check if allocated IPv6 leases are inside the set limits.
IS_JSON_SUPPORTED, // Checks if JSON support is enabled in the database.
NUM_STATEMENTS // Number of statements
};
@@ -949,6 +944,12 @@ private:
virtual std::string
checkLimits6(isc::data::ConstElementPtr const& user_context) const override;
/// @brief Checks if JSON support is enabled in the database.
/// PostgreSQL implementation.
///
/// @return true if there is JSON support, false otherwise
bool isJsonSupported() const override;
/// @brief Context RAII Allocator.
class PgSqlLeaseContextAlloc {
public: