blob: f40135c5ddd609d88a72f4c065eb420f1819c361 [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"
22#include "concurrency/PosixThreadFactory.h"
23
24#include <string>
25#include <iostream>
26#include <pthread.h>
27#include <unistd.h>
28
T Jake Lucianib5e62212009-01-31 22:36:20 +000029namespace apache { namespace thrift { namespace server {
Mark Sleeb3cb6292007-02-01 22:55:00 +000030
Mark Slee5ea15f92007-03-05 22:55:59 +000031using boost::shared_ptr;
Mark Sleeb3cb6292007-02-01 22:55:00 +000032using namespace std;
T Jake Lucianib5e62212009-01-31 22:36:20 +000033using namespace apache::thrift;
34using namespace apache::thrift::protocol;
35using namespace apache::thrift::transport;
36using namespace apache::thrift::concurrency;
Mark Sleeb3cb6292007-02-01 22:55:00 +000037
38class TThreadedServer::Task: public Runnable {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000039
Mark Sleeb3cb6292007-02-01 22:55:00 +000040public:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000041
42 Task(TThreadedServer& server,
Mark Slee1d4ce802007-03-07 05:16:16 +000043 shared_ptr<TProcessor> processor,
Mark Sleeb3cb6292007-02-01 22:55:00 +000044 shared_ptr<TProtocol> input,
David Reiss23248712010-10-06 17:10:08 +000045 shared_ptr<TProtocol> output,
46 shared_ptr<TTransport> transport) :
Mark Slee1d4ce802007-03-07 05:16:16 +000047 server_(server),
Mark Sleeb3cb6292007-02-01 22:55:00 +000048 processor_(processor),
49 input_(input),
David Reiss23248712010-10-06 17:10:08 +000050 output_(output),
51 transport_(transport) {
Mark Sleeb3cb6292007-02-01 22:55:00 +000052 }
53
54 ~Task() {}
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000055
Mark Sleeb3cb6292007-02-01 22:55:00 +000056 void run() {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000057 boost::shared_ptr<TServerEventHandler> eventHandler =
58 server_.getEventHandler();
David Reiss23248712010-10-06 17:10:08 +000059 void* connectionContext = NULL;
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000060 if (eventHandler != NULL) {
David Reiss23248712010-10-06 17:10:08 +000061 connectionContext = eventHandler->createContext(input_, output_);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000062 }
Mark Sleeb3cb6292007-02-01 22:55:00 +000063 try {
David Reiss23248712010-10-06 17:10:08 +000064 for (;;) {
65 if (eventHandler != NULL) {
66 eventHandler->processContext(connectionContext, transport_);
67 }
68 if (!processor_->process(input_, output_, connectionContext) ||
69 !input_->getTransport()->peek()) {
Mark Sleeb3cb6292007-02-01 22:55:00 +000070 break;
71 }
72 }
Bryan Duxbury1e987582011-08-25 17:33:03 +000073 } catch (const TTransportException& ttx) {
Bryan Duxbury756d73e2011-08-25 17:30:21 +000074 if (ttx.getType() != TTransportException::END_OF_FILE) {
75 string errStr = string("TThreadedServer client died: ") + ttx.what();
76 GlobalOutput(errStr.c_str());
77 }
Bryan Duxbury1e987582011-08-25 17:33:03 +000078 } catch (const std::exception &x) {
79 GlobalOutput.printf("TThreadedServer exception: %s: %s",
80 typeid(x).name(), x.what());
Mark Sleeb3cb6292007-02-01 22:55:00 +000081 } catch (...) {
Mark Slee2e8a8d42008-01-16 00:38:20 +000082 GlobalOutput("TThreadedServer uncaught exception.");
Mark Sleeb3cb6292007-02-01 22:55:00 +000083 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000084 if (eventHandler != NULL) {
David Reiss23248712010-10-06 17:10:08 +000085 eventHandler->deleteContext(connectionContext, input_, output_);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +000086 }
Mark Slee2e8a8d42008-01-16 00:38:20 +000087
88 try {
89 input_->getTransport()->close();
90 } catch (TTransportException& ttx) {
91 string errStr = string("TThreadedServer 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("TThreadedServer output close failed: ") + ttx.what();
98 GlobalOutput(errStr.c_str());
99 }
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000100
Mark Slee1d4ce802007-03-07 05:16:16 +0000101 // Remove this task from parent bookkeeping
102 {
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000103 Synchronized s(server_.tasksMonitor_);
104 server_.tasks_.erase(this);
105 if (server_.tasks_.empty()) {
106 server_.tasksMonitor_.notify();
Mark Slee1d4ce802007-03-07 05:16:16 +0000107 }
108 }
109
Mark Sleeb3cb6292007-02-01 22:55:00 +0000110 }
111
112 private:
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000113 TThreadedServer& server_;
Mark Slee1d4ce802007-03-07 05:16:16 +0000114 friend class TThreadedServer;
115
Mark Sleeb3cb6292007-02-01 22:55:00 +0000116 shared_ptr<TProcessor> processor_;
117 shared_ptr<TProtocol> input_;
118 shared_ptr<TProtocol> output_;
David Reiss23248712010-10-06 17:10:08 +0000119 shared_ptr<TTransport> transport_;
Mark Sleeb3cb6292007-02-01 22:55:00 +0000120};
121
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000122void TThreadedServer::init() {
123 stop_ = false;
Mark Sleeb3cb6292007-02-01 22:55:00 +0000124
Bryan Duxbury7a9fb812011-09-01 18:31:53 +0000125 if (!threadFactory_) {
126 threadFactory_.reset(new PosixThreadFactory);
127 }
David Reiss45d56962009-03-14 23:35:16 +0000128}
129
Mark Sleeb3cb6292007-02-01 22:55:00 +0000130TThreadedServer::~TThreadedServer() {}
131
132void TThreadedServer::serve() {
133
134 shared_ptr<TTransport> client;
135 shared_ptr<TTransport> inputTransport;
136 shared_ptr<TTransport> outputTransport;
137 shared_ptr<TProtocol> inputProtocol;
138 shared_ptr<TProtocol> outputProtocol;
139
140 try {
141 // Start the server listening
142 serverTransport_->listen();
143 } catch (TTransportException& ttx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000144 string errStr = string("TThreadedServer::run() listen(): ") +ttx.what();
145 GlobalOutput(errStr.c_str());
Mark Sleeb3cb6292007-02-01 22:55:00 +0000146 return;
147 }
148
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000149 // Run the preServe event
150 if (eventHandler_ != NULL) {
151 eventHandler_->preServe();
152 }
153
154 while (!stop_) {
Mark Sleeb3cb6292007-02-01 22:55:00 +0000155 try {
Mark Slee1d4ce802007-03-07 05:16:16 +0000156 client.reset();
157 inputTransport.reset();
158 outputTransport.reset();
159 inputProtocol.reset();
160 outputProtocol.reset();
161
Mark Sleeb3cb6292007-02-01 22:55:00 +0000162 // Fetch client from server
163 client = serverTransport_->accept();
Mark Slee1d4ce802007-03-07 05:16:16 +0000164
Mark Sleeb3cb6292007-02-01 22:55:00 +0000165 // Make IO transports
166 inputTransport = inputTransportFactory_->getTransport(client);
167 outputTransport = outputTransportFactory_->getTransport(client);
168 inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
169 outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
170
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000171 shared_ptr<TProcessor> processor = getProcessor(inputProtocol,
172 outputProtocol, client);
173
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000174 TThreadedServer::Task* task = new TThreadedServer::Task(*this,
Bryan Duxbury6dd9cd02011-09-01 18:06:20 +0000175 processor,
Mark Slee1d4ce802007-03-07 05:16:16 +0000176 inputProtocol,
David Reiss23248712010-10-06 17:10:08 +0000177 outputProtocol,
178 client);
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000179
Mark Slee1d4ce802007-03-07 05:16:16 +0000180 // Create a task
181 shared_ptr<Runnable> runnable =
182 shared_ptr<Runnable>(task);
Mark Sleeb3cb6292007-02-01 22:55:00 +0000183
184 // Create a thread for this task
185 shared_ptr<Thread> thread =
Mark Slee1d4ce802007-03-07 05:16:16 +0000186 shared_ptr<Thread>(threadFactory_->newThread(runnable));
Mark Sleeb4d3e7b2007-11-28 01:51:43 +0000187
Mark Slee1d4ce802007-03-07 05:16:16 +0000188 // Insert thread into the set of threads
189 {
190 Synchronized s(tasksMonitor_);
191 tasks_.insert(task);
192 }
193
Mark Sleeb3cb6292007-02-01 22:55:00 +0000194 // Start the thread!
195 thread->start();
196
197 } catch (TTransportException& ttx) {
Mark Slee1d4ce802007-03-07 05:16:16 +0000198 if (inputTransport != NULL) { inputTransport->close(); }
199 if (outputTransport != NULL) { outputTransport->close(); }
200 if (client != NULL) { client->close(); }
201 if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000202 string errStr = string("TThreadedServer: TServerTransport died on accept: ") + ttx.what();
203 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000204 }
Mark Slee907e3d62007-02-08 22:29:24 +0000205 continue;
206 } catch (TException& tx) {
Mark Slee1d4ce802007-03-07 05:16:16 +0000207 if (inputTransport != NULL) { inputTransport->close(); }
208 if (outputTransport != NULL) { outputTransport->close(); }
209 if (client != NULL) { client->close(); }
Mark Slee2e8a8d42008-01-16 00:38:20 +0000210 string errStr = string("TThreadedServer: Caught TException: ") + tx.what();
211 GlobalOutput(errStr.c_str());
Mark Slee907e3d62007-02-08 22:29:24 +0000212 continue;
213 } catch (string s) {
Mark Slee1d4ce802007-03-07 05:16:16 +0000214 if (inputTransport != NULL) { inputTransport->close(); }
215 if (outputTransport != NULL) { outputTransport->close(); }
216 if (client != NULL) { client->close(); }
Mark Slee2e8a8d42008-01-16 00:38:20 +0000217 string errStr = "TThreadedServer: Unknown exception: " + s;
218 GlobalOutput(errStr.c_str());
Mark Sleeb3cb6292007-02-01 22:55:00 +0000219 break;
220 }
221 }
Mark Slee1d4ce802007-03-07 05:16:16 +0000222
223 // If stopped manually, make sure to close server transport
224 if (stop_) {
225 try {
226 serverTransport_->close();
227 } catch (TException &tx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000228 string errStr = string("TThreadedServer: Exception shutting down: ") + tx.what();
229 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000230 }
231 try {
232 Synchronized s(tasksMonitor_);
233 while (!tasks_.empty()) {
234 tasksMonitor_.wait();
235 }
236 } catch (TException &tx) {
Mark Slee2e8a8d42008-01-16 00:38:20 +0000237 string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what();
238 GlobalOutput(errStr.c_str());
Mark Slee1d4ce802007-03-07 05:16:16 +0000239 }
240 stop_ = false;
241 }
242
Mark Sleeb3cb6292007-02-01 22:55:00 +0000243}
244
T Jake Lucianib5e62212009-01-31 22:36:20 +0000245}}} // apache::thrift::server