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 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 20 | #include "TNonblockingServer.h" |
David Reiss | e11f307 | 2008-10-07 21:39:19 +0000 | [diff] [blame] | 21 | #include <concurrency/Exception.h> |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 22 | #include <transport/TSocket.h> |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 23 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 24 | #include <iostream> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 25 | |
| 26 | #ifdef HAVE_SYS_SOCKET_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 27 | #include <sys/socket.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | #ifdef HAVE_NETINET_IN_H |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 31 | #include <netinet/in.h> |
| 32 | #include <netinet/tcp.h> |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 33 | #include <arpa/inet.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #ifdef HAVE_NETDB_H |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 37 | #include <netdb.h> |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 40 | #include <fcntl.h> |
| 41 | #include <errno.h> |
| 42 | #include <assert.h> |
| 43 | |
David Reiss | 9b90344 | 2009-10-21 05:51:28 +0000 | [diff] [blame] | 44 | #ifndef AF_LOCAL |
| 45 | #define AF_LOCAL AF_UNIX |
| 46 | #endif |
| 47 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 48 | namespace apache { namespace thrift { namespace server { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 49 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 50 | using namespace apache::thrift::protocol; |
| 51 | using namespace apache::thrift::transport; |
| 52 | using namespace apache::thrift::concurrency; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 53 | using namespace std; |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 54 | using apache::thrift::transport::TSocket; |
| 55 | using apache::thrift::transport::TTransportException; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 56 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 57 | /// Three states for sockets: recv frame size, recv data, and send mode |
| 58 | enum TSocketState { |
| 59 | SOCKET_RECV_FRAMING, |
| 60 | SOCKET_RECV, |
| 61 | SOCKET_SEND |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * Five states for the nonblocking server: |
| 66 | * 1) initialize |
| 67 | * 2) read 4 byte frame size |
| 68 | * 3) read frame of data |
| 69 | * 4) send back data (if any) |
| 70 | * 5) force immediate connection close |
| 71 | */ |
| 72 | enum TAppState { |
| 73 | APP_INIT, |
| 74 | APP_READ_FRAME_SIZE, |
| 75 | APP_READ_REQUEST, |
| 76 | APP_WAIT_TASK, |
| 77 | APP_SEND_RESULT, |
| 78 | APP_CLOSE_CONNECTION |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * Represents a connection that is handled via libevent. This connection |
| 83 | * essentially encapsulates a socket that has some associated libevent state. |
| 84 | */ |
| 85 | class TNonblockingServer::TConnection { |
| 86 | private: |
| 87 | |
| 88 | /// Server handle |
| 89 | TNonblockingServer* server_; |
| 90 | |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 91 | /// TProcessor |
| 92 | boost::shared_ptr<TProcessor> processor_; |
| 93 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 94 | /// Object wrapping network socket |
| 95 | boost::shared_ptr<TSocket> tSocket_; |
| 96 | |
| 97 | /// Libevent object |
| 98 | struct event event_; |
| 99 | |
| 100 | /// Libevent flags |
| 101 | short eventFlags_; |
| 102 | |
| 103 | /// Socket mode |
| 104 | TSocketState socketState_; |
| 105 | |
| 106 | /// Application state |
| 107 | TAppState appState_; |
| 108 | |
| 109 | /// How much data needed to read |
| 110 | uint32_t readWant_; |
| 111 | |
| 112 | /// Where in the read buffer are we |
| 113 | uint32_t readBufferPos_; |
| 114 | |
| 115 | /// Read buffer |
| 116 | uint8_t* readBuffer_; |
| 117 | |
| 118 | /// Read buffer size |
| 119 | uint32_t readBufferSize_; |
| 120 | |
| 121 | /// Write buffer |
| 122 | uint8_t* writeBuffer_; |
| 123 | |
| 124 | /// Write buffer size |
| 125 | uint32_t writeBufferSize_; |
| 126 | |
| 127 | /// How far through writing are we? |
| 128 | uint32_t writeBufferPos_; |
| 129 | |
| 130 | /// Largest size of write buffer seen since buffer was constructed |
| 131 | size_t largestWriteBufferSize_; |
| 132 | |
| 133 | /// Count of the number of calls for use with getResizeBufferEveryN(). |
| 134 | int32_t callsForResize_; |
| 135 | |
| 136 | /// Task handle |
| 137 | int taskHandle_; |
| 138 | |
| 139 | /// Task event |
| 140 | struct event taskEvent_; |
| 141 | |
| 142 | /// Transport to read from |
| 143 | boost::shared_ptr<TMemoryBuffer> inputTransport_; |
| 144 | |
| 145 | /// Transport that processor writes to |
| 146 | boost::shared_ptr<TMemoryBuffer> outputTransport_; |
| 147 | |
| 148 | /// extra transport generated by transport factory (e.g. BufferedRouterTransport) |
| 149 | boost::shared_ptr<TTransport> factoryInputTransport_; |
| 150 | boost::shared_ptr<TTransport> factoryOutputTransport_; |
| 151 | |
| 152 | /// Protocol decoder |
| 153 | boost::shared_ptr<TProtocol> inputProtocol_; |
| 154 | |
| 155 | /// Protocol encoder |
| 156 | boost::shared_ptr<TProtocol> outputProtocol_; |
| 157 | |
| 158 | /// Server event handler, if any |
| 159 | boost::shared_ptr<TServerEventHandler> serverEventHandler_; |
| 160 | |
| 161 | /// Thrift call context, if any |
| 162 | void *connectionContext_; |
| 163 | |
| 164 | /// Go into read mode |
| 165 | void setRead() { |
| 166 | setFlags(EV_READ | EV_PERSIST); |
| 167 | } |
| 168 | |
| 169 | /// Go into write mode |
| 170 | void setWrite() { |
| 171 | setFlags(EV_WRITE | EV_PERSIST); |
| 172 | } |
| 173 | |
| 174 | /// Set socket idle |
| 175 | void setIdle() { |
| 176 | setFlags(0); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set event flags for this connection. |
| 181 | * |
| 182 | * @param eventFlags flags we pass to libevent for the connection. |
| 183 | */ |
| 184 | void setFlags(short eventFlags); |
| 185 | |
| 186 | /** |
| 187 | * Libevent handler called (via our static wrapper) when the connection |
| 188 | * socket had something happen. Rather than use the flags libevent passed, |
| 189 | * we use the connection state to determine whether we need to read or |
| 190 | * write the socket. |
| 191 | */ |
| 192 | void workSocket(); |
| 193 | |
| 194 | /// Close this connection and free or reset its resources. |
| 195 | void close(); |
| 196 | |
| 197 | public: |
| 198 | |
| 199 | class Task; |
| 200 | |
| 201 | /// Constructor |
| 202 | TConnection(int socket, short eventFlags, TNonblockingServer *s, |
| 203 | const sockaddr* addr, socklen_t addrLen) { |
| 204 | readBuffer_ = NULL; |
| 205 | readBufferSize_ = 0; |
| 206 | |
| 207 | // Allocate input and output transports |
| 208 | // these only need to be allocated once per TConnection (they don't need to be |
| 209 | // reallocated on init() call) |
| 210 | inputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(readBuffer_, readBufferSize_)); |
| 211 | outputTransport_ = boost::shared_ptr<TMemoryBuffer>(new TMemoryBuffer(s->getWriteBufferDefaultSize())); |
| 212 | tSocket_.reset(new TSocket()); |
| 213 | |
| 214 | init(socket, eventFlags, s, addr, addrLen); |
| 215 | server_->incrementNumConnections(); |
| 216 | } |
| 217 | |
| 218 | ~TConnection() { |
| 219 | std::free(readBuffer_); |
| 220 | server_->decrementNumConnections(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Check buffers against any size limits and shrink it if exceeded. |
| 225 | * |
| 226 | * @param readLimit we reduce read buffer size to this (if nonzero). |
| 227 | * @param writeLimit if nonzero and write buffer is larger, replace it. |
| 228 | */ |
| 229 | void checkIdleBufferMemLimit(size_t readLimit, size_t writeLimit); |
| 230 | |
| 231 | /// Initialize |
| 232 | void init(int socket, short eventFlags, TNonblockingServer *s, |
| 233 | const sockaddr* addr, socklen_t addrLen); |
| 234 | |
| 235 | /** |
| 236 | * This is called when the application transitions from one state into |
| 237 | * another. This means that it has finished writing the data that it needed |
| 238 | * to, or finished receiving the data that it needed to. |
| 239 | */ |
| 240 | void transition(); |
| 241 | |
| 242 | /** |
| 243 | * C-callable event handler for connection events. Provides a callback |
| 244 | * that libevent can understand which invokes connection_->workSocket(). |
| 245 | * |
| 246 | * @param fd the descriptor the event occurred on. |
| 247 | * @param which the flags associated with the event. |
| 248 | * @param v void* callback arg where we placed TConnection's "this". |
| 249 | */ |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 250 | static void eventHandler(evutil_socket_t fd, short /* which */, void* v) { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 251 | assert(fd == ((TConnection*)v)->getTSocket()->getSocketFD()); |
| 252 | ((TConnection*)v)->workSocket(); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * C-callable event handler for signaling task completion. Provides a |
| 257 | * callback that libevent can understand that will read a connection |
| 258 | * object's address from a pipe and call connection->transition() for |
| 259 | * that object. |
| 260 | * |
| 261 | * @param fd the descriptor the event occurred on. |
| 262 | */ |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 263 | static void taskHandler(evutil_socket_t fd, short /* which */, void* /* v */) { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 264 | TConnection* connection; |
| 265 | ssize_t nBytes; |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 266 | while ((nBytes = recv(fd, cast_sockopt(&connection), sizeof(TConnection*), 0)) |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 267 | == sizeof(TConnection*)) { |
| 268 | connection->transition(); |
| 269 | } |
| 270 | if (nBytes > 0) { |
| 271 | throw TException("TConnection::taskHandler unexpected partial read"); |
| 272 | } |
| 273 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
| 274 | GlobalOutput.perror("TConnection::taskHandler read failed, resource leak", errno); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Notification to server that processing has ended on this request. |
| 280 | * Can be called either when processing is completed or when a waiting |
| 281 | * task has been preemptively terminated (on overload). |
| 282 | * |
| 283 | * @return true if successful, false if unable to notify (check errno). |
| 284 | */ |
| 285 | bool notifyServer() { |
| 286 | TConnection* connection = this; |
Bryan Duxbury | 266b173 | 2011-09-01 16:50:28 +0000 | [diff] [blame] | 287 | if (send(server_->getNotificationSendFD(), const_cast_sockopt(&connection), |
| 288 | sizeof(TConnection*), 0) != sizeof(TConnection*)) { |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | /// Force connection shutdown for this connection. |
| 296 | void forceClose() { |
| 297 | appState_ = APP_CLOSE_CONNECTION; |
| 298 | if (!notifyServer()) { |
| 299 | throw TException("TConnection::forceClose: failed write on notify pipe"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /// return the server this connection was initialized for. |
| 304 | TNonblockingServer* getServer() { |
| 305 | return server_; |
| 306 | } |
| 307 | |
| 308 | /// get state of connection. |
| 309 | TAppState getState() { |
| 310 | return appState_; |
| 311 | } |
| 312 | |
| 313 | /// return the TSocket transport wrapping this network connection |
| 314 | boost::shared_ptr<TSocket> getTSocket() const { |
| 315 | return tSocket_; |
| 316 | } |
| 317 | |
| 318 | /// return the server event handler if any |
| 319 | boost::shared_ptr<TServerEventHandler> getServerEventHandler() { |
| 320 | return serverEventHandler_; |
| 321 | } |
| 322 | |
| 323 | /// return the Thrift connection context if any |
| 324 | void* getConnectionContext() { |
| 325 | return connectionContext_; |
| 326 | } |
| 327 | |
| 328 | }; |
| 329 | |
| 330 | class TNonblockingServer::TConnection::Task: public Runnable { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 331 | public: |
| 332 | Task(boost::shared_ptr<TProcessor> processor, |
| 333 | boost::shared_ptr<TProtocol> input, |
| 334 | boost::shared_ptr<TProtocol> output, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 335 | TConnection* connection) : |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 336 | processor_(processor), |
| 337 | input_(input), |
| 338 | output_(output), |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 339 | connection_(connection), |
| 340 | serverEventHandler_(connection_->getServerEventHandler()), |
| 341 | connectionContext_(connection_->getConnectionContext()) {} |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 342 | |
| 343 | void run() { |
| 344 | try { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 345 | for (;;) { |
| 346 | if (serverEventHandler_ != NULL) { |
| 347 | serverEventHandler_->processContext(connectionContext_, connection_->getTSocket()); |
| 348 | } |
| 349 | if (!processor_->process(input_, output_, connectionContext_) || |
| 350 | !input_->getTransport()->peek()) { |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | } |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 354 | } catch (const TTransportException& ttx) { |
| 355 | GlobalOutput.printf("TNonblockingServer client died: %s", ttx.what()); |
| 356 | } catch (const bad_alloc&) { |
| 357 | GlobalOutput("TNonblockingServer caught bad_alloc exception."); |
David Reiss | 28e88ec | 2010-03-09 05:19:27 +0000 | [diff] [blame] | 358 | exit(-1); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 359 | } catch (const std::exception& x) { |
| 360 | GlobalOutput.printf("TNonblockingServer process() exception: %s: %s", |
| 361 | typeid(x).name(), x.what()); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 362 | } catch (...) { |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 363 | GlobalOutput("TNonblockingServer uncaught exception."); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 364 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 365 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 366 | // Signal completion back to the libevent thread via a pipe |
| 367 | if (!connection_->notifyServer()) { |
| 368 | throw TException("TNonblockingServer::Task::run: failed write on notify pipe"); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 369 | } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TConnection* getTConnection() { |
| 373 | return connection_; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | private: |
| 377 | boost::shared_ptr<TProcessor> processor_; |
| 378 | boost::shared_ptr<TProtocol> input_; |
| 379 | boost::shared_ptr<TProtocol> output_; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 380 | TConnection* connection_; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 381 | boost::shared_ptr<TServerEventHandler> serverEventHandler_; |
| 382 | void* connectionContext_; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 383 | }; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 384 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 385 | void TNonblockingServer::TConnection::init(int socket, short eventFlags, |
| 386 | TNonblockingServer* s, |
| 387 | const sockaddr* addr, |
| 388 | socklen_t addrLen) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 389 | tSocket_->setSocketFD(socket); |
| 390 | tSocket_->setCachedAddress(addr, addrLen); |
| 391 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 392 | server_ = s; |
| 393 | appState_ = APP_INIT; |
| 394 | eventFlags_ = 0; |
| 395 | |
| 396 | readBufferPos_ = 0; |
| 397 | readWant_ = 0; |
| 398 | |
| 399 | writeBuffer_ = NULL; |
| 400 | writeBufferSize_ = 0; |
| 401 | writeBufferPos_ = 0; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 402 | largestWriteBufferSize_ = 0; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 403 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 404 | socketState_ = SOCKET_RECV_FRAMING; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 405 | appState_ = APP_INIT; |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 406 | callsForResize_ = 0; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 407 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 408 | // Set flags, which also registers the event |
| 409 | setFlags(eventFlags); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 410 | |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 411 | // get input/transports |
| 412 | factoryInputTransport_ = s->getInputTransportFactory()->getTransport(inputTransport_); |
| 413 | factoryOutputTransport_ = s->getOutputTransportFactory()->getTransport(outputTransport_); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 414 | |
| 415 | // Create protocol |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 416 | inputProtocol_ = s->getInputProtocolFactory()->getProtocol(factoryInputTransport_); |
| 417 | outputProtocol_ = s->getOutputProtocolFactory()->getProtocol(factoryOutputTransport_); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 418 | |
| 419 | // Set up for any server event handler |
| 420 | serverEventHandler_ = server_->getEventHandler(); |
| 421 | if (serverEventHandler_ != NULL) { |
| 422 | connectionContext_ = serverEventHandler_->createContext(inputProtocol_, outputProtocol_); |
| 423 | } else { |
| 424 | connectionContext_ = NULL; |
| 425 | } |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 426 | |
| 427 | // Get the processor |
| 428 | processor_ = s->getProcessor(inputProtocol_, outputProtocol_, tSocket_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 431 | void TNonblockingServer::TConnection::workSocket() { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 432 | int got=0, left=0, sent=0; |
Mark Slee | aaa23ed | 2007-01-30 19:52:05 +0000 | [diff] [blame] | 433 | uint32_t fetch = 0; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 434 | |
| 435 | switch (socketState_) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 436 | case SOCKET_RECV_FRAMING: |
| 437 | union { |
| 438 | uint8_t buf[sizeof(uint32_t)]; |
| 439 | int32_t size; |
| 440 | } framing; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 441 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 442 | // if we've already received some bytes we kept them here |
| 443 | framing.size = readWant_; |
| 444 | // determine size of this frame |
| 445 | try { |
| 446 | // Read from the socket |
| 447 | fetch = tSocket_->read(&framing.buf[readBufferPos_], |
| 448 | uint32_t(sizeof(framing.size) - readBufferPos_)); |
| 449 | if (fetch == 0) { |
| 450 | // Whenever we get here it means a remote disconnect |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 451 | close(); |
| 452 | return; |
| 453 | } |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 454 | readBufferPos_ += fetch; |
| 455 | } catch (TTransportException& te) { |
| 456 | GlobalOutput.printf("TConnection::workSocket(): %s", te.what()); |
| 457 | close(); |
| 458 | |
| 459 | return; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 460 | } |
| 461 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 462 | if (readBufferPos_ < sizeof(framing.size)) { |
| 463 | // more needed before frame size is known -- save what we have so far |
| 464 | readWant_ = framing.size; |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | readWant_ = ntohl(framing.size); |
| 469 | if (static_cast<int>(readWant_) <= 0) { |
| 470 | GlobalOutput.printf("TConnection:workSocket() Negative frame size %d, remote side not using TFramedTransport?", static_cast<int>(readWant_)); |
| 471 | close(); |
| 472 | return; |
| 473 | } |
| 474 | // size known; now get the rest of the frame |
| 475 | transition(); |
| 476 | return; |
| 477 | |
| 478 | case SOCKET_RECV: |
| 479 | // It is an error to be in this state if we already have all the data |
| 480 | assert(readBufferPos_ < readWant_); |
| 481 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 482 | try { |
| 483 | // Read from the socket |
| 484 | fetch = readWant_ - readBufferPos_; |
| 485 | got = tSocket_->read(readBuffer_ + readBufferPos_, fetch); |
| 486 | } |
| 487 | catch (TTransportException& te) { |
| 488 | GlobalOutput.printf("TConnection::workSocket(): %s", te.what()); |
| 489 | close(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 490 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 494 | if (got > 0) { |
| 495 | // Move along in the buffer |
| 496 | readBufferPos_ += got; |
| 497 | |
| 498 | // Check that we did not overdo it |
| 499 | assert(readBufferPos_ <= readWant_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 500 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 501 | // We are done reading, move onto the next state |
| 502 | if (readBufferPos_ == readWant_) { |
| 503 | transition(); |
| 504 | } |
| 505 | return; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Whenever we get down here it means a remote disconnect |
| 509 | close(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 510 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 511 | return; |
| 512 | |
| 513 | case SOCKET_SEND: |
| 514 | // Should never have position past size |
| 515 | assert(writeBufferPos_ <= writeBufferSize_); |
| 516 | |
| 517 | // If there is no data to send, then let us move on |
| 518 | if (writeBufferPos_ == writeBufferSize_) { |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 519 | GlobalOutput("WARNING: Send state with no data to send\n"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 520 | transition(); |
| 521 | return; |
| 522 | } |
| 523 | |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 524 | try { |
| 525 | left = writeBufferSize_ - writeBufferPos_; |
| 526 | sent = tSocket_->write_partial(writeBuffer_ + writeBufferPos_, left); |
| 527 | } |
| 528 | catch (TTransportException& te) { |
| 529 | GlobalOutput.printf("TConnection::workSocket(): %s ", te.what()); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 530 | close(); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | writeBufferPos_ += sent; |
| 535 | |
| 536 | // Did we overdo it? |
| 537 | assert(writeBufferPos_ <= writeBufferSize_); |
| 538 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 539 | // We are done! |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 540 | if (writeBufferPos_ == writeBufferSize_) { |
| 541 | transition(); |
| 542 | } |
| 543 | |
| 544 | return; |
| 545 | |
| 546 | default: |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 547 | GlobalOutput.printf("Unexpected Socket State %d", socketState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 548 | assert(0); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * This is called when the application transitions from one state into |
| 554 | * another. This means that it has finished writing the data that it needed |
| 555 | * to, or finished receiving the data that it needed to. |
| 556 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 557 | void TNonblockingServer::TConnection::transition() { |
| 558 | |
| 559 | int sz = 0; |
| 560 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 561 | // Switch upon the state that we are currently in and move to a new state |
| 562 | switch (appState_) { |
| 563 | |
| 564 | case APP_READ_REQUEST: |
| 565 | // We are done reading the request, package the read buffer into transport |
| 566 | // and get back some data from the dispatch function |
| 567 | inputTransport_->resetBuffer(readBuffer_, readBufferPos_); |
David Reiss | 7197efb | 2010-10-06 17:10:43 +0000 | [diff] [blame] | 568 | outputTransport_->resetBuffer(); |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 569 | // Prepend four bytes of blank space to the buffer so we can |
| 570 | // write the frame size there later. |
| 571 | outputTransport_->getWritePtr(4); |
| 572 | outputTransport_->wroteBytes(4); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 573 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 574 | server_->incrementActiveProcessors(); |
| 575 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 576 | if (server_->isThreadPoolProcessing()) { |
| 577 | // 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] | 578 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 579 | // Create task and dispatch to the thread manager |
| 580 | boost::shared_ptr<Runnable> task = |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 581 | boost::shared_ptr<Runnable>(new Task(processor_, |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 582 | inputProtocol_, |
| 583 | outputProtocol_, |
| 584 | this)); |
| 585 | // The application is now waiting on the task to finish |
| 586 | appState_ = APP_WAIT_TASK; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 587 | |
David Reiss | e11f307 | 2008-10-07 21:39:19 +0000 | [diff] [blame] | 588 | try { |
| 589 | server_->addTask(task); |
| 590 | } catch (IllegalStateException & ise) { |
| 591 | // The ThreadManager is not ready to handle any more tasks (it's probably shutting down). |
David Reiss | c53a594 | 2008-10-07 23:55:24 +0000 | [diff] [blame] | 592 | GlobalOutput.printf("IllegalStateException: Server::process() %s", ise.what()); |
David Reiss | e11f307 | 2008-10-07 21:39:19 +0000 | [diff] [blame] | 593 | close(); |
| 594 | } |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 595 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 596 | // Set this connection idle so that libevent doesn't process more |
| 597 | // data on it while we're still waiting for the threadmanager to |
| 598 | // finish this task |
| 599 | setIdle(); |
| 600 | return; |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 601 | } else { |
| 602 | try { |
| 603 | // Invoke the processor |
Bryan Duxbury | 6dd9cd0 | 2011-09-01 18:06:20 +0000 | [diff] [blame] | 604 | processor_->process(inputProtocol_, outputProtocol_, |
| 605 | connectionContext_); |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 606 | } catch (const TTransportException &ttx) { |
| 607 | GlobalOutput.printf("TNonblockingServer transport error in " |
| 608 | "process(): %s", ttx.what()); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 609 | server_->decrementActiveProcessors(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 610 | close(); |
| 611 | return; |
Bryan Duxbury | 1e98758 | 2011-08-25 17:33:03 +0000 | [diff] [blame] | 612 | } catch (const std::exception &x) { |
| 613 | GlobalOutput.printf("Server::process() uncaught exception: %s: %s", |
| 614 | typeid(x).name(), x.what()); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 615 | server_->decrementActiveProcessors(); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 616 | close(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 617 | return; |
| 618 | } catch (...) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 619 | GlobalOutput.printf("Server::process() unknown exception"); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 620 | server_->decrementActiveProcessors(); |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 621 | close(); |
| 622 | return; |
| 623 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 626 | // Intentionally fall through here, the call to process has written into |
| 627 | // the writeBuffer_ |
| 628 | |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 629 | case APP_WAIT_TASK: |
| 630 | // We have now finished processing a task and the result has been written |
| 631 | // into the outputTransport_, so we grab its contents and place them into |
| 632 | // the writeBuffer_ for actual writing by the libevent thread |
| 633 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 634 | server_->decrementActiveProcessors(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 635 | // Get the result of the operation |
| 636 | outputTransport_->getBuffer(&writeBuffer_, &writeBufferSize_); |
| 637 | |
| 638 | // If the function call generated return data, then move into the send |
| 639 | // state and get going |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 640 | // 4 bytes were reserved for frame size |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 641 | if (writeBufferSize_ > 4) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 642 | |
| 643 | // Move into write state |
| 644 | writeBufferPos_ = 0; |
| 645 | socketState_ = SOCKET_SEND; |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 646 | |
David Reiss | af78778 | 2008-07-03 20:29:34 +0000 | [diff] [blame] | 647 | // Put the frame size into the write buffer |
| 648 | int32_t frameSize = (int32_t)htonl(writeBufferSize_ - 4); |
| 649 | memcpy(writeBuffer_, &frameSize, 4); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 650 | |
| 651 | // Socket into write mode |
David Reiss | 52cb7a7 | 2008-06-30 21:40:35 +0000 | [diff] [blame] | 652 | appState_ = APP_SEND_RESULT; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 653 | setWrite(); |
| 654 | |
| 655 | // Try to work the socket immediately |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 656 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 657 | |
| 658 | return; |
| 659 | } |
| 660 | |
David Reiss | c51986f | 2009-03-24 20:01:25 +0000 | [diff] [blame] | 661 | // In this case, the request was oneway and we should fall through |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 662 | // right back into the read frame header state |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 663 | goto LABEL_APP_INIT; |
| 664 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 665 | case APP_SEND_RESULT: |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 666 | // it's now safe to perform buffer size housekeeping. |
| 667 | if (writeBufferSize_ > largestWriteBufferSize_) { |
| 668 | largestWriteBufferSize_ = writeBufferSize_; |
| 669 | } |
| 670 | if (server_->getResizeBufferEveryN() > 0 |
| 671 | && ++callsForResize_ >= server_->getResizeBufferEveryN()) { |
| 672 | checkIdleBufferMemLimit(server_->getIdleReadBufferLimit(), |
| 673 | server_->getIdleWriteBufferLimit()); |
| 674 | callsForResize_ = 0; |
| 675 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 676 | |
| 677 | // N.B.: We also intentionally fall through here into the INIT state! |
| 678 | |
Mark Slee | 92f00fb | 2006-10-25 01:28:17 +0000 | [diff] [blame] | 679 | LABEL_APP_INIT: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 680 | case APP_INIT: |
| 681 | |
| 682 | // Clear write buffer variables |
| 683 | writeBuffer_ = NULL; |
| 684 | writeBufferPos_ = 0; |
| 685 | writeBufferSize_ = 0; |
| 686 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 687 | // Into read4 state we go |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 688 | socketState_ = SOCKET_RECV_FRAMING; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 689 | appState_ = APP_READ_FRAME_SIZE; |
| 690 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 691 | readBufferPos_ = 0; |
| 692 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 693 | // Register read event |
| 694 | setRead(); |
David Reiss | 84e63ab | 2008-03-07 20:12:28 +0000 | [diff] [blame] | 695 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 696 | // Try to work the socket right away |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 697 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 698 | |
| 699 | return; |
| 700 | |
| 701 | case APP_READ_FRAME_SIZE: |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 702 | // We just read the request length |
| 703 | // Double the buffer size until it is big enough |
| 704 | if (readWant_ > readBufferSize_) { |
| 705 | if (readBufferSize_ == 0) { |
| 706 | readBufferSize_ = 1; |
| 707 | } |
| 708 | uint32_t newSize = readBufferSize_; |
| 709 | while (readWant_ > newSize) { |
| 710 | newSize *= 2; |
| 711 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 712 | |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 713 | uint8_t* newBuffer = (uint8_t*)std::realloc(readBuffer_, newSize); |
| 714 | if (newBuffer == NULL) { |
| 715 | // nothing else to be done... |
| 716 | throw std::bad_alloc(); |
| 717 | } |
| 718 | readBuffer_ = newBuffer; |
| 719 | readBufferSize_ = newSize; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 722 | readBufferPos_= 0; |
| 723 | |
| 724 | // Move into read request state |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 725 | socketState_ = SOCKET_RECV; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 726 | appState_ = APP_READ_REQUEST; |
| 727 | |
| 728 | // Work the socket right away |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 729 | // workSocket(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 730 | |
| 731 | return; |
| 732 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 733 | case APP_CLOSE_CONNECTION: |
| 734 | server_->decrementActiveProcessors(); |
| 735 | close(); |
| 736 | return; |
| 737 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 738 | default: |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 739 | GlobalOutput.printf("Unexpected Application State %d", appState_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 740 | assert(0); |
| 741 | } |
| 742 | } |
| 743 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 744 | void TNonblockingServer::TConnection::setFlags(short eventFlags) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 745 | // Catch the do nothing case |
| 746 | if (eventFlags_ == eventFlags) { |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | // Delete a previously existing event |
| 751 | if (eventFlags_ != 0) { |
| 752 | if (event_del(&event_) == -1) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 753 | GlobalOutput("TConnection::setFlags event_del"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // Update in memory structure |
| 759 | eventFlags_ = eventFlags; |
| 760 | |
Mark Slee | 402ee28 | 2007-08-23 01:43:20 +0000 | [diff] [blame] | 761 | // Do not call event_set if there are no flags |
| 762 | if (!eventFlags_) { |
| 763 | return; |
| 764 | } |
| 765 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 766 | /* |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 767 | * event_set: |
| 768 | * |
| 769 | * Prepares the event structure &event to be used in future calls to |
| 770 | * 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] | 771 | * eventHandler using the 'sock' file descriptor to monitor events. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 772 | * |
| 773 | * The events can be either EV_READ, EV_WRITE, or both, indicating |
| 774 | * that an application can read or write from the file respectively without |
| 775 | * blocking. |
| 776 | * |
Mark Slee | e02385b | 2007-06-09 01:21:16 +0000 | [diff] [blame] | 777 | * The eventHandler will be called with the file descriptor that triggered |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 778 | * the event and the type of event which will be one of: EV_TIMEOUT, |
| 779 | * EV_SIGNAL, EV_READ, EV_WRITE. |
| 780 | * |
| 781 | * The additional flag EV_PERSIST makes an event_add() persistent until |
| 782 | * event_del() has been called. |
| 783 | * |
| 784 | * Once initialized, the &event struct can be used repeatedly with |
| 785 | * 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] | 786 | * 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] | 787 | * when an ev structure has been added to libevent using event_add() the |
| 788 | * structure must persist until the event occurs (assuming EV_PERSIST |
| 789 | * is not set) or is removed using event_del(). You may not reuse the same |
| 790 | * ev structure for multiple monitored descriptors; each descriptor needs |
| 791 | * its own ev. |
| 792 | */ |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 793 | event_set(&event_, tSocket_->getSocketFD(), eventFlags_, |
| 794 | TConnection::eventHandler, this); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 795 | event_base_set(server_->getEventBase(), &event_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 796 | |
| 797 | // Add the event |
| 798 | if (event_add(&event_, 0) == -1) { |
Mark Slee | 17496a0 | 2007-08-02 06:37:40 +0000 | [diff] [blame] | 799 | GlobalOutput("TConnection::setFlags(): could not event_add"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Closes a connection |
| 805 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 806 | void TNonblockingServer::TConnection::close() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 807 | // Delete the registered libevent |
| 808 | if (event_del(&event_) == -1) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 809 | GlobalOutput.perror("TConnection::close() event_del", errno); |
| 810 | } |
| 811 | |
| 812 | if (serverEventHandler_ != NULL) { |
| 813 | serverEventHandler_->deleteContext(connectionContext_, inputProtocol_, outputProtocol_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // Close the socket |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 817 | tSocket_->close(); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 818 | |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 819 | // close any factory produced transports |
| 820 | factoryInputTransport_->close(); |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 821 | factoryOutputTransport_->close(); |
Aditya Agarwal | 1ea9052 | 2007-01-19 02:02:12 +0000 | [diff] [blame] | 822 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 823 | // Give this object back to the server that owns it |
| 824 | server_->returnConnection(this); |
| 825 | } |
| 826 | |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 827 | void TNonblockingServer::TConnection::checkIdleBufferMemLimit( |
| 828 | size_t readLimit, |
| 829 | size_t writeLimit) { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 830 | if (readLimit > 0 && readBufferSize_ > readLimit) { |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 831 | free(readBuffer_); |
| 832 | readBuffer_ = NULL; |
| 833 | readBufferSize_ = 0; |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 834 | } |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 835 | |
| 836 | if (writeLimit > 0 && largestWriteBufferSize_ > writeLimit) { |
| 837 | // just start over |
David Reiss | 89a1294 | 2010-10-06 17:10:52 +0000 | [diff] [blame] | 838 | outputTransport_->resetBuffer(server_->getWriteBufferDefaultSize()); |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 839 | largestWriteBufferSize_ = 0; |
| 840 | } |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 841 | } |
| 842 | |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 843 | TNonblockingServer::~TNonblockingServer() { |
| 844 | // TODO: We currently leak any active TConnection objects. |
| 845 | // Since we're shutting down and destroying the event_base, the TConnection |
| 846 | // objects will never receive any additional callbacks. (And even if they |
| 847 | // did, it would be bad, since they keep a pointer around to the server, |
| 848 | // which is being destroyed.) |
| 849 | |
| 850 | // Clean up unused TConnection objects in connectionStack_ |
| 851 | while (!connectionStack_.empty()) { |
| 852 | TConnection* connection = connectionStack_.top(); |
| 853 | connectionStack_.pop(); |
| 854 | delete connection; |
| 855 | } |
| 856 | |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 857 | if (eventBase_ && ownEventBase_) { |
David Reiss | 8ede818 | 2010-09-02 15:26:28 +0000 | [diff] [blame] | 858 | event_base_free(eventBase_); |
| 859 | } |
| 860 | |
| 861 | if (serverSocket_ >= 0) { |
| 862 | close(serverSocket_); |
| 863 | } |
| 864 | } |
| 865 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 866 | /** |
| 867 | * Creates a new connection either by reusing an object off the stack or |
| 868 | * by allocating a new one entirely |
| 869 | */ |
Bryan Duxbury | 526fa8e | 2011-08-29 20:28:23 +0000 | [diff] [blame] | 870 | TNonblockingServer::TConnection* TNonblockingServer::createConnection( |
| 871 | int socket, short flags, |
| 872 | const sockaddr* addr, |
| 873 | socklen_t addrLen) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 874 | // Check the stack |
| 875 | if (connectionStack_.empty()) { |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 876 | return new TConnection(socket, flags, this, addr, addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 877 | } else { |
| 878 | TConnection* result = connectionStack_.top(); |
| 879 | connectionStack_.pop(); |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 880 | result->init(socket, flags, this, addr, addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 881 | return result; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * Returns a connection to the stack |
| 887 | */ |
| 888 | void TNonblockingServer::returnConnection(TConnection* connection) { |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 889 | if (connectionStackLimit_ && |
| 890 | (connectionStack_.size() >= connectionStackLimit_)) { |
| 891 | delete connection; |
| 892 | } else { |
David Reiss | 54bec5d | 2010-10-06 17:10:45 +0000 | [diff] [blame] | 893 | connection->checkIdleBufferMemLimit(idleReadBufferLimit_, idleWriteBufferLimit_); |
Kevin Clark | cbcd63a | 2009-03-19 03:50:05 +0000 | [diff] [blame] | 894 | connectionStack_.push(connection); |
| 895 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /** |
David Reiss | a79e488 | 2008-03-05 07:51:47 +0000 | [diff] [blame] | 899 | * Server socket had something happen. We accept all waiting client |
| 900 | * connections on fd and assign TConnection objects to handle those requests. |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 901 | */ |
| 902 | void TNonblockingServer::handleEvent(int fd, short which) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 903 | (void) which; |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 904 | // Make sure that libevent didn't mess up the socket handles |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 905 | assert(fd == serverSocket_); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 906 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 907 | // Server socket accepted a new connection |
| 908 | socklen_t addrLen; |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 909 | sockaddr_storage addrStorage; |
| 910 | sockaddr* addrp = (sockaddr*)&addrStorage; |
| 911 | addrLen = sizeof(addrStorage); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 912 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 913 | // Going to accept a new client socket |
| 914 | int clientSocket; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 915 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 916 | // Accept as many new clients as possible, even though libevent signaled only |
| 917 | // one, this helps us to avoid having to go back into the libevent engine so |
| 918 | // many times |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 919 | while ((clientSocket = ::accept(fd, addrp, &addrLen)) != -1) { |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 920 | // If we're overloaded, take action here |
| 921 | if (overloadAction_ != T_OVERLOAD_NO_ACTION && serverOverloaded()) { |
| 922 | nConnectionsDropped_++; |
| 923 | nTotalConnectionsDropped_++; |
| 924 | if (overloadAction_ == T_OVERLOAD_CLOSE_ON_ACCEPT) { |
| 925 | close(clientSocket); |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 926 | return; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 927 | } else if (overloadAction_ == T_OVERLOAD_DRAIN_TASK_QUEUE) { |
| 928 | if (!drainPendingTask()) { |
| 929 | // Nothing left to discard, so we drop connection instead. |
| 930 | close(clientSocket); |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 931 | return; |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 935 | // Explicitly set this socket to NONBLOCK mode |
| 936 | int flags; |
| 937 | if ((flags = fcntl(clientSocket, F_GETFL, 0)) < 0 || |
| 938 | fcntl(clientSocket, F_SETFL, flags | O_NONBLOCK) < 0) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 939 | GlobalOutput.perror("thriftServerEventHandler: set O_NONBLOCK (fcntl) ", errno); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 940 | close(clientSocket); |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | // Create a new TConnection for this client socket. |
| 945 | TConnection* clientConnection = |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 946 | createConnection(clientSocket, EV_READ | EV_PERSIST, addrp, addrLen); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 947 | |
| 948 | // Fail fast if we could not create a TConnection object |
| 949 | if (clientConnection == NULL) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 950 | GlobalOutput.printf("thriftServerEventHandler: failed TConnection factory"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 951 | close(clientSocket); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | // Put this client connection into the proper state |
| 956 | clientConnection->transition(); |
David Reiss | 3e7fca4 | 2009-09-19 01:59:13 +0000 | [diff] [blame] | 957 | |
| 958 | // addrLen is written by the accept() call, so needs to be set before the next call. |
David Reiss | 105961d | 2010-10-06 17:10:17 +0000 | [diff] [blame] | 959 | addrLen = sizeof(addrStorage); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 960 | } |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 961 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 962 | // Done looping accept, now we have to make sure the error is due to |
| 963 | // blocking. Any other error is a problem |
| 964 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 965 | GlobalOutput.perror("thriftServerEventHandler: accept() ", errno); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 970 | * 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] | 971 | */ |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 972 | void TNonblockingServer::listenSocket() { |
| 973 | int s; |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 974 | struct addrinfo hints, *res, *res0; |
| 975 | int error; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 976 | |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 977 | char port[sizeof("65536") + 1]; |
| 978 | memset(&hints, 0, sizeof(hints)); |
| 979 | hints.ai_family = PF_UNSPEC; |
| 980 | hints.ai_socktype = SOCK_STREAM; |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 981 | hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 982 | sprintf(port, "%d", port_); |
| 983 | |
| 984 | // Wildcard address |
| 985 | error = getaddrinfo(NULL, port, &hints, &res0); |
| 986 | if (error) { |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 987 | string errStr = "TNonblockingServer::serve() getaddrinfo " + string(gai_strerror(error)); |
| 988 | GlobalOutput(errStr.c_str()); |
Mark Slee | fb4b514 | 2007-11-20 01:27:08 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
| 991 | |
| 992 | // Pick the ipv6 address first since ipv4 addresses can be mapped |
| 993 | // into ipv6 space. |
| 994 | for (res = res0; res; res = res->ai_next) { |
| 995 | if (res->ai_family == AF_INET6 || res->ai_next == NULL) |
| 996 | break; |
| 997 | } |
| 998 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 999 | // Create the server socket |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1000 | s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 1001 | if (s == -1) { |
| 1002 | freeaddrinfo(res0); |
| 1003 | throw TException("TNonblockingServer::serve() socket() -1"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 1006 | #ifdef IPV6_V6ONLY |
David Reiss | eee98be | 2010-03-09 05:20:10 +0000 | [diff] [blame] | 1007 | if (res->ai_family == AF_INET6) { |
| 1008 | int zero = 0; |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1009 | if (-1 == setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, const_cast_sockopt(&zero), sizeof(zero))) { |
David Reiss | eee98be | 2010-03-09 05:20:10 +0000 | [diff] [blame] | 1010 | GlobalOutput("TServerSocket::listen() IPV6_V6ONLY"); |
| 1011 | } |
David Reiss | 13aea46 | 2008-06-10 22:56:04 +0000 | [diff] [blame] | 1012 | } |
| 1013 | #endif // #ifdef IPV6_V6ONLY |
| 1014 | |
| 1015 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1016 | int one = 1; |
| 1017 | |
| 1018 | // Set reuseaddr to avoid 2MSL delay on server restart |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1019 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, const_cast_sockopt(&one), sizeof(one)); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1020 | |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1021 | if (::bind(s, res->ai_addr, res->ai_addrlen) == -1) { |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1022 | close(s); |
| 1023 | freeaddrinfo(res0); |
| 1024 | throw TException("TNonblockingServer::serve() bind"); |
| 1025 | } |
| 1026 | |
| 1027 | // Done with the addr info |
| 1028 | freeaddrinfo(res0); |
| 1029 | |
| 1030 | // Set up this file descriptor for listening |
| 1031 | listenSocket(s); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * Takes a socket created by listenSocket() and sets various options on it |
| 1036 | * to prepare for use in the server. |
| 1037 | */ |
| 1038 | void TNonblockingServer::listenSocket(int s) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1039 | // Set socket to nonblocking mode |
| 1040 | int flags; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1041 | if ((flags = fcntl(s, F_GETFL, 0)) < 0 || |
| 1042 | fcntl(s, F_SETFL, flags | O_NONBLOCK) < 0) { |
| 1043 | close(s); |
| 1044 | throw TException("TNonblockingServer::serve() O_NONBLOCK"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | int one = 1; |
| 1048 | struct linger ling = {0, 0}; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Keepalive to ensure full result flushing |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1051 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, const_cast_sockopt(&one), sizeof(one)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1052 | |
| 1053 | // Turn linger off to avoid hung sockets |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1054 | setsockopt(s, SOL_SOCKET, SO_LINGER, const_cast_sockopt(&ling), sizeof(ling)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Set TCP nodelay if available, MAC OS X Hack |
| 1057 | // See http://lists.danga.com/pipermail/memcached/2005-March/001240.html |
| 1058 | #ifndef TCP_NOPUSH |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1059 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, const_cast_sockopt(&one), sizeof(one)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1060 | #endif |
| 1061 | |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 1062 | #ifdef TCP_LOW_MIN_RTO |
| 1063 | if (TSocket::getUseLowMinRto()) { |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1064 | setsockopt(s, IPPROTO_TCP, TCP_LOW_MIN_RTO, const_cast_sockopt(&one), sizeof(one)); |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 1065 | } |
| 1066 | #endif |
| 1067 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1068 | if (listen(s, LISTEN_BACKLOG) == -1) { |
| 1069 | close(s); |
| 1070 | throw TException("TNonblockingServer::serve() listen"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1073 | // Cool, this socket is good to go, set it as the serverSocket_ |
| 1074 | serverSocket_ = s; |
| 1075 | } |
| 1076 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1077 | void TNonblockingServer::createNotificationPipe() { |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1078 | if(evutil_socketpair(AF_LOCAL, SOCK_STREAM, 0, notificationPipeFDs_) == -1) { |
| 1079 | GlobalOutput.perror("TNonblockingServer::createNotificationPipe ", EVUTIL_SOCKET_ERROR()); |
| 1080 | throw TException("can't create notification pipe"); |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1081 | } |
Roger Meier | 30aae0c | 2011-07-08 12:23:31 +0000 | [diff] [blame] | 1082 | if(evutil_make_socket_nonblocking(notificationPipeFDs_[0])<0 || |
| 1083 | evutil_make_socket_nonblocking(notificationPipeFDs_[1])<0) { |
David Reiss | 83b8fda | 2010-03-09 05:19:34 +0000 | [diff] [blame] | 1084 | close(notificationPipeFDs_[0]); |
| 1085 | close(notificationPipeFDs_[1]); |
| 1086 | throw TException("TNonblockingServer::createNotificationPipe() O_NONBLOCK"); |
| 1087 | } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1090 | /** |
| 1091 | * Register the core libevent events onto the proper base. |
| 1092 | */ |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 1093 | void TNonblockingServer::registerEvents(event_base* base, bool ownEventBase) { |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1094 | assert(serverSocket_ != -1); |
| 1095 | assert(!eventBase_); |
| 1096 | eventBase_ = base; |
Roger Meier | c190558 | 2011-08-02 23:37:36 +0000 | [diff] [blame] | 1097 | ownEventBase_ = ownEventBase; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1098 | |
| 1099 | // Print some libevent stats |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 1100 | GlobalOutput.printf("libevent %s method %s", |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1101 | event_get_version(), |
Bryan Duxbury | 37874ca | 2011-08-25 17:28:23 +0000 | [diff] [blame] | 1102 | event_base_get_method(eventBase_)); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1103 | |
| 1104 | // Register the server event |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1105 | event_set(&serverEvent_, |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1106 | serverSocket_, |
| 1107 | EV_READ | EV_PERSIST, |
| 1108 | TNonblockingServer::eventHandler, |
| 1109 | this); |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1110 | event_base_set(eventBase_, &serverEvent_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1111 | |
| 1112 | // Add the event and start up the server |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1113 | if (-1 == event_add(&serverEvent_, 0)) { |
| 1114 | throw TException("TNonblockingServer::serve(): coult not event_add"); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1115 | } |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1116 | if (threadPoolProcessing_) { |
| 1117 | // Create an event to be notified when a task finishes |
| 1118 | event_set(¬ificationEvent_, |
| 1119 | getNotificationRecvFD(), |
| 1120 | EV_READ | EV_PERSIST, |
| 1121 | TConnection::taskHandler, |
| 1122 | this); |
David Reiss | 1c20c87 | 2010-03-09 05:20:14 +0000 | [diff] [blame] | 1123 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1124 | // Attach to the base |
| 1125 | event_base_set(eventBase_, ¬ificationEvent_); |
| 1126 | |
| 1127 | // Add the event and start up the server |
| 1128 | if (-1 == event_add(¬ificationEvent_, 0)) { |
| 1129 | throw TException("TNonblockingServer::serve(): notification event_add fail"); |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1134 | void TNonblockingServer::setThreadManager(boost::shared_ptr<ThreadManager> threadManager) { |
| 1135 | threadManager_ = threadManager; |
| 1136 | if (threadManager != NULL) { |
| 1137 | threadManager->setExpireCallback(std::tr1::bind(&TNonblockingServer::expireClose, this, std::tr1::placeholders::_1)); |
| 1138 | threadPoolProcessing_ = true; |
| 1139 | } else { |
| 1140 | threadPoolProcessing_ = false; |
| 1141 | } |
| 1142 | } |
| 1143 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1144 | bool TNonblockingServer::serverOverloaded() { |
| 1145 | size_t activeConnections = numTConnections_ - connectionStack_.size(); |
| 1146 | if (numActiveProcessors_ > maxActiveProcessors_ || |
| 1147 | activeConnections > maxConnections_) { |
| 1148 | if (!overloaded_) { |
| 1149 | GlobalOutput.printf("thrift non-blocking server overload condition"); |
| 1150 | overloaded_ = true; |
| 1151 | } |
| 1152 | } else { |
| 1153 | if (overloaded_ && |
| 1154 | (numActiveProcessors_ <= overloadHysteresis_ * maxActiveProcessors_) && |
| 1155 | (activeConnections <= overloadHysteresis_ * maxConnections_)) { |
| 1156 | GlobalOutput.printf("thrift non-blocking server overload ended; %u dropped (%llu total)", |
| 1157 | nConnectionsDropped_, nTotalConnectionsDropped_); |
| 1158 | nConnectionsDropped_ = 0; |
| 1159 | overloaded_ = false; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | return overloaded_; |
| 1164 | } |
| 1165 | |
| 1166 | bool TNonblockingServer::drainPendingTask() { |
| 1167 | if (threadManager_) { |
| 1168 | boost::shared_ptr<Runnable> task = threadManager_->removeNextPending(); |
| 1169 | if (task) { |
| 1170 | TConnection* connection = |
| 1171 | static_cast<TConnection::Task*>(task.get())->getTConnection(); |
| 1172 | assert(connection && connection->getServer() |
| 1173 | && connection->getState() == APP_WAIT_TASK); |
| 1174 | connection->forceClose(); |
| 1175 | return true; |
| 1176 | } |
| 1177 | } |
| 1178 | return false; |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
David Reiss | 068f416 | 2010-03-09 05:19:45 +0000 | [diff] [blame] | 1181 | void TNonblockingServer::expireClose(boost::shared_ptr<Runnable> task) { |
| 1182 | TConnection* connection = |
| 1183 | static_cast<TConnection::Task*>(task.get())->getTConnection(); |
| 1184 | assert(connection && connection->getServer() |
| 1185 | && connection->getState() == APP_WAIT_TASK); |
| 1186 | connection->forceClose(); |
| 1187 | } |
| 1188 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1189 | /** |
| 1190 | * Main workhorse function, starts up the server listening on a port and |
| 1191 | * loops over the libevent handler. |
| 1192 | */ |
| 1193 | void TNonblockingServer::serve() { |
| 1194 | // Init socket |
| 1195 | listenSocket(); |
| 1196 | |
David Reiss | 01fe153 | 2010-03-09 05:19:25 +0000 | [diff] [blame] | 1197 | if (threadPoolProcessing_) { |
| 1198 | // Init task completion notification pipe |
| 1199 | createNotificationPipe(); |
| 1200 | } |
| 1201 | |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1202 | // Initialize libevent core |
Bryan Duxbury | 37874ca | 2011-08-25 17:28:23 +0000 | [diff] [blame] | 1203 | registerEvents(static_cast<event_base*>(event_base_new()), true); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1204 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 1205 | // Run the preServe event |
| 1206 | if (eventHandler_ != NULL) { |
| 1207 | eventHandler_->preServe(); |
dweatherford | 5898599 | 2007-06-19 23:10:19 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1210 | // Run libevent engine, invokes calls to eventHandler |
| 1211 | // Only returns if stop() is called. |
Mark Slee | 79b1694 | 2007-11-26 19:05:29 +0000 | [diff] [blame] | 1212 | event_base_loop(eventBase_, 0); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Bryan Duxbury | 76c4368 | 2011-08-24 21:26:48 +0000 | [diff] [blame] | 1215 | void TNonblockingServer::stop() { |
| 1216 | if (!eventBase_) { |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | // Call event_base_loopbreak() to tell libevent to exit the loop |
| 1221 | // |
| 1222 | // (The libevent documentation doesn't explicitly state that this function is |
| 1223 | // safe to call from another thread. However, all it does is set a variable, |
| 1224 | // in the event_base, so it should be fine.) |
| 1225 | event_base_loopbreak(eventBase_); |
| 1226 | |
| 1227 | // event_base_loopbreak() only causes the loop to exit the next time it wakes |
| 1228 | // up. We need to force it to wake up, in case there are no real events |
| 1229 | // it needs to process. |
| 1230 | // |
| 1231 | // Attempt to connect to the server socket. If anything fails, |
| 1232 | // we'll just have to wait until libevent wakes up on its own. |
| 1233 | // |
| 1234 | // First create a socket |
| 1235 | int fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1236 | if (fd < 0) { |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | // Set up the address |
| 1241 | struct sockaddr_in addr; |
| 1242 | addr.sin_family = AF_INET; |
| 1243 | addr.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 |
| 1244 | addr.sin_port = htons(port_); |
| 1245 | |
| 1246 | // Finally do the connect(). |
| 1247 | // We don't care about the return value; |
| 1248 | // we're just going to close the socket either way. |
| 1249 | connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)); |
| 1250 | close(fd); |
| 1251 | } |
| 1252 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 1253 | }}} // apache::thrift::server |