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

Manages reactor (polling thread) state including poll interest, request cache, and timers. More...

#include <Reactor.h>

Collaboration diagram for Hypertable::Reactor:
Collaboration graph
[legend]

Public Types

enum  Priority { Priority::HIGH = 0, Priority::NORMAL }
 Enumeration for reactor priority. More...
 

Public Member Functions

 Reactor ()
 Constructor. More...
 
 ~Reactor ()
 Destructor. More...
 
void add_request (uint32_t id, IOHandler *handler, DispatchHandler *dh, ClockT::time_point expire)
 Adds a request to request cache and adjusts poll timeout if necessary. More...
 
bool remove_request (uint32_t id, DispatchHandler *&handler)
 Removes request associated with id More...
 
void cancel_requests (IOHandler *handler, int32_t error=Error::COMM_BROKEN_CONNECTION)
 Cancels outstanding requests associated with handler More...
 
void add_timer (ExpireTimer &timer)
 Adds a timer. More...
 
void cancel_timer (const DispatchHandlerPtr &handler)
 Cancels timers associated with handler. More...
 
void schedule_removal (IOHandler *handler)
 Schedules handler for removal. More...
 
void get_removed_handlers (std::set< IOHandler * > &dst)
 Returns set of I/O handlers scheduled for removal. More...
 
void handle_timeouts (PollTimeout &next_timeout)
 Processes request timeouts and timers. More...
 
int add_poll_interest (int sd, short events, IOHandler *handler)
 Add poll interest for socket (POSIX poll only). More...
 
int remove_poll_interest (int sd)
 Remove poll interest for socket (POSIX poll only). More...
 
int modify_poll_interest (int sd, short events)
 Modify poll interest for socket (POSIX poll only). More...
 
void fetch_poll_array (std::vector< struct pollfd > &fdarray, std::vector< IOHandler * > &handlers)
 Fetches poll state vectors (POSIX poll only). More...
 
int poll_loop_interrupt ()
 Forces polling interface wait call to return. More...
 
int poll_loop_continue ()
 Reset state after call to poll_loop_interrupt. More...
 
int interrupt_sd ()
 Returns interrupt socket. More...
 

Protected Types

typedef std::priority_queue
< ExpireTimer, std::vector
< ExpireTimer >, LtTimerHeap
TimerHeap
 Priority queue for timers. More...
 

Protected Attributes

std::mutex m_mutex
 Mutex to protect members. More...
 
std::mutex m_polldata_mutex
 Mutex to protect m_polldata member. More...
 
RequestCache m_request_cache
 Request cache. More...
 
TimerHeap m_timer_heap
 ExpireTimer heap. More...
 
int m_interrupt_sd
 Interrupt socket. More...
 
bool m_interrupt_in_progress
 Set to true if poll loop interrupt in progress. More...
 
std::vector< PollDescriptorTm_polldata
 Vector of poll descriptor state structures for use with POSIX poll(). More...
 
ClockT::time_point m_next_wakeup
 Next polling interface wait timeout (absolute) More...
 
std::set< IOHandler * > m_removed_handlers
 Set of IOHandler objects scheduled for removal. More...
 

Friends

class ReactorFactory
 

Detailed Description

Manages reactor (polling thread) state including poll interest, request cache, and timers.

Definition at line 66 of file Reactor.h.

Member Typedef Documentation

typedef std::priority_queue<ExpireTimer, std::vector<ExpireTimer>, LtTimerHeap> Hypertable::Reactor::TimerHeap
protected

Priority queue for timers.

Definition at line 272 of file Reactor.h.

Constructor & Destructor Documentation

Reactor::Reactor ( )

Constructor.

Initializes polling interface and creates interrupt socket. If ReactorFactory::use_poll is set to true, then the reactor will use the POSIX poll() interface, otherwise epoll is used on Linux, kqueue on OSX and FreeBSD, and port_associate on Solaris. For polling mechanisms that do not provide an interface for breaking out of the poll wait, a UDP socket m_interrupt_sd is created (and connected to itself) and added to the poll set.

The following logic creates a UDP socket that is used to interrupt epoll_wait so that it can reset its timeout value

Definition at line 63 of file Reactor.cc.

Hypertable::Reactor::~Reactor ( )
inline

Destructor.

Definition at line 92 of file Reactor.h.

Member Function Documentation

int Reactor::add_poll_interest ( int  sd,
short  events,
IOHandler handler 
)

Add poll interest for socket (POSIX poll only).

This method is only called when ReactorFactory::use_poll is set to true. It modifies m_polldata accordingly and then calls poll_loop_interrupt. If an error is encountered, then the poll interest is removed by clearing the m_polldata entry for sd.

Parameters
sdSocket descriptor for which to add poll interest
eventsBitmask of poll events (see POSIX poll())
handlerPointer to I/O handler associated with socket
Returns
Error code returned by poll_loop_interrupt

Definition at line 383 of file Reactor.cc.

void Hypertable::Reactor::add_request ( uint32_t  id,
IOHandler handler,
DispatchHandler dh,
ClockT::time_point  expire 
)
inline

Adds a request to request cache and adjusts poll timeout if necessary.

Parameters
idRequest ID
handlerI/O handler with which request is associated
dhApplication dispatch handler for response MESSAGE events
expireAbsolute expiration time

Definition at line 102 of file Reactor.h.

void Hypertable::Reactor::add_timer ( ExpireTimer timer)
inline

Adds a timer.

Pushes timer onto m_timer_heap and interrupts the polling loop so that the poll timeout can be adjusted if necessary.

Parameters
timerReference to ExpireTimer object

Definition at line 135 of file Reactor.h.

