zone_reloader.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 ZONE_RELOADER_H
  15. #define ZONE_RELOADER_H
  16. #include "load_action.h"
  17. #include <dns/rrclass.h>
  18. #include <dns/name.h>
  19. namespace isc {
  20. namespace datasrc {
  21. namespace memory {
  22. class ZoneData;
  23. class ZoneTableSegment;
  24. /// \brief Does an update to a zone.
  25. ///
  26. /// This abstract base class represents the work of a reload of a zone.
  27. /// The work is divided into three stages -- load(), install() and cleanup().
  28. /// They should be called in this order for the effect to take place.
  29. ///
  30. /// We divide them so the update of zone data can be done asynchronously,
  31. /// in a different thread. The install() operation is the only one that needs
  32. /// to be done in a critical section.
  33. class ZoneReloader {
  34. public:
  35. /// \brief Get the zone data into memory.
  36. ///
  37. /// This is the part that does the time-consuming loading into the memory.
  38. /// This can be run in a separate thread, for example. It has no effect on
  39. /// the data actually served, it only prepares them for future use.
  40. ///
  41. /// This is the first method you should call on the object. Never call it
  42. /// multiple times.
  43. ///
  44. /// \note As this contains reading of files or other data sources, or with
  45. /// some other source of the data to load, it may throw quite anything.
  46. /// If it throws, do not call any other methods on the object and
  47. /// discard it.
  48. /// \note After successful load(), you have to call cleanup() some time
  49. /// later.
  50. /// \throw isc::Unexpected if called second time.
  51. virtual void load() = 0;
  52. /// \brief Put the changes to effect.
  53. ///
  54. /// This replaces the old version of zone with the one previously prepared
  55. /// by load(). It takes ownership of the old zone data, if any.
  56. ///
  57. /// You may call it only after successful load() and at most once.
  58. ///
  59. /// The operation is expected to be fast and is meant to be used inside
  60. /// a critical section.
  61. ///
  62. /// This may throw in rare cases, depending on the concrete implementation.
  63. /// If it throws, you still need to call cleanup().
  64. ///
  65. /// \throw isc::Unexpected if called without previous load() or for the
  66. /// second time or cleanup() was called already.
  67. virtual void install() = 0;
  68. /// \brief Clean up resources.
  69. ///
  70. /// This releases all resources held by owned zone data. That means the
  71. /// one loaded by load() in case install() was not called or was not
  72. /// successful, or the one replaced in install().
  73. ///
  74. /// Generally, this should never throw.
  75. virtual void cleanup() = 0;
  76. };
  77. /// \brief Reloader implementation which loads data locally.
  78. ///
  79. /// This implementation prepares a clean zone data and lets one callback
  80. /// to fill it and another to install it somewhere. The class does mostly
  81. /// nothing (and delegates the work to the callbacks), just stores little bit
  82. /// of state between the calls.
  83. class ZoneReloaderLocal : public ZoneReloader {
  84. public:
  85. /// \brief Constructor
  86. ///
  87. /// \param segment The zone table segment to store the zone into.
  88. /// \param load_action The callback used to load data.
  89. /// \param install_action The callback used to install the loaded zone.
  90. /// \param rrclass The class of the zone.
  91. ZoneReloaderLocal(ZoneTableSegment* segment, const LoadAction& load_action,
  92. const dns::Name& name, const dns::RRClass& rrclass);
  93. /// \brief Destructor
  94. ~ZoneReloaderLocal();
  95. /// \brief Loads the data.
  96. ///
  97. /// This prepares an empty ZoneData and calls load_action (passed to
  98. /// constructor) to fill it with data.
  99. ///
  100. /// \throw std::bad_alloc If there's a problem allocating the ZoneData.
  101. /// \throw isc::Unexpected if it is called the second time in lifetime
  102. /// of the object.
  103. /// \throw Whatever the load_action throws, it is propagated up.
  104. virtual void load();
  105. /// \brief Installs the zone.
  106. ///
  107. /// This simply calls the install_action.
  108. ///
  109. /// \throw isc::Unexpected if it is called the second time in lifetime
  110. /// of the object or if load() was not called previously or if
  111. /// cleanup() was already called.
  112. /// \throw Whatever the install_action throws, it is propagated up.
  113. virtual void install();
  114. /// \brief Clean up memory.
  115. ///
  116. /// Cleans up the memory used by load()ed zone if not yet installed, or
  117. /// the old zone replaced by install().
  118. virtual void cleanup();
  119. private:
  120. ZoneTableSegment* segment_;
  121. LoadAction load_action_;
  122. dns::Name origin_;
  123. dns::RRClass rrclass_;
  124. ZoneData* zone_data_;
  125. // The load was performed
  126. bool loaded_;
  127. // The data are ready to be installed
  128. bool data_ready_;
  129. };
  130. }
  131. }
  132. }
  133. #endif