blob: 0ce7ccb80a48f0112137301dcc00b5f813106b93 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Slee2f6404d2006-10-10 01:37:40 +00007#ifndef _THRIFT_SERVER_TNONBLOCKINGSERVER_H_
8#define _THRIFT_SERVER_TNONBLOCKINGSERVER_H_ 1
9
Mark Slee4af6ed72006-10-25 19:02:49 +000010#include <Thrift.h>
11#include <server/TServer.h>
12#include <transport/TTransportUtils.h>
Mark Sleee02385b2007-06-09 01:21:16 +000013#include <concurrency/ThreadManager.h>
Mark Slee2f6404d2006-10-10 01:37:40 +000014#include <stack>
15#include <event.h>
16
Mark Slee2f6404d2006-10-10 01:37:40 +000017namespace facebook { namespace thrift { namespace server {
18
Mark Slee5ea15f92007-03-05 22:55:59 +000019using facebook::thrift::transport::TMemoryBuffer;
20using facebook::thrift::protocol::TProtocol;
Mark Sleee02385b2007-06-09 01:21:16 +000021using facebook::thrift::concurrency::Runnable;
22using facebook::thrift::concurrency::ThreadManager;
Mark Slee2f6404d2006-10-10 01:37:40 +000023
24// Forward declaration of class
25class 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 */
37class 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 Slee92f00fb2006-10-25 01:28:17 +000049 // Whether to frame responses
50 bool frameResponses_;
51
Mark Sleee02385b2007-06-09 01:21:16 +000052 // 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 Slee2f6404d2006-10-10 01:37:40 +000058 /**
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 Slee5ea15f92007-03-05 22:55:59 +000069 TNonblockingServer(boost::shared_ptr<TProcessor> processor,
Mark Sleef9373392007-01-24 19:41:57 +000070 int port) :
71 TServer(processor),
72 serverSocket_(0),
73 port_(port),
74 frameResponses_(true) {}
75
Mark Slee5ea15f92007-03-05 22:55:59 +000076 TNonblockingServer(boost::shared_ptr<TProcessor> processor,
77 boost::shared_ptr<TProtocolFactory> protocolFactory,
Mark Sleee02385b2007-06-09 01:21:16 +000078 int port,
79 boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) :
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000080 TServer(processor),
Mark Slee92f00fb2006-10-25 01:28:17 +000081 serverSocket_(0),
82 port_(port),
Mark Sleee02385b2007-06-09 01:21:16 +000083 frameResponses_(true),
84 threadManager_(threadManager) {
Mark Slee5ea15f92007-03-05 22:55:59 +000085 setInputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
86 setOutputTransportFactory(boost::shared_ptr<TTransportFactory>(new TTransportFactory()));
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000087 setInputProtocolFactory(protocolFactory);
88 setOutputProtocolFactory(protocolFactory);
Mark Sleee02385b2007-06-09 01:21:16 +000089 setThreadManager(threadManager);
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000090 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +000091
Mark Slee5ea15f92007-03-05 22:55:59 +000092 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 Sleee02385b2007-06-09 01:21:16 +000097 int port,
98 boost::shared_ptr<ThreadManager> threadManager = boost::shared_ptr<ThreadManager>()) :
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000099 TServer(processor),
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000100 serverSocket_(0),
101 port_(port),
Mark Sleee02385b2007-06-09 01:21:16 +0000102 frameResponses_(true),
103 threadManager_(threadManager) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000104 setInputTransportFactory(inputTransportFactory);
105 setOutputTransportFactory(outputTransportFactory);
106 setInputProtocolFactory(inputProtocolFactory);
107 setOutputProtocolFactory(outputProtocolFactory);
Mark Sleee02385b2007-06-09 01:21:16 +0000108 setThreadManager(threadManager);
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000109 }
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000110
Mark Slee2f6404d2006-10-10 01:37:40 +0000111 ~TNonblockingServer() {}
112
Mark Sleee02385b2007-06-09 01:21:16 +0000113 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 Slee92f00fb2006-10-25 01:28:17 +0000126 void setFrameResponses(bool frameResponses) {
127 frameResponses_ = frameResponses;
128 }
129
130 bool getFrameResponses() {
131 return frameResponses_;
132 }
133
Mark Slee2f6404d2006-10-10 01:37:40 +0000134 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 */
148enum 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 */
160enum TAppState {
161 APP_INIT,
162 APP_READ_FRAME_SIZE,
163 APP_READ_REQUEST,
Mark Sleee02385b2007-06-09 01:21:16 +0000164 APP_WAIT_TASK,
Mark Slee92f00fb2006-10-25 01:28:17 +0000165 APP_SEND_FRAME_SIZE,
Mark Slee2f6404d2006-10-10 01:37:40 +0000166 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 */
173class TConnection {
174 private:
175
Mark Sleee02385b2007-06-09 01:21:16 +0000176 class Task;
177
Mark Slee2f6404d2006-10-10 01:37:40 +0000178 // 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 Slee92f00fb2006-10-25 01:28:17 +0000217 // Frame size
218 int32_t frameSize_;
219
Mark Sleee02385b2007-06-09 01:21:16 +0000220 // Task handle
221 int taskHandle_;
222
223 // Task event
224 struct event taskEvent_;
225
Mark Slee2f6404d2006-10-10 01:37:40 +0000226 // Transport to read from
Mark Slee5ea15f92007-03-05 22:55:59 +0000227 boost::shared_ptr<TMemoryBuffer> inputTransport_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000228
229 // Transport that processor writes to
Mark Slee5ea15f92007-03-05 22:55:59 +0000230 boost::shared_ptr<TMemoryBuffer> outputTransport_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000231
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000232 // extra transport generated by transport factory (e.g. BufferedRouterTransport)
Mark Slee5ea15f92007-03-05 22:55:59 +0000233 boost::shared_ptr<TTransport> factoryInputTransport_;
234 boost::shared_ptr<TTransport> factoryOutputTransport_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000235
236 // Protocol decoder
Mark Slee5ea15f92007-03-05 22:55:59 +0000237 boost::shared_ptr<TProtocol> inputProtocol_;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000238
239 // Protocol encoder
Mark Slee5ea15f92007-03-05 22:55:59 +0000240 boost::shared_ptr<TProtocol> outputProtocol_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000241
Mark Slee2f6404d2006-10-10 01:37:40 +0000242 // 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 Agarwal9abb0d62007-01-24 22:53:54 +0000264 TConnection(int socket, short eventFlags, TNonblockingServer *s) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000265 readBuffer_ = (uint8_t*)malloc(1024);
266 if (readBuffer_ == NULL) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000267 throw new facebook::thrift::TException("Out of memory.");
Mark Slee2f6404d2006-10-10 01:37:40 +0000268 }
269 readBufferSize_ = 1024;
270
271 // Allocate input and output tranpsorts
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000272 // these only need to be allocated once per TConnection (they don't need to be
273 // reallocated on init() call)
Mark Slee5ea15f92007-03-05 22:55:59 +0000274 inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_));
275 outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer());
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000276
Mark Slee2f6404d2006-10-10 01:37:40 +0000277 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 Sleee02385b2007-06-09 01:21:16 +0000288 assert(fd == ((TConnection*)v)->socket_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000289 ((TConnection*)v)->workSocket();
290 }
Mark Sleee02385b2007-06-09 01:21:16 +0000291
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 Slee2f6404d2006-10-10 01:37:40 +0000301};
302
303}}} // facebook::thrift::server
304
305#endif // #ifndef _THRIFT_SERVER_TSIMPLESERVER_H_