Example usage for org.apache.thrift.transport TServerSocket TServerSocket

List of usage examples for org.apache.thrift.transport TServerSocket TServerSocket

Introduction

In this page you can find the example usage for org.apache.thrift.transport TServerSocket TServerSocket.

Prototype

public TServerSocket(InetSocketAddress bindAddr, int clientTimeout) throws TTransportException 

Source Link

Usage

From source file:alluxio.master.AlluxioMasterProcess.java

License:Apache License

/**
 * Creates a {@link AlluxioMasterProcess} by the classes in the same packet of
 * {@link AlluxioMasterProcess} or the subclasses of {@link AlluxioMasterProcess}.
 *//*w ww  . j a v  a  2  s .c o m*/
AlluxioMasterProcess() {
    mMinWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MIN);
    mMaxWorkerThreads = Configuration.getInt(PropertyKey.MASTER_WORKER_THREADS_MAX);
    int connectionTimeout = Configuration.getInt(PropertyKey.MASTER_CONNECTION_TIMEOUT_MS);

    Preconditions.checkArgument(mMaxWorkerThreads >= mMinWorkerThreads, PropertyKey.MASTER_WORKER_THREADS_MAX
            + " can not be less than " + PropertyKey.MASTER_WORKER_THREADS_MIN);

    if (connectionTimeout > 0) {
        LOG.debug("{} connection timeout[{}] is {}", this, PropertyKey.MASTER_CONNECTION_TIMEOUT_MS,
                connectionTimeout);
    }
    try {
        // Extract the port from the generated socket.
        // When running tests, it is fine to use port '0' so the system will figure out what port to
        // use (any random free port).
        // In a production or any real deployment setup, port '0' should not be used as it will make
        // deployment more complicated.
        if (!Configuration.getBoolean(PropertyKey.TEST_MODE)) {
            Preconditions.checkState(Configuration.getInt(PropertyKey.MASTER_RPC_PORT) > 0,
                    this + " rpc port is only allowed to be zero in test mode.");
            Preconditions.checkState(Configuration.getInt(PropertyKey.MASTER_WEB_PORT) > 0,
                    this + " web port is only allowed to be zero in test mode.");
        }

        mTransportProvider = TransportProvider.Factory.create();
        mTServerSocket = new TServerSocket(NetworkAddressUtils.getBindAddress(ServiceType.MASTER_RPC),
                Configuration.getInt(PropertyKey.MASTER_CONNECTION_TIMEOUT_MS));
        mPort = NetworkAddressUtils.getThriftPort(mTServerSocket);
        // reset master rpc port
        Configuration.set(PropertyKey.MASTER_RPC_PORT, Integer.toString(mPort));
        mRpcAddress = NetworkAddressUtils.getConnectAddress(ServiceType.MASTER_RPC);

        // Check that journals of each service have been formatted.
        MasterUtils.checkJournalFormatted();
        // Create masters.
        mRegistry = new MasterRegistry();
        MasterUtils.createMasters(new Journal.Factory(MasterUtils.getJournalLocation()), mRegistry);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:alluxio.master.AlluxioMasterProcess.java

License:Apache License

/**
 * Starts the Thrift RPC server. The AlluxioMaster registers the Services of registered
 * {@link Master}s and meta services to a multiplexed processor, then creates the master thrift
 * service with the multiplexed processor.
 *//*ww  w  . ja  v  a 2  s .  c  o  m*/
protected void startServingRPCServer() {
    // set up multiplexed thrift processors
    TMultiplexedProcessor processor = new TMultiplexedProcessor();
    // register master services
    for (Master master : mRegistry.getServers()) {
        registerServices(processor, master.getServices());
    }
    // register meta services
    processor.registerProcessor(Constants.META_MASTER_SERVICE_NAME,
            new MetaMasterClientService.Processor<>(new MetaMasterClientServiceHandler(this)));

    // Return a TTransportFactory based on the authentication type
    TTransportFactory transportFactory;
    try {
        transportFactory = mTransportProvider.getServerTransportFactory();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    try {
        if (mTServerSocket != null) {
            mTServerSocket.close();
        }
        mTServerSocket = new TServerSocket(mRpcAddress,
                Configuration.getInt(PropertyKey.MASTER_CONNECTION_TIMEOUT_MS));
    } catch (TTransportException e) {
        throw new RuntimeException(e);
    }
    // create master thrift service with the multiplexed processor.
    Args args = new TThreadPoolServer.Args(mTServerSocket).maxWorkerThreads(mMaxWorkerThreads)
            .minWorkerThreads(mMinWorkerThreads).processor(processor).transportFactory(transportFactory)
            .protocolFactory(new TBinaryProtocol.Factory(true, true));
    if (Configuration.getBoolean(PropertyKey.TEST_MODE)) {
        args.stopTimeoutVal = 0;
    } else {
        args.stopTimeoutVal = Constants.THRIFT_STOP_TIMEOUT_SECONDS;
    }
    mThriftServer = new TThreadPoolServer(args);

    // start thrift rpc server
    mIsServing = true;
    mStartTimeMs = System.currentTimeMillis();
    mThriftServer.serve();
}

From source file:com.cloudera.llama.server.ThriftEndPoint.java

License:Apache License

public static TServerSocket createTServerSocket(ServerConfiguration conf) throws Exception {
    String strAddress = conf.getThriftAddress();
    int timeout = conf.getTransportTimeOut();
    int defaultPort = conf.getThriftDefaultPort();
    InetSocketAddress address = NetUtils.createSocketAddr(strAddress, defaultPort);
    return new TServerSocket(address, timeout);
}

From source file:com.cloudera.llama.server.ThriftEndPoint.java

License:Apache License

public static TServerSocket createAdminTServerSocket(ServerConfiguration conf) throws Exception {
    String strAddress = conf.getAdminThriftAddress();
    int timeout = 2000;
    int defaultPort = conf.getAdminThriftDefaultPort();
    InetSocketAddress address = NetUtils.createSocketAddr(strAddress, defaultPort);
    return new TServerSocket(address, timeout);
}

From source file:com.cottsoft.thrift.framework.server.ThriftMultiBinaryServerFactory.java

License:Apache License

/**
 * ??Socket//  ww  w  .j  a v  a 2s.c  o  m
 * 
 * @return
 * @throws ThriftException
 */
public TServerTransport getServerTransport() throws ThriftException {
    try {
        if (timeout > 0) {
            return new TServerSocket(port, timeout);
        } else {
            return new TServerSocket(port);
        }
    } catch (TTransportException e) {
        e.printStackTrace();
        throw new ThriftException(e);
    }
}

From source file:com.netflix.metacat.thrift.AbstractThriftServer.java

License:Apache License

/**
 * Server initialization./*from ww w. ja  va 2  s  .c o  m*/
 *
 * @throws Exception error
 */
public void start() throws Exception {
    log.info("initializing thrift server {}", getServerName());
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadPoolNameFormat)
            .setUncaughtExceptionHandler(
                    (t, e) -> log.error("Uncaught exception in thread: {}", t.getName(), e))
            .build();
    final ExecutorService executorService = new ThreadPoolExecutor(
            Math.min(2, config.getThriftServerMaxWorkerThreads()), config.getThriftServerMaxWorkerThreads(),
            60L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory);
    RegistryUtil.registerThreadPool(registry, threadPoolNameFormat, (ThreadPoolExecutor) executorService);
    final int timeout = config.getThriftServerSocketClientTimeoutInSeconds() * 1000;
    final TServerTransport serverTransport = new TServerSocket(portNumber, timeout);
    startServing(executorService, serverTransport);
}

From source file:org.apache.accumulo.server.rpc.TServerUtils.java

License:Apache License

public static ServerAddress createSaslThreadPoolServer(HostAndPort address, TProcessor processor,
        TProtocolFactory protocolFactory, long socketTimeout, SaslServerConnectionParams params,
        final String serverName, String threadName, final int numThreads, final int numSTThreads,
        long timeBetweenThreadChecks) throws TTransportException {
    // We'd really prefer to use THsHaServer (or similar) to avoid 1 RPC == 1 Thread that the TThreadPoolServer does,
    // but sadly this isn't the case. Because TSaslTransport needs to issue a handshake when it open()'s which will fail
    // when the server does an accept() to (presumably) wake up the eventing system.
    log.info("Creating SASL thread pool thrift server on listening on {}:{}", address.getHostText(),
            address.getPort());//from  w w w. jav a2s.c o  m
    TServerSocket transport = new TServerSocket(address.getPort(), (int) socketTimeout);

    String hostname, fqdn;
    try {
        hostname = InetAddress.getByName(address.getHostText()).getCanonicalHostName();
        fqdn = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
        throw new TTransportException(e);
    }

    // If we can't get a real hostname from the provided host test, use the hostname from DNS for localhost
    if ("0.0.0.0".equals(hostname)) {
        hostname = fqdn;
    }

    // ACCUMULO-3497 an easy sanity check we can perform for the user when SASL is enabled. Clients and servers have to agree upon the FQDN
    // so that the SASL handshake can occur. If the provided hostname doesn't match the FQDN for this host, fail quickly and inform them to update
    // their configuration.
    if (!hostname.equals(fqdn)) {
        log.error(
                "Expected hostname of '{}' but got '{}'. Ensure the entries in the Accumulo hosts files (e.g. masters, slaves) are the FQDN for each host when using SASL.",
                fqdn, hostname);
        throw new RuntimeException(
                "SASL requires that the address the thrift server listens on is the same as the FQDN for this host");
    }

    final UserGroupInformation serverUser;
    try {
        serverUser = UserGroupInformation.getLoginUser();
    } catch (IOException e) {
        throw new TTransportException(e);
    }

    log.debug("Logged in as {}, creating TSaslServerTransport factory with {}/{}", serverUser,
            params.getKerberosServerPrimary(), hostname);

    // Make the SASL transport factory with the instance and primary from the kerberos server principal, SASL properties
    // and the SASL callback handler from Hadoop to ensure authorization ID is the authentication ID. Despite the 'protocol' argument seeming to be useless, it
    // *must* be the primary of the server.
    TSaslServerTransport.Factory saslTransportFactory = new TSaslServerTransport.Factory();
    saslTransportFactory.addServerDefinition(ThriftUtil.GSSAPI, params.getKerberosServerPrimary(), hostname,
            params.getSaslProperties(), new SaslRpcServer.SaslGssCallbackHandler());

    if (null != params.getSecretManager()) {
        log.info("Adding DIGEST-MD5 server definition for delegation tokens");
        saslTransportFactory.addServerDefinition(ThriftUtil.DIGEST_MD5, params.getKerberosServerPrimary(),
                hostname, params.getSaslProperties(),
                new SaslServerDigestCallbackHandler(params.getSecretManager()));
    } else {
        log.info("SecretManager is null, not adding support for delegation token authentication");
    }

    // Make sure the TTransportFactory is performing a UGI.doAs
    TTransportFactory ugiTransportFactory = new UGIAssumingTransportFactory(saslTransportFactory, serverUser);

    if (address.getPort() == 0) {
        // If we chose a port dynamically, make a new use it (along with the proper hostname)
        address = HostAndPort.fromParts(address.getHostText(), transport.getServerSocket().getLocalPort());
        log.info("SASL thrift server bound on {}", address);
    }

    ThreadPoolExecutor pool = createSelfResizingThreadPool(serverName, numThreads, numSTThreads,
            timeBetweenThreadChecks);

    final TThreadPoolServer server = createTThreadPoolServer(transport, processor, ugiTransportFactory,
            protocolFactory, pool);

    return new ServerAddress(server, address);
}

From source file:org.apache.hadoop.corona.SessionDriver.java

License:Apache License

/**
 * Start the SessionDriver callback server
 * @param conf the corona configuration for this session
 * @return the server socket of the callback server
 * @throws IOException/*from  w  ww  . ja v a 2  s  .c  o m*/
 */
private ServerSocket initializeServer(CoronaConf conf) throws IOException {
    // Choose any free port.
    ServerSocket sessionServerSocket = new ServerSocket(0, 0, getLocalAddress());
    TServerSocket tServerSocket = new TServerSocket(sessionServerSocket, conf.getCMSoTimeout());

    TFactoryBasedThreadPoolServer.Args args = new TFactoryBasedThreadPoolServer.Args(tServerSocket);
    args.processor(new SessionDriverServiceProcessor(incoming));
    args.transportFactory(new TTransportFactory());
    args.protocolFactory(new TBinaryProtocol.Factory(true, true));
    args.stopTimeoutVal = 0;
    server = new TFactoryBasedThreadPoolServer(args, new TFactoryBasedThreadPoolServer.DaemonThreadFactory());
    return sessionServerSocket;
}

From source file:org.apache.hadoop.hdfs.fsshellservice.FsShellServiceImpl.java

License:Apache License

private void initThriftServer(int port, int maxQueue) {
    // Setup the Thrift server
    LOG.info("Setting up Thrift server listening port " + port);
    TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
    TTransportFactory transportFactory = new TFramedTransport.Factory();
    TServerTransport serverTransport;//from  w w  w.  j a v  a  2 s  .c  o m
    FsShellService.Processor<FsShellService.Iface> processor = new FsShellService.Processor<FsShellService.Iface>(
            this);

    ServerSocket serverSocket_;
    try {
        // Make server socket. Use loop-back address to only serve requests
        // from local clients, in order to prevent ambiguity for commands
        // of copyFromLocal() and copyToLocal()
        serverSocket_ = new ServerSocket(port, maxQueue, InetAddress.getAllByName(null)[0]);
        // Prevent 2MSL delay problem on server restarts
        serverSocket_.setReuseAddress(true);
    } catch (IOException ioe) {
        LOG.error("Could not create ServerSocket on local address ", ioe);
        return;
    }
    serverTransport = new TServerSocket(serverSocket_, clientTimeout);
    TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
    serverArgs.processor(processor).transportFactory(transportFactory).protocolFactory(protocolFactory);
    tserver = new TThreadPoolServer(serverArgs);
}

From source file:org.apache.hadoop.thriftfs.ThriftPluginServer.java

License:Apache License

/**
 * Start processing requests./*from ww  w  . ja v  a2  s  . c o  m*/
 *
 * @throws IllegalStateException if the server has already been started.
 * @throws IOException on network errors.
 */
public void start() throws IOException {
    String hostname = address.getAddress().getHostAddress();

    synchronized (this) {
        if (server != null) {
            throw new IllegalStateException("Thrift server already started");
        }
        LOG.info("Starting Thrift server");
        ServerSocket sock = new ServerSocket();
        sock.setReuseAddress(true);
        if (port == 0) {
            sock.bind(null);
            address = new InetSocketAddress(hostname, sock.getLocalPort());
            port = address.getPort();
        } else {
            sock.bind(address);
        }

        int socketTimeout = conf.getInt(ThriftFsConfig.DFS_THRIFT_SOCKET_TIMEOUT_KEY, SOCKET_READ_TIMEOUT);

        TServerTransport transport = new TServerSocket(sock, socketTimeout);
        SanerThreadPoolServer.Options options = new SanerThreadPoolServer.Options();
        options.minWorkerThreads = conf.getInt(ThriftFsConfig.DFS_THRIFT_THREADS_MIN_KEY, 10);
        options.maxWorkerThreads = conf.getInt(ThriftFsConfig.DFS_THRIFT_THREADS_MAX_KEY, 20);
        options.stopTimeoutVal = conf.getInt(ThriftFsConfig.DFS_THRIFT_TIMEOUT_KEY, 60);
        options.stopTimeoutUnit = TimeUnit.SECONDS;
        options.queueSize = conf.getInt(ThriftFsConfig.DFS_THRIFT_QUEUE_SIZE_KEY, 4 * options.maxWorkerThreads);

        server = new SanerThreadPoolServer(processorFactory, transport, transportFactory, transportFactory,
                new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), options);
    }

    Thread t = new Thread(this);
    t.start();
    LOG.info("Thrift server listening on " + hostname + ":" + port);
}