Kea 2.5.6
d2_update_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2023 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
7#include <config.h>
8
9#include <d2/d2_update_mgr.h>
10#include <d2/nc_add.h>
11#include <d2/nc_remove.h>
12#include <d2/simple_add.h>
13#include <d2/simple_remove.h>
14#include <d2/check_exists_add.h>
18
19#include <sstream>
20#include <iostream>
21#include <vector>
22
23namespace isc {
24namespace d2 {
25
27
29 asiolink::IOServicePtr& io_service,
30 const size_t max_transactions)
31 :queue_mgr_(queue_mgr), cfg_mgr_(cfg_mgr), io_service_(io_service) {
32 if (!queue_mgr_) {
33 isc_throw(D2UpdateMgrError, "D2UpdateMgr queue manager cannot be null");
34 }
35
36 if (!cfg_mgr_) {
38 "D2UpdateMgr configuration manager cannot be null");
39 }
40
41 if (!io_service_) {
42 isc_throw(D2UpdateMgrError, "IOServicePtr cannot be null");
43 }
44
45 // Use setter to do validation.
46 setMaxTransactions(max_transactions);
47}
48
50 transaction_list_.clear();
51}
52
54 // cleanup finished transactions;
56
57 // if the queue isn't empty, find the next suitable job and
58 // start a transaction for it.
59 // @todo - Do we want to queue max transactions? The logic here will only
60 // start one new transaction per invocation. On the other hand a busy
61 // system will generate many IO events and this method will be called
62 // frequently. It will likely achieve max transactions quickly on its own.
63 if (getQueueCount() > 0) {
64 if (getTransactionCount() >= max_transactions_) {
67 .arg(getMaxTransactions());
68
69 return;
70 }
71
72 // We are not at maximum transactions, so pick and start the next job.
74 }
75}
76
77void
79 // Cycle through transaction list and do whatever needs to be done
80 // for finished transactions.
81 // At the moment all we do is remove them from the list. This is likely
82 // to expand as DHCP_DDNS matures.
83 // NOTE: One must use postfix increments of the iterator on the calls
84 // to erase. This replaces the old iterator which becomes invalid by the
85 // erase with the next valid iterator. Prefix incrementing will not
86 // work.
87 TransactionList::iterator it = transaction_list_.begin();
88 while (it != transaction_list_.end()) {
89 NameChangeTransactionPtr trans = (*it).second;
90 if (trans->isModelDone()) {
91 // @todo Additional actions based on NCR status could be
92 // performed here.
93 transaction_list_.erase(it++);
94 } else {
95 ++it;
96 }
97 }
98}
99
101 // Start at the front of the queue, looking for the first entry for
102 // which no transaction is in progress. If we find an eligible entry
103 // remove it from the queue and make a transaction for it.
104 // Requests and transactions are associated by DHCID. If a request has
105 // the same DHCID as a transaction, they are presumed to be for the same
106 // "end user".
107 size_t queue_count = getQueueCount();
108 for (size_t index = 0; index < queue_count; ++index) {
109 dhcp_ddns::NameChangeRequestPtr found_ncr = queue_mgr_->peekAt(index);
110 if (!hasTransaction(found_ncr->getDhcid())) {
111 queue_mgr_->dequeueAt(index);
112 makeTransaction(found_ncr);
113 return;
114 }
115 }
116
117 // There were no eligible jobs. All of the current DHCIDs already have
118 // transactions pending.
121 .arg(getQueueCount()).arg(getTransactionCount());
122}
123
124void
126 // First lets ensure there is not a transaction in progress for this
127 // DHCID. (pickNextJob should ensure this, as it is the only real caller
128 // but for safety's sake we'll check).
129 const TransactionKey& key = next_ncr->getDhcid();
130 if (findTransaction(key) != transactionListEnd()) {
131 // This is programmatic error. Caller(s) should be checking this.
132 isc_throw(D2UpdateMgrError, "Transaction already in progress for: "
133 << key.toStr());
134 }
135
136 int direction_count = 0;
137 // If forward change is enabled, match to forward servers.
138 DdnsDomainPtr forward_domain;
139 if (next_ncr->isForwardChange()) {
140 if (!cfg_mgr_->forwardUpdatesEnabled()) {
141 next_ncr->setForwardChange(false);
144 .arg(next_ncr->getRequestId())
145 .arg(next_ncr->toText());
146 } else {
147 bool matched = cfg_mgr_->matchForward(next_ncr->getFqdn(),
148 forward_domain);
149 // Could not find a match for forward DNS server. Log it and get
150 // out. This has the net affect of dropping the request on the
151 // floor.
152 if (!matched) {
154 .arg(next_ncr->getRequestId())
155 .arg(next_ncr->toText());
156 return;
157 }
158
159 ++direction_count;
160 }
161 }
162
163 // If reverse change is enabled, match to reverse servers.
164 DdnsDomainPtr reverse_domain;
165 if (next_ncr->isReverseChange()) {
166 if (!cfg_mgr_->reverseUpdatesEnabled()) {
167 next_ncr->setReverseChange(false);
170 .arg(next_ncr->getRequestId())
171 .arg(next_ncr->toText());
172 } else {
173 bool matched = cfg_mgr_->matchReverse(next_ncr->getIpAddress(),
174 reverse_domain);
175 // Could not find a match for reverse DNS server. Log it and get
176 // out. This has the net affect of dropping the request on the
177 // floor.
178 if (!matched) {
180 .arg(next_ncr->getRequestId())
181 .arg(next_ncr->toText());
182 return;
183 }
184
185 ++direction_count;
186 }
187 }
188
189 // If there is nothing to actually do, then the request falls on the floor.
190 // Should we log this?
191 if (!direction_count) {
194 .arg(next_ncr->getRequestId())
195 .arg(next_ncr->toText());
196 return;
197 }
198
199 // We matched to the required servers, so construct the transaction.
200 // @todo If multi-threading is implemented, one would pass in an
201 // empty IOServicePtr, rather than our instance value. This would cause
202 // the transaction to instantiate its own, separate IOService to handle
203 // the transaction's IO.
205 if (next_ncr->getChangeType() == dhcp_ddns::CHG_ADD) {
206 switch(next_ncr->getConflictResolutionMode()) {
208 trans.reset(new NameAddTransaction(io_service_, next_ncr,
209 forward_domain, reverse_domain,
210 cfg_mgr_));
211 break;
213 trans.reset(new CheckExistsAddTransaction(io_service_, next_ncr,
214 forward_domain, reverse_domain,
215 cfg_mgr_));
216 break;
218 trans.reset(new SimpleAddWithoutDHCIDTransaction(io_service_, next_ncr,
219 forward_domain, reverse_domain,
220 cfg_mgr_));
221 break;
222 default:
223 // dhcp_ddns::NO_CHECK_WITH_DHCID
224 trans.reset(new SimpleAddTransaction(io_service_, next_ncr,
225 forward_domain, reverse_domain,
226 cfg_mgr_));
227 break;
228 }
229 } else {
230 switch(next_ncr->getConflictResolutionMode()) {
232 trans.reset(new NameRemoveTransaction(io_service_, next_ncr,
233 forward_domain, reverse_domain,
234 cfg_mgr_));
235 break;
237 trans.reset(new CheckExistsRemoveTransaction(io_service_, next_ncr,
238 forward_domain, reverse_domain,
239 cfg_mgr_));
240 break;
242 trans.reset(new SimpleRemoveWithoutDHCIDTransaction(io_service_, next_ncr,
243 forward_domain, reverse_domain,
244 cfg_mgr_));
245 break;
246 default:
247 // dhcp_ddns::NO_CHECK_WITH_DHCID
248 trans.reset(new SimpleRemoveTransaction(io_service_, next_ncr,
249 forward_domain, reverse_domain,
250 cfg_mgr_));
251 break;
252 }
253 }
254
255 // Add the new transaction to the list.
256 transaction_list_[key] = trans;
257
258 // Start it.
259 trans->startTransaction();
260}
261
262TransactionList::iterator
264 return (transaction_list_.find(key));
265}
266
267bool
269 return (findTransaction(key) != transactionListEnd());
270}
271
272void
274 TransactionList::iterator pos = findTransaction(key);
275 if (pos != transactionListEnd()) {
276 transaction_list_.erase(pos);
277 }
278}
279
280TransactionList::iterator
282 return (transaction_list_.begin());
283}
284
285TransactionList::iterator
287 return (transaction_list_.end());
288}
289
290void
292 // @todo for now this just wipes them out. We might need something
293 // more elegant, that allows a cancel first.
294 transaction_list_.clear();
295}
296
297void
298D2UpdateMgr::setMaxTransactions(const size_t new_trans_max) {
299 // Obviously we need at room for at least one transaction.
300 if (new_trans_max < 1) {
301 isc_throw(D2UpdateMgrError, "D2UpdateMgr"
302 " maximum transactions limit must be greater than zero");
303 }
304
305 // Do not allow the list maximum to be set to less then current list size.
306 if (new_trans_max < getTransactionCount()) {
307 isc_throw(D2UpdateMgrError, "D2UpdateMgr maximum transaction limit "
308 "cannot be less than the current transaction count :"
310 }
311
312 max_transactions_ = new_trans_max;
313}
314
315size_t
317 return (queue_mgr_->getQueueSize());
318}
319
320size_t
322 return (transaction_list_.size());
323}
324
325
326} // namespace isc::d2
327} // namespace isc
This file defines the class CheckExistsAddTransaction.
This file defines the class CheckExistsRemoveTransaction.
Embodies the "life-cycle" required to carry out a DDNS Add update.
Embodies the "life-cycle" required to carry out a DDNS Remove update.
Thrown if the update manager encounters a general error.
Definition: d2_update_mgr.h:27
TransactionList::iterator transactionListEnd()
Returns the transaction list end position.
bool hasTransaction(const TransactionKey &key)
Convenience method that checks transaction list for the given key.
TransactionList::iterator findTransaction(const TransactionKey &key)
Search the transaction list for the given key.
virtual ~D2UpdateMgr()
Destructor.
void removeTransaction(const TransactionKey &key)
Removes the entry pointed to by key from the transaction list.
void makeTransaction(isc::dhcp_ddns::NameChangeRequestPtr &ncr)
Create a new transaction for the given request.
void checkFinishedTransactions()
Performs post-completion cleanup on completed transactions.
void setMaxTransactions(const size_t max_transactions)
Sets the maximum number of entries allowed in the queue.
TransactionList::iterator transactionListBegin()
Returns the transaction list beg position.
void clearTransactionList()
Immediately discards all entries in the transaction list.
void sweep()
Check current transactions; start transactions for new requests.
size_t getMaxTransactions() const
Returns the maximum number of concurrent transactions.
size_t getQueueCount() const
Convenience method that returns the number of requests queued.
D2UpdateMgr(D2QueueMgrPtr &queue_mgr, D2CfgMgrPtr &cfg_mgr, asiolink::IOServicePtr &io_service, const size_t max_transactions=MAX_TRANSACTIONS_DEFAULT)
Constructor.
size_t getTransactionCount() const
Returns the current number of transactions.
void pickNextJob()
Starts a transaction for the next eligible request in the queue.
static const size_t MAX_TRANSACTIONS_DEFAULT
Maximum number of concurrent transactions NOTE that 32 is an arbitrary choice picked for the initial ...
Definition: d2_update_mgr.h:70
Embodies the "life-cycle" required to carry out a DDNS Add update.
Definition: nc_add.h:52
Embodies the "life-cycle" required to carry out a DDNS Remove update.
Definition: nc_remove.h:51
Embodies the "life-cycle" required to carry out a DDNS Add update.
Definition: simple_add.h:47
Embodies the "life-cycle" required to carry out a DDNS Add update without checking for conflicts.
Embodies the "life-cycle" required to carry out a DDNS Remove update.
Definition: simple_remove.h:49
Embodies the "life-cycle" required to carry out a DDNS Remove update without removing any matching DH...
Container class for handling the DHCID value within a NameChangeRequest.
Definition: ncr_msg.h:114
std::string toStr() const
Returns the DHCID value as a string of hexadecimal digits.
Definition: ncr_msg.cc:135
This file defines the class D2UpdateMgr.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
const isc::log::MessageID DHCP_DDNS_REQUEST_DROPPED
Definition: d2_messages.h:72
boost::shared_ptr< DdnsDomain > DdnsDomainPtr
Defines a pointer for DdnsDomain instances.
Definition: d2_config.h:610
boost::shared_ptr< D2CfgMgr > D2CfgMgrPtr
Defines a shared pointer to D2CfgMgr.
Definition: d2_cfg_mgr.h:334
const isc::log::MessageID DHCP_DDNS_NO_REV_MATCH_ERROR
Definition: d2_messages.h:54
const isc::log::MessageID DHCP_DDNS_FWD_REQUEST_IGNORED
Definition: d2_messages.h:47
const isc::log::MessageID DHCP_DDNS_REV_REQUEST_IGNORED
Definition: d2_messages.h:85
isc::log::Logger dhcp_to_d2_logger("dhcp-to-d2")
Definition: d2_log.h:19
const isc::log::MessageID DHCP_DDNS_AT_MAX_TRANSACTIONS
Definition: d2_messages.h:14
boost::shared_ptr< NameChangeTransaction > NameChangeTransactionPtr
Defines a pointer to a NameChangeTransaction.
Definition: nc_trans.h:598
boost::shared_ptr< D2QueueMgr > D2QueueMgrPtr
Defines a pointer for manager instances.
Definition: d2_queue_mgr.h:343
const isc::log::MessageID DHCP_DDNS_NO_ELIGIBLE_JOBS
Definition: d2_messages.h:51
const isc::log::MessageID DHCP_DDNS_NO_FWD_MATCH_ERROR
Definition: d2_messages.h:52
@ CHECK_WITH_DHCID
Definition: ncr_msg.h:66
@ NO_CHECK_WITHOUT_DHCID
Definition: ncr_msg.h:69
@ CHECK_EXISTS_WITH_DHCID
Definition: ncr_msg.h:68
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition: ncr_msg.h:242
const int DBGLVL_TRACE_DETAIL_DATA
Trace data associated with detailed operations.
Definition: log_dbglevels.h:78
Defines the logger used by the top-level component of kea-lfc.
This file defines the class NameAddTransaction.
This file defines the class NameRemoveTransaction.
This file defines the class SimpleAddWithoutDHCIDTransaction.
This file defines the class SimpleRemoveWithoutDHCIDTransaction.