blob: eea0427d253cb345ed9001505c5f49142177ff8a [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Konrad Grochowski9be4e682013-06-22 22:03:31 +020020#include <thrift/thrift-config.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000021
Roger Meier4285ba22013-06-10 21:17:23 +020022#include <thrift/server/TNonblockingServer.h>
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/concurrency/Exception.h>
24#include <thrift/transport/TSocket.h>
cyyca8af9b2019-01-11 22:13:12 +080025#include <thrift/concurrency/ThreadFactory.h>
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -040026#include <thrift/transport/PlatformSocket.h>
Mark Slee2f6404d2006-10-10 01:37:40 +000027
James E. King, III82ae9572017-08-05 12:23:54 -040028#include <algorithm>
Mark Sleee02385b2007-06-09 01:21:16 +000029#include <iostream>
Lei Feiweib5ebcd12015-04-04 22:12:07 +080030
st0ke961fa702018-10-12 18:37:40 +070031#ifdef HAVE_POLL_H
32#include <poll.h>
33#elif HAVE_SYS_POLL_H
34#include <sys/poll.h>
35#elif HAVE_SYS_SELECT_H
Lei Feiweib5ebcd12015-04-04 22:12:07 +080036#include <sys/select.h>
37#endif
Roger Meier30aae0c2011-07-08 12:23:31 +000038
39#ifdef HAVE_SYS_SOCKET_H
Mark Slee2f6404d2006-10-10 01:37:40 +000040#include <sys/socket.h>
Roger Meier30aae0c2011-07-08 12:23:31 +000041#endif
42
43#ifdef HAVE_NETINET_IN_H
Mark Slee2f6404d2006-10-10 01:37:40 +000044#include <netinet/in.h>
45#include <netinet/tcp.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000046#endif
47
48#ifdef HAVE_ARPA_INET_H
Bryan Duxbury76c43682011-08-24 21:26:48 +000049#include <arpa/inet.h>
Roger Meier30aae0c2011-07-08 12:23:31 +000050#endif
51
52#ifdef HAVE_NETDB_H
Mark Sleefb4b5142007-11-20 01:27:08 +000053#include <netdb.h>
Roger Meier30aae0c2011-07-08 12:23:31 +000054#endif
55
Roger Meier2fa9c312011-09-05 19:15:53 +000056#ifdef HAVE_FCNTL_H
Mark Slee2f6404d2006-10-10 01:37:40 +000057#include <fcntl.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000058#endif
59
Mark Slee2f6404d2006-10-10 01:37:40 +000060#include <assert.h>
Roger Meier12d70532011-12-14 23:35:28 +000061
62#ifdef HAVE_SCHED_H
Jake Farrellb0d95602011-12-06 01:17:26 +000063#include <sched.h>
Roger Meier12d70532011-12-14 23:35:28 +000064#endif
Mark Slee2f6404d2006-10-10 01:37:40 +000065
David Reiss9b903442009-10-21 05:51:28 +000066#ifndef AF_LOCAL
67#define AF_LOCAL AF_UNIX
68#endif
69
James E. King, III7edc8fa2017-01-20 10:11:41 -050070#ifdef HAVE_INTTYPES_H
71#include <inttypes.h>
Roger Meier12d70532011-12-14 23:35:28 +000072#endif
73
James E. King, III7edc8fa2017-01-20 10:11:41 -050074#ifdef HAVE_STDINT_H
75#include <stdint.h>
Antonio Di Monaco796667b2016-01-04 23:05:19 +010076#endif
77
Konrad Grochowski16a23a62014-11-13 15:33:38 +010078namespace apache {
79namespace thrift {
80namespace server {
Mark Slee2f6404d2006-10-10 01:37:40 +000081
T Jake Lucianib5e62212009-01-31 22:36:20 +000082using namespace apache::thrift::protocol;
83using namespace apache::thrift::transport;
84using namespace apache::thrift::concurrency;
David Reiss1c20c872010-03-09 05:20:14 +000085using apache::thrift::transport::TSocket;
86using apache::thrift::transport::TTransportException;
cyy316723a2019-01-05 16:35:14 +080087using std::shared_ptr;
Mark Sleee02385b2007-06-09 01:21:16 +000088
Bryan Duxbury526fa8e2011-08-29 20:28:23 +000089/// Three states for sockets: recv frame size, recv data, and send mode
Konrad Grochowski16a23a62014-11-13 15:33:38 +010090enum TSocketState { SOCKET_RECV_FRAMING, SOCKET_RECV, SOCKET_SEND };
Bryan Duxbury526fa8e2011-08-29 20:28:23 +000091
92/**
93 * Five states for the nonblocking server:
94 * 1) initialize
95 * 2) read 4 byte frame size
96 * 3) read frame of data
97 * 4) send back data (if any)
98 * 5) force immediate connection close
99 */
100enum TAppState {
101 APP_INIT,
102 APP_READ_FRAME_SIZE,
103 APP_READ_REQUEST,
104 APP_WAIT_TASK,
105 APP_SEND_RESULT,
106 APP_CLOSE_CONNECTION
107};
108
109/**
110 * Represents a connection that is handled via libevent. This connection
111 * essentially encapsulates a socket that has some associated libevent state.
112 */
113class TNonblockingServer::TConnection {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100114private:
Jake Farrellb0d95602011-12-06 01:17:26 +0000115 /// Server IO Thread handling this connection
116 TNonblockingIOThread* ioThread_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000117
118 /// Server handle
119 TNonblockingServer* server_;
120
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000121 /// TProcessor
cyy316723a2019-01-05 16:35:14 +0800122 std::shared_ptr<TProcessor> processor_;
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000123
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000124 /// Object wrapping network socket
cyy316723a2019-01-05 16:35:14 +0800125 std::shared_ptr<TSocket> tSocket_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000126
127 /// Libevent object
128 struct event event_;
129
130 /// Libevent flags
131 short eventFlags_;
132
133 /// Socket mode
134 TSocketState socketState_;
135
136 /// Application state
137 TAppState appState_;
138
139 /// How much data needed to read
140 uint32_t readWant_;
141
142 /// Where in the read buffer are we
143 uint32_t readBufferPos_;
144
145 /// Read buffer
146 uint8_t* readBuffer_;
147
148 /// Read buffer size
149 uint32_t readBufferSize_;
150
151 /// Write buffer
152 uint8_t* writeBuffer_;
153
154 /// Write buffer size
155 uint32_t writeBufferSize_;
156
157 /// How far through writing are we?
158 uint32_t writeBufferPos_;
159
160 /// Largest size of write buffer seen since buffer was constructed
161 size_t largestWriteBufferSize_;
162
163 /// Count of the number of calls for use with getResizeBufferEveryN().
164 int32_t callsForResize_;
165
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000166 /// Transport to read from
cyy316723a2019-01-05 16:35:14 +0800167 std::shared_ptr<TMemoryBuffer> inputTransport_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000168
169 /// Transport that processor writes to
cyy316723a2019-01-05 16:35:14 +0800170 std::shared_ptr<TMemoryBuffer> outputTransport_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000171
172 /// extra transport generated by transport factory (e.g. BufferedRouterTransport)
cyy316723a2019-01-05 16:35:14 +0800173 std::shared_ptr<TTransport> factoryInputTransport_;
174 std::shared_ptr<TTransport> factoryOutputTransport_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000175
176 /// Protocol decoder
cyy316723a2019-01-05 16:35:14 +0800177 std::shared_ptr<TProtocol> inputProtocol_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000178
179 /// Protocol encoder
cyy316723a2019-01-05 16:35:14 +0800180 std::shared_ptr<TProtocol> outputProtocol_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000181
182 /// Server event handler, if any
cyy316723a2019-01-05 16:35:14 +0800183 std::shared_ptr<TServerEventHandler> serverEventHandler_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000184
185 /// Thrift call context, if any
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100186 void* connectionContext_;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000187
188 /// Go into read mode
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100189 void setRead() { setFlags(EV_READ | EV_PERSIST); }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000190
191 /// Go into write mode
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100192 void setWrite() { setFlags(EV_WRITE | EV_PERSIST); }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000193
194 /// Set socket idle
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100195 void setIdle() { setFlags(0); }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000196
197 /**
198 * Set event flags for this connection.
199 *
200 * @param eventFlags flags we pass to libevent for the connection.
201 */
202 void setFlags(short eventFlags);
203
204 /**
205 * Libevent handler called (via our static wrapper) when the connection
206 * socket had something happen. Rather than use the flags libevent passed,
207 * we use the connection state to determine whether we need to read or
208 * write the socket.
209 */
210 void workSocket();
211
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100212public:
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000213 class Task;
214
215 /// Constructor
cyy316723a2019-01-05 16:35:14 +0800216 TConnection(std::shared_ptr<TSocket> socket,
Divya Thaluru808d1432017-08-06 16:36:36 -0700217 TNonblockingIOThread* ioThread) {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100218 readBuffer_ = nullptr;
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000219 readBufferSize_ = 0;
220
Jake Farrellb0d95602011-12-06 01:17:26 +0000221 ioThread_ = ioThread;
222 server_ = ioThread->getServer();
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000223
Jake Farrellb0d95602011-12-06 01:17:26 +0000224 // Allocate input and output transports these only need to be allocated
225 // once per TConnection (they don't need to be reallocated on init() call)
226 inputTransport_.reset(new TMemoryBuffer(readBuffer_, readBufferSize_));
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400227 outputTransport_.reset(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100228 new TMemoryBuffer(static_cast<uint32_t>(server_->getWriteBufferDefaultSize())));
Divya Thaluru808d1432017-08-06 16:36:36 -0700229
230 tSocket_ = socket;
231
232 init(ioThread);
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000233 }
234
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100235 ~TConnection() { std::free(readBuffer_); }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000236
Roger Meier0c04fcc2013-03-22 19:52:08 +0100237 /// Close this connection and free or reset its resources.
238 void close();
239
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100240 /**
241 * Check buffers against any size limits and shrink it if exceeded.
242 *
243 * @param readLimit we reduce read buffer size to this (if nonzero).
244 * @param writeLimit if nonzero and write buffer is larger, replace it.
245 */
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000246 void checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit);
247
248 /// Initialize
Divya Thaluru808d1432017-08-06 16:36:36 -0700249 void init(TNonblockingIOThread* ioThread);
250
251 /// set socket for connection
cyy316723a2019-01-05 16:35:14 +0800252 void setSocket(std::shared_ptr<TSocket> socket);
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000253
254 /**
255 * This is called when the application transitions from one state into
256 * another. This means that it has finished writing the data that it needed
257 * to, or finished receiving the data that it needed to.
258 */
259 void transition();
260
261 /**
262 * C-callable event handler for connection events. Provides a callback
263 * that libevent can understand which invokes connection_->workSocket().
264 *
265 * @param fd the descriptor the event occurred on.
266 * @param which the flags associated with the event.
267 * @param v void* callback arg where we placed TConnection's "this".
268 */
Bryan Duxbury266b1732011-09-01 16:50:28 +0000269 static void eventHandler(evutil_socket_t fd, short /* which */, void* v) {
Konrad Grochowskib7af66e2014-07-08 19:22:44 +0200270 assert(fd == static_cast<evutil_socket_t>(((TConnection*)v)->getTSocket()->getSocketFD()));
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000271 ((TConnection*)v)->workSocket();
272 }
273
274 /**
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000275 * Notification to server that processing has ended on this request.
276 * Can be called either when processing is completed or when a waiting
277 * task has been preemptively terminated (on overload).
278 *
Jake Farrellb0d95602011-12-06 01:17:26 +0000279 * Don't call this from the IO thread itself.
280 *
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400281 * @return true if successful, false if unable to notify (check THRIFT_GET_SOCKET_ERROR).
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000282 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100283 bool notifyIOThread() { return ioThread_->notify(this); }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000284
Jake Farrellb0d95602011-12-06 01:17:26 +0000285 /*
286 * Returns the number of this connection's currently assigned IO
287 * thread.
288 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100289 int getIOThreadNumber() const { return ioThread_->getThreadNumber(); }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000290
291 /// Force connection shutdown for this connection.
292 void forceClose() {
293 appState_ = APP_CLOSE_CONNECTION;
Jake Farrellb0d95602011-12-06 01:17:26 +0000294 if (!notifyIOThread()) {
Changli Gao257dcef2017-04-06 00:42:01 +0800295 server_->decrementActiveProcessors();
Jens Geyerfb05cf62014-12-04 21:49:07 +0100296 close();
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000297 throw TException("TConnection::forceClose: failed write on notify pipe");
298 }
299 }
300
301 /// return the server this connection was initialized for.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100302 TNonblockingServer* getServer() const { return server_; }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000303
304 /// get state of connection.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100305 TAppState getState() const { return appState_; }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000306
307 /// return the TSocket transport wrapping this network connection
cyy316723a2019-01-05 16:35:14 +0800308 std::shared_ptr<TSocket> getTSocket() const { return tSocket_; }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000309
310 /// return the server event handler if any
cyy316723a2019-01-05 16:35:14 +0800311 std::shared_ptr<TServerEventHandler> getServerEventHandler() { return serverEventHandler_; }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000312
313 /// return the Thrift connection context if any
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100314 void* getConnectionContext() { return connectionContext_; }
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000315};
316
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100317class TNonblockingServer::TConnection::Task : public Runnable {
318public:
cyy316723a2019-01-05 16:35:14 +0800319 Task(std::shared_ptr<TProcessor> processor,
320 std::shared_ptr<TProtocol> input,
321 std::shared_ptr<TProtocol> output,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100322 TConnection* connection)
323 : processor_(processor),
324 input_(input),
325 output_(output),
326 connection_(connection),
327 serverEventHandler_(connection_->getServerEventHandler()),
328 connectionContext_(connection_->getConnectionContext()) {}
Mark Sleee02385b2007-06-09 01:21:16 +0000329
Sebastian Zenker042580f2019-01-29 15:48:12 +0100330 void run() override {
Mark Sleee02385b2007-06-09 01:21:16 +0000331 try {
David Reiss105961d2010-10-06 17:10:17 +0000332 for (;;) {
Roger Meier72957452013-06-29 00:28:50 +0200333 if (serverEventHandler_) {
David Reiss105961d2010-10-06 17:10:17 +0000334 serverEventHandler_->processContext(connectionContext_, connection_->getTSocket());
335 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100336 if (!processor_->process(input_, output_, connectionContext_)
337 || !input_->getTransport()->peek()) {
Mark Sleee02385b2007-06-09 01:21:16 +0000338 break;
339 }
340 }
Bryan Duxbury1e987582011-08-25 17:33:03 +0000341 } catch (const TTransportException& ttx) {
Jake Farrellb0d95602011-12-06 01:17:26 +0000342 GlobalOutput.printf("TNonblockingServer: client died: %s", ttx.what());
James E. King, III82ae9572017-08-05 12:23:54 -0400343 } catch (const std::bad_alloc&) {
Jake Farrellb0d95602011-12-06 01:17:26 +0000344 GlobalOutput("TNonblockingServer: caught bad_alloc exception.");
Henrique Mendonca962b3532012-09-20 13:19:55 +0000345 exit(1);
Bryan Duxbury1e987582011-08-25 17:33:03 +0000346 } catch (const std::exception& x) {
Jake Farrellb0d95602011-12-06 01:17:26 +0000347 GlobalOutput.printf("TNonblockingServer: process() exception: %s: %s",
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100348 typeid(x).name(),
349 x.what());
Mark Sleee02385b2007-06-09 01:21:16 +0000350 } catch (...) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100351 GlobalOutput.printf("TNonblockingServer: unknown exception while processing.");
Mark Sleee02385b2007-06-09 01:21:16 +0000352 }
Mark Slee79b16942007-11-26 19:05:29 +0000353
David Reiss01fe1532010-03-09 05:19:25 +0000354 // Signal completion back to the libevent thread via a pipe
Jake Farrellb0d95602011-12-06 01:17:26 +0000355 if (!connection_->notifyIOThread()) {
Jens Geyerfb05cf62014-12-04 21:49:07 +0100356 GlobalOutput.printf("TNonblockingServer: failed to notifyIOThread, closing.");
Changli Gao257dcef2017-04-06 00:42:01 +0800357 connection_->server_->decrementActiveProcessors();
Jens Geyerfb05cf62014-12-04 21:49:07 +0100358 connection_->close();
David Reiss01fe1532010-03-09 05:19:25 +0000359 throw TException("TNonblockingServer::Task::run: failed write on notify pipe");
Mark Sleee02385b2007-06-09 01:21:16 +0000360 }
David Reiss01fe1532010-03-09 05:19:25 +0000361 }
362
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100363 TConnection* getTConnection() { return connection_; }
Mark Sleee02385b2007-06-09 01:21:16 +0000364
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100365private:
cyy316723a2019-01-05 16:35:14 +0800366 std::shared_ptr<TProcessor> processor_;
367 std::shared_ptr<TProtocol> input_;
368 std::shared_ptr<TProtocol> output_;
David Reiss01fe1532010-03-09 05:19:25 +0000369 TConnection* connection_;
cyy316723a2019-01-05 16:35:14 +0800370 std::shared_ptr<TServerEventHandler> serverEventHandler_;
David Reiss105961d2010-10-06 17:10:17 +0000371 void* connectionContext_;
Mark Sleee02385b2007-06-09 01:21:16 +0000372};
Mark Slee5ea15f92007-03-05 22:55:59 +0000373
Divya Thaluru808d1432017-08-06 16:36:36 -0700374void TNonblockingServer::TConnection::init(TNonblockingIOThread* ioThread) {
Jake Farrellb0d95602011-12-06 01:17:26 +0000375 ioThread_ = ioThread;
376 server_ = ioThread->getServer();
Mark Slee2f6404d2006-10-10 01:37:40 +0000377 appState_ = APP_INIT;
378 eventFlags_ = 0;
379
380 readBufferPos_ = 0;
381 readWant_ = 0;
382
Sebastian Zenker042580f2019-01-29 15:48:12 +0100383 writeBuffer_ = nullptr;
Mark Slee2f6404d2006-10-10 01:37:40 +0000384 writeBufferSize_ = 0;
385 writeBufferPos_ = 0;
David Reiss54bec5d2010-10-06 17:10:45 +0000386 largestWriteBufferSize_ = 0;
Mark Slee2f6404d2006-10-10 01:37:40 +0000387
David Reiss89a12942010-10-06 17:10:52 +0000388 socketState_ = SOCKET_RECV_FRAMING;
David Reiss54bec5d2010-10-06 17:10:45 +0000389 callsForResize_ = 0;
Mark Slee79b16942007-11-26 19:05:29 +0000390
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000391 // get input/transports
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100392 factoryInputTransport_ = server_->getInputTransportFactory()->getTransport(inputTransport_);
393 factoryOutputTransport_ = server_->getOutputTransportFactory()->getTransport(outputTransport_);
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000394
395 // Create protocol
Dave Watson792db4e2015-01-16 11:22:01 -0800396 if (server_->getHeaderTransport()) {
397 inputProtocol_ = server_->getInputProtocolFactory()->getProtocol(factoryInputTransport_,
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +0100398 factoryOutputTransport_);
Dave Watson792db4e2015-01-16 11:22:01 -0800399 outputProtocol_ = inputProtocol_;
400 } else {
401 inputProtocol_ = server_->getInputProtocolFactory()->getProtocol(factoryInputTransport_);
402 outputProtocol_ = server_->getOutputProtocolFactory()->getProtocol(factoryOutputTransport_);
403 }
David Reiss105961d2010-10-06 17:10:17 +0000404
405 // Set up for any server event handler
406 serverEventHandler_ = server_->getEventHandler();
Roger Meier72957452013-06-29 00:28:50 +0200407 if (serverEventHandler_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100408 connectionContext_ = serverEventHandler_->createContext(inputProtocol_, outputProtocol_);
David Reiss105961d2010-10-06 17:10:17 +0000409 } else {
Sebastian Zenker042580f2019-01-29 15:48:12 +0100410 connectionContext_ = nullptr;
David Reiss105961d2010-10-06 17:10:17 +0000411 }
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000412
413 // Get the processor
Jake Farrellb0d95602011-12-06 01:17:26 +0000414 processor_ = server_->getProcessor(inputProtocol_, outputProtocol_, tSocket_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000415}
416
cyy316723a2019-01-05 16:35:14 +0800417void TNonblockingServer::TConnection::setSocket(std::shared_ptr<TSocket> socket) {
Divya Thaluru808d1432017-08-06 16:36:36 -0700418 tSocket_ = socket;
419}
420
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000421void TNonblockingServer::TConnection::workSocket() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100422 int got = 0, left = 0, sent = 0;
Mark Sleeaaa23ed2007-01-30 19:52:05 +0000423 uint32_t fetch = 0;
Mark Slee2f6404d2006-10-10 01:37:40 +0000424
425 switch (socketState_) {
David Reiss89a12942010-10-06 17:10:52 +0000426 case SOCKET_RECV_FRAMING:
427 union {
428 uint8_t buf[sizeof(uint32_t)];
Roger Meier3781c242011-12-11 20:07:21 +0000429 uint32_t size;
David Reiss89a12942010-10-06 17:10:52 +0000430 } framing;
Mark Slee2f6404d2006-10-10 01:37:40 +0000431
David Reiss89a12942010-10-06 17:10:52 +0000432 // if we've already received some bytes we kept them here
433 framing.size = readWant_;
434 // determine size of this frame
435 try {
436 // Read from the socket
437 fetch = tSocket_->read(&framing.buf[readBufferPos_],
438 uint32_t(sizeof(framing.size) - readBufferPos_));
439 if (fetch == 0) {
440 // Whenever we get here it means a remote disconnect
Mark Slee2f6404d2006-10-10 01:37:40 +0000441 close();
442 return;
443 }
David Reiss89a12942010-10-06 17:10:52 +0000444 readBufferPos_ += fetch;
445 } catch (TTransportException& te) {
Divya Thaluru808d1432017-08-06 16:36:36 -0700446 //In Nonblocking SSLSocket some operations need to be retried again.
447 //Current approach is parsing exception message, but a better solution needs to be investigated.
448 if(!strstr(te.what(), "retry")) {
449 GlobalOutput.printf("TConnection::workSocket(): %s", te.what());
450 close();
David Reiss89a12942010-10-06 17:10:52 +0000451
Divya Thaluru808d1432017-08-06 16:36:36 -0700452 return;
453 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000454 }
455
David Reiss89a12942010-10-06 17:10:52 +0000456 if (readBufferPos_ < sizeof(framing.size)) {
457 // more needed before frame size is known -- save what we have so far
458 readWant_ = framing.size;
459 return;
460 }
461
462 readWant_ = ntohl(framing.size);
Roger Meier3781c242011-12-11 20:07:21 +0000463 if (readWant_ > server_->getMaxFrameSize()) {
464 // Don't allow giant frame sizes. This prevents bad clients from
465 // causing us to try and allocate a giant buffer.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100466 GlobalOutput.printf(
467 "TNonblockingServer: frame size too large "
468 "(%" PRIu32 " > %" PRIu64
469 ") from client %s. "
470 "Remote side not using TFramedTransport?",
471 readWant_,
472 (uint64_t)server_->getMaxFrameSize(),
473 tSocket_->getSocketInfo().c_str());
David Reiss89a12942010-10-06 17:10:52 +0000474 close();
475 return;
476 }
477 // size known; now get the rest of the frame
478 transition();
Bugra Gedik8bcb7ac2018-01-21 09:43:49 -0800479
480 // If the socket has more data than the frame header, continue to work on it. This is not strictly necessary for
481 // regular sockets, because if there is more data, libevent will fire the event handler registered for read
482 // readiness, which will in turn call workSocket(). However, some socket types (such as TSSLSocket) may have the
483 // data sitting in their internal buffers and from libevent's perspective, there is no further data available. In
484 // that case, not having this workSocket() call here would result in a hang as we will never get to work the socket,
485 // despite having more data.
486 if (tSocket_->hasPendingDataToRead())
487 {
488 workSocket();
489 }
490
David Reiss89a12942010-10-06 17:10:52 +0000491 return;
492
493 case SOCKET_RECV:
494 // It is an error to be in this state if we already have all the data
495 assert(readBufferPos_ < readWant_);
496
David Reiss105961d2010-10-06 17:10:17 +0000497 try {
498 // Read from the socket
499 fetch = readWant_ - readBufferPos_;
500 got = tSocket_->read(readBuffer_ + readBufferPos_, fetch);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100501 } catch (TTransportException& te) {
Divya Thaluru808d1432017-08-06 16:36:36 -0700502 //In Nonblocking SSLSocket some operations need to be retried again.
503 //Current approach is parsing exception message, but a better solution needs to be investigated.
504 if(!strstr(te.what(), "retry")) {
505 GlobalOutput.printf("TConnection::workSocket(): %s", te.what());
506 close();
507 }
Mark Slee79b16942007-11-26 19:05:29 +0000508
David Reiss105961d2010-10-06 17:10:17 +0000509 return;
510 }
Jake Farrellb0d95602011-12-06 01:17:26 +0000511
Mark Slee2f6404d2006-10-10 01:37:40 +0000512 if (got > 0) {
513 // Move along in the buffer
514 readBufferPos_ += got;
515
516 // Check that we did not overdo it
517 assert(readBufferPos_ <= readWant_);
Mark Slee79b16942007-11-26 19:05:29 +0000518
Mark Slee2f6404d2006-10-10 01:37:40 +0000519 // We are done reading, move onto the next state
520 if (readBufferPos_ == readWant_) {
521 transition();
522 }
523 return;
Mark Slee2f6404d2006-10-10 01:37:40 +0000524 }
525
526 // Whenever we get down here it means a remote disconnect
527 close();
Mark Slee79b16942007-11-26 19:05:29 +0000528
Mark Slee2f6404d2006-10-10 01:37:40 +0000529 return;
530
531 case SOCKET_SEND:
532 // Should never have position past size
533 assert(writeBufferPos_ <= writeBufferSize_);
534
535 // If there is no data to send, then let us move on
536 if (writeBufferPos_ == writeBufferSize_) {
Buğra Gedik36d1b0d2016-09-04 17:18:15 +0900537 GlobalOutput("WARNING: Send state with no data to send");
Mark Slee2f6404d2006-10-10 01:37:40 +0000538 transition();
539 return;
540 }
541
David Reiss105961d2010-10-06 17:10:17 +0000542 try {
543 left = writeBufferSize_ - writeBufferPos_;
544 sent = tSocket_->write_partial(writeBuffer_ + writeBufferPos_, left);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100545 } catch (TTransportException& te) {
David Reiss105961d2010-10-06 17:10:17 +0000546 GlobalOutput.printf("TConnection::workSocket(): %s ", te.what());
Mark Slee2f6404d2006-10-10 01:37:40 +0000547 close();
548 return;
549 }
550
551 writeBufferPos_ += sent;
552
553 // Did we overdo it?
554 assert(writeBufferPos_ <= writeBufferSize_);
555
Mark Slee79b16942007-11-26 19:05:29 +0000556 // We are done!
Mark Slee2f6404d2006-10-10 01:37:40 +0000557 if (writeBufferPos_ == writeBufferSize_) {
558 transition();
559 }
560
561 return;
562
563 default:
David Reiss3bb5e052010-01-25 19:31:31 +0000564 GlobalOutput.printf("Unexpected Socket State %d", socketState_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000565 assert(0);
566 }
567}
568
Dave Watson792db4e2015-01-16 11:22:01 -0800569bool TNonblockingServer::getHeaderTransport() {
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +0100570 // Currently if there is no output protocol factory,
Dave Watson792db4e2015-01-16 11:22:01 -0800571 // we assume header transport (without having to create
572 // a new transport and check)
Sebastian Zenker042580f2019-01-29 15:48:12 +0100573 return getOutputProtocolFactory() == nullptr;
Dave Watson792db4e2015-01-16 11:22:01 -0800574}
575
Mark Slee2f6404d2006-10-10 01:37:40 +0000576/**
577 * This is called when the application transitions from one state into
578 * another. This means that it has finished writing the data that it needed
579 * to, or finished receiving the data that it needed to.
580 */
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000581void TNonblockingServer::TConnection::transition() {
Jake Farrellb0d95602011-12-06 01:17:26 +0000582 // ensure this connection is active right now
583 assert(ioThread_);
584 assert(server_);
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000585
Mark Slee2f6404d2006-10-10 01:37:40 +0000586 // Switch upon the state that we are currently in and move to a new state
587 switch (appState_) {
588
589 case APP_READ_REQUEST:
590 // We are done reading the request, package the read buffer into transport
591 // and get back some data from the dispatch function
Dave Watson792db4e2015-01-16 11:22:01 -0800592 if (server_->getHeaderTransport()) {
593 inputTransport_->resetBuffer(readBuffer_, readBufferPos_);
594 outputTransport_->resetBuffer();
595 } else {
596 // We saved room for the framing size in case header transport needed it,
597 // but just skip it for the non-header case
598 inputTransport_->resetBuffer(readBuffer_ + 4, readBufferPos_ - 4);
599 outputTransport_->resetBuffer();
600
601 // Prepend four bytes of blank space to the buffer so we can
602 // write the frame size there later.
603 outputTransport_->getWritePtr(4);
604 outputTransport_->wroteBytes(4);
605 }
Mark Slee79b16942007-11-26 19:05:29 +0000606
David Reiss01fe1532010-03-09 05:19:25 +0000607 server_->incrementActiveProcessors();
608
Mark Sleee02385b2007-06-09 01:21:16 +0000609 if (server_->isThreadPoolProcessing()) {
610 // We are setting up a Task to do this work and we will wait on it
Mark Slee79b16942007-11-26 19:05:29 +0000611
David Reiss01fe1532010-03-09 05:19:25 +0000612 // Create task and dispatch to the thread manager
cyy316723a2019-01-05 16:35:14 +0800613 std::shared_ptr<Runnable> task = std::shared_ptr<Runnable>(
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100614 new Task(processor_, inputProtocol_, outputProtocol_, this));
David Reiss01fe1532010-03-09 05:19:25 +0000615 // The application is now waiting on the task to finish
616 appState_ = APP_WAIT_TASK;
Mark Slee2f6404d2006-10-10 01:37:40 +0000617
Changli Gaod4fa7062017-03-10 13:25:43 +0800618 // Set this connection idle so that libevent doesn't process more
619 // data on it while we're still waiting for the threadmanager to
620 // finish this task
621 setIdle();
622
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100623 try {
624 server_->addTask(task);
625 } catch (IllegalStateException& ise) {
626 // The ThreadManager is not ready to handle any more tasks (it's probably shutting down).
627 GlobalOutput.printf("IllegalStateException: Server::process() %s", ise.what());
Changli Gaod4fa7062017-03-10 13:25:43 +0800628 server_->decrementActiveProcessors();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100629 close();
Jens Geyerfb05cf62014-12-04 21:49:07 +0100630 } catch (TimedOutException& to) {
631 GlobalOutput.printf("[ERROR] TimedOutException: Server::process() %s", to.what());
Changli Gaod4fa7062017-03-10 13:25:43 +0800632 server_->decrementActiveProcessors();
Jens Geyerfb05cf62014-12-04 21:49:07 +0100633 close();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100634 }
Mark Slee402ee282007-08-23 01:43:20 +0000635
David Reiss01fe1532010-03-09 05:19:25 +0000636 return;
Mark Sleee02385b2007-06-09 01:21:16 +0000637 } else {
638 try {
Roger Meier72957452013-06-29 00:28:50 +0200639 if (serverEventHandler_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100640 serverEventHandler_->processContext(connectionContext_, getTSocket());
Roger Meier72957452013-06-29 00:28:50 +0200641 }
Mark Sleee02385b2007-06-09 01:21:16 +0000642 // Invoke the processor
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100643 processor_->process(inputProtocol_, outputProtocol_, connectionContext_);
644 } catch (const TTransportException& ttx) {
645 GlobalOutput.printf(
646 "TNonblockingServer transport error in "
647 "process(): %s",
648 ttx.what());
David Reiss01fe1532010-03-09 05:19:25 +0000649 server_->decrementActiveProcessors();
Mark Sleee02385b2007-06-09 01:21:16 +0000650 close();
651 return;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100652 } catch (const std::exception& x) {
Bryan Duxbury1e987582011-08-25 17:33:03 +0000653 GlobalOutput.printf("Server::process() uncaught exception: %s: %s",
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100654 typeid(x).name(),
655 x.what());
David Reiss01fe1532010-03-09 05:19:25 +0000656 server_->decrementActiveProcessors();
Mark Slee79b16942007-11-26 19:05:29 +0000657 close();
Mark Sleee02385b2007-06-09 01:21:16 +0000658 return;
659 } catch (...) {
David Reiss01e55c12008-07-13 22:18:51 +0000660 GlobalOutput.printf("Server::process() unknown exception");
David Reiss01fe1532010-03-09 05:19:25 +0000661 server_->decrementActiveProcessors();
Mark Sleee02385b2007-06-09 01:21:16 +0000662 close();
663 return;
664 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000665 }
James E. King III9bea32f2018-03-16 16:07:42 -0400666 // fallthrough
Mark Slee2f6404d2006-10-10 01:37:40 +0000667
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100668 // Intentionally fall through here, the call to process has written into
669 // the writeBuffer_
Mark Slee402ee282007-08-23 01:43:20 +0000670
Mark Sleee02385b2007-06-09 01:21:16 +0000671 case APP_WAIT_TASK:
672 // We have now finished processing a task and the result has been written
673 // into the outputTransport_, so we grab its contents and place them into
674 // the writeBuffer_ for actual writing by the libevent thread
675
David Reiss01fe1532010-03-09 05:19:25 +0000676 server_->decrementActiveProcessors();
Mark Slee2f6404d2006-10-10 01:37:40 +0000677 // Get the result of the operation
678 outputTransport_->getBuffer(&writeBuffer_, &writeBufferSize_);
679
680 // If the function call generated return data, then move into the send
681 // state and get going
David Reissaf787782008-07-03 20:29:34 +0000682 // 4 bytes were reserved for frame size
David Reiss52cb7a72008-06-30 21:40:35 +0000683 if (writeBufferSize_ > 4) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000684
685 // Move into write state
686 writeBufferPos_ = 0;
687 socketState_ = SOCKET_SEND;
Mark Slee92f00fb2006-10-25 01:28:17 +0000688
David Reissaf787782008-07-03 20:29:34 +0000689 // Put the frame size into the write buffer
Sebastian Zenker042580f2019-01-29 15:48:12 +0100690 auto frameSize = (int32_t)htonl(writeBufferSize_ - 4);
David Reissaf787782008-07-03 20:29:34 +0000691 memcpy(writeBuffer_, &frameSize, 4);
Mark Slee2f6404d2006-10-10 01:37:40 +0000692
693 // Socket into write mode
David Reiss52cb7a72008-06-30 21:40:35 +0000694 appState_ = APP_SEND_RESULT;
Mark Slee2f6404d2006-10-10 01:37:40 +0000695 setWrite();
696
Mark Slee2f6404d2006-10-10 01:37:40 +0000697 return;
698 }
699
David Reissc51986f2009-03-24 20:01:25 +0000700 // In this case, the request was oneway and we should fall through
Mark Slee2f6404d2006-10-10 01:37:40 +0000701 // right back into the read frame header state
Mark Slee92f00fb2006-10-25 01:28:17 +0000702 goto LABEL_APP_INIT;
703
Mark Slee2f6404d2006-10-10 01:37:40 +0000704 case APP_SEND_RESULT:
David Reiss54bec5d2010-10-06 17:10:45 +0000705 // it's now safe to perform buffer size housekeeping.
706 if (writeBufferSize_ > largestWriteBufferSize_) {
707 largestWriteBufferSize_ = writeBufferSize_;
708 }
709 if (server_->getResizeBufferEveryN() > 0
710 && ++callsForResize_ >= server_->getResizeBufferEveryN()) {
711 checkIdleBufferMemLimit(server_->getIdleReadBufferLimit(),
712 server_->getIdleWriteBufferLimit());
713 callsForResize_ = 0;
714 }
James E. King III9bea32f2018-03-16 16:07:42 -0400715 // fallthrough
Mark Slee2f6404d2006-10-10 01:37:40 +0000716
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100717 // N.B.: We also intentionally fall through here into the INIT state!
Mark Slee2f6404d2006-10-10 01:37:40 +0000718
Mark Slee92f00fb2006-10-25 01:28:17 +0000719 LABEL_APP_INIT:
Mark Slee2f6404d2006-10-10 01:37:40 +0000720 case APP_INIT:
721
722 // Clear write buffer variables
Sebastian Zenker042580f2019-01-29 15:48:12 +0100723 writeBuffer_ = nullptr;
Mark Slee2f6404d2006-10-10 01:37:40 +0000724 writeBufferPos_ = 0;
725 writeBufferSize_ = 0;
726
Mark Slee2f6404d2006-10-10 01:37:40 +0000727 // Into read4 state we go
David Reiss89a12942010-10-06 17:10:52 +0000728 socketState_ = SOCKET_RECV_FRAMING;
Mark Slee2f6404d2006-10-10 01:37:40 +0000729 appState_ = APP_READ_FRAME_SIZE;
730
David Reiss89a12942010-10-06 17:10:52 +0000731 readBufferPos_ = 0;
732
Mark Slee2f6404d2006-10-10 01:37:40 +0000733 // Register read event
734 setRead();
David Reiss84e63ab2008-03-07 20:12:28 +0000735
Mark Slee2f6404d2006-10-10 01:37:40 +0000736 return;
737
738 case APP_READ_FRAME_SIZE:
Dave Watson792db4e2015-01-16 11:22:01 -0800739 readWant_ += 4;
740
David Reiss89a12942010-10-06 17:10:52 +0000741 // We just read the request length
742 // Double the buffer size until it is big enough
743 if (readWant_ > readBufferSize_) {
744 if (readBufferSize_ == 0) {
745 readBufferSize_ = 1;
746 }
747 uint32_t newSize = readBufferSize_;
748 while (readWant_ > newSize) {
749 newSize *= 2;
750 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000751
Sebastian Zenker042580f2019-01-29 15:48:12 +0100752 auto* newBuffer = (uint8_t*)std::realloc(readBuffer_, newSize);
753 if (newBuffer == nullptr) {
David Reiss89a12942010-10-06 17:10:52 +0000754 // nothing else to be done...
755 throw std::bad_alloc();
756 }
757 readBuffer_ = newBuffer;
758 readBufferSize_ = newSize;
Mark Slee2f6404d2006-10-10 01:37:40 +0000759 }
760
Dave Watson792db4e2015-01-16 11:22:01 -0800761 readBufferPos_ = 4;
762 *((uint32_t*)readBuffer_) = htonl(readWant_ - 4);
Mark Slee2f6404d2006-10-10 01:37:40 +0000763
764 // Move into read request state
David Reiss89a12942010-10-06 17:10:52 +0000765 socketState_ = SOCKET_RECV;
Mark Slee2f6404d2006-10-10 01:37:40 +0000766 appState_ = APP_READ_REQUEST;
767
Mark Slee2f6404d2006-10-10 01:37:40 +0000768 return;
769
David Reiss01fe1532010-03-09 05:19:25 +0000770 case APP_CLOSE_CONNECTION:
771 server_->decrementActiveProcessors();
772 close();
773 return;
774
Mark Slee2f6404d2006-10-10 01:37:40 +0000775 default:
David Reiss3bb5e052010-01-25 19:31:31 +0000776 GlobalOutput.printf("Unexpected Application State %d", appState_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000777 assert(0);
778 }
779}
780
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000781void TNonblockingServer::TConnection::setFlags(short eventFlags) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000782 // Catch the do nothing case
783 if (eventFlags_ == eventFlags) {
784 return;
785 }
786
787 // Delete a previously existing event
Buğra Gedik36d1b0d2016-09-04 17:18:15 +0900788 if (eventFlags_ && event_del(&event_) == -1) {
789 GlobalOutput.perror("TConnection::setFlags() event_del", THRIFT_GET_SOCKET_ERROR);
790 return;
Mark Slee2f6404d2006-10-10 01:37:40 +0000791 }
792
793 // Update in memory structure
794 eventFlags_ = eventFlags;
795
Mark Slee402ee282007-08-23 01:43:20 +0000796 // Do not call event_set if there are no flags
797 if (!eventFlags_) {
798 return;
799 }
800
David Reiss01fe1532010-03-09 05:19:25 +0000801 /*
Mark Slee2f6404d2006-10-10 01:37:40 +0000802 * event_set:
803 *
804 * Prepares the event structure &event to be used in future calls to
805 * event_add() and event_del(). The event will be prepared to call the
Mark Sleee02385b2007-06-09 01:21:16 +0000806 * eventHandler using the 'sock' file descriptor to monitor events.
Mark Slee2f6404d2006-10-10 01:37:40 +0000807 *
808 * The events can be either EV_READ, EV_WRITE, or both, indicating
809 * that an application can read or write from the file respectively without
810 * blocking.
811 *
Mark Sleee02385b2007-06-09 01:21:16 +0000812 * The eventHandler will be called with the file descriptor that triggered
Mark Slee2f6404d2006-10-10 01:37:40 +0000813 * the event and the type of event which will be one of: EV_TIMEOUT,
814 * EV_SIGNAL, EV_READ, EV_WRITE.
815 *
816 * The additional flag EV_PERSIST makes an event_add() persistent until
817 * event_del() has been called.
818 *
819 * Once initialized, the &event struct can be used repeatedly with
820 * event_add() and event_del() and does not need to be reinitialized unless
Mark Sleee02385b2007-06-09 01:21:16 +0000821 * the eventHandler and/or the argument to it are to be changed. However,
Mark Slee2f6404d2006-10-10 01:37:40 +0000822 * when an ev structure has been added to libevent using event_add() the
823 * structure must persist until the event occurs (assuming EV_PERSIST
824 * is not set) or is removed using event_del(). You may not reuse the same
825 * ev structure for multiple monitored descriptors; each descriptor needs
826 * its own ev.
827 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100828 event_set(&event_, tSocket_->getSocketFD(), eventFlags_, TConnection::eventHandler, this);
Jake Farrellb0d95602011-12-06 01:17:26 +0000829 event_base_set(ioThread_->getEventBase(), &event_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000830
831 // Add the event
Sebastian Zenker042580f2019-01-29 15:48:12 +0100832 if (event_add(&event_, nullptr) == -1) {
Buğra Gedik36d1b0d2016-09-04 17:18:15 +0900833 GlobalOutput.perror("TConnection::setFlags(): could not event_add", THRIFT_GET_SOCKET_ERROR);
Mark Slee2f6404d2006-10-10 01:37:40 +0000834 }
835}
836
837/**
838 * Closes a connection
839 */
Bryan Duxbury526fa8e2011-08-29 20:28:23 +0000840void TNonblockingServer::TConnection::close() {
Changli Gaobf42d552017-03-20 14:29:07 +0800841 setIdle();
David Reiss105961d2010-10-06 17:10:17 +0000842
Roger Meier72957452013-06-29 00:28:50 +0200843 if (serverEventHandler_) {
David Reiss105961d2010-10-06 17:10:17 +0000844 serverEventHandler_->deleteContext(connectionContext_, inputProtocol_, outputProtocol_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000845 }
Sebastian Zenker042580f2019-01-29 15:48:12 +0100846 ioThread_ = nullptr;
Mark Slee2f6404d2006-10-10 01:37:40 +0000847
848 // Close the socket
David Reiss105961d2010-10-06 17:10:17 +0000849 tSocket_->close();
Mark Slee2f6404d2006-10-10 01:37:40 +0000850
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000851 // close any factory produced transports
852 factoryInputTransport_->close();
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000853 factoryOutputTransport_->close();
Aditya Agarwal1ea90522007-01-19 02:02:12 +0000854
Roger Meier464a3a42014-07-07 21:48:28 +0200855 // release processor and handler
856 processor_.reset();
857
Mark Slee2f6404d2006-10-10 01:37:40 +0000858 // Give this object back to the server that owns it
859 server_->returnConnection(this);
860}
861
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100862void TNonblockingServer::TConnection::checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit) {
David Reiss54bec5d2010-10-06 17:10:45 +0000863 if (readLimit > 0 && readBufferSize_ > readLimit) {
David Reiss89a12942010-10-06 17:10:52 +0000864 free(readBuffer_);
Sebastian Zenker042580f2019-01-29 15:48:12 +0100865 readBuffer_ = nullptr;
David Reiss89a12942010-10-06 17:10:52 +0000866 readBufferSize_ = 0;
Kevin Clarkcbcd63a2009-03-19 03:50:05 +0000867 }
David Reiss54bec5d2010-10-06 17:10:45 +0000868
869 if (writeLimit > 0 && largestWriteBufferSize_ > writeLimit) {
870 // just start over
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400871 outputTransport_->resetBuffer(static_cast<uint32_t>(server_->getWriteBufferDefaultSize()));
David Reiss54bec5d2010-10-06 17:10:45 +0000872 largestWriteBufferSize_ = 0;
873 }
Kevin Clarkcbcd63a2009-03-19 03:50:05 +0000874}
875
David Reiss8ede8182010-09-02 15:26:28 +0000876TNonblockingServer::~TNonblockingServer() {
Roger Meier0c04fcc2013-03-22 19:52:08 +0100877 // Close any active connections (moves them to the idle connection stack)
878 while (activeConnections_.size()) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100879 activeConnections_.front()->close();
Roger Meier0c04fcc2013-03-22 19:52:08 +0100880 }
David Reiss8ede8182010-09-02 15:26:28 +0000881 // Clean up unused TConnection objects in connectionStack_
882 while (!connectionStack_.empty()) {
883 TConnection* connection = connectionStack_.top();
884 connectionStack_.pop();
885 delete connection;
886 }
Roger Meier0c04fcc2013-03-22 19:52:08 +0100887 // The TNonblockingIOThread objects have shared_ptrs to the Thread
888 // objects and the Thread objects have shared_ptrs to the TNonblockingIOThread
889 // objects (as runnable) so these objects will never deallocate without help.
890 while (!ioThreads_.empty()) {
cyy316723a2019-01-05 16:35:14 +0800891 std::shared_ptr<TNonblockingIOThread> iot = ioThreads_.back();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100892 ioThreads_.pop_back();
cyy316723a2019-01-05 16:35:14 +0800893 iot->setThread(std::shared_ptr<Thread>());
Roger Meier0c04fcc2013-03-22 19:52:08 +0100894 }
David Reiss8ede8182010-09-02 15:26:28 +0000895}
896
Mark Slee2f6404d2006-10-10 01:37:40 +0000897/**
898 * Creates a new connection either by reusing an object off the stack or
899 * by allocating a new one entirely
900 */
cyy316723a2019-01-05 16:35:14 +0800901TNonblockingServer::TConnection* TNonblockingServer::createConnection(std::shared_ptr<TSocket> socket) {
Mark Slee2f6404d2006-10-10 01:37:40 +0000902 // Check the stack
Jake Farrellb0d95602011-12-06 01:17:26 +0000903 Guard g(connMutex_);
904
905 // pick an IO thread to handle this connection -- currently round robin
Jake Farrellb0d95602011-12-06 01:17:26 +0000906 assert(nextIOThread_ < ioThreads_.size());
907 int selectedThreadIdx = nextIOThread_;
Ben Craig64935232013-10-09 15:21:38 -0500908 nextIOThread_ = static_cast<uint32_t>((nextIOThread_ + 1) % ioThreads_.size());
Jake Farrellb0d95602011-12-06 01:17:26 +0000909
910 TNonblockingIOThread* ioThread = ioThreads_[selectedThreadIdx].get();
911
912 // Check the connection stack to see if we can re-use
Sebastian Zenker042580f2019-01-29 15:48:12 +0100913 TConnection* result = nullptr;
Mark Slee2f6404d2006-10-10 01:37:40 +0000914 if (connectionStack_.empty()) {
Divya Thaluru808d1432017-08-06 16:36:36 -0700915 result = new TConnection(socket, ioThread);
Jake Farrellb0d95602011-12-06 01:17:26 +0000916 ++numTConnections_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000917 } else {
Jake Farrellb0d95602011-12-06 01:17:26 +0000918 result = connectionStack_.top();
Mark Slee2f6404d2006-10-10 01:37:40 +0000919 connectionStack_.pop();
Divya Thaluru808d1432017-08-06 16:36:36 -0700920 result->setSocket(socket);
921 result->init(ioThread);
Mark Slee2f6404d2006-10-10 01:37:40 +0000922 }
Roger Meier0c04fcc2013-03-22 19:52:08 +0100923 activeConnections_.push_back(result);
Jake Farrellb0d95602011-12-06 01:17:26 +0000924 return result;
Mark Slee2f6404d2006-10-10 01:37:40 +0000925}
926
927/**
928 * Returns a connection to the stack
929 */
930void TNonblockingServer::returnConnection(TConnection* connection) {
Jake Farrellb0d95602011-12-06 01:17:26 +0000931 Guard g(connMutex_);
932
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100933 activeConnections_.erase(std::remove(activeConnections_.begin(),
934 activeConnections_.end(),
935 connection),
936 activeConnections_.end());
Roger Meier0c04fcc2013-03-22 19:52:08 +0100937
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100938 if (connectionStackLimit_ && (connectionStack_.size() >= connectionStackLimit_)) {
Kevin Clarkcbcd63a2009-03-19 03:50:05 +0000939 delete connection;
Jake Farrellb0d95602011-12-06 01:17:26 +0000940 --numTConnections_;
Kevin Clarkcbcd63a2009-03-19 03:50:05 +0000941 } else {
David Reiss54bec5d2010-10-06 17:10:45 +0000942 connection->checkIdleBufferMemLimit(idleReadBufferLimit_, idleWriteBufferLimit_);
Kevin Clarkcbcd63a2009-03-19 03:50:05 +0000943 connectionStack_.push(connection);
944 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000945}
946
947/**
David Reissa79e4882008-03-05 07:51:47 +0000948 * Server socket had something happen. We accept all waiting client
949 * connections on fd and assign TConnection objects to handle those requests.
Mark Slee2f6404d2006-10-10 01:37:40 +0000950 */
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -0400951void TNonblockingServer::handleEvent(THRIFT_SOCKET fd, short which) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100952 (void)which;
David Reiss3bb5e052010-01-25 19:31:31 +0000953 // Make sure that libevent didn't mess up the socket handles
Mark Slee2f6404d2006-10-10 01:37:40 +0000954 assert(fd == serverSocket_);
Mark Slee79b16942007-11-26 19:05:29 +0000955
Mark Slee2f6404d2006-10-10 01:37:40 +0000956 // Going to accept a new client socket
cyy316723a2019-01-05 16:35:14 +0800957 std::shared_ptr<TSocket> clientSocket;
Mark Slee79b16942007-11-26 19:05:29 +0000958
Divya Thaluru808d1432017-08-06 16:36:36 -0700959 clientSocket = serverTransport_->accept();
960 if (clientSocket) {
David Reiss01fe1532010-03-09 05:19:25 +0000961 // If we're overloaded, take action here
962 if (overloadAction_ != T_OVERLOAD_NO_ACTION && serverOverloaded()) {
Jake Farrellb0d95602011-12-06 01:17:26 +0000963 Guard g(connMutex_);
David Reiss01fe1532010-03-09 05:19:25 +0000964 nConnectionsDropped_++;
965 nTotalConnectionsDropped_++;
966 if (overloadAction_ == T_OVERLOAD_CLOSE_ON_ACCEPT) {
Divya Thaluru808d1432017-08-06 16:36:36 -0700967 clientSocket->close();
David Reiss83b8fda2010-03-09 05:19:34 +0000968 return;
David Reiss01fe1532010-03-09 05:19:25 +0000969 } else if (overloadAction_ == T_OVERLOAD_DRAIN_TASK_QUEUE) {
970 if (!drainPendingTask()) {
971 // Nothing left to discard, so we drop connection instead.
Divya Thaluru808d1432017-08-06 16:36:36 -0700972 clientSocket->close();
David Reiss83b8fda2010-03-09 05:19:34 +0000973 return;
David Reiss01fe1532010-03-09 05:19:25 +0000974 }
975 }
976 }
Jake Farrellb0d95602011-12-06 01:17:26 +0000977
Mark Slee2f6404d2006-10-10 01:37:40 +0000978 // Create a new TConnection for this client socket.
Divya Thaluru808d1432017-08-06 16:36:36 -0700979 TConnection* clientConnection = createConnection(clientSocket);
Mark Slee2f6404d2006-10-10 01:37:40 +0000980
981 // Fail fast if we could not create a TConnection object
Sebastian Zenker042580f2019-01-29 15:48:12 +0100982 if (clientConnection == nullptr) {
David Reiss01e55c12008-07-13 22:18:51 +0000983 GlobalOutput.printf("thriftServerEventHandler: failed TConnection factory");
Divya Thaluru808d1432017-08-06 16:36:36 -0700984 clientSocket->close();
Mark Slee2f6404d2006-10-10 01:37:40 +0000985 return;
986 }
987
Jake Farrellb0d95602011-12-06 01:17:26 +0000988 /*
989 * Either notify the ioThread that is assigned this connection to
990 * start processing, or if it is us, we'll just ask this
991 * connection to do its initial state change here.
992 *
993 * (We need to avoid writing to our own notification pipe, to
994 * avoid possible deadlocks if the pipe is full.)
995 *
996 * The IO thread #0 is the only one that handles these listen
997 * events, so unless the connection has been assigned to thread #0
998 * we know it's not on our thread.
999 */
1000 if (clientConnection->getIOThreadNumber() == 0) {
1001 clientConnection->transition();
1002 } else {
Jens Geyerfb05cf62014-12-04 21:49:07 +01001003 if (!clientConnection->notifyIOThread()) {
1004 GlobalOutput.perror("[ERROR] notifyIOThread failed on fresh connection, closing", errno);
Changli Gao75386db2017-03-10 13:15:37 +08001005 clientConnection->close();
Jens Geyerfb05cf62014-12-04 21:49:07 +01001006 }
Jake Farrellb0d95602011-12-06 01:17:26 +00001007 }
Mark Slee2f6404d2006-10-10 01:37:40 +00001008 }
1009}
1010
1011/**
Mark Slee79b16942007-11-26 19:05:29 +00001012 * Creates a socket to listen on and binds it to the local port.
Mark Slee2f6404d2006-10-10 01:37:40 +00001013 */
Jake Farrellb0d95602011-12-06 01:17:26 +00001014void TNonblockingServer::createAndListenOnSocket() {
Divya Thaluru808d1432017-08-06 16:36:36 -07001015 serverTransport_->listen();
1016 serverSocket_ = serverTransport_->getSocketFD();
Mark Slee79b16942007-11-26 19:05:29 +00001017}
1018
Mark Slee79b16942007-11-26 19:05:29 +00001019
cyy316723a2019-01-05 16:35:14 +08001020void TNonblockingServer::setThreadManager(std::shared_ptr<ThreadManager> threadManager) {
David Reiss068f4162010-03-09 05:19:45 +00001021 threadManager_ = threadManager;
Roger Meier72957452013-06-29 00:28:50 +02001022 if (threadManager) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001023 threadManager->setExpireCallback(
cyy316723a2019-01-05 16:35:14 +08001024 std::bind(&TNonblockingServer::expireClose,
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001025 this,
cyy316723a2019-01-05 16:35:14 +08001026 std::placeholders::_1));
David Reiss068f4162010-03-09 05:19:45 +00001027 threadPoolProcessing_ = true;
1028 } else {
1029 threadPoolProcessing_ = false;
1030 }
1031}
1032
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001033bool TNonblockingServer::serverOverloaded() {
David Reiss01fe1532010-03-09 05:19:25 +00001034 size_t activeConnections = numTConnections_ - connectionStack_.size();
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001035 if (numActiveProcessors_ > maxActiveProcessors_ || activeConnections > maxConnections_) {
David Reiss01fe1532010-03-09 05:19:25 +00001036 if (!overloaded_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001037 GlobalOutput.printf("TNonblockingServer: overload condition begun.");
David Reiss01fe1532010-03-09 05:19:25 +00001038 overloaded_ = true;
1039 }
1040 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001041 if (overloaded_ && (numActiveProcessors_ <= overloadHysteresis_ * maxActiveProcessors_)
1042 && (activeConnections <= overloadHysteresis_ * maxConnections_)) {
1043 GlobalOutput.printf(
1044 "TNonblockingServer: overload ended; "
1045 "%u dropped (%llu total)",
1046 nConnectionsDropped_,
1047 nTotalConnectionsDropped_);
David Reiss01fe1532010-03-09 05:19:25 +00001048 nConnectionsDropped_ = 0;
1049 overloaded_ = false;
1050 }
1051 }
1052
1053 return overloaded_;
1054}
1055
1056bool TNonblockingServer::drainPendingTask() {
1057 if (threadManager_) {
cyy316723a2019-01-05 16:35:14 +08001058 std::shared_ptr<Runnable> task = threadManager_->removeNextPending();
David Reiss01fe1532010-03-09 05:19:25 +00001059 if (task) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001060 TConnection* connection = static_cast<TConnection::Task*>(task.get())->getTConnection();
1061 assert(connection && connection->getServer() && connection->getState() == APP_WAIT_TASK);
David Reiss01fe1532010-03-09 05:19:25 +00001062 connection->forceClose();
1063 return true;
1064 }
1065 }
1066 return false;
Mark Slee79b16942007-11-26 19:05:29 +00001067}
1068
cyy316723a2019-01-05 16:35:14 +08001069void TNonblockingServer::expireClose(std::shared_ptr<Runnable> task) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001070 TConnection* connection = static_cast<TConnection::Task*>(task.get())->getTConnection();
1071 assert(connection && connection->getServer() && connection->getState() == APP_WAIT_TASK);
David Reiss068f4162010-03-09 05:19:45 +00001072 connection->forceClose();
1073}
1074
Bugra Gedik8bcb7ac2018-01-21 09:43:49 -08001075void TNonblockingServer::stop() {
Jake Farrellb0d95602011-12-06 01:17:26 +00001076 // Breaks the event loop in all threads so that they end ASAP.
cyy64750162019-02-08 13:40:59 +08001077 for (auto & ioThread : ioThreads_) {
1078 ioThread->stop();
Jake Farrellb0d95602011-12-06 01:17:26 +00001079 }
1080}
1081
Roger Meier6f2a5032013-07-08 23:35:25 +02001082void TNonblockingServer::registerEvents(event_base* user_event_base) {
1083 userEventBase_ = user_event_base;
1084
Jake Farrellb0d95602011-12-06 01:17:26 +00001085 // init listen socket
Roger Meiere802aa42013-07-19 21:10:54 +02001086 if (serverSocket_ == THRIFT_INVALID_SOCKET)
Roger Meier6f2a5032013-07-08 23:35:25 +02001087 createAndListenOnSocket();
Mark Slee79b16942007-11-26 19:05:29 +00001088
Jake Farrellb0d95602011-12-06 01:17:26 +00001089 // set up the IO threads
1090 assert(ioThreads_.empty());
1091 if (!numIOThreads_) {
1092 numIOThreads_ = DEFAULT_IO_THREADS;
David Reiss01fe1532010-03-09 05:19:25 +00001093 }
Nobuaki Sukegawa8016af82015-01-02 23:14:22 +09001094 // User-provided event-base doesn't works for multi-threaded servers
1095 assert(numIOThreads_ == 1 || !userEventBase_);
David Reiss01fe1532010-03-09 05:19:25 +00001096
Roger Meierd0cdecf2011-12-08 19:34:01 +00001097 for (uint32_t id = 0; id < numIOThreads_; ++id) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001098 // the first IO thread also does the listening on server socket
Roger Meier0be9ffa2013-07-19 21:10:01 +02001099 THRIFT_SOCKET listenFd = (id == 0 ? serverSocket_ : THRIFT_INVALID_SOCKET);
Mark Slee2f6404d2006-10-10 01:37:40 +00001100
Jake Farrellb0d95602011-12-06 01:17:26 +00001101 shared_ptr<TNonblockingIOThread> thread(
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001102 new TNonblockingIOThread(this, id, listenFd, useHighPriorityIOThreads_));
Jake Farrellb0d95602011-12-06 01:17:26 +00001103 ioThreads_.push_back(thread);
1104 }
1105
1106 // Notify handler of the preServe event
Roger Meier72957452013-06-29 00:28:50 +02001107 if (eventHandler_) {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +00001108 eventHandler_->preServe();
dweatherford58985992007-06-19 23:10:19 +00001109 }
1110
Jake Farrellb0d95602011-12-06 01:17:26 +00001111 // Start all of our helper IO threads. Note that the threads run forever,
1112 // only terminating if stop() is called.
1113 assert(ioThreads_.size() == numIOThreads_);
1114 assert(ioThreads_.size() > 0);
1115
Divya Thaluru808d1432017-08-06 16:36:36 -07001116 GlobalOutput.printf("TNonblockingServer: Serving with %d io threads.",
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001117 ioThreads_.size());
Jake Farrellb0d95602011-12-06 01:17:26 +00001118
1119 // Launch all the secondary IO threads in separate threads
1120 if (ioThreads_.size() > 1) {
cyyca8af9b2019-01-11 22:13:12 +08001121 ioThreadFactory_.reset(new ThreadFactory(
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001122 false // detached
1123 ));
Jake Farrellb0d95602011-12-06 01:17:26 +00001124
1125 assert(ioThreadFactory_.get());
1126
1127 // intentionally starting at thread 1, not 0
Roger Meierd0cdecf2011-12-08 19:34:01 +00001128 for (uint32_t i = 1; i < ioThreads_.size(); ++i) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001129 shared_ptr<Thread> thread = ioThreadFactory_->newThread(ioThreads_[i]);
1130 ioThreads_[i]->setThread(thread);
1131 thread->start();
1132 }
1133 }
1134
Roger Meier6f2a5032013-07-08 23:35:25 +02001135 // Register the events for the primary (listener) IO thread
1136 ioThreads_[0]->registerEvents();
1137}
1138
1139/**
1140 * Main workhorse function, starts up the server listening on a port and
1141 * loops over the libevent handler.
1142 */
1143void TNonblockingServer::serve() {
1144
Konrad Grochowski1f6e3802015-05-18 18:10:06 +02001145 if (ioThreads_.empty())
Sebastian Zenker042580f2019-01-29 15:48:12 +01001146 registerEvents(nullptr);
Roger Meier6f2a5032013-07-08 23:35:25 +02001147
Jake Farrellb0d95602011-12-06 01:17:26 +00001148 // Run the primary (listener) IO thread loop in our main thread; this will
1149 // only return when the server is shutting down.
1150 ioThreads_[0]->run();
1151
1152 // Ensure all threads are finished before exiting serve()
Roger Meierd0cdecf2011-12-08 19:34:01 +00001153 for (uint32_t i = 0; i < ioThreads_.size(); ++i) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001154 ioThreads_[i]->join();
1155 GlobalOutput.printf("TNonblocking: join done for IO thread #%d", i);
1156 }
Mark Slee2f6404d2006-10-10 01:37:40 +00001157}
1158
Jake Farrellb0d95602011-12-06 01:17:26 +00001159TNonblockingIOThread::TNonblockingIOThread(TNonblockingServer* server,
1160 int number,
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001161 THRIFT_SOCKET listenSocket,
Jake Farrellb0d95602011-12-06 01:17:26 +00001162 bool useHighPriority)
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001163 : server_(server),
1164 number_(number),
cyy64750162019-02-08 13:40:59 +08001165 threadId_{},
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001166 listenSocket_(listenSocket),
1167 useHighPriority_(useHighPriority),
Sebastian Zenker042580f2019-01-29 15:48:12 +01001168 eventBase_(nullptr),
cyy64750162019-02-08 13:40:59 +08001169 ownEventBase_(false),
1170 serverEvent_{},
1171 notificationEvent_{} {
Jake Farrellb0d95602011-12-06 01:17:26 +00001172 notificationPipeFDs_[0] = -1;
1173 notificationPipeFDs_[1] = -1;
1174}
1175
1176TNonblockingIOThread::~TNonblockingIOThread() {
1177 // make sure our associated thread is fully finished
1178 join();
1179
Roger Meier6f2a5032013-07-08 23:35:25 +02001180 if (eventBase_ && ownEventBase_) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001181 event_base_free(eventBase_);
Roger Meier6f2a5032013-07-08 23:35:25 +02001182 ownEventBase_ = false;
Bryan Duxbury76c43682011-08-24 21:26:48 +00001183 }
1184
gzshi41945622017-01-06 10:47:03 +08001185 if (listenSocket_ != THRIFT_INVALID_SOCKET) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001186 if (0 != ::THRIFT_CLOSESOCKET(listenSocket_)) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001187 GlobalOutput.perror("TNonblockingIOThread listenSocket_ close(): ", THRIFT_GET_SOCKET_ERROR);
Jake Farrellb0d95602011-12-06 01:17:26 +00001188 }
Roger Meier0be9ffa2013-07-19 21:10:01 +02001189 listenSocket_ = THRIFT_INVALID_SOCKET;
Jake Farrellb0d95602011-12-06 01:17:26 +00001190 }
1191
cyy64750162019-02-08 13:40:59 +08001192 for (auto notificationPipeFD : notificationPipeFDs_) {
1193 if (notificationPipeFD >= 0) {
1194 if (0 != ::THRIFT_CLOSESOCKET(notificationPipeFD)) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001195 GlobalOutput.perror("TNonblockingIOThread notificationPipe close(): ",
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001196 THRIFT_GET_SOCKET_ERROR);
Jake Farrellb0d95602011-12-06 01:17:26 +00001197 }
cyy64750162019-02-08 13:40:59 +08001198 notificationPipeFD = THRIFT_INVALID_SOCKET;
Jake Farrellb0d95602011-12-06 01:17:26 +00001199 }
1200 }
1201}
1202
1203void TNonblockingIOThread::createNotificationPipe() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001204 if (evutil_socketpair(AF_LOCAL, SOCK_STREAM, 0, notificationPipeFDs_) == -1) {
Roger Meier12d70532011-12-14 23:35:28 +00001205 GlobalOutput.perror("TNonblockingServer::createNotificationPipe ", EVUTIL_SOCKET_ERROR());
Jake Farrellb0d95602011-12-06 01:17:26 +00001206 throw TException("can't create notification pipe");
1207 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001208 if (evutil_make_socket_nonblocking(notificationPipeFDs_[0]) < 0
1209 || evutil_make_socket_nonblocking(notificationPipeFDs_[1]) < 0) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001210 ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]);
1211 ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]);
1212 throw TException("TNonblockingServer::createNotificationPipe() THRIFT_O_NONBLOCK");
Jake Farrellb0d95602011-12-06 01:17:26 +00001213 }
cyy64750162019-02-08 13:40:59 +08001214 for (auto notificationPipeFD : notificationPipeFDs_) {
Roger Meier12d70532011-12-14 23:35:28 +00001215#if LIBEVENT_VERSION_NUMBER < 0x02000000
1216 int flags;
cyy64750162019-02-08 13:40:59 +08001217 if ((flags = THRIFT_FCNTL(notificationPipeFD, F_GETFD, 0)) < 0
1218 || THRIFT_FCNTL(notificationPipeFD, F_SETFD, flags | FD_CLOEXEC) < 0) {
Roger Meier12d70532011-12-14 23:35:28 +00001219#else
cyy64750162019-02-08 13:40:59 +08001220 if (evutil_make_socket_closeonexec(notificationPipeFD) < 0) {
Roger Meier12d70532011-12-14 23:35:28 +00001221#endif
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001222 ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]);
1223 ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001224 throw TException(
1225 "TNonblockingServer::createNotificationPipe() "
1226 "FD_CLOEXEC");
Jake Farrellb0d95602011-12-06 01:17:26 +00001227 }
1228 }
1229}
1230
1231/**
1232 * Register the core libevent events onto the proper base.
1233 */
1234void TNonblockingIOThread::registerEvents() {
Roger Meier6f2a5032013-07-08 23:35:25 +02001235 threadId_ = Thread::get_current();
1236
Sebastian Zenker042580f2019-01-29 15:48:12 +01001237 assert(eventBase_ == nullptr);
Roger Meier6f2a5032013-07-08 23:35:25 +02001238 eventBase_ = getServer()->getUserEventBase();
Sebastian Zenker042580f2019-01-29 15:48:12 +01001239 if (eventBase_ == nullptr) {
Roger Meier6f2a5032013-07-08 23:35:25 +02001240 eventBase_ = event_base_new();
1241 ownEventBase_ = true;
1242 }
1243
1244 // Print some libevent stats
1245 if (number_ == 0) {
1246 GlobalOutput.printf("TNonblockingServer: using libevent %s method %s",
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001247 event_get_version(),
1248 event_base_get_method(eventBase_));
Roger Meier6f2a5032013-07-08 23:35:25 +02001249 }
1250
gzshi41945622017-01-06 10:47:03 +08001251 if (listenSocket_ != THRIFT_INVALID_SOCKET) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001252 // Register the server event
1253 event_set(&serverEvent_,
1254 listenSocket_,
1255 EV_READ | EV_PERSIST,
1256 TNonblockingIOThread::listenHandler,
1257 server_);
1258 event_base_set(eventBase_, &serverEvent_);
1259
1260 // Add the event and start up the server
Sebastian Zenker042580f2019-01-29 15:48:12 +01001261 if (-1 == event_add(&serverEvent_, nullptr)) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001262 throw TException(
1263 "TNonblockingServer::serve(): "
1264 "event_add() failed on server listen event");
Jake Farrellb0d95602011-12-06 01:17:26 +00001265 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001266 GlobalOutput.printf("TNonblocking: IO thread #%d registered for listen.", number_);
Jake Farrellb0d95602011-12-06 01:17:26 +00001267 }
1268
1269 createNotificationPipe();
1270
1271 // Create an event to be notified when a task finishes
1272 event_set(&notificationEvent_,
1273 getNotificationRecvFD(),
1274 EV_READ | EV_PERSIST,
1275 TNonblockingIOThread::notifyHandler,
1276 this);
1277
1278 // Attach to the base
1279 event_base_set(eventBase_, &notificationEvent_);
1280
1281 // Add the event and start up the server
Sebastian Zenker042580f2019-01-29 15:48:12 +01001282 if (-1 == event_add(&notificationEvent_, nullptr)) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001283 throw TException(
1284 "TNonblockingServer::serve(): "
1285 "event_add() failed on task-done notification event");
Jake Farrellb0d95602011-12-06 01:17:26 +00001286 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001287 GlobalOutput.printf("TNonblocking: IO thread #%d registered for notify.", number_);
Jake Farrellb0d95602011-12-06 01:17:26 +00001288}
1289
1290bool TNonblockingIOThread::notify(TNonblockingServer::TConnection* conn) {
cyy9fed9012019-01-16 14:43:51 +08001291 auto fd = getNotificationSendFD();
Jake Farrellb0d95602011-12-06 01:17:26 +00001292 if (fd < 0) {
1293 return false;
1294 }
1295
st0ke961fa702018-10-12 18:37:40 +07001296 int ret = -1;
tpcwangf98d59f2016-03-23 16:18:52 -07001297 long kSize = sizeof(conn);
st0ke961fa702018-10-12 18:37:40 +07001298 const char * pos = (const char *)const_cast_sockopt(&conn);
1299
1300#if defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)
1301 struct pollfd pfd = {fd, POLLOUT, 0};
1302
1303 while (kSize > 0) {
1304 pfd.revents = 0;
1305 ret = poll(&pfd, 1, -1);
1306 if (ret < 0) {
1307 return false;
1308 } else if (ret == 0) {
1309 continue;
1310 }
1311
1312 if (pfd.revents & POLLHUP || pfd.revents & POLLERR) {
1313 ::THRIFT_CLOSESOCKET(fd);
1314 return false;
1315 }
1316
1317 if (pfd.revents & POLLOUT) {
1318 ret = send(fd, pos, kSize, 0);
1319 if (ret < 0) {
1320 if (errno == EAGAIN) {
1321 continue;
1322 }
1323
1324 ::THRIFT_CLOSESOCKET(fd);
1325 return false;
1326 }
1327
1328 kSize -= ret;
1329 pos += ret;
1330 }
1331 }
1332#else
1333 fd_set wfds, efds;
abadcafe38772c92015-04-03 22:23:04 +08001334
1335 while (kSize > 0) {
Lei Feiweib5ebcd12015-04-04 22:12:07 +08001336 FD_ZERO(&wfds);
1337 FD_ZERO(&efds);
1338 FD_SET(fd, &wfds);
1339 FD_SET(fd, &efds);
tpcwangf98d59f2016-03-23 16:18:52 -07001340 ret = select(static_cast<int>(fd + 1), NULL, &wfds, &efds, NULL);
abadcafe38772c92015-04-03 22:23:04 +08001341 if (ret < 0) {
1342 return false;
1343 } else if (ret == 0) {
1344 continue;
1345 }
1346
Lei Feiweib5ebcd12015-04-04 22:12:07 +08001347 if (FD_ISSET(fd, &efds)) {
1348 ::THRIFT_CLOSESOCKET(fd);
abadcafe38772c92015-04-03 22:23:04 +08001349 return false;
1350 }
1351
Lei Feiweib5ebcd12015-04-04 22:12:07 +08001352 if (FD_ISSET(fd, &wfds)) {
abadcafe38772c92015-04-03 22:23:04 +08001353 ret = send(fd, pos, kSize, 0);
1354 if (ret < 0) {
1355 if (errno == EAGAIN) {
1356 continue;
1357 }
1358
Lei Feiweib5ebcd12015-04-04 22:12:07 +08001359 ::THRIFT_CLOSESOCKET(fd);
abadcafe38772c92015-04-03 22:23:04 +08001360 return false;
1361 }
1362
1363 kSize -= ret;
1364 pos += ret;
1365 }
Jake Farrellb0d95602011-12-06 01:17:26 +00001366 }
st0ke961fa702018-10-12 18:37:40 +07001367#endif
Jake Farrellb0d95602011-12-06 01:17:26 +00001368
1369 return true;
1370}
1371
1372/* static */
Roger Meier12d70532011-12-14 23:35:28 +00001373void TNonblockingIOThread::notifyHandler(evutil_socket_t fd, short which, void* v) {
Sebastian Zenker042580f2019-01-29 15:48:12 +01001374 auto* ioThread = (TNonblockingIOThread*)v;
Jake Farrellb0d95602011-12-06 01:17:26 +00001375 assert(ioThread);
Roger Meierd0cdecf2011-12-08 19:34:01 +00001376 (void)which;
Jake Farrellb0d95602011-12-06 01:17:26 +00001377
1378 while (true) {
Sebastian Zenker042580f2019-01-29 15:48:12 +01001379 TNonblockingServer::TConnection* connection = nullptr;
Jake Farrellb0d95602011-12-06 01:17:26 +00001380 const int kSize = sizeof(connection);
Ben Craig64935232013-10-09 15:21:38 -05001381 long nBytes = recv(fd, cast_sockopt(&connection), kSize, 0);
Jake Farrellb0d95602011-12-06 01:17:26 +00001382 if (nBytes == kSize) {
Sebastian Zenker042580f2019-01-29 15:48:12 +01001383 if (connection == nullptr) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001384 // this is the command to stop our thread, exit the handler!
Buğra Gedik36d1b0d2016-09-04 17:18:15 +09001385 ioThread->breakLoop(false);
Jake Farrellb0d95602011-12-06 01:17:26 +00001386 return;
1387 }
1388 connection->transition();
1389 } else if (nBytes > 0) {
1390 // throw away these bytes and hope that next time we get a solid read
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001391 GlobalOutput.printf("notifyHandler: Bad read of %d bytes, wanted %d", nBytes, kSize);
Jake Farrellb0d95602011-12-06 01:17:26 +00001392 ioThread->breakLoop(true);
1393 return;
1394 } else if (nBytes == 0) {
1395 GlobalOutput.printf("notifyHandler: Notify socket closed!");
Buğra Gedik36d1b0d2016-09-04 17:18:15 +09001396 ioThread->breakLoop(false);
Jake Farrellb0d95602011-12-06 01:17:26 +00001397 // exit the loop
1398 break;
1399 } else { // nBytes < 0
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001400 if (THRIFT_GET_SOCKET_ERROR != THRIFT_EWOULDBLOCK
1401 && THRIFT_GET_SOCKET_ERROR != THRIFT_EAGAIN) {
1402 GlobalOutput.perror("TNonblocking: notifyHandler read() failed: ", THRIFT_GET_SOCKET_ERROR);
1403 ioThread->breakLoop(true);
1404 return;
Jake Farrellb0d95602011-12-06 01:17:26 +00001405 }
1406 // exit the loop
1407 break;
1408 }
1409 }
1410}
1411
1412void TNonblockingIOThread::breakLoop(bool error) {
1413 if (error) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001414 GlobalOutput.printf("TNonblockingServer: IO thread #%d exiting with error.", number_);
Jake Farrellb0d95602011-12-06 01:17:26 +00001415 // TODO: figure out something better to do here, but for now kill the
1416 // whole process.
1417 GlobalOutput.printf("TNonblockingServer: aborting process.");
1418 ::abort();
1419 }
1420
Jake Farrellb0d95602011-12-06 01:17:26 +00001421 // If we're running in the same thread, we can't use the notify(0)
1422 // mechanism to stop the thread, but happily if we're running in the
1423 // same thread, this means the thread can't be blocking in the event
1424 // loop either.
Roger Meier12d70532011-12-14 23:35:28 +00001425 if (!Thread::is_current(threadId_)) {
Sebastian Zenker042580f2019-01-29 15:48:12 +01001426 notify(nullptr);
Buğra Gedik36d1b0d2016-09-04 17:18:15 +09001427 } else {
1428 // cause the loop to stop ASAP - even if it has things to do in it
1429 event_base_loopbreak(eventBase_);
Jake Farrellb0d95602011-12-06 01:17:26 +00001430 }
1431}
1432
1433void TNonblockingIOThread::setCurrentThreadHighPriority(bool value) {
Roger Meier12d70532011-12-14 23:35:28 +00001434#ifdef HAVE_SCHED_H
Jake Farrellb0d95602011-12-06 01:17:26 +00001435 // Start out with a standard, low-priority setup for the sched params.
1436 struct sched_param sp;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001437 bzero((void*)&sp, sizeof(sp));
Jake Farrellb0d95602011-12-06 01:17:26 +00001438 int policy = SCHED_OTHER;
1439
1440 // If desired, set up high-priority sched params structure.
1441 if (value) {
1442 // FIFO scheduler, ranked above default SCHED_OTHER queue
1443 policy = SCHED_FIFO;
1444 // The priority only compares us to other SCHED_FIFO threads, so we
1445 // just pick a random priority halfway between min & max.
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001446 const int priority = (sched_get_priority_max(policy) + sched_get_priority_min(policy)) / 2;
Jake Farrellb0d95602011-12-06 01:17:26 +00001447
1448 sp.sched_priority = priority;
Bryan Duxbury76c43682011-08-24 21:26:48 +00001449 }
1450
Jake Farrellb0d95602011-12-06 01:17:26 +00001451 // Actually set the sched params for the current thread.
1452 if (0 == pthread_setschedparam(pthread_self(), policy, &sp)) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001453 GlobalOutput.printf("TNonblocking: IO Thread #%d using high-priority scheduler!", number_);
Jake Farrellb0d95602011-12-06 01:17:26 +00001454 } else {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001455 GlobalOutput.perror("TNonblocking: pthread_setschedparam(): ", THRIFT_GET_SOCKET_ERROR);
Jake Farrellb0d95602011-12-06 01:17:26 +00001456 }
Roger Meierd051ca02013-08-15 01:35:11 +02001457#else
1458 THRIFT_UNUSED_VARIABLE(value);
Roger Meier12d70532011-12-14 23:35:28 +00001459#endif
Jake Farrellb0d95602011-12-06 01:17:26 +00001460}
Bryan Duxbury76c43682011-08-24 21:26:48 +00001461
Jake Farrellb0d95602011-12-06 01:17:26 +00001462void TNonblockingIOThread::run() {
Sebastian Zenker042580f2019-01-29 15:48:12 +01001463 if (eventBase_ == nullptr) {
Roger Meier6f2a5032013-07-08 23:35:25 +02001464 registerEvents();
Buğra Gedik36d1b0d2016-09-04 17:18:15 +09001465 }
Jake Farrellb0d95602011-12-06 01:17:26 +00001466 if (useHighPriority_) {
1467 setCurrentThreadHighPriority(true);
1468 }
1469
Sebastian Zenker042580f2019-01-29 15:48:12 +01001470 if (eventBase_ != nullptr)
Buğra Gedik36d1b0d2016-09-04 17:18:15 +09001471 {
1472 GlobalOutput.printf("TNonblockingServer: IO thread #%d entering loop...", number_);
1473 // Run libevent engine, never returns, invokes calls to eventHandler
1474 event_base_loop(eventBase_, 0);
Jake Farrellb0d95602011-12-06 01:17:26 +00001475
Buğra Gedik36d1b0d2016-09-04 17:18:15 +09001476 if (useHighPriority_) {
1477 setCurrentThreadHighPriority(false);
1478 }
1479
1480 // cleans up our registered events
1481 cleanupEvents();
Jake Farrellb0d95602011-12-06 01:17:26 +00001482 }
1483
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001484 GlobalOutput.printf("TNonblockingServer: IO thread #%d run() done!", number_);
Jake Farrellb0d95602011-12-06 01:17:26 +00001485}
1486
1487void TNonblockingIOThread::cleanupEvents() {
1488 // stop the listen socket, if any
gzshi41945622017-01-06 10:47:03 +08001489 if (listenSocket_ != THRIFT_INVALID_SOCKET) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001490 if (event_del(&serverEvent_) == -1) {
Carl Yeksigian7cb7fc82013-06-07 07:33:01 -04001491 GlobalOutput.perror("TNonblockingIOThread::stop() event_del: ", THRIFT_GET_SOCKET_ERROR);
Jake Farrellb0d95602011-12-06 01:17:26 +00001492 }
1493 }
1494
1495 event_del(&notificationEvent_);
1496}
1497
Jake Farrellb0d95602011-12-06 01:17:26 +00001498void TNonblockingIOThread::stop() {
1499 // This should cause the thread to fall out of its event loop ASAP.
1500 breakLoop(false);
1501}
1502
1503void TNonblockingIOThread::join() {
1504 // If this was a thread created by a factory (not the thread that called
1505 // serve()), we join() it to make sure we shut down fully.
1506 if (thread_) {
1507 try {
1508 // Note that it is safe to both join() ourselves twice, as well as join
1509 // the current thread as the pthread implementation checks for deadlock.
1510 thread_->join();
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001511 } catch (...) {
Jake Farrellb0d95602011-12-06 01:17:26 +00001512 // swallow everything
1513 }
1514 }
Bryan Duxbury76c43682011-08-24 21:26:48 +00001515}
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001516}
1517}
1518} // apache::thrift::server