use of org.apache.accumulo.core.client.impl.ThriftTransportPool in project accumulo by apache.
the class TransportCachingIT method testCachedTransport.
@Test
public void testCachedTransport() {
Connector conn = getConnector();
Instance instance = conn.getInstance();
ClientConfiguration clientConf = cluster.getClientConfig();
ClientContext context = new ClientContext(instance, new Credentials(getAdminPrincipal(), getAdminToken()), clientConf);
long rpcTimeout = ConfigurationTypeHelper.getTimeInMillis(Property.GENERAL_RPC_TIMEOUT.getDefaultValue());
// create list of servers
ArrayList<ThriftTransportKey> servers = new ArrayList<>();
// add tservers
ZooCache zc = new ZooCacheFactory().getZooCache(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut());
for (String tserver : zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTSERVERS)) {
String path = ZooUtil.getRoot(instance) + Constants.ZTSERVERS + "/" + tserver;
byte[] data = ZooUtil.getLockData(zc, path);
if (data != null) {
String strData = new String(data, UTF_8);
if (!strData.equals("master"))
servers.add(new ThriftTransportKey(new ServerServices(strData).getAddress(Service.TSERV_CLIENT), rpcTimeout, context));
}
}
ThriftTransportPool pool = ThriftTransportPool.getInstance();
TTransport first = null;
while (null == first) {
try {
// Get a transport (cached or not)
first = pool.getAnyTransport(servers, true).getSecond();
} catch (TTransportException e) {
log.warn("Failed to obtain transport to {}", servers);
}
}
assertNotNull(first);
// Return it to unreserve it
pool.returnTransport(first);
TTransport second = null;
while (null == second) {
try {
// Get a cached transport (should be the first)
second = pool.getAnyTransport(servers, true).getSecond();
} catch (TTransportException e) {
log.warn("Failed obtain 2nd transport to {}", servers);
}
}
// We should get the same transport
assertTrue("Expected the first and second to be the same instance", first == second);
// Return the 2nd
pool.returnTransport(second);
TTransport third = null;
while (null == third) {
try {
// Get a non-cached transport
third = pool.getAnyTransport(servers, false).getSecond();
} catch (TTransportException e) {
log.warn("Failed obtain 2nd transport to {}", servers);
}
}
assertFalse("Expected second and third transport to be different instances", second == third);
pool.returnTransport(third);
}
Aggregations