client.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <utility>
  17. #include <boost/noncopyable.hpp>
  18. #include <boost/shared_ptr.hpp>
  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. /// Return an updater to make updates to a specific zone.
  216. ///
  217. /// The RR class of the zone is the one that the client is expected to
  218. /// handle (see the detailed description of this class).
  219. ///
  220. /// If the specified zone is not found via the client, a NULL pointer
  221. /// will be returned; in other words a completely new zone cannot be
  222. /// created using an updater. It must be created beforehand (even if
  223. /// it's an empty placeholder) in a way specific to the underlying data
  224. /// source.
  225. ///
  226. /// Conceptually, the updater will trigger a separate transaction for
  227. /// subsequent updates to the zone within the context of the updater
  228. /// (the actual implementation of the "transaction" may vary for the
  229. /// specific underlying data source). Until \c commit() is performed
  230. /// on the updater, the intermediate updates won't affect the results
  231. /// of other methods (and the result of the object's methods created
  232. /// by other factory methods). Likewise, if the updater is destructed
  233. /// without performing \c commit(), the intermediate updates will be
  234. /// effectively canceled and will never affect other methods.
  235. ///
  236. /// If the underlying data source allows concurrent updates, this method
  237. /// can be called multiple times while the previously returned updater(s)
  238. /// are still active. In this case each updater triggers a different
  239. /// "transaction". Normally it would be for different zones for such a
  240. /// case as handling multiple incoming AXFR streams concurrently, but
  241. /// this interface does not even prohibit an attempt of getting more than
  242. /// one updater for the same zone, as long as the underlying data source
  243. /// allows such an operation (and any conflict resolution is left to the
  244. /// specific derived class implementation).
  245. ///
  246. /// If \c replace is true, any existing RRs of the zone will be
  247. /// deleted on successful completion of updates (after \c commit() on
  248. /// the updater); if it's false, the existing RRs will be
  249. /// intact unless explicitly deleted by \c deleteRRset() on the updater.
  250. ///
  251. /// A data source can be "read only" or can prohibit partial updates.
  252. /// In such cases this method will result in an \c isc::NotImplemented
  253. /// exception unconditionally or when \c replace is false).
  254. ///
  255. /// If \c journaling is true, the data source should store a journal
  256. /// of changes. These can be used later on by, for example, IXFR-out.
  257. /// However, the parameter is a hint only. It might be unable to store
  258. /// them and they would be silently discarded. Or it might need to
  259. /// store them no matter what (for example a git-based data source would
  260. /// store journal implicitly). When the \c journaling is true, it
  261. /// requires that the following update be formatted as IXFR transfer
  262. /// (SOA to be removed, bunch of RRs to be removed, SOA to be added,
  263. /// bunch of RRs to be added, and possibly repeated). However, it is not
  264. /// required that the updater checks that. If it is false, it must not
  265. /// require so and must accept any order of changes.
  266. ///
  267. /// We don't support erasing the whole zone (by replace being true) and
  268. /// saving a journal at the same time. In such situation, BadValue is
  269. /// thrown.
  270. ///
  271. /// \note To avoid throwing the exception accidentally with a lazy
  272. /// implementation, we still keep this method pure virtual without
  273. /// an implementation. All derived classes must explicitly define this
  274. /// method, even if it simply throws the NotImplemented exception.
  275. ///
  276. /// \exception NotImplemented The underlying data source does not support
  277. /// updates.
  278. /// \exception DataSourceError Internal error in the underlying data
  279. /// source.
  280. /// \exception std::bad_alloc Resource allocation failure.
  281. /// \exception BadValue if both replace and journaling are true.
  282. ///
  283. /// \param name The zone name to be updated
  284. /// \param replace Whether to delete existing RRs before making updates
  285. /// \param journaling The zone updater should store a journal of the
  286. /// changes.
  287. ///
  288. /// \return A pointer to the updater; it will be NULL if the specified
  289. /// zone isn't found.
  290. virtual ZoneUpdaterPtr getUpdater(const isc::dns::Name& name,
  291. bool replace, bool journaling = false)
  292. const = 0;
  293. /// Return a journal reader to retrieve differences of a zone.
  294. ///
  295. /// A derived version of this method creates a concrete
  296. /// \c ZoneJournalReader object specific to the underlying data source
  297. /// for the specified name of zone and differences between the versions
  298. /// specified by the beginning and ending serials of the corresponding
  299. /// SOA RRs.
  300. /// The RR class of the zone is the one that the client is expected to
  301. /// handle (see the detailed description of this class).
  302. ///
  303. /// Note that the SOA serials are compared by the semantics of the serial
  304. /// number arithmetic. So, for example, \c begin_serial can be larger than
  305. /// \c end_serial as bare unsigned integers. The underlying data source
  306. /// implementation is assumed to keep track of sufficient history to
  307. /// identify (if exist) the corresponding difference between the specified
  308. /// versions.
  309. ///
  310. /// This method returns the result as a pair of a result code and
  311. /// a pointer to a \c ZoneJournalReader object. On success, the result
  312. /// code is \c SUCCESS and the pointer must be non NULL; otherwise
  313. /// the result code is something other than \c SUCCESS and the pinter
  314. /// must be NULL.
  315. ///
  316. /// If the specified zone is not found in the data source, the result
  317. /// code is \c NO_SUCH_ZONE.
  318. /// Otherwise, if specified range of difference for the zone is not found
  319. /// in the data source, the result code is \c NO_SUCH_VERSION.
  320. ///
  321. /// Handling differences is an optional feature of data source.
  322. /// If the underlying data source does not support difference handling,
  323. /// this method for that type of data source can throw an exception of
  324. /// class \c NotImplemented.
  325. ///
  326. /// \exception NotImplemented The data source does not support differences.
  327. /// \exception DataSourceError Other operational errors at the data source
  328. /// level.
  329. ///
  330. /// \param zone The name of the zone for which the difference should be
  331. /// retrieved.
  332. /// \param begin_serial The SOA serial of the beginning version of the
  333. /// differences.
  334. /// \param end_serial The SOA serial of the ending version of the
  335. /// differences.
  336. ///
  337. /// \return A pair of result code and a pointer to \c ZoneJournalReader.
  338. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  339. getJournalReader(const isc::dns::Name& zone, uint32_t begin_serial,
  340. uint32_t end_serial) const = 0;
  341. /// Return the number of zones currently known to this datasource
  342. ///
  343. /// This is an optional convenience method, currently only implemented
  344. /// by the InMemory datasource. By default, it throws NotImplemented
  345. ///
  346. /// \note This is a tentative API, and this method is likely to change
  347. /// or be removed in the near future. For that reason, it currently
  348. /// provides a default implementation that throws NotImplemented.
  349. ///
  350. /// \exception NotImplemented Thrown if this method is not supported
  351. /// by the datasource
  352. ///
  353. /// \return The number of zones known to this datasource
  354. virtual unsigned int getZoneCount() const;
  355. /// \brief Create a zone in the database
  356. ///
  357. /// Creates a new (empty) zone in the data source backend, which
  358. /// can subsequently be filled with data (through getUpdater()).
  359. ///
  360. /// \note This is a tentative API, and this method is likely to change
  361. /// or be removed in the near future. For that reason, it currently
  362. /// provides a default implementation that throws NotImplemented.
  363. ///
  364. /// Apart from the two exceptions mentioned below, in theory this
  365. /// call can throw anything, depending on the implementation of
  366. /// the datasource backend.
  367. ///
  368. /// \throw NotImplemented If the datasource backend does not support
  369. /// direct zone creation.
  370. /// \throw DataSourceError If something goes wrong in the data source
  371. /// while creating the zone.
  372. /// \param name The (fully qualified) name of the zone to create
  373. /// \return True if the zone was added, false if it already existed
  374. virtual bool createZone(const dns::Name& name);
  375. };
  376. }
  377. }
  378. #endif // DATA_SOURCE_CLIENT_H
  379. // Local Variables:
  380. // mode: c++
  381. // End: