Kea  1.5.0
ha_service.h
Go to the documentation of this file.
1 // Copyright (C) 2018 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 #ifndef HA_SERVICE_H
8 #define HA_SERVICE_H
9 
10 #include <communication_state.h>
11 #include <ha_config.h>
12 #include <ha_server_type.h>
13 #include <query_filter.h>
14 #include <asiolink/io_service.h>
15 #include <cc/data.h>
16 #include <dhcp/pkt4.h>
17 #include <http/response.h>
18 #include <dhcp/pkt4.h>
19 #include <dhcpsrv/lease.h>
20 #include <dhcpsrv/network_state.h>
21 #include <hooks/parking_lots.h>
22 #include <http/client.h>
23 #include <util/state_model.h>
24 #include <boost/noncopyable.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <functional>
27 #include <map>
28 #include <vector>
29 
30 namespace isc {
31 namespace ha {
32 
38 class HAService : public boost::noncopyable, public util::StateModel {
39 public:
40 
43 
46 
49 
52 
53 protected:
54 
60  typedef std::function<void(const bool, const std::string&)> PostRequestCallback;
61 
69  typedef std::function<void(const bool, const std::string&, const bool)> PostSyncCallback;
70 
71 public:
72 
80  HAService(const asiolink::IOServicePtr& io_service,
81  const dhcp::NetworkStatePtr& network_state,
82  const HAConfigPtr& config,
83  const HAServerType& server_type = HAServerType::DHCPv4);
84 
87  return (server_type_);
88  }
89 
91  virtual void defineEvents();
92 
94  virtual void verifyEvents();
95 
97  virtual void defineStates();
98 
111  void backupStateHandler();
112 
142  void normalStateHandler();
143 
169 
185  void readyStateHandler();
186 
203  void syncingStateHandler();
204 
222  void terminatedStateHandler();
223 
247  void waitingStateHandler();
248 
249 protected:
250 
254  void verboseTransition(const unsigned state);
255 
256 public:
257 
265  bool unpause();
266 
267 protected:
268 
273  void conditionalLogPausedState() const;
274 
275 public:
276 
281  void serveDefaultScopes();
282 
296  bool inScope(dhcp::Pkt4Ptr& query4);
297 
311  bool inScope(dhcp::Pkt6Ptr& query6);
312 
313 private:
314 
328  template<typename QueryPtrType>
329  bool inScopeInternal(QueryPtrType& query);
330 
331 public:
332 
337  void adjustNetworkState();
338 
339 protected:
340 
354  bool shouldPartnerDown() const;
355 
367  bool shouldTerminate() const;
368 
369 public:
370 
404  size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr& query,
405  const dhcp::Lease4CollectionPtr& leases,
406  const dhcp::Lease4CollectionPtr& deleted_leases,
407  const hooks::ParkingLotHandlePtr& parking_lot);
408 
409 
428  size_t asyncSendLeaseUpdates(const dhcp::Pkt6Ptr& query,
429  const dhcp::Lease6CollectionPtr& leases,
430  const dhcp::Lease6CollectionPtr& deleted_leases,
431  const hooks::ParkingLotHandlePtr& parking_lot);
432 
433 protected:
434 
447  template<typename QueryPtrType>
448  void asyncSendLeaseUpdate(const QueryPtrType& query,
449  const HAConfig::PeerConfigPtr& config,
450  const data::ConstElementPtr& command,
451  const hooks::ParkingLotHandlePtr& parking_lot);
452 
463  bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr& peer_config) const;
464 
465 public:
466 
491 
492 protected:
493 
495  void asyncSendHeartbeat();
496 
502  void scheduleHeartbeat();
503 
505  void startHeartbeat();
506 
518  void asyncDisableDHCPService(http::HttpClient& http_client,
519  const std::string& server_name,
520  const unsigned int max_period,
521  PostRequestCallback post_request_action);
522 
532  void asyncEnableDHCPService(http::HttpClient& http_client,
533  const std::string& server_name,
534  PostRequestCallback post_request_action);
535 
538 
540  void localEnableDHCPService();
541 
557  void asyncSyncLeases();
558 
609  void asyncSyncLeases(http::HttpClient& http_client,
610  const std::string& server_name,
611  const unsigned int max_period,
612  const dhcp::LeasePtr& last_lease,
613  PostSyncCallback post_sync_action,
614  const bool dhcp_disabled = false);
615 
642  void asyncSyncLeasesInternal(http::HttpClient& http_client,
643  const std::string& server_name,
644  const unsigned int max_period,
645  const dhcp::LeasePtr& last_lease,
646  PostSyncCallback post_sync_action,
647  const bool dhcp_disabled);
648 
649 
650 public:
651 
671  data::ConstElementPtr processSynchronize(const std::string& server_name,
672  const unsigned int max_period);
673 
674 protected:
675 
689  int synchronize(std::string& status_message, const std::string& server_name,
690  const unsigned int max_period);
691 
692 public:
693 
699  data::ConstElementPtr processScopes(const std::vector<std::string>& scopes);
700 
705 
706 protected:
707 
717 
721 
724 
727 
730 
733 
736 
739 
748  std::map<boost::shared_ptr<dhcp::Pkt>, int> pending_requests_;
749 };
750 
752 typedef boost::shared_ptr<HAService> HAServicePtr;
753 
754 } // end of namespace isc::ha
755 } // end of namespace isc
756 
757 #endif
virtual void defineStates()
Defines states of the HA service.
Definition: ha_service.cc:85
Implements a finite state machine.
Definition: state_model.h:271
void serveDefaultScopes()
Instructs the HA service to serve default scopes.
Definition: ha_service.cc:566
void readyStateHandler()
Handler for "ready" state.
Definition: ha_service.cc:243
data::ConstElementPtr processScopes(const std::vector< std::string > &scopes)
Processes ha-scopes command and returns a response.
Definition: ha_service.cc:1483
void scheduleHeartbeat()
Schedules asynchronous heartbeat to a peer if it is not scheduled.
Definition: ha_service.cc:1009
void asyncEnableDHCPService(http::HttpClient &http_client, const std::string &server_name, PostRequestCallback post_request_action)
Schedules asynchronous "dhcp-enable" command to the specified server.
Definition: ha_service.cc:1094
bool unpause()
Unpauses the HA state machine with logging.
Definition: ha_service.cc:545
void asyncSyncLeases()
Asynchronously reads leases from a peer and updates local lease database.
Definition: ha_service.cc:1170
data::ConstElementPtr processHeartbeat()
Processes ha-heartbeat command and returns a response.
Definition: ha_service.cc:902
QueryFilter query_filter_
Selects queries to be processed/dropped.
Definition: ha_service.h:738
http::HttpClient client_
HTTP client instance used to send lease updates.
Definition: ha_service.h:732
CommunicationStatePtr communication_state_
Holds communication state with a peer.
Definition: ha_service.h:735
virtual void verifyEvents()
Verifies events used by the HA service.
Definition: ha_service.cc:75
static const int HA_SYNCING_SUCCEEDED_EVT
Lease database synchroniation succeeded.
Definition: ha_service.h:51
data::ConstElementPtr processContinue()
Processes ha-continue command and returns a response.
Definition: ha_service.cc:1496
HAServerType server_type_
DHCP server type.
Definition: ha_service.h:729
void waitingStateHandler()
Handler for "waiting" state.
Definition: ha_service.cc:410
HAServerType
Lists possible server types for which HA service is created.
bool shouldTerminate() const
Indicates if the server should transition to the terminated state as a result of high clock skew.
Definition: ha_service.cc:657
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:26
dhcp::NetworkStatePtr network_state_
Pointer to the state of the DHCP service (enabled/disabled).
Definition: ha_service.h:723
asiolink::IOServicePtr io_service_
Pointer to the IO service object shared between this hooks library and the DHCP server.
Definition: ha_service.h:720
HAServerType getServerType() const
Returns HA server type used in object construction.
Definition: ha_service.h:86
DHCP query filtering class.
Definition: query_filter.h:57
boost::shared_ptr< Pkt6 > Pkt6Ptr
A pointer to Pkt6 packet.
Definition: pkt6.h:28
static const int HA_HEARTBEAT_COMPLETE_EVT
Finished heartbeat commannd.
Definition: ha_service.h:42
static const int HA_LEASE_UPDATES_COMPLETE_EVT
Finished lease updates commands.
Definition: ha_service.h:45
HAService(const asiolink::IOServicePtr &io_service, const dhcp::NetworkStatePtr &network_state, const HAConfigPtr &config, const HAServerType &server_type=HAServerType::DHCPv4)
Constructor.
Definition: ha_service.cc:44
void partnerDownStateHandler()
Handler for "partner-down" state.
Definition: ha_service.cc:187
void syncingStateHandler()
Handler for "syncing" state.
Definition: ha_service.cc:308
boost::shared_ptr< HttpResponse > HttpResponsePtr
Pointer to the HttpResponse object.
Definition: response.h:78
void asyncSendHeartbeat()
Starts asynchronous heartbeat to a peer.
Definition: ha_service.cc:915
boost::shared_ptr< HAService > HAServicePtr
Pointer to the HAService class.
Definition: ha_service.h:752
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:458
void asyncSendLeaseUpdate(const QueryPtrType &query, const HAConfig::PeerConfigPtr &config, const data::ConstElementPtr &command, const hooks::ParkingLotHandlePtr &parking_lot)
Asynchronously sends lease update to the peer.
Definition: ha_service.cc:753
boost::shared_ptr< Pkt4 > Pkt4Ptr
A pointer to Pkt4 object.
Definition: pkt4.h:546
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
bool inScope(dhcp::Pkt4Ptr &query4)
Checks if the DHCPv4 query should be processed by this server.
Definition: ha_service.cc:571
void terminatedStateHandler()
Handler for "terminated" state.
Definition: ha_service.cc:389
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
void localDisableDHCPService()
Disables local DHCP service.
Definition: ha_service.cc:1160
void adjustNetworkState()
Enables or disables network state depending on the served scopes.
Definition: ha_service.cc:601
data::ConstElementPtr verifyAsyncResponse(const http::HttpResponsePtr &response)
Checks if the response is valid or contains an error.
Definition: ha_service.cc:1504
void asyncSyncLeasesInternal(http::HttpClient &http_client, const std::string &server_name, const unsigned int max_period, const dhcp::LeasePtr &last_lease, PostSyncCallback post_sync_action, const bool dhcp_disabled)
Implements fetching one page of leases during synchronization.
Definition: ha_service.cc:1217
std::function< void(const bool, const std::string &, const bool)> PostSyncCallback
Callback invoked when lease database synchronization is complete.
Definition: ha_service.h:69
HTTP client class.
Definition: client.h:66
Defines the logger used by the top-level component of kea-dhcp-ddns.
bool shouldPartnerDown() const
Indicates if the server should transition to the partner down state.
Definition: ha_service.cc:630
HAConfigPtr config_
Pointer to the HA hooks library configuration.
Definition: ha_service.h:726
void startHeartbeat()
Unconditionally starts one heartbeat to a peer.
Definition: ha_service.cc:1016
void normalStateHandler()
Handler for the "hot-standby" and "load-balancing" states.
Definition: ha_service.cc:137
High availability service.
Definition: ha_service.h:38
void localEnableDHCPService()
Enables local DHCP service.
Definition: ha_service.cc:1165
virtual void defineEvents()
Defines events used by the HA service.
Definition: ha_service.cc:65
std::function< void(const bool, const std::string &)> PostRequestCallback
Callback invoked when request was sent and a response received or an error occurred.
Definition: ha_service.h:60
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:608
boost::shared_ptr< NetworkState > NetworkStatePtr
Pointer to the NetworkState object.
bool shouldSendLeaseUpdates(const HAConfig::PeerConfigPtr &peer_config) const
Checks if the lease updates should be sent as result of leases allocation or release.
Definition: ha_service.cc:871
void backupStateHandler()
Handler for the "backup" state.
Definition: ha_service.cc:122
static const int HA_SYNCING_FAILED_EVT
Lease database synchronization failed.
Definition: ha_service.h:48
size_t asyncSendLeaseUpdates(const dhcp::Pkt4Ptr &query, const dhcp::Lease4CollectionPtr &leases, const dhcp::Lease4CollectionPtr &deleted_leases, const hooks::ParkingLotHandlePtr &parking_lot)
Schedules asynchronous IPv4 leases updates.
Definition: ha_service.cc:672
boost::shared_ptr< ParkingLotHandle > ParkingLotHandlePtr
Pointer to the parking lot handle.
Definition: parking_lots.h:292
This file defines the class StateModel.
void conditionalLogPausedState() const
Logs if the server is paused in the current state.
Definition: ha_service.cc:555
data::ConstElementPtr processSynchronize(const std::string &server_name, const unsigned int max_period)
Processes ha-sync command and returns a response.
Definition: ha_service.cc:1395
boost::shared_ptr< CommunicationState > CommunicationStatePtr
Type of the pointer to the CommunicationState object.
void verboseTransition(const unsigned state)
Transitions to a desired state and logs it.
Definition: ha_service.cc:491
boost::shared_ptr< HAConfig > HAConfigPtr
Pointer to the High Availability configuration structure.
Definition: ha_config.h:509
std::map< boost::shared_ptr< dhcp::Pkt >, int > pending_requests_
Map holding a number of scheduled requests for a given packet.
Definition: ha_service.h:748
int synchronize(std::string &status_message, const std::string &server_name, const unsigned int max_period)
Synchronizes lease database with a partner.
Definition: ha_service.cc:1403
boost::shared_ptr< PeerConfig > PeerConfigPtr
Pointer to the server's configuration.
Definition: ha_config.h:159
void asyncDisableDHCPService(http::HttpClient &http_client, const std::string &server_name, const unsigned int max_period, PostRequestCallback post_request_action)
Schedules asynchronous "dhcp-disable" command to the specified server.
Definition: ha_service.cc:1025