Kea 2.5.8
database_connection.h
Go to the documentation of this file.
1// Copyright (C) 2015-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef DATABASE_CONNECTION_H
8#define DATABASE_CONNECTION_H
9
10#include <asiolink/io_service.h>
11#include <cc/data.h>
12#include <boost/noncopyable.hpp>
13#include <boost/shared_ptr.hpp>
15#include <util/reconnect_ctl.h>
16#include <functional>
17#include <map>
18#include <string>
19
20namespace isc {
21namespace db {
22
24class NoDatabaseName : public Exception {
25public:
26 NoDatabaseName(const char* file, size_t line, const char* what) :
27 isc::Exception(file, line, what) {}
28};
29
31class DbOpenError : public Exception {
32public:
33 DbOpenError(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) {}
35};
36
39public:
40 DbOpenErrorWithRetry(const char* file, size_t line, const char* what) :
41 isc::Exception(file, line, what) {}
42};
43
46public:
47 DbOperationError(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) {}
49};
50
54public:
55 DbConnectionUnusable(const char* file, size_t line, const char* what) :
56 isc::Exception(file, line, what) {}
57};
58
59
63class InvalidType : public Exception {
64public:
65 InvalidType(const char* file, size_t line, const char* what) :
66 isc::Exception(file, line, what) {}
67};
68
73public:
74 DbInvalidTimeout(const char* file, size_t line, const char* what) :
75 isc::Exception(file, line, what) {}
76};
77
81class DbInvalidPort : public Exception {
82public:
83 DbInvalidPort(const char* file, size_t line, const char* what) :
84 isc::Exception(file, line, what) {}
85};
86
91public:
92 DbInvalidReadOnly(const char* file, size_t line, const char* what) :
93 isc::Exception(file, line, what) {}
94};
95
98public:
99 SchemaInitializationFailed(const char* file, size_t line, const char* what) :
100 isc::Exception(file, line, what) {}
101};
102
103
105typedef std::function<bool (util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback;
106
115
117typedef boost::shared_ptr<IOServiceAccessor> IOServiceAccessorPtr;
118
127class DatabaseConnection : public boost::noncopyable {
128public:
129
136 static const time_t MAX_DB_TIME;
137
139 typedef std::map<std::string, std::string> ParameterMap;
140
147 DbCallback callback = DbCallback())
148 : parameters_(parameters), callback_(callback), unusable_(false) {
149 }
150
153
158 virtual void makeReconnectCtl(const std::string& timer_name);
159
164 return (reconnect_ctl_);
165 }
166
172 std::string getParameter(const std::string& name) const;
173
183 static ParameterMap parse(const std::string& dbaccess);
184
193 static std::string redactedAccessString(const ParameterMap& parameters);
194
201 bool configuredReadOnly() const;
202
207 static bool invokeDbLostCallback(const util::ReconnectCtlPtr& db_reconnect_ctl);
208
213 static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr& db_reconnect_ctl);
214
219 static bool invokeDbFailedCallback(const util::ReconnectCtlPtr& db_reconnect_ctl);
220
225 static isc::data::ElementPtr toElement(const ParameterMap& params);
226
231 static isc::data::ElementPtr toElementDbAccessString(const std::string& dbaccess);
232
236 static void setIOService(const isc::asiolink::IOServicePtr& io_service) {
237 io_service_ = io_service;
238 }
239
242 return (io_service_);
243 }
244
248
252
256
263 static bool retry_;
264
268 if (unusable_) {
269 isc_throw (DbConnectionUnusable, "Attempt to use an invalid connection");
270 }
271 }
272
276 bool isUnusable() {
277 return (unusable_);
278 }
279
280protected:
281
283 void markUnusable() { unusable_ = true; }
284
285private:
286
292 ParameterMap parameters_;
293
294protected:
295
298
299private:
300
307 bool unusable_;
308
310 util::ReconnectCtlPtr reconnect_ctl_;
311
313 static isc::asiolink::IOServicePtr io_service_;
314};
315
318public:
324 }
325
331 }
332};
333
334} // namespace db
335} // namespace isc
336
337#endif // DATABASE_CONNECTION_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Common database connection class.
virtual ~DatabaseConnection()
Destructor.
bool configuredReadOnly() const
Convenience method checking if database should be opened with read only access.
std::string getParameter(const std::string &name) const
Returns value of a connection parameter.
util::ReconnectCtlPtr reconnectCtl()
The reconnect settings.
static bool invokeDbLostCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's lost connectivity callback.
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
static void setIOService(const isc::asiolink::IOServicePtr &io_service)
Sets IO service to be used by the database backends.
void markUnusable()
Sets the unusable flag to true.
static bool invokeDbFailedCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restore failed connectivity callback.
static isc::data::ElementPtr toElement(const ParameterMap &params)
Unparse a parameter map.
static isc::data::ElementPtr toElementDbAccessString(const std::string &dbaccess)
Unparse an access string.
static DbCallback db_recovered_callback_
Optional callback function to invoke if an opened connection recovery succeeded.
virtual void makeReconnectCtl(const std::string &timer_name)
Instantiates a ReconnectCtl based on the connection's reconnect parameters.
DatabaseConnection(const ParameterMap &parameters, DbCallback callback=DbCallback())
Constructor.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
static bool retry_
Flag which indicates if the database connection should be retried on fail.
static bool invokeDbRecoveredCallback(const util::ReconnectCtlPtr &db_reconnect_ctl)
Invokes the connection's restored connectivity callback.
static DbCallback db_failed_callback_
Optional callback function to invoke if an opened connection recovery failed.
void checkUnusable()
Throws an exception if the connection is not usable.
static DbCallback db_lost_callback_
Optional callback function to invoke if an opened connection is lost.
bool isUnusable()
Flag which indicates if connection is unusable.
static isc::asiolink::IOServicePtr & getIOService()
Returns pointer to the IO service.
static const time_t MAX_DB_TIME
Defines maximum value for time that can be reliably stored.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
DbCallback callback_
The callback used to recover the connection.
RAII class to enable DB reconnect retries on server startup.
Exception thrown when a specific connection has been rendered unusable either through loss of connect...
DbConnectionUnusable(const char *file, size_t line, const char *what)
Invalid port number.
DbInvalidPort(const char *file, size_t line, const char *what)
Invalid 'readonly' value specification.
DbInvalidReadOnly(const char *file, size_t line, const char *what)
DbInvalidTimeout(const char *file, size_t line, const char *what)
Exception thrown on failure to open database but permit retries.
DbOpenErrorWithRetry(const char *file, size_t line, const char *what)
Exception thrown on failure to open database.
DbOpenError(const char *file, size_t line, const char *what)
Exception thrown on failure to execute a database function.
DbOperationError(const char *file, size_t line, const char *what)
Invalid type exception.
InvalidType(const char *file, size_t line, const char *what)
Exception thrown if name of database is not specified.
NoDatabaseName(const char *file, size_t line, const char *what)
Thrown when an initialization of the schema failed.
SchemaInitializationFailed(const char *file, size_t line, const char *what)
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
boost::shared_ptr< IOServiceAccessor > IOServiceAccessorPtr
Pointer to an instance of IOServiceAccessor.
std::function< bool(util::ReconnectCtlPtr db_reconnect_ctl)> DbCallback
Defines a callback prototype for propagating events upward.
std::function< isc::asiolink::IOServicePtr()> IOServiceAccessor
Function which returns the IOService that can be used to recover the connection.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.