Package org.apache.thrift.server

Examples of org.apache.thrift.server.TServer


        this.serverType = serverType;
    }

    public TServer buildTServer(TServerFactory.Args args)
    {
        TServer server;
        if (ThriftServer.SYNC.equalsIgnoreCase(serverType))
        {
            server = new CustomTThreadPoolServer.Factory().buildTServer(args);
            logger.info(String.format("Using synchronous/threadpool thrift server on %s : %s", args.addr.getHostName(), args.addr.getPort()));
        }
        else if(ThriftServer.ASYNC.equalsIgnoreCase(serverType))
        {
            server = new CustomTNonBlockingServer.Factory().buildTServer(args);
            logger.info(String.format("Using non-blocking/asynchronous thrift server on %s : %s", args.addr.getHostName(), args.addr.getPort()));
        }
        else if(ThriftServer.HSHA.equalsIgnoreCase(serverType))
        {
            server = new CustomTHsHaServer.Factory().buildTServer(args);
            logger.info(String.format("Using custom half-sync/half-async thrift server on %s : %s", args.addr.getHostName(), args.addr.getPort()));
        }
        else
        {
            TServerFactory serverFactory;
            try
            {
                serverFactory = (TServerFactory) Class.forName(serverType).newInstance();
            }
            catch (Exception e)
            {
                throw new RuntimeException("Failed to instantiate server factory:" + serverType, e);
            }
            server = serverFactory.buildTServer(args);
            logger.info(String.format("Using custom thrift server %s on %s : %s", server.getClass().getName(), args.addr.getHostName(), args.addr.getPort()));
        }
        return server;
    }
