Kea 2.7.6
d2_update_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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. If the
104 // transaction creation fails try the next entry in the queue.
105 // Requests and transactions are associated by DHCID. If a request has
106 // the same DHCID as a transaction, they are presumed to be for the same
107 // "end user".
108 size_t queue_count = getQueueCount();
109 for (size_t index = 0; index < queue_count; ) {
110 dhcp_ddns::NameChangeRequestPtr found_ncr = queue_mgr_->peekAt(index);
111 if (hasTransaction(found_ncr->getDhcid())) {
112 // Leave it on the queue, move on to the next.
113 ++index;
114 } else {
115 // Dequeue it and try to make transaction for it.
116 queue_mgr_->dequeueAt(index);
117 if (makeTransaction(found_ncr)) {
118 return;
119 }
120
121 // One less in the queue.
122 --queue_count;
123 }
124 }
125
126 // There were no eligible jobs. All of the current DHCIDs already have
127 // transactions pending.
130 .arg(getQueueCount()).arg(getTransactionCount());
131}
132
133bool
135 // First lets ensure there is not a transaction in progress for this
136 // DHCID. (pickNextJob should ensure this, as it is the only real caller
137 // but for safety's sake we'll check).
138 const TransactionKey& key = next_ncr->getDhcid();
139 if (findTransaction(key) != transactionListEnd()) {
140 // This is programmatic error. Caller(s) should be checking this.
141 isc_throw(D2UpdateMgrError, "Transaction already in progress for: "
142 << key.toStr());
143 }
144
145 int direction_count = 0;
146 // If forward change is enabled, match to forward servers.
147 DdnsDomainPtr forward_domain;
148 if (next_ncr->isForwardChange()) {
149 if (!cfg_mgr_->forwardUpdatesEnabled()) {
150 next_ncr->setForwardChange(false);
153 .arg(next_ncr->getRequestId())
154 .arg(next_ncr->toText());
155 } else {
156 bool matched = cfg_mgr_->matchForward(next_ncr->getFqdn(),
157 forward_domain);
158 // Could not find a match for forward DNS server. Log it and get
159 // out. This has the net affect of dropping the request on the
160 // floor.
161 if (!matched) {
163 .arg(next_ncr->getRequestId())
164 .arg(next_ncr->toText());
165 return (false);
166 }
167
168 ++direction_count;
169 }
170 }
171
172 // If reverse change is enabled, match to reverse servers.
173 DdnsDomainPtr reverse_domain;
174 if (next_ncr->isReverseChange()) {
175 if (!cfg_mgr_->reverseUpdatesEnabled()) {
176 next_ncr->setReverseChange(false);
179 .arg(next_ncr->getRequestId())
180 .arg(next_ncr->toText());
181 } else {
182 bool matched = cfg_mgr_->matchReverse(next_ncr->getIpAddress(),
183 reverse_domain);
184 // Could not find a match for reverse DNS server. Log it and get
185 // out. This has the net affect of dropping the request on the
186 // floor.
187 if (!matched) {
189 .arg(next_ncr->getRequestId())
190 .arg(next_ncr->toText());
191 return (false);
192 }
193
194 ++direction_count;
195 }
196 }
197
198 // If there is nothing to actually do, then the request falls on the floor.
199 // Should we log this?
200 if (!direction_count) {
203 .arg(next_ncr->getRequestId())
204 .arg(next_ncr->toText());
205 return (false);
206 }
207
208 // We matched to the required servers, so construct the transaction.
209 // @todo If multi-threading is implemented, one would pass in an
210 // empty IOServicePtr, rather than our instance value. This would cause
211 // the transaction to instantiate its own, separate IOService to handle
212 // the transaction's IO.
214 if (next_ncr->getChangeType() == dhcp_ddns::CHG_ADD) {
215 switch(next_ncr->getConflictResolutionMode()) {
217 trans.reset(new NameAddTransaction(io_service_, next_ncr,
218 forward_domain, reverse_domain,
219 cfg_mgr_));
220 break;
222 trans.reset(new CheckExistsAddTransaction(io_service_, next_ncr,
223 forward_domain, reverse_domain,
224 cfg_mgr_));
225 break;
227 trans.reset(new SimpleAddWithoutDHCIDTransaction(io_service_, next_ncr,
228 forward_domain, reverse_domain,
229 cfg_mgr_));
230 break;
231 default:
232 // dhcp_ddns::NO_CHECK_WITH_DHCID
233 trans.reset(new SimpleAddTransaction(io_service_, next_ncr,
234 forward_domain, reverse_domain,
235 cfg_mgr_));
236 break;
237 }
238 } else {
239 switch(next_ncr->getConflictResolutionMode()) {
241 trans.reset(new NameRemoveTransaction(io_service_, next_ncr,
242 forward_domain, reverse_domain,
243 cfg_mgr_));
244 break;
246 trans.reset(new CheckExistsRemoveTransaction(io_service_, next_ncr,
247 forward_domain, reverse_domain,
248 cfg_mgr_));
249 break;
251 trans.reset(new SimpleRemoveWithoutDHCIDTransaction(io_service_, next_ncr,
252 forward_domain, reverse_domain,
253 cfg_mgr_));
254 break;
255 default:
256 // dhcp_ddns::NO_CHECK_WITH_DHCID
257 trans.reset(new SimpleRemoveTransaction(io_service_, next_ncr,
258 forward_domain, reverse_domain,
259 cfg_mgr_));
260 break;
261 }
262 }
263
264 // Add the new transaction to the list.
265 transaction_list_[key] = trans;
266
267 // Start it.
268 trans->startTransaction();
269 return (true);
270}
271
272TransactionList::iterator
274 return (transaction_list_.find(key));
275}
276
277bool
281
282void
284 TransactionList::iterator pos = findTransaction(key);
285 if (pos != transactionListEnd()) {
286 transaction_list_.erase(pos);
287 }
288}
289
290TransactionList::iterator
292 return (transaction_list_.begin());
293}
294
295TransactionList::iterator
297 return (transaction_list_.end());
298}
299
300void
302 // @todo for now this just wipes them out. We might need something
303 // more elegant, that allows a cancel first.
304 transaction_list_.clear();
305}
306
307void
308D2UpdateMgr::setMaxTransactions(const size_t new_trans_max) {
309 // Obviously we need at room for at least one transaction.
310 if (new_trans_max < 1) {
311 isc_throw(D2UpdateMgrError, "D2UpdateMgr"
312 " maximum transactions limit must be greater than zero");
313 }
314
315 // Do not allow the list maximum to be set to less then current list size.
316 if (new_trans_max < getTransactionCount()) {
317 isc_throw(D2UpdateMgrError, "D2UpdateMgr maximum transaction limit "
318 "cannot be less than the current transaction count :"
320 }
321
322 max_transactions_ = new_trans_max;
323}
324
325size_t
327 return (queue_mgr_->getQueueSize());
328}
329
330size_t
332 return (transaction_list_.size());
333}
334
335
336} // namespace isc::d2
337} // 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.
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.
bool 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 ...
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.
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:113
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:69
boost::shared_ptr< DdnsDomain > DdnsDomainPtr
Defines a pointer for DdnsDomain instances.
Definition d2_config.h:624
boost::shared_ptr< D2CfgMgr > D2CfgMgrPtr
Defines a shared pointer to D2CfgMgr.
Definition d2_cfg_mgr.h:367
const isc::log::MessageID DHCP_DDNS_NO_REV_MATCH_ERROR
Definition d2_messages.h:52
const isc::log::MessageID DHCP_DDNS_FWD_REQUEST_IGNORED
Definition d2_messages.h:45
const isc::log::MessageID DHCP_DDNS_REV_REQUEST_IGNORED
Definition d2_messages.h:82
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:13
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.
const isc::log::MessageID DHCP_DDNS_NO_ELIGIBLE_JOBS
Definition d2_messages.h:49
const isc::log::MessageID DHCP_DDNS_NO_FWD_MATCH_ERROR
Definition d2_messages.h:50
@ NO_CHECK_WITHOUT_DHCID
Definition ncr_msg.h:68
@ CHECK_EXISTS_WITH_DHCID
Definition ncr_msg.h:67
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
Definition ncr_msg.h:241
const int DBGLVL_TRACE_DETAIL_DATA
Trace data associated with detailed operations.
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.