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