blob: 355bf8e41beb810b0a828d82ee4d67a15131bf68 [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;
57 } catch (...) {
58 cerr << "TThreadPoolServer uncaught exception." << endl;
Marc Slemko35452342006-08-03 19:01:37 +000059 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000060 if (eventHandler != NULL) {
61 eventHandler->clientEnd(input_, output_);
62 }
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000063 input_->getTransport()->close();
64 output_->getTransport()->close();
Marc Slemko35452342006-08-03 19:01:37 +000065 }
Mark Slee2f6404d2006-10-10 01:37:40 +000066
67 private:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000068 TServer& server_;
Mark Slee2f6404d2006-10-10 01:37:40 +000069 shared_ptr<TProcessor> processor_;
Mark Slee4af6ed72006-10-25 19:02:49 +000070 shared_ptr<TProtocol> input_;
71 shared_ptr<TProtocol> output_;
Mark Slee2f6404d2006-10-10 01:37:40 +000072
Marc Slemko35452342006-08-03 19:01:37 +000073};
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000074
Marc Slemko16698852006-08-04 03:16:10 +000075TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
Mark Sleed788b2e2006-09-07 01:26:35 +000076 shared_ptr<TServerTransport> serverTransport,
77 shared_ptr<TTransportFactory> transportFactory,
Mark Slee4af6ed72006-10-25 19:02:49 +000078 shared_ptr<TProtocolFactory> protocolFactory,
Mark Slee4af6ed72006-10-25 19:02:49 +000079 shared_ptr<ThreadManager> threadManager) :
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000080 TServer(processor, serverTransport, transportFactory, protocolFactory),
Mark Slee6e3f6372007-03-01 22:05:46 +000081 threadManager_(threadManager),
Marc Slemko3a3b53b2007-05-22 23:59:54 +000082 stop_(false), timeout_(0) {}
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000083
84TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
85 shared_ptr<TServerTransport> serverTransport,
86 shared_ptr<TTransportFactory> inputTransportFactory,
87 shared_ptr<TTransportFactory> outputTransportFactory,
88 shared_ptr<TProtocolFactory> inputProtocolFactory,
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000089 shared_ptr<TProtocolFactory> outputProtocolFactory,
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000090 shared_ptr<ThreadManager> threadManager) :
91 TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory,
92 inputProtocolFactory, outputProtocolFactory),
Mark Slee6e3f6372007-03-01 22:05:46 +000093 threadManager_(threadManager),
Marc Slemko3a3b53b2007-05-22 23:59:54 +000094 stop_(false), timeout_(0) {}
Aditya Agarwal9abb0d62007-01-24 22:53:54 +000095
Mark Sleed788b2e2006-09-07 01:26:35 +000096
Marc Slemko16698852006-08-04 03:16:10 +000097TThreadPoolServer::~TThreadPoolServer() {}
98
Mark Slee794993d2006-09-20 01:56:10 +000099void TThreadPoolServer::serve() {
Mark Sleed788b2e2006-09-07 01:26:35 +0000100 shared_ptr<TTransport> client;
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000101 shared_ptr<TTransport> inputTransport;
102 shared_ptr<TTransport> outputTransport;
103 shared_ptr<TProtocol> inputProtocol;
104 shared_ptr<TProtocol> outputProtocol;
Mark Sleed788b2e2006-09-07 01:26:35 +0000105
Marc Slemko16698852006-08-04 03:16:10 +0000106 try {
107 // Start the server listening
108 serverTransport_->listen();
109 } catch (TTransportException& ttx) {
Mark Sleeb9ff32a2006-11-16 01:00:24 +0000110 cerr << "TThreadPoolServer::run() listen(): " << ttx.what() << endl;
Marc Slemko16698852006-08-04 03:16:10 +0000111 return;
112 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000113
114 // Run the preServe event
115 if (eventHandler_ != NULL) {
116 eventHandler_->preServe();
117 }
118
Mark Slee6e3f6372007-03-01 22:05:46 +0000119 while (!stop_) {
Marc Slemko16698852006-08-04 03:16:10 +0000120 try {
Mark Slee3303f362007-03-05 20:09:37 +0000121 client.reset();
122 inputTransport.reset();
123 outputTransport.reset();
124 inputProtocol.reset();
125 outputProtocol.reset();
126
Mark Sleed788b2e2006-09-07 01:26:35 +0000127 // Fetch client from server
128 client = serverTransport_->accept();
Mark Sleea5a783f2007-03-02 19:41:08 +0000129
Mark Sleed788b2e2006-09-07 01:26:35 +0000130 // Make IO transports
Aditya Agarwal9abb0d62007-01-24 22:53:54 +0000131 inputTransport = inputTransportFactory_->getTransport(client);
132 outputTransport = outputTransportFactory_->getTransport(client);
133 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
134 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
Mark Slee4af6ed72006-10-25 19:02:49 +0000135
Mark Sleed788b2e2006-09-07 01:26:35 +0000136 // Add to threadmanager pool
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000137 threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(*this, processor_, inputProtocol, outputProtocol)), timeout_);
Mark Sleea5a783f2007-03-02 19:41:08 +0000138
Marc Slemko16698852006-08-04 03:16:10 +0000139 } catch (TTransportException& ttx) {
Mark Slee3303f362007-03-05 20:09:37 +0000140 if (inputTransport != NULL) { inputTransport->close(); }
141 if (outputTransport != NULL) { outputTransport->close(); }
142 if (client != NULL) { client->close(); }
Mark Sleea5a783f2007-03-02 19:41:08 +0000143 if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
144 cerr << "TThreadPoolServer: TServerTransport died on accept: " << ttx.what() << endl;
145 }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000146 continue;
147 } catch (TException& tx) {
Mark Slee3303f362007-03-05 20:09:37 +0000148 if (inputTransport != NULL) { inputTransport->close(); }
149 if (outputTransport != NULL) { outputTransport->close(); }
150 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000151 cerr << "TThreadPoolServer: Caught TException: " << tx.what() << endl;
152 continue;
153 } catch (string s) {
Mark Slee3303f362007-03-05 20:09:37 +0000154 if (inputTransport != NULL) { inputTransport->close(); }
155 if (outputTransport != NULL) { outputTransport->close(); }
156 if (client != NULL) { client->close(); }
Aditya Agarwalfdef47e2007-02-07 03:54:18 +0000157 cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
Marc Slemko16698852006-08-04 03:16:10 +0000158 break;
159 }
160 }
Mark Slee6e3f6372007-03-01 22:05:46 +0000161
162 // If stopped manually, join the existing threads
163 if (stop_) {
164 try {
165 serverTransport_->close();
166 threadManager_->join();
167 } catch (TException &tx) {
168 cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl;
169 }
Mark Sleea5a783f2007-03-02 19:41:08 +0000170 stop_ = false;
Mark Slee6e3f6372007-03-01 22:05:46 +0000171 }
Mark Slee6e3f6372007-03-01 22:05:46 +0000172
Marc Slemko16698852006-08-04 03:16:10 +0000173}
Marc Slemko35452342006-08-03 19:01:37 +0000174
Mark Slee9b82d272007-05-23 05:16:07 +0000175int64_t TThreadPoolServer::getTimeout() const {
176 return timeout_;
177}
178
179void TThreadPoolServer::setTimeout(int64_t value) {
180 timeout_ = value;
181}
Marc Slemko3a3b53b2007-05-22 23:59:54 +0000182
Marc Slemko35452342006-08-03 19:01:37 +0000183}}} // facebook::thrift::server