client_list.h 20 KB

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