client_list.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // Copyright (C) 2012 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 DATASRC_CONTAINER_H
  15. #define DATASRC_CONTAINER_H
  16. #include <util/memory_segment.h>
  17. #include <dns/name.h>
  18. #include <dns/rrclass.h>
  19. #include <cc/data.h>
  20. #include <exceptions/exceptions.h>
  21. #include "memory/zone_table_segment.h"
  22. #include <vector>
  23. #include <boost/shared_ptr.hpp>
  24. #include <boost/scoped_ptr.hpp>
  25. #include <boost/noncopyable.hpp>
  26. namespace isc {
  27. namespace datasrc {
  28. class ZoneFinder;
  29. typedef boost::shared_ptr<ZoneFinder> ZoneFinderPtr;
  30. class DataSourceClient;
  31. typedef boost::shared_ptr<DataSourceClient> DataSourceClientPtr;
  32. class DataSourceClientContainer;
  33. typedef boost::shared_ptr<DataSourceClientContainer>
  34. DataSourceClientContainerPtr;
  35. // XXX: it's better to even hide the existence of the "memory" namespace.
  36. // We should probably consider pimpl for details of ConfigurableClientList
  37. // and hide real definitions except for itself and tests.
  38. namespace memory {
  39. class InMemoryClient;
  40. }
  41. /// \brief The list of data source clients.
  42. ///
  43. /// The purpose of this class is to hold several data source clients and search
  44. /// through them to find one containing a zone best matching a request.
  45. ///
  46. /// All the data source clients should be for the same class. If you need
  47. /// to handle multiple classes, you need to create multiple separate lists.
  48. ///
  49. /// This is an abstract base class. It is not expected we would use multiple
  50. /// implementation inside the servers (but it is not forbidden either), we
  51. /// have it to allow easy testing. It is possible to create a mock-up class
  52. /// instead of creating a full-blown configuration. The real implementation
  53. /// is the ConfigurableClientList.
  54. class ClientList : public boost::noncopyable {
  55. protected:
  56. /// \brief Constructor.
  57. ///
  58. /// It is protected to prevent accidental creation of the abstract base
  59. /// class.
  60. ClientList() {}
  61. public:
  62. /// \brief Virtual destructor
  63. virtual ~ClientList() {}
  64. /// \brief Structure holding the (compound) result of find.
  65. ///
  66. /// As this is read-only structure, we don't bother to create accessors.
  67. /// Instead, all the member variables are defined as const and can be
  68. /// accessed directly.
  69. struct FindResult {
  70. /// \brief Internal class for holding a reference.
  71. ///
  72. /// This is used to make sure the data source client isn't released
  73. /// too soon.
  74. ///
  75. /// \see life_keeper_;
  76. class LifeKeeper {
  77. public:
  78. virtual ~LifeKeeper() {};
  79. };
  80. /// \brief Constructor.
  81. ///
  82. /// It simply fills in the member variables according to the
  83. /// parameters. See the member descriptions for their meaning.
  84. FindResult(DataSourceClient* dsrc_client, const ZoneFinderPtr& finder,
  85. bool exact_match,
  86. const boost::shared_ptr<LifeKeeper>& life_keeper) :
  87. dsrc_client_(dsrc_client),
  88. finder_(finder),
  89. exact_match_(exact_match),
  90. life_keeper_(life_keeper)
  91. {}
  92. /// \brief Negative answer constructor.
  93. ///
  94. /// This conscructs a result for negative answer. Both pointers are
  95. /// NULL, and exact_match_ is false.
  96. FindResult() :
  97. dsrc_client_(NULL),
  98. exact_match_(false)
  99. {}
  100. /// \brief Comparison operator.
  101. ///
  102. /// It is needed for tests and it might be of some use elsewhere
  103. /// too.
  104. bool operator ==(const FindResult& other) const {
  105. return (dsrc_client_ == other.dsrc_client_ &&
  106. finder_ == other.finder_ &&
  107. exact_match_ == other.exact_match_);
  108. }
  109. /// \brief The found data source client.
  110. ///
  111. /// The client of the data source containing the best matching zone.
  112. /// If no such data source exists, this is NULL pointer.
  113. ///
  114. /// Note that the pointer is valid only as long the ClientList which
  115. /// returned the pointer is alive and was not reconfigured or you hold
  116. /// a reference to life_keeper_. The ownership is preserved within the
  117. /// ClientList.
  118. DataSourceClient* const dsrc_client_;
  119. /// \brief The finder for the requested zone.
  120. ///
  121. /// This is the finder corresponding to the best matching zone.
  122. /// This may be NULL even in case the datasrc_ is something
  123. /// else, depending on the find options.
  124. ///
  125. /// \see find
  126. const ZoneFinderPtr finder_;
  127. /// \brief If the result is an exact match.
  128. const bool exact_match_;
  129. /// \brief Something that holds the dsrc_client_ valid.
  130. ///
  131. /// As long as you hold the life_keeper_, the dsrc_client_ is
  132. /// guaranteed to be valid.
  133. const boost::shared_ptr<LifeKeeper> life_keeper_;
  134. };
  135. /// \brief Search for a zone through the data sources.
  136. ///
  137. /// This searches the contained data source clients for a one that best
  138. /// matches the zone name.
  139. ///
  140. /// There are two expected usage scenarios. One is answering queries. In
  141. /// this case, the zone finder is needed and the best matching superzone
  142. /// of the searched name is needed. Therefore, the call would look like:
  143. ///
  144. /// \code FindResult result(list->find(queried_name));
  145. /// FindResult result(list->find(queried_name));
  146. /// if (result.datasrc_) {
  147. /// createTheAnswer(result.finder_);
  148. /// } else {
  149. /// createNotAuthAnswer();
  150. /// } \endcode
  151. ///
  152. /// The other scenario is manipulating zone data (XfrOut, XfrIn, DDNS,
  153. /// ...). In this case, the finder itself is not so important. However,
  154. /// we need an exact match (if we want to manipulate zone data, we must
  155. /// know exactly, which zone we are about to manipulate). Then the call
  156. ///
  157. /// \code FindResult result(list->find(zone_name, true, false));
  158. /// FindResult result(list->find(zone_name, true, false));
  159. /// if (result.datasrc_) {
  160. /// ZoneUpdaterPtr updater(result.datasrc_->getUpdater(zone_name);
  161. /// ...
  162. /// } \endcode
  163. ///
  164. /// \param zone The name of the zone to look for.
  165. /// \param want_exact_match If it is true, it returns only exact matches.
  166. /// If the best possible match is partial, a negative result is
  167. /// returned instead. It is possible the caller could check it and
  168. /// act accordingly if the result would be partial match, but with this
  169. /// set to true, the find might be actually faster under some
  170. /// circumstances.
  171. /// \param want_finder If this is false, the finder_ member of FindResult
  172. /// might be NULL even if the corresponding data source is found. This
  173. /// is because of performance, in some cases the finder is a side
  174. /// result of the searching algorithm (therefore asking for it again
  175. /// would be a waste), but under other circumstances it is not, so
  176. /// providing it when it is not needed would also be wasteful.
  177. ///
  178. /// Other things are never the side effect of searching, therefore the
  179. /// caller can get them explicitly (the updater, journal reader and
  180. /// iterator).
  181. /// \return A FindResult describing the data source and zone with the
  182. /// longest match against the zone parameter.
  183. virtual FindResult find(const dns::Name& zone,
  184. bool want_exact_match = false,
  185. bool want_finder = true) const = 0;
  186. };
  187. /// \brief Shared pointer to the list.
  188. typedef boost::shared_ptr<ClientList> ClientListPtr;
  189. /// \brief Shared const pointer to the list.
  190. typedef boost::shared_ptr<const ClientList> ConstClientListPtr;
  191. /// \Concrete implementation of the ClientList, which is constructed based on
  192. /// configuration.
  193. ///
  194. /// This is the implementation which is expected to be used in the servers.
  195. /// However, it is expected most of the code will use it as the ClientList,
  196. /// only the creation is expected to be direct.
  197. ///
  198. /// While it is possible to inherit this class, it is not expected to be
  199. /// inherited except for tests.
  200. class ConfigurableClientList : public ClientList {
  201. public:
  202. /// \brief Constructor
  203. ///
  204. /// \param rrclass For which class the list should work.
  205. ConfigurableClientList(const isc::dns::RRClass& rrclass);
  206. /// \brief Destructor
  207. virtual ~ConfigurableClientList();
  208. /// \brief Exception thrown when there's an error in configuration.
  209. class ConfigurationError : public Exception {
  210. public:
  211. ConfigurationError(const char* file, size_t line, const char* what) :
  212. Exception(file, line, what)
  213. {}
  214. };
  215. /// \brief Sets the configuration.
  216. ///
  217. /// This fills the ClientList with data source clients corresponding to the
  218. /// configuration. The data source clients are newly created or recycled
  219. /// from previous configuration.
  220. ///
  221. /// If any error is detected, an exception is thrown and the current
  222. /// configuration is preserved.
  223. ///
  224. /// \param configuration The JSON element describing the configuration to
  225. /// use.
  226. /// \param allow_cache If it is true, the 'cache' option of the
  227. /// configuration is used and some zones are cached into an In-Memory
  228. /// data source according to it. If it is false, it is ignored and
  229. /// no In-Memory data sources are created.
  230. /// \throw DataSourceError if there's a problem creating a data source
  231. /// client.
  232. /// \throw ConfigurationError if the configuration is invalid in some
  233. /// sense.
  234. /// \throw BadValue if configuration is NULL
  235. /// \throw Unexpected if something misbehaves (like the data source
  236. /// returning NULL iterator).
  237. /// \throw NotImplemented if the auto-detection of list of zones is
  238. /// needed.
  239. /// \throw Whatever is propagated from within the data source.
  240. void configure(const isc::data::ConstElementPtr& configuration,
  241. bool allow_cache);
  242. /// \brief Returns the currently active configuration.
  243. ///
  244. /// In case configure was not called yet, it returns an empty
  245. /// list, which corresponds to the default content.
  246. const isc::data::ConstElementPtr& getConfiguration() const {
  247. return (configuration_);
  248. }
  249. /// \brief Result of the reload() method.
  250. enum ReloadResult {
  251. CACHE_DISABLED, ///< The cache is not enabled in this list.
  252. ZONE_NOT_CACHED, ///< Zone is served directly, not from cache.
  253. ZONE_NOT_FOUND, ///< Zone does not exist or not cached.
  254. ZONE_RELOADED ///< The zone was successfully reloaded.
  255. };
  256. /// \brief Reloads a cached zone.
  257. ///
  258. /// This method finds a zone which is loaded into a cache and reloads it.
  259. /// This may be used to renew the cache when the underlying data source
  260. /// changes.
  261. ///
  262. /// \param zone The origin of the zone to reload.
  263. /// \return A status if the command worked.
  264. /// \throw DataSourceError or anything else that the data source
  265. /// containing the zone might throw is propagated.
  266. /// \throw DataSourceError if something unexpected happens, like when
  267. /// the original data source no longer contains the cached zone.
  268. ReloadResult reload(const dns::Name& zone);
  269. /// \brief Implementation of the ClientList::find.
  270. virtual FindResult find(const dns::Name& zone,
  271. bool want_exact_match = false,
  272. bool want_finder = true) const;
  273. /// \brief This holds one data source client and corresponding information.
  274. ///
  275. /// \todo The content yet to be defined.
  276. struct DataSourceInfo {
  277. // Plays a role of default constructor too (for vector)
  278. DataSourceInfo(const dns::RRClass& rrclass, boost::shared_ptr
  279. <isc::datasrc::memory::ZoneTableSegment>& segment,
  280. bool has_cache = false);
  281. DataSourceInfo(DataSourceClient* data_src_client,
  282. const DataSourceClientContainerPtr& container,
  283. bool has_cache, const dns::RRClass& rrclass,
  284. boost::shared_ptr
  285. <isc::datasrc::memory::ZoneTableSegment>& segment);
  286. DataSourceClient* data_src_client_;
  287. DataSourceClientContainerPtr container_;
  288. // Accessor to cache_ in the form of DataSourceClient, hiding
  289. // the existence of InMemoryClient as much as possible. We should
  290. // really consider cleaner abstraction, but for now it works.
  291. // This is also only intended to be used in auth unit tests right now.
  292. // No other applications or tests may use it.
  293. const DataSourceClient* getCacheClient() const;
  294. boost::shared_ptr<memory::InMemoryClient> cache_;
  295. };
  296. /// \brief The collection of data sources.
  297. typedef std::vector<DataSourceInfo> DataSources;
  298. /// \brief Convenience type alias.
  299. ///
  300. /// \see getDataSource
  301. typedef std::pair<DataSourceClient*, DataSourceClientContainerPtr>
  302. DataSourcePair;
  303. /// \brief Create a data source client of given type and configuration.
  304. ///
  305. /// This is a thin wrapper around the DataSourceClientContainer
  306. /// constructor. The function is here to make it possible for tests
  307. /// to replace the DataSourceClientContainer with something else.
  308. /// Also, derived classes could want to create the data source clients
  309. /// in a different way, though inheriting this class is not recommended.
  310. ///
  311. /// The parameters are the same as of the constructor.
  312. /// \return Pair containing both the data source client and the container.
  313. /// The container might be NULL in the derived class, it is
  314. /// only stored so the data source client is properly destroyed when
  315. /// not needed. However, in such case, it is the caller's
  316. /// responsibility to ensure the data source client is deleted when
  317. /// needed.
  318. virtual DataSourcePair getDataSourceClient(const std::string& type,
  319. const data::ConstElementPtr&
  320. configuration);
  321. public:
  322. /// \brief Access to the data source clients.
  323. ///
  324. /// It can be used to examine the loaded list of data sources clients
  325. /// directly. It is not known if it is of any use other than testing, but
  326. /// it might be, so it is just made public (there's no real reason to
  327. /// hide it).
  328. const DataSources& getDataSources() const { return (data_sources_); }
  329. private:
  330. struct MutableResult;
  331. /// \brief Internal implementation of find.
  332. ///
  333. /// The class itself needs to do some internal searches in other methods,
  334. /// so the implementation is shared.
  335. ///
  336. /// The result is returned as parameter because MutableResult is not
  337. /// defined in the header file.
  338. ///
  339. /// If there's no match, the result is not modified. Therefore, this
  340. /// expects to get a fresh result object each time it is called, not
  341. /// to reuse it.
  342. void findInternal(MutableResult& result, const dns::Name& name,
  343. bool want_exact_match, bool want_finder) const;
  344. const isc::dns::RRClass rrclass_;
  345. /// \brief Memory segment for in-memory cache.
  346. ///
  347. /// Note that this must be placed before data_sources_ so it won't be
  348. /// destroyed before the built objects in the destructor.
  349. boost::scoped_ptr<util::MemorySegment> mem_sgmt_;
  350. /// \brief Currently active configuration.
  351. isc::data::ConstElementPtr configuration_;
  352. /// \brief The last set value of allow_cache.
  353. bool allow_cache_;
  354. protected:
  355. /// \brief The data sources held here.
  356. ///
  357. /// All our data sources are stored here. It is protected to let the
  358. /// tests in. You should consider it private if you ever want to
  359. /// derive this class (which is not really recommended anyway).
  360. DataSources data_sources_;
  361. };
  362. } // namespace datasrc
  363. } // namespace isc
  364. #endif // DATASRC_CONTAINER_H