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