LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends
LogCabin::RPC::ClientSession Class Reference

A ClientSession is used to initiate OpaqueClientRPCs. More...

#include <ClientSession.h>

List of all members.

Classes

class  MessageSocketHandler
 This handles events from messageSocket. More...
struct  Response
 This contains an expected response for a OpaqueClientRPC object. More...
class  Timer
 This is used to time out RPCs and sessions when the server is no longer responding. More...

Public Types

typedef Address::Clock Clock
 Clock used for timeouts.
typedef Address::TimePoint TimePoint
 Type for absolute time values used for timeouts.

Public Member Functions

 ~ClientSession ()
 Destructor.
OpaqueClientRPC sendRequest (Core::Buffer request)
 Initiate an RPC.
std::string getErrorMessage () const
 If the socket has been disconnected, return a descriptive message.
std::string toString () const
 Return a string describing this session.

Static Public Member Functions

static std::shared_ptr
< ClientSession
makeSession (Event::Loop &eventLoop, const Address &address, uint32_t maxMessageLength, TimePoint timeout, const Core::Config &config)
 Return a new ClientSession object.
static std::shared_ptr
< ClientSession
makeErrorSession (Event::Loop &eventLoop, const std::string &errorMessage)
 Return a ClientSession object that's already in an error state.

Private Member Functions

 ClientSession (Event::Loop &eventLoop, const Address &address, uint32_t maxMessageLength, TimePoint timeout, const Core::Config &config)
 This constructor is private because the class must be allocated in a particular way.
void cancel (OpaqueClientRPC &rpc)
 Called by the RPC when it is no longer interested in its response.
void update (OpaqueClientRPC &rpc)
 Called by the RPC when it wants to be learn of its response (non-blocking).
void wait (const OpaqueClientRPC &rpc, TimePoint timeout)
 Called by the RPC to wait for its response (blocking).
 ClientSession (const ClientSession &)
ClientSessionoperator= (const ClientSession &)

Private Attributes

std::weak_ptr< ClientSessionself
 This is used to keep this object alive while there are outstanding RPCs.
const uint64_t PING_TIMEOUT_NS
 The number of nanoseconds to wait until the client gets suspicious about the server not responding.
Event::LoopeventLoop
 The event loop that is used for non-blocking I/O.
const Address address
 The RPC server address provided to the constructor.
MessageSocketHandler messageSocketHandler
 Receives events from messageSocket.
Timer timer
 This is used to time out RPCs and sessions when the server is no longer responding.
std::mutex mutex
 This mutex protects several members of this class:
MessageSocket::MessageId nextMessageId
 The message ID to assign to the next RPC.
std::unordered_map
< MessageSocket::MessageId,
Response * > 
responses
 A map from MessageId to Response objects that is used to store the response to RPCs and look it up for OpaqueClientRPC objects.
std::string errorMessage
 If this session is disconnected then this holds the error message.
uint32_t numActiveRPCs
 The number of outstanding RPC requests that have been sent but whose responses have not yet been received.
bool activePing
 When numActiveRPCs is > 0, this field indicates that we are waiting for a ping response as evidence that the server is still alive.
std::unique_ptr< MessageSocketmessageSocket
 The MessageSocket used to send RPC requests and receive RPC responses.
Event::Timer::Monitor timerMonitor
 Registers timer with the event loop.

Static Private Attributes

static std::function< int(int
sockfd, const struct sockaddr
*addr, socklen_t addrlen) 
connectFn )
 Usually set to connect() but mocked out in some unit tests.

Friends

class OpaqueClientRPC

Detailed Description

A ClientSession is used to initiate OpaqueClientRPCs.

It encapsulates a connection to a server. Sessions can be relatively expensive to create, so clients should keep them around.

Definition at line 47 of file ClientSession.h.


Member Typedef Documentation

Clock used for timeouts.

Definition at line 50 of file ClientSession.h.

Type for absolute time values used for timeouts.

Definition at line 52 of file ClientSession.h.


Constructor & Destructor Documentation

LogCabin::RPC::ClientSession::ClientSession ( Event::Loop eventLoop,
const Address address,
uint32_t  maxMessageLength,
TimePoint  timeout,
const Core::Config config 
) [private]

This constructor is private because the class must be allocated in a particular way.

See makeSession().

Definition at line 221 of file ClientSession.cc.

Destructor.

Definition at line 369 of file ClientSession.cc.


Member Function Documentation

std::shared_ptr< ClientSession > LogCabin::RPC::ClientSession::makeSession ( Event::Loop eventLoop,
const Address address,
uint32_t  maxMessageLength,
TimePoint  timeout,
const Core::Config config 
) [static]

Return a new ClientSession object.

This object is managed by a std::shared_ptr to ensure that it remains alive while there are outstanding RPCs.

This should only be used from worker threads, as it invokes possibly long-running syscalls.

Parameters:
eventLoopEvent::Loop that will be used to find out when the underlying socket may be read from or written to without blocking.
addressThe RPC server address on which to connect.
maxMessageLengthThe maximum number of bytes to allow per request/response. This exists to limit the amount of buffer space a single RPC can use. Attempting to send longer requests will PANIC; attempting to receive longer requests will disconnect the underlying socket.
timeoutAfter this time has elapsed, stop trying to initiate the connection and leave the session in an error state.
configGeneral settings. This object does not keep a reference.

