Kea 2.7.1
isc::dhcp::PgSqlHostDataSourceImpl Class Reference

Implementation of the PgSqlHostDataSource. More...

Public Types

enum  StatementIndex {
  GET_HOST_DHCPID , GET_HOST_ADDR , GET_HOST_SUBID4_DHCPID , GET_HOST_SUBID6_DHCPID ,
  GET_HOST_SUBID_ADDR , GET_HOST_PREFIX , GET_HOST_SUBID6_ADDR , GET_HOST_ADDR6 ,
  GET_HOST_SUBID4 , GET_HOST_SUBID6 , GET_HOST_HOSTNAME , GET_HOST_HOSTNAME_SUBID4 ,
  GET_HOST_HOSTNAME_SUBID6 , GET_HOST_SUBID4_PAGE , GET_HOST_SUBID6_PAGE , GET_HOST_PAGE4 ,
  GET_HOST_PAGE6 , INSERT_HOST_NON_UNIQUE_IP , INSERT_HOST_UNIQUE_IP , INSERT_V6_RESRV_NON_UNIQUE ,
  INSERT_V6_RESRV_UNIQUE , INSERT_V4_HOST_OPTION , INSERT_V6_HOST_OPTION , DEL_HOST_ADDR4 ,
  DEL_HOST_ADDR6 , DEL_HOST_SUBID4_ID , DEL_HOST_SUBID6_ID , NUM_STATEMENTS
}
 Statement Tags. More...
 

Public Member Functions

 PgSqlHostDataSourceImpl (const DatabaseConnection::ParameterMap &parameters)
 Constructor.
 
 ~PgSqlHostDataSourceImpl ()
 Destructor.
 
void addOption (PgSqlHostContextPtr &ctx, const PgSqlHostDataSourceImpl::StatementIndex &stindex, const OptionDescriptor &opt_desc, const std::string &opt_space, const Optional< SubnetID > &subnet_id, const HostID &host_id)
 Inserts a single DHCP option into the database.
 
void addOptions (PgSqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id)
 Inserts multiple options into the database.
 
void addResv (PgSqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id)
 Inserts IPv6 Reservation into ipv6_reservation table.
 
uint64_t addStatement (PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind, const bool return_last_id=false)
 Executes statements which insert a row into one of the tables.
 
void checkReadOnly (PgSqlHostContextPtr &ctx) const
 Throws exception if database is read only.
 
PgSqlHostContextPtr createContext () const
 Create a new context.
 
bool delStatement (PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind)
 Executes statements that delete records.
 
ConstHostPtr getHost (PgSqlHostContextPtr &ctx, const SubnetID &subnet_id, const Host::IdentifierType &identifier_type, const uint8_t *identifier_begin, const size_t identifier_len, StatementIndex stindex, boost::shared_ptr< PgSqlHostExchange > exchange) const
 Retrieves a host by subnet and client's unique identifier.
 
void getHostCollection (PgSqlHostContextPtr &ctx, StatementIndex stindex, PsqlBindArrayPtr bind, boost::shared_ptr< PgSqlHostExchange > exchange, ConstHostCollection &result, bool single) const
 Creates collection of Host objects with associated information such as IPv6 reservations and/or DHCP options.
 
std::pair< uint32_t, uint32_t > getVersion (const std::string &timer_name=std::string()) const
 Returns PostgreSQL schema version of the open database.
 

Static Public Member Functions

static bool dbReconnect (ReconnectCtlPtr db_reconnect_ctl)
 Attempts to reconnect the server to the host DB backend manager.
 

Public Attributes

bool ip_reservations_unique_
 Holds the setting whether the IP reservations must be unique or may be non-unique.
 
DatabaseConnection::ParameterMap parameters_
 The parameters.
 
PgSqlHostContextPoolPtr pool_
 The pool of contexts.
 
std::string timer_name_
 Timer name used to register database reconnect timer.
 
bool unusable_
 Indicates if there is at least one connection that can no longer be used for normal operations.
 

Static Public Attributes

static const StatementIndex WRITE_STMTS_BEGIN = INSERT_HOST_NON_UNIQUE_IP
 Index of first statement performing write to the database.
 

Detailed Description

Implementation of the PgSqlHostDataSource.

Definition at line 1391 of file pgsql_host_data_source.cc.

Member Enumeration Documentation

◆ StatementIndex

Statement Tags.

The contents of the enum are indexes into the list of SQL statements. It is assumed that the order is such that the indices of statements reading the database are less than those of statements modifying the database.

