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 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 28 | #include <algorithm> |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 29 | #include <iostream> |
Lei Feiwei | b5ebcd1 | 2015-04-04 22:12:07 +0800 | [diff] [blame] | 30 | |
| 31 | #ifdef HAVE_SYS_SELECT_H |
| 32 | #include <sys/select.h> |
| 33 | #endif |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 34 | |
| 35 | #ifdef HAVE_SYS_SOCKET_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 36 | #include <sys/socket.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #ifdef HAVE_NETINET_IN_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 40 | #include <netinet/in.h> |
| 41 | #include <netinet/tcp.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #ifdef HAVE_ARPA_INET_H |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 45 | #include <arpa/inet.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #ifdef HAVE_NETDB_H |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 49 | #include <netdb.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 52 | #ifdef HAVE_FCNTL_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 53 | #include <fcntl.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 56 | #include <assert.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 57 | |
| 58 | #ifdef HAVE_SCHED_H |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 59 | #include <sched.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 60 | #endif |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 61 | |
David Reiss | 9b90344 | 2009-10-21 05:51:28 +0000 | [diff] [blame] | 62 | #ifndef AF_LOCAL |
| 63 | #define AF_LOCAL AF_UNIX |
| 64 | #endif |
| 65 | |
James E. King, III | 7edc8fa | 2017-01-20 10:11:41 -0500 | [diff] [blame] | 66 | #ifdef HAVE_INTTYPES_H |
| 67 | #include <inttypes.h> |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
James E. King, III | 7edc8fa | 2017-01-20 10:11:41 -0500 | [diff] [blame] | 70 | #ifdef HAVE_STDINT_H |
| 71 | #include <stdint.h> |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 72 | #endif |
| 73 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 74 | namespace apache { |
| 75 | namespace thrift { |
| 76 | namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 77 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 78 | using namespace apache::thrift::protocol; |
| 79 | using namespace apache::thrift::transport; |
| 80 | using namespace apache::thrift::concurrency; |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 81 | using apache::thrift::transport::TSocket; |
| 82 | using apache::thrift::transport::TTransportException; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 83 | using stdcxx::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 |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 118 | stdcxx::shared_ptr<TProcessor> processor_; |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 119 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 120 | /// Object wrapping network socket |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 121 | stdcxx::shared_ptr<TSocket> tSocket_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 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 |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 163 | stdcxx::shared_ptr<TMemoryBuffer> inputTransport_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 164 | |
| 165 | /// Transport that processor writes to |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 166 | stdcxx::shared_ptr<TMemoryBuffer> outputTransport_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 167 | |
| 168 | /// extra transport generated by transport factory (e.g. BufferedRouterTransport) |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 169 | stdcxx::shared_ptr<TTransport> factoryInputTransport_; |
| 170 | stdcxx::shared_ptr<TTransport> factoryOutputTransport_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 171 | |
| 172 | /// Protocol decoder |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 173 | stdcxx::shared_ptr<TProtocol> inputProtocol_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 174 | |
| 175 | /// Protocol encoder |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 176 | stdcxx::shared_ptr<TProtocol> outputProtocol_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 177 | |
| 178 | /// Server event handler, if any |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 179 | stdcxx::shared_ptr<TServerEventHandler> serverEventHandler_; |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 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 |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 212 | TConnection(stdcxx::shared_ptr<TSocket> socket, |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 213 | TNonblockingIOThread* ioThread) { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 214 | readBuffer_ = NULL; |
| 215 | readBufferSize_ = 0; |
| 216 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 217 | ioThread_ = ioThread; |
| 218 | server_ = ioThread->getServer(); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 219 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 220 | // Allocate input and output transports these only need to be allocated |
| 221 | // once per TConnection (they don't need to be reallocated on init() call) |
| 222 | inputTransport_.reset(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 223 | outputTransport_.reset( |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 224 | new TMemoryBuffer(static_cast<uint32_t>(server_->getWriteBufferDefaultSize()))); |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 225 | |
| 226 | tSocket_ = socket; |
| 227 | |
| 228 | init(ioThread); |
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 |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 245 | void init(TNonblockingIOThread* ioThread); |
| 246 | |
| 247 | /// set socket for connection |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 248 | void setSocket(stdcxx::shared_ptr<TSocket> socket); |
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()) { |
Changli Gao | 257dcef | 2017-04-06 00:42:01 +0800 | [diff] [blame] | 291 | server_->decrementActiveProcessors(); |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 292 | close(); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 293 | throw TException("TConnection::forceClose: failed write on notify pipe"); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /// return the server this connection was initialized for. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 298 | TNonblockingServer* getServer() const { return server_; } |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 299 | |
| 300 | /// get state of connection. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 301 | TAppState getState() const { return appState_; } |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 302 | |
| 303 | /// return the TSocket transport wrapping this network connection |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 304 | stdcxx::shared_ptr<TSocket> getTSocket() const { return tSocket_; } |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 305 | |
| 306 | /// return the server event handler if any |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 307 | stdcxx::shared_ptr<TServerEventHandler> getServerEventHandler() { return serverEventHandler_; } |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 308 | |
| 309 | /// return the Thrift connection context if any |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 310 | void* getConnectionContext() { return connectionContext_; } |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 313 | class TNonblockingServer::TConnection::Task : public Runnable { |
| 314 | public: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 315 | Task(stdcxx::shared_ptr<TProcessor> processor, |
| 316 | stdcxx::shared_ptr<TProtocol> input, |
| 317 | stdcxx::shared_ptr<TProtocol> output, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 318 | TConnection* connection) |
| 319 | : processor_(processor), |
| 320 | input_(input), |
| 321 | output_(output), |
| 322 | connection_(connection), |
| 323 | serverEventHandler_(connection_->getServerEventHandler()), |
| 324 | connectionContext_(connection_->getConnectionContext()) {} |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 325 | |
| 326 | void run() { |
| 327 | try { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 328 | for (;;) { |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 329 | if (serverEventHandler_) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 330 | serverEventHandler_->processContext(connectionContext_, connection_->getTSocket()); |
| 331 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 332 | if (!processor_->process(input_, output_, connectionContext_) |
| 333 | || !input_->getTransport()->peek()) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | } |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 337 | } catch (const TTransportException& ttx) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 338 | GlobalOutput.printf("TNonblockingServer: client died: %s", ttx.what()); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 339 | } catch (const std::bad_alloc&) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 340 | GlobalOutput("TNonblockingServer: caught bad_alloc exception."); |
Henrique Mendonca | 962b353 | 2012-09-20 13:19:55 +0000 | [diff] [blame] | 341 | exit(1); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 342 | } catch (const std::exception& x) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 343 | GlobalOutput.printf("TNonblockingServer: process() exception: %s: %s", |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 344 | typeid(x).name(), |
| 345 | x.what()); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 346 | } catch (...) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 347 | GlobalOutput.printf("TNonblockingServer: unknown exception while processing."); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 348 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 349 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 350 | // Signal completion back to the libevent thread via a pipe |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 351 | if (!connection_->notifyIOThread()) { |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 352 | GlobalOutput.printf("TNonblockingServer: failed to notifyIOThread, closing."); |
Changli Gao | 257dcef | 2017-04-06 00:42:01 +0800 | [diff] [blame] | 353 | connection_->server_->decrementActiveProcessors(); |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 354 | connection_->close(); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 355 | throw TException("TNonblockingServer::Task::run: failed write on notify pipe"); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 356 | } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 359 | TConnection* getTConnection() { return connection_; } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 360 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 361 | private: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 362 | stdcxx::shared_ptr<TProcessor> processor_; |
| 363 | stdcxx::shared_ptr<TProtocol> input_; |
| 364 | stdcxx::shared_ptr<TProtocol> output_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 365 | TConnection* connection_; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 366 | stdcxx::shared_ptr<TServerEventHandler> serverEventHandler_; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 367 | void* connectionContext_; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 368 | }; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 369 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 370 | void TNonblockingServer::TConnection::init(TNonblockingIOThread* ioThread) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 371 | ioThread_ = ioThread; |
| 372 | server_ = ioThread->getServer(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 373 | appState_ = APP_INIT; |
| 374 | eventFlags_ = 0; |
| 375 | |
| 376 | readBufferPos_ = 0; |
| 377 | readWant_ = 0; |
| 378 | |
| 379 | writeBuffer_ = NULL; |
| 380 | writeBufferSize_ = 0; |
| 381 | writeBufferPos_ = 0; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 382 | largestWriteBufferSize_ = 0; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 383 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 384 | socketState_ = SOCKET_RECV_FRAMING; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 385 | callsForResize_ = 0; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 386 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 387 | // get input/transports |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 388 | factoryInputTransport_ = server_->getInputTransportFactory()->getTransport(inputTransport_); |
| 389 | factoryOutputTransport_ = server_->getOutputTransportFactory()->getTransport(outputTransport_); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 390 | |
| 391 | // Create protocol |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 392 | if (server_->getHeaderTransport()) { |
| 393 | inputProtocol_ = server_->getInputProtocolFactory()->getProtocol(factoryInputTransport_, |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 394 | factoryOutputTransport_); |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 395 | outputProtocol_ = inputProtocol_; |
| 396 | } else { |
| 397 | inputProtocol_ = server_->getInputProtocolFactory()->getProtocol(factoryInputTransport_); |
| 398 | outputProtocol_ = server_->getOutputProtocolFactory()->getProtocol(factoryOutputTransport_); |
| 399 | } |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 400 | |
| 401 | // Set up for any server event handler |
| 402 | serverEventHandler_ = server_->getEventHandler(); |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 403 | if (serverEventHandler_) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 404 | connectionContext_ = serverEventHandler_->createContext(inputProtocol_, outputProtocol_); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 405 | } else { |
| 406 | connectionContext_ = NULL; |
| 407 | } |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 408 | |
| 409 | // Get the processor |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 410 | processor_ = server_->getProcessor(inputProtocol_, outputProtocol_, tSocket_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 411 | } |
| 412 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 413 | void TNonblockingServer::TConnection::setSocket(stdcxx::shared_ptr<TSocket> socket) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 414 | tSocket_ = socket; |
| 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) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 442 | //In Nonblocking SSLSocket some operations need to be retried again. |
| 443 | //Current approach is parsing exception message, but a better solution needs to be investigated. |
| 444 | if(!strstr(te.what(), "retry")) { |
| 445 | GlobalOutput.printf("TConnection::workSocket(): %s", te.what()); |
| 446 | close(); |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 447 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 448 | return; |
| 449 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 450 | } |
| 451 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 452 | if (readBufferPos_ < sizeof(framing.size)) { |
| 453 | // more needed before frame size is known -- save what we have so far |
| 454 | readWant_ = framing.size; |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | readWant_ = ntohl(framing.size); |
Roger Meier | 3781c24 | 2011-12-11 20:07:21 +0000 | [diff] [blame] | 459 | if (readWant_ > server_->getMaxFrameSize()) { |
| 460 | // Don't allow giant frame sizes. This prevents bad clients from |
| 461 | // causing us to try and allocate a giant buffer. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 462 | GlobalOutput.printf( |
| 463 | "TNonblockingServer: frame size too large " |
| 464 | "(%" PRIu32 " > %" PRIu64 |
| 465 | ") from client %s. " |
| 466 | "Remote side not using TFramedTransport?", |
| 467 | readWant_, |
| 468 | (uint64_t)server_->getMaxFrameSize(), |
| 469 | tSocket_->getSocketInfo().c_str()); |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 470 | close(); |
| 471 | return; |
| 472 | } |
| 473 | // size known; now get the rest of the frame |
| 474 | transition(); |
Bugra Gedik | 8bcb7ac | 2018-01-21 09:43:49 -0800 | [diff] [blame^] | 475 | |
| 476 | // If the socket has more data than the frame header, continue to work on it. This is not strictly necessary for |
| 477 | // regular sockets, because if there is more data, libevent will fire the event handler registered for read |
| 478 | // readiness, which will in turn call workSocket(). However, some socket types (such as TSSLSocket) may have the |
| 479 | // data sitting in their internal buffers and from libevent's perspective, there is no further data available. In |
| 480 | // that case, not having this workSocket() call here would result in a hang as we will never get to work the socket, |
| 481 | // despite having more data. |
| 482 | if (tSocket_->hasPendingDataToRead()) |
| 483 | { |
| 484 | workSocket(); |
| 485 | } |
| 486 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 487 | return; |
| 488 | |
| 489 | case SOCKET_RECV: |
| 490 | // It is an error to be in this state if we already have all the data |
| 491 | assert(readBufferPos_ < readWant_); |
| 492 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 493 | try { |
| 494 | // Read from the socket |
| 495 | fetch = readWant_ - readBufferPos_; |
| 496 | got = tSocket_->read(readBuffer_ + readBufferPos_, fetch); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 497 | } catch (TTransportException& te) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 498 | //In Nonblocking SSLSocket some operations need to be retried again. |
| 499 | //Current approach is parsing exception message, but a better solution needs to be investigated. |
| 500 | if(!strstr(te.what(), "retry")) { |
| 501 | GlobalOutput.printf("TConnection::workSocket(): %s", te.what()); |
| 502 | close(); |
| 503 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 504 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 507 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 508 | if (got > 0) { |
| 509 | // Move along in the buffer |
| 510 | readBufferPos_ += got; |
| 511 | |
| 512 | // Check that we did not overdo it |
| 513 | assert(readBufferPos_ <= readWant_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 514 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 515 | // We are done reading, move onto the next state |
| 516 | if (readBufferPos_ == readWant_) { |
| 517 | transition(); |
| 518 | } |
| 519 | return; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // Whenever we get down here it means a remote disconnect |
| 523 | close(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 524 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 525 | return; |
| 526 | |
| 527 | case SOCKET_SEND: |
| 528 | // Should never have position past size |
| 529 | assert(writeBufferPos_ <= writeBufferSize_); |
| 530 | |
| 531 | // If there is no data to send, then let us move on |
| 532 | if (writeBufferPos_ == writeBufferSize_) { |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 533 | GlobalOutput("WARNING: Send state with no data to send"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 534 | transition(); |
| 535 | return; |
| 536 | } |
| 537 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 538 | try { |
| 539 | left = writeBufferSize_ - writeBufferPos_; |
| 540 | sent = tSocket_->write_partial(writeBuffer_ + writeBufferPos_, left); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 541 | } catch (TTransportException& te) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 542 | GlobalOutput.printf("TConnection::workSocket(): %s ", te.what()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 543 | close(); |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | writeBufferPos_ += sent; |
| 548 | |
| 549 | // Did we overdo it? |
| 550 | assert(writeBufferPos_ <= writeBufferSize_); |
| 551 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 552 | // We are done! |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 553 | if (writeBufferPos_ == writeBufferSize_) { |
| 554 | transition(); |
| 555 | } |
| 556 | |
| 557 | return; |
| 558 | |
| 559 | default: |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 560 | GlobalOutput.printf("Unexpected Socket State %d", socketState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 561 | assert(0); |
| 562 | } |
| 563 | } |
| 564 | |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 565 | bool TNonblockingServer::getHeaderTransport() { |
Konrad Grochowski | 7f4be5f | 2015-11-05 20:23:11 +0100 | [diff] [blame] | 566 | // Currently if there is no output protocol factory, |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 567 | // we assume header transport (without having to create |
| 568 | // a new transport and check) |
| 569 | return getOutputProtocolFactory() == NULL; |
| 570 | } |
| 571 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 572 | /** |
| 573 | * This is called when the application transitions from one state into |
| 574 | * another. This means that it has finished writing the data that it needed |
| 575 | * to, or finished receiving the data that it needed to. |
| 576 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 577 | void TNonblockingServer::TConnection::transition() { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 578 | // ensure this connection is active right now |
| 579 | assert(ioThread_); |
| 580 | assert(server_); |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 581 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 582 | // Switch upon the state that we are currently in and move to a new state |
| 583 | switch (appState_) { |
| 584 | |
| 585 | case APP_READ_REQUEST: |
| 586 | // We are done reading the request, package the read buffer into transport |
| 587 | // and get back some data from the dispatch function |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 588 | if (server_->getHeaderTransport()) { |
| 589 | inputTransport_->resetBuffer(readBuffer_, readBufferPos_); |
| 590 | outputTransport_->resetBuffer(); |
| 591 | } else { |
| 592 | // We saved room for the framing size in case header transport needed it, |
| 593 | // but just skip it for the non-header case |
| 594 | inputTransport_->resetBuffer(readBuffer_ + 4, readBufferPos_ - 4); |
| 595 | outputTransport_->resetBuffer(); |
| 596 | |
| 597 | // Prepend four bytes of blank space to the buffer so we can |
| 598 | // write the frame size there later. |
| 599 | outputTransport_->getWritePtr(4); |
| 600 | outputTransport_->wroteBytes(4); |
| 601 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 602 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 603 | server_->incrementActiveProcessors(); |
| 604 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 605 | if (server_->isThreadPoolProcessing()) { |
| 606 | // 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] | 607 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 608 | // Create task and dispatch to the thread manager |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 609 | stdcxx::shared_ptr<Runnable> task = stdcxx::shared_ptr<Runnable>( |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 610 | new Task(processor_, inputProtocol_, outputProtocol_, this)); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 611 | // The application is now waiting on the task to finish |
| 612 | appState_ = APP_WAIT_TASK; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 613 | |
Changli Gao | d4fa706 | 2017-03-10 13:25:43 +0800 | [diff] [blame] | 614 | // Set this connection idle so that libevent doesn't process more |
| 615 | // data on it while we're still waiting for the threadmanager to |
| 616 | // finish this task |
| 617 | setIdle(); |
| 618 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 619 | try { |
| 620 | server_->addTask(task); |
| 621 | } catch (IllegalStateException& ise) { |
| 622 | // The ThreadManager is not ready to handle any more tasks (it's probably shutting down). |
| 623 | GlobalOutput.printf("IllegalStateException: Server::process() %s", ise.what()); |
Changli Gao | d4fa706 | 2017-03-10 13:25:43 +0800 | [diff] [blame] | 624 | server_->decrementActiveProcessors(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 625 | close(); |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 626 | } catch (TimedOutException& to) { |
| 627 | GlobalOutput.printf("[ERROR] TimedOutException: Server::process() %s", to.what()); |
Changli Gao | d4fa706 | 2017-03-10 13:25:43 +0800 | [diff] [blame] | 628 | server_->decrementActiveProcessors(); |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 629 | close(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 630 | } |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 631 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 632 | return; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 633 | } else { |
| 634 | try { |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 635 | if (serverEventHandler_) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 636 | serverEventHandler_->processContext(connectionContext_, getTSocket()); |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 637 | } |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 638 | // Invoke the processor |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 639 | processor_->process(inputProtocol_, outputProtocol_, connectionContext_); |
| 640 | } catch (const TTransportException& ttx) { |
| 641 | GlobalOutput.printf( |
| 642 | "TNonblockingServer transport error in " |
| 643 | "process(): %s", |
| 644 | ttx.what()); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 645 | server_->decrementActiveProcessors(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 646 | close(); |
| 647 | return; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 648 | } catch (const std::exception& x) { |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 649 | GlobalOutput.printf("Server::process() uncaught exception: %s: %s", |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 650 | typeid(x).name(), |
| 651 | x.what()); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 652 | server_->decrementActiveProcessors(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 653 | close(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 654 | return; |
| 655 | } catch (...) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 656 | GlobalOutput.printf("Server::process() unknown exception"); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 657 | server_->decrementActiveProcessors(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 658 | close(); |
| 659 | return; |
| 660 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 663 | // Intentionally fall through here, the call to process has written into |
| 664 | // the writeBuffer_ |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 665 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 666 | case APP_WAIT_TASK: |
| 667 | // We have now finished processing a task and the result has been written |
| 668 | // into the outputTransport_, so we grab its contents and place them into |
| 669 | // the writeBuffer_ for actual writing by the libevent thread |
| 670 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 671 | server_->decrementActiveProcessors(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 672 | // Get the result of the operation |
| 673 | outputTransport_->getBuffer(&writeBuffer_, &writeBufferSize_); |
| 674 | |
| 675 | // If the function call generated return data, then move into the send |
| 676 | // state and get going |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 677 | // 4 bytes were reserved for frame size |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 678 | if (writeBufferSize_ > 4) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 679 | |
| 680 | // Move into write state |
| 681 | writeBufferPos_ = 0; |
| 682 | socketState_ = SOCKET_SEND; |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 683 | |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 684 | // Put the frame size into the write buffer |
| 685 | int32_t frameSize = (int32_t)htonl(writeBufferSize_ - 4); |
| 686 | memcpy(writeBuffer_, &frameSize, 4); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 687 | |
| 688 | // Socket into write mode |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 689 | appState_ = APP_SEND_RESULT; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 690 | setWrite(); |
| 691 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 695 | // In this case, the request was oneway and we should fall through |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 696 | // right back into the read frame header state |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 697 | goto LABEL_APP_INIT; |
| 698 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 699 | case APP_SEND_RESULT: |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 700 | // it's now safe to perform buffer size housekeeping. |
| 701 | if (writeBufferSize_ > largestWriteBufferSize_) { |
| 702 | largestWriteBufferSize_ = writeBufferSize_; |
| 703 | } |
| 704 | if (server_->getResizeBufferEveryN() > 0 |
| 705 | && ++callsForResize_ >= server_->getResizeBufferEveryN()) { |
| 706 | checkIdleBufferMemLimit(server_->getIdleReadBufferLimit(), |
| 707 | server_->getIdleWriteBufferLimit()); |
| 708 | callsForResize_ = 0; |
| 709 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 710 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 711 | // N.B.: We also intentionally fall through here into the INIT state! |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 712 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 713 | LABEL_APP_INIT: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 714 | case APP_INIT: |
| 715 | |
| 716 | // Clear write buffer variables |
| 717 | writeBuffer_ = NULL; |
| 718 | writeBufferPos_ = 0; |
| 719 | writeBufferSize_ = 0; |
| 720 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 721 | // Into read4 state we go |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 722 | socketState_ = SOCKET_RECV_FRAMING; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 723 | appState_ = APP_READ_FRAME_SIZE; |
| 724 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 725 | readBufferPos_ = 0; |
| 726 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 727 | // Register read event |
| 728 | setRead(); |
David Reiss | 84e63ab | 2008-03-07 20:12:28 +0000 | [diff] [blame] | 729 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 730 | return; |
| 731 | |
| 732 | case APP_READ_FRAME_SIZE: |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 733 | readWant_ += 4; |
| 734 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 735 | // We just read the request length |
| 736 | // Double the buffer size until it is big enough |
| 737 | if (readWant_ > readBufferSize_) { |
| 738 | if (readBufferSize_ == 0) { |
| 739 | readBufferSize_ = 1; |
| 740 | } |
| 741 | uint32_t newSize = readBufferSize_; |
| 742 | while (readWant_ > newSize) { |
| 743 | newSize *= 2; |
| 744 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 745 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 746 | uint8_t* newBuffer = (uint8_t*)std::realloc(readBuffer_, newSize); |
| 747 | if (newBuffer == NULL) { |
| 748 | // nothing else to be done... |
| 749 | throw std::bad_alloc(); |
| 750 | } |
| 751 | readBuffer_ = newBuffer; |
| 752 | readBufferSize_ = newSize; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Dave Watson | 792db4e | 2015-01-16 11:22:01 -0800 | [diff] [blame] | 755 | readBufferPos_ = 4; |
| 756 | *((uint32_t*)readBuffer_) = htonl(readWant_ - 4); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 757 | |
| 758 | // Move into read request state |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 759 | socketState_ = SOCKET_RECV; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 760 | appState_ = APP_READ_REQUEST; |
| 761 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 762 | return; |
| 763 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 764 | case APP_CLOSE_CONNECTION: |
| 765 | server_->decrementActiveProcessors(); |
| 766 | close(); |
| 767 | return; |
| 768 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 769 | default: |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 770 | GlobalOutput.printf("Unexpected Application State %d", appState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 771 | assert(0); |
| 772 | } |
| 773 | } |
| 774 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 775 | void TNonblockingServer::TConnection::setFlags(short eventFlags) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 776 | // Catch the do nothing case |
| 777 | if (eventFlags_ == eventFlags) { |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | // Delete a previously existing event |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 782 | if (eventFlags_ && event_del(&event_) == -1) { |
| 783 | GlobalOutput.perror("TConnection::setFlags() event_del", THRIFT_GET_SOCKET_ERROR); |
| 784 | return; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | // Update in memory structure |
| 788 | eventFlags_ = eventFlags; |
| 789 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 790 | // Do not call event_set if there are no flags |
| 791 | if (!eventFlags_) { |
| 792 | return; |
| 793 | } |
| 794 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 795 | /* |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 796 | * event_set: |
| 797 | * |
| 798 | * Prepares the event structure &event to be used in future calls to |
| 799 | * 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] | 800 | * eventHandler using the 'sock' file descriptor to monitor events. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 801 | * |
| 802 | * The events can be either EV_READ, EV_WRITE, or both, indicating |
| 803 | * that an application can read or write from the file respectively without |
| 804 | * blocking. |
| 805 | * |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 806 | * The eventHandler will be called with the file descriptor that triggered |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 807 | * the event and the type of event which will be one of: EV_TIMEOUT, |
| 808 | * EV_SIGNAL, EV_READ, EV_WRITE. |
| 809 | * |
| 810 | * The additional flag EV_PERSIST makes an event_add() persistent until |
| 811 | * event_del() has been called. |
| 812 | * |
| 813 | * Once initialized, the &event struct can be used repeatedly with |
| 814 | * 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] | 815 | * 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] | 816 | * when an ev structure has been added to libevent using event_add() the |
| 817 | * structure must persist until the event occurs (assuming EV_PERSIST |
| 818 | * is not set) or is removed using event_del(). You may not reuse the same |
| 819 | * ev structure for multiple monitored descriptors; each descriptor needs |
| 820 | * its own ev. |
| 821 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 822 | event_set(&event_, tSocket_->getSocketFD(), eventFlags_, TConnection::eventHandler, this); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 823 | event_base_set(ioThread_->getEventBase(), &event_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 824 | |
| 825 | // Add the event |
| 826 | if (event_add(&event_, 0) == -1) { |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 827 | GlobalOutput.perror("TConnection::setFlags(): could not event_add", THRIFT_GET_SOCKET_ERROR); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Closes a connection |
| 833 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 834 | void TNonblockingServer::TConnection::close() { |
Changli Gao | bf42d55 | 2017-03-20 14:29:07 +0800 | [diff] [blame] | 835 | setIdle(); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 836 | |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 837 | if (serverEventHandler_) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 838 | serverEventHandler_->deleteContext(connectionContext_, inputProtocol_, outputProtocol_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 839 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 840 | ioThread_ = NULL; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 841 | |
| 842 | // Close the socket |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 843 | tSocket_->close(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 844 | |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 845 | // close any factory produced transports |
| 846 | factoryInputTransport_->close(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 847 | factoryOutputTransport_->close(); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 848 | |
Roger Meier | 464a3a4 | 2014-07-07 21:48:28 +0200 | [diff] [blame] | 849 | // release processor and handler |
| 850 | processor_.reset(); |
| 851 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 852 | // Give this object back to the server that owns it |
| 853 | server_->returnConnection(this); |
| 854 | } |
| 855 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 856 | void TNonblockingServer::TConnection::checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit) { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 857 | if (readLimit > 0 && readBufferSize_ > readLimit) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 858 | free(readBuffer_); |
| 859 | readBuffer_ = NULL; |
| 860 | readBufferSize_ = 0; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 861 | } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 862 | |
| 863 | if (writeLimit > 0 && largestWriteBufferSize_ > writeLimit) { |
| 864 | // just start over |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 865 | outputTransport_->resetBuffer(static_cast<uint32_t>(server_->getWriteBufferDefaultSize())); |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 866 | largestWriteBufferSize_ = 0; |
| 867 | } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 868 | } |
| 869 | |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 870 | TNonblockingServer::~TNonblockingServer() { |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 871 | // Close any active connections (moves them to the idle connection stack) |
| 872 | while (activeConnections_.size()) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 873 | activeConnections_.front()->close(); |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 874 | } |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 875 | // Clean up unused TConnection objects in connectionStack_ |
| 876 | while (!connectionStack_.empty()) { |
| 877 | TConnection* connection = connectionStack_.top(); |
| 878 | connectionStack_.pop(); |
| 879 | delete connection; |
| 880 | } |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 881 | // The TNonblockingIOThread objects have shared_ptrs to the Thread |
| 882 | // objects and the Thread objects have shared_ptrs to the TNonblockingIOThread |
| 883 | // objects (as runnable) so these objects will never deallocate without help. |
| 884 | while (!ioThreads_.empty()) { |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 885 | stdcxx::shared_ptr<TNonblockingIOThread> iot = ioThreads_.back(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 886 | ioThreads_.pop_back(); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 887 | iot->setThread(stdcxx::shared_ptr<Thread>()); |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 888 | } |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 891 | /** |
| 892 | * Creates a new connection either by reusing an object off the stack or |
| 893 | * by allocating a new one entirely |
| 894 | */ |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 895 | TNonblockingServer::TConnection* TNonblockingServer::createConnection(stdcxx::shared_ptr<TSocket> socket) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 896 | // Check the stack |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 897 | Guard g(connMutex_); |
| 898 | |
| 899 | // pick an IO thread to handle this connection -- currently round robin |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 900 | assert(nextIOThread_ < ioThreads_.size()); |
| 901 | int selectedThreadIdx = nextIOThread_; |
Ben Craig | 6493523 | 2013-10-09 15:21:38 -0500 | [diff] [blame] | 902 | nextIOThread_ = static_cast<uint32_t>((nextIOThread_ + 1) % ioThreads_.size()); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 903 | |
| 904 | TNonblockingIOThread* ioThread = ioThreads_[selectedThreadIdx].get(); |
| 905 | |
| 906 | // Check the connection stack to see if we can re-use |
| 907 | TConnection* result = NULL; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 908 | if (connectionStack_.empty()) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 909 | result = new TConnection(socket, ioThread); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 910 | ++numTConnections_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 911 | } else { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 912 | result = connectionStack_.top(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 913 | connectionStack_.pop(); |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 914 | result->setSocket(socket); |
| 915 | result->init(ioThread); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 916 | } |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 917 | activeConnections_.push_back(result); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 918 | return result; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Returns a connection to the stack |
| 923 | */ |
| 924 | void TNonblockingServer::returnConnection(TConnection* connection) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 925 | Guard g(connMutex_); |
| 926 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 927 | activeConnections_.erase(std::remove(activeConnections_.begin(), |
| 928 | activeConnections_.end(), |
| 929 | connection), |
| 930 | activeConnections_.end()); |
Roger Meier | 0c04fcc | 2013-03-22 19:52:08 +0100 | [diff] [blame] | 931 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 932 | if (connectionStackLimit_ && (connectionStack_.size() >= connectionStackLimit_)) { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 933 | delete connection; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 934 | --numTConnections_; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 935 | } else { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 936 | connection->checkIdleBufferMemLimit(idleReadBufferLimit_, idleWriteBufferLimit_); |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 937 | connectionStack_.push(connection); |
| 938 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 942 | * Server socket had something happen. We accept all waiting client |
| 943 | * connections on fd and assign TConnection objects to handle those requests. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 944 | */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 945 | void TNonblockingServer::handleEvent(THRIFT_SOCKET fd, short which) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 946 | (void)which; |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 947 | // Make sure that libevent didn't mess up the socket handles |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 948 | assert(fd == serverSocket_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 949 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 950 | // Going to accept a new client socket |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 951 | stdcxx::shared_ptr<TSocket> clientSocket; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 952 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 953 | clientSocket = serverTransport_->accept(); |
| 954 | if (clientSocket) { |
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) { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 961 | clientSocket->close(); |
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. |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 966 | clientSocket->close(); |
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 | // Create a new TConnection for this client socket. |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 973 | TConnection* clientConnection = createConnection(clientSocket); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 974 | |
| 975 | // Fail fast if we could not create a TConnection object |
| 976 | if (clientConnection == NULL) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 977 | GlobalOutput.printf("thriftServerEventHandler: failed TConnection factory"); |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 978 | clientSocket->close(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 979 | return; |
| 980 | } |
| 981 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 982 | /* |
| 983 | * Either notify the ioThread that is assigned this connection to |
| 984 | * start processing, or if it is us, we'll just ask this |
| 985 | * connection to do its initial state change here. |
| 986 | * |
| 987 | * (We need to avoid writing to our own notification pipe, to |
| 988 | * avoid possible deadlocks if the pipe is full.) |
| 989 | * |
| 990 | * The IO thread #0 is the only one that handles these listen |
| 991 | * events, so unless the connection has been assigned to thread #0 |
| 992 | * we know it's not on our thread. |
| 993 | */ |
| 994 | if (clientConnection->getIOThreadNumber() == 0) { |
| 995 | clientConnection->transition(); |
| 996 | } else { |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 997 | if (!clientConnection->notifyIOThread()) { |
| 998 | GlobalOutput.perror("[ERROR] notifyIOThread failed on fresh connection, closing", errno); |
Changli Gao | 75386db | 2017-03-10 13:15:37 +0800 | [diff] [blame] | 999 | clientConnection->close(); |
Jens Geyer | fb05cf6 | 2014-12-04 21:49:07 +0100 | [diff] [blame] | 1000 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1001 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /** |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1006 | * 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] | 1007 | */ |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1008 | void TNonblockingServer::createAndListenOnSocket() { |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 1009 | serverTransport_->listen(); |
| 1010 | serverSocket_ = serverTransport_->getSocketFD(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1013 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 1014 | void TNonblockingServer::setThreadManager(stdcxx::shared_ptr<ThreadManager> threadManager) { |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1015 | threadManager_ = threadManager; |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 1016 | if (threadManager) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1017 | threadManager->setExpireCallback( |
| 1018 | apache::thrift::stdcxx::bind(&TNonblockingServer::expireClose, |
| 1019 | this, |
| 1020 | apache::thrift::stdcxx::placeholders::_1)); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1021 | threadPoolProcessing_ = true; |
| 1022 | } else { |
| 1023 | threadPoolProcessing_ = false; |
| 1024 | } |
| 1025 | } |
| 1026 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1027 | bool TNonblockingServer::serverOverloaded() { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1028 | size_t activeConnections = numTConnections_ - connectionStack_.size(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1029 | if (numActiveProcessors_ > maxActiveProcessors_ || activeConnections > maxConnections_) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1030 | if (!overloaded_) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1031 | GlobalOutput.printf("TNonblockingServer: overload condition begun."); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1032 | overloaded_ = true; |
| 1033 | } |
| 1034 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1035 | if (overloaded_ && (numActiveProcessors_ <= overloadHysteresis_ * maxActiveProcessors_) |
| 1036 | && (activeConnections <= overloadHysteresis_ * maxConnections_)) { |
| 1037 | GlobalOutput.printf( |
| 1038 | "TNonblockingServer: overload ended; " |
| 1039 | "%u dropped (%llu total)", |
| 1040 | nConnectionsDropped_, |
| 1041 | nTotalConnectionsDropped_); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1042 | nConnectionsDropped_ = 0; |
| 1043 | overloaded_ = false; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | return overloaded_; |
| 1048 | } |
| 1049 | |
| 1050 | bool TNonblockingServer::drainPendingTask() { |
| 1051 | if (threadManager_) { |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 1052 | stdcxx::shared_ptr<Runnable> task = threadManager_->removeNextPending(); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1053 | if (task) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1054 | TConnection* connection = static_cast<TConnection::Task*>(task.get())->getTConnection(); |
| 1055 | assert(connection && connection->getServer() && connection->getState() == APP_WAIT_TASK); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1056 | connection->forceClose(); |
| 1057 | return true; |
| 1058 | } |
| 1059 | } |
| 1060 | return false; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 1063 | void TNonblockingServer::expireClose(stdcxx::shared_ptr<Runnable> task) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1064 | TConnection* connection = static_cast<TConnection::Task*>(task.get())->getTConnection(); |
| 1065 | assert(connection && connection->getServer() && connection->getState() == APP_WAIT_TASK); |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1066 | connection->forceClose(); |
| 1067 | } |
| 1068 | |
Bugra Gedik | 8bcb7ac | 2018-01-21 09:43:49 -0800 | [diff] [blame^] | 1069 | void TNonblockingServer::stop() { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1070 | // Breaks the event loop in all threads so that they end ASAP. |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1071 | for (uint32_t i = 0; i < ioThreads_.size(); ++i) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1072 | ioThreads_[i]->stop(); |
| 1073 | } |
| 1074 | } |
| 1075 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1076 | void TNonblockingServer::registerEvents(event_base* user_event_base) { |
| 1077 | userEventBase_ = user_event_base; |
| 1078 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1079 | // init listen socket |
Roger Meier | e802aa4 | 2013-07-19 21:10:54 +0200 | [diff] [blame] | 1080 | if (serverSocket_ == THRIFT_INVALID_SOCKET) |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1081 | createAndListenOnSocket(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1082 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1083 | // set up the IO threads |
| 1084 | assert(ioThreads_.empty()); |
| 1085 | if (!numIOThreads_) { |
| 1086 | numIOThreads_ = DEFAULT_IO_THREADS; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1087 | } |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 1088 | // User-provided event-base doesn't works for multi-threaded servers |
| 1089 | assert(numIOThreads_ == 1 || !userEventBase_); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1090 | |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1091 | for (uint32_t id = 0; id < numIOThreads_; ++id) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1092 | // the first IO thread also does the listening on server socket |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 1093 | THRIFT_SOCKET listenFd = (id == 0 ? serverSocket_ : THRIFT_INVALID_SOCKET); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1094 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1095 | shared_ptr<TNonblockingIOThread> thread( |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1096 | new TNonblockingIOThread(this, id, listenFd, useHighPriorityIOThreads_)); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1097 | ioThreads_.push_back(thread); |
| 1098 | } |
| 1099 | |
| 1100 | // Notify handler of the preServe event |
Roger Meier | 7295745 | 2013-06-29 00:28:50 +0200 | [diff] [blame] | 1101 | if (eventHandler_) { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 1102 | eventHandler_->preServe(); |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1105 | // Start all of our helper IO threads. Note that the threads run forever, |
| 1106 | // only terminating if stop() is called. |
| 1107 | assert(ioThreads_.size() == numIOThreads_); |
| 1108 | assert(ioThreads_.size() > 0); |
| 1109 | |
Divya Thaluru | 808d143 | 2017-08-06 16:36:36 -0700 | [diff] [blame] | 1110 | GlobalOutput.printf("TNonblockingServer: Serving with %d io threads.", |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1111 | ioThreads_.size()); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Launch all the secondary IO threads in separate threads |
| 1114 | if (ioThreads_.size() > 1) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1115 | ioThreadFactory_.reset(new PlatformThreadFactory( |
Nobuaki Sukegawa | 2825664 | 2014-12-16 03:24:37 +0900 | [diff] [blame] | 1116 | #if !USE_BOOST_THREAD && !USE_STD_THREAD |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1117 | PlatformThreadFactory::OTHER, // scheduler |
| 1118 | PlatformThreadFactory::NORMAL, // priority |
| 1119 | 1, // stack size (MB) |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1120 | #endif |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1121 | false // detached |
| 1122 | )); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1123 | |
| 1124 | assert(ioThreadFactory_.get()); |
| 1125 | |
| 1126 | // intentionally starting at thread 1, not 0 |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1127 | for (uint32_t i = 1; i < ioThreads_.size(); ++i) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1128 | shared_ptr<Thread> thread = ioThreadFactory_->newThread(ioThreads_[i]); |
| 1129 | ioThreads_[i]->setThread(thread); |
| 1130 | thread->start(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1134 | // Register the events for the primary (listener) IO thread |
| 1135 | ioThreads_[0]->registerEvents(); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Main workhorse function, starts up the server listening on a port and |
| 1140 | * loops over the libevent handler. |
| 1141 | */ |
| 1142 | void TNonblockingServer::serve() { |
| 1143 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 1144 | if (ioThreads_.empty()) |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 1145 | registerEvents(NULL); |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1146 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1147 | // Run the primary (listener) IO thread loop in our main thread; this will |
| 1148 | // only return when the server is shutting down. |
| 1149 | ioThreads_[0]->run(); |
| 1150 | |
| 1151 | // Ensure all threads are finished before exiting serve() |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1152 | for (uint32_t i = 0; i < ioThreads_.size(); ++i) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1153 | ioThreads_[i]->join(); |
| 1154 | GlobalOutput.printf("TNonblocking: join done for IO thread #%d", i); |
| 1155 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1158 | TNonblockingIOThread::TNonblockingIOThread(TNonblockingServer* server, |
| 1159 | int number, |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1160 | THRIFT_SOCKET listenSocket, |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1161 | bool useHighPriority) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1162 | : server_(server), |
| 1163 | number_(number), |
| 1164 | listenSocket_(listenSocket), |
| 1165 | useHighPriority_(useHighPriority), |
| 1166 | eventBase_(NULL), |
| 1167 | ownEventBase_(false) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1168 | notificationPipeFDs_[0] = -1; |
| 1169 | notificationPipeFDs_[1] = -1; |
| 1170 | } |
| 1171 | |
| 1172 | TNonblockingIOThread::~TNonblockingIOThread() { |
| 1173 | // make sure our associated thread is fully finished |
| 1174 | join(); |
| 1175 | |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1176 | if (eventBase_ && ownEventBase_) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1177 | event_base_free(eventBase_); |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1178 | ownEventBase_ = false; |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
gzshi | 4194562 | 2017-01-06 10:47:03 +0800 | [diff] [blame] | 1181 | if (listenSocket_ != THRIFT_INVALID_SOCKET) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1182 | if (0 != ::THRIFT_CLOSESOCKET(listenSocket_)) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1183 | GlobalOutput.perror("TNonblockingIOThread listenSocket_ close(): ", THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1184 | } |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 1185 | listenSocket_ = THRIFT_INVALID_SOCKET; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | for (int i = 0; i < 2; ++i) { |
| 1189 | if (notificationPipeFDs_[i] >= 0) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1190 | if (0 != ::THRIFT_CLOSESOCKET(notificationPipeFDs_[i])) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1191 | GlobalOutput.perror("TNonblockingIOThread notificationPipe close(): ", |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1192 | THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1193 | } |
Roger Meier | 0be9ffa | 2013-07-19 21:10:01 +0200 | [diff] [blame] | 1194 | notificationPipeFDs_[i] = THRIFT_INVALID_SOCKET; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | void TNonblockingIOThread::createNotificationPipe() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1200 | if (evutil_socketpair(AF_LOCAL, SOCK_STREAM, 0, notificationPipeFDs_) == -1) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1201 | GlobalOutput.perror("TNonblockingServer::createNotificationPipe ", EVUTIL_SOCKET_ERROR()); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1202 | throw TException("can't create notification pipe"); |
| 1203 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1204 | if (evutil_make_socket_nonblocking(notificationPipeFDs_[0]) < 0 |
| 1205 | || evutil_make_socket_nonblocking(notificationPipeFDs_[1]) < 0) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1206 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]); |
| 1207 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]); |
| 1208 | throw TException("TNonblockingServer::createNotificationPipe() THRIFT_O_NONBLOCK"); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1209 | } |
| 1210 | for (int i = 0; i < 2; ++i) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1211 | #if LIBEVENT_VERSION_NUMBER < 0x02000000 |
| 1212 | int flags; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1213 | if ((flags = THRIFT_FCNTL(notificationPipeFDs_[i], F_GETFD, 0)) < 0 |
| 1214 | || THRIFT_FCNTL(notificationPipeFDs_[i], F_SETFD, flags | FD_CLOEXEC) < 0) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1215 | #else |
| 1216 | if (evutil_make_socket_closeonexec(notificationPipeFDs_[i]) < 0) { |
| 1217 | #endif |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1218 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]); |
| 1219 | ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1220 | throw TException( |
| 1221 | "TNonblockingServer::createNotificationPipe() " |
| 1222 | "FD_CLOEXEC"); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | /** |
| 1228 | * Register the core libevent events onto the proper base. |
| 1229 | */ |
| 1230 | void TNonblockingIOThread::registerEvents() { |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1231 | threadId_ = Thread::get_current(); |
| 1232 | |
| 1233 | assert(eventBase_ == 0); |
| 1234 | eventBase_ = getServer()->getUserEventBase(); |
| 1235 | if (eventBase_ == NULL) { |
| 1236 | eventBase_ = event_base_new(); |
| 1237 | ownEventBase_ = true; |
| 1238 | } |
| 1239 | |
| 1240 | // Print some libevent stats |
| 1241 | if (number_ == 0) { |
| 1242 | GlobalOutput.printf("TNonblockingServer: using libevent %s method %s", |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1243 | event_get_version(), |
| 1244 | event_base_get_method(eventBase_)); |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
gzshi | 4194562 | 2017-01-06 10:47:03 +0800 | [diff] [blame] | 1247 | if (listenSocket_ != THRIFT_INVALID_SOCKET) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1248 | // Register the server event |
| 1249 | event_set(&serverEvent_, |
| 1250 | listenSocket_, |
| 1251 | EV_READ | EV_PERSIST, |
| 1252 | TNonblockingIOThread::listenHandler, |
| 1253 | server_); |
| 1254 | event_base_set(eventBase_, &serverEvent_); |
| 1255 | |
| 1256 | // Add the event and start up the server |
| 1257 | if (-1 == event_add(&serverEvent_, 0)) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1258 | throw TException( |
| 1259 | "TNonblockingServer::serve(): " |
| 1260 | "event_add() failed on server listen event"); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1261 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1262 | GlobalOutput.printf("TNonblocking: IO thread #%d registered for listen.", number_); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | createNotificationPipe(); |
| 1266 | |
| 1267 | // Create an event to be notified when a task finishes |
| 1268 | event_set(¬ificationEvent_, |
| 1269 | getNotificationRecvFD(), |
| 1270 | EV_READ | EV_PERSIST, |
| 1271 | TNonblockingIOThread::notifyHandler, |
| 1272 | this); |
| 1273 | |
| 1274 | // Attach to the base |
| 1275 | event_base_set(eventBase_, ¬ificationEvent_); |
| 1276 | |
| 1277 | // Add the event and start up the server |
| 1278 | if (-1 == event_add(¬ificationEvent_, 0)) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1279 | throw TException( |
| 1280 | "TNonblockingServer::serve(): " |
| 1281 | "event_add() failed on task-done notification event"); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1282 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1283 | GlobalOutput.printf("TNonblocking: IO thread #%d registered for notify.", number_); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | bool TNonblockingIOThread::notify(TNonblockingServer::TConnection* conn) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1287 | THRIFT_SOCKET fd = getNotificationSendFD(); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1288 | if (fd < 0) { |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
Lei Feiwei | b5ebcd1 | 2015-04-04 22:12:07 +0800 | [diff] [blame] | 1292 | fd_set wfds, efds; |
tpcwang | f98d59f | 2016-03-23 16:18:52 -0700 | [diff] [blame] | 1293 | long ret = -1; |
| 1294 | long kSize = sizeof(conn); |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 1295 | const char* pos = reinterpret_cast<const char*>(&conn); |
abadcafe | 38772c9 | 2015-04-03 22:23:04 +0800 | [diff] [blame] | 1296 | |
| 1297 | while (kSize > 0) { |
Lei Feiwei | b5ebcd1 | 2015-04-04 22:12:07 +0800 | [diff] [blame] | 1298 | FD_ZERO(&wfds); |
| 1299 | FD_ZERO(&efds); |
| 1300 | FD_SET(fd, &wfds); |
| 1301 | FD_SET(fd, &efds); |
tpcwang | f98d59f | 2016-03-23 16:18:52 -0700 | [diff] [blame] | 1302 | ret = select(static_cast<int>(fd + 1), NULL, &wfds, &efds, NULL); |
abadcafe | 38772c9 | 2015-04-03 22:23:04 +0800 | [diff] [blame] | 1303 | if (ret < 0) { |
| 1304 | return false; |
| 1305 | } else if (ret == 0) { |
| 1306 | continue; |
| 1307 | } |
| 1308 | |
Lei Feiwei | b5ebcd1 | 2015-04-04 22:12:07 +0800 | [diff] [blame] | 1309 | if (FD_ISSET(fd, &efds)) { |
| 1310 | ::THRIFT_CLOSESOCKET(fd); |
abadcafe | 38772c9 | 2015-04-03 22:23:04 +0800 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
| 1313 | |
Lei Feiwei | b5ebcd1 | 2015-04-04 22:12:07 +0800 | [diff] [blame] | 1314 | if (FD_ISSET(fd, &wfds)) { |
abadcafe | 38772c9 | 2015-04-03 22:23:04 +0800 | [diff] [blame] | 1315 | ret = send(fd, pos, kSize, 0); |
| 1316 | if (ret < 0) { |
| 1317 | if (errno == EAGAIN) { |
| 1318 | continue; |
| 1319 | } |
| 1320 | |
Lei Feiwei | b5ebcd1 | 2015-04-04 22:12:07 +0800 | [diff] [blame] | 1321 | ::THRIFT_CLOSESOCKET(fd); |
abadcafe | 38772c9 | 2015-04-03 22:23:04 +0800 | [diff] [blame] | 1322 | return false; |
| 1323 | } |
| 1324 | |
| 1325 | kSize -= ret; |
| 1326 | pos += ret; |
| 1327 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | return true; |
| 1331 | } |
| 1332 | |
| 1333 | /* static */ |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1334 | void TNonblockingIOThread::notifyHandler(evutil_socket_t fd, short which, void* v) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1335 | TNonblockingIOThread* ioThread = (TNonblockingIOThread*)v; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1336 | assert(ioThread); |
Roger Meier | d0cdecf | 2011-12-08 19:34:01 +0000 | [diff] [blame] | 1337 | (void)which; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1338 | |
| 1339 | while (true) { |
| 1340 | TNonblockingServer::TConnection* connection = 0; |
| 1341 | const int kSize = sizeof(connection); |
Ben Craig | 6493523 | 2013-10-09 15:21:38 -0500 | [diff] [blame] | 1342 | long nBytes = recv(fd, cast_sockopt(&connection), kSize, 0); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1343 | if (nBytes == kSize) { |
| 1344 | if (connection == NULL) { |
| 1345 | // this is the command to stop our thread, exit the handler! |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1346 | ioThread->breakLoop(false); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
| 1349 | connection->transition(); |
| 1350 | } else if (nBytes > 0) { |
| 1351 | // 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] | 1352 | GlobalOutput.printf("notifyHandler: Bad read of %d bytes, wanted %d", nBytes, kSize); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1353 | ioThread->breakLoop(true); |
| 1354 | return; |
| 1355 | } else if (nBytes == 0) { |
| 1356 | GlobalOutput.printf("notifyHandler: Notify socket closed!"); |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1357 | ioThread->breakLoop(false); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1358 | // exit the loop |
| 1359 | break; |
| 1360 | } else { // nBytes < 0 |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1361 | if (THRIFT_GET_SOCKET_ERROR != THRIFT_EWOULDBLOCK |
| 1362 | && THRIFT_GET_SOCKET_ERROR != THRIFT_EAGAIN) { |
| 1363 | GlobalOutput.perror("TNonblocking: notifyHandler read() failed: ", THRIFT_GET_SOCKET_ERROR); |
| 1364 | ioThread->breakLoop(true); |
| 1365 | return; |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1366 | } |
| 1367 | // exit the loop |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | void TNonblockingIOThread::breakLoop(bool error) { |
| 1374 | if (error) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1375 | GlobalOutput.printf("TNonblockingServer: IO thread #%d exiting with error.", number_); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1376 | // TODO: figure out something better to do here, but for now kill the |
| 1377 | // whole process. |
| 1378 | GlobalOutput.printf("TNonblockingServer: aborting process."); |
| 1379 | ::abort(); |
| 1380 | } |
| 1381 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1382 | // If we're running in the same thread, we can't use the notify(0) |
| 1383 | // mechanism to stop the thread, but happily if we're running in the |
| 1384 | // same thread, this means the thread can't be blocking in the event |
| 1385 | // loop either. |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1386 | if (!Thread::is_current(threadId_)) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1387 | notify(NULL); |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1388 | } else { |
| 1389 | // cause the loop to stop ASAP - even if it has things to do in it |
| 1390 | event_base_loopbreak(eventBase_); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void TNonblockingIOThread::setCurrentThreadHighPriority(bool value) { |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1395 | #ifdef HAVE_SCHED_H |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1396 | // Start out with a standard, low-priority setup for the sched params. |
| 1397 | struct sched_param sp; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1398 | bzero((void*)&sp, sizeof(sp)); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1399 | int policy = SCHED_OTHER; |
| 1400 | |
| 1401 | // If desired, set up high-priority sched params structure. |
| 1402 | if (value) { |
| 1403 | // FIFO scheduler, ranked above default SCHED_OTHER queue |
| 1404 | policy = SCHED_FIFO; |
| 1405 | // The priority only compares us to other SCHED_FIFO threads, so we |
| 1406 | // just pick a random priority halfway between min & max. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1407 | 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] | 1408 | |
| 1409 | sp.sched_priority = priority; |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1412 | // Actually set the sched params for the current thread. |
| 1413 | if (0 == pthread_setschedparam(pthread_self(), policy, &sp)) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1414 | GlobalOutput.printf("TNonblocking: IO Thread #%d using high-priority scheduler!", number_); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1415 | } else { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1416 | GlobalOutput.perror("TNonblocking: pthread_setschedparam(): ", THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1417 | } |
Roger Meier | d051ca0 | 2013-08-15 01:35:11 +0200 | [diff] [blame] | 1418 | #else |
| 1419 | THRIFT_UNUSED_VARIABLE(value); |
Roger Meier | 12d7053 | 2011-12-14 23:35:28 +0000 | [diff] [blame] | 1420 | #endif |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1421 | } |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1422 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1423 | void TNonblockingIOThread::run() { |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1424 | if (eventBase_ == NULL) { |
Roger Meier | 6f2a503 | 2013-07-08 23:35:25 +0200 | [diff] [blame] | 1425 | registerEvents(); |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1426 | } |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1427 | if (useHighPriority_) { |
| 1428 | setCurrentThreadHighPriority(true); |
| 1429 | } |
| 1430 | |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1431 | if (eventBase_ != NULL) |
| 1432 | { |
| 1433 | GlobalOutput.printf("TNonblockingServer: IO thread #%d entering loop...", number_); |
| 1434 | // Run libevent engine, never returns, invokes calls to eventHandler |
| 1435 | event_base_loop(eventBase_, 0); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1436 | |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame] | 1437 | if (useHighPriority_) { |
| 1438 | setCurrentThreadHighPriority(false); |
| 1439 | } |
| 1440 | |
| 1441 | // cleans up our registered events |
| 1442 | cleanupEvents(); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1445 | GlobalOutput.printf("TNonblockingServer: IO thread #%d run() done!", number_); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | void TNonblockingIOThread::cleanupEvents() { |
| 1449 | // stop the listen socket, if any |
gzshi | 4194562 | 2017-01-06 10:47:03 +0800 | [diff] [blame] | 1450 | if (listenSocket_ != THRIFT_INVALID_SOCKET) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1451 | if (event_del(&serverEvent_) == -1) { |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 1452 | GlobalOutput.perror("TNonblockingIOThread::stop() event_del: ", THRIFT_GET_SOCKET_ERROR); |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | event_del(¬ificationEvent_); |
| 1457 | } |
| 1458 | |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1459 | void TNonblockingIOThread::stop() { |
| 1460 | // This should cause the thread to fall out of its event loop ASAP. |
| 1461 | breakLoop(false); |
| 1462 | } |
| 1463 | |
| 1464 | void TNonblockingIOThread::join() { |
| 1465 | // If this was a thread created by a factory (not the thread that called |
| 1466 | // serve()), we join() it to make sure we shut down fully. |
| 1467 | if (thread_) { |
| 1468 | try { |
| 1469 | // Note that it is safe to both join() ourselves twice, as well as join |
| 1470 | // the current thread as the pthread implementation checks for deadlock. |
| 1471 | thread_->join(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1472 | } catch (...) { |
Jake Farrell | b0d9560 | 2011-12-06 01:17:26 +0000 | [diff] [blame] | 1473 | // swallow everything |
| 1474 | } |
| 1475 | } |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1476 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } // apache::thrift::server |