Data structure for mapping socket addresses to I/O handlers. More...
#include <HandlerMap.h>
Public Member Functions | |
HandlerMap () | |
Constructor. More... | |
void | insert_handler (IOHandlerAccept *handler) |
Inserts an accept handler. More... | |
void | insert_handler (IOHandlerData *handler, bool checkout=false) |
Inserts a data (TCP) handler. More... | |
void | insert_handler (IOHandlerDatagram *handler) |
Inserts a datagram (UDP) handler. More... | |
void | insert_handler (IOHandlerRaw *handler) |
Inserts a raw handler. More... | |
int | checkout_handler (const CommAddress &addr, IOHandlerAccept **handler) |
Checks out accept I/O handler associated with addr . More... | |
int | checkout_handler (const CommAddress &addr, IOHandlerData **handler) |
Checks out data (TCP) I/O handler associated with addr . More... | |
int | checkout_handler (const CommAddress &addr, IOHandlerDatagram **handler) |
Checks out datagram (UDP) I/O handler associated with addr . More... | |
int | checkout_handler (const CommAddress &addr, IOHandlerRaw **handler) |
Checks out raw I/O handler associated with addr . More... | |
int | contains_data_handler (const CommAddress &addr) |
Checks to see if addr is contained in map. More... | |
void | decrement_reference_count (IOHandler *handler) |
Decrements the reference count of handler . More... | |
int | set_alias (const InetAddr &addr, const InetAddr &alias) |
Sets an alias address for an existing TCP address in map. More... | |
int | remove_handler (IOHandler *handler) |
Removes handler from map. More... | |
void | decomission_handler_unlocked (IOHandler *handler) |
Decomissions handler . More... | |
void | decomission_handler (IOHandler *handler) |
Decomissions handler with m_mutex locked. More... | |
void | decomission_all () |
Decomissions all handlers. More... | |
bool | destroy_ok (IOHandler *handler) |
Determines if handler can be destoryed. More... | |
bool | translate_proxy_address (const CommAddress &proxy_addr, InetAddr *addr) |
Translates proxy_addr to its corresponding IPV4 address. More... | |
void | purge_handler (IOHandler *handler) |
Purges (removes) handler . More... | |
void | wait_for_empty () |
Waits for map to become empty. More... | |
int | add_proxy (const String &proxy, const String &hostname, const InetAddr &addr) |
Adds or updates proxy information. More... | |
int | remove_proxy (const String &proxy) |
Removes a proxy name from the proxy map. More... | |
void | get_proxy_map (ProxyMapT &proxy_map) |
Returns the proxy map. More... | |
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. More... | |
int32_t | propagate_proxy_map (IOHandlerData *handler) |
Sends the current proxy map over connection identified by handler . More... | |
bool | wait_for_proxy_map (Timer &timer) |
Waits for proxy map to get updated from a proxy map update message received from the master. More... | |
Private Member Functions | |
int | propagate_proxy_map (ProxyMapT &mappings) |
Propagates proxy map information in mappings to all active data (TCP) connections. More... | |
int | translate_address (const CommAddress &addr, InetAddr *inet_addr) |
Translates addr to an InetAddr (IP address). More... | |
int | remove_handler_unlocked (IOHandler *handler) |
Removes handler from map without locking m_mutex. More... | |
IOHandlerAccept * | lookup_accept_handler (const InetAddr &addr) |
Finds accept I/O handler associated with addr . More... | |
IOHandlerData * | lookup_data_handler (const InetAddr &addr) |
Finds data (TCP) I/O handler associated with addr . More... | |
IOHandlerDatagram * | lookup_datagram_handler (const InetAddr &addr) |
Finds datagram I/O handler associated with addr . More... | |
IOHandlerRaw * | lookup_raw_handler (const InetAddr &addr) |
Finds raw I/O handler associated with addr . More... | |
Private Attributes | |
std::mutex | m_mutex |
Mutex for serializing concurrent access More... | |
std::condition_variable | m_cond |
Condition variable for signalling empty map. More... | |
std::condition_variable | m_cond_proxy |
Condition variable for signalling proxy map load. More... | |
SockAddrMap< IOHandlerAccept * > | m_accept_handler_map |
Accept map (InetAddr-to-IOHandlerAccept) More... | |
SockAddrMap< IOHandlerData * > | m_data_handler_map |
Data (TCP) map (InetAddr-to-IOHandlerData) More... | |
SockAddrMap< IOHandlerDatagram * > | m_datagram_handler_map |
Datagram (UDP) map (InetAddr-to-IOHandlerDatagram) More... | |
SockAddrMap< IOHandlerRaw * > | m_raw_handler_map |
Raw map (InetAddr-to-IOHandlerRaw) More... | |
std::set< IOHandler * > | m_decomissioned_handlers |
Decomissioned handler set. More... | |
ProxyMap | m_proxy_map |
Proxy map. More... | |
bool | m_proxies_loaded |
Flag indicating if proxy map has been loaded. More... | |
Data structure for mapping socket addresses to I/O handlers.
An I/O handler is associated with each socket connection and is used to handle polling events on the socket descriptor. Examples incude writing a message to the socket, reading a message from the socket, or completing a connection request. This class maintains three maps, one for TCP socket connections, UDP socket connections, and one for accept sockets. The Comm methods use this map to locate the I/O handler for a given address.
Definition at line 69 of file HandlerMap.h.
|
inline |
Constructor.
Definition at line 74 of file HandlerMap.h.
Adds or updates proxy information.
This method adds or updates proxy information in m_proxy_map. For the data handler to which addr
refers, it updates its proxy name via a call to IOHandler::set_proxy and then calls propagate_proxy_map with the newly added/updated proxy information to update all active data connections with the new proxy information.
proxy | Proxy name of new/updated mapping |
hostname | Hostname of new/updated mapping |
addr | InetAddr of new/updated mapping |
Definition at line 299 of file HandlerMap.cc.
int HandlerMap::checkout_handler | ( | const CommAddress & | addr, |
IOHandlerAccept ** | handler | ||
) |
Checks out accept I/O handler associated with addr
.
Looks up addr
in accept map. If an entry is found, then its reference count is incremented and it is returned in handler
.
addr | Connection address |
handler | Address of handler pointer returned |
addr
. Definition at line 70 of file HandlerMap.cc.
int HandlerMap::checkout_handler | ( | const CommAddress & | addr, |
IOHandlerData ** | handler | ||
) |
Checks out data (TCP) I/O handler associated with addr
.
First translates addr
to socket address and then looks up translated address in data map. If an entry is found, then its reference count is incremented and it is returned in handler
.
addr | Connection address |
handler | Address of handler pointer returned |
addr
is of type CommAddress::PROXY and no translation exists, or Error::COMM_NOT_CONNECTED if no mapping found for translated address. Definition at line 85 of file HandlerMap.cc.
int HandlerMap::checkout_handler | ( | const CommAddress & | addr, |
IOHandlerDatagram ** | handler | ||
) |
Checks out datagram (UDP) I/O handler associated with addr
.
Looks up addr
in datagram map. If an entry is found, then its reference count is incremented and it is returned in handler
.
addr | Connection address |
handler | Address of handler pointer returned |
addr
. Definition at line 104 of file HandlerMap.cc.
int HandlerMap::checkout_handler | ( | const CommAddress & | addr, |
IOHandlerRaw ** | handler | ||
) |
Checks out raw I/O handler associated with addr
.
Looks up addr
in raw map. If an entry is found, then its reference count is incremented and it is returned in handler
.
addr | Connection address |
handler | Address of handler pointer returned |
addr
. Definition at line 118 of file HandlerMap.cc.
int HandlerMap::contains_data_handler | ( | const CommAddress & | addr | ) |
Checks to see if addr
is contained in map.
First translates addr
to socket address and then looks up translated address in data map.
addr | Connection address |
addr
is of type CommAddress::PROXY and no translation exists, or Error::COMM_NOT_CONNECTED if no mapping found for translated address. Definition at line 137 of file HandlerMap.cc.
void HandlerMap::decomission_all | ( | ) |
Decomissions all handlers.
This method is called by the ~Comm to decomission all of the handlers in the map.
Definition at line 223 of file HandlerMap.cc.
|
inline |
Decomissions handler
with m_mutex locked.
This method locks m_mutex and calls decomission_handler_unlocked
handler | Pointer to IOHandler to decomission |
Definition at line 217 of file HandlerMap.h.
void HandlerMap::decomission_handler_unlocked | ( | IOHandler * | handler | ) |
Decomissions handler
.
Since handler pointers are passed into the polling mechanism and asynchronously referenced by reactor threads, care must be taken to not delete a handler until it has be removed from the polling mechanism. This is accomplished by introducing a two-step removal process. First a handler is decomissioned (by this method) by removing it from the associated map, adding it to the m_decomissioned_handlers set and marking it decomissioned. Once there are no more references to the handler, it may be safely removed. The removal is accomplished via purge_handler which is called by the reactor thread after it has been removed from the polling interface.
handler | Pointer to IOHandler to decomission |
Definition at line 214 of file HandlerMap.cc.
void HandlerMap::decrement_reference_count | ( | IOHandler * | handler | ) |
Decrements the reference count of handler
.
The decrementing of a handler's reference count is done by this method with m_mutex locked which avoids a race condition between checking out handlers and purging them.
handler | Pointer to I/O handler for which to decrement reference count |
Definition at line 132 of file HandlerMap.cc.
bool HandlerMap::destroy_ok | ( | IOHandler * | handler | ) |
Determines if handler
can be destoryed.
handler
is decomissioned and has a reference count of 0. Definition at line 263 of file HandlerMap.cc.
void HandlerMap::get_proxy_map | ( | ProxyMapT & | proxy_map | ) |
Returns the proxy map.
proxy_map | reference to returned proxy map |
Definition at line 331 of file HandlerMap.cc.
void HandlerMap::insert_handler | ( | IOHandlerAccept * | handler | ) |
Inserts an accept handler.
Uses IOHandler::m_local_addr as the key
handler | Accept I/O handler to insert |
Definition at line 39 of file HandlerMap.cc.
void HandlerMap::insert_handler | ( | IOHandlerData * | handler, |
bool | checkout = false |
||
) |
Inserts a data (TCP) handler.
Uses IOHandler::m_addr as the key. If program is the proxy master, a proxy map update message with the new mapping is broadcast to all connections.
handler | Data (TCP) I/O handler to insert |
checkout | Atomically checkout handler |
Definition at line 46 of file HandlerMap.cc.
void HandlerMap::insert_handler | ( | IOHandlerDatagram * | handler | ) |
Inserts a datagram (UDP) handler.
Uses IOHandler::m_local_addr as the key.
handler | Datagram (UDP) I/O handler to insert |
Definition at line 55 of file HandlerMap.cc.
void HandlerMap::insert_handler | ( | IOHandlerRaw * | handler | ) |
Inserts a raw handler.
Uses IOHandler::m_addr as the key.
handler | Raw I/O handler to insert |
Definition at line 62 of file HandlerMap.cc.
|
private |
Finds accept I/O handler associated with addr
.
This method looks up addr
in m_accept_handler_map and returns the handler, if found.
addr | Address of accept handler to locate |
addr
, or 0 if not found. Definition at line 439 of file HandlerMap.cc.
|
private |
Finds data (TCP) I/O handler associated with addr
.
This method looks up addr
in m_data_handler_map and returns the handler, if found.
addr | Address of data handler to locate |
addr
, or 0 if not found. Definition at line 446 of file HandlerMap.cc.
|
private |
Finds datagram I/O handler associated with addr
.
This method looks up addr
in m_datagram_handler_map and returns the handler, if found.
addr | Address of datagram handler to locate |
addr
, or 0 if not found. Definition at line 453 of file HandlerMap.cc.
|
private |
Finds raw I/O handler associated with addr
.
This method looks up addr
in m_raw_handler_map and returns the handler, if found.
addr | Address of raw handler to locate |
addr
, or nullptr if not found. Definition at line 460 of file HandlerMap.cc.
int32_t HandlerMap::propagate_proxy_map | ( | IOHandlerData * | handler | ) |
Sends the current proxy map over connection identified by handler
.
This method must only be called by the proxy master, otherwise it will assert.
handler | Connection over which to send proxy map |
Definition at line 365 of file HandlerMap.cc.
|
private |
Propagates proxy map information in mappings
to all active data (TCP) connections.
This method creates a proxy map update message from the mappings in mappings
. The update message is just a list of mapping entries in the following format:
<proxy> '\t' <hostname> '\t' <addr> '\n'
Then the proxy map update message is sent via each of the handlers in the data (TCP) handler map. If an error is encountered on a handler when trying to send the proxy map, it will be decomissioned.
mappings | Proxy map information to propagate. |
Definition at line 383 of file HandlerMap.cc.
void HandlerMap::purge_handler | ( | IOHandler * | handler | ) |
Purges (removes) handler
.
This method removes handler
from the m_decomissioned_handlers set, signals m_cond if m_decomissioned_handlers becomes empty, calls hander->disconnect()
, and then deletes the handler. This method must only be called from a reactor thread after the handler has been removed from the polling interface and destroy_ok returns true for the handler.
handler | Handler to purge |
Definition at line 286 of file HandlerMap.cc.
int HandlerMap::remove_handler | ( | IOHandler * | handler | ) |
Removes handler
from map.
This method removes handler
from the data, datagram, or accept map, depending on the type of handler. If handler
refers to a data handler, then its alias address entry is also removed from the data map.
handler | IOHandler to remove |
handler
is not found in any of the maps. Definition at line 209 of file HandlerMap.cc.
|
private |
Removes handler
from map without locking m_mutex.
This method removes handler
from the data, datagram, or accept map, depending on the type of handler. If handler
refers to a data handler, then its alias address entry is also removed from the data map.
handler | IOHandler to remove |
handler
is not found in any of the maps. Definition at line 168 of file HandlerMap.cc.
int HandlerMap::remove_proxy | ( | const String & | proxy | ) |
Removes a proxy name from the proxy map.
This method removes proxy
from m_proxy_map and then calls propagate_proxy_map to propagate the removed mapping information to all connections.
proxy | Proxy name to remove |
proxy
not found in proxy map or if it was successfully removed, or one of the errors returned by propagate_proxy_map Definition at line 314 of file HandlerMap.cc.
Sets an alias address for an existing TCP address in map.
RangeServers listen on a well-known port defined by the Hypertable.RangeServer.Port
configuration property (default = 15865). However, RangeServers connect to the master using an ephemeral port due to a bind conflict with its listen socket. So that the Master can refer to the RangeServer using the well-known port, an alias address can be registered and subsequently used to reference the connection. This method adds an entry to the data (TCP) map for alias
which references the IOHandlerData object previously registered under addr
.
addr | Address of previously registered data handler |
alias | Alias address to add |
alias
is already in the data map, or Error::COMM_NOT_CONNECTED if addr
is not found in the data map. Definition at line 152 of file HandlerMap.cc.
|
private |
Translates addr
to an InetAddr (IP address).
If addr
is of type CommAddress::PROXY, then the m_proxy_map is consulted to translate the proxy name to an IP address, otherwise if addr
is of type CommAddress::INET, the IP address held in addr
is copied to inet_addr
.
addr | Address to translate |
inet_addr | Pointer to valid InetAddr object to hold translated address |
addr
is of type CommAddress::PROXY and no mapping is found. Definition at line 424 of file HandlerMap.cc.
bool HandlerMap::translate_proxy_address | ( | const CommAddress & | proxy_addr, |
InetAddr * | addr | ||
) |
Translates proxy_addr
to its corresponding IPV4 address.
This method fetches the mapping for proxy_addr
from m_proxy_map and returns the associated IPV4 address in addr
. If addr
is NULL, then no translation occurs but the return value can be checked to see if the proxy name contains a mapping.
proxy_addr | Reference to proxy address |
addr | Pointer to return IPV4 address |
Definition at line 270 of file HandlerMap.cc.
void HandlerMap::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.
Calls ProxyMap::update_mappings with message
to update the proxy map. If any of the proxy names have changed, the corresponding data handlers are updated with a call to IOHandler::set_proxy. For each mapping in message
that has the hostname set to –DELETED–
, the associated data handler is decomissioned. After the proxy map has been successfuly updated, the m_proxies_loaded flag is set to true and the m_cond_proxy condition variable is signalled.
message | Pointer to proxy map update message |
message_len | Length of proxy map update message |
Definition at line 336 of file HandlerMap.cc.
void HandlerMap::wait_for_empty | ( | ) |
Waits for map to become empty.
This method assumes that all of the handlers in the map have been decomissioned. It waits for the m_decomissioned_handlers set to become empty, waiting on m_cond until it does.
Definition at line 281 of file HandlerMap.cc.
bool HandlerMap::wait_for_proxy_map | ( | Timer & | timer | ) |
Waits for proxy map to get updated from a proxy map update message received from the master.
This method waits on m_cond_proxy for m_proxies_loaded to become true or timer
expires.
timer | Deadline timer |
timer
expired before proxy map was loaded. Definition at line 374 of file HandlerMap.cc.
|
private |
Accept map (InetAddr-to-IOHandlerAccept)
Definition at line 415 of file HandlerMap.h.
|
private |
Condition variable for signalling empty map.
Definition at line 409 of file HandlerMap.h.
|
private |
Condition variable for signalling proxy map load.
Definition at line 412 of file HandlerMap.h.
|
private |
Data (TCP) map (InetAddr-to-IOHandlerData)
Definition at line 418 of file HandlerMap.h.
|
private |
Datagram (UDP) map (InetAddr-to-IOHandlerDatagram)
Definition at line 421 of file HandlerMap.h.
|
private |
Decomissioned handler set.
Definition at line 427 of file HandlerMap.h.
|
private |
Mutex for serializing concurrent access
Definition at line 406 of file HandlerMap.h.
|
private |
Flag indicating if proxy map has been loaded.
Definition at line 433 of file HandlerMap.h.
|
private |
Proxy map.
Definition at line 430 of file HandlerMap.h.
|
private |
Raw map (InetAddr-to-IOHandlerRaw)
Definition at line 424 of file HandlerMap.h.