client_list.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. class ZoneWriter;
  41. }
  42. /// \brief Segment status of the cache
  43. ///
  44. /// Describes the status in which the memory segment for the in-memory cache of
  45. // /given data source is.
  46. enum MemorySegmentState {
  47. /// \brief No segment used for this data source.
  48. ///
  49. /// This is usually a result of the cache being disabled.
  50. SEGMENT_UNUSED,
  51. /// \brief It is a mapped segment and we wait for information how to map
  52. /// it.
  53. SEGMENT_WAITING,
  54. /// \brief The segment is ready to be used.
  55. SEGMENT_INUSE
  56. };
  57. /// \brief The type of the memory segment in cache
  58. enum MemorySegmentType {
  59. /// \brief A locally loaded, unshared cache. Normal memory.
  60. SEGMENT_LOCAL,
  61. /// \brief A file image mapped into memory
  62. SEGMENT_FILE
  63. };
  64. /// \brief Status of one data source.
  65. ///
  66. /// This indicates the status a data soure is in. It is used with segment
  67. /// and cache management, to discover the data sources that need external
  68. /// mapping or local loading.
  69. ///
  70. /// In future, it may be extended for other purposes, such as performing an
  71. /// operation on named data source.
  72. class DataSourceStatus {
  73. public:
  74. /// \brief Constructor
  75. ///
  76. /// Sets initial values. It doesn't matter what is provided for the type
  77. /// if state is SEGMENT_UNUSED, the value is effectively ignored.
  78. DataSourceStatus(const std::string& name, MemorySegmentState state,
  79. MemorySegmentType type) :
  80. name_(name),
  81. state_(state),
  82. type_(type)
  83. {}
  84. /// \brief Get the segment state
  85. MemorySegmentState getSegmentState() const {
  86. return (state_);
  87. }
  88. /// \brief Get the segment type
  89. ///
  90. /// \throw isc::BadValue if called and state is SEGMENT_UNUSED.
  91. MemorySegmentType getSegmentType() const {
  92. if (getSegmentState() == SEGMENT_UNUSED) {
  93. isc_throw(isc::BadValue, "No segment used, no type therefore.");
  94. }
  95. return (type_);
  96. }
  97. /// \brief Get the name.
  98. const std::string& getName() const {
  99. return (name_);
  100. }
  101. private:
  102. std::string name_;
  103. MemorySegmentState state_;
  104. MemorySegmentType type_;
  105. };
  106. /// \brief The list of data source clients.
  107. ///
  108. /// The purpose of this class is to hold several data source clients and search
  109. /// through them to find one containing a zone best matching a request.
  110. ///
  111. /// All the data source clients should be for the same class. If you need
  112. /// to handle multiple classes, you need to create multiple separate lists.
  113. ///
  114. /// This is an abstract base class. It is not expected we would use multiple
  115. /// implementation inside the servers (but it is not forbidden either), we
  116. /// have it to allow easy testing. It is possible to create a mock-up class
  117. /// instead of creating a full-blown configuration. The real implementation
  118. /// is the ConfigurableClientList.
  119. class ClientList : public boost::noncopyable {
  120. protected:
  121. /// \brief Constructor.
  122. ///
  123. /// It is protected to prevent accidental creation of the abstract base
  124. /// class.
  125. ClientList() {}
  126. public:
  127. /// \brief Virtual destructor
  128. virtual ~ClientList() {}
  129. /// \brief Structure holding the (compound) result of find.
  130. ///
  131. /// As this is read-only structure, we don't bother to create accessors.
  132. /// Instead, all the member variables are defined as const and can be
  133. /// accessed directly.
  134. struct FindResult {
  135. /// \brief Internal class for holding a reference.
  136. ///
  137. /// This is used to make sure the data source client isn't released
  138. /// too soon.
  139. ///
  140. /// \see life_keeper_;
  141. class LifeKeeper {
  142. public:
  143. virtual ~LifeKeeper() {};
  144. };
  145. /// \brief Constructor.
  146. ///
  147. /// It simply fills in the member variables according to the
  148. /// parameters. See the member descriptions for their meaning.
  149. FindResult(DataSourceClient* dsrc_client, const ZoneFinderPtr& finder,
  150. bool exact_match,
  151. const boost::shared_ptr<LifeKeeper>& life_keeper) :
  152. dsrc_client_(dsrc_client),
  153. finder_(finder),
  154. exact_match_(exact_match),
  155. life_keeper_(life_keeper)
  156. {}
  157. /// \brief Negative answer constructor.
  158. ///
  159. /// This conscructs a result for negative answer. Both pointers are
  160. /// NULL, and exact_match_ is false.
  161. FindResult() :
  162. dsrc_client_(NULL),
  163. exact_match_(false)
  164. {}
  165. /// \brief Comparison operator.
  166. ///
  167. /// It is needed for tests and it might be of some use elsewhere
  168. /// too.
  169. bool operator ==(const FindResult& other) const {
  170. return (dsrc_client_ == other.dsrc_client_ &&
  171. finder_ == other.finder_ &&
  172. exact_match_ == other.exact_match_);
  173. }
  174. /// \brief The found data source client.
  175. ///
  176. /// The client of the data source containing the best matching zone.
  177. /// If no such data source exists, this is NULL pointer.
  178. ///
  179. /// Note that the pointer is valid only as long the ClientList which
  180. /// returned the pointer is alive and was not reconfigured or you hold
  181. /// a reference to life_keeper_. The ownership is preserved within the
  182. /// ClientList.
  183. DataSourceClient* const dsrc_client_;
  184. /// \brief The finder for the requested zone.
  185. ///
  186. /// This is the finder corresponding to the best matching zone.
  187. /// This may be NULL even in case the datasrc_ is something
  188. /// else, depending on the find options.
  189. ///
  190. /// \see find
  191. const ZoneFinderPtr finder_;
  192. /// \brief If the result is an exact match.
  193. const bool exact_match_;
  194. /// \brief Something that holds the dsrc_client_ valid.
  195. ///
  196. /// As long as you hold the life_keeper_, the dsrc_client_ is
  197. /// guaranteed to be valid.
  198. const boost::shared_ptr<LifeKeeper> life_keeper_;
  199. };
  200. /// \brief Search for a zone through the data sources.
  201. ///
  202. /// This searches the contained data source clients for a one that best
  203. /// matches the zone name.
  204. ///
  205. /// There are two expected usage scenarios. One is answering queries. In
  206. /// this case, the zone finder is needed and the best matching superzone
  207. /// of the searched name is needed. Therefore, the call would look like:
  208. ///
  209. /// \code FindResult result(list->find(queried_name));
  210. /// FindResult result(list->find(queried_name));
  211. /// if (result.datasrc_) {
  212. /// createTheAnswer(result.finder_);
  213. /// } else {
  214. /// createNotAuthAnswer();
  215. /// } \endcode
  216. ///
  217. /// The other scenario is manipulating zone data (XfrOut, XfrIn, DDNS,
  218. /// ...). In this case, the finder itself is not so important. However,
  219. /// we need an exact match (if we want to manipulate zone data, we must
  220. /// know exactly, which zone we are about to manipulate). Then the call
  221. ///
  222. /// \code FindResult result(list->find(zone_name, true, false));
  223. /// FindResult result(list->find(zone_name, true, false));
  224. /// if (result.datasrc_) {
  225. /// ZoneUpdaterPtr updater(result.datasrc_->getUpdater(zone_name);
  226. /// ...
  227. /// } \endcode
  228. ///
  229. /// \param zone The name of the zone to look for.
  230. /// \param want_exact_match If it is true, it returns only exact matches.
  231. /// If the best possible match is partial, a negative result is
  232. /// returned instead. It is possible the caller could check it and
  233. /// act accordingly if the result would be partial match, but with this
  234. /// set to true, the find might be actually faster under some
  235. /// circumstances.
  236. /// \param want_finder If this is false, the finder_ member of FindResult
  237. /// might be NULL even if the corresponding data source is found. This
  238. /// is because of performance, in some cases the finder is a side
  239. /// result of the searching algorithm (therefore asking for it again
  240. /// would be a waste), but under other circumstances it is not, so
  241. /// providing it when it is not needed would also be wasteful.
  242. ///
  243. /// Other things are never the side effect of searching, therefore the
  244. /// caller can get them explicitly (the updater, journal reader and
  245. /// iterator).
  246. /// \return A FindResult describing the data source and zone with the
  247. /// longest match against the zone parameter.
  248. virtual FindResult find(const dns::Name& zone,
  249. bool want_exact_match = false,
  250. bool want_finder = true) const = 0;
  251. };
  252. /// \brief Shared pointer to the list.
  253. typedef boost::shared_ptr<ClientList> ClientListPtr;
  254. /// \brief Shared const pointer to the list.
  255. typedef boost::shared_ptr<const ClientList> ConstClientListPtr;
  256. /// \Concrete implementation of the ClientList, which is constructed based on
  257. /// configuration.
  258. ///
  259. /// This is the implementation which is expected to be used in the servers.
  260. /// However, it is expected most of the code will use it as the ClientList,
  261. /// only the creation is expected to be direct.
  262. ///
  263. /// While it is possible to inherit this class, it is not expected to be
  264. /// inherited except for tests.
  265. class ConfigurableClientList : public ClientList {
  266. public:
  267. /// \brief Constructor
  268. ///
  269. /// \param rrclass For which class the list should work.
  270. ConfigurableClientList(const isc::dns::RRClass& rrclass);
  271. /// \brief Exception thrown when there's an error in configuration.
  272. class ConfigurationError : public Exception {
  273. public:
  274. ConfigurationError(const char* file, size_t line, const char* what) :
  275. Exception(file, line, what)
  276. {}
  277. };
  278. /// \brief Sets the configuration.
  279. ///
  280. /// This fills the ClientList with data source clients corresponding to the
  281. /// configuration. The data source clients are newly created or recycled
  282. /// from previous configuration.
  283. ///
  284. /// If any error is detected, an exception is thrown and the current
  285. /// configuration is preserved.
  286. ///
  287. /// \param configuration The JSON element describing the configuration to
  288. /// use.
  289. /// \param allow_cache If it is true, the 'cache' option of the
  290. /// configuration is used and some zones are cached into an In-Memory
  291. /// data source according to it. If it is false, it is ignored and
  292. /// no In-Memory data sources are created.
  293. /// \throw DataSourceError if there's a problem creating a data source
  294. /// client.
  295. /// \throw ConfigurationError if the configuration is invalid in some
  296. /// sense.
  297. /// \throw BadValue if configuration is NULL
  298. /// \throw Unexpected if something misbehaves (like the data source
  299. /// returning NULL iterator).
  300. /// \throw NotImplemented if the auto-detection of list of zones is
  301. /// needed.
  302. /// \throw Whatever is propagated from within the data source.
  303. void configure(const isc::data::ConstElementPtr& configuration,
  304. bool allow_cache);
  305. /// \brief Returns the currently active configuration.
  306. ///
  307. /// In case configure was not called yet, it returns an empty
  308. /// list, which corresponds to the default content.
  309. const isc::data::ConstElementPtr& getConfiguration() const {
  310. return (configuration_);
  311. }
  312. /// \brief Result of the reload() method.
  313. enum ReloadResult {
  314. CACHE_DISABLED, ///< The cache is not enabled in this list.
  315. ZONE_NOT_CACHED, ///< Zone is served directly, not from cache.
  316. ZONE_NOT_FOUND, ///< Zone does not exist or not cached.
  317. ZONE_SUCCESS ///< The zone was successfully reloaded or
  318. /// the writer provided.
  319. };
  320. /// \brief Reloads a cached zone.
  321. ///
  322. /// This method finds a zone which is loaded into a cache and reloads it.
  323. /// This may be used to renew the cache when the underlying data source
  324. /// changes.
  325. ///
  326. /// \param zone The origin of the zone to reload.
  327. /// \return A status if the command worked.
  328. /// \throw DataSourceError or anything else that the data source
  329. /// containing the zone might throw is propagated.
  330. /// \throw DataSourceError if something unexpected happens, like when
  331. /// the original data source no longer contains the cached zone.
  332. ReloadResult reload(const dns::Name& zone);
  333. private:
  334. /// \brief Convenience type shortcut
  335. typedef boost::shared_ptr<memory::ZoneWriter> ZoneWriterPtr;
  336. public:
  337. /// \brief Return value of getCachedZoneWriter()
  338. ///
  339. /// A pair containing status and the zone writer, for the
  340. /// getCachedZoneWriter() method.
  341. typedef std::pair<ReloadResult, ZoneWriterPtr> ZoneWriterPair;
  342. /// \brief Return a zone writer that can be used to reload a zone.
  343. ///
  344. /// This looks up a cached copy of zone and returns the ZoneWriter
  345. /// that can be used to reload the content of the zone. This can
  346. /// be used instead of reload() -- reload() works synchronously, which
  347. /// is not what is needed every time.
  348. ///
  349. /// \param zone The origin of the zone to reload.
  350. /// \return The result has two parts. The first one is a status describing
  351. /// if it worked or not (and in case it didn't, also why). If the
  352. /// status is ZONE_SUCCESS, the second part contains a shared pointer
  353. /// to the writer. If the status is anything else, the second part is
  354. /// NULL.
  355. /// \throw DataSourceError or anything else that the data source
  356. /// containing the zone might throw is propagated.
  357. /// \throw DataSourceError if something unexpected happens, like when
  358. /// the original data source no longer contains the cached zone.
  359. ZoneWriterPair getCachedZoneWriter(const dns::Name& zone);
  360. /// \brief Implementation of the ClientList::find.
  361. virtual FindResult find(const dns::Name& zone,
  362. bool want_exact_match = false,
  363. bool want_finder = true) const;
  364. /// \brief This holds one data source client and corresponding information.
  365. ///
  366. /// \todo The content yet to be defined.
  367. struct DataSourceInfo {
  368. // Plays a role of default constructor too (for vector)
  369. DataSourceInfo(const dns::RRClass& rrclass,
  370. const boost::shared_ptr
  371. <isc::datasrc::memory::ZoneTableSegment>&
  372. ztable_segment,
  373. bool has_cache = false,
  374. const std::string& name = std::string());
  375. DataSourceInfo(DataSourceClient* data_src_client,
  376. const DataSourceClientContainerPtr& container,
  377. bool has_cache, const dns::RRClass& rrclass,
  378. const boost::shared_ptr
  379. <isc::datasrc::memory::ZoneTableSegment>&
  380. ztable_segment, const std::string& name);
  381. DataSourceClient* data_src_client_;
  382. DataSourceClientContainerPtr container_;
  383. // Accessor to cache_ in the form of DataSourceClient, hiding
  384. // the existence of InMemoryClient as much as possible. We should
  385. // really consider cleaner abstraction, but for now it works.
  386. // This is also only intended to be used in auth unit tests right now.
  387. // No other applications or tests may use it.
  388. const DataSourceClient* getCacheClient() const;
  389. boost::shared_ptr<memory::InMemoryClient> cache_;
  390. boost::shared_ptr<memory::ZoneTableSegment> ztable_segment_;
  391. std::string name_;
  392. };
  393. /// \brief The collection of data sources.
  394. typedef std::vector<DataSourceInfo> DataSources;
  395. /// \brief Convenience type alias.
  396. ///
  397. /// \see getDataSource
  398. typedef std::pair<DataSourceClient*, DataSourceClientContainerPtr>
  399. DataSourcePair;
  400. /// \brief Create a data source client of given type and configuration.
  401. ///
  402. /// This is a thin wrapper around the DataSourceClientContainer
  403. /// constructor. The function is here to make it possible for tests
  404. /// to replace the DataSourceClientContainer with something else.
  405. /// Also, derived classes could want to create the data source clients
  406. /// in a different way, though inheriting this class is not recommended.
  407. ///
  408. /// The parameters are the same as of the constructor.
  409. /// \return Pair containing both the data source client and the container.
  410. /// The container might be NULL in the derived class, it is
  411. /// only stored so the data source client is properly destroyed when
  412. /// not needed. However, in such case, it is the caller's
  413. /// responsibility to ensure the data source client is deleted when
  414. /// needed.
  415. virtual DataSourcePair getDataSourceClient(const std::string& type,
  416. const data::ConstElementPtr&
  417. configuration);
  418. /// \brief Get status information of all internal data sources.
  419. ///
  420. /// Get a DataSourceStatus for current state of each data source client
  421. /// in this list.
  422. ///
  423. /// This may throw standad exceptions, such as std::bad_alloc. Otherwise,
  424. /// it is exception free.
  425. std::vector<DataSourceStatus> getStatus() const;
  426. public:
  427. /// \brief Access to the data source clients.
  428. ///
  429. /// It can be used to examine the loaded list of data sources clients
  430. /// directly. It is not known if it is of any use other than testing, but
  431. /// it might be, so it is just made public (there's no real reason to
  432. /// hide it).
  433. const DataSources& getDataSources() const { return (data_sources_); }
  434. private:
  435. struct MutableResult;
  436. /// \brief Internal implementation of find.
  437. ///
  438. /// The class itself needs to do some internal searches in other methods,
  439. /// so the implementation is shared.
  440. ///
  441. /// The result is returned as parameter because MutableResult is not
  442. /// defined in the header file.
  443. ///
  444. /// If there's no match, the result is not modified. Therefore, this
  445. /// expects to get a fresh result object each time it is called, not
  446. /// to reuse it.
  447. void findInternal(MutableResult& result, const dns::Name& name,
  448. bool want_exact_match, bool want_finder) const;
  449. const isc::dns::RRClass rrclass_;
  450. /// \brief Currently active configuration.
  451. isc::data::ConstElementPtr configuration_;
  452. /// \brief The last set value of allow_cache.
  453. bool allow_cache_;
  454. protected:
  455. /// \brief The data sources held here.
  456. ///
  457. /// All our data sources are stored here. It is protected to let the
  458. /// tests in. You should consider it private if you ever want to
  459. /// derive this class (which is not really recommended anyway).
  460. DataSources data_sources_;
  461. };
  462. /// \brief Shortcut typedef for maps of client_lists.
  463. typedef boost::shared_ptr<std::map<
  464. isc::dns::RRClass, boost::shared_ptr<ConfigurableClientList> > >
  465. ClientListMapPtr;
  466. } // namespace datasrc
  467. } // namespace isc
  468. #endif // DATASRC_CONTAINER_H