cfg_expiration.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright (C) 2015,2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef CFG_EXPIRATION_H
  7. #define CFG_EXPIRATION_H
  8. #include <asiolink/interval_timer.h>
  9. #include <cc/cfg_to_element.h>
  10. #include <dhcpsrv/timer_mgr.h>
  11. #include <boost/bind.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <stdint.h>
  14. #include <string>
  15. namespace isc {
  16. namespace dhcp {
  17. /// @brief Holds configuration parameters pertaining to lease expiration
  18. /// and lease affinity.
  19. ///
  20. /// This class holds the values of the following configuration parameters:
  21. ///
  22. /// - reclaim-timer-wait-time - is the time between two cycles of processing
  23. /// expired leases, expressed in seconds, i.e. the time between the end of
  24. /// one cycle and the beginning of the next cycle. If this value is 0, the
  25. /// expired leases are not processed.
  26. ///
  27. /// - flush-reclaimed-timer-wait-time - is the time between two cycles of
  28. /// recycling "expired-reclaimed" leases, expressed in seconds. If this
  29. /// value is 0, the expired leases are removed by the leases reclamation
  30. /// routine rather than recycling function. The recycling function is not
  31. /// executed and the value of the "hold-reclaimed-time" is ignored.
  32. ///
  33. /// - hold-reclaimed-time -is the time for which "expired-reclaimed" leases
  34. /// are held in the lease database in the "expired-reclaimed" state after
  35. /// they expire. If this time is set to 0, the recycling function is not
  36. /// executed and the value of the "recycle-timer-wait-time" is ignored.
  37. /// This value is expressed in seconds.
  38. ///
  39. /// - max-reclaim-leases - is the maximum number of leases to be processed
  40. /// in a single cycle. If this value is 0, all expired leases are
  41. /// processed in a single cycle, unless the maximum processing time
  42. /// (configured with the "max-time") parameter elapses first.
  43. ///
  44. /// - max-reclaim-time - the maximum time that a single processing cycle
  45. /// may last, expressed in milliseconds. If this value is 0, there is no
  46. /// limitation for the maximum processing time. This value is expressed
  47. /// in milliseconds.
  48. ///
  49. /// - unwarned-reclaim-cycles - is the number of consecutive processing
  50. /// cycles of expired leases, after which the system issues a warning if
  51. /// there are still expired leases in the database. If this value is 0,
  52. /// the warning is never issued.
  53. ///
  54. /// The @c CfgExpiration class provides a collection of accessors and
  55. /// modifiers to manage the data. Each accessor checks if the given value
  56. /// is in range allowed for this value.
  57. class CfgExpiration : public isc::data::CfgToElement {
  58. public:
  59. /// @name Default values.
  60. //@{
  61. ///
  62. /// @brief Default value for reclaim-timer-wait-time.
  63. static const uint16_t DEFAULT_RECLAIM_TIMER_WAIT_TIME;
  64. /// @brief Default value for flush-reclaimed-timer-wait-time.
  65. static const uint16_t DEFAULT_FLUSH_RECLAIMED_TIMER_WAIT_TIME;
  66. /// @brief Default value for hold-reclaimed-time.
  67. static const uint32_t DEFAULT_HOLD_RECLAIMED_TIME;
  68. /// @brief Default value for max-reclaim-leases.
  69. static const uint32_t DEFAULT_MAX_RECLAIM_LEASES;
  70. /// @brief Default value for max-reclaim-time.
  71. static const uint16_t DEFAULT_MAX_RECLAIM_TIME;
  72. /// @brief Default value for unwarned-reclaim-cycles.
  73. static const uint16_t DEFAULT_UNWARNED_RECLAIM_CYCLES;
  74. //@}
  75. /// @name Upper limits for the parameters
  76. //@{
  77. ///
  78. /// @brief Maximum value for reclaim-timer-wait-time.
  79. static const uint16_t LIMIT_RECLAIM_TIMER_WAIT_TIME;
  80. /// @brief Maximum value for flush-reclaimed-timer-wait-time.
  81. static const uint16_t LIMIT_FLUSH_RECLAIMED_TIMER_WAIT_TIME;
  82. /// @brief Maximum value for hold-reclaimed-time.
  83. static const uint32_t LIMIT_HOLD_RECLAIMED_TIME;
  84. /// @brief Maximum value for max-reclaim-leases.
  85. static const uint32_t LIMIT_MAX_RECLAIM_LEASES;
  86. /// @brief Defalt value for max-reclaim-time.
  87. static const uint16_t LIMIT_MAX_RECLAIM_TIME;
  88. /// @brief Maximum value for unwarned-reclaim-cycles.
  89. static const uint16_t LIMIT_UNWARNED_RECLAIM_CYCLES;
  90. //@}
  91. /// @name Timers' names
  92. //@{
  93. /// @brief Name of the timer for reclaiming expired leases.
  94. static const std::string RECLAIM_EXPIRED_TIMER_NAME;
  95. /// @brief Name of the timer for flushing reclaimed leases.
  96. static const std::string FLUSH_RECLAIMED_TIMER_NAME;
  97. //@}
  98. /// @brief Constructor.
  99. ///
  100. /// Sets all parameters to their defaults.
  101. ///
  102. /// @param test_mode Indicates if the instance should be created in the
  103. /// test mode. In this mode the intervals for the timers are considered to
  104. /// be specified in milliseconds, rather than seconds. This facilitates
  105. /// testing execution of timers without the delays.
  106. CfgExpiration(const bool test_mode = false);
  107. /// @brief Returns reclaim-timer-wait-time
  108. uint16_t getReclaimTimerWaitTime() const {
  109. return (reclaim_timer_wait_time_);
  110. }
  111. /// @brief Sets reclaim-timer-wait-time
  112. ///
  113. /// @param reclaim_timer_wait_time New value.
  114. void setReclaimTimerWaitTime(const int64_t reclaim_timer_wait_time);
  115. /// @brief Returns flush-reclaimed-timer-wait-time.
  116. uint16_t getFlushReclaimedTimerWaitTime() const {
  117. return (flush_reclaimed_timer_wait_time_);
  118. }
  119. /// @brief Sets flush-reclaimed-timer-wait-time.
  120. ///
  121. /// @param flush_reclaimed_wait_time New value.
  122. void setFlushReclaimedTimerWaitTime(const int64_t flush_reclaimed_wait_time);
  123. /// @brief Returns hold-reclaimed-time.
  124. uint32_t getHoldReclaimedTime() const {
  125. return (hold_reclaimed_time_);
  126. }
  127. /// @brief Sets hold-reclaimed-time
  128. ///
  129. /// @param hold_reclaimed_time New value.
  130. void setHoldReclaimedTime(const int64_t hold_reclaimed_time);
  131. /// @brief Returns max-reclaim-leases.
  132. uint32_t getMaxReclaimLeases() const {
  133. return (max_reclaim_leases_);
  134. }
  135. /// @brief Sets max-reclaim-leases.
  136. ///
  137. /// @param max_reclaim_leases New value.
  138. void setMaxReclaimLeases(const int64_t max_reclaim_leases);
  139. /// @brief Returns max-reclaim-time.
  140. uint16_t getMaxReclaimTime() const {
  141. return (max_reclaim_time_);
  142. }
  143. /// @brief Sets max-reclaim-time.
  144. ///
  145. /// @param max_reclaim_time New value.
  146. void setMaxReclaimTime(const int64_t max_reclaim_time);
  147. /// @brief Returns unwarned-reclaim-cycles.
  148. uint16_t getUnwarnedReclaimCycles() const {
  149. return (unwarned_reclaim_cycles_);
  150. }
  151. /// @brief Sets unwarned-reclaim-cycles.
  152. ///
  153. /// @param unwarned_reclaim_cycles New value.
  154. void setUnwarnedReclaimCycles(const int64_t unwarned_reclaim_cycles);
  155. /// @brief Setup timers for the reclamation of expired leases according
  156. /// to the configuration parameters.
  157. ///
  158. /// This method includes the logic for setting the interval timers
  159. /// performing the reclamation of the expired leases and the removal
  160. /// of expired-reclaimed leases.
  161. ///
  162. /// The following is the sample code illustrating how to call this function
  163. /// to setup the leases reclamation for the DHCPv4 server.
  164. /// @code
  165. /// CfgExpiration cfg;
  166. ///
  167. /// (set some cfg values here)
  168. ///
  169. /// AllocEnginePtr alloc_engine(new AllocEngine(...));
  170. /// cfg.setupTimers(&AllocEngine::reclaimExpiredLeases4,
  171. /// &AllocEngine::deleteExpiredReclaimedLeases4,
  172. /// alloc_engine.get());
  173. /// @endcode
  174. ///
  175. /// @param reclaim_fun Pointer to the leases reclamation routine.
  176. /// @param delete_fun Pointer to the function which removes the
  177. /// expired-reclaimed leases from the lease database.
  178. /// @param instance_ptr Pointer to the instance of the object which
  179. /// implements the lease reclamation routine. Typically it will be
  180. /// the pointer to the @c AllocEngine. In case of unit tests it
  181. /// will be a pointer to some test class which provides stub
  182. /// implementation of the leases reclamation routines.
  183. /// @tparam Instance Instance of the object in which both functions
  184. /// are implemented.
  185. template<typename Instance>
  186. void setupTimers(void (Instance::*reclaim_fun)(const size_t, const uint16_t,
  187. const bool, const uint16_t),
  188. void (Instance::*delete_fun)(const uint32_t),
  189. Instance* instance_ptr) const;
  190. /// @brief Unparse a configuration object
  191. ///
  192. /// @return a pointer to unparsed configuration
  193. virtual isc::data::ElementPtr toElement() const;
  194. private:
  195. /// @brief Checks if the value being set by one of the modifiers is
  196. /// within an allowed range.
  197. ///
  198. /// @param value Value to be checked.
  199. /// @param max_value Maximum allowed value.
  200. /// @param config_parameter_name A name of the configuration parameter
  201. /// (used for logging purposes if value is out of range).
  202. ///
  203. /// @throw isc::OutOfRange if the value is negative or greater than
  204. /// the maximum value.
  205. void rangeCheck(const int64_t value, const uint64_t max_value,
  206. const std::string& config_parameter_name) const;
  207. /// @brief reclaim-timer-wait-time
  208. uint16_t reclaim_timer_wait_time_;
  209. /// @brief flush-reclaimed-timer-wait-time
  210. uint16_t flush_reclaimed_timer_wait_time_;
  211. /// @brief hold-reclaimed-time
  212. uint32_t hold_reclaimed_time_;
  213. /// @brief max-reclaim-leases
  214. uint32_t max_reclaim_leases_;
  215. /// @brief max-reclaim-time
  216. uint16_t max_reclaim_time_;
  217. /// @brief unwarned-reclaim-cycles.
  218. uint16_t unwarned_reclaim_cycles_;
  219. /// @brief Pointer to the instance of the Timer Manager.
  220. TimerMgrPtr timer_mgr_;
  221. /// @brief Indicates if the instance is in the test mode.
  222. bool test_mode_;
  223. };
  224. /// @name Pointers to the @c CfgExpiration objects.
  225. //@{
  226. /// @brief Pointer to the Non-const object.
  227. typedef boost::shared_ptr<CfgExpiration> CfgExpirationPtr;
  228. /// @brief Pointer to the const object.
  229. typedef boost::shared_ptr<const CfgExpiration> ConstCfgExpirationPtr;
  230. //@}
  231. template<typename Instance>
  232. void
  233. CfgExpiration::setupTimers(void (Instance::*reclaim_fun)(const size_t,
  234. const uint16_t,
  235. const bool,
  236. const uint16_t),
  237. void (Instance::*delete_fun)(const uint32_t),
  238. Instance* instance_ptr) const {
  239. // One of the parameters passed to the leases' reclamation routine
  240. // is a boolean value which indicates if reclaimed leases should
  241. // be removed by the leases' reclamation routine. This is the case
  242. // when the timer for flushing reclaimed leases is set to 0
  243. // (disabled).
  244. const bool flush_timer_disabled = (getFlushReclaimedTimerWaitTime() == 0);
  245. // If the timer interval for the leases reclamation is non-zero
  246. // the timer will be scheduled.
  247. if (getReclaimTimerWaitTime() > 0) {
  248. // In the test mode the interval is expressed in milliseconds.
  249. // If this is not the test mode, the interval is in seconds.
  250. const long reclaim_interval = test_mode_ ? getReclaimTimerWaitTime() :
  251. 1000 * getReclaimTimerWaitTime();
  252. // Register timer for leases' reclamation routine.
  253. timer_mgr_->registerTimer(RECLAIM_EXPIRED_TIMER_NAME,
  254. boost::bind(reclaim_fun, instance_ptr,
  255. getMaxReclaimLeases(),
  256. getMaxReclaimTime(),
  257. flush_timer_disabled,
  258. getUnwarnedReclaimCycles()),
  259. reclaim_interval,
  260. asiolink::IntervalTimer::ONE_SHOT);
  261. timer_mgr_->setup(RECLAIM_EXPIRED_TIMER_NAME);
  262. }
  263. // If the interval for the timer flusing expired-reclaimed leases
  264. // is set we will schedule the timer.
  265. if (!flush_timer_disabled) {
  266. // The interval is specified in milliseconds if we're in the test mode.
  267. // It is specified in seconds otherwise.
  268. const long flush_interval = test_mode_ ?
  269. getFlushReclaimedTimerWaitTime() :
  270. 1000 * getFlushReclaimedTimerWaitTime();
  271. // Register and setup the timer.
  272. timer_mgr_->registerTimer(FLUSH_RECLAIMED_TIMER_NAME,
  273. boost::bind(delete_fun, instance_ptr,
  274. getHoldReclaimedTime()),
  275. flush_interval,
  276. asiolink::IntervalTimer::ONE_SHOT);
  277. timer_mgr_->setup(FLUSH_RECLAIMED_TIMER_NAME);
  278. }
  279. }
  280. } // end of isc::dhcp namespace
  281. } // end of isc namespace
  282. #endif // CFG_EXPIRATION_H