blob: 9f710f5e0ea53d149c9b3e5a444ad20981fcefba [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Slemko35452342006-08-03 19:01:37 +00007#include "server/TThreadPoolServer.h"
Marc Slemko35452342006-08-03 19:01:37 +00008#include "transport/TTransportException.h"
9#include "concurrency/Thread.h"
10#include "concurrency/ThreadManager.h"
11#include <string>
12#include <iostream>
13
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000014namespace facebook { namespace thrift { namespace server {
Marc Slemko35452342006-08-03 19:01:37 +000015
Mark Slee5ea15f92007-03-05 22:55:59 +000016using boost::shared_ptr;
Marc Slemko35452342006-08-03 19:01:37 +000017using namespace std;
Mark Sleeb9ff32a2006-11-16 01:00:24 +000018using namespace facebook::thrift;
Marc Slemko35452342006-08-03 19:01:37 +000019using namespace facebook::thrift::concurrency;
Mark Slee5ea15f92007-03-05 22:55:59 +000020using namespace facebook::thrift::protocol;;
Marc Slemko35452342006-08-03 19:01:37 +000021using namespace facebook::thrift::transport;
22
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000023class TThreadPoolServer::Task : public Runnable {
24
Marc Slemko16698852006-08-04 03:16:10 +000025public:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000026
27 Task(TThreadPoolServer &server,
28 shared_ptr<TProcessor> processor,
Mark Slee4af6ed72006-10-25 19:02:49 +000029 shared_ptr<TProtocol> input,
30 shared_ptr<TProtocol> output) :
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000031 server_(server),
Mark Slee2f6404d2006-10-10 01:37:40 +000032 processor_(processor),
33 input_(input),
34 output_(output) {
Marc Slemko35452342006-08-03 19:01:37 +000035 }
Marc Slemko16698852006-08-04 03:16:10 +000036
37 ~Task() {}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000038
Mark Sleeeb0d0242007-01-25 07:58:55 +000039 void run() {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000040 boost::shared_ptr<TServerEventHandler> eventHandler =
41 server_.getEventHandler();
42 if (eventHandler != NULL) {
43 eventHandler->clientBegin(input_, output_);
44 }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000045 try {
46 while (processor_->process(input_, output_)) {
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000047 if (!input_->getTransport()->peek()) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000048 break;
49 }
Marc Slemko35452342006-08-03 19:01:37 +000050 }
Mark Sleeb9ff32a2006-11-16 01:00:24 +000051 } catch (TTransportException& ttx) {
Martin Kraemeree341cb2007-02-05 21:40:38 +000052 // This is reasonably expected, client didn't send a full request so just
53 // ignore him
Martin Kraemerac62f942007-03-30 18:41:48 +000054 //cerr << "TThreadPoolServer client died: " << ttx.what() << endl;
Mark Sleeb9ff32a2006-11-16 01:00:24 +000055 } catch (TException& x) {
56 cerr << "TThreadPoolServer exception: " << x.what() << endl;
pfung78ee85c2007-12-13 22:30:47 +000057 } catch (std::exception &x) {
58 cerr << "TThreadPoolServer, std::exception: " << x.what() << endl;
Mark Sleeb9ff32a2006-11-16 01:00:24 +000059 } catch (...) {
60 cerr << "TThreadPoolServer uncaught exception." << endl;
Marc Slemko35452342006-08-03 19:01:37 +000061 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000062 if (eventHandler != NULL) {
63 eventHandler->clientEnd(input_, output_);
64 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000065 input_->getTransport()->close();
66 output_->getTransport()->close();
Marc Slemko35452342006-08-03 19:01:37 +000067 }
Mark Slee2f6404d2006-10-10 01:37:40 +000068
69 private:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000070 TServer& server_;
Mark Slee2f6404d2006-10-10 01:37:40 +000071 shared_ptr<TProcessor> processor_;
Mark Slee4af6ed72006-10-25 19:02:49 +000072 shared_ptr<TProtocol> input_;
73 shared_ptr<TProtocol> output_;
Mark Slee2f6404d2006-10-10 01:37:40 +000074
Marc Slemko35452342006-08-03 19:01:37 +000075};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000076
Marc Slemko16698852006-08-04 03:16:10 +000077TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
Mark Sleed788b2e2006-09-07 01:26:35 +000078 shared_ptr<TServerTransport> serverTransport,
79 shared_ptr<TTransportFactory> transportFactory,
Mark Slee4af6ed72006-10-25 19:02:49 +000080 shared_ptr<TProtocolFactory> protocolFactory,
Mark Slee4af6ed72006-10-25 19:02:49 +000081 shared_ptr<ThreadManager> threadManager) :
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000082 TServer(processor, serverTransport, transportFactory, protocolFactory),
Mark Slee6e3f6372007-03-01 22:05:46 +000083 threadManager_(threadManager),
Marc Slemko3a3b53b2007-05-22 23:59:54 +000084 stop_(false), timeout_(0) {}
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000085
86TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
87 shared_ptr<TServerTransport> serverTransport,
88 shared_ptr<TTransportFactory> inputTransportFactory,
89 shared_ptr<TTransportFactory> outputTransportFactory,
90 shared_ptr<TProtocolFactory> inputProtocolFactory,
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000091 shared_ptr<TProtocolFactory> outputProtocolFactory,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000092 shared_ptr<ThreadManager> threadManager) :
93 TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory,
94 inputProtocolFactory, outputProtocolFactory),
Mark Slee6e3f6372007-03-01 22:05:46 +000095 threadManager_(threadManager),
Marc Slemko3a3b53b2007-05-22 23:59:54 +000096 stop_(false), timeout_(0) {}
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000097
Mark Sleed788b2e2006-09-07 01:26:35 +000098
Marc Slemko16698852006-08-04 03:16:10 +000099TThreadPoolServer::~TThreadPoolServer() {}
100
Mark Slee794993d2006-09-20 01:56:10 +0000101void TThreadPoolServer::serve() {
Mark Sleed788b2e2006-09-07 01:26:35 +0000102 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000103 shared_ptr<TTransport> inputTransport;
104 shared_ptr<TTransport> outputTransport;
105 shared_ptr<TProtocol> inputProtocol;
106 shared_ptr<TProtocol> outputProtocol;
Mark Sleed788b2e2006-09-07 01:26:35 +0000107
Marc Slemko16698852006-08-04 03:16:10 +0000108 try {
109 // Start the server listening
110 serverTransport_->listen();
111 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000112 cerr << "TThreadPoolServer::run() listen(): " << ttx.what() << endl;
Marc Slemko16698852006-08-04 03:16:10 +0000113 return;
114 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000115
116 // Run the preServe event
117 if (eventHandler_ != NULL) {
118 eventHandler_->preServe();
119 }
120
Mark Slee6e3f6372007-03-01 22:05:46 +0000121 while (!stop_) {
Marc Slemko16698852006-08-04 03:16:10 +0000122 try {
Mark Slee3303f362007-03-05 20:09:37 +0000123 client.reset();
124 inputTransport.reset();
125 outputTransport.reset();
126 inputProtocol.reset();
127 outputProtocol.reset();
128
Mark Sleed788b2e2006-09-07 01:26:35 +0000129 // Fetch client from server
130 client = serverTransport_->accept();
Mark Sleea5a783f2007-03-02 19:41:08 +0000131
Mark Sleed788b2e2006-09-07 01:26:35 +0000132 // Make IO transports
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000133 inputTransport = inputTransportFactory_->getTransport(client);
134 outputTransport = outputTransportFactory_->getTransport(client);
135 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
136 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Slee4af6ed72006-10-25 19:02:49 +0000137
Mark Sleed788b2e2006-09-07 01:26:35 +0000138 // Add to threadmanager pool
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000139 threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(*this, processor_, inputProtocol, outputProtocol)), timeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000140
Marc Slemko16698852006-08-04 03:16:10 +0000141 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +0000142 if (inputTransport != NULL) { inputTransport->close(); }
143 if (outputTransport != NULL) { outputTransport->close(); }
144 if (client != NULL) { client->close(); }
Mark Sleea5a783f2007-03-02 19:41:08 +0000145 if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
146 cerr << "TThreadPoolServer: TServerTransport died on accept: " << ttx.what() << endl;
147 }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000148 continue;
149 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +0000150 if (inputTransport != NULL) { inputTransport->close(); }
151 if (outputTransport != NULL) { outputTransport->close(); }
152 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000153 cerr << "TThreadPoolServer: Caught TException: " << tx.what() << endl;
154 continue;
155 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +0000156 if (inputTransport != NULL) { inputTransport->close(); }
157 if (outputTransport != NULL) { outputTransport->close(); }
158 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000159 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
Marc Slemko16698852006-08-04 03:16:10 +0000160 break;
161 }
162 }
Mark Slee6e3f6372007-03-01 22:05:46 +0000163
164 // If stopped manually, join the existing threads
165 if (stop_) {
166 try {
167 serverTransport_->close();
168 threadManager_->join();
169 } catch (TException &tx) {
170 cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl;
171 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000172 stop_ = false;
Mark Slee6e3f6372007-03-01 22:05:46 +0000173 }
Mark Slee6e3f6372007-03-01 22:05:46 +0000174
Marc Slemko16698852006-08-04 03:16:10 +0000175}
Marc Slemko35452342006-08-03 19:01:37 +0000176
Mark Slee9b82d272007-05-23 05:16:07 +0000177int64_t TThreadPoolServer::getTimeout() const {
178 return timeout_;
179}
180
181void TThreadPoolServer::setTimeout(int64_t value) {
182 timeout_ = value;
183}
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000184
Marc Slemko35452342006-08-03 19:01:37 +0000185}}} // facebook::thrift::server