David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 20 | #define __STDC_FORMAT_MACROS |
| 21 | |
Konrad Grochowski | 9be4e68 | 2013-06-22 22:03:31 +0200 | [diff] [blame] | 22 | #include <thrift/thrift-config.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 23 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 24 | #include <thrift/server/TNonblockingServer.h> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 25 | #include <thrift/concurrency/Exception.h> |
| 26 | #include <thrift/transport/TSocket.h> |
| 27 | #include <thrift/concurrency/PlatformThreadFactory.h> |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 28 | #include <thrift/transport/PlatformSocket.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 29 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 30 | #include <iostream> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 31 | |
| 32 | #ifdef HAVE_SYS_SOCKET_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 33 | #include <sys/socket.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #ifdef HAVE_NETINET_IN_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 37 | #include <netinet/in.h> |
| 38 | #include <netinet/tcp.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | #ifdef HAVE_ARPA_INET_H |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 42 | #include <arpa/inet.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #ifdef HAVE_NETDB_H |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 46 | #include <netdb.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 49 | #ifdef HAVE_FCNTL_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 50 | #include <fcntl.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 53 | #include <assert.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 54 | |
| 55 | #ifdef HAVE_SCHED_H |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 56 | #include <sched.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 57 | #endif |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 58 | |
David Reiss | 9b90344 | 2009-10-21 05:51:28 +0000 | [diff] [blame] | 59 | #ifndef AF_LOCAL |
| 60 | #define AF_LOCAL AF_UNIX |
| 61 | #endif |
| 62 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 63 | #if !defined(PRIu32) |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 64 | #define PRIu32 "I32u" |
Roger Meier | f2b094f | 2013-06-04 22:09:37 +0200 | [diff] [blame] | 65 | #define PRIu64 "I64u" |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 68 | namespace apache { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 69 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 70 | using namespace apache::thrift::protocol; |
| 71 | using namespace apache::thrift::transport; |
| 72 | using namespace apache::thrift::concurrency; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 73 | using namespace std; |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 74 | using apache::thrift::transport::TSocket; |
| 75 | using apache::thrift::transport::TTransportException; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 76 | using boost::shared_ptr; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 77 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 78 | /// Three states for sockets: recv frame size, recv data, and send mode |
| 79 | enum TSocketState { |
| 80 | SOCKET_RECV_FRAMING, |
| 81 | SOCKET_RECV, |
| 82 | SOCKET_SEND |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Five states for the nonblocking server: |
| 87 | * 1) initialize |
| 88 | * 2) read 4 byte frame size |
| 89 | * 3) read frame of data |
| 90 | * 4) send back data (if any) |
| 91 | * 5) force immediate connection close |
| 92 | */ |
| 93 | enum TAppState { |
| 94 | APP_INIT, |
| 95 | APP_READ_FRAME_SIZE, |
| 96 | APP_READ_REQUEST, |
| 97 | APP_WAIT_TASK, |
| 98 | APP_SEND_RESULT, |
| 99 | APP_CLOSE_CONNECTION |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * Represents a connection that is handled via libevent. This connection |
| 104 | * essentially encapsulates a socket that has some associated libevent state. |
| 105 | */ |
| 106 | class TNonblockingServer::TConnection { |
| 107 | private: |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 108 | /// Server IO Thread handling this connection |
| 109 | TNonblockingIOThread* ioThread_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 110 | |
| 111 | /// Server handle |
| 112 | TNonblockingServer* server_; |
| 113 | |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 114 | /// TProcessor |
| 115 | boost::shared_ptr<TProcessor> processor_; |
| 116 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 117 | /// Object wrapping network socket |
| 118 | boost::shared_ptr<TSocket> tSocket_; |
| 119 | |
| 120 | /// Libevent object |
| 121 | struct event event_; |
| 122 | |
| 123 | /// Libevent flags |
| 124 | short eventFlags_; |
| 125 | |
| 126 | /// Socket mode |
| 127 | TSocketState socketState_; |
| 128 | |
| 129 | /// Application state |
| 130 | TAppState appState_; |
| 131 | |
| 132 | /// How much data needed to read |
| 133 | uint32_t readWant_; |
| 134 | |
| 135 | /// Where in the read buffer are we |
| 136 | uint32_t readBufferPos_; |
| 137 | |
| 138 | /// Read buffer |
| 139 | uint8_t* readBuffer_; |
| 140 | |
| 141 | /// Read buffer size |
| 142 | uint32_t readBufferSize_; |
| 143 | |
| 144 | /// Write buffer |
| 145 | uint8_t* writeBuffer_; |
| 146 | |
| 147 | /// Write buffer size |
| 148 | uint32_t writeBufferSize_; |
| 149 | |
| 150 | /// How far through writing are we? |
| 151 | uint32_t writeBufferPos_; |
| 152 | |
| 153 | /// Largest size of write buffer seen since buffer was constructed |
| 154 | size_t largestWriteBufferSize_; |
| 155 | |
| 156 | /// Count of the number of calls for use with getResizeBufferEveryN(). |
| 157 | int32_t callsForResize_; |
| 158 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 159 | /// Transport to read from |
| 160 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
| 161 | |
| 162 | /// Transport that processor writes to |
| 163 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
| 164 | |
| 165 | /// extra transport generated by transport factory (e.g. BufferedRouterTransport) |
| 166 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 167 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
| 168 | |
| 169 | /// Protocol decoder |
| 170 | boost::shared_ptr<TProtocol> inputProtocol_; |
| 171 | |
| 172 | /// Protocol encoder |
| 173 | boost::shared_ptr<TProtocol> outputProtocol_; |
| 174 | |
| 175 | /// Server event handler, if any |
| 176 | boost::shared_ptr<TServerEventHandler> serverEventHandler_; |
| 177 | |
| 178 | /// Thrift call context, if any |
| 179 | void *connectionContext_; |
| 180 | |
| 181 | /// Go into read mode |
| 182 | void setRead() { |
| 183 | setFlags(EV_READ | EV_PERSIST); |
| 184 | } |
| 185 | |
| 186 | /// Go into write mode |
| 187 | void setWrite() { |
| 188 | setFlags(EV_WRITE | EV_PERSIST); |
| 189 | } |
| 190 | |
| 191 | /// Set socket idle |
| 192 | void setIdle() { |
| 193 | setFlags(0); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Set event flags for this connection. |
| 198 | * |
| 199 | * @param eventFlags flags we pass to libevent for the connection. |
| 200 | */ |
| 201 | void setFlags(short eventFlags); |
| 202 | |
| 203 | /** |
| 204 | * Libevent handler called (via our static wrapper) when the connection |
| 205 | * socket had something happen. Rather than use the flags libevent passed, |
| 206 | * we use the connection state to determine whether we need to read or |
| 207 | * write the socket. |
| 208 | */ |
| 209 | void workSocket(); |
| 210 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 211 | public: |
| 212 | |
| 213 | class Task; |
| 214 | |
| 215 | /// Constructor |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 216 | TConnection(THRIFT_SOCKET socket, TNonblockingIOThread* ioThread, |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 217 | const sockaddr* addr, socklen_t addrLen) { |
| 218 | readBuffer_ = NULL; |
| 219 | readBufferSize_ = 0; |
| 220 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 221 | ioThread_ = ioThread; |
| 222 | server_ = ioThread->getServer(); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 223 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 224 | // 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 Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 227 | outputTransport_.reset( |
| 228 | new TMemoryBuffer(static_cast<uint32_t>(server_->getWriteBufferDefaultSize()))); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 229 | tSocket_.reset(new TSocket()); |
| 230 | init(socket, ioThread, addr, addrLen); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | ~TConnection() { |
| 234 | std::free(readBuffer_); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 237 | /// Close this connection and free or reset its resources. |
| 238 | void close(); |
| 239 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 240 | /** |
| 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 | */ |
| 246 | void checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit); |
| 247 | |
| 248 | /// Initialize |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 249 | void init(THRIFT_SOCKET socket, TNonblockingIOThread* ioThread, |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 250 | const sockaddr* addr, socklen_t addrLen); |
| 251 | |
| 252 | /** |
| 253 | * This is called when the application transitions from one state into |
| 254 | * another. This means that it has finished writing the data that it needed |
| 255 | * to, or finished receiving the data that it needed to. |
| 256 | */ |
| 257 | void transition(); |
| 258 | |
| 259 | /** |
| 260 | * C-callable event handler for connection events. Provides a callback |
| 261 | * that libevent can understand which invokes connection_->workSocket(). |
| 262 | * |
| 263 | * @param fd the descriptor the event occurred on. |
| 264 | * @param which the flags associated with the event. |
| 265 | * @param v void* callback arg where we placed TConnection's "this". |
| 266 | */ |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 267 | static void eventHandler(evutil_socket_t fd, short /* which */, void* v) { |
Konrad Grochowski | b7af66e | 2014-07-08 19:22:44 +0200 | [diff] [blame] | 268 | assert(fd == static_cast<evutil_socket_t>(((TConnection*)v)->getTSocket()->getSocketFD())); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 269 | ((TConnection*)v)->workSocket(); |
| 270 | } |
| 271 | |
| 272 | /** |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 273 | * Notification to server that processing has ended on this request. |
| 274 | * Can be called either when processing is completed or when a waiting |
| 275 | * task has been preemptively terminated (on overload). |
| 276 | * |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 277 | * Don't call this from the IO thread itself. |
| 278 | * |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 279 | * @return true if successful, false if unable to notify (check THRIFT_GET_SOCKET_ERROR). |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 280 | */ |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 281 | bool notifyIOThread() { |
| 282 | return ioThread_->notify(this); |
| 283 | } |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 284 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 285 | /* |
| 286 | * Returns the number of this connection's currently assigned IO |
| 287 | * thread. |
| 288 | */ |
| 289 | int getIOThreadNumber() const { |
| 290 | return ioThread_->getThreadNumber(); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /// Force connection shutdown for this connection. |
| 294 | void forceClose() { |
| 295 | appState_ = APP_CLOSE_CONNECTION; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 296 | if (!notifyIOThread()) { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 297 | throw TException("TConnection::forceClose: failed write on notify pipe"); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /// return the server this connection was initialized for. |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 302 | TNonblockingServer* getServer() const { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 303 | return server_; |
| 304 | } |
| 305 | |
| 306 | /// get state of connection. |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 307 | TAppState getState() const { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 308 | return appState_; |
| 309 | } |
| 310 | |
| 311 | /// return the TSocket transport wrapping this network connection |
| 312 | boost::shared_ptr<TSocket> getTSocket() const { |
| 313 | return tSocket_; |
| 314 | } |
| 315 | |
| 316 | /// return the server event handler if any |
| 317 | boost::shared_ptr<TServerEventHandler> getServerEventHandler() { |
| 318 | return serverEventHandler_; |
| 319 | } |
| 320 | |
| 321 | /// return the Thrift connection context if any |
| 322 | void* getConnectionContext() { |
| 323 | return connectionContext_; |
| 324 | } |
| 325 | |
| 326 | }; |
| 327 | |
| 328 | class TNonblockingServer::TConnection::Task: public Runnable { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 329 | public: |
| 330 | Task(boost::shared_ptr<TProcessor> processor, |
| 331 | boost::shared_ptr<TProtocol> input, |
| 332 | boost::shared_ptr<TProtocol> output, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 333 | TConnection* connection) : |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 334 | processor_(processor), |
| 335 | input_(input), |
| 336 | output_(output), |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 337 | connection_(connection), |
| 338 | serverEventHandler_(connection_->getServerEventHandler()), |
| 339 | connectionContext_(connection_->getConnectionContext()) {} |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 340 | |
| 341 | void run() { |
| 342 | try { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 343 | for (;;) { |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 344 | if (serverEventHandler_) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 345 | serverEventHandler_->processContext(connectionContext_, connection_->getTSocket()); |
| 346 | } |
| 347 | if (!processor_->process(input_, output_, connectionContext_) || |
| 348 | !input_->getTransport()->peek()) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | } |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 352 | } catch (const TTransportException& ttx) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 353 | GlobalOutput.printf("TNonblockingServer: client died: %s", ttx.what()); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 354 | } catch (const bad_alloc&) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 355 | GlobalOutput("TNonblockingServer: caught bad_alloc exception."); |
Henrique Mendonca | 962b353 | 2012-09-20 13:19:55 +0000 | [diff] [blame] | 356 | exit(1); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 357 | } catch (const std::exception& x) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 358 | GlobalOutput.printf("TNonblockingServer: process() exception: %s: %s", |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 359 | typeid(x).name(), x.what()); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 360 | } catch (...) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 361 | GlobalOutput.printf( |
| 362 | "TNonblockingServer: unknown exception while processing."); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 363 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 364 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 365 | // Signal completion back to the libevent thread via a pipe |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 366 | if (!connection_->notifyIOThread()) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 367 | throw TException("TNonblockingServer::Task::run: failed write on notify pipe"); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 368 | } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | TConnection* getTConnection() { |
| 372 | return connection_; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | private: |
| 376 | boost::shared_ptr<TProcessor> processor_; |
| 377 | boost::shared_ptr<TProtocol> input_; |
| 378 | boost::shared_ptr<TProtocol> output_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 379 | TConnection* connection_; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 380 | boost::shared_ptr<TServerEventHandler> serverEventHandler_; |
| 381 | void* connectionContext_; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 382 | }; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 383 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 384 | void TNonblockingServer::TConnection::init(THRIFT_SOCKET socket, |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 385 | TNonblockingIOThread* ioThread, |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 386 | const sockaddr* addr, |
| 387 | socklen_t addrLen) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 388 | tSocket_->setSocketFD(socket); |
| 389 | tSocket_->setCachedAddress(addr, addrLen); |
| 390 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 391 | ioThread_ = ioThread; |
| 392 | server_ = ioThread->getServer(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 393 | appState_ = APP_INIT; |
| 394 | eventFlags_ = 0; |
| 395 | |
| 396 | readBufferPos_ = 0; |
| 397 | readWant_ = 0; |
| 398 | |
| 399 | writeBuffer_ = NULL; |
| 400 | writeBufferSize_ = 0; |
| 401 | writeBufferPos_ = 0; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 402 | largestWriteBufferSize_ = 0; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 403 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 404 | socketState_ = SOCKET_RECV_FRAMING; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 405 | callsForResize_ = 0; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 406 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 407 | // get input/transports |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 408 | factoryInputTransport_ = server_->getInputTransportFactory()->getTransport( |
| 409 | inputTransport_); |
| 410 | factoryOutputTransport_ = server_->getOutputTransportFactory()->getTransport( |
| 411 | outputTransport_); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 412 | |
| 413 | // Create protocol |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 414 | inputProtocol_ = server_->getInputProtocolFactory()->getProtocol( |
| 415 | factoryInputTransport_); |
| 416 | outputProtocol_ = server_->getOutputProtocolFactory()->getProtocol( |
| 417 | factoryOutputTransport_); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 418 | |
| 419 | // Set up for any server event handler |
| 420 | serverEventHandler_ = server_->getEventHandler(); |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 421 | if (serverEventHandler_) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 422 | connectionContext_ = serverEventHandler_->createContext(inputProtocol_, |
| 423 | outputProtocol_); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 424 | } else { |
| 425 | connectionContext_ = NULL; |
| 426 | } |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 427 | |
| 428 | // Get the processor |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 429 | processor_ = server_->getProcessor(inputProtocol_, outputProtocol_, tSocket_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 432 | void TNonblockingServer::TConnection::workSocket() { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 433 | int got=0, left=0, sent=0; |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 434 | uint32_t fetch = 0; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 435 | |
| 436 | switch (socketState_) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 437 | case SOCKET_RECV_FRAMING: |
| 438 | union { |
| 439 | uint8_t buf[sizeof(uint32_t)]; |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 440 | uint32_t size; |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 441 | } framing; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 442 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 443 | // if we've already received some bytes we kept them here |
| 444 | framing.size = readWant_; |
| 445 | // determine size of this frame |
| 446 | try { |
| 447 | // Read from the socket |
| 448 | fetch = tSocket_->read(&framing.buf[readBufferPos_], |
| 449 | uint32_t(sizeof(framing.size) - readBufferPos_)); |
| 450 | if (fetch == 0) { |
| 451 | // Whenever we get here it means a remote disconnect |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 452 | close(); |
| 453 | return; |
| 454 | } |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 455 | readBufferPos_ += fetch; |
| 456 | } catch (TTransportException& te) { |
| 457 | GlobalOutput.printf("TConnection::workSocket(): %s", te.what()); |
| 458 | close(); |
| 459 | |
| 460 | return; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 461 | } |
| 462 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 463 | if (readBufferPos_ < sizeof(framing.size)) { |
| 464 | // more needed before frame size is known -- save what we have so far |
| 465 | readWant_ = framing.size; |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | readWant_ = ntohl(framing.size); |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 470 | if (readWant_ > server_->getMaxFrameSize()) { |
| 471 | // Don't allow giant frame sizes. This prevents bad clients from |
| 472 | // causing us to try and allocate a giant buffer. |
| 473 | GlobalOutput.printf("TNonblockingServer: frame size too large " |
Roger Meier | 42cb873 | 2013-06-28 22:49:14 +0200 | [diff] [blame] | 474 | "(%" PRIu32 " > %" PRIu64 ") from client %s. " |
Roger Meier | f2b094f | 2013-06-04 22:09:37 +0200 | [diff] [blame] | 475 | "Remote side not using TFramedTransport?", |
| 476 | readWant_, |
| 477 | (uint64_t)server_->getMaxFrameSize(), |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 478 | tSocket_->getSocketInfo().c_str()); |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 479 | close(); |
| 480 | return; |
| 481 | } |
| 482 | // size known; now get the rest of the frame |
| 483 | transition(); |
| 484 | return; |
| 485 | |
| 486 | case SOCKET_RECV: |
| 487 | // It is an error to be in this state if we already have all the data |
| 488 | assert(readBufferPos_ < readWant_); |
| 489 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 490 | try { |
| 491 | // Read from the socket |
| 492 | fetch = readWant_ - readBufferPos_; |
| 493 | got = tSocket_->read(readBuffer_ + readBufferPos_, fetch); |
| 494 | } |
| 495 | catch (TTransportException& te) { |
| 496 | GlobalOutput.printf("TConnection::workSocket(): %s", te.what()); |
| 497 | close(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 498 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 499 | return; |
| 500 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 501 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 502 | if (got > 0) { |
| 503 | // Move along in the buffer |
| 504 | readBufferPos_ += got; |
| 505 | |
| 506 | // Check that we did not overdo it |
| 507 | assert(readBufferPos_ <= readWant_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 508 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 509 | // We are done reading, move onto the next state |
| 510 | if (readBufferPos_ == readWant_) { |
| 511 | transition(); |
| 512 | } |
| 513 | return; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Whenever we get down here it means a remote disconnect |
| 517 | close(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 518 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 519 | return; |
| 520 | |
| 521 | case SOCKET_SEND: |
| 522 | // Should never have position past size |
| 523 | assert(writeBufferPos_ <= writeBufferSize_); |
| 524 | |
| 525 | // If there is no data to send, then let us move on |
| 526 | if (writeBufferPos_ == writeBufferSize_) { |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 527 | GlobalOutput("WARNING: Send state with no data to send\n"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 528 | transition(); |
| 529 | return; |
| 530 | } |
| 531 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 532 | try { |
| 533 | left = writeBufferSize_ - writeBufferPos_; |
| 534 | sent = tSocket_->write_partial(writeBuffer_ + writeBufferPos_, left); |
| 535 | } |
| 536 | catch (TTransportException& te) { |
| 537 | GlobalOutput.printf("TConnection::workSocket(): %s ", te.what()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 538 | close(); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | writeBufferPos_ += sent; |
| 543 | |
| 544 | // Did we overdo it? |
| 545 | assert(writeBufferPos_ <= writeBufferSize_); |
| 546 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 547 | // We are done! |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 548 | if (writeBufferPos_ == writeBufferSize_) { |
| 549 | transition(); |
| 550 | } |
| 551 | |
| 552 | return; |
| 553 | |
| 554 | default: |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 555 | GlobalOutput.printf("Unexpected Socket State %d", socketState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 556 | assert(0); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * This is called when the application transitions from one state into |
| 562 | * another. This means that it has finished writing the data that it needed |
| 563 | * to, or finished receiving the data that it needed to. |
| 564 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 565 | void TNonblockingServer::TConnection::transition() { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 566 | // ensure this connection is active right now |
| 567 | assert(ioThread_); |
| 568 | assert(server_); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 569 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 570 | // Switch upon the state that we are currently in and move to a new state |
| 571 | switch (appState_) { |
| 572 | |
| 573 | case APP_READ_REQUEST: |
| 574 | // We are done reading the request, package the read buffer into transport |
| 575 | // and get back some data from the dispatch function |
| 576 | inputTransport_->resetBuffer(readBuffer_, readBufferPos_); |
David Reiss | 7197efb | 2010-10-06 17:10:43 +0000 | [diff] [blame] | 577 | outputTransport_->resetBuffer(); |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 578 | // Prepend four bytes of blank space to the buffer so we can |
| 579 | // write the frame size there later. |
| 580 | outputTransport_->getWritePtr(4); |
| 581 | outputTransport_->wroteBytes(4); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 582 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 583 | server_->incrementActiveProcessors(); |
| 584 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 585 | if (server_->isThreadPoolProcessing()) { |
| 586 | // We are setting up a Task to do this work and we will wait on it |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 587 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 588 | // Create task and dispatch to the thread manager |
| 589 | boost::shared_ptr<Runnable> task = |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 590 | boost::shared_ptr<Runnable>(new Task(processor_, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 591 | inputProtocol_, |
| 592 | outputProtocol_, |
| 593 | this)); |
| 594 | // The application is now waiting on the task to finish |
| 595 | appState_ = APP_WAIT_TASK; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 596 | |
David Reiss | e11f307 | 2008-10-07 21:39:19 +0000 | [diff] [blame] | 597 | try { |
| 598 | server_->addTask(task); |
| 599 | } catch (IllegalStateException & ise) { |
| 600 | // The ThreadManager is not ready to handle any more tasks (it's probably shutting down). |
David Reiss | c53a594 | 2008-10-07 23:55:24 +0000 | [diff] [blame] | 601 | GlobalOutput.printf("IllegalStateException: Server::process() %s", ise.what()); |
David Reiss | e11f307 | 2008-10-07 21:39:19 +0000 | [diff] [blame] | 602 | close(); |
| 603 | } |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 604 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 605 | // Set this connection idle so that libevent doesn't process more |
| 606 | // data on it while we're still waiting for the threadmanager to |
| 607 | // finish this task |
| 608 | setIdle(); |
| 609 | return; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 610 | } else { |
| 611 | try { |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 612 | if (serverEventHandler_) { |
| 613 | serverEventHandler_->processContext(connectionContext_, |
| 614 | getTSocket()); |
| 615 | } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 616 | // Invoke the processor |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 617 | processor_->process(inputProtocol_, outputProtocol_, |
| 618 | connectionContext_); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 619 | } catch (const TTransportException &ttx) { |
| 620 | GlobalOutput.printf("TNonblockingServer transport error in " |
| 621 | "process(): %s", ttx.what()); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 622 | server_->decrementActiveProcessors(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 623 | close(); |
| 624 | return; |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 625 | } catch (const std::exception &x) { |
| 626 | GlobalOutput.printf("Server::process() uncaught exception: %s: %s", |
| 627 | typeid(x).name(), x.what()); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 628 | server_->decrementActiveProcessors(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 629 | close(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 630 | return; |
| 631 | } catch (...) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 632 | GlobalOutput.printf("Server::process() unknown exception"); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 633 | server_->decrementActiveProcessors(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 634 | close(); |
| 635 | return; |
| 636 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 639 | // Intentionally fall through here, the call to process has written into |
| 640 | // the writeBuffer_ |
| 641 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 642 | case APP_WAIT_TASK: |
| 643 | // We have now finished processing a task and the result has been written |
| 644 | // into the outputTransport_, so we grab its contents and place them into |
| 645 | // the writeBuffer_ for actual writing by the libevent thread |
| 646 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 647 | server_->decrementActiveProcessors(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 648 | // Get the result of the operation |
| 649 | outputTransport_->getBuffer(&writeBuffer_, &writeBufferSize_); |
| 650 | |
| 651 | // If the function call generated return data, then move into the send |
| 652 | // state and get going |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 653 | // 4 bytes were reserved for frame size |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 654 | if (writeBufferSize_ > 4) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 655 | |
| 656 | // Move into write state |
| 657 | writeBufferPos_ = 0; |
| 658 | socketState_ = SOCKET_SEND; |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 659 | |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 660 | // Put the frame size into the write buffer |
| 661 | int32_t frameSize = (int32_t)htonl(writeBufferSize_ - 4); |
| 662 | memcpy(writeBuffer_, &frameSize, 4); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 663 | |
| 664 | // Socket into write mode |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 665 | appState_ = APP_SEND_RESULT; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 666 | setWrite(); |
| 667 | |
| 668 | // Try to work the socket immediately |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 669 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 670 | |
| 671 | return; |
| 672 | } |
| 673 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 674 | // In this case, the request was oneway and we should fall through |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 675 | // right back into the read frame header state |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 676 | goto LABEL_APP_INIT; |
| 677 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 678 | case APP_SEND_RESULT: |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 679 | // it's now safe to perform buffer size housekeeping. |
| 680 | if (writeBufferSize_ > largestWriteBufferSize_) { |
| 681 | largestWriteBufferSize_ = writeBufferSize_; |
| 682 | } |
| 683 | if (server_->getResizeBufferEveryN() > 0 |
| 684 | && ++callsForResize_ >= server_->getResizeBufferEveryN()) { |
| 685 | checkIdleBufferMemLimit(server_->getIdleReadBufferLimit(), |
| 686 | server_->getIdleWriteBufferLimit()); |
| 687 | callsForResize_ = 0; |
| 688 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 689 | |
| 690 | // N.B.: We also intentionally fall through here into the INIT state! |
| 691 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 692 | LABEL_APP_INIT: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 693 | case APP_INIT: |
| 694 | |
| 695 | // Clear write buffer variables |
| 696 | writeBuffer_ = NULL; |
| 697 | writeBufferPos_ = 0; |
| 698 | writeBufferSize_ = 0; |
| 699 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 700 | // Into read4 state we go |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 701 | socketState_ = SOCKET_RECV_FRAMING; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 702 | appState_ = APP_READ_FRAME_SIZE; |
| 703 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 704 | readBufferPos_ = 0; |
| 705 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 706 | // Register read event |
| 707 | setRead(); |
David Reiss | 84e63ab | 2008-03-07 20:12:28 +0000 | [diff] [blame] | 708 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 709 | // Try to work the socket right away |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 710 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 711 | |
| 712 | return; |
| 713 | |
| 714 | case APP_READ_FRAME_SIZE: |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 715 | // We just read the request length |
| 716 | // Double the buffer size until it is big enough |
| 717 | if (readWant_ > readBufferSize_) { |
| 718 | if (readBufferSize_ == 0) { |
| 719 | readBufferSize_ = 1; |
| 720 | } |
| 721 | uint32_t newSize = readBufferSize_; |
| 722 | while (readWant_ > newSize) { |
| 723 | newSize *= 2; |
| 724 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 725 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 726 | uint8_t* newBuffer = (uint8_t*)std::realloc(readBuffer_, newSize); |
| 727 | if (newBuffer == NULL) { |
| 728 | // nothing else to be done... |
| 729 | throw std::bad_alloc(); |
| 730 | } |
| 731 | readBuffer_ = newBuffer; |
| 732 | readBufferSize_ = newSize; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 735 | readBufferPos_= 0; |
| 736 | |
| 737 | // Move into read request state |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 738 | socketState_ = SOCKET_RECV; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 739 | appState_ = APP_READ_REQUEST; |
| 740 | |
| 741 | // Work the socket right away |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 742 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 743 | |
| 744 | return; |
| 745 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 746 | case APP_CLOSE_CONNECTION: |
| 747 | server_->decrementActiveProcessors(); |
| 748 | close(); |
| 749 | return; |
| 750 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 751 | default: |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 752 | GlobalOutput.printf("Unexpected Application State %d", appState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 753 | assert(0); |
| 754 | } |
| 755 | } |
| 756 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 757 | void TNonblockingServer::TConnection::setFlags(short eventFlags) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 758 | // Catch the do nothing case |
| 759 | if (eventFlags_ == eventFlags) { |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | // Delete a previously existing event |
| 764 | if (eventFlags_ != 0) { |
| 765 | if (event_del(&event_) == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 766 | GlobalOutput("TConnection::setFlags event_del"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | // Update in memory structure |
| 772 | eventFlags_ = eventFlags; |
| 773 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 774 | // Do not call event_set if there are no flags |
| 775 | if (!eventFlags_) { |
| 776 | return; |
| 777 | } |
| 778 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 779 | /* |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 780 | * event_set: |
| 781 | * |
| 782 | * Prepares the event structure &event to be used in future calls to |
| 783 | * event_add() and event_del(). The event will be prepared to call the |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 784 | * eventHandler using the 'sock' file descriptor to monitor events. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 785 | * |
| 786 | * The events can be either EV_READ, EV_WRITE, or both, indicating |
| 787 | * that an application can read or write from the file respectively without |
| 788 | * blocking. |
| 789 | * |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 790 | * The eventHandler will be called with the file descriptor that triggered |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 791 | * the event and the type of event which will be one of: EV_TIMEOUT, |
| 792 | * EV_SIGNAL, EV_READ, EV_WRITE. |
| 793 | * |
| 794 | * The additional flag EV_PERSIST makes an event_add() persistent until |
| 795 | * event_del() has been called. |
| 796 | * |
| 797 | * Once initialized, the &event struct can be used repeatedly with |
| 798 | * event_add() and event_del() and does not need to be reinitialized unless |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 799 | * the eventHandler and/or the argument to it are to be changed. However, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 800 | * when an ev structure has been added to libevent using event_add() the |
| 801 | * structure must persist until the event occurs (assuming EV_PERSIST |
| 802 | * is not set) or is removed using event_del(). You may not reuse the same |
| 803 | * ev structure for multiple monitored descriptors; each descriptor needs |
| 804 | * its own ev. |
| 805 | */ |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 806 | event_set(&event_, tSocket_->getSocketFD(), eventFlags_, |
| 807 | TConnection::eventHandler, this); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 808 | event_base_set(ioThread_->getEventBase(), &event_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 809 | |
| 810 | // Add the event |
| 811 | if (event_add(&event_, 0) == -1) { |
Mark Slee | 17496a0 | 2007-08-02 06:37:40 +0000 | [diff] [blame] | 812 | GlobalOutput("TConnection::setFlags(): could not event_add"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Closes a connection |
| 818 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 819 | void TNonblockingServer::TConnection::close() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 820 | // Delete the registered libevent |
| 821 | if (event_del(&event_) == -1) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 822 | GlobalOutput.perror("TConnection::close() event_del", THRIFT_GET_SOCKET_ERROR); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 825 | if (serverEventHandler_) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 826 | serverEventHandler_->deleteContext(connectionContext_, inputProtocol_, outputProtocol_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 827 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 828 | ioThread_ = NULL; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 829 | |
| 830 | // Close the socket |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 831 | tSocket_->close(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 832 | |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 833 | // close any factory produced transports |
| 834 | factoryInputTransport_->close(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 835 | factoryOutputTransport_->close(); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 836 | |
Roger Meier | 464a3a4 | 2014-07-07 21:48:28 +0200 | [diff] [blame] | 837 | // release processor and handler |
| 838 | processor_.reset(); |
| 839 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 840 | // Give this object back to the server that owns it |
| 841 | server_->returnConnection(this); |
| 842 | } |
| 843 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 844 | void TNonblockingServer::TConnection::checkIdleBufferMemLimit( |
| 845 | size_t readLimit, |
| 846 | size_t writeLimit) { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 847 | if (readLimit > 0 && readBufferSize_ > readLimit) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 848 | free(readBuffer_); |
| 849 | readBuffer_ = NULL; |
| 850 | readBufferSize_ = 0; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 851 | } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 852 | |
| 853 | if (writeLimit > 0 && largestWriteBufferSize_ > writeLimit) { |
| 854 | // just start over |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 855 | outputTransport_->resetBuffer(static_cast<uint32_t>(server_->getWriteBufferDefaultSize())); |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 856 | largestWriteBufferSize_ = 0; |
| 857 | } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 858 | } |
| 859 | |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 860 | TNonblockingServer::~TNonblockingServer() { |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 861 | // Close any active connections (moves them to the idle connection stack) |
| 862 | while (activeConnections_.size()) { |
| 863 | activeConnections_.front()->close(); |
| 864 | } |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 865 | // Clean up unused TConnection objects in connectionStack_ |
| 866 | while (!connectionStack_.empty()) { |
| 867 | TConnection* connection = connectionStack_.top(); |
| 868 | connectionStack_.pop(); |
| 869 | delete connection; |
| 870 | } |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 871 | // The TNonblockingIOThread objects have shared_ptrs to the Thread |
| 872 | // objects and the Thread objects have shared_ptrs to the TNonblockingIOThread |
| 873 | // objects (as runnable) so these objects will never deallocate without help. |
| 874 | while (!ioThreads_.empty()) { |
| 875 | boost::shared_ptr<TNonblockingIOThread> iot = ioThreads_.back(); |
| 876 | ioThreads_.pop_back(); |
| 877 | iot->setThread(boost::shared_ptr<Thread>()); |
| 878 | } |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 881 | /** |
| 882 | * Creates a new connection either by reusing an object off the stack or |
| 883 | * by allocating a new one entirely |
| 884 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 885 | TNonblockingServer::TConnection* TNonblockingServer::createConnection( |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 886 | THRIFT_SOCKET socket, const sockaddr* addr, socklen_t addrLen) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 887 | // Check the stack |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 888 | Guard g(connMutex_); |
| 889 | |
| 890 | // pick an IO thread to handle this connection -- currently round robin |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 891 | assert(nextIOThread_ < ioThreads_.size()); |
| 892 | int selectedThreadIdx = nextIOThread_; |
Ben Craig | 6493523 | 2013-10-09 15:21:38 -0500 | [diff] [blame] | 893 | nextIOThread_ = static_cast<uint32_t>((nextIOThread_ + 1) % ioThreads_.size()); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 894 | |
| 895 | TNonblockingIOThread* ioThread = ioThreads_[selectedThreadIdx].get(); |
| 896 | |
| 897 | // Check the connection stack to see if we can re-use |
| 898 | TConnection* result = NULL; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 899 | if (connectionStack_.empty()) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 900 | result = new TConnection(socket, ioThread, addr, addrLen); |
| 901 | ++numTConnections_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 902 | } else { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 903 | result = connectionStack_.top(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 904 | connectionStack_.pop(); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 905 | result->init(socket, ioThread, addr, addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 906 | } |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 907 | activeConnections_.push_back(result); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 908 | return result; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Returns a connection to the stack |
| 913 | */ |
| 914 | void TNonblockingServer::returnConnection(TConnection* connection) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 915 | Guard g(connMutex_); |
| 916 | |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 917 | activeConnections_.erase(std::remove(activeConnections_.begin(), activeConnections_.end(), connection), activeConnections_.end()); |
| 918 | |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 919 | if (connectionStackLimit_ && |
| 920 | (connectionStack_.size() >= connectionStackLimit_)) { |
| 921 | delete connection; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 922 | --numTConnections_; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 923 | } else { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 924 | connection->checkIdleBufferMemLimit(idleReadBufferLimit_, idleWriteBufferLimit_); |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 925 | connectionStack_.push(connection); |
| 926 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /** |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 930 | * Server socket had something happen. We accept all waiting client |
| 931 | * connections on fd and assign TConnection objects to handle those requests. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 932 | */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 933 | void TNonblockingServer::handleEvent(THRIFT_SOCKET fd, short which) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 934 | (void) which; |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 935 | // Make sure that libevent didn't mess up the socket handles |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 936 | assert(fd == serverSocket_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 937 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 938 | // Server socket accepted a new connection |
| 939 | socklen_t addrLen; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 940 | sockaddr_storage addrStorage; |
| 941 | sockaddr* addrp = (sockaddr*)&addrStorage; |
| 942 | addrLen = sizeof(addrStorage); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 943 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 944 | // Going to accept a new client socket |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 945 | THRIFT_SOCKET clientSocket; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 946 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 947 | // Accept as many new clients as possible, even though libevent signaled only |
| 948 | // one, this helps us to avoid having to go back into the libevent engine so |
| 949 | // many times |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 950 | while ((clientSocket = ::accept(fd, addrp, &addrLen)) != -1) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 951 | // If we're overloaded, take action here |
| 952 | if (overloadAction_ != T_OVERLOAD_NO_ACTION && serverOverloaded()) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 953 | Guard g(connMutex_); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 954 | nConnectionsDropped_++; |
| 955 | nTotalConnectionsDropped_++; |
| 956 | if (overloadAction_ == T_OVERLOAD_CLOSE_ON_ACCEPT) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 957 | ::THRIFT_CLOSESOCKET(clientSocket); |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 958 | return; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 959 | } else if (overloadAction_ == T_OVERLOAD_DRAIN_TASK_QUEUE) { |
| 960 | if (!drainPendingTask()) { |
| 961 | // Nothing left to discard, so we drop connection instead. |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 962 | ::THRIFT_CLOSESOCKET(clientSocket); |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 963 | return; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 967 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 968 | // Explicitly set this socket to NONBLOCK mode |
| 969 | int flags; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 970 | if ((flags = THRIFT_FCNTL(clientSocket, THRIFT_F_GETFL, 0)) < 0 || |
| 971 | THRIFT_FCNTL(clientSocket, THRIFT_F_SETFL, flags | THRIFT_O_NONBLOCK) < 0) { |
| 972 | GlobalOutput.perror("thriftServerEventHandler: set THRIFT_O_NONBLOCK (THRIFT_FCNTL) ", THRIFT_GET_SOCKET_ERROR); |
| 973 | ::THRIFT_CLOSESOCKET(clientSocket); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | |
| 977 | // Create a new TConnection for this client socket. |
| 978 | TConnection* clientConnection = |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 979 | createConnection(clientSocket, addrp, addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 980 | |
| 981 | // Fail fast if we could not create a TConnection object |
| 982 | if (clientConnection == NULL) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 983 | GlobalOutput.printf("thriftServerEventHandler: failed TConnection factory"); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 984 | ::THRIFT_CLOSESOCKET(clientSocket); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 985 | return; |
| 986 | } |
| 987 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 988 | /* |
| 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 { |
| 1003 | clientConnection->notifyIOThread(); |
| 1004 | } |
David Reiss | 3e7fca4 | 2009-09-19 01:59:13 +0000 | [diff] [blame] | 1005 | |
| 1006 | // addrLen is written by the accept() call, so needs to be set before the next call. |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 1007 | addrLen = sizeof(addrStorage); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1008 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1009 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1010 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1011 | // Done looping accept, now we have to make sure the error is due to |
| 1012 | // blocking. Any other error is a problem |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1013 | if (THRIFT_GET_SOCKET_ERROR != THRIFT_EAGAIN && THRIFT_GET_SOCKET_ERROR != THRIFT_EWOULDBLOCK) { |
| 1014 | GlobalOutput.perror("thriftServerEventHandler: accept() ", THRIFT_GET_SOCKET_ERROR); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /** |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1019 | * Creates a socket to listen on and binds it to the local port. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1020 | */ |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1021 | void TNonblockingServer::createAndListenOnSocket() { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1022 | THRIFT_SOCKET s; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1023 | |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 1024 | struct addrinfo hints, *res, *res0; |
| 1025 | int error; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1026 | |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 1027 | char port[sizeof("65536") + 1]; |
| 1028 | memset(&hints, 0, sizeof(hints)); |
| 1029 | hints.ai_family = PF_UNSPEC; |
| 1030 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 1031 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 1032 | sprintf(port, "%d", port_); |
| 1033 | |
| 1034 | // Wildcard address |
| 1035 | error = getaddrinfo(NULL, port, &hints, &res0); |
| 1036 | if (error) { |
Roger Meier | d8f50f3 | 2012-04-11 21:48:56 +0000 | [diff] [blame] | 1037 | throw TException("TNonblockingServer::serve() getaddrinfo " + |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1038 | string(THRIFT_GAI_STRERROR(error))); |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // Pick the ipv6 address first since ipv4 addresses can be mapped |
| 1042 | // into ipv6 space. |
| 1043 | for (res = res0; res; res = res->ai_next) { |
| 1044 | if (res->ai_family == AF_INET6 || res->ai_next == NULL) |
| 1045 | break; |
| 1046 | } |
| 1047 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1048 | // Create the server socket |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1049 | s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 1050 | if (s == -1) { |
| 1051 | freeaddrinfo(res0); |
| 1052 | throw TException("TNonblockingServer::serve() socket() -1"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 1055 | #ifdef IPV6_V6ONLY |
David Reiss | eee98be | 2010-03-09 05:20:10 +0000 | [diff] [blame] | 1056 | if (res->ai_family == AF_INET6) { |
| 1057 | int zero = 0; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1058 | if (-1 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, const_cast_sockopt(&zero), sizeof(zero))) { |
David Reiss | eee98be | 2010-03-09 05:20:10 +0000 | [diff] [blame] | 1059 | GlobalOutput("TServerSocket::listen() IPV6_V6ONLY"); |
| 1060 | } |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 1061 | } |
| 1062 | #endif // #ifdef IPV6_V6ONLY |
| 1063 | |
| 1064 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1065 | int one = 1; |
| 1066 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1067 | // Set THRIFT_NO_SOCKET_CACHING to avoid 2MSL delay on server restart |
| 1068 | setsockopt(s, SOL_SOCKET, THRIFT_NO_SOCKET_CACHING, const_cast_sockopt(&one), sizeof(one)); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1069 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1070 | if (::bind(s, res->ai_addr, static_cast<int>(res->ai_addrlen)) == -1) { |
| 1071 | ::THRIFT_CLOSESOCKET(s); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1072 | freeaddrinfo(res0); |
Roger Meier | d8f50f3 | 2012-04-11 21:48:56 +0000 | [diff] [blame] | 1073 | throw TTransportException(TTransportException::NOT_OPEN, |
| 1074 | "TNonblockingServer::serve() bind", |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1075 | THRIFT_GET_SOCKET_ERROR); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | // Done with the addr info |
| 1079 | freeaddrinfo(res0); |
| 1080 | |
| 1081 | // Set up this file descriptor for listening |
| 1082 | listenSocket(s); |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * Takes a socket created by listenSocket() and sets various options on it |
| 1087 | * to prepare for use in the server. |
| 1088 | */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1089 | void TNonblockingServer::listenSocket(THRIFT_SOCKET s) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1090 | // Set socket to nonblocking mode |
| 1091 | int flags; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1092 | if ((flags = THRIFT_FCNTL(s, THRIFT_F_GETFL, 0)) < 0 || |
| 1093 | THRIFT_FCNTL(s, THRIFT_F_SETFL, flags | THRIFT_O_NONBLOCK) < 0) { |
| 1094 | ::THRIFT_CLOSESOCKET(s); |
| 1095 | throw TException("TNonblockingServer::serve() THRIFT_O_NONBLOCK"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | int one = 1; |
| 1099 | struct linger ling = {0, 0}; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1100 | |
| 1101 | // Keepalive to ensure full result flushing |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1102 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, const_cast_sockopt(&one), sizeof(one)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Turn linger off to avoid hung sockets |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1105 | setsockopt(s, SOL_SOCKET, SO_LINGER, const_cast_sockopt(&ling), sizeof(ling)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Set TCP nodelay if available, MAC OS X Hack |
| 1108 | // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html |
| 1109 | #ifndef TCP_NOPUSH |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1110 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, const_cast_sockopt(&one), sizeof(one)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1111 | #endif |
| 1112 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 1113 | #ifdef TCP_LOW_MIN_RTO |
| 1114 | if (TSocket::getUseLowMinRto()) { |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1115 | setsockopt(s, IPPROTO_TCP, TCP_LOW_MIN_RTO, const_cast_sockopt(&one), sizeof(one)); |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 1116 | } |
| 1117 | #endif |
| 1118 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1119 | if (listen(s, LISTEN_BACKLOG) == -1) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1120 | ::THRIFT_CLOSESOCKET(s); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1121 | throw TException("TNonblockingServer::serve() listen"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1124 | // Cool, this socket is good to go, set it as the serverSocket_ |
| 1125 | serverSocket_ = s; |
| 1126 | } |
| 1127 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1128 | void TNonblockingServer::setThreadManager(boost::shared_ptr<ThreadManager> threadManager) { |
| 1129 | threadManager_ = threadManager; |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 1130 | if (threadManager) { |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 1131 | threadManager->setExpireCallback(apache::thrift::stdcxx::bind(&TNonblockingServer::expireClose, this, apache::thrift::stdcxx::placeholders::_1)); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1132 | threadPoolProcessing_ = true; |
| 1133 | } else { |
| 1134 | threadPoolProcessing_ = false; |
| 1135 | } |
| 1136 | } |
| 1137 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1138 | bool TNonblockingServer::serverOverloaded() { |
| 1139 | size_t activeConnections = numTConnections_ - connectionStack_.size(); |
| 1140 | if (numActiveProcessors_ > maxActiveProcessors_ || |
| 1141 | activeConnections > maxConnections_) { |
| 1142 | if (!overloaded_) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1143 | GlobalOutput.printf("TNonblockingServer: overload condition begun."); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1144 | overloaded_ = true; |
| 1145 | } |
| 1146 | } else { |
| 1147 | if (overloaded_ && |
| 1148 | (numActiveProcessors_ <= overloadHysteresis_ * maxActiveProcessors_) && |
| 1149 | (activeConnections <= overloadHysteresis_ * maxConnections_)) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1150 | GlobalOutput.printf("TNonblockingServer: overload ended; " |
| 1151 | "%u dropped (%llu total)", |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1152 | nConnectionsDropped_, nTotalConnectionsDropped_); |
| 1153 | nConnectionsDropped_ = 0; |
| 1154 | overloaded_ = false; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | return overloaded_; |
| 1159 | } |
| 1160 | |
| 1161 | bool TNonblockingServer::drainPendingTask() { |
| 1162 | if (threadManager_) { |
| 1163 | boost::shared_ptr<Runnable> task = threadManager_->removeNextPending(); |
| 1164 | if (task) { |
| 1165 | TConnection* connection = |
| 1166 | static_cast<TConnection::Task*>(task.get())->getTConnection(); |
| 1167 | assert(connection && connection->getServer() |
| 1168 | && connection->getState() == APP_WAIT_TASK); |
| 1169 | connection->forceClose(); |
| 1170 | return true; |
| 1171 | } |
| 1172 | } |
| 1173 | return false; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1176 | void TNonblockingServer::expireClose(boost::shared_ptr<Runnable> task) { |
| 1177 | TConnection* connection = |
| 1178 | static_cast<TConnection::Task*>(task.get())->getTConnection(); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1179 | assert(connection && connection->getServer() && |
| 1180 | connection->getState() == APP_WAIT_TASK); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1181 | connection->forceClose(); |
| 1182 | } |
| 1183 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1184 | void TNonblockingServer::stop() { |
| 1185 | // Breaks the event loop in all threads so that they end ASAP. |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1186 | for (uint32_t i = 0; i < ioThreads_.size(); ++i) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1187 | ioThreads_[i]->stop(); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1191 | void TNonblockingServer::registerEvents(event_base* user_event_base) { |
| 1192 | userEventBase_ = user_event_base; |
| 1193 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1194 | // init listen socket |
Roger Meier | e802aa4 | 2013-07-19 21:10:54 +0200 | [diff] [blame] | 1195 | if (serverSocket_ == THRIFT_INVALID_SOCKET) |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1196 | createAndListenOnSocket(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1197 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1198 | // set up the IO threads |
| 1199 | assert(ioThreads_.empty()); |
| 1200 | if (!numIOThreads_) { |
| 1201 | numIOThreads_ = DEFAULT_IO_THREADS; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1204 | for (uint32_t id = 0; id < numIOThreads_; ++id) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1205 | // the first IO thread also does the listening on server socket |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 1206 | THRIFT_SOCKET listenFd = (id == 0 ? serverSocket_ : THRIFT_INVALID_SOCKET); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1207 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1208 | shared_ptr<TNonblockingIOThread> thread( |
| 1209 | new TNonblockingIOThread(this, id, listenFd, useHighPriorityIOThreads_)); |
| 1210 | ioThreads_.push_back(thread); |
| 1211 | } |
| 1212 | |
| 1213 | // Notify handler of the preServe event |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 1214 | if (eventHandler_) { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 1215 | eventHandler_->preServe(); |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1218 | // Start all of our helper IO threads. Note that the threads run forever, |
| 1219 | // only terminating if stop() is called. |
| 1220 | assert(ioThreads_.size() == numIOThreads_); |
| 1221 | assert(ioThreads_.size() > 0); |
| 1222 | |
| 1223 | GlobalOutput.printf("TNonblockingServer: Serving on port %d, %d io threads.", |
| 1224 | port_, ioThreads_.size()); |
| 1225 | |
| 1226 | // Launch all the secondary IO threads in separate threads |
| 1227 | if (ioThreads_.size() > 1) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1228 | ioThreadFactory_.reset(new PlatformThreadFactory( |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1229 | #if !defined(USE_BOOST_THREAD) && !defined(USE_STD_THREAD) |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1230 | PlatformThreadFactory::OTHER, // scheduler |
| 1231 | PlatformThreadFactory::NORMAL, // priority |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1232 | 1, // stack size (MB) |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1233 | #endif |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1234 | false // detached |
| 1235 | )); |
| 1236 | |
| 1237 | assert(ioThreadFactory_.get()); |
| 1238 | |
| 1239 | // intentionally starting at thread 1, not 0 |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1240 | for (uint32_t i = 1; i < ioThreads_.size(); ++i) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1241 | shared_ptr<Thread> thread = ioThreadFactory_->newThread(ioThreads_[i]); |
| 1242 | ioThreads_[i]->setThread(thread); |
| 1243 | thread->start(); |
| 1244 | } |
| 1245 | } |
| 1246 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1247 | // Register the events for the primary (listener) IO thread |
| 1248 | ioThreads_[0]->registerEvents(); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * Main workhorse function, starts up the server listening on a port and |
| 1253 | * loops over the libevent handler. |
| 1254 | */ |
| 1255 | void TNonblockingServer::serve() { |
| 1256 | |
| 1257 | registerEvents(NULL); |
| 1258 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1259 | // Run the primary (listener) IO thread loop in our main thread; this will |
| 1260 | // only return when the server is shutting down. |
| 1261 | ioThreads_[0]->run(); |
| 1262 | |
| 1263 | // Ensure all threads are finished before exiting serve() |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1264 | for (uint32_t i = 0; i < ioThreads_.size(); ++i) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1265 | ioThreads_[i]->join(); |
| 1266 | GlobalOutput.printf("TNonblocking: join done for IO thread #%d", i); |
| 1267 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1270 | TNonblockingIOThread::TNonblockingIOThread(TNonblockingServer* server, |
| 1271 | int number, |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1272 | THRIFT_SOCKET listenSocket, |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1273 | bool useHighPriority) |
| 1274 | : server_(server) |
| 1275 | , number_(number) |
| 1276 | , listenSocket_(listenSocket) |
| 1277 | , useHighPriority_(useHighPriority) |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1278 | , eventBase_(NULL) |
| 1279 | , ownEventBase_(false) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1280 | notificationPipeFDs_[0] = -1; |
| 1281 | notificationPipeFDs_[1] = -1; |
| 1282 | } |
| 1283 | |
| 1284 | TNonblockingIOThread::~TNonblockingIOThread() { |
| 1285 | // make sure our associated thread is fully finished |
| 1286 | join(); |
| 1287 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1288 | if (eventBase_ && ownEventBase_) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1289 | event_base_free(eventBase_); |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1290 | ownEventBase_ = false; |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1293 | if (listenSocket_ >= 0) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1294 | if (0 != ::THRIFT_CLOSESOCKET(listenSocket_)) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1295 | GlobalOutput.perror("TNonblockingIOThread listenSocket_ close(): ", |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1296 | THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1297 | } |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 1298 | listenSocket_ = THRIFT_INVALID_SOCKET; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | for (int i = 0; i < 2; ++i) { |
| 1302 | if (notificationPipeFDs_[i] >= 0) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1303 | if (0 != ::THRIFT_CLOSESOCKET(notificationPipeFDs_[i])) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1304 | GlobalOutput.perror("TNonblockingIOThread notificationPipe close(): ", |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1305 | THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1306 | } |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 1307 | notificationPipeFDs_[i] = THRIFT_INVALID_SOCKET; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | void TNonblockingIOThread::createNotificationPipe() { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1313 | if(evutil_socketpair(AF_LOCAL, SOCK_STREAM, 0, notificationPipeFDs_) == -1) { |
| 1314 | GlobalOutput.perror("TNonblockingServer::createNotificationPipe ", EVUTIL_SOCKET_ERROR()); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1315 | throw TException("can't create notification pipe"); |
| 1316 | } |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1317 | if(evutil_make_socket_nonblocking(notificationPipeFDs_[0])<0 || |
| 1318 | evutil_make_socket_nonblocking(notificationPipeFDs_[1])<0) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1319 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]); |
| 1320 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]); |
| 1321 | throw TException("TNonblockingServer::createNotificationPipe() THRIFT_O_NONBLOCK"); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1322 | } |
| 1323 | for (int i = 0; i < 2; ++i) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1324 | #if LIBEVENT_VERSION_NUMBER < 0x02000000 |
| 1325 | int flags; |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1326 | if ((flags = THRIFT_FCNTL(notificationPipeFDs_[i], F_GETFD, 0)) < 0 || |
| 1327 | THRIFT_FCNTL(notificationPipeFDs_[i], F_SETFD, flags | FD_CLOEXEC) < 0) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1328 | #else |
| 1329 | if (evutil_make_socket_closeonexec(notificationPipeFDs_[i]) < 0) { |
| 1330 | #endif |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1331 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]); |
| 1332 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1333 | throw TException("TNonblockingServer::createNotificationPipe() " |
| 1334 | "FD_CLOEXEC"); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /** |
| 1340 | * Register the core libevent events onto the proper base. |
| 1341 | */ |
| 1342 | void TNonblockingIOThread::registerEvents() { |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1343 | threadId_ = Thread::get_current(); |
| 1344 | |
| 1345 | assert(eventBase_ == 0); |
| 1346 | eventBase_ = getServer()->getUserEventBase(); |
| 1347 | if (eventBase_ == NULL) { |
| 1348 | eventBase_ = event_base_new(); |
| 1349 | ownEventBase_ = true; |
| 1350 | } |
| 1351 | |
| 1352 | // Print some libevent stats |
| 1353 | if (number_ == 0) { |
| 1354 | GlobalOutput.printf("TNonblockingServer: using libevent %s method %s", |
| 1355 | event_get_version(), |
| 1356 | event_base_get_method(eventBase_)); |
| 1357 | } |
| 1358 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1359 | if (listenSocket_ >= 0) { |
| 1360 | // Register the server event |
| 1361 | event_set(&serverEvent_, |
| 1362 | listenSocket_, |
| 1363 | EV_READ | EV_PERSIST, |
| 1364 | TNonblockingIOThread::listenHandler, |
| 1365 | server_); |
| 1366 | event_base_set(eventBase_, &serverEvent_); |
| 1367 | |
| 1368 | // Add the event and start up the server |
| 1369 | if (-1 == event_add(&serverEvent_, 0)) { |
| 1370 | throw TException("TNonblockingServer::serve(): " |
| 1371 | "event_add() failed on server listen event"); |
| 1372 | } |
| 1373 | GlobalOutput.printf("TNonblocking: IO thread #%d registered for listen.", |
| 1374 | number_); |
| 1375 | } |
| 1376 | |
| 1377 | createNotificationPipe(); |
| 1378 | |
| 1379 | // Create an event to be notified when a task finishes |
| 1380 | event_set(¬ificationEvent_, |
| 1381 | getNotificationRecvFD(), |
| 1382 | EV_READ | EV_PERSIST, |
| 1383 | TNonblockingIOThread::notifyHandler, |
| 1384 | this); |
| 1385 | |
| 1386 | // Attach to the base |
| 1387 | event_base_set(eventBase_, ¬ificationEvent_); |
| 1388 | |
| 1389 | // Add the event and start up the server |
| 1390 | if (-1 == event_add(¬ificationEvent_, 0)) { |
| 1391 | throw TException("TNonblockingServer::serve(): " |
| 1392 | "event_add() failed on task-done notification event"); |
| 1393 | } |
| 1394 | GlobalOutput.printf("TNonblocking: IO thread #%d registered for notify.", |
| 1395 | number_); |
| 1396 | } |
| 1397 | |
| 1398 | bool TNonblockingIOThread::notify(TNonblockingServer::TConnection* conn) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1399 | THRIFT_SOCKET fd = getNotificationSendFD(); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1400 | if (fd < 0) { |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
| 1404 | const int kSize = sizeof(conn); |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1405 | if (send(fd, const_cast_sockopt(&conn), kSize, 0) != kSize) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1406 | return false; |
| 1407 | } |
| 1408 | |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
| 1412 | /* static */ |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1413 | void TNonblockingIOThread::notifyHandler(evutil_socket_t fd, short which, void* v) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1414 | TNonblockingIOThread* ioThread = (TNonblockingIOThread*) v; |
| 1415 | assert(ioThread); |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1416 | (void)which; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1417 | |
| 1418 | while (true) { |
| 1419 | TNonblockingServer::TConnection* connection = 0; |
| 1420 | const int kSize = sizeof(connection); |
Ben Craig | 6493523 | 2013-10-09 15:21:38 -0500 | [diff] [blame] | 1421 | long nBytes = recv(fd, cast_sockopt(&connection), kSize, 0); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1422 | if (nBytes == kSize) { |
| 1423 | if (connection == NULL) { |
| 1424 | // this is the command to stop our thread, exit the handler! |
| 1425 | return; |
| 1426 | } |
| 1427 | connection->transition(); |
| 1428 | } else if (nBytes > 0) { |
| 1429 | // throw away these bytes and hope that next time we get a solid read |
| 1430 | GlobalOutput.printf("notifyHandler: Bad read of %d bytes, wanted %d", |
| 1431 | nBytes, kSize); |
| 1432 | ioThread->breakLoop(true); |
| 1433 | return; |
| 1434 | } else if (nBytes == 0) { |
| 1435 | GlobalOutput.printf("notifyHandler: Notify socket closed!"); |
| 1436 | // exit the loop |
| 1437 | break; |
| 1438 | } else { // nBytes < 0 |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1439 | if (THRIFT_GET_SOCKET_ERROR != THRIFT_EWOULDBLOCK && THRIFT_GET_SOCKET_ERROR != THRIFT_EAGAIN) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1440 | GlobalOutput.perror( |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1441 | "TNonblocking: notifyHandler read() failed: ", THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1442 | ioThread->breakLoop(true); |
| 1443 | return; |
| 1444 | } |
| 1445 | // exit the loop |
| 1446 | break; |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | void TNonblockingIOThread::breakLoop(bool error) { |
| 1452 | if (error) { |
| 1453 | GlobalOutput.printf( |
| 1454 | "TNonblockingServer: IO thread #%d exiting with error.", number_); |
| 1455 | // TODO: figure out something better to do here, but for now kill the |
| 1456 | // whole process. |
| 1457 | GlobalOutput.printf("TNonblockingServer: aborting process."); |
| 1458 | ::abort(); |
| 1459 | } |
| 1460 | |
| 1461 | // sets a flag so that the loop exits on the next event |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1462 | event_base_loopbreak(eventBase_); |
| 1463 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1464 | // event_base_loopbreak() only causes the loop to exit the next time |
| 1465 | // it wakes up. We need to force it to wake up, in case there are |
| 1466 | // no real events it needs to process. |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1467 | // |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1468 | // If we're running in the same thread, we can't use the notify(0) |
| 1469 | // mechanism to stop the thread, but happily if we're running in the |
| 1470 | // same thread, this means the thread can't be blocking in the event |
| 1471 | // loop either. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1472 | if (!Thread::is_current(threadId_)) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1473 | notify(NULL); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | void TNonblockingIOThread::setCurrentThreadHighPriority(bool value) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1478 | #ifdef HAVE_SCHED_H |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1479 | // Start out with a standard, low-priority setup for the sched params. |
| 1480 | struct sched_param sp; |
| 1481 | bzero((void*) &sp, sizeof(sp)); |
| 1482 | int policy = SCHED_OTHER; |
| 1483 | |
| 1484 | // If desired, set up high-priority sched params structure. |
| 1485 | if (value) { |
| 1486 | // FIFO scheduler, ranked above default SCHED_OTHER queue |
| 1487 | policy = SCHED_FIFO; |
| 1488 | // The priority only compares us to other SCHED_FIFO threads, so we |
| 1489 | // just pick a random priority halfway between min & max. |
| 1490 | const int priority = (sched_get_priority_max(policy) + |
| 1491 | sched_get_priority_min(policy)) / 2; |
| 1492 | |
| 1493 | sp.sched_priority = priority; |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1496 | // Actually set the sched params for the current thread. |
| 1497 | if (0 == pthread_setschedparam(pthread_self(), policy, &sp)) { |
| 1498 | GlobalOutput.printf( |
| 1499 | "TNonblocking: IO Thread #%d using high-priority scheduler!", number_); |
| 1500 | } else { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1501 | GlobalOutput.perror("TNonblocking: pthread_setschedparam(): ", THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1502 | } |
Roger Meier | d051ca0 | 2013-08-15 01:35:11 +0200 | [diff] [blame] | 1503 | #else |
| 1504 | THRIFT_UNUSED_VARIABLE(value); |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1505 | #endif |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1506 | } |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1507 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1508 | void TNonblockingIOThread::run() { |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1509 | if (eventBase_ == NULL) |
| 1510 | registerEvents(); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1511 | |
| 1512 | GlobalOutput.printf("TNonblockingServer: IO thread #%d entering loop...", |
| 1513 | number_); |
| 1514 | |
| 1515 | if (useHighPriority_) { |
| 1516 | setCurrentThreadHighPriority(true); |
| 1517 | } |
| 1518 | |
| 1519 | // Run libevent engine, never returns, invokes calls to eventHandler |
| 1520 | event_base_loop(eventBase_, 0); |
| 1521 | |
| 1522 | if (useHighPriority_) { |
| 1523 | setCurrentThreadHighPriority(false); |
| 1524 | } |
| 1525 | |
| 1526 | // cleans up our registered events |
| 1527 | cleanupEvents(); |
| 1528 | |
| 1529 | GlobalOutput.printf("TNonblockingServer: IO thread #%d run() done!", |
| 1530 | number_); |
| 1531 | } |
| 1532 | |
| 1533 | void TNonblockingIOThread::cleanupEvents() { |
| 1534 | // stop the listen socket, if any |
| 1535 | if (listenSocket_ >= 0) { |
| 1536 | if (event_del(&serverEvent_) == -1) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1537 | GlobalOutput.perror("TNonblockingIOThread::stop() event_del: ", THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | event_del(¬ificationEvent_); |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | void TNonblockingIOThread::stop() { |
| 1546 | // This should cause the thread to fall out of its event loop ASAP. |
| 1547 | breakLoop(false); |
| 1548 | } |
| 1549 | |
| 1550 | void TNonblockingIOThread::join() { |
| 1551 | // If this was a thread created by a factory (not the thread that called |
| 1552 | // serve()), we join() it to make sure we shut down fully. |
| 1553 | if (thread_) { |
| 1554 | try { |
| 1555 | // Note that it is safe to both join() ourselves twice, as well as join |
| 1556 | // the current thread as the pthread implementation checks for deadlock. |
| 1557 | thread_->join(); |
| 1558 | } catch(...) { |
| 1559 | // swallow everything |
| 1560 | } |
| 1561 | } |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 1564 | }}} // apache::thrift::server |