Example usage for org.apache.thrift.server TThreadPoolServer TThreadPoolServer

List of usage examples for org.apache.thrift.server TThreadPoolServer TThreadPoolServer

Introduction

In this page you can find the example usage for org.apache.thrift.server TThreadPoolServer TThreadPoolServer.

Prototype

public TThreadPoolServer(Args args) 

Source Link

Usage

From source file:org.apache.hadoop.hive.service.HiveServer.java

License:Apache License

public static void main(String[] args) {
    try {/*w w  w .  ja  v  a2  s  .  c o m*/

        if (args.length >= 1) {
            port = Integer.parseInt(args[0]);
        }

        //set dns cache to 3s;the negtive cache default value is 10s in java
        java.security.Security.setProperty("networkaddress.cache.ttl", "3");

        SessionState.initHiveLog4j();
        sessionManager = new HSSessionManager();
        Thread t = new Thread(sessionManager);
        t.setDaemon(true);
        t.start();

        dioPool = new DataInOutPool(50);
        dioPool.init();
        Thread dioPoolThread = new Thread(dioPool);
        dioPoolThread.setDaemon(true);
        dioPoolThread.start();

        TServerTransport serverTransport = new TServerSocket(port);
        ThriftHiveProcessorFactory hfactory = new ThriftHiveProcessorFactory(null);

        Args serverArgs = new Args(serverTransport);
        serverArgs.processorFactory(hfactory);
        serverArgs.inputTransportFactory(new TTransportFactory());
        serverArgs.outputTransportFactory(new TTransportFactory());
        serverArgs.inputProtocolFactory(new TBinaryProtocol.Factory());
        serverArgs.outputProtocolFactory(new TBinaryProtocol.Factory());

        TServer server = new TThreadPoolServer(serverArgs);
        HiveServerHandler.LOG.info("Starting hive server on port " + port);

        server.serve();
    } catch (Exception x) {
        x.printStackTrace();
    }
}

From source file:org.apache.hive.service.cli.thrift.ThriftBinaryCLIService.java

License:Apache License

@Override
public void run() {
    try {/*from   www .  j a  va  2  s  . c o  m*/
        // Server thread pool
        String threadPoolName = "HiveServer2-Handler-Pool";
        ExecutorService executorService = new ThreadPoolExecutor(minWorkerThreads, maxWorkerThreads,
                workerKeepAliveTime, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
                new ThreadFactoryWithGarbageCleanup(threadPoolName));

        // Thrift configs
        hiveAuthFactory = new HiveAuthFactory(hiveConf);
        TTransportFactory transportFactory = hiveAuthFactory.getAuthTransFactory();
        TProcessorFactory processorFactory = hiveAuthFactory.getAuthProcFactory(this);
        TServerSocket serverSocket = null;
        List<String> sslVersionBlacklist = new ArrayList<String>();
        for (String sslVersion : hiveConf.getVar(ConfVars.HIVE_SSL_PROTOCOL_BLACKLIST).split(",")) {
            sslVersionBlacklist.add(sslVersion);
        }
        if (!hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_USE_SSL)) {
            serverSocket = HiveAuthFactory.getServerSocket(hiveHost, portNum);
        } else {
            String keyStorePath = hiveConf.getVar(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH).trim();
            if (keyStorePath.isEmpty()) {
                throw new IllegalArgumentException(
                        ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname + " Not configured for SSL connection");
            }
            String keyStorePassword = ShimLoader.getHadoopShims().getPassword(hiveConf,
                    HiveConf.ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PASSWORD.varname);
            serverSocket = HiveAuthFactory.getServerSSLSocket(hiveHost, portNum, keyStorePath, keyStorePassword,
                    sslVersionBlacklist);
        }

        // Server args
        int maxMessageSize = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE);
        int requestTimeout = (int) hiveConf.getTimeVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_LOGIN_TIMEOUT,
                TimeUnit.SECONDS);
        int beBackoffSlotLength = (int) hiveConf.getTimeVar(
                HiveConf.ConfVars.HIVE_SERVER2_THRIFT_LOGIN_BEBACKOFF_SLOT_LENGTH, TimeUnit.MILLISECONDS);
        TThreadPoolServer.Args sargs = new TThreadPoolServer.Args(serverSocket)
                .processorFactory(processorFactory).transportFactory(transportFactory)
                .protocolFactory(new TBinaryProtocol.Factory())
                .inputProtocolFactory(new TBinaryProtocol.Factory(true, true, maxMessageSize, maxMessageSize))
                .requestTimeout(requestTimeout).requestTimeoutUnit(TimeUnit.SECONDS)
                .beBackoffSlotLength(beBackoffSlotLength).beBackoffSlotLengthUnit(TimeUnit.MILLISECONDS)
                .executorService(executorService);

        // TCP Server
        server = new TThreadPoolServer(sargs);
        server.setServerEventHandler(serverEventHandler);
        String msg = "Starting " + ThriftBinaryCLIService.class.getSimpleName() + " on port " + portNum
                + " with " + minWorkerThreads + "..." + maxWorkerThreads + " worker threads";
        LOG.info(msg);
        server.serve();
    } catch (Throwable t) {
        LOG.fatal("Error starting HiveServer2: could not start " + ThriftBinaryCLIService.class.getSimpleName(),
                t);
        System.exit(-1);
    }
}

