client.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __DATA_SOURCE_CLIENT_H
  15. #define __DATA_SOURCE_CLIENT_H 1
  16. #include <boost/noncopyable.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <exceptions/exceptions.h>
  19. #include <datasrc/zone.h>
  20. /// \file
  21. /// Datasource clients
  22. ///
  23. /// The data source client API is specified in client.h, and provides the
  24. /// functionality to query and modify data in the data sources. There are
  25. /// multiple datasource implementations, and by subclassing DataSourceClient or
  26. /// DatabaseClient, more can be added.
  27. ///
  28. /// All datasources are implemented as loadable modules, with a name of the
  29. /// form "<type>_ds.so". This has been chosen intentionally, to minimize
  30. /// confusion and potential mistakes.
  31. ///
  32. /// In order to use a datasource client backend, the class
  33. /// DataSourceClientContainer is provided in factory.h; this will load the
  34. /// library, set up the instance, and clean everything up once it is destroyed.
  35. ///
  36. /// Access to the actual instance is provided with the getInstance() method
  37. /// in DataSourceClientContainer
  38. ///
  39. /// \note Depending on actual usage, we might consider making the container
  40. /// a transparent abstraction layer, so it can be used as a DataSourceClient
  41. /// directly. This has some other implications though so for now the only access
  42. /// provided is through getInstance()).
  43. ///
  44. /// For datasource backends, we use a dynamically loaded library system (with
  45. /// dlopen()). This library must contain the following things;
  46. /// - A subclass of DataSourceClient or DatabaseClient (which itself is a
  47. /// subclass of DataSourceClient)
  48. /// - A creator function for an instance of that subclass, of the form:
  49. /// \code
  50. /// extern "C" DataSourceClient* createInstance(isc::data::ConstElementPtr cfg);
  51. /// \endcode
  52. /// - A destructor for said instance, of the form:
  53. /// \code
  54. /// extern "C" void destroyInstance(isc::data::DataSourceClient* instance);
  55. /// \endcode
  56. ///
  57. /// See the documentation for the \link DataSourceClient \endlink class for
  58. /// more information on implementing subclasses of it.
  59. ///
  60. namespace isc {
  61. namespace datasrc {
  62. // The iterator.h is not included on purpose, most application won't need it
  63. class ZoneIterator;
  64. typedef boost::shared_ptr<ZoneIterator> ZoneIteratorPtr;
  65. /// \brief The base class of data source clients.
  66. ///
  67. /// This is an abstract base class that defines the common interface for
  68. /// various types of data source clients. A data source client is a top level
  69. /// access point to a data source, allowing various operations on the data
  70. /// source such as lookups, traversing or updates. The client class itself
  71. /// has limited focus and delegates the responsibility for these specific
  72. /// operations to other classes; in general methods of this class act as
  73. /// factories of these other classes.
  74. ///
  75. /// See \link datasrc/client.h datasrc/client.h \endlink for more information
  76. /// on adding datasource implementations.
  77. ///
  78. /// The following derived classes are currently (expected to be) provided:
  79. /// - \c InMemoryClient: A client of a conceptual data source that stores
  80. /// all necessary data in memory for faster lookups
  81. /// - \c DatabaseClient: A client that uses a real database backend (such as
  82. /// an SQL database). It would internally hold a connection to the underlying
  83. /// database system.
  84. ///
  85. /// \note It is intentional that while the term these derived classes don't
  86. /// contain "DataSource" unlike their base class. It's also noteworthy
  87. /// that the naming of the base class is somewhat redundant because the
  88. /// namespace \c datasrc would indicate that it's related to a data source.
  89. /// The redundant naming comes from the observation that namespaces are
  90. /// often omitted with \c using directives, in which case "Client"
  91. /// would be too generic. On the other hand, concrete derived classes are
  92. /// generally not expected to be referenced directly from other modules and
  93. /// applications, so we'll give them more concise names such as InMemoryClient.
  94. ///
  95. /// A single \c DataSourceClient object is expected to handle only a single
  96. /// RR class even if the underlying data source contains records for multiple
  97. /// RR classes. Likewise, (when we support views) a \c DataSourceClient
  98. /// object is expected to handle only a single view.
  99. ///
  100. /// If the application uses multiple threads, each thread will need to
  101. /// create and use a separate DataSourceClient. This is because some
  102. /// database backend doesn't allow multiple threads to share the same
  103. /// connection to the database.
  104. ///
  105. /// \note For a client using an in memory backend, this may result in
  106. /// having a multiple copies of the same data in memory, increasing the
  107. /// memory footprint substantially. Depending on how to support multiple
  108. /// CPU cores for concurrent lookups on the same single data source (which
  109. /// is not fully fixed yet, and for which multiple threads may be used),
  110. /// this design may have to be revisited.
  111. ///
  112. /// This class (and therefore its derived classes) are not copyable.
  113. /// This is because the derived classes would generally contain attributes
  114. /// that are not easy to copy (such as a large size of in memory data or a
  115. /// network connection to a database server). In order to avoid a surprising
  116. /// disruption with a naive copy it's prohibited explicitly. For the expected
  117. /// usage of the client classes the restriction should be acceptable.
  118. ///
  119. /// \todo This class is still not complete. It will need more factory methods,
  120. /// e.g. for (re)loading a zone.
  121. class DataSourceClient : boost::noncopyable {
  122. public:
  123. /// \brief A helper structure to represent the search result of
  124. /// \c find().
  125. ///
  126. /// This is a straightforward pair of the result code and a share pointer
  127. /// to the found zone to represent the result of \c find().
  128. /// We use this in order to avoid overloading the return value for both
  129. /// the result code ("success" or "not found") and the found object,
  130. /// i.e., avoid using \c NULL to mean "not found", etc.
  131. ///
  132. /// This is a simple value class with no internal state, so for
  133. /// convenience we allow the applications to refer to the members
  134. /// directly.
  135. ///
  136. /// See the description of \c find() for the semantics of the member
  137. /// variables.
  138. struct FindResult {
  139. FindResult(result::Result param_code,
  140. const ZoneFinderPtr param_zone_finder) :
  141. code(param_code), zone_finder(param_zone_finder)
  142. {}
  143. const result::Result code;
  144. const ZoneFinderPtr zone_finder;
  145. };
  146. ///
  147. /// \name Constructors and Destructor.
  148. ///
  149. protected:
  150. /// Default constructor.
  151. ///
  152. /// This is intentionally defined as protected as this base class
  153. /// should never be instantiated directly.
  154. ///
  155. /// The constructor of a concrete derived class may throw an exception.
  156. /// This interface does not specify which exceptions can happen (at least
  157. /// at this moment), and the caller should expect any type of exception
  158. /// and react accordingly.
  159. DataSourceClient() {}
  160. public:
  161. /// The destructor.
  162. virtual ~DataSourceClient() {}
  163. //@}
  164. /// Returns a \c ZoneFinder for a zone that best matches the given name.
  165. ///
  166. /// A concrete derived version of this method gets access to its backend
  167. /// data source to search for a zone whose origin gives the longest match
  168. /// against \c name. It returns the search result in the form of a
  169. /// \c FindResult object as follows:
  170. /// - \c code: The result code of the operation.
  171. /// - \c result::SUCCESS: A zone that gives an exact match is found
  172. /// - \c result::PARTIALMATCH: A zone whose origin is a
  173. /// super domain of \c name is found (but there is no exact match)
  174. /// - \c result::NOTFOUND: For all other cases.
  175. /// - \c zone_finder: Pointer to a \c ZoneFinder object for the found zone
  176. /// if one is found; otherwise \c NULL.
  177. ///
  178. /// A specific derived version of this method may throw an exception.
  179. /// This interface does not specify which exceptions can happen (at least
  180. /// at this moment), and the caller should expect any type of exception
  181. /// and react accordingly.
  182. ///
  183. /// \param name A domain name for which the search is performed.
  184. /// \return A \c FindResult object enclosing the search result (see above).
  185. virtual FindResult findZone(const isc::dns::Name& name) const = 0;
  186. /// \brief Returns an iterator to the given zone
  187. ///
  188. /// This allows for traversing the whole zone. The returned object can
  189. /// provide the RRsets one by one.
  190. ///
  191. /// This throws DataSourceError when the zone does not exist in the
  192. /// datasource.
  193. ///
  194. /// The default implementation throws isc::NotImplemented. This allows
  195. /// for easy and fast deployment of minimal custom data sources, where
  196. /// the user/implementator doesn't have to care about anything else but
  197. /// the actual queries. Also, in some cases, it isn't possible to traverse
  198. /// the zone from logic point of view (eg. dynamically generated zone
  199. /// data).
  200. ///
  201. /// It is not fixed if a concrete implementation of this method can throw
  202. /// anything else.
  203. ///
  204. /// \param name The name of zone apex to be traversed. It doesn't do
  205. /// nearest match as findZone.
  206. /// \param separate_rrs If true, the iterator will return each RR as a
  207. /// new RRset object. If false, the iterator will
  208. /// combine consecutive RRs with the name and type
  209. /// into 1 RRset. The capitalization of the RRset will
  210. /// be that of the first RR read, and TTLs will be
  211. /// adjusted to the lowest one found.
  212. /// \return Pointer to the iterator.
  213. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name,
  214. bool separate_rrs = false) const {
  215. // This is here to both document the parameter in doxygen (therefore it
  216. // needs a name) and avoid unused parameter warning.
  217. static_cast<void>(name);
  218. static_cast<void>(separate_rrs);
  219. isc_throw(isc::NotImplemented,
  220. "Data source doesn't support iteration");
  221. }
  222. /// Return an updater to make updates to a specific zone.
  223. ///
  224. /// The RR class of the zone is the one that the client is expected to
  225. /// handle (see the detailed description of this class).
  226. ///
  227. /// If the specified zone is not found via the client, a NULL pointer
  228. /// will be returned; in other words a completely new zone cannot be
  229. /// created using an updater. It must be created beforehand (even if
  230. /// it's an empty placeholder) in a way specific to the underlying data
  231. /// source.
  232. ///
  233. /// Conceptually, the updater will trigger a separate transaction for
  234. /// subsequent updates to the zone within the context of the updater
  235. /// (the actual implementation of the "transaction" may vary for the
  236. /// specific underlying data source). Until \c commit() is performed
  237. /// on the updater, the intermediate updates won't affect the results
  238. /// of other methods (and the result of the object's methods created
  239. /// by other factory methods). Likewise, if the updater is destructed
  240. /// without performing \c commit(), the intermediate updates will be
  241. /// effectively canceled and will never affect other methods.
  242. ///
  243. /// If the underlying data source allows concurrent updates, this method
  244. /// can be called multiple times while the previously returned updater(s)
  245. /// are still active. In this case each updater triggers a different
  246. /// "transaction". Normally it would be for different zones for such a
  247. /// case as handling multiple incoming AXFR streams concurrently, but
  248. /// this interface does not even prohibit an attempt of getting more than
  249. /// one updater for the same zone, as long as the underlying data source
  250. /// allows such an operation (and any conflict resolution is left to the
  251. /// specific derived class implementation).
  252. ///
  253. /// If \c replace is true, any existing RRs of the zone will be
  254. /// deleted on successful completion of updates (after \c commit() on
  255. /// the updater); if it's false, the existing RRs will be
  256. /// intact unless explicitly deleted by \c deleteRRset() on the updater.
  257. ///
  258. /// A data source can be "read only" or can prohibit partial updates.
  259. /// In such cases this method will result in an \c isc::NotImplemented
  260. /// exception unconditionally or when \c replace is false).
  261. ///
  262. /// If \c journaling is true, the data source should store a journal
  263. /// of changes. These can be used later on by, for example, IXFR-out.
  264. /// However, the parameter is a hint only. It might be unable to store
  265. /// them and they would be silently discarded. Or it might need to
  266. /// store them no matter what (for example a git-based data source would
  267. /// store journal implicitly). When the \c journaling is true, it
  268. /// requires that the following update be formatted as IXFR transfer
  269. /// (SOA to be removed, bunch of RRs to be removed, SOA to be added,
  270. /// bunch of RRs to be added, and possibly repeated). However, it is not
  271. /// required that the updater checks that. If it is false, it must not
  272. /// require so and must accept any order of changes.
  273. ///
  274. /// We don't support erasing the whole zone (by replace being true) and
  275. /// saving a journal at the same time. In such situation, BadValue is
  276. /// thrown.
  277. ///
  278. /// \note To avoid throwing the exception accidentally with a lazy
  279. /// implementation, we still keep this method pure virtual without
  280. /// an implementation. All derived classes must explicitly define this
  281. /// method, even if it simply throws the NotImplemented exception.
  282. ///
  283. /// \exception NotImplemented The underlying data source does not support
  284. /// updates.
  285. /// \exception DataSourceError Internal error in the underlying data
  286. /// source.
  287. /// \exception std::bad_alloc Resource allocation failure.
  288. /// \exception BadValue if both replace and journaling are true.
  289. ///
  290. /// \param name The zone name to be updated
  291. /// \param replace Whether to delete existing RRs before making updates
  292. /// \param journaling The zone updater should store a journal of the
  293. /// changes.
  294. ///
  295. /// \return A pointer to the updater; it will be NULL if the specified
  296. /// zone isn't found.
  297. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  298. bool replace, bool journaling = false)
  299. const = 0;
  300. };
  301. }
  302. }
  303. #endif // DATA_SOURCE_CLIENT_H
  304. // Local Variables:
  305. // mode: c++
  306. // End: