Kea
1.9.9-git
|
Implementation of the PgSqlHostDataSource. More...
Public Member Functions | |
PgSqlHostDataSourceImpl (const DatabaseConnection::ParameterMap ¶meters) | |
Constructor. More... | |
~PgSqlHostDataSourceImpl () | |
Destructor. More... | |
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. More... | |
void | addOptions (PgSqlHostContextPtr &ctx, const StatementIndex &stindex, const ConstCfgOptionPtr &options_cfg, const uint64_t host_id) |
Inserts multiple options into the database. More... | |
void | addResv (PgSqlHostContextPtr &ctx, const IPv6Resrv &resv, const HostID &id) |
Inserts IPv6 Reservation into ipv6_reservation table. More... | |
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. More... | |
void | checkReadOnly (PgSqlHostContextPtr &ctx) const |
Throws exception if database is read only. More... | |
PgSqlHostContextPtr | createContext () const |
Create a new context. More... | |
bool | delStatement (PgSqlHostContextPtr &ctx, PgSqlHostDataSourceImpl::StatementIndex stindex, PsqlBindArrayPtr &bind) |
Executes statements that delete records. More... | |
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. More... | |
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. More... | |
std::pair< uint32_t, uint32_t > | getVersion () const |
Returns PostgreSQL schema version of the open database. More... | |
Static Public Member Functions | |
static bool | dbReconnect (ReconnectCtlPtr db_reconnect_ctl) |
Attempts to reconnect the server to the host DB backend manager. More... | |
Public Attributes | |
bool | ip_reservations_unique_ |
Holds the setting whether the IP reservations must be unique or may be non-unique. More... | |
DatabaseConnection::ParameterMap | parameters_ |
The parameters. More... | |
PgSqlHostContextPoolPtr | pool_ |
The pool of contexts. More... | |
std::string | timer_name_ |
Timer name used to register database reconnect timer. More... | |
bool | unusable_ |
Indicates if there is at least one connection that can no longer be used for normal operations. More... | |
Static Public Attributes | |
static const StatementIndex | WRITE_STMTS_BEGIN = INSERT_HOST_NON_UNIQUE_IP |
Index of first statement performing write to the database. More... | |
Implementation of the PgSqlHostDataSource.
Definition at line 1376 of file pgsql_host_data_source.cc.
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.
Definition at line 1387 of file pgsql_host_data_source.cc.
isc::dhcp::PgSqlHostDataSourceImpl::PgSqlHostDataSourceImpl | ( | const DatabaseConnection::ParameterMap & | parameters | ) |
Constructor.
This constructor opens database connection and initializes prepared statements used in the queries.
Definition at line 2221 of file pgsql_host_data_source.cc.
References createContext(), getVersion(), isc_throw, isc::db::PG_SCHEMA_VERSION_MAJOR, isc::db::PG_SCHEMA_VERSION_MINOR, pool_, and timer_name_.
isc::dhcp::PgSqlHostDataSourceImpl::~PgSqlHostDataSourceImpl | ( | ) |
Destructor.
Definition at line 2287 of file pgsql_host_data_source.cc.
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.
ctx | Context |
stindex | Index of a statement being executed. |
opt_desc | Option descriptor holding information about an option to be inserted into the database. |
opt_space | Option space name. |
subnet_id | Subnet identifier. |
host_id | Host identifier. |
Definition at line 2452 of file pgsql_host_data_source.cc.
References addStatement().
Referenced by 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.
ctx | Context |
stindex | Index of a statement being executed. |
options_cfg | An object holding a collection of options to be inserted into the database. |
host_id | Host identifier retrieved using getColumnValue in addStatement method |
Definition at line 2464 of file pgsql_host_data_source.cc.
References addOption().
void isc::dhcp::PgSqlHostDataSourceImpl::addResv | ( | PgSqlHostContextPtr & | ctx, |
const IPv6Resrv & | resv, | ||
const HostID & | id | ||
) |
Inserts IPv6 Reservation into ipv6_reservation table.
ctx | Context |
resv | IPv6 Reservation to be added |
id | ID of a host owning this reservation |
Definition at line 2440 of file pgsql_host_data_source.cc.
References addStatement(), INSERT_V6_RESRV_NON_UNIQUE, INSERT_V6_RESRV_UNIQUE, and ip_reservations_unique_.
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.
ctx | Context |
stindex | Index of a statement being executed. |
bind | Vector of PgsqlBindArray objects to be used for the query |
return_last_id | flag 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. |
isc::db::DuplicateEntry | Database throws duplicate entry error |
Definition at line 2364 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().
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.
ctx | Context |
DbReadOnly | if backend is operating in read only mode. |
Definition at line 2557 of file pgsql_host_data_source.cc.
References isc_throw.
PgSqlHostContextPtr isc::dhcp::PgSqlHostDataSourceImpl::createContext | ( | ) | const |
Create a new context.
The database is opened with all the SQL commands pre-compiled.
isc::dhcp::NoDatabaseName | Mandatory database name not given. |
isc::db::DbOperationError | An operation on the open database has failed. |
Definition at line 2250 of file pgsql_host_data_source.cc.
References dbReconnect(), isc::dhcp::dhcpsrv_logger, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_READONLY, isc::dhcp::HostMgr::getIOService(), LOG_INFO, parameters_, timer_name_, and WRITE_STMTS_BEGIN.
Referenced by isc::dhcp::PgSqlHostDataSource::PgSqlHostContextAlloc::PgSqlHostContextAlloc(), and PgSqlHostDataSourceImpl().
|
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.
db_reconnect_ctl | pointer to the ReconnectCtl containing the configured reconnect parameters. |
Definition at line 2291 of file pgsql_host_data_source.cc.
References isc::dhcp::HostMgr::addBackend(), 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::getCurrentCfg(), isc::dhcp::TimerMgr::instance(), isc::dhcp::CfgMgr::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 createContext().
bool isc::dhcp::PgSqlHostDataSourceImpl::delStatement | ( | PgSqlHostContextPtr & | ctx, |
PgSqlHostDataSourceImpl::StatementIndex | stindex, | ||
PsqlBindArrayPtr & | bind | ||
) |
Executes statements that delete records.
ctx | Context |
stindex | Index of a statement being executed. |
bind | pointer to PsqlBindArray objects to be used for the query |
Definition at line 2412 of file pgsql_host_data_source.cc.
References isc_throw.
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.
ctx | Context |
subnet_id | Subnet identifier. |
identifier_type | Identifier type. |
identifier_begin | Pointer to a beginning of a buffer containing an identifier. |
identifier_len | Identifier length. |
stindex | Statement index. |
exchange | Pointer to the exchange object used for the particular query. |
Definition at line 2517 of file pgsql_host_data_source.cc.
References 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.
ctx | Context | |
stindex | Statement index. | |
bind | Pointer to an array of PgSQL bindings. | |
exchange | Pointer to the exchange object used for the particular query. | |
[out] | result | Reference to the collection of hosts returned. |
single | A boolean value indicating if a single host is expected to be returned, or multiple hosts. |
Definition at line 2488 of file pgsql_host_data_source.cc.
References isc::db::PgSqlResult::getRows(), and isc_throw.
Referenced by getHost().
std::pair< uint32_t, uint32_t > isc::dhcp::PgSqlHostDataSourceImpl::getVersion | ( | ) | const |
Returns PostgreSQL schema version of the open database.
isc::db::DbOperationError | An operation on the open database has failed. |
Definition at line 2550 of file pgsql_host_data_source.cc.
References isc::dhcp::DHCPSRV_DBG_TRACE_DETAIL, isc::dhcp::dhcpsrv_logger, isc::dhcp::DHCPSRV_PGSQL_HOST_DB_GET_VERSION, isc::db::PgSqlConnection::getVersion(), LOG_DEBUG, and parameters_.
Referenced by PgSqlHostDataSourceImpl().
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 1614 of file pgsql_host_data_source.cc.
Referenced by addResv().
DatabaseConnection::ParameterMap isc::dhcp::PgSqlHostDataSourceImpl::parameters_ |
The parameters.
Definition at line 1610 of file pgsql_host_data_source.cc.
Referenced by createContext(), and getVersion().
PgSqlHostContextPoolPtr isc::dhcp::PgSqlHostDataSourceImpl::pool_ |
The pool of contexts.
Definition at line 1617 of file pgsql_host_data_source.cc.
Referenced by isc::dhcp::PgSqlHostDataSource::PgSqlHostContextAlloc::PgSqlHostContextAlloc(), and PgSqlHostDataSourceImpl().
std::string isc::dhcp::PgSqlHostDataSourceImpl::timer_name_ |
Timer name used to register database reconnect timer.
Definition at line 1624 of file pgsql_host_data_source.cc.
Referenced by createContext(), and PgSqlHostDataSourceImpl().
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 1621 of file pgsql_host_data_source.cc.
|
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 1422 of file pgsql_host_data_source.cc.
Referenced by createContext().