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 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 7 | #include "server/TThreadPoolServer.h" |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 8 | #include "transport/TTransportException.h" |
| 9 | #include "concurrency/Thread.h" |
| 10 | #include "concurrency/ThreadManager.h" |
| 11 | #include <string> |
| 12 | #include <iostream> |
| 13 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 14 | namespace facebook { namespace thrift { namespace server { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 15 | |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 16 | using boost::shared_ptr; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 17 | using namespace std; |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 18 | using namespace facebook::thrift; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 19 | using namespace facebook::thrift::concurrency; |
Mark Slee | 5ea15f9 | 2007-03-05 22:55:59 +0000 | [diff] [blame] | 20 | using namespace facebook::thrift::protocol;; |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 21 | using namespace facebook::thrift::transport; |
| 22 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 23 | class TThreadPoolServer::Task : public Runnable { |
| 24 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 25 | public: |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 26 | |
| 27 | Task(TThreadPoolServer &server, |
| 28 | shared_ptr<TProcessor> processor, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 29 | shared_ptr<TProtocol> input, |
| 30 | shared_ptr<TProtocol> output) : |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 31 | server_(server), |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 32 | processor_(processor), |
| 33 | input_(input), |
| 34 | output_(output) { |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 35 | } |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 36 | |
| 37 | ~Task() {} |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 38 | |
Mark Slee | eb0d024 | 2007-01-25 07:58:55 +0000 | [diff] [blame] | 39 | void run() { |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 40 | boost::shared_ptr<TServerEventHandler> eventHandler = |
| 41 | server_.getEventHandler(); |
| 42 | if (eventHandler != NULL) { |
| 43 | eventHandler->clientBegin(input_, output_); |
| 44 | } |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 45 | try { |
| 46 | while (processor_->process(input_, output_)) { |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 47 | if (!input_->getTransport()->peek()) { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 48 | break; |
| 49 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 50 | } |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 51 | } catch (TTransportException& ttx) { |
Martin Kraemer | ee341cb | 2007-02-05 21:40:38 +0000 | [diff] [blame] | 52 | // This is reasonably expected, client didn't send a full request so just |
| 53 | // ignore him |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 54 | // string errStr = string("TThreadPoolServer client died: ") + ttx.what(); |
| 55 | // GlobalOutput(errStr.c_str()); |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 56 | } catch (TException& x) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 57 | string errStr = string("TThreadPoolServer exception: ") + x.what(); |
| 58 | GlobalOutput(errStr.c_str()); |
pfung | 78ee85c | 2007-12-13 22:30:47 +0000 | [diff] [blame] | 59 | } catch (std::exception &x) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 60 | string errStr = string("TThreadPoolServer, std::exception: ") + x.what(); |
| 61 | GlobalOutput(errStr.c_str()); |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 62 | } |
pfung | e8abada | 2008-01-05 23:23:53 +0000 | [diff] [blame] | 63 | |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 64 | if (eventHandler != NULL) { |
| 65 | eventHandler->clientEnd(input_, output_); |
| 66 | } |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 67 | |
| 68 | try { |
| 69 | input_->getTransport()->close(); |
| 70 | } catch (TTransportException& ttx) { |
| 71 | string errStr = string("TThreadPoolServer input close failed: ") + ttx.what(); |
| 72 | GlobalOutput(errStr.c_str()); |
| 73 | } |
| 74 | try { |
| 75 | output_->getTransport()->close(); |
| 76 | } catch (TTransportException& ttx) { |
| 77 | string errStr = string("TThreadPoolServer output close failed: ") + ttx.what(); |
| 78 | GlobalOutput(errStr.c_str()); |
| 79 | } |
| 80 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 81 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 84 | TServer& server_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 85 | shared_ptr<TProcessor> processor_; |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 86 | shared_ptr<TProtocol> input_; |
| 87 | shared_ptr<TProtocol> output_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 88 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 89 | }; |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 90 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 91 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 92 | shared_ptr<TServerTransport> serverTransport, |
| 93 | shared_ptr<TTransportFactory> transportFactory, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 94 | shared_ptr<TProtocolFactory> protocolFactory, |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 95 | shared_ptr<ThreadManager> threadManager) : |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 96 | TServer(processor, serverTransport, transportFactory, protocolFactory), |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 97 | threadManager_(threadManager), |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 98 | stop_(false), timeout_(0) {} |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 99 | |
| 100 | TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor, |
| 101 | shared_ptr<TServerTransport> serverTransport, |
| 102 | shared_ptr<TTransportFactory> inputTransportFactory, |
| 103 | shared_ptr<TTransportFactory> outputTransportFactory, |
| 104 | shared_ptr<TProtocolFactory> inputProtocolFactory, |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 105 | shared_ptr<TProtocolFactory> outputProtocolFactory, |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 106 | shared_ptr<ThreadManager> threadManager) : |
| 107 | TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, |
| 108 | inputProtocolFactory, outputProtocolFactory), |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 109 | threadManager_(threadManager), |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 110 | stop_(false), timeout_(0) {} |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 111 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 112 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 113 | TThreadPoolServer::~TThreadPoolServer() {} |
| 114 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 115 | void TThreadPoolServer::serve() { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 116 | shared_ptr<TTransport> client; |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 117 | shared_ptr<TTransport> inputTransport; |
| 118 | shared_ptr<TTransport> outputTransport; |
| 119 | shared_ptr<TProtocol> inputProtocol; |
| 120 | shared_ptr<TProtocol> outputProtocol; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 121 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 122 | try { |
| 123 | // Start the server listening |
| 124 | serverTransport_->listen(); |
| 125 | } catch (TTransportException& ttx) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 126 | string errStr = string("TThreadPoolServer::run() listen(): ") + ttx.what(); |
| 127 | GlobalOutput(errStr.c_str()); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 128 | return; |
| 129 | } |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 130 | |
| 131 | // Run the preServe event |
| 132 | if (eventHandler_ != NULL) { |
| 133 | eventHandler_->preServe(); |
| 134 | } |
| 135 | |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 136 | while (!stop_) { |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 137 | try { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 138 | client.reset(); |
| 139 | inputTransport.reset(); |
| 140 | outputTransport.reset(); |
| 141 | inputProtocol.reset(); |
| 142 | outputProtocol.reset(); |
| 143 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 144 | // Fetch client from server |
| 145 | client = serverTransport_->accept(); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 146 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 147 | // Make IO transports |
Aditya Agarwal | 9abb0d6 | 2007-01-24 22:53:54 +0000 | [diff] [blame] | 148 | inputTransport = inputTransportFactory_->getTransport(client); |
| 149 | outputTransport = outputTransportFactory_->getTransport(client); |
| 150 | inputProtocol = inputProtocolFactory_->getProtocol(inputTransport); |
| 151 | outputProtocol = outputProtocolFactory_->getProtocol(outputTransport); |
Mark Slee | 4af6ed7 | 2006-10-25 19:02:49 +0000 | [diff] [blame] | 152 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 153 | // Add to threadmanager pool |
Mark Slee | b4d3e7b | 2007-11-28 01:51:43 +0000 | [diff] [blame] | 154 | threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(*this, processor_, inputProtocol, outputProtocol)), timeout_); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 155 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 156 | } catch (TTransportException& ttx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 157 | if (inputTransport != NULL) { inputTransport->close(); } |
| 158 | if (outputTransport != NULL) { outputTransport->close(); } |
| 159 | if (client != NULL) { client->close(); } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 160 | if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 161 | string errStr = string("TThreadPoolServer: TServerTransport died on accept: ") + ttx.what(); |
| 162 | GlobalOutput(errStr.c_str()); |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 163 | } |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 164 | continue; |
| 165 | } catch (TException& tx) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 166 | if (inputTransport != NULL) { inputTransport->close(); } |
| 167 | if (outputTransport != NULL) { outputTransport->close(); } |
| 168 | if (client != NULL) { client->close(); } |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 169 | string errStr = string("TThreadPoolServer: Caught TException: ") + tx.what(); |
| 170 | GlobalOutput(errStr.c_str()); |
Aditya Agarwal | fdef47e | 2007-02-07 03:54:18 +0000 | [diff] [blame] | 171 | continue; |
| 172 | } catch (string s) { |
Mark Slee | 3303f36 | 2007-03-05 20:09:37 +0000 | [diff] [blame] | 173 | if (inputTransport != NULL) { inputTransport->close(); } |
| 174 | if (outputTransport != NULL) { outputTransport->close(); } |
| 175 | if (client != NULL) { client->close(); } |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 176 | string errStr = "TThreadPoolServer: Unknown exception: " + s; |
| 177 | GlobalOutput(errStr.c_str()); |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 181 | |
| 182 | // If stopped manually, join the existing threads |
| 183 | if (stop_) { |
| 184 | try { |
| 185 | serverTransport_->close(); |
| 186 | threadManager_->join(); |
| 187 | } catch (TException &tx) { |
Mark Slee | 2e8a8d4 | 2008-01-16 00:38:20 +0000 | [diff] [blame^] | 188 | string errStr = string("TThreadPoolServer: Exception shutting down: ") + tx.what(); |
| 189 | GlobalOutput(errStr.c_str()); |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 190 | } |
Mark Slee | a5a783f | 2007-03-02 19:41:08 +0000 | [diff] [blame] | 191 | stop_ = false; |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 192 | } |
Mark Slee | 6e3f637 | 2007-03-01 22:05:46 +0000 | [diff] [blame] | 193 | |
Marc Slemko | 1669885 | 2006-08-04 03:16:10 +0000 | [diff] [blame] | 194 | } |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 195 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 196 | int64_t TThreadPoolServer::getTimeout() const { |
| 197 | return timeout_; |
| 198 | } |
| 199 | |
| 200 | void TThreadPoolServer::setTimeout(int64_t value) { |
| 201 | timeout_ = value; |
| 202 | } |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 203 | |
Marc Slemko | 3545234 | 2006-08-03 19:01:37 +0000 | [diff] [blame] | 204 | }}} // facebook::thrift::server |