Note
: please add new statements doing read only operations before the WRITE_STMTS_BEGIN position.
Enumerator
GET_HOST_DHCPID 
GET_HOST_ADDR 
GET_HOST_SUBID4_DHCPID 
GET_HOST_SUBID6_DHCPID 
GET_HOST_SUBID_ADDR 
GET_HOST_PREFIX 
GET_HOST_SUBID6_ADDR 
GET_HOST_ADDR6 
GET_HOST_SUBID4 
GET_HOST_SUBID6 
GET_HOST_HOSTNAME 
GET_HOST_HOSTNAME_SUBID4 
GET_HOST_HOSTNAME_SUBID6 
GET_HOST_SUBID4_PAGE 
GET_HOST_SUBID6_PAGE 
GET_HOST_PAGE4 
GET_HOST_PAGE6 
INSERT_HOST_NON_UNIQUE_IP 
INSERT_HOST_UNIQUE_IP 
INSERT_V6_RESRV_NON_UNIQUE 
INSERT_V6_RESRV_UNIQUE 
INSERT_V4_HOST_OPTION 
INSERT_V6_HOST_OPTION 
DEL_HOST_ADDR4 
DEL_HOST_ADDR6 
DEL_HOST_SUBID4_ID 
DEL_HOST_SUBID6_ID 
NUM_STATEMENTS 

Definition at line 1402 of file pgsql_host_data_source.cc.

Constructor & Destructor Documentation

◆ PgSqlHostDataSourceImpl()

isc::dhcp::PgSqlHostDataSourceImpl::PgSqlHostDataSourceImpl ( const DatabaseConnection::ParameterMap & parameters)

◆ ~PgSqlHostDataSourceImpl()

isc::dhcp::PgSqlHostDataSourceImpl::~PgSqlHostDataSourceImpl ( )

Destructor.

Definition at line 2350 of file pgsql_host_data_source.cc.

Member Function Documentation

◆ addOption()

void isc::dhcp::PgSqlHostDataSourceImpl::addOption ( PgSqlHostContextPtr & ctx,
const PgSqlHostDataSourceImpl::StatementIndex & stindex,
const OptionDescriptor & opt_desc,
const std::string & opt_space,
const Optional< SubnetID > & subnet_id,
const HostID & host_id )

Inserts a single DHCP option into the database.

Parameters
ctxContext
stindexIndex of a statement being executed.
opt_descOption descriptor holding information about an option to be inserted into the database.
opt_spaceOption space name.
subnet_idSubnet identifier.
host_idHost identifier.

Definition at line 2515 of file pgsql_host_data_source.cc.

References addStatement().

Referenced by addOptions().

+ Here is the call graph for this function:

◆ addOptions()

void isc::dhcp::PgSqlHostDataSourceImpl::addOptions ( PgSqlHostContextPtr & ctx,
const StatementIndex & stindex,
const ConstCfgOptionPtr & options_cfg,
const uint64_t host_id )

Inserts multiple options into the database.

Parameters
ctxContext
stindexIndex of a statement being executed.
options_cfgAn object holding a collection of options to be inserted into the database.
host_idHost identifier retrieved using getColumnValue in addStatement method

Definition at line 2527 of file pgsql_host_data_source.cc.

References addOption().

+ Here is the call graph for this function:

◆ addResv()

void isc::dhcp::PgSqlHostDataSourceImpl::addResv ( PgSqlHostContextPtr & ctx,
const IPv6Resrv & resv,
const HostID & id )

Inserts IPv6 Reservation into ipv6_reservation table.

Parameters
ctxContext
resvIPv6 Reservation to be added
idID of a host owning this reservation

Definition at line 2503 of file pgsql_host_data_source.cc.

References addStatement(), INSERT_V6_RESRV_NON_UNIQUE, INSERT_V6_RESRV_UNIQUE, and ip_reservations_unique_.

+ Here is the call graph for this function:

◆ addStatement()

uint64_t isc::dhcp::PgSqlHostDataSourceImpl::addStatement ( PgSqlHostContextPtr & ctx,
PgSqlHostDataSourceImpl::StatementIndex stindex,
PsqlBindArrayPtr & bind,
const bool return_last_id = false )

Executes statements which insert a row into one of the tables.

Parameters
ctxContext
stindexIndex of a statement being executed.
bindVector of PgsqlBindArray objects to be used for the query
return_last_idflag indicating whether or not the insert returns the primary key of from the row inserted via " RETURNING <primary key> as pid" clause on the INSERT statement. The RETURNING clause causes the INSERT to return a result set that should consist of a single row with one column, the value of the primary key. Defaults to false.
Returns
0 if return_last_id is false, otherwise it returns the the value in the result set in the first col of the first row.
Exceptions
isc::db::DuplicateEntryDatabase throws duplicate entry error

Definition at line 2427 of file pgsql_host_data_source.cc.

References isc::db::PgSqlConnection::DUPLICATE_KEY, isc::db::PgSqlExchange::getColumnValue(), and isc_throw.

Referenced by addOption(), and addResv().

+ Here is the call graph for this function:

◆ checkReadOnly()

void isc::dhcp::PgSqlHostDataSourceImpl::checkReadOnly ( PgSqlHostContextPtr & ctx) const

Throws exception if database is read only.

This method should be called by the methods which write to the database. If the backend is operating in read-only mode this method will throw exception.

Parameters
ctxContext
Exceptions
DbReadOnlyif backend is operating in read only mode.

Definition at line 2623 of file pgsql_host_data_source.cc.

References isc_throw.

◆ createContext()

PgSqlHostContextPtr isc::dhcp::PgSqlHostDataSourceImpl::createContext ( ) const

Create a new context.

The database is opened with all the SQL commands pre-compiled.

Returns
A new (never null) context.
Exceptions
isc::dhcp::NoDatabaseNameMandatory database name not given.
isc::db::DbOperationErrorAn operation on the open database has failed.

Definition at line 2313 of file pgsql_host_data_source.cc.

References dbReconnect(), isc::dhcp::dhcpsrv_logger, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_READONLY, isc::db::DatabaseConnection::getIOService(), LOG_INFO, parameters_, timer_name_, and WRITE_STMTS_BEGIN.

Referenced by isc::dhcp::PgSqlHostDataSource::PgSqlHostContextAlloc::PgSqlHostContextAlloc(), and PgSqlHostDataSourceImpl().

+ Here is the call graph for this function:

◆ dbReconnect()

bool isc::dhcp::PgSqlHostDataSourceImpl::dbReconnect ( ReconnectCtlPtr db_reconnect_ctl)
static

Attempts to reconnect the server to the host DB backend manager.

This is a self-rescheduling function that attempts to reconnect to the server's host DB backends after connectivity to one or more have been lost. Upon entry it will attempt to reconnect via HostDataSourceFactory::add. If this is successful, DHCP servicing is re-enabled and server returns to normal operation.

If reconnection fails and the maximum number of retries has not been exhausted, it will schedule a call to itself to occur at the configured retry interval. DHCP service remains disabled.

If the maximum number of retries has been exhausted an error is logged and the server shuts down.

This function is passed to the connection recovery mechanism. It will be invoked when a connection loss is detected.

Parameters
db_reconnect_ctlpointer to the ReconnectCtl containing the configured reconnect parameters.
Returns
true if connection has been recovered, false otherwise.

Definition at line 2354 of file pgsql_host_data_source.cc.

References isc::dhcp::HostMgr::addBackend(), dbReconnect(), isc::dhcp::HostMgr::delBackend(), isc::dhcp::dhcpsrv_logger, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_FAILED, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_RECONNECT_ATTEMPT_SCHEDULE, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_RECONNECT_FAILED, isc::dhcp::CfgMgr::instance(), isc::dhcp::TimerMgr::instance(), isc::db::DatabaseConnection::invokeDbFailedCallback(), isc::db::DatabaseConnection::invokeDbLostCallback(), isc::db::DatabaseConnection::invokeDbRecoveredCallback(), LOG_ERROR, LOG_INFO, isc::asiolink::IntervalTimer::ONE_SHOT, and isc::db::DatabaseConnection::parse().

Referenced by PgSqlHostDataSourceImpl(), createContext(), dbReconnect(), and getVersion().

+ Here is the call graph for this function:

◆ delStatement()

bool isc::dhcp::PgSqlHostDataSourceImpl::delStatement ( PgSqlHostContextPtr & ctx,
PgSqlHostDataSourceImpl::StatementIndex stindex,
PsqlBindArrayPtr & bind )

Executes statements that delete records.

Parameters
ctxContext
stindexIndex of a statement being executed.
bindpointer to PsqlBindArray objects to be used for the query
Returns
true if any records were deleted, false otherwise

Definition at line 2475 of file pgsql_host_data_source.cc.

References isc_throw.

◆ getHost()

ConstHostPtr isc::dhcp::PgSqlHostDataSourceImpl::getHost ( PgSqlHostContextPtr & ctx,
const SubnetID & subnet_id,
const Host::IdentifierType & identifier_type,
const uint8_t * identifier_begin,
const size_t identifier_len,
StatementIndex stindex,
boost::shared_ptr< PgSqlHostExchange > exchange ) const

