0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Hypertable::ConnectionManager Class Reference

Establishes and maintains a set of TCP connections. More...

#include <ConnectionManager.h>

Inheritance diagram for Hypertable::ConnectionManager:
Inheritance graph
[legend]
Collaboration diagram for Hypertable::ConnectionManager:
Collaboration graph
[legend]

Classes

class  ConnectionState
 Per-connection state. More...
 
struct  LtConnectionState
 StringWeakOrdering for connection retry heap. More...
 
class  SharedImpl
 Connection manager state shared between Connection manager objects. More...
 

Public Member Functions

 ConnectionManager (Comm *comm=0)
 Constructor. More...
 
 ConnectionManager (const ConnectionManager &cm)=delete
 
virtual ~ConnectionManager ()
 Destructor. More...
 
void add (const CommAddress &addr, uint32_t timeout_ms, const char *service_name)
 Adds a connection. More...
 
void add (const CommAddress &addr, uint32_t timeout_ms, const char *service_name, DispatchHandlerPtr &handler)
 Adds a connection with a dispatch handler. More...
 
void add_with_initializer (const CommAddress &addr, uint32_t timeout_ms, const char *service_name, DispatchHandlerPtr &handler, ConnectionInitializerPtr &initializer)
 Adds a connection with a dispatch handler and connection initializer. More...
 
void add (const CommAddress &addr, const CommAddress &local_addr, uint32_t timeout_ms, const char *service_name)
 Adds a connection bound to a local address. More...
 
void add (const CommAddress &addr, const CommAddress &local_addr, uint32_t timeout_ms, const char *service_name, DispatchHandlerPtr &handler)
 Adds a connection with a dispatch handler bound to a local address. More...
 
int remove (const CommAddress &addr)
 Removes a connection from the connection manager. More...
 
bool wait_for_connection (const CommAddress &addr, uint32_t max_wait_ms)
 Blocks until the connection to the given address is established. More...
 
bool wait_for_connection (const CommAddress &addr, Timer &timer)
 Blocks until the connection to the given address is established. More...
 
Commget_comm ()
 Returns the Comm object associated with this connection manager. More...
 
void set_quiet_mode (bool mode)
 Sets the SharedImpl::quiet_mode flag which will disable the generation of log messages upon failed connection attempts. More...
 
virtual void handle (EventPtr &event)
 Primary dispatch handler method. More...
 
void connect_retry_loop ()
 Connect retry loop. More...
 
- Public Member Functions inherited from Hypertable::DispatchHandler
virtual ~DispatchHandler ()
 Destructor. More...
 

Private Types

enum  State { State::DISCONNECTED = 0, State::CONNECTED, State::READY, State::DECOMMISSIONED }
 
typedef std::shared_ptr
< ConnectionState
ConnectionStatePtr
 Smart pointer to ConnectionState. More...
 
typedef std::shared_ptr
< SharedImpl
SharedImplPtr
 Smart pointer to SharedImpl object. More...
 

Private Member Functions

void add_internal (const CommAddress &addr, const CommAddress &local_addr, uint32_t timeout_ms, const char *service_name, DispatchHandlerPtr &handler, ConnectionInitializerPtr &initializer)
 Called by the add methods to add a connection. More...
 
bool wait_for_connection (ConnectionStatePtr &conn_state, Timer &timer)
 This method blocks until the connection represented by conn_state is established. More...
 
void send_connect_request (ConnectionStatePtr &conn_state)
 Calls Comm::connect to establish a connection. More...
 
void send_initialization_request (ConnectionStatePtr &conn_state)
 Sends an initialization request. More...
 
void schedule_retry (ConnectionStatePtr &conn_state, const std::string &message)
 Schedules a connection retry attempt. More...
 

Private Attributes

SharedImplPtr m_impl
 Smart pointer to connection manager state. More...
 

Detailed Description

Establishes and maintains a set of TCP connections.

If any of the connections gets broken, then this class will continuously attempt to re-establish the connection, pausing for a while in between attempts.

Definition at line 60 of file ConnectionManager.h.

Member Typedef Documentation

Smart pointer to ConnectionState.

Definition at line 97 of file ConnectionManager.h.

typedef std::shared_ptr<SharedImpl> Hypertable::ConnectionManager::SharedImplPtr
private

Smart pointer to SharedImpl object.

Definition at line 144 of file ConnectionManager.h.

Constructor & Destructor Documentation

Hypertable::ConnectionManager::ConnectionManager ( Comm comm = 0)
inline

Constructor.

Creates a thread to do connection retry attempts.

Parameters
commPointer to the comm object

Definition at line 153 of file ConnectionManager.h.

Hypertable::ConnectionManager::ConnectionManager ( const ConnectionManager cm)
delete
virtual Hypertable::ConnectionManager::~ConnectionManager ( )
inlinevirtual

Destructor.

Definition at line 164 of file ConnectionManager.h.

Member Function Documentation

