blob: e99a909314568a2ff141ffa7d74ab0fe15d820d2 [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
Mark Sleeb3cb6292007-02-01 22:55:00 +000020#include "server/TThreadedServer.h"
21#include "transport/TTransportException.h"
Roger Meier3faaedf2011-10-02 10:51:45 +000022#include <concurrency/PlatformThreadFactory.h>
Mark Sleeb3cb6292007-02-01 22:55:00 +000023
24#include <string>
25#include <iostream>
Roger Meier3faaedf2011-10-02 10:51:45 +000026
27#ifdef HAVE_UNISTD_H
Mark Sleeb3cb6292007-02-01 22:55:00 +000028#include <unistd.h>
Roger Meier3faaedf2011-10-02 10:51:45 +000029#endif
Mark Sleeb3cb6292007-02-01 22:55:00 +000030
T Jake Lucianib5e62212009-01-31 22:36:20 +000031namespace apache { namespace thrift { namespace server {
Mark Sleeb3cb6292007-02-01 22:55:00 +000032
Mark Slee5ea15f92007-03-05 22:55:59 +000033using boost::shared_ptr;
Mark Sleeb3cb6292007-02-01 22:55:00 +000034using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000035using namespace apache::thrift;
36using namespace apache::thrift::protocol;
37using namespace apache::thrift::transport;
38using namespace apache::thrift::concurrency;
Mark Sleeb3cb6292007-02-01 22:55:00 +000039
40class TThreadedServer::Task: public Runnable {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000041
Mark Sleeb3cb6292007-02-01 22:55:00 +000042public:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000043
44 Task(TThreadedServer& server,
Mark Slee1d4ce802007-03-07 05:16:16 +000045 shared_ptr<TProcessor> processor,
Mark Sleeb3cb6292007-02-01 22:55:00 +000046 shared_ptr<TProtocol> input,
David Reiss23248712010-10-06 17:10:08 +000047 shared_ptr<TProtocol> output,
48 shared_ptr<TTransport> transport) :
Mark Slee1d4ce802007-03-07 05:16:16 +000049 server_(server),
Mark Sleeb3cb6292007-02-01 22:55:00 +000050 processor_(processor),
51 input_(input),
David Reiss23248712010-10-06 17:10:08 +000052 output_(output),
53 transport_(transport) {
Mark Sleeb3cb6292007-02-01 22:55:00 +000054 }
55
56 ~Task() {}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000057
Mark Sleeb3cb6292007-02-01 22:55:00 +000058 void run() {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000059 boost::shared_ptr<TServerEventHandler> eventHandler =
60 server_.getEventHandler();
David Reiss23248712010-10-06 17:10:08 +000061 void* connectionContext = NULL;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000062 if (eventHandler != NULL) {
David Reiss23248712010-10-06 17:10:08 +000063 connectionContext = eventHandler->createContext(input_, output_);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000064 }
Mark Sleeb3cb6292007-02-01 22:55:00 +000065 try {
David Reiss23248712010-10-06 17:10:08 +000066 for (;;) {
67 if (eventHandler != NULL) {
68 eventHandler->processContext(connectionContext, transport_);
69 }
70 if (!processor_->process(input_, output_, connectionContext) ||
71 !input_->getTransport()->peek()) {
Mark Sleeb3cb6292007-02-01 22:55:00 +000072 break;
73 }
74 }
Bryan Duxbury1e987582011-08-25 17:33:03 +000075 } catch (const TTransportException& ttx) {
Bryan Duxbury756d73e2011-08-25 17:30:21 +000076 if (ttx.getType() != TTransportException::END_OF_FILE) {
77 string errStr = string("TThreadedServer client died: ") + ttx.what();
78 GlobalOutput(errStr.c_str());
79 }
Bryan Duxbury1e987582011-08-25 17:33:03 +000080 } catch (const std::exception &x) {
81 GlobalOutput.printf("TThreadedServer exception: %s: %s",
82 typeid(x).name(), x.what());
Mark Sleeb3cb6292007-02-01 22:55:00 +000083 } catch (...) {
Mark Slee2e8a8d42008-01-16 00:38:20 +000084 GlobalOutput("TThreadedServer uncaught exception.");
Mark Sleeb3cb6292007-02-01 22:55:00 +000085 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000086 if (eventHandler != NULL) {
David Reiss23248712010-10-06 17:10:08 +000087 eventHandler->deleteContext(connectionContext, input_, output_);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000088 }
Mark Slee2e8a8d42008-01-16 00:38:20 +000089
90 try {
91 input_->getTransport()->close();
92 } catch (TTransportException& ttx) {
93 string errStr = string("TThreadedServer input close failed: ") + ttx.what();
94 GlobalOutput(errStr.c_str());
95 }
96 try {
97 output_->getTransport()->close();
98 } catch (TTransportException& ttx) {
99 string errStr = string("TThreadedServer output close failed: ") + ttx.what();
100 GlobalOutput(errStr.c_str());
101 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000102
Mark Slee1d4ce802007-03-07 05:16:16 +0000103 // Remove this task from parent bookkeeping
104 {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000105 Synchronized s(server_.tasksMonitor_);
106 server_.tasks_.erase(this);
107 if (server_.tasks_.empty()) {
108 server_.tasksMonitor_.notify();
Mark Slee1d4ce802007-03-07 05:16:16 +0000109 }
110 }
111
Mark Sleeb3cb6292007-02-01 22:55:00 +0000112 }
113
114 private:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000115 TThreadedServer& server_;
Mark Slee1d4ce802007-03-07 05:16:16 +0000116 friend class TThreadedServer;
117
Mark Sleeb3cb6292007-02-01 22:55:00 +0000118 shared_ptr<TProcessor> processor_;
119 shared_ptr<TProtocol> input_;
120 shared_ptr<TProtocol> output_;
David Reiss23248712010-10-06 17:10:08 +0000121 shared_ptr<TTransport> transport_;
Mark Sleeb3cb6292007-02-01 22:55:00 +0000122};
123
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000124void TThreadedServer::init() {
125 stop_ = false;
Mark Sleeb3cb6292007-02-01 22:55:00 +0000126
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000127 if (!threadFactory_) {
Roger Meier3faaedf2011-10-02 10:51:45 +0000128 threadFactory_.reset(new PlatformThreadFactory);
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000129 }
David Reiss45d56962009-03-14 23:35:16 +0000130}
131
Mark Sleeb3cb6292007-02-01 22:55:00 +0000132TThreadedServer::~TThreadedServer() {}
133
134void TThreadedServer::serve() {
135
136 shared_ptr<TTransport> client;
137 shared_ptr<TTransport> inputTransport;
138 shared_ptr<TTransport> outputTransport;
139 shared_ptr<TProtocol> inputProtocol;
140 shared_ptr<TProtocol> outputProtocol;
141
Roger Meierd8f50f32012-04-11 21:48:56 +0000142 // Start the server listening
143 serverTransport_->listen();
Mark Sleeb3cb6292007-02-01 22:55:00 +0000144
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000145 // Run the preServe event
146 if (eventHandler_ != NULL) {
147 eventHandler_->preServe();
148 }
149
150 while (!stop_) {
Mark Sleeb3cb6292007-02-01 22:55:00 +0000151 try {
Mark Slee1d4ce802007-03-07 05:16:16 +0000152 client.reset();
153 inputTransport.reset();
154 outputTransport.reset();
155 inputProtocol.reset();
156 outputProtocol.reset();
157
Mark Sleeb3cb6292007-02-01 22:55:00 +0000158 // Fetch client from server
159 client = serverTransport_->accept();
Mark Slee1d4ce802007-03-07 05:16:16 +0000160
Mark Sleeb3cb6292007-02-01 22:55:00 +0000161 // Make IO transports
162 inputTransport = inputTransportFactory_->getTransport(client);
163 outputTransport = outputTransportFactory_->getTransport(client);
164 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
165 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
166
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000167 shared_ptr<TProcessor> processor = getProcessor(inputProtocol,
168 outputProtocol, client);
169
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000170 TThreadedServer::Task* task = new TThreadedServer::Task(*this,
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000171 processor,
Mark Slee1d4ce802007-03-07 05:16:16 +0000172 inputProtocol,
David Reiss23248712010-10-06 17:10:08 +0000173 outputProtocol,
174 client);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000175
Mark Slee1d4ce802007-03-07 05:16:16 +0000176 // Create a task
177 shared_ptr<Runnable> runnable =
178 shared_ptr<Runnable>(task);
Mark Sleeb3cb6292007-02-01 22:55:00 +0000179
180 // Create a thread for this task
181 shared_ptr<Thread> thread =
Mark Slee1d4ce802007-03-07 05:16:16 +0000182 shared_ptr<Thread>(threadFactory_->newThread(runnable));
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000183
Mark Slee1d4ce802007-03-07 05:16:16 +0000184 // Insert thread into the set of threads
185 {
186 Synchronized s(tasksMonitor_);
187 tasks_.insert(task);
188 }
189
Mark Sleeb3cb6292007-02-01 22:55:00 +0000190 // Start the thread!
191 thread->start();
192
193 } catch (TTransportException& ttx) {
Mark Slee1d4ce802007-03-07 05:16:16 +0000194 if (inputTransport != NULL) { inputTransport->close(); }
195 if (outputTransport != NULL) { outputTransport->close(); }
196 if (client != NULL) { client->close(); }
197 if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000198 string errStr = string("TThreadedServer: TServerTransport died on accept: ") + ttx.what();
199 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000200 }
Mark Slee907e3d62007-02-08 22:29:24 +0000201 continue;
202 } catch (TException& tx) {
Mark Slee1d4ce802007-03-07 05:16:16 +0000203 if (inputTransport != NULL) { inputTransport->close(); }
204 if (outputTransport != NULL) { outputTransport->close(); }
205 if (client != NULL) { client->close(); }
Mark Slee2e8a8d42008-01-16 00:38:20 +0000206 string errStr = string("TThreadedServer: Caught TException: ") + tx.what();
207 GlobalOutput(errStr.c_str());
Mark Slee907e3d62007-02-08 22:29:24 +0000208 continue;
209 } catch (string s) {
Mark Slee1d4ce802007-03-07 05:16:16 +0000210 if (inputTransport != NULL) { inputTransport->close(); }
211 if (outputTransport != NULL) { outputTransport->close(); }
212 if (client != NULL) { client->close(); }
Mark Slee2e8a8d42008-01-16 00:38:20 +0000213 string errStr = "TThreadedServer: Unknown exception: " + s;
214 GlobalOutput(errStr.c_str());
Mark Sleeb3cb6292007-02-01 22:55:00 +0000215 break;
216 }
217 }
Mark Slee1d4ce802007-03-07 05:16:16 +0000218
219 // If stopped manually, make sure to close server transport
220 if (stop_) {
221 try {
222 serverTransport_->close();
223 } catch (TException &tx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000224 string errStr = string("TThreadedServer: Exception shutting down: ") + tx.what();
225 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000226 }
227 try {
228 Synchronized s(tasksMonitor_);
229 while (!tasks_.empty()) {
230 tasksMonitor_.wait();
231 }
232 } catch (TException &tx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000233 string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what();
234 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000235 }
236 stop_ = false;
237 }
238
Mark Sleeb3cb6292007-02-01 22:55:00 +0000239}
240
T Jake Lucianib5e62212009-01-31 22:36:20 +0000241}}} // apache::thrift::server