From source file:org.apache.people.mreutegg.jsinfonia.net.ThriftServer.java

License:Apache License

private void createServer() throws TTransportException {
    if (port >= 0) {
        while (transport == null) {
            usedPort = (int) (Math.random() * 32000d) + 32000;
            try {
                if (nonBlocking) {
                    transport = new TNonblockingServerSocket(usedPort);
                } else {
                    transport = new TServerSocket(usedPort);
                }/*from   ww w. j  a v  a 2s. co m*/
            } catch (TTransportException e) {
                // try another one
            }
        }
    } else {
        if (nonBlocking) {
            transport = new TNonblockingServerSocket(port);
        } else {
            transport = new TServerSocket(usedPort);
        }
        usedPort = port;
    }
    TProcessor processor = createProcessor();
    if (nonBlocking) {
        server = new TThreadedSelectorServer(
                new TThreadedSelectorServer.Args((TNonblockingServerSocket) transport)
                        .transportFactory(new TFramedTransport.Factory()).processor(processor));
    } else {
        server = new TThreadPoolServer(new TThreadPoolServer.Args(transport).processor(processor));
    }
}

From source file:org.apache.sentry.service.thrift.SentryService.java

License:Apache License

private void runServer() throws Exception {
    Iterable<String> processorFactories = ConfUtilties.CLASS_SPLITTER
            .split(conf.get(ServerConfig.PROCESSOR_FACTORIES, ServerConfig.PROCESSOR_FACTORIES_DEFAULT).trim());
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    boolean registeredProcessor = false;
    for (String processorFactory : processorFactories) {
        Class<?> clazz = conf.getClassByName(processorFactory);
        if (!ProcessorFactory.class.isAssignableFrom(clazz)) {
            throw new IllegalArgumentException(
                    "Processor Factory " + processorFactory + " is not a " + ProcessorFactory.class.getName());
        }//from w  w w .j  a v  a 2s.c o  m
        try {
            Constructor<?> constructor = clazz.getConstructor(Configuration.class);
            ProcessorFactory factory = (ProcessorFactory) constructor.newInstance(conf);
            registeredProcessor = registeredProcessor || factory.register(processor);
        } catch (Exception e) {
            throw new IllegalStateException("Could not create " + processorFactory, e);
        }
    }
    if (!registeredProcessor) {
        throw new IllegalStateException("Failed to register any processors from " + processorFactories);
    }
    TServerTransport serverTransport = new TServerSocket(address);
    TTransportFactory transportFactory = null;
    if (kerberos) {
        TSaslServerTransport.Factory saslTransportFactory = new TSaslServerTransport.Factory();
        saslTransportFactory.addServerDefinition(AuthMethod.KERBEROS.getMechanismName(), principalParts[0],
                principalParts[1], ServerConfig.SASL_PROPERTIES, new GSSCallback(conf));
        transportFactory = saslTransportFactory;
    } else {
        transportFactory = new TTransportFactory();
    }
    TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport).processor(processor)
            .transportFactory(transportFactory).protocolFactory(new TBinaryProtocol.Factory())
            .minWorkerThreads(minThreads).maxWorkerThreads(maxThreads);
    thriftServer = new TThreadPoolServer(args);
    LOGGER.info("Serving on " + address);
    thriftServer.serve();
}