void ConnectionManager::add ( const CommAddress addr,
uint32_t  timeout_ms,
const char *  service_name 
)

Adds a connection.

The addr parameter holds the address to which the connection manager should maintain a connection. This method first checks to see if the address is already registered with the connection manager and returns immediately if it is. Otherwise, it adds the address to an internal connection map, attempts to establish a connection to the address, and then returns. Once a connection has been added, the internal manager thread will maintian the connection by continually re-establishing the connection if it ever gets broken.

Parameters
addrThe address to maintain a connection to
timeout_msWhen connection dies, wait this many milliseconds before attempting to reestablish
service_nameThe name of the serivce at the other end of the connection used for descriptive log messages

Definition at line 74 of file ConnectionManager.cc.

void ConnectionManager::add ( const CommAddress addr,
uint32_t  timeout_ms,
const char *  service_name,
DispatchHandlerPtr handler 
)

Adds a connection with a dispatch handler.

The addr parameter holds the address to which connection manager should maintain a connection. This method first checks to see if the address is already registered with the connection manager and returns immediately if it is. Otherwise, it adds the address to an internal connection map, attempts to establish a connection to the address, and then returns. Once a connection has been added, the internal manager thread will maintian the connection by continually re-establishing the connection if it ever gets broken.

This version of add accepts a DispatchHandler argument, handler, which is registered as the connection handler for the connection and receives all events that occur on the connection (except for the initialization handshake messages).

Parameters
addrThe address to maintain a connection to
timeout_msThe timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period betweeen connection attempts
service_nameThe name of the serivce at the other end of the connection used for descriptive log messages
handlerThis is the default handler to install on the connection. All events get changed through to this handler.

Definition at line 58 of file ConnectionManager.cc.

void ConnectionManager::add ( const CommAddress addr,
const CommAddress local_addr,
uint32_t  timeout_ms,
const char *  service_name 
)

Adds a connection bound to a local address.

The addr holds the address to which the connection manager should maintain a connection. This method first checks to see if the address is already registered with the connection manager and returns immediately if it is. Otherwise, it adds the address to an internal connection map, attempts to establish a connection to the address, and then returns. From here on out, the internal manager thread will maintian the connection by continually re-establishing the connection if it ever gets broken.

Parameters
addrThe address to maintain a connection to
local_addrThe local address to bind to
timeout_msWhen connection dies, wait this many milliseconds before attempting to reestablish
service_nameThe name of the serivce at the other end of the connection used for descriptive log messages

Definition at line 91 of file ConnectionManager.cc.

void ConnectionManager::add ( const CommAddress addr,
const CommAddress local_addr,
uint32_t  timeout_ms,
const char *  service_name,
DispatchHandlerPtr handler 
)

Adds a connection with a dispatch handler bound to a local address.

The addr holds the address to which the connection manager should maintain a connection. This method first checks to see if the address is already registered with the connection manager and returns immediately if it is. Otherwise, it adds the address to an internal connection map, attempts to establish a connection to the address, and then returns. From here on out, the internal manager thread will maintian the connection by continually re-establishing the connection if it ever gets broken.

Parameters
addrThe address to maintain a connection to
local_addrThe local address to bind to
timeout_msThe timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period betweeen connection attempts
service_nameThe name of the serivce at the other end of the connection used for descriptive log messages
handlerThis is the default handler to install on the connection. All events get changed through to this handler.

Definition at line 82 of file ConnectionManager.cc.

void ConnectionManager::add_internal ( const CommAddress addr,
const CommAddress local_addr,
uint32_t  timeout_ms,
const char *  service_name,
DispatchHandlerPtr handler,
ConnectionInitializerPtr initializer 
)
private

Called by the add methods to add a connection.

This method creates and initializes a ConnectionState object for the connnection, adds it to either SharedImpl::conn_map or SharedImpl::conn_map_proxy depending on the type of address, and then issues a connect request.

Parameters
addrThe address to maintain a connection to
local_addrThe local address to bind to
timeout_msThe timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period betweeen connection attempts
service_nameThe name of the serivce at the other end of the connection used for descriptive log messages
handlerThis is the default handler to install on the connection. All events get changed through to this handler.
initializerConnection initialization handshake driver

Start retry thread

Definition at line 98 of file ConnectionManager.cc.

void ConnectionManager::add_with_initializer ( const CommAddress addr,
uint32_t  timeout_ms,
const char *  service_name,
DispatchHandlerPtr handler,
ConnectionInitializerPtr initializer 
)

Adds a connection with a dispatch handler and connection initializer.

The addr parameter holds the address to which connection manager should maintain a connection. This method first checks to see if the address is already registered with the connection manager and returns immediately if it is. Otherwise, it adds the address to an internal connection map, attempts to establish a connection to the address, and then returns. Once a connection has been added, the internal manager thread will maintian the connection by continually re-establishing the connection if it ever gets broken.

