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