1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift2;
20
21 import java.net.InetAddress;
22 import java.net.InetSocketAddress;
23 import java.net.UnknownHostException;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.LinkedBlockingQueue;
27 import java.util.concurrent.ThreadPoolExecutor;
28 import java.util.concurrent.TimeUnit;
29
30 import org.apache.commons.cli.CommandLine;
31 import org.apache.commons.cli.CommandLineParser;
32 import org.apache.commons.cli.HelpFormatter;
33 import org.apache.commons.cli.Option;
34 import org.apache.commons.cli.OptionGroup;
35 import org.apache.commons.cli.Options;
36 import org.apache.commons.cli.ParseException;
37 import org.apache.commons.cli.PosixParser;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.hadoop.classification.InterfaceAudience;
41 import org.apache.hadoop.conf.Configuration;
42 import org.apache.hadoop.hbase.HBaseConfiguration;
43 import org.apache.hadoop.hbase.thrift.CallQueue;
44 import org.apache.hadoop.hbase.thrift.CallQueue.Call;
45 import org.apache.hadoop.hbase.thrift.ThriftMetrics;
46 import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
47 import org.apache.hadoop.hbase.util.InfoServer;
48 import org.apache.thrift.protocol.TBinaryProtocol;
49 import org.apache.thrift.protocol.TCompactProtocol;
50 import org.apache.thrift.protocol.TProtocolFactory;
51 import org.apache.thrift.server.THsHaServer;
52 import org.apache.thrift.server.TNonblockingServer;
53 import org.apache.thrift.server.TServer;
54 import org.apache.thrift.server.TThreadPoolServer;
55 import org.apache.thrift.transport.TFramedTransport;
56 import org.apache.thrift.transport.TNonblockingServerSocket;
57 import org.apache.thrift.transport.TNonblockingServerTransport;
58 import org.apache.thrift.transport.TServerSocket;
59 import org.apache.thrift.transport.TServerTransport;
60 import org.apache.thrift.transport.TTransportException;
61 import org.apache.thrift.transport.TTransportFactory;
62
63 import com.google.common.util.concurrent.ThreadFactoryBuilder;
64
65
66
67
68
69 @InterfaceAudience.Private
70 @SuppressWarnings({ "rawtypes", "unchecked" })
71 public class ThriftServer {
72 private static final Log log = LogFactory.getLog(ThriftServer.class);
73
74 public static final String DEFAULT_LISTEN_PORT = "9090";
75
76 public ThriftServer() {
77 }
78
79 private static void printUsage() {
80 HelpFormatter formatter = new HelpFormatter();
81 formatter.printHelp("Thrift", null, getOptions(),
82 "To start the Thrift server run 'bin/hbase-daemon.sh start thrift2'\n" +
83 "To shutdown the thrift server run 'bin/hbase-daemon.sh stop thrift2' or" +
84 " send a kill signal to the thrift server pid",
85 true);
86 }
87
88 private static Options getOptions() {
89 Options options = new Options();
90 options.addOption("b", "bind", true,
91 "Address to bind the Thrift server to. [default: 0.0.0.0]");
92 options.addOption("p", "port", true, "Port to bind to [default: " + DEFAULT_LISTEN_PORT + "]");
93 options.addOption("f", "framed", false, "Use framed transport");
94 options.addOption("c", "compact", false, "Use the compact protocol");
95 options.addOption("h", "help", false, "Print help information");
96 options.addOption(null, "infoport", true, "Port for web UI");
97
98 OptionGroup servers = new OptionGroup();
99 servers.addOption(
100 new Option("nonblocking", false, "Use the TNonblockingServer. This implies the framed transport."));
101 servers.addOption(new Option("hsha", false, "Use the THsHaServer. This implies the framed transport."));
102 servers.addOption(new Option("threadpool", false, "Use the TThreadPoolServer. This is the default."));
103 options.addOptionGroup(servers);
104 return options;
105 }
106
107 private static CommandLine parseArguments(Options options, String[] args) throws ParseException {
108 CommandLineParser parser = new PosixParser();
109 return parser.parse(options, args);
110 }
111
112 private static TProtocolFactory getTProtocolFactory(boolean isCompact) {
113 if (isCompact) {
114 log.debug("Using compact protocol");
115 return new TCompactProtocol.Factory();
116 } else {
117 log.debug("Using binary protocol");
118 return new TBinaryProtocol.Factory();
119 }
120 }
121
122 private static TTransportFactory getTTransportFactory(boolean framed) {
123 if (framed) {
124 log.debug("Using framed transport");
125 return new TFramedTransport.Factory();
126 } else {
127 return new TTransportFactory();
128 }
129 }
130
131
132
133
134 private static InetSocketAddress bindToPort(String bindValue, int listenPort)
135 throws UnknownHostException {
136 try {
137 if (bindValue == null) {
138 return new InetSocketAddress(listenPort);
139 } else {
140 return new InetSocketAddress(InetAddress.getByName(bindValue), listenPort);
141 }
142 } catch (UnknownHostException e) {
143 throw new RuntimeException("Could not bind to provided ip address", e);
144 }
145 }
146
147 private static TServer getTNonBlockingServer(TProtocolFactory protocolFactory, THBaseService.Processor processor,
148 TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws TTransportException {
149 TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress);
150 log.info("starting HBase Nonblocking Thrift server on " + inetSocketAddress.toString());
151 TNonblockingServer.Args serverArgs = new TNonblockingServer.Args(serverTransport);
152 serverArgs.processor(processor);
153 serverArgs.transportFactory(transportFactory);
154 serverArgs.protocolFactory(protocolFactory);
155 return new TNonblockingServer(serverArgs);
156 }
157
158 private static TServer getTHsHaServer(TProtocolFactory protocolFactory,
159 THBaseService.Processor processor, TTransportFactory transportFactory,
160 InetSocketAddress inetSocketAddress, ThriftMetrics metrics)
161 throws TTransportException {
162 TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress);
163 log.info("starting HBase HsHA Thrift server on " + inetSocketAddress.toString());
164 THsHaServer.Args serverArgs = new THsHaServer.Args(serverTransport);
165 ExecutorService executorService = createExecutor(
166 serverArgs.getWorkerThreads(), metrics);
167 serverArgs.executorService(executorService);
168 serverArgs.processor(processor);
169 serverArgs.transportFactory(transportFactory);
170 serverArgs.protocolFactory(protocolFactory);
171 return new THsHaServer(serverArgs);
172 }
173
174 private static ExecutorService createExecutor(
175 int workerThreads, ThriftMetrics metrics) {
176 CallQueue callQueue = new CallQueue(
177 new LinkedBlockingQueue<Call>(), metrics);
178 ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
179 tfb.setDaemon(true);
180 tfb.setNameFormat("thrift2-worker-%d");
181 return new ThreadPoolExecutor(workerThreads, workerThreads,
182 Long.MAX_VALUE, TimeUnit.SECONDS, callQueue, tfb.build());
183 }
184
185 private static TServer getTThreadPoolServer(TProtocolFactory protocolFactory, THBaseService.Processor processor,
186 TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws TTransportException {
187 TServerTransport serverTransport = new TServerSocket(inetSocketAddress);
188 log.info("starting HBase ThreadPool Thrift server on " + inetSocketAddress.toString());
189 TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
190 serverArgs.processor(processor);
191 serverArgs.transportFactory(transportFactory);
192 serverArgs.protocolFactory(protocolFactory);
193 return new TThreadPoolServer(serverArgs);
194 }
195
196
197
198
199
200
201 public static void main(String[] args) throws Exception {
202 TServer server = null;
203 Options options = getOptions();
204 try {
205 CommandLine cmd = parseArguments(options, args);
206
207
208
209
210
211 List<?> argList = cmd.getArgList();
212 if (cmd.hasOption("help") || !argList.contains("start") || argList.contains("stop")) {
213 printUsage();
214 System.exit(1);
215 }
216
217
218 int listenPort = 0;
219 try {
220 listenPort = Integer.parseInt(cmd.getOptionValue("port", DEFAULT_LISTEN_PORT));
221 } catch (NumberFormatException e) {
222 throw new RuntimeException("Could not parse the value provided for the port option", e);
223 }
224
225 boolean nonblocking = cmd.hasOption("nonblocking");
226 boolean hsha = cmd.hasOption("hsha");
227
228 Configuration conf = HBaseConfiguration.create();
229 ThriftMetrics metrics = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.TWO);
230
231 String implType = "threadpool";
232 if (nonblocking) {
233 implType = "nonblocking";
234 } else if (hsha) {
235 implType = "hsha";
236 }
237
238 conf.set("hbase.regionserver.thrift.server.type", implType);
239 conf.setInt("hbase.regionserver.thrift.port", listenPort);
240
241
242 boolean compact = cmd.hasOption("compact");
243 TProtocolFactory protocolFactory = getTProtocolFactory(compact);
244 THBaseService.Iface handler =
245 ThriftHBaseServiceHandler.newInstance(conf, metrics);
246 THBaseService.Processor processor = new THBaseService.Processor(handler);
247 conf.setBoolean("hbase.regionserver.thrift.compact", compact);
248
249 boolean framed = cmd.hasOption("framed") || nonblocking || hsha;
250 TTransportFactory transportFactory = getTTransportFactory(framed);
251 InetSocketAddress inetSocketAddress = bindToPort(cmd.getOptionValue("bind"), listenPort);
252 conf.setBoolean("hbase.regionserver.thrift.framed", framed);
253
254
255 try {
256 if (cmd.hasOption("infoport")) {
257 String val = cmd.getOptionValue("infoport");
258 conf.setInt("hbase.thrift.info.port", Integer.valueOf(val));
259 log.debug("Web UI port set to " + val);
260 }
261 } catch (NumberFormatException e) {
262 log.error("Could not parse the value provided for the infoport option", e);
263 printUsage();
264 System.exit(1);
265 }
266
267
268 int port = conf.getInt("hbase.thrift.info.port", 9095);
269 if (port >= 0) {
270 conf.setLong("startcode", System.currentTimeMillis());
271 String a = conf.get("hbase.thrift.info.bindAddress", "0.0.0.0");
272 InfoServer infoServer = new InfoServer("thrift", a, port, false, conf);
273 infoServer.setAttribute("hbase.conf", conf);
274 infoServer.start();
275 }
276
277 if (nonblocking) {
278 server = getTNonBlockingServer(protocolFactory, processor, transportFactory, inetSocketAddress);
279 } else if (hsha) {
280 server = getTHsHaServer(protocolFactory, processor, transportFactory, inetSocketAddress, metrics);
281 } else {
282 server = getTThreadPoolServer(protocolFactory, processor, transportFactory, inetSocketAddress);
283 }
284 } catch (Exception e) {
285 log.error(e.getMessage(), e);
286 printUsage();
287 System.exit(1);
288 }
289 server.serve();
290 }
291 }