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 | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 20 | #include "server/TThreadedServer.h" |
| 21 | #include "transport/TTransportException.h" |
| 22 | #include "concurrency/PosixThreadFactory.h" |
| 23 | |
| 24 | #include <string> |
| 25 | #include <iostream> |
| 26 | #include <pthread.h> |
| 27 | #include <unistd.h> |
| 28 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 29 | namespace apache { namespace thrift { namespace server { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 30 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 31 | using boost::shared_ptr; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 32 | using namespace std; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 33 | using namespace apache::thrift; |
| 34 | using namespace apache::thrift::protocol; |
| 35 | using namespace apache::thrift::transport; |
| 36 | using namespace apache::thrift::concurrency; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 37 | |
| 38 | class TThreadedServer::Task: public Runnable { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 39 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 40 | public: |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 41 | |
| 42 | Task(TThreadedServer& server, |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 43 | shared_ptr<TProcessor> processor, |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 44 | shared_ptr<TProtocol> input, |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 45 | shared_ptr<TProtocol> output, |
| 46 | shared_ptr<TTransport> transport) : |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 47 | server_(server), |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 48 | processor_(processor), |
| 49 | input_(input), |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 50 | output_(output), |
| 51 | transport_(transport) { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ~Task() {} |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 55 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 56 | void run() { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 57 | boost::shared_ptr<TServerEventHandler> eventHandler = |
| 58 | server_.getEventHandler(); |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 59 | void* connectionContext = NULL; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 60 | if (eventHandler != NULL) { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 61 | connectionContext = eventHandler->createContext(input_, output_); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 62 | } |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 63 | try { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 64 | for (;;) { |
| 65 | if (eventHandler != NULL) { |
| 66 | eventHandler->processContext(connectionContext, transport_); |
| 67 | } |
| 68 | if (!processor_->process(input_, output_, connectionContext) || |
| 69 | !input_->getTransport()->peek()) { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | } |
| 73 | } catch (TTransportException& ttx) { |
Bryan Duxbury | 756d73e | 2011-08-25 17:30:21 +0000 | [diff] [blame^] | 74 | if (ttx.getType() != TTransportException::END_OF_FILE) { |
| 75 | string errStr = string("TThreadedServer client died: ") + ttx.what(); |
| 76 | GlobalOutput(errStr.c_str()); |
| 77 | } |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 78 | } catch (TException& x) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 79 | string errStr = string("TThreadedServer exception: ") + x.what(); |
| 80 | GlobalOutput(errStr.c_str()); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 81 | } catch (...) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 82 | GlobalOutput("TThreadedServer uncaught exception."); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 83 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 84 | if (eventHandler != NULL) { |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 85 | eventHandler->deleteContext(connectionContext, input_, output_); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 86 | } |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 87 | |
| 88 | try { |
| 89 | input_->getTransport()->close(); |
| 90 | } catch (TTransportException& ttx) { |
| 91 | string errStr = string("TThreadedServer input close failed: ") + ttx.what(); |
| 92 | GlobalOutput(errStr.c_str()); |
| 93 | } |
| 94 | try { |
| 95 | output_->getTransport()->close(); |
| 96 | } catch (TTransportException& ttx) { |
| 97 | string errStr = string("TThreadedServer output close failed: ") + ttx.what(); |
| 98 | GlobalOutput(errStr.c_str()); |
| 99 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 100 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 101 | // Remove this task from parent bookkeeping |
| 102 | { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 103 | Synchronized s(server_.tasksMonitor_); |
| 104 | server_.tasks_.erase(this); |
| 105 | if (server_.tasks_.empty()) { |
| 106 | server_.tasksMonitor_.notify(); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private: |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 113 | TThreadedServer& server_; |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 114 | friend class TThreadedServer; |
| 115 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 116 | shared_ptr<TProcessor> processor_; |
| 117 | shared_ptr<TProtocol> input_; |
| 118 | shared_ptr<TProtocol> output_; |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 119 | shared_ptr<TTransport> transport_; |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | |
| 123 | TThreadedServer::TThreadedServer(shared_ptr<TProcessor> processor, |
| 124 | shared_ptr<TServerTransport> serverTransport, |
| 125 | shared_ptr<TTransportFactory> transportFactory, |
| 126 | shared_ptr<TProtocolFactory> protocolFactory): |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 127 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
| 128 | stop_(false) { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 129 | threadFactory_ = shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); |
| 130 | } |
| 131 | |
David Reiss | 45d5696 | 2009-03-14 23:35:16 +0000 | [diff] [blame] | 132 | TThreadedServer::TThreadedServer(boost::shared_ptr<TProcessor> processor, |
| 133 | boost::shared_ptr<TServerTransport> serverTransport, |
| 134 | boost::shared_ptr<TTransportFactory> transportFactory, |
| 135 | boost::shared_ptr<TProtocolFactory> protocolFactory, |
| 136 | boost::shared_ptr<ThreadFactory> threadFactory): |
| 137 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
| 138 | threadFactory_(threadFactory), |
| 139 | stop_(false) { |
| 140 | } |
| 141 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 142 | TThreadedServer::~TThreadedServer() {} |
| 143 | |
| 144 | void TThreadedServer::serve() { |
| 145 | |
| 146 | shared_ptr<TTransport> client; |
| 147 | shared_ptr<TTransport> inputTransport; |
| 148 | shared_ptr<TTransport> outputTransport; |
| 149 | shared_ptr<TProtocol> inputProtocol; |
| 150 | shared_ptr<TProtocol> outputProtocol; |
| 151 | |
| 152 | try { |
| 153 | // Start the server listening |
| 154 | serverTransport_->listen(); |
| 155 | } catch (TTransportException& ttx) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 156 | string errStr = string("TThreadedServer::run() listen(): ") +ttx.what(); |
| 157 | GlobalOutput(errStr.c_str()); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 161 | // Run the preServe event |
| 162 | if (eventHandler_ != NULL) { |
| 163 | eventHandler_->preServe(); |
| 164 | } |
| 165 | |
| 166 | while (!stop_) { |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 167 | try { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 168 | client.reset(); |
| 169 | inputTransport.reset(); |
| 170 | outputTransport.reset(); |
| 171 | inputProtocol.reset(); |
| 172 | outputProtocol.reset(); |
| 173 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 174 | // Fetch client from server |
| 175 | client = serverTransport_->accept(); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 176 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 177 | // Make IO transports |
| 178 | inputTransport = inputTransportFactory_->getTransport(client); |
| 179 | outputTransport = outputTransportFactory_->getTransport(client); |
| 180 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 181 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
| 182 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 183 | TThreadedServer::Task* task = new TThreadedServer::Task(*this, |
| 184 | processor_, |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 185 | inputProtocol, |
David Reiss | 2324871 | 2010-10-06 17:10:08 +0000 | [diff] [blame] | 186 | outputProtocol, |
| 187 | client); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 188 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 189 | // Create a task |
| 190 | shared_ptr<Runnable> runnable = |
| 191 | shared_ptr<Runnable>(task); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 192 | |
| 193 | // Create a thread for this task |
| 194 | shared_ptr<Thread> thread = |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 195 | shared_ptr<Thread>(threadFactory_->newThread(runnable)); |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 196 | |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 197 | // Insert thread into the set of threads |
| 198 | { |
| 199 | Synchronized s(tasksMonitor_); |
| 200 | tasks_.insert(task); |
| 201 | } |
| 202 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 203 | // Start the thread! |
| 204 | thread->start(); |
| 205 | |
| 206 | } catch (TTransportException& ttx) { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 207 | if (inputTransport != NULL) { inputTransport->close(); } |
| 208 | if (outputTransport != NULL) { outputTransport->close(); } |
| 209 | if (client != NULL) { client->close(); } |
| 210 | if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 211 | string errStr = string("TThreadedServer: TServerTransport died on accept: ") + ttx.what(); |
| 212 | GlobalOutput(errStr.c_str()); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 213 | } |
Mark Slee | 907e3d6 | 2007-02-08 22:29:24 +0000 | [diff] [blame] | 214 | continue; |
| 215 | } catch (TException& tx) { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 216 | if (inputTransport != NULL) { inputTransport->close(); } |
| 217 | if (outputTransport != NULL) { outputTransport->close(); } |
| 218 | if (client != NULL) { client->close(); } |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 219 | string errStr = string("TThreadedServer: Caught TException: ") + tx.what(); |
| 220 | GlobalOutput(errStr.c_str()); |
Mark Slee | 907e3d6 | 2007-02-08 22:29:24 +0000 | [diff] [blame] | 221 | continue; |
| 222 | } catch (string s) { |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 223 | if (inputTransport != NULL) { inputTransport->close(); } |
| 224 | if (outputTransport != NULL) { outputTransport->close(); } |
| 225 | if (client != NULL) { client->close(); } |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 226 | string errStr = "TThreadedServer: Unknown exception: " + s; |
| 227 | GlobalOutput(errStr.c_str()); |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | } |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 231 | |
| 232 | // If stopped manually, make sure to close server transport |
| 233 | if (stop_) { |
| 234 | try { |
| 235 | serverTransport_->close(); |
| 236 | } catch (TException &tx) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 237 | string errStr = string("TThreadedServer: Exception shutting down: ") + tx.what(); |
| 238 | GlobalOutput(errStr.c_str()); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 239 | } |
| 240 | try { |
| 241 | Synchronized s(tasksMonitor_); |
| 242 | while (!tasks_.empty()) { |
| 243 | tasksMonitor_.wait(); |
| 244 | } |
| 245 | } catch (TException &tx) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame] | 246 | string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what(); |
| 247 | GlobalOutput(errStr.c_str()); |
Mark Slee | 1d4ce80 | 2007-03-07 05:16:16 +0000 | [diff] [blame] | 248 | } |
| 249 | stop_ = false; |
| 250 | } |
| 251 | |
Mark Slee | b3cb629 | 2007-02-01 22:55:00 +0000 | [diff] [blame] | 252 | } |
| 253 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 254 | }}} // apache::thrift::server |