Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ |
| 8 | #define _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ 1 |
| 9 | |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 10 | #include <Thrift.h> |
| 11 | #include <server/TServer.h> |
| 12 | #include <transport/TTransportUtils.h> |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 13 | #include <concurrency/ThreadManager.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 14 | #include <stack> |
| 15 | #include <event.h> |
| 16 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 17 | namespace facebook { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 18 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 19 | using facebook::thrift::transport::TMemoryBuffer; |
| 20 | using facebook::thrift::protocol::TProtocol; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 21 | using facebook::thrift::concurrency::Runnable; |
| 22 | using facebook::thrift::concurrency::ThreadManager; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 23 | |
| 24 | // Forward declaration of class |
| 25 | class TConnection; |
| 26 | |
| 27 | /** |
| 28 | * This is a non-blocking server in C++ for high performance that operates a |
| 29 | * single IO thread. It assumes that all incoming requests are framed with a |
| 30 | * 4 byte length indicator and writes out responses using the same framing. |
| 31 | * |
| 32 | * It does not use the TServerTransport framework, but rather has socket |
| 33 | * operations hardcoded for use with select. |
| 34 | * |
| 35 | * @author Mark Slee <mcslee@facebook.com> |
| 36 | */ |
| 37 | class TNonblockingServer : public TServer { |
| 38 | private: |
| 39 | |
| 40 | // Listen backlog |
| 41 | static const int LISTEN_BACKLOG = 1024; |
| 42 | |
| 43 | // Server socket file descriptor |
| 44 | int serverSocket_; |
| 45 | |
| 46 | // Port server runs on |
| 47 | int port_; |
| 48 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 49 | // Whether to frame responses |
| 50 | bool frameResponses_; |
| 51 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 52 | // For processing via thread pool, may be NULL |
| 53 | boost::shared_ptr<ThreadManager> threadManager_; |
| 54 | |
| 55 | // Is thread pool processing? |
| 56 | bool threadPoolProcessing_; |
| 57 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 58 | // The event base for libevent |
| 59 | event_base* eventBase_; |
| 60 | |
| 61 | // Event struct, for use with eventBase_ |
| 62 | struct event serverEvent_; |
| 63 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 64 | /** |
| 65 | * This is a stack of all the objects that have been created but that |
| 66 | * are NOT currently in use. When we close a connection, we place it on this |
| 67 | * stack so that the object can be reused later, rather than freeing the |
| 68 | * memory and reallocating a new object later. |
| 69 | */ |
| 70 | std::stack<TConnection*> connectionStack_; |
| 71 | |
| 72 | void handleEvent(int fd, short which); |
| 73 | |
| 74 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 75 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 76 | int port) : |
| 77 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 78 | serverSocket_(-1), |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 79 | port_(port), |
Mark Slee | 8eceaea | 2007-06-15 01:43:21 +0000 | [diff] [blame] | 80 | frameResponses_(true), |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 81 | threadPoolProcessing_(false), |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 82 | eventBase_(NULL) {} |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 83 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 84 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 85 | boost::shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 86 | int port, |
| 87 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 88 | TServer(processor), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 89 | serverSocket_(-1), |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 90 | port_(port), |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 91 | frameResponses_(true), |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 92 | threadManager_(threadManager), |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 93 | eventBase_(NULL) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 94 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 95 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 96 | setInputProtocolFactory(protocolFactory); |
| 97 | setOutputProtocolFactory(protocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 98 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 99 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 100 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 101 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
| 102 | boost::shared_ptr<TTransportFactory> inputTransportFactory, |
| 103 | boost::shared_ptr<TTransportFactory> outputTransportFactory, |
| 104 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 105 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 106 | int port, |
| 107 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 108 | TServer(processor), |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 109 | serverSocket_(0), |
| 110 | port_(port), |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 111 | frameResponses_(true), |
Mark Slee | 5d1784a | 2007-12-05 23:20:54 +0000 | [diff] [blame] | 112 | threadManager_(threadManager), |
| 113 | eventBase_(NULL) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 114 | setInputTransportFactory(inputTransportFactory); |
| 115 | setOutputTransportFactory(outputTransportFactory); |
| 116 | setInputProtocolFactory(inputProtocolFactory); |
| 117 | setOutputProtocolFactory(outputProtocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 118 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 119 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 120 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 121 | ~TNonblockingServer() {} |
| 122 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 123 | void setThreadManager(boost::shared_ptr<ThreadManager> threadManager) { |
| 124 | threadManager_ = threadManager; |
| 125 | threadPoolProcessing_ = (threadManager != NULL); |
| 126 | } |
| 127 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 128 | bool isThreadPoolProcessing() const { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 129 | return threadPoolProcessing_; |
| 130 | } |
| 131 | |
| 132 | void addTask(boost::shared_ptr<Runnable> task) { |
| 133 | threadManager_->add(task); |
| 134 | } |
| 135 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 136 | void setFrameResponses(bool frameResponses) { |
| 137 | frameResponses_ = frameResponses; |
| 138 | } |
| 139 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 140 | bool getFrameResponses() const { |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 141 | return frameResponses_; |
| 142 | } |
| 143 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 144 | event_base* getEventBase() const { |
| 145 | return eventBase_; |
| 146 | } |
| 147 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 148 | TConnection* createConnection(int socket, short flags); |
| 149 | |
| 150 | void returnConnection(TConnection* connection); |
| 151 | |
| 152 | static void eventHandler(int fd, short which, void* v) { |
| 153 | ((TNonblockingServer*)v)->handleEvent(fd, which); |
| 154 | } |
| 155 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 156 | void listenSocket(); |
| 157 | |
| 158 | void listenSocket(int fd); |
| 159 | |
| 160 | void registerEvents(event_base* base); |
| 161 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 162 | void serve(); |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 163 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * Two states for sockets, recv and send mode |
| 168 | */ |
| 169 | enum TSocketState { |
| 170 | SOCKET_RECV, |
| 171 | SOCKET_SEND |
| 172 | }; |
| 173 | |
| 174 | /** |
| 175 | * Four states for the nonblocking servr: |
| 176 | * 1) initialize |
| 177 | * 2) read 4 byte frame size |
| 178 | * 3) read frame of data |
| 179 | * 4) send back data (if any) |
| 180 | */ |
| 181 | enum TAppState { |
| 182 | APP_INIT, |
| 183 | APP_READ_FRAME_SIZE, |
| 184 | APP_READ_REQUEST, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 185 | APP_WAIT_TASK, |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 186 | APP_SEND_FRAME_SIZE, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 187 | APP_SEND_RESULT |
| 188 | }; |
| 189 | |
| 190 | /** |
| 191 | * Represents a connection that is handled via libevent. This connection |
| 192 | * essentially encapsulates a socket that has some associated libevent state. |
| 193 | */ |
| 194 | class TConnection { |
| 195 | private: |
| 196 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 197 | class Task; |
| 198 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 199 | // Server handle |
| 200 | TNonblockingServer* server_; |
| 201 | |
| 202 | // Socket handle |
| 203 | int socket_; |
| 204 | |
| 205 | // Libevent object |
| 206 | struct event event_; |
| 207 | |
| 208 | // Libevent flags |
| 209 | short eventFlags_; |
| 210 | |
| 211 | // Socket mode |
| 212 | TSocketState socketState_; |
| 213 | |
| 214 | // Application state |
| 215 | TAppState appState_; |
| 216 | |
| 217 | // How much data needed to read |
| 218 | uint32_t readWant_; |
| 219 | |
| 220 | // Where in the read buffer are we |
| 221 | uint32_t readBufferPos_; |
| 222 | |
| 223 | // Read buffer |
| 224 | uint8_t* readBuffer_; |
| 225 | |
| 226 | // Read buffer size |
| 227 | uint32_t readBufferSize_; |
| 228 | |
| 229 | // Write buffer |
| 230 | uint8_t* writeBuffer_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 231 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 232 | // Write buffer size |
| 233 | uint32_t writeBufferSize_; |
| 234 | |
| 235 | // How far through writing are we? |
| 236 | uint32_t writeBufferPos_; |
| 237 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 238 | // Frame size |
| 239 | int32_t frameSize_; |
| 240 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 241 | // Task handle |
| 242 | int taskHandle_; |
| 243 | |
| 244 | // Task event |
| 245 | struct event taskEvent_; |
| 246 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 247 | // Transport to read from |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 248 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 249 | |
| 250 | // Transport that processor writes to |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 251 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 252 | |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 253 | // extra transport generated by transport factory (e.g. BufferedRouterTransport) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 254 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 255 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 256 | |
| 257 | // Protocol decoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 258 | boost::shared_ptr<TProtocol> inputProtocol_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 259 | |
| 260 | // Protocol encoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 261 | boost::shared_ptr<TProtocol> outputProtocol_; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 262 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 263 | // Go into read mode |
| 264 | void setRead() { |
| 265 | setFlags(EV_READ | EV_PERSIST); |
| 266 | } |
| 267 | |
| 268 | // Go into write mode |
| 269 | void setWrite() { |
| 270 | setFlags(EV_WRITE | EV_PERSIST); |
| 271 | } |
| 272 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 273 | // Set socket idle |
| 274 | void setIdle() { |
| 275 | setFlags(0); |
| 276 | } |
| 277 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 278 | // Set event flags |
| 279 | void setFlags(short eventFlags); |
| 280 | |
| 281 | // Libevent handlers |
| 282 | void workSocket(); |
| 283 | |
| 284 | // Close this client and reset |
| 285 | void close(); |
| 286 | |
| 287 | public: |
| 288 | |
| 289 | // Constructor |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 290 | TConnection(int socket, short eventFlags, TNonblockingServer *s) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 291 | readBuffer_ = (uint8_t*)malloc(1024); |
| 292 | if (readBuffer_ == NULL) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 293 | throw new facebook::thrift::TException("Out of memory."); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 294 | } |
| 295 | readBufferSize_ = 1024; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 296 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 297 | // Allocate input and output tranpsorts |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 298 | // these only need to be allocated once per TConnection (they don't need to be |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 299 | // reallocated on init() call) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 300 | inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
| 301 | outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer()); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 302 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 303 | init(socket, eventFlags, s); |
| 304 | } |
| 305 | |
| 306 | // Initialize |
| 307 | void init(int socket, short eventFlags, TNonblockingServer *s); |
| 308 | |
| 309 | // Transition into a new state |
| 310 | void transition(); |
| 311 | |
| 312 | // Handler wrapper |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame^] | 313 | static void eventHandler(int fd, short /* which */, void* v) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 314 | assert(fd == ((TConnection*)v)->socket_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 315 | ((TConnection*)v)->workSocket(); |
| 316 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 317 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 318 | // Handler wrapper for task block |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame^] | 319 | static void taskHandler(int fd, short /* which */, void* v) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 320 | assert(fd == ((TConnection*)v)->taskHandle_); |
| 321 | if (-1 == ::close(((TConnection*)v)->taskHandle_)) { |
| 322 | GlobalOutput("TConnection::taskHandler close handle failed, resource leak"); |
| 323 | } |
| 324 | ((TConnection*)v)->transition(); |
| 325 | } |
| 326 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | }}} // facebook::thrift::server |
| 330 | |
| 331 | #endif // #ifndef _THRIFT_SERVER_TSIMPLESERVER_H_ |