client_list.h 16 KB

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