From: Mark Slee Date: Thu, 1 Mar 2007 22:05:46 +0000 (+0000) Subject: Proper shutdown functionality for Thrift servers X-Git-Tag: 0.2.0~1434 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=6e3f637bab9bb19621382047d413100fe6c44e0a;p=common%2Fthrift.git Proper shutdown functionality for Thrift servers Reviewed By: karl git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665038 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/concurrency/ThreadManager.h b/lib/cpp/src/concurrency/ThreadManager.h index f7c4b3c0..cc13665a 100644 --- a/lib/cpp/src/concurrency/ThreadManager.h +++ b/lib/cpp/src/concurrency/ThreadManager.h @@ -68,6 +68,8 @@ class ThreadManager { * block until all the workers have finished their work. At that point * the ThreadManager will transition into the STOPPED state. */ + virtual void join() = 0; + enum STATE { UNINITIALIZED, STARTING, diff --git a/lib/cpp/src/server/TServer.h b/lib/cpp/src/server/TServer.h index a9bcb26e..2936e421 100644 --- a/lib/cpp/src/server/TServer.h +++ b/lib/cpp/src/server/TServer.h @@ -31,6 +31,8 @@ public: virtual void serve() = 0; + virtual void stop() {} + // Allows running the server as a Runnable thread virtual void run() { serve(); diff --git a/lib/cpp/src/server/TSimpleServer.cpp b/lib/cpp/src/server/TSimpleServer.cpp index eb936f5e..8657fab3 100644 --- a/lib/cpp/src/server/TSimpleServer.cpp +++ b/lib/cpp/src/server/TSimpleServer.cpp @@ -34,7 +34,7 @@ void TSimpleServer::serve() { } // Fetch client from server - while (true) { + while (!stop_) { try { client = serverTransport_->accept(); inputTransport = inputTransportFactory_->getTransport(client); @@ -77,7 +77,14 @@ void TSimpleServer::serve() { } } - // TODO(mcslee): Could this be a timeout case? Or always the real thing? + if (stop_) { + try { + serverTransport_->close(); + } catch (TTransportException &ttx) { + cerr << "TServerTransport failed on close: " << ttx.what() << endl; + } + stop_ = false; + } } }}} // facebook::thrift::server diff --git a/lib/cpp/src/server/TSimpleServer.h b/lib/cpp/src/server/TSimpleServer.h index 05befbf4..0bb03650 100644 --- a/lib/cpp/src/server/TSimpleServer.h +++ b/lib/cpp/src/server/TSimpleServer.h @@ -26,7 +26,8 @@ class TSimpleServer : public TServer { shared_ptr serverTransport, shared_ptr transportFactory, shared_ptr protocolFactory) : - TServer(processor, serverTransport, transportFactory, protocolFactory) {} + TServer(processor, serverTransport, transportFactory, protocolFactory), + stop_(false) {} TSimpleServer(shared_ptr processor, shared_ptr serverTransport, @@ -36,12 +37,20 @@ class TSimpleServer : public TServer { shared_ptr outputProtocolFactory): TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, - inputProtocolFactory, outputProtocolFactory) {} + inputProtocolFactory, outputProtocolFactory), + stop_(false) {} ~TSimpleServer() {} void serve(); + void stop() { + stop_ = true; + } + + protected: + bool stop_; + }; }}} // facebook::thrift::server diff --git a/lib/cpp/src/server/TThreadPoolServer.cpp b/lib/cpp/src/server/TThreadPoolServer.cpp index 1a9898ab..32a02231 100644 --- a/lib/cpp/src/server/TThreadPoolServer.cpp +++ b/lib/cpp/src/server/TThreadPoolServer.cpp @@ -65,7 +65,8 @@ TThreadPoolServer::TThreadPoolServer(shared_ptr processor, shared_ptr protocolFactory, shared_ptr threadManager) : TServer(processor, serverTransport, transportFactory, protocolFactory), - threadManager_(threadManager) {} + threadManager_(threadManager), + stop_(false) {} TThreadPoolServer::TThreadPoolServer(shared_ptr processor, shared_ptr serverTransport, @@ -76,13 +77,13 @@ TThreadPoolServer::TThreadPoolServer(shared_ptr processor, shared_ptr threadManager) : TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory, inputProtocolFactory, outputProtocolFactory), - threadManager_(threadManager) {} + threadManager_(threadManager), + stop_(false) {} TThreadPoolServer::~TThreadPoolServer() {} void TThreadPoolServer::serve() { - shared_ptr client; shared_ptr inputTransport; shared_ptr outputTransport; @@ -97,7 +98,7 @@ void TThreadPoolServer::serve() { return; } - while (true) { + while (!stop_) { try { // Fetch client from server client = serverTransport_->accept(); @@ -131,6 +132,18 @@ void TThreadPoolServer::serve() { break; } } + + // If stopped manually, join the existing threads + if (stop_) { + try { + serverTransport_->close(); + threadManager_->join(); + } catch (TException &tx) { + cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl; + } + } + stop_ = false; + } }}} // facebook::thrift::server diff --git a/lib/cpp/src/server/TThreadPoolServer.h b/lib/cpp/src/server/TThreadPoolServer.h index d2279309..f6809fcd 100644 --- a/lib/cpp/src/server/TThreadPoolServer.h +++ b/lib/cpp/src/server/TThreadPoolServer.h @@ -40,10 +40,14 @@ class TThreadPoolServer : public TServer { virtual ~TThreadPoolServer(); virtual void serve(); + + virtual void stop() { stop_ = true; } protected: shared_ptr threadManager_; + + volatile bool stop_; };