0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
HandlerMap.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
28 #ifndef AsyncComm_HandlerMap_h
29 #define AsyncComm_HandlerMap_h
30 
31 
32 //#define HT_DISABLE_LOG_DEBUG
33 
34 #include "CommAddress.h"
35 #include "CommBuf.h"
36 #include "IOHandlerData.h"
37 #include "IOHandlerDatagram.h"
38 #include "IOHandlerRaw.h"
39 #include "ProxyMap.h"
40 
41 #include <Common/Error.h>
42 #include <Common/Logger.h>
43 #include <Common/SockAddrMap.h>
44 #include <Common/Time.h>
45 #include <Common/Timer.h>
46 
47 #include <condition_variable>
48 #include <cassert>
49 #include <memory>
50 #include <mutex>
51 
52 namespace Hypertable {
53 
58  class IOHandlerAccept;
59 
69  class HandlerMap {
70 
71  public:
72 
75 
80  void insert_handler(IOHandlerAccept *handler);
81 
89  void insert_handler(IOHandlerData *handler, bool checkout=false);
90 
95  void insert_handler(IOHandlerDatagram *handler);
96 
101  void insert_handler(IOHandlerRaw *handler);
102 
112  int checkout_handler(const CommAddress &addr, IOHandlerAccept **handler);
113 
126  int checkout_handler(const CommAddress &addr, IOHandlerData **handler);
127 
137  int checkout_handler(const CommAddress &addr, IOHandlerDatagram **handler);
138 
148  int checkout_handler(const CommAddress &addr, IOHandlerRaw **handler);
149 
159  int contains_data_handler(const CommAddress &addr);
160 
168  void decrement_reference_count(IOHandler *handler);
169 
187  int set_alias(const InetAddr &addr, const InetAddr &alias);
188 
197  int remove_handler(IOHandler *handler);
198 
212 
218  std::lock_guard<std::mutex> lock(m_mutex);
220  }
221 
227  void decomission_all();
228 
233  bool destroy_ok(IOHandler *handler);
234 
245  bool translate_proxy_address(const CommAddress &proxy_addr, InetAddr *addr);
246 
256  void purge_handler(IOHandler *handler);
257 
263  void wait_for_empty();
264 
278  int add_proxy(const String &proxy, const String &hostname, const InetAddr &addr);
279 
289  int remove_proxy(const String &proxy);
290 
294  void get_proxy_map(ProxyMapT &proxy_map);
295 
308  void update_proxy_map(const char *message, size_t message_len);
309 
316  int32_t propagate_proxy_map(IOHandlerData *handler);
317 
325  bool wait_for_proxy_map(Timer &timer);
326 
327  private:
328 
343  int propagate_proxy_map(ProxyMapT &mappings);
344 
356  int translate_address(const CommAddress &addr, InetAddr *inet_addr);
357 
367  int remove_handler_unlocked(IOHandler *handler);
368 
377 
386 
395 
404 
407 
409  std::condition_variable m_cond;
410 
412  std::condition_variable m_cond_proxy;
413 
416 
419 
422 
425 
427  std::set<IOHandler *> m_decomissioned_handlers;
428 
431 
434  };
435 
437  typedef std::shared_ptr<HandlerMap> HandlerMapPtr;
438 
440 }
441 
442 
443 #endif // AsyncComm_HandlerMap_h
static std::mutex mutex
Definition: Logger.cc:43
std::set< IOHandler * > m_decomissioned_handlers
Decomissioned handler set.
Definition: HandlerMap.h:427
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
I/O handler for datagram (UDP) sockets.
int checkout_handler(const CommAddress &addr, IOHandlerAccept **handler)
Checks out accept I/O handler associated with addr.
Definition: HandlerMap.cc:70
bool wait_for_proxy_map(Timer &timer)
Waits for proxy map to get updated from a proxy map update message received from the master...
Definition: HandlerMap.cc:374
Declarations for IOHandlerDatagram.
void decomission_handler_unlocked(IOHandler *handler)
Decomissions handler.
Definition: HandlerMap.cc:214
Declarations for CommAddress.
void update_proxy_map(const char *message, size_t message_len)
Updates the proxy map with a proxy map update message received from the proxy master.
Definition: HandlerMap.cc:336
int set_alias(const InetAddr &addr, const InetAddr &alias)
Sets an alias address for an existing TCP address in map.
Definition: HandlerMap.cc:152
std::condition_variable m_cond_proxy
Condition variable for signalling proxy map load.
Definition: HandlerMap.h:412
bool destroy_ok(IOHandler *handler)
Determines if handler can be destoryed.
Definition: HandlerMap.cc:263
SockAddrMap< IOHandlerAccept * > m_accept_handler_map
Accept map (InetAddr-to-IOHandlerAccept)
Definition: HandlerMap.h:415
Declarations for IOHandlerRaw.
Data structure for mapping socket addresses to I/O handlers.
Definition: HandlerMap.h:69
int remove_handler_unlocked(IOHandler *handler)
Removes handler from map without locking m_mutex.
Definition: HandlerMap.cc:168
Declarations for ProxyMap.
bool m_proxies_loaded
Flag indicating if proxy map has been loaded.
Definition: HandlerMap.h:433
std::shared_ptr< HandlerMap > HandlerMapPtr
Smart pointer to HandlerMap.
Definition: HandlerMap.h:437
Unordered map specialization for InetAddr keys.
Definition: SockAddrMap.h:57
IOHandlerAccept * lookup_accept_handler(const InetAddr &addr)
Finds accept I/O handler associated with addr.
Definition: HandlerMap.cc:439
int remove_proxy(const String &proxy)
Removes a proxy name from the proxy map.
Definition: HandlerMap.cc:314
Encapsulate an internet address.
Definition: InetAddr.h:66
ProxyMap m_proxy_map
Proxy map.
Definition: HandlerMap.h:430
std::mutex m_mutex
Mutex for serializing concurrent access
Definition: HandlerMap.h:406
IOHandlerRaw * lookup_raw_handler(const InetAddr &addr)
Finds raw I/O handler associated with addr.
Definition: HandlerMap.cc:460
int remove_handler(IOHandler *handler)
Removes handler from map.
Definition: HandlerMap.cc:209
A timer class to keep timeout states across AsyncComm related calls.
Logging routines and macros.
std::unordered_map< String, ProxyAddressInfo > ProxyMapT
Forward mapping hash type from proxy name to ProxyAddressInfo.
Definition: ProxyMap.h:57
IOHandlerData * lookup_data_handler(const InetAddr &addr)
Finds data (TCP) I/O handler associated with addr.
Definition: HandlerMap.cc:446
void insert_handler(IOHandlerAccept *handler)
Inserts an accept handler.
Definition: HandlerMap.cc:39
Base class for socket descriptor I/O handlers.
Definition: IOHandler.h:76
I/O handler for TCP sockets.
Definition: IOHandlerData.h:51
void purge_handler(IOHandler *handler)
Purges (removes) handler.
Definition: HandlerMap.cc:286
Time related declarations.
Declarations for IOHandlerData.
void wait_for_empty()
Waits for map to become empty.
Definition: HandlerMap.cc:281
Declarations for SockAddrMap.
SockAddrMap< IOHandlerDatagram * > m_datagram_handler_map
Datagram (UDP) map (InetAddr-to-IOHandlerDatagram)
Definition: HandlerMap.h:421
Hypertable definitions
void get_proxy_map(ProxyMapT &proxy_map)
Returns the proxy map.
Definition: HandlerMap.cc:331
Declarations for CommBuf.
SockAddrMap< IOHandlerRaw * > m_raw_handler_map
Raw map (InetAddr-to-IOHandlerRaw)
Definition: HandlerMap.h:424
int contains_data_handler(const CommAddress &addr)
Checks to see if addr is contained in map.
Definition: HandlerMap.cc:137
void decomission_all()
Decomissions all handlers.
Definition: HandlerMap.cc:223
A timer class to keep timeout states across AsyncComm related calls.
Definition: Timer.h:44
bool translate_proxy_address(const CommAddress &proxy_addr, InetAddr *addr)
Translates proxy_addr to its corresponding IPV4 address.
Definition: HandlerMap.cc:270
HandlerMap()
Constructor.
Definition: HandlerMap.h:74
void decrement_reference_count(IOHandler *handler)
Decrements the reference count of handler.
Definition: HandlerMap.cc:132
int translate_address(const CommAddress &addr, InetAddr *inet_addr)
Translates addr to an InetAddr (IP address).
Definition: HandlerMap.cc:424
I/O handler for accept (listen) sockets.
int32_t propagate_proxy_map(IOHandlerData *handler)
Sends the current proxy map over connection identified by handler.
Definition: HandlerMap.cc:365
SockAddrMap< IOHandlerData * > m_data_handler_map
Data (TCP) map (InetAddr-to-IOHandlerData)
Definition: HandlerMap.h:418
void alias(const String &cmdline_opt, const String &file_opt, bool overwrite)
Setup command line option alias for config file option.
Definition: Config.cc:607
Maps a set of proxy names to their associated IP addresses.
Definition: ProxyMap.h:79
IOHandlerDatagram * lookup_datagram_handler(const InetAddr &addr)
Finds datagram I/O handler associated with addr.
Definition: HandlerMap.cc:453
void decomission_handler(IOHandler *handler)
Decomissions handler with m_mutex locked.
Definition: HandlerMap.h:217
int add_proxy(const String &proxy, const String &hostname, const InetAddr &addr)
Adds or updates proxy information.
Definition: HandlerMap.cc:299
Error codes, Exception handling, error logging.
I/O handler for raw sockets.
Definition: IOHandlerRaw.h:42
std::condition_variable m_cond
Condition variable for signalling empty map.
Definition: HandlerMap.h:409
Address abstraction to hold either proxy name or IPv4:port address.
Definition: CommAddress.h:52