blob: 6eea3dbde226801f07f0d66eee474b5784afc38d [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Marc Slemko35452342006-08-03 19:01:37 +000020#include "server/TThreadPoolServer.h"
Marc Slemko35452342006-08-03 19:01:37 +000021#include "transport/TTransportException.h"
22#include "concurrency/Thread.h"
23#include "concurrency/ThreadManager.h"
24#include <string>
25#include <iostream>
26
T Jake Lucianib5e62212009-01-31 22:36:20 +000027namespace apache { namespace thrift { namespace server {
Marc Slemko35452342006-08-03 19:01:37 +000028
Mark Slee5ea15f92007-03-05 22:55:59 +000029using boost::shared_ptr;
Marc Slemko35452342006-08-03 19:01:37 +000030using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000031using namespace apache::thrift;
32using namespace apache::thrift::concurrency;
33using namespace apache::thrift::protocol;;
34using namespace apache::thrift::transport;
Marc Slemko35452342006-08-03 19:01:37 +000035
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000036class TThreadPoolServer::Task : public Runnable {
37
Marc Slemko16698852006-08-04 03:16:10 +000038public:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000039
40 Task(TThreadPoolServer &server,
41 shared_ptr<TProcessor> processor,
Mark Slee4af6ed72006-10-25 19:02:49 +000042 shared_ptr<TProtocol> input,
43 shared_ptr<TProtocol> output) :
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000044 server_(server),
Mark Slee2f6404d2006-10-10 01:37:40 +000045 processor_(processor),
46 input_(input),
47 output_(output) {
Marc Slemko35452342006-08-03 19:01:37 +000048 }
Marc Slemko16698852006-08-04 03:16:10 +000049
50 ~Task() {}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000051
Mark Sleeeb0d0242007-01-25 07:58:55 +000052 void run() {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000053 boost::shared_ptr<TServerEventHandler> eventHandler =
54 server_.getEventHandler();
55 if (eventHandler != NULL) {
56 eventHandler->clientBegin(input_, output_);
57 }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000058 try {
59 while (processor_->process(input_, output_)) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000060 if (!input_->getTransport()->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000061 break;
62 }
Marc Slemko35452342006-08-03 19:01:37 +000063 }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000064 } catch (TTransportException& ttx) {
Martin Kraemeree341cb2007-02-05 21:40:38 +000065 // This is reasonably expected, client didn't send a full request so just
66 // ignore him
Mark Slee2e8a8d42008-01-16 00:38:20 +000067 // string errStr = string("TThreadPoolServer client died: ") + ttx.what();
68 // GlobalOutput(errStr.c_str());
Mark Sleeb9ff32a2006-11-16 01:00:24 +000069 } catch (TException& x) {
Mark Slee2e8a8d42008-01-16 00:38:20 +000070 string errStr = string("TThreadPoolServer exception: ") + x.what();
71 GlobalOutput(errStr.c_str());
pfung78ee85c2007-12-13 22:30:47 +000072 } catch (std::exception &x) {
Mark Slee2e8a8d42008-01-16 00:38:20 +000073 string errStr = string("TThreadPoolServer, std::exception: ") + x.what();
74 GlobalOutput(errStr.c_str());
David Reiss58e4d2c2010-03-09 05:19:43 +000075 } catch (...) {
76 GlobalOutput("TThreadPoolServer, unexpected exception in "
77 "TThreadPoolServer::Task::run()");
Marc Slemko35452342006-08-03 19:01:37 +000078 }
pfunge8abada2008-01-05 23:23:53 +000079
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000080 if (eventHandler != NULL) {
81 eventHandler->clientEnd(input_, output_);
82 }
Mark Slee2e8a8d42008-01-16 00:38:20 +000083
84 try {
85 input_->getTransport()->close();
86 } catch (TTransportException& ttx) {
87 string errStr = string("TThreadPoolServer input close failed: ") + ttx.what();
88 GlobalOutput(errStr.c_str());
89 }
90 try {
91 output_->getTransport()->close();
92 } catch (TTransportException& ttx) {
93 string errStr = string("TThreadPoolServer output close failed: ") + ttx.what();
94 GlobalOutput(errStr.c_str());
95 }
96
Marc Slemko35452342006-08-03 19:01:37 +000097 }
Mark Slee2f6404d2006-10-10 01:37:40 +000098
99 private:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000100 TServer& server_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000101 shared_ptr<TProcessor> processor_;
Mark Slee4af6ed72006-10-25 19:02:49 +0000102 shared_ptr<TProtocol> input_;
103 shared_ptr<TProtocol> output_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000104
Marc Slemko35452342006-08-03 19:01:37 +0000105};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000106
Marc Slemko16698852006-08-04 03:16:10 +0000107TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
Mark Sleed788b2e2006-09-07 01:26:35 +0000108 shared_ptr<TServerTransport> serverTransport,
109 shared_ptr<TTransportFactory> transportFactory,
Mark Slee4af6ed72006-10-25 19:02:49 +0000110 shared_ptr<TProtocolFactory> protocolFactory,
Mark Slee4af6ed72006-10-25 19:02:49 +0000111 shared_ptr<ThreadManager> threadManager) :
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000112 TServer(processor, serverTransport, transportFactory, protocolFactory),
Mark Slee6e3f6372007-03-01 22:05:46 +0000113 threadManager_(threadManager),
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000114 stop_(false), timeout_(0) {}
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000115
116TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
117 shared_ptr<TServerTransport> serverTransport,
118 shared_ptr<TTransportFactory> inputTransportFactory,
119 shared_ptr<TTransportFactory> outputTransportFactory,
120 shared_ptr<TProtocolFactory> inputProtocolFactory,
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000121 shared_ptr<TProtocolFactory> outputProtocolFactory,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000122 shared_ptr<ThreadManager> threadManager) :
123 TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory,
124 inputProtocolFactory, outputProtocolFactory),
Mark Slee6e3f6372007-03-01 22:05:46 +0000125 threadManager_(threadManager),
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000126 stop_(false), timeout_(0) {}
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000127
Mark Sleed788b2e2006-09-07 01:26:35 +0000128
Marc Slemko16698852006-08-04 03:16:10 +0000129TThreadPoolServer::~TThreadPoolServer() {}
130
Mark Slee794993d2006-09-20 01:56:10 +0000131void TThreadPoolServer::serve() {
Mark Sleed788b2e2006-09-07 01:26:35 +0000132 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000133 shared_ptr<TTransport> inputTransport;
134 shared_ptr<TTransport> outputTransport;
135 shared_ptr<TProtocol> inputProtocol;
136 shared_ptr<TProtocol> outputProtocol;
Mark Sleed788b2e2006-09-07 01:26:35 +0000137
Marc Slemko16698852006-08-04 03:16:10 +0000138 try {
139 // Start the server listening
140 serverTransport_->listen();
141 } catch (TTransportException& ttx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000142 string errStr = string("TThreadPoolServer::run() listen(): ") + ttx.what();
143 GlobalOutput(errStr.c_str());
Marc Slemko16698852006-08-04 03:16:10 +0000144 return;
145 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000146
147 // Run the preServe event
148 if (eventHandler_ != NULL) {
149 eventHandler_->preServe();
150 }
151
Mark Slee6e3f6372007-03-01 22:05:46 +0000152 while (!stop_) {
Marc Slemko16698852006-08-04 03:16:10 +0000153 try {
Mark Slee3303f362007-03-05 20:09:37 +0000154 client.reset();
155 inputTransport.reset();
156 outputTransport.reset();
157 inputProtocol.reset();
158 outputProtocol.reset();
159
Mark Sleed788b2e2006-09-07 01:26:35 +0000160 // Fetch client from server
161 client = serverTransport_->accept();
Mark Sleea5a783f2007-03-02 19:41:08 +0000162
Mark Sleed788b2e2006-09-07 01:26:35 +0000163 // Make IO transports
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000164 inputTransport = inputTransportFactory_->getTransport(client);
165 outputTransport = outputTransportFactory_->getTransport(client);
166 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
167 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Slee4af6ed72006-10-25 19:02:49 +0000168
Mark Sleed788b2e2006-09-07 01:26:35 +0000169 // Add to threadmanager pool
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000170 threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(*this, processor_, inputProtocol, outputProtocol)), timeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000171
Marc Slemko16698852006-08-04 03:16:10 +0000172 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +0000173 if (inputTransport != NULL) { inputTransport->close(); }
174 if (outputTransport != NULL) { outputTransport->close(); }
175 if (client != NULL) { client->close(); }
Mark Sleea5a783f2007-03-02 19:41:08 +0000176 if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000177 string errStr = string("TThreadPoolServer: TServerTransport died on accept: ") + ttx.what();
178 GlobalOutput(errStr.c_str());
Mark Sleea5a783f2007-03-02 19:41:08 +0000179 }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000180 continue;
181 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +0000182 if (inputTransport != NULL) { inputTransport->close(); }
183 if (outputTransport != NULL) { outputTransport->close(); }
184 if (client != NULL) { client->close(); }
Mark Slee2e8a8d42008-01-16 00:38:20 +0000185 string errStr = string("TThreadPoolServer: Caught TException: ") + tx.what();
186 GlobalOutput(errStr.c_str());
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000187 continue;
188 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +0000189 if (inputTransport != NULL) { inputTransport->close(); }
190 if (outputTransport != NULL) { outputTransport->close(); }
191 if (client != NULL) { client->close(); }
Mark Slee2e8a8d42008-01-16 00:38:20 +0000192 string errStr = "TThreadPoolServer: Unknown exception: " + s;
193 GlobalOutput(errStr.c_str());
Marc Slemko16698852006-08-04 03:16:10 +0000194 break;
195 }
196 }
Mark Slee6e3f6372007-03-01 22:05:46 +0000197
198 // If stopped manually, join the existing threads
199 if (stop_) {
200 try {
201 serverTransport_->close();
202 threadManager_->join();
203 } catch (TException &tx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000204 string errStr = string("TThreadPoolServer: Exception shutting down: ") + tx.what();
205 GlobalOutput(errStr.c_str());
Mark Slee6e3f6372007-03-01 22:05:46 +0000206 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000207 stop_ = false;
Mark Slee6e3f6372007-03-01 22:05:46 +0000208 }
Mark Slee6e3f6372007-03-01 22:05:46 +0000209
Marc Slemko16698852006-08-04 03:16:10 +0000210}
Marc Slemko35452342006-08-03 19:01:37 +0000211
Mark Slee9b82d272007-05-23 05:16:07 +0000212int64_t TThreadPoolServer::getTimeout() const {
213 return timeout_;
214}
215
216void TThreadPoolServer::setTimeout(int64_t value) {
217 timeout_ = value;
218}
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000219
T Jake Lucianib5e62212009-01-31 22:36:20 +0000220}}} // apache::thrift::server