View Full Code Here


   * Start up the Thrift2 server.
   *
   * @param args
   */
  public static void main(String[] args) throws Exception {
    TServer server = null;
    Options options = getOptions();
    Configuration conf = HBaseConfiguration.create();
    CommandLine cmd = parseArguments(conf, options, args);

    /**
     * This is to please both bin/hbase and bin/hbase-daemon. hbase-daemon provides "start" and "stop" arguments hbase
     * should print the help if no argument is provided
     */
    List<?> argList = cmd.getArgList();
    if (cmd.hasOption("help") || !argList.contains("start") || argList.contains("stop")) {
      printUsage();
      System.exit(1);
    }

    // Get port to bind to
    int listenPort = 0;
    try {
      if (cmd.hasOption("port")) {
        listenPort = Integer.parseInt(cmd.getOptionValue("port"));
      } else {
        listenPort = conf.getInt("hbase.regionserver.thrift.port", DEFAULT_LISTEN_PORT);
      }
    } catch (NumberFormatException e) {
      throw new RuntimeException("Could not parse the value provided for the port option", e);
    }

    // Local hostname and user name,
    // used only if QOP is configured.
    String host = null;
    String name = null;

    UserProvider userProvider = UserProvider.instantiate(conf);
    // login the server principal (if using secure Hadoop)
    boolean securityEnabled = userProvider.isHadoopSecurityEnabled()
      && userProvider.isHBaseSecurityEnabled();
    if (securityEnabled) {
      host = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
        conf.get("hbase.thrift.dns.interface", "default"),
        conf.get("hbase.thrift.dns.nameserver", "default")));
      userProvider.login("hbase.thrift.keytab.file",
        "hbase.thrift.kerberos.principal", host);
    }

    UserGroupInformation realUser = userProvider.getCurrent().getUGI();
    String qop = conf.get(THRIFT_QOP_KEY);
    if (qop != null) {
      if (!qop.equals("auth") && !qop.equals("auth-int")
          && !qop.equals("auth-conf")) {
        throw new IOException("Invalid " + THRIFT_QOP_KEY + ": " + qop
          + ", it must be 'auth', 'auth-int', or 'auth-conf'");
      }
      if (!securityEnabled) {
        throw new IOException("Thrift server must"
          + " run in secure mode to support authentication");
      }
      // Extract the name from the principal
      name = SecurityUtil.getUserFromPrincipal(
        conf.get("hbase.thrift.kerberos.principal"));
    }

    boolean nonblocking = cmd.hasOption("nonblocking");
    boolean hsha = cmd.hasOption("hsha");

    ThriftMetrics metrics = new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.TWO);

    String implType = "threadpool";
    if (nonblocking) {
      implType = "nonblocking";
    } else if (hsha) {
      implType = "hsha";
    }

    conf.set("hbase.regionserver.thrift.server.type", implType);
    conf.setInt("hbase.regionserver.thrift.port", listenPort);
    registerFilters(conf);

    // Construct correct ProtocolFactory
    boolean compact = cmd.hasOption("compact") ||
        conf.getBoolean("hbase.regionserver.thrift.compact", false);
    TProtocolFactory protocolFactory = getTProtocolFactory(compact);
    final ThriftHBaseServiceHandler hbaseHandler =
      new ThriftHBaseServiceHandler(conf, userProvider);
    THBaseService.Iface handler =
      ThriftHBaseServiceHandler.newInstance(hbaseHandler, metrics);
    final THBaseService.Processor p = new THBaseService.Processor(handler);
    conf.setBoolean("hbase.regionserver.thrift.compact", compact);
    TProcessor processor = p;

    boolean framed = cmd.hasOption("framed") ||
        conf.getBoolean("hbase.regionserver.thrift.framed", false) || nonblocking || hsha;
    TTransportFactory transportFactory = getTTransportFactory(qop, name, host, framed,
        conf.getInt("hbase.regionserver.thrift.framed.max_frame_size_in_mb", 2) * 1024 * 1024);
    InetSocketAddress inetSocketAddress = bindToPort(cmd.getOptionValue("bind"), listenPort);
    conf.setBoolean("hbase.regionserver.thrift.framed", framed);
    if (qop != null) {
      // Create a processor wrapper, to get the caller
      processor = new TProcessor() {
        @Override
        public boolean process(TProtocol inProt,
            TProtocol outProt) throws TException {
          TSaslServerTransport saslServerTransport =
            (TSaslServerTransport)inProt.getTransport();
          SaslServer saslServer = saslServerTransport.getSaslServer();
          String principal = saslServer.getAuthorizationID();
          hbaseHandler.setEffectiveUser(principal);
          return p.process(inProt, outProt);
        }
      };
    }

    // check for user-defined info server port setting, if so override the conf
    try {
      if (cmd.hasOption("infoport")) {
        String val = cmd.getOptionValue("infoport");
        conf.setInt("hbase.thrift.info.port", Integer.valueOf(val));
        log.debug("Web UI port set to " + val);
      }
    } catch (NumberFormatException e) {
      log.error("Could not parse the value provided for the infoport option", e);
      printUsage();
      System.exit(1);
    }

    // Put up info server.
    int port = conf.getInt("hbase.thrift.info.port", 9095);
    if (port >= 0) {
      conf.setLong("startcode", System.currentTimeMillis());
      String a = conf.get("hbase.thrift.info.bindAddress", "0.0.0.0");
      InfoServer infoServer = new InfoServer("thrift", a, port, false, conf);
      infoServer.setAttribute("hbase.conf", conf);
      infoServer.start();
    }

    if (nonblocking) {
      server = getTNonBlockingServer(protocolFactory, processor, transportFactory, inetSocketAddress);
    } else if (hsha) {
      server = getTHsHaServer(protocolFactory, processor, transportFactory, inetSocketAddress, metrics);
    } else {
      server = getTThreadPoolServer(protocolFactory, processor, transportFactory, inetSocketAddress);
    }

    final TServer tserver = server;
    realUser.doAs(
      new PrivilegedAction<Object>() {
        @Override
        public Object run() {
          tserver.serve();
          return null;
        }
      });
  }
View Full Code Here

        this.serverType = serverType;
    }

    public TServer buildTServer(TServerFactory.Args args)
    {
        TServer server;
        if (ThriftServer.SYNC.equalsIgnoreCase(serverType))
        {
            server = new CustomTThreadPoolServer.Factory().buildTServer(args);
            logger.info(String.format("Using synchronous/threadpool thrift server on %s : %s", args.addr.getHostName(), args.addr.getPort()));
        }
        else if(ThriftServer.ASYNC.equalsIgnoreCase(serverType))
        {
            server = new CustomTNonBlockingServer.Factory().buildTServer(args);
            logger.info(String.format("Using non-blocking/asynchronous thrift server on %s : %s", args.addr.getHostName(), args.addr.getPort()));
        }
        else if(ThriftServer.HSHA.equalsIgnoreCase(serverType))
        {
            server = new THsHaDisruptorServer.Factory().buildTServer(args);
            logger.info(String.format("Using custom half-sync/half-async thrift server on %s : %s", args.addr.getHostName(), args.addr.getPort()));
        }
        else
        {
            TServerFactory serverFactory;
            try
            {
                serverFactory = (TServerFactory) Class.forName(serverType).newInstance();
            }
            catch (Exception e)
            {
                throw new RuntimeException("Failed to instantiate server factory:" + serverType, e);
            }
            server = serverFactory.buildTServer(args);
            logger.info(String.format("Using custom thrift server %s on %s : %s", server.getClass().getName(), args.addr.getHostName(), args.addr.getPort()));
        }
        return server;
    }
View Full Code Here

   * Start up the Thrift2 server.
   *
   * @param args
   */
  public static void main(String[] args) throws Exception {
    TServer server = null;
    Options options = getOptions();
    try {
      CommandLine cmd = parseArguments(options, args);

      /**
       * This is to please both bin/hbase and bin/hbase-daemon. hbase-daemon provides "start" and "stop" arguments hbase
       * should print the help if no argument is provided
       */
      List<?> argList = cmd.getArgList();
      if (cmd.hasOption("help") || !argList.contains("start") || argList.contains("stop")) {
        printUsage();
        System.exit(1);
      }

      // Get port to bind to
      int listenPort = 0;
      try {
        listenPort = Integer.parseInt(cmd.getOptionValue("port", DEFAULT_LISTEN_PORT));
      } catch (NumberFormatException e) {
        throw new RuntimeException("Could not parse the value provided for the port option", e);
      }

      boolean nonblocking = cmd.hasOption("nonblocking");
      boolean hsha = cmd.hasOption("hsha");

      Configuration conf = HBaseConfiguration.create();
      ThriftMetrics metrics = new ThriftMetrics(
          listenPort, conf, THBaseService.Iface.class);

      // Construct correct ProtocolFactory
      TProtocolFactory protocolFactory = getTProtocolFactory(cmd.hasOption("compact"));
      THBaseService.Iface handler =
          ThriftHBaseServiceHandler.newInstance(conf, metrics);
      THBaseService.Processor processor = new THBaseService.Processor(handler);

      boolean framed = cmd.hasOption("framed") || nonblocking || hsha;
      TTransportFactory transportFactory = getTTransportFactory(framed);

      // TODO: Remove once HBASE-2155 is resolved
      if (cmd.hasOption("bind") && (nonblocking || hsha)) {
        log.error("The Nonblocking and HsHaServer servers don't support IP address binding at the moment." +
            " See https://issues.apache.org/jira/browse/HBASE-2155 for details.");
        printUsage();
        System.exit(1);
      }

      InetSocketAddress inetSocketAddress = bindToPort(cmd.getOptionValue("bind"), listenPort);

      if (nonblocking) {
        server = getTNonBlockingServer(protocolFactory, processor, transportFactory, inetSocketAddress);
      } else if (hsha) {
        server = getTHsHaServer(protocolFactory, processor, transportFactory, inetSocketAddress, metrics);
      } else {
        server = getTThreadPoolServer(protocolFactory, processor, transportFactory, inetSocketAddress);
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      printUsage();
      System.exit(1);
    }
    server.serve();
  }
View Full Code Here

    }

    HBaseHandler handler = new HBaseHandler();
    Hbase.Processor processor = new Hbase.Processor(handler);

    TServer server;
    if (cmd.hasOption("nonblocking") || cmd.hasOption("hsha")) {
      if (cmd.hasOption("bind")) {
        LOG.error("The Nonblocking and HsHa servers don't support IP address binding at the moment." +
                " See https://issues.apache.org/jira/browse/HBASE-2155 for details.");
        printUsageAndExit(options, -1);
      }

      TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(listenPort);
      TFramedTransport.Factory transportFactory = new TFramedTransport.Factory();

      if (cmd.hasOption("nonblocking")) {
        LOG.info("starting HBase Nonblocking Thrift server on " + Integer.toString(listenPort));
        server = new TNonblockingServer(processor, serverTransport, transportFactory, protocolFactory);
      } else {
        LOG.info("starting HBase HsHA Thrift server on " + Integer.toString(listenPort));
        server = new THsHaServer(processor, serverTransport, transportFactory, protocolFactory);
      }
    } else {
      // Get IP address to bind to
      InetAddress listenAddress = null;
      if (cmd.hasOption("bind")) {
        try {
          listenAddress = InetAddress.getByName(cmd.getOptionValue("bind"));
        } catch (UnknownHostException e) {
          LOG.error("Could not bind to provided ip address", e);
          printUsageAndExit(options, -1);
        }
      } else {
        listenAddress = InetAddress.getLocalHost();
      }
      TServerTransport serverTransport = new TServerSocket(new InetSocketAddress(listenAddress, listenPort));

      // Construct correct TransportFactory
      TTransportFactory transportFactory;
      if (cmd.hasOption("framed")) {
        transportFactory = new TFramedTransport.Factory();
        LOG.debug("Using framed transport");
      } else {
        transportFactory = new TTransportFactory();
      }

      LOG.info("starting HBase ThreadPool Thrift server on " + listenAddress + ":" + Integer.toString(listenPort));
      server = new TThreadPoolServer(processor, serverTransport, transportFactory, protocolFactory);
    }

    server.serve();
  }
