use of org.apache.accumulo.core.clientImpl.ThriftTransportKey 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);
}
}
Aggregations