1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
// Copyright (C) 2016-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <asiolink/interval_timer.h>
#include <asiolink/io_service.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <dhcpsrv/timer_mgr.h>
#include <exceptions/exceptions.h>
#include <util/multi_threading_mgr.h>
#include <boost/scoped_ptr.hpp>
#include <exception>
#include <functional>
#include <map>
#include <mutex>
#include <ostream>
#include <string>
#include <utility>
#include <stddef.h>
using namespace isc;
using namespace isc::asiolink;
using namespace isc::util;
namespace {
/// @brief Structure holding information for a single timer.
///
/// This structure holds the instance of the watch socket being used to
/// signal that the timer is "ready". It also holds the instance of the
/// interval timer and other parameters pertaining to it.
struct TimerInfo {
/// @brief Instance of the interval timer.
asiolink::IntervalTimer interval_timer_;
/// @brief Holds the pointer to the callback supplied when registering
/// the timer.
asiolink::IntervalTimer::Callback user_callback_;
/// @brief Interval timer interval supplied during registration.
long interval_;
/// @brief Interval timer scheduling mode supplied during registration.
asiolink::IntervalTimer::Mode scheduling_mode_;
/// @brief Constructor.
///
/// @param io_service Reference to the IO service to be used by the
/// interval timer created.
/// @param user_callback Pointer to the callback function supplied
/// during the timer registration.
/// @param interval Timer interval in milliseconds.
/// @param mode Interval timer scheduling mode.
TimerInfo(const asiolink::IOServicePtr& io_service,
const asiolink::IntervalTimer::Callback& user_callback,
const long interval,
const asiolink::IntervalTimer::Mode& mode)
: interval_timer_(io_service),
user_callback_(user_callback),
interval_(interval),
scheduling_mode_(mode) { };
};
}
namespace isc {
namespace dhcp {
/// @brief A type definition for the pointer to @c TimerInfo structure.
typedef boost::shared_ptr<TimerInfo> TimerInfoPtr;
/// @brief A type definition for the map holding timers configuration.
typedef std::map<std::string, TimerInfoPtr> TimerInfoMap;
/// @brief Implementation of the @c TimerMgr
class TimerMgrImpl {
public:
/// @brief Constructor.
TimerMgrImpl();
/// @brief Sets IO service to be used by the Timer Manager.
///
/// @param io_service Pointer to the IO service.
void setIOService(const IOServicePtr& io_service);
/// @brief Registers new timer in the @c TimerMgr.
///
/// @param timer_name Unique name for the timer.
/// @param callback Pointer to the callback function to be invoked
/// when the timer elapses, e.g. function processing expired leases
/// in the DHCP server.
/// @param interval Timer interval in milliseconds.
/// @param scheduling_mode Scheduling mode of the timer as described in
/// @c asiolink::IntervalTimer::Mode.
///
/// @throw BadValue if the timer name is invalid or duplicate.
void registerTimer(const std::string& timer_name,
const asiolink::IntervalTimer::Callback& callback,
const long interval,
const asiolink::IntervalTimer::Mode& scheduling_mode);
/// @brief Unregisters specified timer.
///
/// This method cancels the timer if it is setup and removes the timer
/// from the internal collection of timers.
///
/// @param timer_name Name of the timer to be unregistered.
///
/// @throw BadValue if the specified timer hasn't been registered.
void unregisterTimer(const std::string& timer_name);
/// @brief Unregisters all timers.
///
/// This method must be explicitly called prior to termination of the
/// process.
void unregisterTimers();
/// @brief Checks if the timer with a specified name has been registered.
///
/// @param timer_name Name of the timer.
/// @return true if the timer with the specified name has been registered,
/// false otherwise.
bool isTimerRegistered(const std::string& timer_name);
/// @brief Returns the number of registered timers.
size_t timersCount() const;
/// @brief Schedules the execution of the interval timer.
///
/// This method schedules the timer, i.e. the callback will be executed
/// after specified interval elapses. The interval has been specified
/// during timer registration. Depending on the mode selected during the
/// timer registration, the callback will be executed once after it has
/// been scheduled or until it is cancelled. Though, in the former case
/// the timer can be re-scheduled in the callback function.
///
/// @param timer_name Unique timer name.
///
/// @throw BadValue if the timer hasn't been registered.
void setup(const std::string& timer_name);
/// @brief Cancels the execution of the interval timer.
///
/// @param timer_name Unique timer name.
///
/// @throw BadValue if the timer hasn't been registered.
void cancel(const std::string& timer_name);
private:
/// @name Internal methods called while holding the mutex in multi threading
/// mode.
/// @brief Registers new timer in the @c TimerMgr.
///
/// @param timer_name Unique name for the timer.
/// @param callback Pointer to the callback function to be invoked
/// when the timer elapses, e.g. function processing expired leases
/// in the DHCP server.
/// @param interval Timer interval in milliseconds.
/// @param scheduling_mode Scheduling mode of the timer as described in
/// @c asiolink::IntervalTimer::Mode.
///
/// @throw BadValue if the timer name is invalid or duplicate.
void registerTimerInternal(const std::string& timer_name,
const asiolink::IntervalTimer::Callback& callback,
const long interval,
const asiolink::IntervalTimer::Mode& scheduling_mode);
/// @brief Unregisters specified timer.
///
/// This method cancels the timer if it is setup and removes the timer
/// from the internal collection of timers.
///
/// @param timer_name Name of the timer to be unregistered.
///
/// @throw BadValue if the specified timer hasn't been registered.
void unregisterTimerInternal(const std::string& timer_name);
/// @brief Unregisters all timers.
///
/// This method must be explicitly called prior to termination of the
/// process.
void unregisterTimersInternal();
/// @brief Schedules the execution of the interval timer.
///
/// This method schedules the timer, i.e. the callback will be executed
/// after specified interval elapses. The interval has been specified
/// during timer registration. Depending on the mode selected during the
/// timer registration, the callback will be executed once after it has
/// been scheduled or until it is cancelled. Though, in the former case
/// the timer can be re-scheduled in the callback function.
///
/// @param timer_name Unique timer name.
///
/// @throw BadValue if the timer hasn't been registered.
void setupInternal(const std::string& timer_name);
/// @brief Cancels the execution of the interval timer.
///
/// @param timer_name Unique timer name.
///
/// @throw BadValue if the timer hasn't been registered.
void cancelInternal(const std::string& timer_name);
/// @brief Callback function to be executed for each interval timer when
/// its scheduled interval elapses.
///
/// @param timer_name Unique timer name.
void timerCallback(const std::string& timer_name);
/// @brief Pointer to the io service.
asiolink::IOServicePtr io_service_;
/// @brief Holds mapping of the timer name to timer instance and other
/// parameters pertaining to the timer.
TimerInfoMap registered_timers_;
/// @brief The mutex to protect the timer manager.
boost::scoped_ptr<std::mutex> mutex_;
};
TimerMgrImpl::TimerMgrImpl() : io_service_(new IOService()),
registered_timers_(), mutex_(new std::mutex) {
}
void
TimerMgrImpl::setIOService(const IOServicePtr& io_service) {
if (!io_service) {
isc_throw(BadValue, "IO service object must not be null for TimerMgr");
}
io_service_ = io_service;
}
void
TimerMgrImpl::registerTimer(const std::string& timer_name,
const IntervalTimer::Callback& callback,
const long interval,
const IntervalTimer::Mode& scheduling_mode) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
registerTimerInternal(timer_name, callback, interval, scheduling_mode);
} else {
registerTimerInternal(timer_name, callback, interval, scheduling_mode);
}
}
void
TimerMgrImpl::registerTimerInternal(const std::string& timer_name,
const IntervalTimer::Callback& callback,
const long interval,
const IntervalTimer::Mode& scheduling_mode) {
// Timer name must not be empty.
if (timer_name.empty()) {
isc_throw(BadValue, "registered timer name must not be empty");
}
// Must not register two timers under the same name.
if (registered_timers_.find(timer_name) != registered_timers_.end()) {
isc_throw(BadValue, "trying to register duplicate timer '"
<< timer_name << "'");
}
// Create a structure holding the configuration for the timer. It will
// create the instance if the IntervalTimer. It will also hold the
// callback, interval and scheduling mode parameters.
TimerInfoPtr timer_info(new TimerInfo(io_service_, callback,
interval, scheduling_mode));
// Actually register the timer.
registered_timers_.insert(std::pair<std::string, TimerInfoPtr>(timer_name,
timer_info));
}
void
TimerMgrImpl::unregisterTimer(const std::string& timer_name) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
unregisterTimerInternal(timer_name);
} else {
unregisterTimerInternal(timer_name);
}
}
void
TimerMgrImpl::unregisterTimerInternal(const std::string& timer_name) {
// Find the timer with specified name.
TimerInfoMap::iterator timer_info_it = registered_timers_.find(timer_name);
// Check if the timer has been registered.
if (timer_info_it == registered_timers_.end()) {
isc_throw(BadValue, "unable to unregister non existing timer '"
<< timer_name << "'");
}
// Cancel any pending asynchronous operation and stop the timer.
cancelInternal(timer_name);
// Remove the timer.
registered_timers_.erase(timer_info_it);
}
void
TimerMgrImpl::unregisterTimers() {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
unregisterTimersInternal();
} else {
unregisterTimersInternal();
}
}
void
TimerMgrImpl::unregisterTimersInternal() {
// Copy the map holding timers configuration. This is required so as
// we don't cut the branch which we're sitting on when we will be
// erasing the timers. We're going to iterate over the register timers
// and remove them with the call to unregisterTimer function. But this
// function will remove them from the register_timers_ map. If we
// didn't work on the copy here, our iterator would invalidate. The
// TimerInfo structure is copyable and since it is using the shared
// pointers the copy is not expensive. Also this function is called when
// the process terminates so it is not critical for performance.
TimerInfoMap registered_timers_copy(registered_timers_);
// Iterate over the existing timers and unregister them.
for (auto const& timer_info_it : registered_timers_copy) {
unregisterTimerInternal(timer_info_it.first);
}
}
bool
TimerMgrImpl::isTimerRegistered(const std::string& timer_name) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
return (registered_timers_.find(timer_name) != registered_timers_.end());
} else {
return (registered_timers_.find(timer_name) != registered_timers_.end());
}
}
size_t
TimerMgrImpl::timersCount() const {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
return (registered_timers_.size());
} else {
return (registered_timers_.size());
}
}
void
TimerMgrImpl::setup(const std::string& timer_name) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
setupInternal(timer_name);
} else {
setupInternal(timer_name);
}
}
void
TimerMgrImpl::setupInternal(const std::string& timer_name) {
// Check if the specified timer exists.
TimerInfoMap::const_iterator timer_info_it = registered_timers_.find(timer_name);
if (timer_info_it == registered_timers_.end()) {
isc_throw(BadValue, "unable to setup timer '" << timer_name << "': "
"no such timer registered");
}
// Schedule the execution of the timer using the parameters supplied
// during the registration.
const TimerInfoPtr& timer_info = timer_info_it->second;
IntervalTimer::Callback cb = std::bind(&TimerMgrImpl::timerCallback, this,
timer_name);
timer_info->interval_timer_.setup(cb, timer_info->interval_,
timer_info->scheduling_mode_);
}
void
TimerMgrImpl::cancel(const std::string& timer_name) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
cancelInternal(timer_name);
} else {
cancelInternal(timer_name);
}
}
void
TimerMgrImpl::cancelInternal(const std::string& timer_name) {
// Find the timer of our interest.
TimerInfoMap::const_iterator timer_info_it = registered_timers_.find(timer_name);
if (timer_info_it == registered_timers_.end()) {
isc_throw(BadValue, "unable to cancel timer '" << timer_name << "': "
"no such timer registered");
}
// Cancel the timer.
timer_info_it->second->interval_timer_.cancel();
}
void
TimerMgrImpl::timerCallback(const std::string& timer_name) {
// Find the specified timer setup.
TimerInfoMap::iterator timer_info_it = registered_timers_.find(timer_name);
if (timer_info_it != registered_timers_.end()) {
// Running user-defined operation for the timer. Logging it
// on the slightly lower debug level as there may be many
// such traces.
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
DHCPSRV_TIMERMGR_RUN_TIMER_OPERATION)
.arg(timer_info_it->first);
std::string error_string;
try {
timer_info_it->second->user_callback_();
} catch (const std::exception& ex){
error_string = ex.what();
} catch (...) {
error_string = "unknown reason";
}
// Exception was thrown. Log an error.
if (!error_string.empty()) {
LOG_ERROR(dhcpsrv_logger, DHCPSRV_TIMERMGR_CALLBACK_FAILED)
.arg(timer_info_it->first)
.arg(error_string);
}
}
}
const TimerMgrPtr&
TimerMgr::instance() {
static TimerMgrPtr timer_mgr(new TimerMgr());
return (timer_mgr);
}
TimerMgr::TimerMgr()
: impl_(new TimerMgrImpl()) {
}
TimerMgr::~TimerMgr() {
impl_->unregisterTimers();
}
void
TimerMgr::registerTimer(const std::string& timer_name,
const IntervalTimer::Callback& callback,
const long interval,
const IntervalTimer::Mode& scheduling_mode) {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_TIMERMGR_REGISTER_TIMER)
.arg(timer_name)
.arg(interval);
impl_->registerTimer(timer_name, callback, interval, scheduling_mode);
}
void
TimerMgr::unregisterTimer(const std::string& timer_name) {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_TIMERMGR_UNREGISTER_TIMER)
.arg(timer_name);
impl_->unregisterTimer(timer_name);
}
void
TimerMgr::unregisterTimers() {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_TIMERMGR_UNREGISTER_ALL_TIMERS);
impl_->unregisterTimers();
}
bool
TimerMgr::isTimerRegistered(const std::string& timer_name) {
return (impl_->isTimerRegistered(timer_name));
}
size_t
TimerMgr::timersCount() const {
return (impl_->timersCount());
}
void
TimerMgr::setup(const std::string& timer_name) {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_TIMERMGR_START_TIMER)
.arg(timer_name);
impl_->setup(timer_name);
}
void
TimerMgr::cancel(const std::string& timer_name) {
LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
DHCPSRV_TIMERMGR_STOP_TIMER)
.arg(timer_name);
impl_->cancel(timer_name);
}
void
TimerMgr::setIOService(const IOServicePtr& io_service) {
impl_->setIOService(io_service);
}
} // end of namespace isc::dhcp
} // end of namespace isc
|