This version of add accepts a connection initializer, initializer, which is used to carry out an initialization handshake. Once the connection has been established, the connection initializer is used to generate an initialization handshake message which is sent to the other end of the connection. and process the response t

Parameters
addrAddress to maintain a connection to
timeout_msTimeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period between connection attempts
service_nameName of the serivce at the other end of the connection used for descriptive log messages
handlerDefault dispatch handler for connection.
initializerConnection initialization handshake driver

Definition at line 65 of file ConnectionManager.cc.

void ConnectionManager::connect_retry_loop ( )

Connect retry loop.

This is the boost::thread run method.

This method is called as the retry thread function.

Definition at line 460 of file ConnectionManager.cc.

Comm* Hypertable::ConnectionManager::get_comm ( )
inline

Returns the Comm object associated with this connection manager.

Returns
the assocated comm object

Definition at line 321 of file ConnectionManager.h.

void ConnectionManager::handle ( EventPtr event)
virtual

Primary dispatch handler method.

This is the AsyncComm dispatch handler method.

The ConnectionManager is a dispatch handler and is registered as the handler for all of the connections that it manages. This method does the job of maintianing connections. This method will forward all events (except initialization handshake messages) to any dispatch handler registered for the connection on which the event was generated.

Parameters
eventComm layer event

It gets called for each connection related event (establishment, disconnect, etc.) for each connection. For connect events, the connection's connected flag is set to true and it's condition variable is signaled. For all other events (e.g. disconnect or error), the connection's connected flag is set to false and a retry is scheduled.

Parameters
eventshared pointer to event object

register address

Implements Hypertable::DispatchHandler.

Definition at line 334 of file ConnectionManager.cc.

int ConnectionManager::remove ( const CommAddress addr)

Removes a connection from the connection manager.

Parameters
addrremote address of connection to remove
Returns
Error code (Error::OK on success)

Definition at line 265 of file ConnectionManager.cc.

void ConnectionManager::schedule_retry ( ConnectionStatePtr conn_state,
const std::string &  message 
)
private

Schedules a connection retry attempt.

Sets conn_state to disconnected and schedules another connection attempt in the future.

Parameters
conn_statePointer to connection state object representing connection
messageMessage indicating why retry is being attempted

Definition at line 437 of file ConnectionManager.cc.

void ConnectionManager::send_connect_request ( ConnectionStatePtr conn_state)
private

Calls Comm::connect to establish a connection.

Attempts to establish a connection for the given ConnectionState object.

If the connection attempt results in an error, an error message is logged indicating that the connection attempt to the given service failed, and another connection attempt is scheduled for ConnectionState::timeout_ms in the future.

Parameters
conn_statePointer to connection state object representing connection

If a failure occurs, it prints an error message and then schedules a retry by updating the next_retry member of the conn_state object and pushing it onto the retry heap

Parameters
conn_stateThe connection state record

Definition at line 209 of file ConnectionManager.cc.

void ConnectionManager::send_initialization_request ( ConnectionStatePtr conn_state)
private

Sends an initialization request.

Parameters
conn_statePointer to connection state object representing connection

Definition at line 420 of file ConnectionManager.cc.

void Hypertable::ConnectionManager::set_quiet_mode ( bool  mode)
inline

Sets the SharedImpl::quiet_mode flag which will disable the generation of log messages upon failed connection attempts.

It is set to false by default.

Parameters
modeThe new value for the SharedImpl::quiet_mode flag

Definition at line 329 of file ConnectionManager.h.

bool ConnectionManager::wait_for_connection ( const CommAddress addr,
uint32_t  max_wait_ms 
)

Blocks until the connection to the given address is established.

The given address must have been previously added with a call to Add. If the connection is not established within max_wait_ms, then the method returns false.

Parameters
addrthe address of the connection to wait for
max_wait_msThe maximum time to wait for the connection before returning
Returns
true if connected, false otherwise

Definition at line 145 of file ConnectionManager.cc.

bool ConnectionManager::wait_for_connection ( const CommAddress addr,
Timer timer 
)

Blocks until the connection to the given address is established.

The given address must have been previously added with a call to Add. If the connection is not established before the timer expires, then the method returns false.

Parameters
addrthe address of the connection to wait for
timerrunning timer object
Returns
true if connected, false otherwise

Definition at line 153 of file ConnectionManager.cc.

bool ConnectionManager::wait_for_connection ( ConnectionStatePtr conn_state,
Timer timer 
)
private

This method blocks until the connection represented by conn_state is established.

If the connection is not established before timer expires, then the method returns false.

Parameters
conn_statePointer to connection state object representing connection
timerMaximum wait timer
Returns
true if connected, false if timer expired before connection was established.

Definition at line 178 of file ConnectionManager.cc.

Member Data Documentation

SharedImplPtr Hypertable::ConnectionManager::m_impl
private

Smart pointer to connection manager state.

Definition at line 406 of file ConnectionManager.h.


The documentation for this class was generated from the following files: