Example usage for org.apache.thrift.transport TNonblockingServerSocket getPort

List of usage examples for org.apache.thrift.transport TNonblockingServerSocket getPort

Introduction

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

Prototype

public int getPort() 

Source Link

Usage

From source file:com.bendb.thrifty.testing.TestServer.java

License:Apache License

public int port() {
    if (serverTransport instanceof TServerSocket) {
        return ((TServerSocket) serverTransport).getServerSocket().getLocalPort();
    } else if (serverTransport instanceof TNonblockingServerSocket) {
        TNonblockingServerSocket sock = (TNonblockingServerSocket) serverTransport;
        return sock.getPort();
    } else {/*from w  ww  .  j  a v a 2 s  .com*/
        throw new AssertionError("Unexpected server transport type: " + serverTransport.getClass());
    }
}

From source file:com.facebook.buck.distributed.build_slave.ThriftCoordinatorServer.java

License:Apache License

public ThriftCoordinatorServer start() throws IOException {
    LOG.info("Starting ThriftCoordinatorServer.");
    synchronized (lock) {
        TNonblockingServerSocket transport;
        try {/*from  w  w  w . j  a va2  s .c  o m*/
            transport = new TNonblockingServerSocket(this.port.orElse(0));
            // If we initially specified port zero, we would now have the correct value.
            this.port = OptionalInt.of(transport.getPort());
        } catch (TTransportException e) {
            throw new ThriftException(e);
        }

        TThreadedSelectorServer.Args serverArgs = new TThreadedSelectorServer.Args(transport);
        serverArgs.processor(processor);
        server = new TThreadedSelectorServer(serverArgs);
        serverThread = new Thread(() -> Preconditions.checkNotNull(server).serve());
        serverThread.start();
    }

    // Note: this call will initialize MinionCountProvider (same Object as eventListener)
    eventListener.onThriftServerStarted(InetAddress.getLocalHost().getHostName(), port.getAsInt());
    return this;
}

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

License:Apache License

/**
 * Create a NonBlockingServer with a custom thread pool that can dynamically resize itself.
 *///from   w  w  w  .j  a  va 2 s. c o  m
public static ServerAddress createNonBlockingServer(HostAndPort address, TProcessor processor,
        TProtocolFactory protocolFactory, final String serverName, String threadName, final int numThreads,
        final int numSTThreads, long timeBetweenThreadChecks, long maxMessageSize) throws TTransportException {

    final TNonblockingServerSocket transport = new TNonblockingServerSocket(
            new InetSocketAddress(address.getHostText(), address.getPort()));
    final CustomNonBlockingServer.Args options = new CustomNonBlockingServer.Args(transport);

    options.protocolFactory(protocolFactory);
    options.transportFactory(ThriftUtil.transportFactory(maxMessageSize));
    options.maxReadBufferBytes = maxMessageSize;
    options.stopTimeoutVal(5);

    // Create our own very special thread pool.
    ThreadPoolExecutor pool = createSelfResizingThreadPool(serverName, numThreads, numSTThreads,
            timeBetweenThreadChecks);

    options.executorService(pool);
    options.processorFactory(new TProcessorFactory(processor));

    if (address.getPort() == 0) {
        address = HostAndPort.fromParts(address.getHostText(), transport.getPort());
    }

    return new ServerAddress(new CustomNonBlockingServer(options), address);
}

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

License:Apache License

public static ServerAddress createNonBlockingServer(HostAndPort address, TProcessor processor,
        final String serverName, String threadName, final int numThreads, final int numSTThreads,
        long timeBetweenThreadChecks, long maxMessageSize) throws TTransportException {
    TNonblockingServerSocket transport = new TNonblockingServerSocket(
            new InetSocketAddress(address.getHostText(), address.getPort()));
    CustomNonBlockingServer.Args options = new CustomNonBlockingServer.Args(transport);
    options.protocolFactory(ThriftUtil.protocolFactory());
    options.transportFactory(ThriftUtil.transportFactory(maxMessageSize));
    options.maxReadBufferBytes = maxMessageSize;
    options.stopTimeoutVal(5);/*ww w .ja va 2 s.  c  o m*/
    /*
     * Create our own very special thread pool.
     */
    final ThreadPoolExecutor pool = new SimpleThreadPool(numThreads, "ClientPool");
    // periodically adjust the number of threads we need by checking how busy our threads are
    SimpleTimer.getInstance(numSTThreads).schedule(new Runnable() {
        @Override
        public void run() {
            if (pool.getCorePoolSize() <= pool.getActiveCount()) {
                int larger = pool.getCorePoolSize() + Math.min(pool.getQueue().size(), 2);
                log.info("Increasing server thread pool size on " + serverName + " to " + larger);
                pool.setMaximumPoolSize(larger);
                pool.setCorePoolSize(larger);
            } else {
                if (pool.getCorePoolSize() > pool.getActiveCount() + 3) {
                    int smaller = Math.max(numThreads, pool.getCorePoolSize() - 1);
                    if (smaller != pool.getCorePoolSize()) {
                        // there is a race condition here... the active count could be higher by the time
                        // we decrease the core pool size... so the active count could end up higher than
                        // the core pool size, in which case everything will be queued... the increase case
                        // should handle this and prevent deadlock
                        log.info("Decreasing server thread pool size on " + serverName + " to " + smaller);
                        pool.setCorePoolSize(smaller);
                    }
                }
            }
        }
    }, timeBetweenThreadChecks, timeBetweenThreadChecks);
    options.executorService(pool);
    options.processorFactory(new TProcessorFactory(processor));
    if (address.getPort() == 0) {
        address = HostAndPort.fromParts(address.getHostText(), transport.getPort());
    }
    return new ServerAddress(new CustomNonBlockingServer(options), address);
}