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