void Hypertable::Reactor::cancel_requests ( IOHandler handler,
int32_t  error = Error::COMM_BROKEN_CONNECTION 
)
inline

Cancels outstanding requests associated with handler

Parameters
handlerI/O handler for which outstanding requests are to be cancelled
errorError code to deliver with ERROR events

Definition at line 125 of file Reactor.h.

void Hypertable::Reactor::cancel_timer ( const DispatchHandlerPtr handler)
inline

Cancels timers associated with handler.

Parameters
handlerDispatch handler for which associated timers are to be cancelled

Definition at line 145 of file Reactor.h.

void Reactor::fetch_poll_array ( std::vector< struct pollfd > &  fdarray,
std::vector< IOHandler * > &  handlers 
)

Fetches poll state vectors (POSIX poll only).

Parameters
fdarrayVector of pollfd structures to be passed into poll()
handlersVector of corresponding I/O handlers

Definition at line 444 of file Reactor.cc.

void Hypertable::Reactor::get_removed_handlers ( std::set< IOHandler * > &  dst)
inline

Returns set of I/O handlers scheduled for removal.

This is a one shot method adds the handlers that have been added to m_removed_handlers and then clears the m_removed_handlers set.

Parameters
dstreference to set filled in with removed handlers

Definition at line 186 of file Reactor.h.

void Reactor::handle_timeouts ( PollTimeout next_timeout)

Processes request timeouts and timers.

This method removes timed out requests from the request cache, delivering ERROR events (with error == Error::REQUEST_TIMEOUT) via each request's dispatch handler. It also processes expired timers by removing them from m_timer_heap and delivering a TIMEOUT event via the timer handler if it exsists.

Parameters
next_timeoutSet to next earliest timeout of active requests and timers

Deliver timer events

Definition at line 170 of file Reactor.cc.

int Hypertable::Reactor::interrupt_sd ( )
inline

Returns interrupt socket.

Returns
Interrupt socket

Definition at line 265 of file Reactor.h.

int Reactor::modify_poll_interest ( int  sd,
short  events 
)

Modify poll interest for socket (POSIX poll only).

This method is only called when ReactorFactory::use_poll is set to true. It modifies m_polldata accordingly and then calls poll_loop_interrupt.

Parameters
sdSocket descriptor for which to add poll interest
eventsBitmask of poll events (see POSIX poll())
Returns
Error code returned by poll_loop_interrupt

Definition at line 433 of file Reactor.cc.

int Reactor::poll_loop_continue ( )

Reset state after call to poll_loop_interrupt.

After calling poll_loop_interrupt and handling the interrupt, this method should be called to reset back to normal polling state.

Returns
Error::OK on success, or Error code on failure

Definition at line 331 of file Reactor.cc.

int Reactor::poll_loop_interrupt ( )

Forces polling interface wait call to return.

Returns
Error::OK on success, or Error code on failure

Definition at line 255 of file Reactor.cc.

int Reactor::remove_poll_interest ( int  sd)

Remove poll interest for socket (POSIX poll only).

This method is only called when ReactorFactory::use_poll is set to true. It modifies m_polldata accordingly and then calls poll_loop_interrupt.

Parameters
sdSocket descriptor for which to modify poll interest
Returns
Error code returned by poll_loop_interrupt

Definition at line 412 of file Reactor.cc.

bool Hypertable::Reactor::remove_request ( uint32_t  id,
DispatchHandler *&  handler 
)
inline

Removes request associated with id

Parameters
idRequest ID
handlerRemoved dispatch handler
Returns
true if request removed, false otherwise

Definition at line 115 of file Reactor.h.

void Hypertable::Reactor::schedule_removal ( IOHandler handler)
inline

Schedules handler for removal.

This method schedules an I/O handler for removal. It should be called only when the handler has been decomissioned in the HandlerMap and when there are no outstanding references to the handler. The handler is added to the m_removed_handlers set and a timer is set for 200 milliseconds in the future so that the ReactorRunner will wake up and complete the removal by removing it completely from the HandlerMap and delivering a DISCONNECT event if it is for a TCP socket.

Parameters
handlerI/O handler to schedule for removal

Definition at line 171 of file Reactor.h.

Friends And Related Function Documentation

friend class ReactorFactory
friend

Definition at line 68 of file Reactor.h.

Member Data Documentation

bool Hypertable::Reactor::m_interrupt_in_progress
protected

Set to true if poll loop interrupt in progress.

Definition at line 281 of file Reactor.h.

int Hypertable::Reactor::m_interrupt_sd
protected

Interrupt socket.

Definition at line 278 of file Reactor.h.

std::mutex Hypertable::Reactor::m_mutex
protected

Mutex to protect members.

Definition at line 274 of file Reactor.h.

ClockT::time_point Hypertable::Reactor::m_next_wakeup
protected

Next polling interface wait timeout (absolute)

Definition at line 288 of file Reactor.h.

std::vector<PollDescriptorT> Hypertable::Reactor::m_polldata
protected

Vector of poll descriptor state structures for use with POSIX poll().

Definition at line 285 of file Reactor.h.

std::mutex Hypertable::Reactor::m_polldata_mutex
protected

Mutex to protect m_polldata member.

Definition at line 275 of file Reactor.h.

std::set<IOHandler *> Hypertable::Reactor::m_removed_handlers
protected

Set of IOHandler objects scheduled for removal.

Definition at line 291 of file Reactor.h.

RequestCache Hypertable::Reactor::m_request_cache
protected

Request cache.

Definition at line 276 of file Reactor.h.

TimerHeap Hypertable::Reactor::m_timer_heap
protected

ExpireTimer heap.

Definition at line 277 of file Reactor.h.


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