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