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 | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 17 | namespace facebook { namespace thrift { namespace server { |
| 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 | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 58 | /** |
| 59 | * This is a stack of all the objects that have been created but that |
| 60 | * are NOT currently in use. When we close a connection, we place it on this |
| 61 | * stack so that the object can be reused later, rather than freeing the |
| 62 | * memory and reallocating a new object later. |
| 63 | */ |
| 64 | std::stack<TConnection*> connectionStack_; |
| 65 | |
| 66 | void handleEvent(int fd, short which); |
| 67 | |
| 68 | public: |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 69 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
Mark Slee | f937339 | 2007-01-24 19:41:57 +0000 | [diff] [blame] | 70 | int port) : |
| 71 | TServer(processor), |
| 72 | serverSocket_(0), |
| 73 | port_(port), |
| 74 | frameResponses_(true) {} |
| 75 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 76 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
| 77 | boost::shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 78 | int port, |
| 79 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 80 | TServer(processor), |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 81 | serverSocket_(0), |
| 82 | port_(port), |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 83 | frameResponses_(true), |
| 84 | threadManager_(threadManager) { |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 85 | setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
| 86 | setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory())); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 87 | setInputProtocolFactory(protocolFactory); |
| 88 | setOutputProtocolFactory(protocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 89 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 90 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 91 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 92 | TNonblockingServer(boost::shared_ptr<TProcessor> processor, |
| 93 | boost::shared_ptr<TTransportFactory> inputTransportFactory, |
| 94 | boost::shared_ptr<TTransportFactory> outputTransportFactory, |
| 95 | boost::shared_ptr<TProtocolFactory> inputProtocolFactory, |
| 96 | boost::shared_ptr<TProtocolFactory> outputProtocolFactory, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 97 | int port, |
| 98 | boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) : |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 99 | TServer(processor), |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 100 | serverSocket_(0), |
| 101 | port_(port), |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 102 | frameResponses_(true), |
| 103 | threadManager_(threadManager) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 104 | setInputTransportFactory(inputTransportFactory); |
| 105 | setOutputTransportFactory(outputTransportFactory); |
| 106 | setInputProtocolFactory(inputProtocolFactory); |
| 107 | setOutputProtocolFactory(outputProtocolFactory); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 108 | setThreadManager(threadManager); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 109 | } |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 110 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 111 | ~TNonblockingServer() {} |
| 112 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 113 | void setThreadManager(boost::shared_ptr<ThreadManager> threadManager) { |
| 114 | threadManager_ = threadManager; |
| 115 | threadPoolProcessing_ = (threadManager != NULL); |
| 116 | } |
| 117 | |
| 118 | bool isThreadPoolProcessing() { |
| 119 | return threadPoolProcessing_; |
| 120 | } |
| 121 | |
| 122 | void addTask(boost::shared_ptr<Runnable> task) { |
| 123 | threadManager_->add(task); |
| 124 | } |
| 125 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 126 | void setFrameResponses(bool frameResponses) { |
| 127 | frameResponses_ = frameResponses; |
| 128 | } |
| 129 | |
| 130 | bool getFrameResponses() { |
| 131 | return frameResponses_; |
| 132 | } |
| 133 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 134 | TConnection* createConnection(int socket, short flags); |
| 135 | |
| 136 | void returnConnection(TConnection* connection); |
| 137 | |
| 138 | static void eventHandler(int fd, short which, void* v) { |
| 139 | ((TNonblockingServer*)v)->handleEvent(fd, which); |
| 140 | } |
| 141 | |
| 142 | void serve(); |
| 143 | }; |
| 144 | |
| 145 | /** |
| 146 | * Two states for sockets, recv and send mode |
| 147 | */ |
| 148 | enum TSocketState { |
| 149 | SOCKET_RECV, |
| 150 | SOCKET_SEND |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * Four states for the nonblocking servr: |
| 155 | * 1) initialize |
| 156 | * 2) read 4 byte frame size |
| 157 | * 3) read frame of data |
| 158 | * 4) send back data (if any) |
| 159 | */ |
| 160 | enum TAppState { |
| 161 | APP_INIT, |
| 162 | APP_READ_FRAME_SIZE, |
| 163 | APP_READ_REQUEST, |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 164 | APP_WAIT_TASK, |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 165 | APP_SEND_FRAME_SIZE, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 166 | APP_SEND_RESULT |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * Represents a connection that is handled via libevent. This connection |
| 171 | * essentially encapsulates a socket that has some associated libevent state. |
| 172 | */ |
| 173 | class TConnection { |
| 174 | private: |
| 175 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 176 | class Task; |
| 177 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 178 | // Server handle |
| 179 | TNonblockingServer* server_; |
| 180 | |
| 181 | // Socket handle |
| 182 | int socket_; |
| 183 | |
| 184 | // Libevent object |
| 185 | struct event event_; |
| 186 | |
| 187 | // Libevent flags |
| 188 | short eventFlags_; |
| 189 | |
| 190 | // Socket mode |
| 191 | TSocketState socketState_; |
| 192 | |
| 193 | // Application state |
| 194 | TAppState appState_; |
| 195 | |
| 196 | // How much data needed to read |
| 197 | uint32_t readWant_; |
| 198 | |
| 199 | // Where in the read buffer are we |
| 200 | uint32_t readBufferPos_; |
| 201 | |
| 202 | // Read buffer |
| 203 | uint8_t* readBuffer_; |
| 204 | |
| 205 | // Read buffer size |
| 206 | uint32_t readBufferSize_; |
| 207 | |
| 208 | // Write buffer |
| 209 | uint8_t* writeBuffer_; |
| 210 | |
| 211 | // Write buffer size |
| 212 | uint32_t writeBufferSize_; |
| 213 | |
| 214 | // How far through writing are we? |
| 215 | uint32_t writeBufferPos_; |
| 216 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 217 | // Frame size |
| 218 | int32_t frameSize_; |
| 219 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 220 | // Task handle |
| 221 | int taskHandle_; |
| 222 | |
| 223 | // Task event |
| 224 | struct event taskEvent_; |
| 225 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 226 | // Transport to read from |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 227 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 228 | |
| 229 | // Transport that processor writes to |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 230 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 231 | |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 232 | // extra transport generated by transport factory (e.g. BufferedRouterTransport) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 233 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 234 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 235 | |
| 236 | // Protocol decoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 237 | boost::shared_ptr<TProtocol> inputProtocol_; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 238 | |
| 239 | // Protocol encoder |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 240 | boost::shared_ptr<TProtocol> outputProtocol_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 241 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 242 | // Go into read mode |
| 243 | void setRead() { |
| 244 | setFlags(EV_READ | EV_PERSIST); |
| 245 | } |
| 246 | |
| 247 | // Go into write mode |
| 248 | void setWrite() { |
| 249 | setFlags(EV_WRITE | EV_PERSIST); |
| 250 | } |
| 251 | |
| 252 | // Set event flags |
| 253 | void setFlags(short eventFlags); |
| 254 | |
| 255 | // Libevent handlers |
| 256 | void workSocket(); |
| 257 | |
| 258 | // Close this client and reset |
| 259 | void close(); |
| 260 | |
| 261 | public: |
| 262 | |
| 263 | // Constructor |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 264 | TConnection(int socket, short eventFlags, TNonblockingServer *s) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 265 | readBuffer_ = (uint8_t*)malloc(1024); |
| 266 | if (readBuffer_ == NULL) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 267 | throw new facebook::thrift::TException("Out of memory."); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 268 | } |
| 269 | readBufferSize_ = 1024; |
| 270 | |
| 271 | // Allocate input and output tranpsorts |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 272 | // these only need to be allocated once per TConnection (they don't need to be |
| 273 | // reallocated on init() call) |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 274 | inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
| 275 | outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer()); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 276 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 277 | init(socket, eventFlags, s); |
| 278 | } |
| 279 | |
| 280 | // Initialize |
| 281 | void init(int socket, short eventFlags, TNonblockingServer *s); |
| 282 | |
| 283 | // Transition into a new state |
| 284 | void transition(); |
| 285 | |
| 286 | // Handler wrapper |
| 287 | static void eventHandler(int fd, short which, void* v) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 288 | assert(fd == ((TConnection*)v)->socket_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 289 | ((TConnection*)v)->workSocket(); |
| 290 | } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame^] | 291 | |
| 292 | // Handler wrapper for task block |
| 293 | static void taskHandler(int fd, short which, void* v) { |
| 294 | assert(fd == ((TConnection*)v)->taskHandle_); |
| 295 | if (-1 == ::close(((TConnection*)v)->taskHandle_)) { |
| 296 | GlobalOutput("TConnection::taskHandler close handle failed, resource leak"); |
| 297 | } |
| 298 | ((TConnection*)v)->transition(); |
| 299 | } |
| 300 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | }}} // facebook::thrift::server |
| 304 | |
| 305 | #endif // #ifndef _THRIFT_SERVER_TSIMPLESERVER_H_ |