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