Search in sources :

Example 1 with ThriftTransportPool

use of org.apache.accumulo.core.clientImpl.ThriftTransportPool in project accumulo by apache.

the class TransportCachingIT method testCachedTransport.

@Test
public void testCachedTransport() throws InterruptedException {
    try (AccumuloClient client = Accumulo.newClient().from(getClientProps()).build()) {
        List<String> tservers;
        while ((tservers = client.instanceOperations().getTabletServers()).isEmpty()) {
            // sleep until a tablet server is up
            Thread.sleep(50);
        }
        ClientContext context = (ClientContext) client;
        long rpcTimeout = ConfigurationTypeHelper.getTimeInMillis(Property.GENERAL_RPC_TIMEOUT.getDefaultValue());
        List<ThriftTransportKey> servers = tservers.stream().map(serverStr -> {
            return new ThriftTransportKey(HostAndPort.fromString(serverStr), rpcTimeout, context);
        }).collect(Collectors.toList());
        // only want to use one server for all subsequent test
        servers = servers.subList(0, 1);
        ThriftTransportPool pool = context.getTransportPool();
        TTransport first = getAnyTransport(servers, pool, true);
        assertNotNull(first);
        // Return it to unreserve it
        pool.returnTransport(first);
        TTransport second = getAnyTransport(servers, pool, true);
        // We should get the same transport
        assertSame("Expected the first and second to be the same instance", first, second);
        pool.returnTransport(second);
        // Ensure does not get cached connection just returned
        TTransport third = getAnyTransport(servers, pool, false);
        assertNotSame("Expected second and third transport to be different instances", second, third);
        TTransport fourth = getAnyTransport(servers, pool, false);
        assertNotSame("Expected third and fourth transport to be different instances", third, fourth);
        pool.returnTransport(third);
        pool.returnTransport(fourth);
        // The following three asserts ensure the per server queue is LIFO
        TTransport fifth = getAnyTransport(servers, pool, true);
        assertSame("Expected fourth and fifth transport to be the same instance", fourth, fifth);
        TTransport sixth = getAnyTransport(servers, pool, true);
        assertSame("Expected third and sixth transport to be the same instance", third, sixth);
        TTransport seventh = getAnyTransport(servers, pool, true);
        assertSame("Expected second and seventh transport to be the same instance", second, seventh);
        pool.returnTransport(fifth);
        pool.returnTransport(sixth);
        pool.returnTransport(seventh);
    }
}
Also used : AccumuloClient(org.apache.accumulo.core.client.AccumuloClient) ThriftTransportKey(org.apache.accumulo.core.clientImpl.ThriftTransportKey) HostAndPort(org.apache.accumulo.core.util.HostAndPort) Logger(org.slf4j.Logger) ClientContext(org.apache.accumulo.core.clientImpl.ClientContext) Assert.assertNotNull(org.junit.Assert.assertNotNull) TTransportException(org.apache.thrift.transport.TTransportException) Assert.assertNotSame(org.junit.Assert.assertNotSame) LoggerFactory(org.slf4j.LoggerFactory) Test(org.junit.Test) Accumulo(org.apache.accumulo.core.client.Accumulo) Collectors(java.util.stream.Collectors) AccumuloClient(org.apache.accumulo.core.client.AccumuloClient) Assert.assertSame(org.junit.Assert.assertSame) AccumuloClusterHarness(org.apache.accumulo.harness.AccumuloClusterHarness) ConfigurationTypeHelper(org.apache.accumulo.core.conf.ConfigurationTypeHelper) List(java.util.List) ThriftTransportPool(org.apache.accumulo.core.clientImpl.ThriftTransportPool) TTransport(org.apache.thrift.transport.TTransport) Property(org.apache.accumulo.core.conf.Property) ClientContext(org.apache.accumulo.core.clientImpl.ClientContext) ThriftTransportKey(org.apache.accumulo.core.clientImpl.ThriftTransportKey) ThriftTransportPool(org.apache.accumulo.core.clientImpl.ThriftTransportPool) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.Test)

Aggregations

List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Accumulo (org.apache.accumulo.core.client.Accumulo)1 AccumuloClient (org.apache.accumulo.core.client.AccumuloClient)1 ClientContext (org.apache.accumulo.core.clientImpl.ClientContext)1 ThriftTransportKey (org.apache.accumulo.core.clientImpl.ThriftTransportKey)1 ThriftTransportPool (org.apache.accumulo.core.clientImpl.ThriftTransportPool)1 ConfigurationTypeHelper (org.apache.accumulo.core.conf.ConfigurationTypeHelper)1 Property (org.apache.accumulo.core.conf.Property)1 HostAndPort (org.apache.accumulo.core.util.HostAndPort)1 AccumuloClusterHarness (org.apache.accumulo.harness.AccumuloClusterHarness)1 TTransport (org.apache.thrift.transport.TTransport)1 TTransportException (org.apache.thrift.transport.TTransportException)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNotSame (org.junit.Assert.assertNotSame)1 Assert.assertSame (org.junit.Assert.assertSame)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1