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