From source file:org.apache.storm.security.auth.sasl.SaslTransportPlugin.java

License:Apache License

@Override
public TServer getServer(TProcessor processor) throws IOException, TTransportException {
    int configuredPort = type.getPort(conf);
    Integer socketTimeout = type.getSocketTimeOut(conf);
    TTransportFactory serverTransportFactory = getServerTransportFactory(type.isImpersonationAllowed());
    TServerSocket serverTransport = null;
    if (socketTimeout != null) {
        serverTransport = new TServerSocket(configuredPort, socketTimeout);
    } else {// w w w.  j  av a  2  s .c  om
        serverTransport = new TServerSocket(configuredPort);
    }
    this.port = serverTransport.getServerSocket().getLocalPort();
    int numWorkerThreads = type.getNumThreads(conf);
    Integer queueSize = type.getQueueSize(conf);

    TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport)
            .processor(new TUGIWrapProcessor(processor)).minWorkerThreads(numWorkerThreads)
            .maxWorkerThreads(numWorkerThreads).protocolFactory(new TBinaryProtocol.Factory(false, true));

    if (serverTransportFactory != null) {
        serverArgs.transportFactory(serverTransportFactory);
    }
    BlockingQueue workQueue = new SynchronousQueue();
    if (queueSize != null) {
        workQueue = new ArrayBlockingQueue(queueSize);
    }
    ThreadPoolExecutor executorService = new ExtendedThreadPoolExecutor(numWorkerThreads, numWorkerThreads, 60,
            TimeUnit.SECONDS, workQueue);
    serverArgs.executorService(executorService);
    return new TThreadPoolServer(serverArgs);
}

From source file:org.apache.storm.security.auth.SaslTransportPlugin.java

License:Apache License

@Override
public TServer getServer(TProcessor processor) throws IOException, TTransportException {
    int port = type.getPort(storm_conf);
    Integer socketTimeout = type.getSocketTimeOut(storm_conf);
    TTransportFactory serverTransportFactory = getServerTransportFactory();
    TServerSocket serverTransport = null;
    if (socketTimeout != null) {
        serverTransport = new TServerSocket(port, socketTimeout);
    } else {//from w w  w.ja va2s . c  o  m
        serverTransport = new TServerSocket(port);
    }
    int numWorkerThreads = type.getNumThreads(storm_conf);
    Integer queueSize = type.getQueueSize(storm_conf);

    TThreadPoolServer.Args server_args = new TThreadPoolServer.Args(serverTransport)
            .processor(new TUGIWrapProcessor(processor)).minWorkerThreads(numWorkerThreads)
            .maxWorkerThreads(numWorkerThreads).protocolFactory(new TBinaryProtocol.Factory(false, true));

    if (serverTransportFactory != null) {
        server_args.transportFactory(serverTransportFactory);
    }
    BlockingQueue workQueue = new SynchronousQueue();
    if (queueSize != null) {
        workQueue = new ArrayBlockingQueue(queueSize);
    }
    ThreadPoolExecutor executorService = new ExtendedThreadPoolExecutor(numWorkerThreads, numWorkerThreads, 60,
            TimeUnit.SECONDS, workQueue);
    server_args.executorService(executorService);
    return new TThreadPoolServer(server_args);
}

From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterManagedProcess.java

License:Apache License