Definition at line 335 of file ClientSession.cc.

std::shared_ptr< ClientSession > LogCabin::RPC::ClientSession::makeErrorSession ( Event::Loop eventLoop,
const std::string &  errorMessage 
) [static]

Return a ClientSession object that's already in an error state.

This can be useful for delaying errors until an RPC is waited on.

Parameters:
eventLoopIgnored but usually readily available to callers and needed to satisfy type requirements.
errorMessageDescription of the error, as will be returned by getErrorMessage() later.

Definition at line 352 of file ClientSession.cc.

Initiate an RPC.

This method is safe to call from any thread.

Parameters:
requestThe contents of the RPC request.
Returns:
This is be used to wait for and retrieve the reply to the RPC.

Definition at line 378 of file ClientSession.cc.

If the socket has been disconnected, return a descriptive message.

The suggested way to detect errors is to wait until an RPC returns an error. This method can be used to detect errors earlier.

This method is safe to call from any thread.

Returns:
If an error has occurred, a message describing that error. Otherwise, an empty string.

Definition at line 405 of file ClientSession.cc.

Return a string describing this session.

It will include the address of the server and, if the session has an error, the error message.

Definition at line 412 of file ClientSession.cc.

Called by the RPC when it is no longer interested in its response.

This may be called while holding the RPC's lock.

TODO(ongaro): It'd be nice to cancel sending the request if it hasn't already gone out, but I guess that's going to be a pretty rare case.

Definition at line 426 of file ClientSession.cc.

Called by the RPC when it wants to be learn of its response (non-blocking).

This must be called while holding the RPC's lock.

Definition at line 459 of file ClientSession.cc.

void LogCabin::RPC::ClientSession::wait ( const OpaqueClientRPC rpc,
TimePoint  timeout 
) [private]

Called by the RPC to wait for its response (blocking).

The caller should call update() after this returns to learn of the response.

This must not be called while holding the RPC's lock.

Parameters:
rpcWait for response to this.
timeoutAfter this time has elapsed, stop waiting and return. The RPC's results will probably not be available yet in this case.

Definition at line 493 of file ClientSession.cc.

ClientSession& LogCabin::RPC::ClientSession::operator= ( const ClientSession ) [private]

Friends And Related Function Documentation

friend class OpaqueClientRPC [friend]

Definition at line 217 of file ClientSession.h.


Member Data Documentation

This is used to keep this object alive while there are outstanding RPCs.

Definition at line 253 of file ClientSession.h.

The number of nanoseconds to wait until the client gets suspicious about the server not responding.

After this amount of time elapses, the client will send a ping to the server. If no response is received within another PING_TIMEOUT_NS milliseconds, the session is closed.

TODO(ongaro): How should this value be chosen? Ideally, you probably want this to be set to something like the 99-th percentile of your RPC latency.

TODO(ongaro): How does this interact with TCP?

Definition at line 267 of file ClientSession.h.

The event loop that is used for non-blocking I/O.

Definition at line 272 of file ClientSession.h.

The RPC server address provided to the constructor.

Definition at line 277 of file ClientSession.h.

Receives events from messageSocket.

Definition at line 282 of file ClientSession.h.

This is used to time out RPCs and sessions when the server is no longer responding.

See Timer.

Definition at line 288 of file ClientSession.h.

std::mutex LogCabin::RPC::ClientSession::mutex [mutable, private]

This mutex protects several members of this class:

Definition at line 298 of file ClientSession.h.

The message ID to assign to the next RPC.

These start at 0 and increment from there.

Definition at line 304 of file ClientSession.h.

A map from MessageId to Response objects that is used to store the response to RPCs and look it up for OpaqueClientRPC objects.

The Response objects mapped to must be deleted manually when removed from this map (gcc 4.4 doesn't support mapping to non-copyable objects).

Definition at line 312 of file ClientSession.h.

If this session is disconnected then this holds the error message.

All new RPCs will be immediately 'ready' with this error message. Otherwise, this is the empty string.

Definition at line 319 of file ClientSession.h.

The number of outstanding RPC requests that have been sent but whose responses have not yet been received.

This does not include ping requests sent by the timer (which aren't real RPCs). This is used to determine when to schedule the timer: the timer is scheduled if numActiveRPCs is non-zero.

Definition at line 328 of file ClientSession.h.

When numActiveRPCs is > 0, this field indicates that we are waiting for a ping response as evidence that the server is still alive.

When numActiveRPCs = 0, this field is undefined.

Definition at line 335 of file ClientSession.h.

The MessageSocket used to send RPC requests and receive RPC responses.

This may be NULL if the socket was never created. In this case, errorMessage will be set.

Definition at line 342 of file ClientSession.h.

Registers timer with the event loop.

Definition at line 347 of file ClientSession.h.

std::function< int(int sockfd, const struct sockaddr *addr, socklen_t addrlen) LogCabin::RPC::ClientSession::connectFn) [static, private]

Usually set to connect() but mocked out in some unit tests.

Definition at line 355 of file ClientSession.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines