Establishes and maintains a set of TCP connections. More...
#include <ConnectionManager.h>
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... | |
Comm * | get_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... | |
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.
|
private |
Smart pointer to ConnectionState.
Definition at line 97 of file ConnectionManager.h.
|
private |
Smart pointer to SharedImpl object.
Definition at line 144 of file ConnectionManager.h.
|
inline |
Constructor.
Creates a thread to do connection retry attempts.
comm | Pointer to the comm object |
Definition at line 153 of file ConnectionManager.h.
|
delete |
|
inlinevirtual |
Destructor.
Definition at line 164 of file ConnectionManager.h.
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.
addr | The address to maintain a connection to |
timeout_ms | When connection dies, wait this many milliseconds before attempting to reestablish |
service_name | The 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).
addr | The address to maintain a connection to |
timeout_ms | The timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period betweeen connection attempts |
service_name | The name of the serivce at the other end of the connection used for descriptive log messages |
handler | This 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.
addr | The address to maintain a connection to |
local_addr | The local address to bind to |
timeout_ms | When connection dies, wait this many milliseconds before attempting to reestablish |
service_name | The 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.
addr | The address to maintain a connection to |
local_addr | The local address to bind to |
timeout_ms | The timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period betweeen connection attempts |
service_name | The name of the serivce at the other end of the connection used for descriptive log messages |
handler | This 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.
|
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.
addr | The address to maintain a connection to |
local_addr | The local address to bind to |
timeout_ms | The timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period betweeen connection attempts |
service_name | The name of the serivce at the other end of the connection used for descriptive log messages |
handler | This is the default handler to install on the connection. All events get changed through to this handler. |
initializer | Connection 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
addr | Address to maintain a connection to |
timeout_ms | Timeout value (in milliseconds) that gets passed into Comm::connect and also used as the waiting period between connection attempts |
service_name | Name of the serivce at the other end of the connection used for descriptive log messages |
handler | Default dispatch handler for connection. |
initializer | Connection 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.
|
inline |
Returns the Comm object associated with this connection manager.
Definition at line 321 of file ConnectionManager.h.
|
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.
event | Comm 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.
event | shared 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.
addr | remote address of connection to remove |
Definition at line 265 of file ConnectionManager.cc.
|
private |
Schedules a connection retry attempt.
Sets conn_state
to disconnected and schedules another connection attempt in the future.
conn_state | Pointer to connection state object representing connection |
message | Message indicating why retry is being attempted |
Definition at line 437 of file ConnectionManager.cc.
|
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.
conn_state | Pointer 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
conn_state | The connection state record |
Definition at line 209 of file ConnectionManager.cc.
|
private |
Sends an initialization request.
conn_state | Pointer to connection state object representing connection |
Definition at line 420 of file ConnectionManager.cc.
|
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.
mode | The 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.
addr | the address of the connection to wait for |
max_wait_ms | The maximum time to wait for the connection before returning |
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.
addr | the address of the connection to wait for |
timer | running timer object |
Definition at line 153 of file ConnectionManager.cc.
|
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.
conn_state | Pointer to connection state object representing connection |
timer | Maximum wait timer |
timer
expired before connection was established. Definition at line 178 of file ConnectionManager.cc.
|
private |
Smart pointer to connection manager state.
Definition at line 406 of file ConnectionManager.h.