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