cfg_expiration.h 13 KB

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