@Override
public void start(String userName, Boolean isUserImpersonate) {
    // start server process
    final String callbackHost;
    final int callbackPort;
    try {//from ww  w . j a  v a2s.co  m
        port = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces(portRange);
        logger.info("Choose port {} for RemoteInterpreterProcess", port);
        callbackHost = RemoteInterpreterUtils.findAvailableHostAddress();
        callbackPort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }

    logger.info("Thrift server for callback will start. Port: {}", callbackPort);
    try {
        callbackServer = new TThreadPoolServer(new TThreadPoolServer.Args(new TServerSocket(callbackPort))
                .processor(new RemoteInterpreterCallbackService.Processor<>(
                        new RemoteInterpreterCallbackService.Iface() {
                            @Override
                            public void callback(CallbackInfo callbackInfo) throws TException {
                                logger.info("Registered: {}", callbackInfo);
                                host = callbackInfo.getHost();
                                port = callbackInfo.getPort();
                                running.set(true);
                                synchronized (running) {
                                    running.notify();
                                }
                            }
                        })));
        // Start thrift server to receive callbackInfo from RemoteInterpreterServer;
        new Thread(new Runnable() {
            @Override
            public void run() {
                callbackServer.serve();
            }
        }).start();

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                if (callbackServer.isServing()) {
                    callbackServer.stop();
                }
            }
        }));

        while (!callbackServer.isServing()) {
            logger.debug("callbackServer is not serving");
            Thread.sleep(500);
        }
        logger.debug("callbackServer is serving now");
    } catch (TTransportException e) {
        logger.error("callback server error.", e);
    } catch (InterruptedException e) {
        logger.warn("", e);
    }

    CommandLine cmdLine = CommandLine.parse(interpreterRunner);
    cmdLine.addArgument("-d", false);
    cmdLine.addArgument(interpreterDir, false);
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(callbackHost, false);
    cmdLine.addArgument("-p", false);
    cmdLine.addArgument(Integer.toString(callbackPort), false);
    if (isUserImpersonate && !userName.equals("anonymous")) {
        cmdLine.addArgument("-u", false);
        cmdLine.addArgument(userName, false);
    }
    cmdLine.addArgument("-l", false);
    cmdLine.addArgument(localRepoDir, false);
    cmdLine.addArgument("-g", false);
    cmdLine.addArgument(interpreterGroupName, false);

    executor = new DefaultExecutor();

    ByteArrayOutputStream cmdOut = new ByteArrayOutputStream();
    ProcessLogOutputStream processOutput = new ProcessLogOutputStream(logger);
    processOutput.setOutputStream(cmdOut);

    executor.setStreamHandler(new PumpStreamHandler(processOutput));
    watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);

    try {
        Map procEnv = EnvironmentUtils.getProcEnvironment();
        procEnv.putAll(env);

        logger.info("Run interpreter process {}", cmdLine);
        executor.execute(cmdLine, procEnv, this);
    } catch (IOException e) {
        running.set(false);
        throw new RuntimeException(e);
    }

    try {
        synchronized (running) {
            if (!running.get()) {
                running.wait(getConnectTimeout() * 2);
            }
        }
        if (!running.get()) {
            callbackServer.stop();
            throw new RuntimeException(new String(cmdOut.toByteArray()));
        }
    } catch (InterruptedException e) {
        logger.error("Remote interpreter is not accessible");
    }
    processOutput.setOutputStream(null);
}

From source file:org.apache.zeppelin.interpreter.remote.RemoteInterpreterServer.java

License:Apache License

public RemoteInterpreterServer(String intpEventServerHost, int intpEventServerPort, String portRange,
        String interpreterGroupId, boolean isTest) throws TTransportException, IOException {
    logger.info("Starting remote interpreter server on port {}, intpEventServerAddress: {}:{}", port,
            intpEventServerHost, intpEventServerPort);
    if (null != intpEventServerHost) {
        this.intpEventServerHost = intpEventServerHost;
        if (!isTest) {
            TTransport transport = new TSocket(intpEventServerHost, intpEventServerPort);
            transport.open();/*ww w.  j a  v a2s . co  m*/
            TProtocol protocol = new TBinaryProtocol(transport);
            intpEventServiceClient = new RemoteInterpreterEventService.Client(protocol);
            intpEventClient = new RemoteInterpreterEventClient(intpEventServiceClient);
        }
    } else {
        // DevInterpreter
        this.port = intpEventServerPort;
    }
    this.isTest = isTest;
    this.interpreterGroupId = interpreterGroupId;
    RemoteInterpreterService.Processor<RemoteInterpreterServer> processor = new RemoteInterpreterService.Processor<>(
            this);
    TServerSocket serverTransport;
    if (null == intpEventServerHost) {
        // Dev Interpreter
        serverTransport = new TServerSocket(intpEventServerPort);
    } else {
        serverTransport = RemoteInterpreterUtils.createTServerSocket(portRange);
        this.port = serverTransport.getServerSocket().getLocalPort();
        this.host = RemoteInterpreterUtils.findAvailableHostAddress();
        logger.info("Launching ThriftServer at " + this.host + ":" + this.port);
    }
    server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
    remoteWorksResponsePool = Collections.synchronizedMap(new HashMap<String, Object>());

    clusterManagerClient.start(interpreterGroupId);
}

