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