Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thrift-server.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_RPC_THRIFT_SERVER_H
17 #define IMPALA_RPC_THRIFT_SERVER_H
18 
19 #include <boost/thread/mutex.hpp>
20 #include <boost/scoped_ptr.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/unordered_map.hpp>
23 #include <boost/uuid/uuid_generators.hpp>
24 #include <thrift/server/TServer.h>
25 #include <thrift/TProcessor.h>
26 
27 #include "common/status.h"
28 #include "rpc/auth-provider.h"
29 #include "util/metrics.h"
30 #include "util/thread.h"
31 
32 namespace impala {
33 
39 class ThriftServer {
40  public:
42  typedef std::string Username;
43 
46  TUniqueId connection_id;
48  TNetworkAddress network_address;
49  std::string server_name;
50  };
51 
54  public:
56  virtual void ConnectionStart(const ConnectionContext& connection_context) = 0;
57 
61  virtual void ConnectionEnd(const ConnectionContext& connection_context) = 0;
62  };
63 
64  static const int DEFAULT_WORKER_THREADS = 2;
65 
71 
82  ThriftServer(const std::string& name,
83  const boost::shared_ptr<apache::thrift::TProcessor>& processor, int port,
84  AuthProvider* auth_provider = NULL, MetricGroup* metrics = NULL,
85  int num_worker_threads = DEFAULT_WORKER_THREADS, ServerType server_type = Threaded);
86 
90  Status EnableSsl(const std::string& certificate, const std::string& private_key);
91 
92  int port() const { return port_; }
93 
94  bool ssl_enabled() const { return ssl_enabled_; }
95 
97  void Join();
98 
101  void StopForTesting();
102 
106  Status Start();
107 
111  connection_handler_ = connection;
112  }
113 
119  static const TUniqueId& GetThreadConnectionId();
120 
131  static const ConnectionContext* GetThreadConnectionContext();
132 
133  private:
137  boost::shared_ptr<apache::thrift::transport::TServerTransport>* socket);
138 
140  bool started_;
141 
143  int port_;
144 
147 
149  std::string certificate_path_;
150 
152  std::string private_key_path_;
153 
157 
160 
162  const std::string name_;
163 
165  boost::scoped_ptr<Thread> server_thread_;
166 
168  boost::scoped_ptr<apache::thrift::server::TServer> server_;
169  boost::shared_ptr<apache::thrift::TProcessor> processor_;
170 
173 
176 
179  typedef boost::unordered_map<ConnectionContext*, boost::shared_ptr<ConnectionContext> >
182 
185 
188 
191 
193  boost::uuids::random_generator uuid_generator_;
194 
197 
201  friend class ThriftServerEventProcessor;
202 };
203 
204 }
205 
206 #endif
bool ssl_enabled_
True if the server socket only accepts SSL connections.
boost::scoped_ptr< apache::thrift::server::TServer > server_
Thrift housekeeping.
static const TUniqueId & GetThreadConnectionId()
virtual void ConnectionStart(const ConnectionContext &connection_context)=0
Called when a connection is established (when a client connects).
class SimpleMetric< int64_t, TMetricKind::COUNTER > IntCounter
Definition: metrics.h:320
boost::scoped_ptr< Thread > server_thread_
Thread that runs ThriftServerEventProcessor::Supervise() in a separate loop.
static const int DEFAULT_WORKER_THREADS
Definition: thrift-server.h:64
std::string Username
Username.
Definition: thrift-server.h:42
MetricGroups may be organised hierarchically as a tree.
Definition: metrics.h:200
Status EnableSsl(const std::string &certificate, const std::string &private_key)
void Join()
Blocks until the server stops and exits its main thread.
int port_
The port on which the server interface is exposed.
bool started_
True if the server has been successfully started, for internal use only.
Interface class for receiving connection creation / termination events.
Definition: thrift-server.h:53
std::string private_key_path_
Path to private key file in .PEM format.
ThriftServer(const std::string &name, const boost::shared_ptr< apache::thrift::TProcessor > &processor, int port, AuthProvider *auth_provider=NULL, MetricGroup *metrics=NULL, int num_worker_threads=DEFAULT_WORKER_THREADS, ServerType server_type=Threaded)
AuthProvider * auth_provider_
Not owned by us, owned by the AuthManager.
ConnectionContextSet connection_contexts_
IntGauge * num_current_connections_metric_
Number of currently active connections.
Per-connection information.
Definition: thrift-server.h:45
boost::mutex connection_contexts_lock_
Protects connection_contexts_.
bool ssl_enabled() const
Definition: thrift-server.h:94
ConnectionHandlerIf * connection_handler_
If not NULL, called when connection events happen. Not owned by us.
Status CreateSocket(boost::shared_ptr< apache::thrift::transport::TServerTransport > *socket)
bool metrics_enabled_
True if metrics are enabled.
void SetConnectionHandler(ConnectionHandlerIf *connection)
ServerType server_type_
ThreadPool or Threaded server.
static const ConnectionContext * GetThreadConnectionContext()
virtual void ConnectionEnd(const ConnectionContext &connection_context)=0
string name
Definition: cpu-info.cc:50
boost::uuids::random_generator uuid_generator_
Used to generate a unique connection id for every connection.
IntCounter * total_connections_metric_
Total connections made over the lifetime of this server.
std::string certificate_path_
Path to certificate file in .PEM format.
boost::unordered_map< ConnectionContext *, boost::shared_ptr< ConnectionContext > > ConnectionContextSet
const std::string name_
User-specified identifier that shows up in logs.
boost::shared_ptr< apache::thrift::TProcessor > processor_