View Full Code Here

          .transportFactory(transFactory)
          .protocolFactory(new TBinaryProtocol.Factory())
          .minWorkerThreads(minWorkerThreads)
          .maxWorkerThreads(maxWorkerThreads);

      final TServer tServer = new TThreadPoolServer(args);
      executorService.submit(new Runnable() {
        @Override
        public void run() {
          tServer.serve();
        }
      });
      return tServer;
    } catch (Throwable x) {
      throw new IOException(x);
View Full Code Here

          .protocolFactory(new TBinaryProtocol.Factory())
          .inputProtocolFactory(new TBinaryProtocol.Factory(true, true, maxMessageSize))
          .minWorkerThreads(minWorkerThreads)
          .maxWorkerThreads(maxWorkerThreads);

      TServer tServer = new TThreadPoolServer(args);
      HMSHandler.LOG.info("Started the new metaserver on port [" + port
          + "]...");
      HMSHandler.LOG.info("Options.minWorkerThreads = "
          + minWorkerThreads);
      HMSHandler.LOG.info("Options.maxWorkerThreads = "
          + maxWorkerThreads);
      HMSHandler.LOG.info("TCP keepalive = " + tcpKeepAlive);

      if (startLock != null) {
        signalOtherThreadsToStart(tServer, startLock, startCondition, startedServing);
      }
      tServer.serve();
    } catch (Throwable x) {
      x.printStackTrace();
      HMSHandler.LOG.error(StringUtils.stringifyException(x));
      throw x;
    }
View Full Code Here

public class SimpleThriftServer {
    public static void StartsimpleServer(InterpreterService.Processor<InterpreterServiceHandler> processor) {
        try {
            TServerTransport serverTransport = new TServerSocket(9090);
            TServer server = new TSimpleServer(
                    new TServer.Args(serverTransport).processor(processor));

            System.out.println("Starting the simple server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
View Full Code Here

        TServerSocket serverSocket = new TServerSocket(0);
        com.facebook.swift.service.scribe.scribe.Iface handler = new PlainScribeHandler();
        TProcessor processor = new com.facebook.swift.service.scribe.scribe.Processor<>(handler);

        TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverSocket).processor(processor);
        final TServer thriftServer = info.server = new TThreadPoolServer(args);

        new Thread() {
            @Override
            public void run()
            {
                thriftServer.serve();
            }
        }.start();

        while (!info.server.isServing()) {
            Thread.sleep(10);
View Full Code Here

    }
   
    Class<? extends TProtocolFactory> protoFactoryClass = Class.forName(opts.prop.getProperty("protocolFactory", TCompactProtocol.Factory.class.getName()))
        .asSubclass(TProtocolFactory.class);
    int port = Integer.parseInt(opts.prop.getProperty("port"));
    TServer server = createProxyServer(AccumuloProxy.class, ProxyServer.class, port, protoFactoryClass, opts.prop);
    server.serve();
  }
View Full Code Here

TOP

Related Classes of org.apache.thrift.server.TServer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.