1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift;
20
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.apache.commons.cli.CommandLine;
25 import org.apache.commons.cli.CommandLineParser;
26 import org.apache.commons.cli.HelpFormatter;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.PosixParser;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.classification.InterfaceAudience;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.HBaseConfiguration;
34 import org.apache.hadoop.hbase.security.User;
35 import org.apache.hadoop.hbase.thrift.ThriftServerRunner.ImplType;
36 import org.apache.hadoop.hbase.util.InfoServer;
37 import org.apache.hadoop.hbase.util.Strings;
38 import org.apache.hadoop.hbase.util.VersionInfo;
39 import org.apache.hadoop.net.DNS;
40 import org.apache.hadoop.util.Shell.ExitCodeException;
41
42
43
44
45
46
47 @InterfaceAudience.Private
48 public class ThriftServer {
49
50 private static final Log LOG = LogFactory.getLog(ThriftServer.class);
51
52 private static final String MIN_WORKERS_OPTION = "minWorkers";
53 private static final String MAX_WORKERS_OPTION = "workers";
54 private static final String MAX_QUEUE_SIZE_OPTION = "queue";
55 private static final String KEEP_ALIVE_SEC_OPTION = "keepAliveSec";
56 static final String BIND_OPTION = "bind";
57 static final String COMPACT_OPTION = "compact";
58 static final String FRAMED_OPTION = "framed";
59 static final String PORT_OPTION = "port";
60
61 private static final String DEFAULT_BIND_ADDR = "0.0.0.0";
62 private static final int DEFAULT_LISTEN_PORT = 9090;
63
64 private Configuration conf;
65 ThriftServerRunner serverRunner;
66
67 private InfoServer infoServer;
68
69
70
71
72
73 public ThriftServer(Configuration conf) {
74 this.conf = HBaseConfiguration.create(conf);
75 }
76
77 private static void printUsageAndExit(Options options, int exitCode)
78 throws ExitCodeException {
79 HelpFormatter formatter = new HelpFormatter();
80 formatter.printHelp("Thrift", null, options,
81 "To start the Thrift server run 'bin/hbase-daemon.sh start thrift'\n" +
82 "To shutdown the thrift server run 'bin/hbase-daemon.sh stop " +
83 "thrift' or send a kill signal to the thrift server pid",
84 true);
85 throw new ExitCodeException(exitCode, "");
86 }
87
88
89
90
91
92 void doMain(final String[] args) throws Exception {
93 processOptions(args);
94
95
96 if (User.isSecurityEnabled() && User.isHBaseSecurityEnabled(conf)) {
97 String machineName = Strings.domainNamePointerToHostName(
98 DNS.getDefaultHost(conf.get("hbase.thrift.dns.interface", "default"),
99 conf.get("hbase.thrift.dns.nameserver", "default")));
100 User.login(conf, "hbase.thrift.keytab.file",
101 "hbase.thrift.kerberos.principal", machineName);
102 }
103
104 serverRunner = new ThriftServerRunner(conf);
105
106
107 int port = conf.getInt("hbase.thrift.info.port", 9095);
108 if (port >= 0) {
109 conf.setLong("startcode", System.currentTimeMillis());
110 String a = conf.get("hbase.thrift.info.bindAddress", "0.0.0.0");
111 infoServer = new InfoServer("thrift", a, port, false, conf);
112 infoServer.setAttribute("hbase.conf", conf);
113 infoServer.start();
114 }
115 serverRunner.run();
116 }
117
118
119
120
121 private void processOptions(final String[] args) throws Exception {
122 Options options = new Options();
123 options.addOption("b", BIND_OPTION, true, "Address to bind " +
124 "the Thrift server to. [default: " + DEFAULT_BIND_ADDR + "]");
125 options.addOption("p", PORT_OPTION, true, "Port to bind to [default: " +
126 DEFAULT_LISTEN_PORT + "]");
127 options.addOption("f", FRAMED_OPTION, false, "Use framed transport");
128 options.addOption("c", COMPACT_OPTION, false, "Use the compact protocol");
129 options.addOption("h", "help", false, "Print help information");
130 options.addOption(null, "infoport", true, "Port for web UI");
131
132 options.addOption("m", MIN_WORKERS_OPTION, true,
133 "The minimum number of worker threads for " +
134 ImplType.THREAD_POOL.simpleClassName());
135
136 options.addOption("w", MAX_WORKERS_OPTION, true,
137 "The maximum number of worker threads for " +
138 ImplType.THREAD_POOL.simpleClassName());
139
140 options.addOption("q", MAX_QUEUE_SIZE_OPTION, true,
141 "The maximum number of queued requests in " +
142 ImplType.THREAD_POOL.simpleClassName());
143
144 options.addOption("k", KEEP_ALIVE_SEC_OPTION, true,
145 "The amount of time in secods to keep a thread alive when idle in " +
146 ImplType.THREAD_POOL.simpleClassName());
147
148 options.addOptionGroup(ImplType.createOptionGroup());
149
150 CommandLineParser parser = new PosixParser();
151 CommandLine cmd = parser.parse(options, args);
152
153
154
155
156 List<String> commandLine = Arrays.asList(args);
157 boolean stop = commandLine.contains("stop");
158 boolean start = commandLine.contains("start");
159 boolean invalidStartStop = (start && stop) || (!start && !stop);
160 if (cmd.hasOption("help") || invalidStartStop) {
161 if (invalidStartStop) {
162 LOG.error("Exactly one of 'start' and 'stop' has to be specified");
163 }
164 printUsageAndExit(options, 1);
165 }
166
167
168 try {
169 int listenPort = Integer.parseInt(cmd.getOptionValue(PORT_OPTION,
170 String.valueOf(DEFAULT_LISTEN_PORT)));
171 conf.setInt(ThriftServerRunner.PORT_CONF_KEY, listenPort);
172 } catch (NumberFormatException e) {
173 LOG.error("Could not parse the value provided for the port option", e);
174 printUsageAndExit(options, -1);
175 }
176
177
178 try {
179 if (cmd.hasOption("infoport")) {
180 String val = cmd.getOptionValue("infoport");
181 conf.setInt("hbase.thrift.info.port", Integer.valueOf(val));
182 LOG.debug("Web UI port set to " + val);
183 }
184 } catch (NumberFormatException e) {
185 LOG.error("Could not parse the value provided for the infoport option", e);
186 printUsageAndExit(options, -1);
187 }
188
189
190 optionToConf(cmd, MIN_WORKERS_OPTION,
191 conf, TBoundedThreadPoolServer.MIN_WORKER_THREADS_CONF_KEY);
192 optionToConf(cmd, MAX_WORKERS_OPTION,
193 conf, TBoundedThreadPoolServer.MAX_WORKER_THREADS_CONF_KEY);
194 optionToConf(cmd, MAX_QUEUE_SIZE_OPTION,
195 conf, TBoundedThreadPoolServer.MAX_QUEUED_REQUESTS_CONF_KEY);
196 optionToConf(cmd, KEEP_ALIVE_SEC_OPTION,
197 conf, TBoundedThreadPoolServer.THREAD_KEEP_ALIVE_TIME_SEC_CONF_KEY);
198
199
200 conf.setBoolean(
201 ThriftServerRunner.COMPACT_CONF_KEY, cmd.hasOption(COMPACT_OPTION));
202 conf.setBoolean(
203 ThriftServerRunner.FRAMED_CONF_KEY, cmd.hasOption(FRAMED_OPTION));
204 if (cmd.hasOption(BIND_OPTION)) {
205 conf.set(
206 ThriftServerRunner.BIND_CONF_KEY, cmd.getOptionValue(BIND_OPTION));
207 }
208
209 ImplType.setServerImpl(cmd, conf);
210 }
211
212 public void stop() {
213 if (this.infoServer != null) {
214 LOG.info("Stopping infoServer");
215 try {
216 this.infoServer.stop();
217 } catch (Exception ex) {
218 ex.printStackTrace();
219 }
220 }
221 serverRunner.shutdown();
222 }
223
224 private static void optionToConf(CommandLine cmd, String option,
225 Configuration conf, String destConfKey) {
226 if (cmd.hasOption(option)) {
227 String value = cmd.getOptionValue(option);
228 LOG.info("Set configuration key:" + destConfKey + " value:" + value);
229 conf.set(destConfKey, value);
230 }
231 }
232
233
234
235
236
237 public static void main(String [] args) throws Exception {
238 VersionInfo.logVersion();
239 try {
240 new ThriftServer(HBaseConfiguration.create()).doMain(args);
241 } catch (ExitCodeException ex) {
242 System.exit(ex.getExitCode());
243 }
244 }
245 }