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

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

Introduction

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

Prototype

public void close() 

Source Link

Usage

From source file:com.liveramp.hank.partition_server.PartitionServer.java

License:Apache License

protected void startThriftServer() throws TTransportException, IOException, InterruptedException {
    IfaceWithShutdown handler = null;//from  w  ww. j a  v  a2  s .  c o  m
    try {
        // Set up the service handler
        handler = getHandler();
        // Launch the thrift server
        TNonblockingServerSocket serverSocket = new TNonblockingServerSocket(configurator.getServicePort());
        TThreadedSelectorServer.Args options = new TThreadedSelectorServer.Args(serverSocket);
        options.processor(new com.liveramp.hank.generated.PartitionServer.Processor(handler));
        options.workerThreads(configurator.getNumConcurrentQueries());
        options.selectorThreads(4);
        options.protocolFactory(new TCompactProtocol.Factory());
        options.maxReadBufferBytes = MAX_BUFFER_SIZE;
        dataServer = new TThreadedSelectorServer(options);
        LOG.info("Launching Thrift server.");
        dataServer.serve();
        LOG.info("Thrift server exited.");
        // The Thrift server does not clean up selectors after stopping, which leads to a file descriptor leak.
        // See https://issues.apache.org/jira/browse/THRIFT-2274
        // TODO: when the bug is fixed in Thrift, remove this ugly hack which takes care of the issue
        List<Selector> selectors = getServerSelectors(dataServer);
        closeServerSelectors(selectors);
        // Close the socket
        serverSocket.close();
    } finally {
        // Always shut down the handler
        if (handler != null) {
            LOG.debug("Shutting down Partition Server handler.");
            handler.shutDown();
        }
    }
}

From source file:com.rapleaf.hank.client.TestHankSmartClient.java

License:Apache License

public void testIt() throws Exception {
    // launch server 1
    final PartDaemon.Iface iface1 = new MockPartDaemonHandler(0, VALUE_1);

    TNonblockingServerSocket trans1 = null;
    while (true) {
        try {/*w  w  w .j a  v  a2  s  . c o  m*/
            trans1 = new TNonblockingServerSocket(server1Port);
            LOG.debug("succeeded in binding server 1 to port " + server1Port);
            break;
        } catch (TTransportException e) {
            LOG.debug("failed to bind to port " + server1Port);
            server1Port++;
        }
    }

    Args args = new Args(trans1);
    args.processor(new PartDaemon.Processor(iface1));
    args.protocolFactory(new TCompactProtocol.Factory());
    TServer server1 = new THsHaServer(args);
    Thread thread1 = new Thread(new ServerRunnable(server1), "mock part daemon #1");
    thread1.start();

    // launch server 2;
    final PartDaemon.Iface iface2 = new MockPartDaemonHandler(0, VALUE_2);

    server2Port = server1Port + 1;
    TNonblockingServerSocket trans2 = null;
    while (true) {
        try {
            trans2 = new TNonblockingServerSocket(server2Port);
            LOG.debug("succeeded in binding server 2 to port " + server2Port);
            break;
        } catch (TTransportException e) {
            LOG.debug("failed to bind to port " + server2Port);
            server2Port++;
        }
    }
    args = new Args(trans2);
    args.processor(new PartDaemon.Processor(iface2));
    args.protocolFactory(new TCompactProtocol.Factory());
    final TServer server2 = new THsHaServer(args);
    Thread thread2 = new Thread(new ServerRunnable(server2), "mock part daemon #2");
    thread2.start();

    final Host hostConfig1 = getHostConfig(new PartDaemonAddress("localhost", server1Port), 0);
    final Host hostConfig2 = getHostConfig(new PartDaemonAddress("localhost", server2Port), 1);

    final MockRing mockRingConfig = new MockRing(null, null, 1, RingState.UP) {
        @Override
        public Set<Host> getHostsForDomainPartition(int domainId, int partition) throws IOException {
            assertEquals(1, domainId);
            if (partition == 0) {
                return Collections.singleton(hostConfig1);
            } else if (partition == 1) {
                return Collections.singleton(hostConfig2);
            }
            fail("got partition id " + partition + " which is invalid");
            throw new IllegalStateException();
        }

        @Override
        public Set<Host> getHosts() {
            return new HashSet<Host>(Arrays.asList(hostConfig1, hostConfig2));
        }
    };

    final MockDomain existentDomain = new MockDomain("existent_domain", 2,
            new MapPartitioner(KEY_1, 0, KEY_2, 1), null, null, null);
    MockDomainGroup mockDomainGroupConfig = new MockDomainGroup("myDomainGroup") {
        private final Map<Integer, Domain> domains = new HashMap<Integer, Domain>() {
            {
                put(1, existentDomain);
            }
        };

        @Override
        public Domain getDomain(int domainId) {
            return domains.get(domainId);
        }

        @Override
        public Integer getDomainId(String domainName) {
            if (domainName.equals("existent_domain")) {
                return 1;
            } else {
                return null;
            }
        }

        @Override
        public DomainGroupVersion getLatestVersion() {
            return new MockDomainGroupVersion(
                    new HashSet<DomainGroupVersionDomainVersion>(
                            Arrays.asList(new MockDomainGroupVersionDomainVersion(existentDomain, 1))),
                    this, 1);
        }
    };
    final MockRingGroup mockRingGroupConfig = new MockRingGroup(mockDomainGroupConfig, "myRingGroup", null) {
        @Override
        public Set<Ring> getRings() {
            return Collections.singleton((Ring) mockRingConfig);
        }
    };
    Coordinator mockCoord = new MockCoordinator() {
        @Override
        public RingGroup getRingGroupConfig(String ringGroupName) {
            return mockRingGroupConfig;
        }
    };

    Thread.sleep(1000);

    try {
        HankSmartClient c = new HankSmartClient(mockCoord, "myRingGroup", 1);

        assertEquals(HankResponse.xception(HankExceptions.no_such_domain(true)),
                c.get("nonexistent_domain", null));

        assertEquals(HankResponse.value(VALUE_1), c.get("existent_domain", KEY_1));
        assertEquals(HankResponse.value(VALUE_2), c.get("existent_domain", KEY_2));
    } finally {
        server1.stop();
        server2.stop();
        thread1.join();
        thread2.join();
        trans1.close();
        trans2.close();
    }
}

From source file:com.twitter.common.thrift.ThriftServerTest.java

License:Apache License

@Test
public void testGetServerSocketFor() throws TTransportException {
    TNonblockingServerSocket ephemeralThriftSocket = new TNonblockingServerSocket(0);
    try {/*from   w w  w  . j  ava2  s . com*/
        assertTrue(ThriftServer.getServerSocketFor(ephemeralThriftSocket).getLocalPort() > 0);
    } finally {
        ephemeralThriftSocket.close();
    }
}