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