From source file:org.apache.zeppelin.interpreter.RemoteInterpreterEventServer.java

License:Apache License

public void start() throws IOException {
    Thread startingThread = new Thread() {
        @Override/*from  w ww.j  a  v a  2 s  .c o  m*/
        public void run() {
            TServerSocket tSocket = null;
            try {
                tSocket = RemoteInterpreterUtils.createTServerSocket(portRange);
                port = tSocket.getServerSocket().getLocalPort();
                host = RemoteInterpreterUtils.findAvailableHostAddress();
            } catch (IOException e1) {
                throw new RuntimeException(e1);
            }

            LOGGER.info("InterpreterEventServer is starting at {}:{}", host, port);
            RemoteInterpreterEventService.Processor processor = new RemoteInterpreterEventService.Processor(
                    RemoteInterpreterEventServer.this);
            thriftServer = new TThreadPoolServer(new TThreadPoolServer.Args(tSocket).processor(processor));
            thriftServer.serve();
        }
    };
    startingThread.start();
    long start = System.currentTimeMillis();
    while ((System.currentTimeMillis() - start) < 30 * 1000) {
        if (thriftServer != null && thriftServer.isServing()) {
            break;
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
    }

    if (thriftServer != null && !thriftServer.isServing()) {
        throw new IOException("Fail to start InterpreterEventServer in 30 seconds.");
    }
    LOGGER.info("RemoteInterpreterEventServer is started");

    runner = new AppendOutputRunner(listener);
    appendFuture = appendService.scheduleWithFixedDelay(runner, 0, AppendOutputRunner.BUFFER_TIME_MS,
            TimeUnit.MILLISECONDS);
}

From source file:org.cinchapi.concourse.server.ConcourseServer.java

License:Open Source License

/**
 * Construct a ConcourseServer that listens on {@code port} and store data
 * in {@code dbStore} and {@code bufferStore}.
 * // w  w  w.  jav  a 2 s . c  om
 * @param port
 * @param bufferStore
 * @param dbStore
 * @throws TTransportException
 */
public ConcourseServer(int port, String bufferStore, String dbStore) throws TTransportException {
    Preconditions.checkState(!bufferStore.equalsIgnoreCase(dbStore),
            "Cannot store buffer and database files in the same directory. " + "Please check concourse.prefs.");
    Preconditions.checkState(!Strings.isNullOrEmpty(Environments.sanitize(DEFAULT_ENVIRONMENT)),
            "Cannot initialize " + "Concourse Server with a default environment of "
                    + "'%s'. Please use a default environment name that "
                    + "contains only alphanumeric characters.",
            DEFAULT_ENVIRONMENT);
    FileSystem.mkdirs(bufferStore);
    FileSystem.mkdirs(dbStore);
    TServerSocket socket = new TServerSocket(port);
    ConcourseService.Processor<Iface> processor = new ConcourseService.Processor<Iface>(this);
    Args args = new TThreadPoolServer.Args(socket);
    args.processor(processor);
    args.maxWorkerThreads(NUM_WORKER_THREADS);
    args.executorService(Executors
            .newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("Client Worker" + " %d").build()));
    this.server = new TThreadPoolServer(args);
    this.bufferStore = bufferStore;
    this.dbStore = dbStore;
    this.engines = Maps.newConcurrentMap();
    this.manager = AccessManager.create(ACCESS_FILE);
    getEngine(); // load the default engine
}