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.conf.Configuration;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.client.UserProvider;
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 public class ThriftServer {
48
49 private static final Log LOG = LogFactory.getLog(ThriftServer.class);
50
51 private static final String MIN_WORKERS_OPTION = "minWorkers";
52 private static final String MAX_WORKERS_OPTION = "workers";
53 private static final String MAX_QUEUE_SIZE_OPTION = "queue";
54 private static final String KEEP_ALIVE_SEC_OPTION = "keepAliveSec";
55 static final String BIND_OPTION = "bind";
56 static final String COMPACT_OPTION = "compact";
57 static final String FRAMED_OPTION = "framed";
58 static final String PORT_OPTION = "port";
59
60 private static final String DEFAULT_BIND_ADDR = "0.0.0.0";
61 private static final int DEFAULT_LISTEN_PORT = 9090;
62
63 private Configuration conf;
64 ThriftServerRunner serverRunner;
65
66 private InfoServer infoServer;
67
68
69
70
71
72 public ThriftServer(Configuration conf) {
73 this.conf = HBaseConfiguration.create(conf);
74 }
75
76 private static void printUsageAndExit(Options options, int exitCode)
77 throws ExitCodeException {
78 HelpFormatter formatter = new HelpFormatter();
79 formatter.printHelp("Thrift", null, options,
80 "To start the Thrift server run 'bin/hbase-daemon.sh start thrift'\n" +
81 "To shutdown the thrift server run 'bin/hbase-daemon.sh stop " +
82 "thrift' or send a kill signal to the thrift server pid",
83 true);
84 throw new ExitCodeException(exitCode, "");
85 }
86
87
88
89
90
91 void doMain(final String[] args) throws Exception {
92 processOptions(args);
93
94 UserProvider provider = UserProvider.instantiate(conf);
95 if (provider.isHadoopSecurityEnabled() && provider.isHBaseSecurityEnabled()) {
96 String machineName =
97 Strings.domainNamePointerToHostName(DNS.getDefaultHost(
98 conf.get("hbase.thrift.dns.interface", "default"),
99 conf.get("hbase.thrift.dns.nameserver", "default")));
100
101 provider.login("hbase.thrift.keytab.file",
102 "hbase.thrift.kerberos.principal", machineName);
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. Not supported by the Nonblocking and " +
125 "HsHa server [default: " + DEFAULT_BIND_ADDR + "]");
126 options.addOption("p", PORT_OPTION, true, "Port to bind to [default: " +
127 DEFAULT_LISTEN_PORT + "]");
128 options.addOption("f", FRAMED_OPTION, false, "Use framed transport");
129 options.addOption("c", COMPACT_OPTION, false, "Use the compact protocol");
130 options.addOption("h", "help", false, "Print help information");
131 options.addOption(null, "infoport", true, "Port for web UI");
132
133 options.addOption("m", MIN_WORKERS_OPTION, true,
134 "The minimum number of worker threads for " +
135 ImplType.THREAD_POOL.simpleClassName());
136
137 options.addOption("w", MAX_WORKERS_OPTION, true,
138 "The maximum number of worker threads for " +
139 ImplType.THREAD_POOL.simpleClassName());
140
141 options.addOption("q", MAX_QUEUE_SIZE_OPTION, true,
142 "The maximum number of queued requests in " +
143 ImplType.THREAD_POOL.simpleClassName());
144
145 options.addOption("k", KEEP_ALIVE_SEC_OPTION, true,
146 "The amount of time in secods to keep a thread alive when idle in " +
147 ImplType.THREAD_POOL.simpleClassName());
148
149 options.addOptionGroup(ImplType.createOptionGroup());
150
151 CommandLineParser parser = new PosixParser();
152 CommandLine cmd = parser.parse(options, args);
153
154
155
156
157 List<String> commandLine = Arrays.asList(args);
158 boolean stop = commandLine.contains("stop");
159 boolean start = commandLine.contains("start");
160 boolean invalidStartStop = (start && stop) || (!start && !stop);
161 if (cmd.hasOption("help") || invalidStartStop) {
162 if (invalidStartStop) {
163 LOG.error("Exactly one of 'start' and 'stop' has to be specified");
164 }
165 printUsageAndExit(options, 1);
166 }
167
168
169 try {
170 int listenPort = Integer.parseInt(cmd.getOptionValue(PORT_OPTION,
171 String.valueOf(DEFAULT_LISTEN_PORT)));
172 conf.setInt(ThriftServerRunner.PORT_CONF_KEY, listenPort);
173 } catch (NumberFormatException e) {
174 LOG.error("Could not parse the value provided for the port option", e);
175 printUsageAndExit(options, -1);
176 }
177
178
179 try {
180 if (cmd.hasOption("infoport")) {
181 String val = cmd.getOptionValue("infoport");
182 conf.setInt("hbase.thrift.info.port", Integer.valueOf(val));
183 LOG.debug("Web UI port set to " + val);
184 }
185 } catch (NumberFormatException e) {
186 LOG.error("Could not parse the value provided for the infoport option", e);
187 printUsageAndExit(options, -1);
188 }
189
190
191 optionToConf(cmd, MIN_WORKERS_OPTION,
192 conf, TBoundedThreadPoolServer.MIN_WORKER_THREADS_CONF_KEY);
193 optionToConf(cmd, MAX_WORKERS_OPTION,
194 conf, TBoundedThreadPoolServer.MAX_WORKER_THREADS_CONF_KEY);
195 optionToConf(cmd, MAX_QUEUE_SIZE_OPTION,
196 conf, TBoundedThreadPoolServer.MAX_QUEUED_REQUESTS_CONF_KEY);
197 optionToConf(cmd, KEEP_ALIVE_SEC_OPTION,
198 conf, TBoundedThreadPoolServer.THREAD_KEEP_ALIVE_TIME_SEC_CONF_KEY);
199
200
201 boolean compact = cmd.hasOption(COMPACT_OPTION) ||
202 conf.getBoolean(ThriftServerRunner.COMPACT_CONF_KEY, false);
203 conf.setBoolean(ThriftServerRunner.COMPACT_CONF_KEY, compact);
204 boolean framed = cmd.hasOption(FRAMED_OPTION) ||
205 conf.getBoolean(ThriftServerRunner.FRAMED_CONF_KEY, false);
206 conf.setBoolean(ThriftServerRunner.FRAMED_CONF_KEY, framed);
207 if (cmd.hasOption(BIND_OPTION)) {
208 conf.set(
209 ThriftServerRunner.BIND_CONF_KEY, cmd.getOptionValue(BIND_OPTION));
210 }
211
212 ImplType.setServerImpl(cmd, conf);
213 }
214
215 public void stop() {
216 if (this.infoServer != null) {
217 LOG.info("Stopping infoServer");
218 try {
219 this.infoServer.stop();
220 } catch (Exception ex) {
221 ex.printStackTrace();
222 }
223 }
224 serverRunner.shutdown();
225 }
226
227 private static void optionToConf(CommandLine cmd, String option,
228 Configuration conf, String destConfKey) {
229 if (cmd.hasOption(option)) {
230 String value = cmd.getOptionValue(option);
231 LOG.info("Set configuration key:" + destConfKey + " value:" + value);
232 conf.set(destConfKey, value);
233 }
234 }
235
236
237
238
239
240 public static void main(String [] args) throws Exception {
241 VersionInfo.logVersion();
242 try {
243 new ThriftServer(HBaseConfiguration.create()).doMain(args);
244 } catch (ExitCodeException ex) {
245 System.exit(ex.getExitCode());
246 }
247 }
248 }