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