View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.thrift2;
20  
21  import java.io.IOException;
22  import java.net.InetAddress;
23  import java.net.InetSocketAddress;
24  import java.net.UnknownHostException;
25  import java.security.PrivilegedAction;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.concurrent.ExecutorService;
30  import java.util.concurrent.LinkedBlockingQueue;
31  import java.util.concurrent.ThreadPoolExecutor;
32  import java.util.concurrent.TimeUnit;
33  
34  import javax.security.auth.callback.Callback;
35  import javax.security.auth.callback.UnsupportedCallbackException;
36  import javax.security.sasl.AuthorizeCallback;
37  import javax.security.sasl.Sasl;
38  import javax.security.sasl.SaslServer;
39  
40  import org.apache.commons.cli.CommandLine;
41  import org.apache.commons.cli.CommandLineParser;
42  import org.apache.commons.cli.HelpFormatter;
43  import org.apache.commons.cli.Option;
44  import org.apache.commons.cli.OptionGroup;
45  import org.apache.commons.cli.Options;
46  import org.apache.commons.cli.ParseException;
47  import org.apache.commons.cli.PosixParser;
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  import org.apache.hadoop.hbase.classification.InterfaceAudience;
51  import org.apache.hadoop.conf.Configuration;
52  import org.apache.hadoop.hbase.HBaseConfiguration;
53  import org.apache.hadoop.hbase.filter.ParseFilter;
54  import org.apache.hadoop.hbase.security.SaslUtil;
55  import org.apache.hadoop.hbase.security.SecurityUtil;
56  import org.apache.hadoop.hbase.security.UserProvider;
57  import org.apache.hadoop.hbase.thrift.CallQueue;
58  import org.apache.hadoop.hbase.thrift.CallQueue.Call;
59  import org.apache.hadoop.hbase.thrift.ThriftMetrics;
60  import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
61  import org.apache.hadoop.hbase.util.DNS;
62  import org.apache.hadoop.hbase.util.InfoServer;
63  import org.apache.hadoop.hbase.util.Strings;
64  import org.apache.hadoop.security.UserGroupInformation;
65  import org.apache.hadoop.security.SaslRpcServer.SaslGssCallbackHandler;
66  import org.apache.hadoop.util.GenericOptionsParser;
67  import org.apache.thrift.TException;
68  import org.apache.thrift.TProcessor;
69  import org.apache.thrift.protocol.TBinaryProtocol;
70  import org.apache.thrift.protocol.TCompactProtocol;
71  import org.apache.thrift.protocol.TProtocol;
72  import org.apache.thrift.protocol.TProtocolFactory;
73  import org.apache.thrift.server.THsHaServer;
74  import org.apache.thrift.server.TNonblockingServer;
75  import org.apache.thrift.server.TServer;
76  import org.apache.thrift.server.TThreadPoolServer;
77  import org.apache.thrift.transport.TFramedTransport;
78  import org.apache.thrift.transport.TNonblockingServerSocket;
79  import org.apache.thrift.transport.TNonblockingServerTransport;
80  import org.apache.thrift.transport.TSaslServerTransport;
81  import org.apache.thrift.transport.TServerSocket;
82  import org.apache.thrift.transport.TServerTransport;
83  import org.apache.thrift.transport.TTransportException;
84  import org.apache.thrift.transport.TTransportFactory;
85  
86  import com.google.common.util.concurrent.ThreadFactoryBuilder;
87  
88  /**
89   * ThriftServer - this class starts up a Thrift server which implements the HBase API specified in the
90   * HbaseClient.thrift IDL file.
91   */
92  @InterfaceAudience.Private
93  @SuppressWarnings({ "rawtypes", "unchecked" })
94  public class ThriftServer {
95    private static final Log log = LogFactory.getLog(ThriftServer.class);
96  
97    /**
98     * Thrift quality of protection configuration key. Valid values can be:
99     * privacy: authentication, integrity and confidentiality checking
100    * integrity: authentication and integrity checking
101    * authentication: authentication only
102    *
103    * This is used to authenticate the callers and support impersonation.
104    * The thrift server and the HBase cluster must run in secure mode.
105    */
106   static final String THRIFT_QOP_KEY = "hbase.thrift.security.qop";
107 
108   public static final int DEFAULT_LISTEN_PORT = 9090;
109 
110   
111   public ThriftServer() {
112   }
113 
114   private static void printUsage() {
115     HelpFormatter formatter = new HelpFormatter();
116     formatter.printHelp("Thrift", null, getOptions(),
117         "To start the Thrift server run 'bin/hbase-daemon.sh start thrift2'\n" +
118             "To shutdown the thrift server run 'bin/hbase-daemon.sh stop thrift2' or" +
119             " send a kill signal to the thrift server pid",
120         true);
121   }
122 
123   private static Options getOptions() {
124     Options options = new Options();
125     options.addOption("b", "bind", true,
126         "Address to bind the Thrift server to. [default: 0.0.0.0]");
127     options.addOption("p", "port", true, "Port to bind to [default: " + DEFAULT_LISTEN_PORT + "]");
128     options.addOption("f", "framed", false, "Use framed transport");
129     options.addOption("c", "compact", 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     OptionGroup servers = new OptionGroup();
134     servers.addOption(
135         new Option("nonblocking", false, "Use the TNonblockingServer. This implies the framed transport."));
136     servers.addOption(new Option("hsha", false, "Use the THsHaServer. This implies the framed transport."));
137     servers.addOption(new Option("threadpool", false, "Use the TThreadPoolServer. This is the default."));
138     options.addOptionGroup(servers);
139     return options;
140   }
141 
142   private static CommandLine parseArguments(Configuration conf, Options options, String[] args)
143       throws ParseException, IOException {
144     GenericOptionsParser genParser = new GenericOptionsParser(conf, args);
145     String[] remainingArgs = genParser.getRemainingArgs();
146     CommandLineParser parser = new PosixParser();
147     return parser.parse(options, remainingArgs);
148   }
149 
150   private static TProtocolFactory getTProtocolFactory(boolean isCompact) {
151     if (isCompact) {
152       log.debug("Using compact protocol");
153       return new TCompactProtocol.Factory();
154     } else {
155       log.debug("Using binary protocol");
156       return new TBinaryProtocol.Factory();
157     }
158   }
159 
160   private static TTransportFactory getTTransportFactory(
161       SaslUtil.QualityOfProtection qop, String name, String host,
162       boolean framed, int frameSize) {
163     if (framed) {
164       if (qop != null) {
165         throw new RuntimeException("Thrift server authentication"
166           + " doesn't work with framed transport yet");
167       }
168       log.debug("Using framed transport");
169       return new TFramedTransport.Factory(frameSize);
170     } else if (qop == null) {
171       return new TTransportFactory();
172     } else {
173       Map<String, String> saslProperties = new HashMap<String, String>();
174       saslProperties.put(Sasl.QOP, qop.getSaslQop());
175       TSaslServerTransport.Factory saslFactory = new TSaslServerTransport.Factory();
176       saslFactory.addServerDefinition("GSSAPI", name, host, saslProperties,
177         new SaslGssCallbackHandler() {
178           @Override
179           public void handle(Callback[] callbacks)
180               throws UnsupportedCallbackException {
181             AuthorizeCallback ac = null;
182             for (Callback callback : callbacks) {
183               if (callback instanceof AuthorizeCallback) {
184                 ac = (AuthorizeCallback) callback;
185               } else {
186                 throw new UnsupportedCallbackException(callback,
187                     "Unrecognized SASL GSSAPI Callback");
188               }
189             }
190             if (ac != null) {
191               String authid = ac.getAuthenticationID();
192               String authzid = ac.getAuthorizationID();
193               if (!authid.equals(authzid)) {
194                 ac.setAuthorized(false);
195               } else {
196                 ac.setAuthorized(true);
197                 String userName = SecurityUtil.getUserFromPrincipal(authzid);
198                 log.info("Effective user: " + userName);
199                 ac.setAuthorizedID(userName);
200               }
201             }
202           }
203         });
204       return saslFactory;
205     }
206   }
207 
208   /*
209    * If bindValue is null, we don't bind.
210    */
211   private static InetSocketAddress bindToPort(String bindValue, int listenPort)
212       throws UnknownHostException {
213     try {
214       if (bindValue == null) {
215         return new InetSocketAddress(listenPort);
216       } else {
217         return new InetSocketAddress(InetAddress.getByName(bindValue), listenPort);
218       }
219     } catch (UnknownHostException e) {
220       throw new RuntimeException("Could not bind to provided ip address", e);
221     }
222   }
223 
224   private static TServer getTNonBlockingServer(TProtocolFactory protocolFactory, TProcessor processor,
225       TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws TTransportException {
226     TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress);
227     log.info("starting HBase Nonblocking Thrift server on " + inetSocketAddress.toString());
228     TNonblockingServer.Args serverArgs = new TNonblockingServer.Args(serverTransport);
229     serverArgs.processor(processor);
230     serverArgs.transportFactory(transportFactory);
231     serverArgs.protocolFactory(protocolFactory);
232     return new TNonblockingServer(serverArgs);
233   }
234 
235   private static TServer getTHsHaServer(TProtocolFactory protocolFactory,
236       TProcessor processor, TTransportFactory transportFactory,
237       InetSocketAddress inetSocketAddress, ThriftMetrics metrics)
238       throws TTransportException {
239     TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(inetSocketAddress);
240     log.info("starting HBase HsHA Thrift server on " + inetSocketAddress.toString());
241     THsHaServer.Args serverArgs = new THsHaServer.Args(serverTransport);
242     ExecutorService executorService = createExecutor(
243         serverArgs.getWorkerThreads(), metrics);
244     serverArgs.executorService(executorService);
245     serverArgs.processor(processor);
246     serverArgs.transportFactory(transportFactory);
247     serverArgs.protocolFactory(protocolFactory);
248     return new THsHaServer(serverArgs);
249   }
250 
251   private static ExecutorService createExecutor(
252       int workerThreads, ThriftMetrics metrics) {
253     CallQueue callQueue = new CallQueue(
254         new LinkedBlockingQueue<Call>(), metrics);
255     ThreadFactoryBuilder tfb = new ThreadFactoryBuilder();
256     tfb.setDaemon(true);
257     tfb.setNameFormat("thrift2-worker-%d");
258     return new ThreadPoolExecutor(workerThreads, workerThreads,
259             Long.MAX_VALUE, TimeUnit.SECONDS, callQueue, tfb.build());
260   }
261 
262   private static TServer getTThreadPoolServer(TProtocolFactory protocolFactory, TProcessor processor,
263       TTransportFactory transportFactory, InetSocketAddress inetSocketAddress) throws TTransportException {
264     TServerTransport serverTransport = new TServerSocket(inetSocketAddress);
265     log.info("starting HBase ThreadPool Thrift server on " + inetSocketAddress.toString());
266     TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
267     serverArgs.processor(processor);
268     serverArgs.transportFactory(transportFactory);
269     serverArgs.protocolFactory(protocolFactory);
270     return new TThreadPoolServer(serverArgs);
271   }
272 
273   /**
274    * Adds the option to pre-load filters at startup.
275    *
276    * @param conf  The current configuration instance.
277    */
278   protected static void registerFilters(Configuration conf) {
279     String[] filters = conf.getStrings("hbase.thrift.filters");
280     if(filters != null) {
281       for(String filterClass: filters) {
282         String[] filterPart = filterClass.split(":");
283         if(filterPart.length != 2) {
284           log.warn("Invalid filter specification " + filterClass + " - skipping");
285         } else {
286           ParseFilter.registerFilter(filterPart[0], filterPart[1]);
287         }
288       }
289     }
290   }
291 
292   /**
293    * Start up the Thrift2 server.
294    *
295    * @param args
296    */
297   public static void main(String[] args) throws Exception {
298     TServer server = null;
299     Options options = getOptions();
300     Configuration conf = HBaseConfiguration.create();
301     CommandLine cmd = parseArguments(conf, options, args);
302 
303     /**
304      * This is to please both bin/hbase and bin/hbase-daemon. hbase-daemon provides "start" and "stop" arguments hbase
305      * should print the help if no argument is provided
306      */
307     List<?> argList = cmd.getArgList();
308     if (cmd.hasOption("help") || !argList.contains("start") || argList.contains("stop")) {
309       printUsage();
310       System.exit(1);
311     }
312 
313     // Get address to bind
314     String bindAddress;
315     if (cmd.hasOption("bind")) {
316       bindAddress = cmd.getOptionValue("bind");
317       conf.set("hbase.thrift.info.bindAddress", bindAddress);
318     } else {
319       bindAddress = conf.get("hbase.thrift.info.bindAddress");
320     }
321 
322     // Get port to bind to
323     int listenPort = 0;
324     try {
325       if (cmd.hasOption("port")) {
326         listenPort = Integer.parseInt(cmd.getOptionValue("port"));
327       } else {
328         listenPort = conf.getInt("hbase.regionserver.thrift.port", DEFAULT_LISTEN_PORT);
329       }
330     } catch (NumberFormatException e) {
331       throw new RuntimeException("Could not parse the value provided for the port option", e);
332     }
333 
334     // Local hostname and user name,
335     // used only if QOP is configured.
336     String host = null;
337     String name = null;
338 
339     UserProvider userProvider = UserProvider.instantiate(conf);
340     // login the server principal (if using secure Hadoop)
341     boolean securityEnabled = userProvider.isHadoopSecurityEnabled()
342       && userProvider.isHBaseSecurityEnabled();
343     if (securityEnabled) {
344       host = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
345         conf.get("hbase.thrift.dns.interface", "default"),
346         conf.get("hbase.thrift.dns.nameserver", "default")));
347       userProvider.login("hbase.thrift.keytab.file",
348         "hbase.thrift.kerberos.principal", host);
349     }
350 
351     UserGroupInformation realUser = userProvider.getCurrent().getUGI();
352     String stringQop = conf.get(THRIFT_QOP_KEY);
353     SaslUtil.QualityOfProtection qop = null;
354     if (stringQop != null) {
355       qop = SaslUtil.getQop(stringQop);
356       if (!securityEnabled) {
357         throw new IOException("Thrift server must"
358           + " run in secure mode to support authentication");
359       }
360       // Extract the name from the principal
361       name = SecurityUtil.getUserFromPrincipal(
362         conf.get("hbase.thrift.kerberos.principal"));
363     }
364 
365     boolean nonblocking = cmd.hasOption("nonblocking");
366     boolean hsha = cmd.hasOption("hsha");
367 
368     ThriftMetrics metrics = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.TWO);
369 
370     String implType = "threadpool";
371     if (nonblocking) {
372       implType = "nonblocking";
373     } else if (hsha) {
374       implType = "hsha";
375     }
376 
377     conf.set("hbase.regionserver.thrift.server.type", implType);
378     conf.setInt("hbase.regionserver.thrift.port", listenPort);
379     registerFilters(conf);
380 
381     // Construct correct ProtocolFactory
382     boolean compact = cmd.hasOption("compact") ||
383         conf.getBoolean("hbase.regionserver.thrift.compact", false);
384     TProtocolFactory protocolFactory = getTProtocolFactory(compact);
385     final ThriftHBaseServiceHandler hbaseHandler =
386       new ThriftHBaseServiceHandler(conf, userProvider);
387     THBaseService.Iface handler =
388       ThriftHBaseServiceHandler.newInstance(hbaseHandler, metrics);
389     final THBaseService.Processor p = new THBaseService.Processor(handler);
390     conf.setBoolean("hbase.regionserver.thrift.compact", compact);
391     TProcessor processor = p;
392 
393     boolean framed = cmd.hasOption("framed") ||
394         conf.getBoolean("hbase.regionserver.thrift.framed", false) || nonblocking || hsha;
395     TTransportFactory transportFactory = getTTransportFactory(qop, name, host, framed,
396         conf.getInt("hbase.regionserver.thrift.framed.max_frame_size_in_mb", 2) * 1024 * 1024);
397     InetSocketAddress inetSocketAddress = bindToPort(bindAddress, listenPort);
398     conf.setBoolean("hbase.regionserver.thrift.framed", framed);
399     if (qop != null) {
400       // Create a processor wrapper, to get the caller
401       processor = new TProcessor() {
402         @Override
403         public boolean process(TProtocol inProt,
404             TProtocol outProt) throws TException {
405           TSaslServerTransport saslServerTransport =
406             (TSaslServerTransport)inProt.getTransport();
407           SaslServer saslServer = saslServerTransport.getSaslServer();
408           String principal = saslServer.getAuthorizationID();
409           hbaseHandler.setEffectiveUser(principal);
410           return p.process(inProt, outProt);
411         }
412       };
413     }
414 
415     // check for user-defined info server port setting, if so override the conf
416     try {
417       if (cmd.hasOption("infoport")) {
418         String val = cmd.getOptionValue("infoport");
419         conf.setInt("hbase.thrift.info.port", Integer.valueOf(val));
420         log.debug("Web UI port set to " + val);
421       }
422     } catch (NumberFormatException e) {
423       log.error("Could not parse the value provided for the infoport option", e);
424       printUsage();
425       System.exit(1);
426     }
427 
428     // Put up info server.
429     int port = conf.getInt("hbase.thrift.info.port", 9095);
430     if (port >= 0) {
431       conf.setLong("startcode", System.currentTimeMillis());
432       String a = conf.get("hbase.thrift.info.bindAddress", "0.0.0.0");
433       InfoServer infoServer = new InfoServer("thrift", a, port, false, conf);
434       infoServer.setAttribute("hbase.conf", conf);
435       infoServer.start();
436     }
437 
438     if (nonblocking) {
439       server = getTNonBlockingServer(protocolFactory, processor, transportFactory, inetSocketAddress);
440     } else if (hsha) {
441       server = getTHsHaServer(protocolFactory, processor, transportFactory, inetSocketAddress, metrics);
442     } else {
443       server = getTThreadPoolServer(protocolFactory, processor, transportFactory, inetSocketAddress);
444     }
445 
446     final TServer tserver = server;
447     realUser.doAs(
448       new PrivilegedAction<Object>() {
449         @Override
450         public Object run() {
451           tserver.serve();
452           return null;
453         }
454       });
455   }
456 }