Retrieves a host by subnet and client's unique identifier.

This method is used by both PgSqlHostDataSource::get4 and PgSqlHostDataSource::get6 methods.

Parameters
ctxContext
subnet_idSubnet identifier.
identifier_typeIdentifier type.
identifier_beginPointer to a beginning of a buffer containing an identifier.
identifier_lenIdentifier length.
stindexStatement index.
exchangePointer to the exchange object used for the particular query.
Returns
Pointer to const instance of Host or null pointer if no host found.

Definition at line 2580 of file pgsql_host_data_source.cc.

References getHostCollection().

+ Here is the call graph for this function:

◆ getHostCollection()

void isc::dhcp::PgSqlHostDataSourceImpl::getHostCollection ( PgSqlHostContextPtr & ctx,
StatementIndex stindex,
PsqlBindArrayPtr bind,
boost::shared_ptr< PgSqlHostExchange > exchange,
ConstHostCollection & result,
bool single ) const

Creates collection of Host objects with associated information such as IPv6 reservations and/or DHCP options.

This method performs a query which returns host information from the 'hosts' table. The query may also use LEFT JOIN clause to retrieve information from other tables, e.g. ipv6_reservations, dhcp4_options and dhcp6_options. Whether IPv6 reservations and/or options are assigned to the Host objects depends on the type of the exchange object.

Parameters
ctxContext
stindexStatement index.
bindPointer to an array of PgSQL bindings.
exchangePointer to the exchange object used for the particular query.
[out]resultReference to the collection of hosts returned.
singleA boolean value indicating if a single host is expected to be returned, or multiple hosts.

Definition at line 2551 of file pgsql_host_data_source.cc.

References isc_throw.

Referenced by getHost().

◆ getVersion()

std::pair< uint32_t, uint32_t > isc::dhcp::PgSqlHostDataSourceImpl::getVersion ( const std::string & timer_name = std::string()) const

Returns PostgreSQL schema version of the open database.

Parameters
timer_nameThe DB reconnect timer name.
Returns
Version number stored in the database, as a pair of unsigned integers. "first" is the major version number, "second" the minor number.
Exceptions
isc::db::DbOperationErrorAn operation on the open database has failed.

Definition at line 2613 of file pgsql_host_data_source.cc.

References dbReconnect(), isc::dhcp::DHCPSRV_DBG_TRACE_DETAIL, isc::dhcp::dhcpsrv_logger, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_GET_VERSION, isc::db::DatabaseConnection::getIOService(), isc::db::PgSqlConnection::getVersion(), LOG_DEBUG, and parameters_.

+ Here is the call graph for this function:

Member Data Documentation

◆ ip_reservations_unique_

bool isc::dhcp::PgSqlHostDataSourceImpl::ip_reservations_unique_

Holds the setting whether the IP reservations must be unique or may be non-unique.

Definition at line 1631 of file pgsql_host_data_source.cc.

Referenced by addResv().

◆ parameters_

DatabaseConnection::ParameterMap isc::dhcp::PgSqlHostDataSourceImpl::parameters_

The parameters.

Definition at line 1627 of file pgsql_host_data_source.cc.

Referenced by PgSqlHostDataSourceImpl(), createContext(), and getVersion().

◆ pool_

PgSqlHostContextPoolPtr isc::dhcp::PgSqlHostDataSourceImpl::pool_

◆ timer_name_

std::string isc::dhcp::PgSqlHostDataSourceImpl::timer_name_

Timer name used to register database reconnect timer.

Definition at line 1641 of file pgsql_host_data_source.cc.

Referenced by PgSqlHostDataSourceImpl(), and createContext().

◆ unusable_

bool isc::dhcp::PgSqlHostDataSourceImpl::unusable_

Indicates if there is at least one connection that can no longer be used for normal operations.

Definition at line 1638 of file pgsql_host_data_source.cc.

◆ WRITE_STMTS_BEGIN

const StatementIndex isc::dhcp::PgSqlHostDataSourceImpl::WRITE_STMTS_BEGIN = INSERT_HOST_NON_UNIQUE_IP
static

Index of first statement performing write to the database.

This value is used to mark border line between queries and other statements and statements performing write operation on the database, such as INSERT, DELETE, UPDATE.

Definition at line 1438 of file pgsql_host_data_source.cc.

Referenced by createContext().


The documentation for this class was generated from the following file: